test_bug596511.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=596511
  5. -->
  6. <head>
  7. <title>Test for Bug 596511</title>
  8. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  10. <style>
  11. select:valid { background-color: green; }
  12. select:invalid { background-color: red; }
  13. </style>
  14. </head>
  15. <body>
  16. <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=596511">Mozilla Bug 596511</a>
  17. <p id="display"></p>
  18. <div id="content" style="display: none">
  19. </div>
  20. <pre id="test">
  21. <script type="application/javascript">
  22. /** Test for Bug 596511 **/
  23. function checkNotSufferingFromBeingMissing(element, aTodo)
  24. {
  25. if (aTodo) {
  26. ok = todo;
  27. is = todo_is;
  28. }
  29. ok(!element.validity.valueMissing,
  30. "Element should not suffer from value missing");
  31. ok(element.validity.valid, "Element should be valid");
  32. ok(element.checkValidity(), "Element should be valid");
  33. is(element.validationMessage, "",
  34. "Validation message should be the empty string");
  35. is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
  36. "rgb(0, 128, 0)", ":valid pseudo-class should apply");
  37. if (aTodo) {
  38. ok = SimpleTest.ok;
  39. is = SimpleTest.is;
  40. }
  41. }
  42. function checkSufferingFromBeingMissing(element, aTodo)
  43. {
  44. if (aTodo) {
  45. ok = todo;
  46. is = todo_is;
  47. }
  48. ok(element.validity.valueMissing, "Element should suffer from value missing");
  49. ok(!element.validity.valid, "Element should not be valid");
  50. ok(!element.checkValidity(), "Element should not be valid");
  51. is(element.validationMessage, "Please select an item in the list.",
  52. "Validation message is wrong");
  53. is(window.getComputedStyle(element, null).getPropertyValue('background-color'),
  54. "rgb(255, 0, 0)", ":invalid pseudo-class should apply");
  55. if (aTodo) {
  56. ok = SimpleTest.ok;
  57. is = SimpleTest.is;
  58. }
  59. }
  60. function checkRequiredAttribute(element)
  61. {
  62. ok('required' in element, "select should have a required attribute");
  63. ok(!element.required, "select required attribute should be disabled");
  64. is(element.getAttribute('required'), null,
  65. "select required attribute should be disabled");
  66. element.required = true;
  67. ok(element.required, "select required attribute should be enabled");
  68. isnot(element.getAttribute('required'), null,
  69. "select required attribute should be enabled");
  70. element.removeAttribute('required');
  71. element.setAttribute('required', '');
  72. ok(element.required, "select required attribute should be enabled");
  73. isnot(element.getAttribute('required'), null,
  74. "select required attribute should be enabled");
  75. element.removeAttribute('required');
  76. ok(!element.required, "select required attribute should be disabled");
  77. is(element.getAttribute('required'), null,
  78. "select required attribute should be disabled");
  79. }
  80. function checkRequiredAndOptionalSelectors(element)
  81. {
  82. is(document.querySelector("select:optional"), element,
  83. "select should be optional");
  84. is(document.querySelector("select:required"), null,
  85. "select shouldn't be required");
  86. element.required = true;
  87. is(document.querySelector("select:optional"), null,
  88. "select shouldn't be optional");
  89. is(document.querySelector("select:required"), element,
  90. "select should be required");
  91. element.required = false;
  92. }
  93. function checkInvalidWhenValueMissing(element)
  94. {
  95. checkNotSufferingFromBeingMissing(select);
  96. element.required = true;
  97. checkSufferingFromBeingMissing(select);
  98. /**
  99. * Non-multiple and size=1.
  100. */
  101. select.appendChild(new Option());
  102. checkSufferingFromBeingMissing(select);
  103. // When removing the required attribute, element should not be invalid.
  104. element.required = false;
  105. checkNotSufferingFromBeingMissing(select);
  106. element.required = true;
  107. select.options[0].textContent = "foo";
  108. // TODO: having that working would require us to add a mutation observer on
  109. // the select element.
  110. checkNotSufferingFromBeingMissing(select, true);
  111. select.remove(0);
  112. checkSufferingFromBeingMissing(select);
  113. select.add(new Option("foo", "foo"), null);
  114. checkNotSufferingFromBeingMissing(select);
  115. select.add(new Option(), null);
  116. checkNotSufferingFromBeingMissing(select);
  117. select.options[1].selected = true;
  118. checkSufferingFromBeingMissing(select);
  119. select.selectedIndex = 0;
  120. checkNotSufferingFromBeingMissing(select);
  121. select.selectedIndex = 1;
  122. checkSufferingFromBeingMissing(select);
  123. select.remove(1);
  124. checkNotSufferingFromBeingMissing(select);
  125. select.options[0].disabled = true;
  126. // TODO: having that working would require us to add a mutation observer on
  127. // the select element.
  128. checkSufferingFromBeingMissing(select, true);
  129. select.options[0].disabled = false
  130. select.remove(0);
  131. checkSufferingFromBeingMissing(select);
  132. var option = new Option("foo", "foo");
  133. option.disabled = true;
  134. select.add(option, null);
  135. select.add(new Option("bar"), null);
  136. option.selected = true;
  137. checkSufferingFromBeingMissing(select);
  138. select.remove(0);
  139. select.remove(0);
  140. /**
  141. * Non-multiple and size > 1.
  142. * Everything should be the same except moving the selection.
  143. */
  144. select.multiple = false;
  145. select.size = 4;
  146. checkSufferingFromBeingMissing(select);
  147. select.add(new Option("", "", true), null);
  148. checkSufferingFromBeingMissing(select);
  149. select.add(new Option("foo", "foo"), null);
  150. select.remove(0);
  151. checkSufferingFromBeingMissing(select);
  152. select.options[0].selected = true;
  153. checkNotSufferingFromBeingMissing(select);
  154. select.remove(0);
  155. /**
  156. * Multiple, any size.
  157. * We can select more than one element and at least needs a value.
  158. */
  159. select.multiple = true;
  160. select.size = 4;
  161. checkSufferingFromBeingMissing(select);
  162. select.add(new Option("", "", true), null);
  163. checkSufferingFromBeingMissing(select);
  164. select.add(new Option("", "", true), null);
  165. checkSufferingFromBeingMissing(select);
  166. select.add(new Option("foo"), null);
  167. checkSufferingFromBeingMissing(select);
  168. select.options[2].selected = true;
  169. checkNotSufferingFromBeingMissing(select);
  170. }
  171. var select = document.createElement("select");
  172. var content = document.getElementById('content');
  173. content.appendChild(select);
  174. checkRequiredAttribute(select);
  175. checkRequiredAndOptionalSelectors(select);
  176. checkInvalidWhenValueMissing(select);
  177. </script>
  178. </pre>
  179. </body>
  180. </html>