test_max_attribute.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=635499
  5. -->
  6. <head>
  7. <title>Test for Bug 635499</title>
  8. <script type="application/javascript" src="/MochiKit/packed.js"></script>
  9. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  10. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  11. </head>
  12. <body>
  13. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=635499">Mozilla Bug 635499</a>
  14. <p id="display"></p>
  15. <div id="content" style="display: none">
  16. </div>
  17. <pre id="test">
  18. <script type="application/javascript">
  19. /** Test for Bug 635499 **/
  20. var data = [
  21. { type: 'hidden', apply: false },
  22. { type: 'text', apply: false },
  23. { type: 'search', apply: false },
  24. { type: 'tel', apply: false },
  25. { type: 'url', apply: false },
  26. { type: 'email', apply: false },
  27. { type: 'password', apply: false },
  28. { type: 'date', apply: true },
  29. { type: 'month', apply: true },
  30. { type: 'week', apply: true },
  31. { type: 'time', apply: true },
  32. { type: 'datetime-local', apply: true },
  33. { type: 'number', apply: true },
  34. { type: 'range', apply: true },
  35. { type: 'color', apply: false },
  36. { type: 'checkbox', apply: false },
  37. { type: 'radio', apply: false },
  38. { type: 'file', apply: false },
  39. { type: 'submit', apply: false },
  40. { type: 'image', apply: false },
  41. { type: 'reset', apply: false },
  42. { type: 'button', apply: false },
  43. ];
  44. var input = document.createElement("input");
  45. document.getElementById('content').appendChild(input);
  46. /**
  47. * @aValidity - boolean indicating whether the element is expected to be valid
  48. * (aElement.validity.valid is true) or not. The value passed is ignored and
  49. * overridden with true if aApply is false.
  50. * @aApply - boolean indicating whether the min/max attributes apply to this
  51. * element type.
  52. * @aRangeApply - A boolean that's set to true if the current input type is a
  53. * "[candidate] for constraint validation" and it "[has] range limitations"
  54. * per http://www.whatwg.org/specs/web-apps/current-work/multipage/selectors.html#selector-in-range
  55. * (in other words, one of the pseudo classes :in-range and :out-of-range
  56. * should apply (which, depends on aValidity)).
  57. * Else (neither :in-range or :out-of-range should match) set to false.
  58. */
  59. function checkValidity(aElement, aValidity, aApply, aRangeApply)
  60. {
  61. aValidity = aApply ? aValidity : true;
  62. is(aElement.validity.valid, aValidity,
  63. "element validity should be " + aValidity);
  64. is(aElement.validity.rangeOverflow, !aValidity,
  65. "element overflow status should be " + !aValidity);
  66. var overflowMsg =
  67. (aElement.type == "date" || aElement.type == "time" ||
  68. aElement.type == "month" || aElement.type == "week" ||
  69. aElement.type == "datetime-local") ?
  70. ("Please select a value that is no later than " + aElement.max + ".") :
  71. ("Please select a value that is no more than " + aElement.max + ".");
  72. is(aElement.validationMessage,
  73. aValidity ? "" : overflowMsg, "Checking range overflow validation message");
  74. is(aElement.matches(":valid"), aElement.willValidate && aValidity,
  75. (aElement.willValidate && aValidity) ? ":valid should apply" : "valid shouldn't apply");
  76. is(aElement.matches(":invalid"), aElement.willValidate && !aValidity,
  77. (aElement.wil && aValidity) ? ":invalid shouldn't apply" : "valid should apply");
  78. if (!aRangeApply) {
  79. ok(!aElement.matches(":in-range"), ":in-range should not match");
  80. ok(!aElement.matches(":out-of-range"),
  81. ":out-of-range should not match");
  82. } else {
  83. is(aElement.matches(":in-range"), aValidity,
  84. ":in-range matches status should be " + aValidity);
  85. is(aElement.matches(":out-of-range"), !aValidity,
  86. ":out-of-range matches status should be " + !aValidity);
  87. }
  88. }
  89. for (var test of data) {
  90. input.type = test.type;
  91. var apply = test.apply;
  92. // The element should be valid. Range should not apply when @min and @max are
  93. // undefined, except if the input type is 'range' (since that type has a
  94. // default minimum and maximum).
  95. if (input.type == 'range') {
  96. checkValidity(input, true, apply, true);
  97. } else {
  98. checkValidity(input, true, apply, false);
  99. }
  100. checkValidity(input, true, apply, test.type == 'range');
  101. switch (input.type) {
  102. case 'hidden':
  103. case 'text':
  104. case 'search':
  105. case 'password':
  106. case 'url':
  107. case 'tel':
  108. case 'email':
  109. case 'number':
  110. case 'checkbox':
  111. case 'radio':
  112. case 'file':
  113. case 'submit':
  114. case 'reset':
  115. case 'button':
  116. case 'image':
  117. case 'color':
  118. input.max = '-1';
  119. break;
  120. case 'date':
  121. input.max = '2012-06-27';
  122. break;
  123. case 'time':
  124. input.max = '02:20';
  125. break;
  126. case 'range':
  127. // range is special, since setting max to -1 will make it invalid since
  128. // it's default would then be 0, meaning it suffers from overflow.
  129. input.max = '-1';
  130. checkValidity(input, false, apply, apply);
  131. // Now make it something that won't cause an error below:
  132. input.max = '10';
  133. break;
  134. case 'month':
  135. input.max = '2016-12';
  136. break;
  137. case 'week':
  138. input.max = '2016-W39';
  139. break;
  140. case 'datetime-local':
  141. input.max = '2016-12-31T23:59:59';
  142. break;
  143. default:
  144. ok(false, 'please, add a case for this new type (' + input.type + ')');
  145. }
  146. checkValidity(input, true, apply, apply);
  147. switch (input.type) {
  148. case 'text':
  149. case 'hidden':
  150. case 'search':
  151. case 'password':
  152. case 'tel':
  153. case 'radio':
  154. case 'checkbox':
  155. case 'reset':
  156. case 'button':
  157. case 'submit':
  158. case 'image':
  159. input.value = '0';
  160. checkValidity(input, true, apply, apply);
  161. break;
  162. case 'url':
  163. input.value = 'http://mozilla.org';
  164. checkValidity(input, true, apply, apply);
  165. break;
  166. case 'email':
  167. input.value = 'foo@bar.com';
  168. checkValidity(input, true, apply, apply);
  169. break;
  170. case 'file':
  171. var file = new File([''], '635499_file');
  172. SpecialPowers.wrap(input).mozSetFileArray([file]);
  173. checkValidity(input, true, apply, apply);
  174. break;
  175. case 'date':
  176. input.max = '2012-06-27';
  177. input.value = '2012-06-26';
  178. checkValidity(input, true, apply, apply);
  179. input.value = '2012-06-27';
  180. checkValidity(input, true, apply, apply);
  181. input.value = 'foo';
  182. checkValidity(input, true, apply, apply);
  183. input.value = '2012-06-28';
  184. checkValidity(input, false, apply, apply);
  185. input.max = '2012-06-30';
  186. checkValidity(input, true, apply, apply);
  187. input.value = '2012-07-05';
  188. checkValidity(input, false, apply, apply);
  189. input.value = '1000-01-01';
  190. checkValidity(input, true, apply, apply);
  191. input.value = '20120-01-01';
  192. checkValidity(input, false, apply, apply);
  193. input.max = '0050-01-01';
  194. checkValidity(input, false, apply, apply);
  195. input.value = '0049-01-01';
  196. checkValidity(input, true, apply, apply);
  197. input.max = '';
  198. checkValidity(input, true, apply, false);
  199. input.max = 'foo';
  200. checkValidity(input, true, apply, false);
  201. break;
  202. case 'number':
  203. input.max = '2';
  204. input.value = '1';
  205. checkValidity(input, true, apply, apply);
  206. input.value = '2';
  207. checkValidity(input, true, apply, apply);
  208. input.value = 'foo';
  209. checkValidity(input, true, apply, apply);
  210. input.value = '3';
  211. checkValidity(input, false, apply, apply);
  212. input.max = '5';
  213. checkValidity(input, true, apply, apply);
  214. input.value = '42';
  215. checkValidity(input, false, apply, apply);
  216. input.max = '';
  217. checkValidity(input, true, apply, false);
  218. input.max = 'foo';
  219. checkValidity(input, true, apply, false);
  220. // Check that we correctly convert input.max to a double in validationMessage.
  221. if (input.type == 'number') {
  222. input.max = "4.333333333333333333333333333333333331";
  223. input.value = "5";
  224. is(input.validationMessage,
  225. "Please select a value that is no more than 4.33333333333333.",
  226. "validation message");
  227. }
  228. break;
  229. case 'range':
  230. input.max = '2';
  231. input.value = '1';
  232. checkValidity(input, true, apply, apply);
  233. input.value = '2';
  234. checkValidity(input, true, apply, apply);
  235. input.value = 'foo';
  236. checkValidity(input, true, apply, apply);
  237. input.value = '3';
  238. checkValidity(input, true, apply, apply);
  239. is(input.value, input.max, "the value should have been set to max");
  240. input.max = '5';
  241. checkValidity(input, true, apply, apply);
  242. input.value = '42';
  243. checkValidity(input, true, apply, apply);
  244. is(input.value, input.max, "the value should have been set to max");
  245. input.max = '';
  246. checkValidity(input, true, apply, apply);
  247. input.max = 'foo';
  248. checkValidity(input, true, apply, apply);
  249. // Check that we correctly convert input.max to a double in validationMessage.
  250. input.step = 'any';
  251. input.min = 5;
  252. input.max = 0.66666666666666666666666666666666666
  253. input.value = 1;
  254. is(input.validationMessage,
  255. "Please select a value that is no more than 0.666666666666667.",
  256. "validation message")
  257. break;
  258. case 'time':
  259. // Don't worry about that.
  260. input.step = 'any';
  261. input.max = '10:10';
  262. input.value = '10:09';
  263. checkValidity(input, true, apply, apply);
  264. input.value = '10:10';
  265. checkValidity(input, true, apply, apply);
  266. input.value = '10:10:00';
  267. checkValidity(input, true, apply, apply);
  268. input.value = '10:10:00.000';
  269. checkValidity(input, true, apply, apply);
  270. input.value = 'foo';
  271. checkValidity(input, true, apply, apply);
  272. input.value = '10:11';
  273. checkValidity(input, false, apply, apply);
  274. input.value = '10:10:00.001';
  275. checkValidity(input, false, apply, apply);
  276. input.max = '01:00:00.01';
  277. input.value = '01:00:00.001';
  278. checkValidity(input, true, apply, apply);
  279. input.value = '01:00:00';
  280. checkValidity(input, true, apply, apply);
  281. input.value = '01:00:00.1';
  282. checkValidity(input, false, apply, apply);
  283. input.max = '';
  284. checkValidity(input, true, apply, false);
  285. input.max = 'foo';
  286. checkValidity(input, true, apply, false);
  287. break;
  288. case 'month':
  289. input.value = '2016-06';
  290. checkValidity(input, true, apply, apply);
  291. input.value = '2016-12';
  292. checkValidity(input, true, apply, apply);
  293. input.value = 'foo';
  294. checkValidity(input, true, apply, apply);
  295. input.value = '2017-01';
  296. checkValidity(input, false, apply, apply);
  297. input.max = '2017-07';
  298. checkValidity(input, true, apply, apply);
  299. input.value = '2017-12';
  300. checkValidity(input, false, apply, apply);
  301. input.value = '1000-01';
  302. checkValidity(input, true, apply, apply);
  303. input.value = '20160-01';
  304. checkValidity(input, false, apply, apply);
  305. input.max = '0050-01';
  306. checkValidity(input, false, apply, apply);
  307. input.value = '0049-12';
  308. checkValidity(input, true, apply, apply);
  309. input.max = '';
  310. checkValidity(input, true, apply, false);
  311. input.max = 'foo';
  312. checkValidity(input, true, apply, false);
  313. break;
  314. case 'week':
  315. input.value = '2016-W01';
  316. checkValidity(input, true, apply, apply);
  317. input.value = '2016-W39';
  318. checkValidity(input, true, apply, apply);
  319. input.value = 'foo';
  320. checkValidity(input, true, apply, apply);
  321. input.value = '2017-W01';
  322. checkValidity(input, false, apply, apply);
  323. input.max = '2017-W01';
  324. checkValidity(input, true, apply, apply);
  325. input.value = '2017-W52';
  326. checkValidity(input, false, apply, apply);
  327. input.value = '1000-W01';
  328. checkValidity(input, true, apply, apply);
  329. input.value = '2100-W01';
  330. checkValidity(input, false, apply, apply);
  331. input.max = '0050-W01';
  332. checkValidity(input, false, apply, apply);
  333. input.value = '0049-W52';
  334. checkValidity(input, true, apply, apply);
  335. input.max = '';
  336. checkValidity(input, true, apply, false);
  337. input.max = 'foo';
  338. checkValidity(input, true, apply, false);
  339. break;
  340. case 'datetime-local':
  341. input.value = '2016-01-01T12:00';
  342. checkValidity(input, true, apply, apply);
  343. input.value = '2016-12-31T23:59:59';
  344. checkValidity(input, true, apply, apply);
  345. input.value = 'foo';
  346. checkValidity(input, true, apply, apply);
  347. input.value = '2016-12-31T23:59:59.123';
  348. checkValidity(input, false, apply, apply);
  349. input.value = '2017-01-01T10:00';
  350. checkValidity(input, false, apply, apply);
  351. input.max = '2017-01-01T10:00';
  352. checkValidity(input, true, apply, apply);
  353. input.value = '2017-01-01T10:00:30';
  354. checkValidity(input, false, apply, apply);
  355. input.value = '1000-01-01T12:00';
  356. checkValidity(input, true, apply, apply);
  357. input.value = '2100-01-01T12:00';
  358. checkValidity(input, false, apply, apply);
  359. input.max = '0050-12-31T23:59:59.999';
  360. checkValidity(input, false, apply, apply);
  361. input.value = '0050-12-31T23:59:59';
  362. checkValidity(input, true, apply, apply);
  363. input.max = '';
  364. checkValidity(input, true, apply, false);
  365. input.max = 'foo';
  366. checkValidity(input, true, apply, false);
  367. break;
  368. }
  369. // Cleaning up,
  370. input.removeAttribute('max');
  371. input.value = '';
  372. }
  373. </script>
  374. </pre>
  375. </body>
  376. </html>