test_select_selectedOptions.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <!DOCTYPE HTML>
  2. <html>
  3. <!--
  4. https://bugzilla.mozilla.org/show_bug.cgi?id=596681
  5. -->
  6. <head>
  7. <title>Test for HTMLSelectElement.selectedOptions</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=596681">Mozilla Bug 596681</a>
  14. <p id="display"></p>
  15. <pre id="test">
  16. <script type="application/javascript;version=1.7">
  17. /** Test for HTMLSelectElement's selectedOptions attribute.
  18. *
  19. * selectedOptions is a live list of the options that have selectedness of true
  20. * (not the selected content attribute).
  21. *
  22. * See http://www.whatwg.org/html/#dom-select-selectedoptions
  23. **/
  24. function checkSelectedOptions(size, elements)
  25. {
  26. is(selectedOptions.length, size,
  27. "select should have " + size + " selected options");
  28. for (let i = 0; i < size; ++i) {
  29. ok(selectedOptions[i], "selected option is valid");
  30. if (selectedOptions[i]) {
  31. is(selectedOptions[i].value, elements[i].value, "selected options are correct");
  32. }
  33. }
  34. }
  35. let select = document.createElement("select");
  36. document.body.appendChild(select);
  37. let selectedOptions = select.selectedOptions;
  38. ok("selectedOptions" in select,
  39. "select element should have a selectedOptions IDL attribute");
  40. ok(select.selectedOptions instanceof HTMLCollection,
  41. "selectedOptions should be an HTMLCollection instance");
  42. let option1 = document.createElement("option");
  43. let option2 = document.createElement("option");
  44. let option3 = document.createElement("option");
  45. option1.id = "option1";
  46. option1.value = "option1";
  47. option2.value = "option2";
  48. option3.value = "option3";
  49. checkSelectedOptions(0, null);
  50. select.add(option1, null);
  51. is(selectedOptions.namedItem("option1").value, "option1", "named getter works");
  52. checkSelectedOptions(1, [option1]);
  53. select.add(option2, null);
  54. checkSelectedOptions(1, [option1]);
  55. select.options[1].selected = true;
  56. checkSelectedOptions(1, [option2]);
  57. select.multiple = true;
  58. checkSelectedOptions(1, [option2]);
  59. select.options[0].selected = true;
  60. checkSelectedOptions(2, [option1, option2]);
  61. option1.selected = false;
  62. // Usinig selected directly on the option should work.
  63. checkSelectedOptions(1, [option2]);
  64. select.remove(1);
  65. select.add(option2, 0);
  66. select.options[0].selected = true;
  67. select.options[1].selected = true;
  68. // Should be in tree order.
  69. checkSelectedOptions(2, [option2, option1]);
  70. select.add(option3, null);
  71. checkSelectedOptions(2, [option2, option1]);
  72. select.options[2].selected = true;
  73. checkSelectedOptions(3, [option2, option1, option3]);
  74. select.length = 0;
  75. option1.selected = false;
  76. option2.selected = false;
  77. option3.selected = false;
  78. var optgroup1 = document.createElement("optgroup");
  79. optgroup1.appendChild(option1);
  80. optgroup1.appendChild(option2);
  81. select.add(optgroup1)
  82. var optgroup2 = document.createElement("optgroup");
  83. optgroup2.appendChild(option3);
  84. select.add(optgroup2);
  85. checkSelectedOptions(0, null);
  86. option2.selected = true;
  87. checkSelectedOptions(1, [option2]);
  88. option3.selected = true;
  89. checkSelectedOptions(2, [option2, option3]);
  90. optgroup1.removeChild(option2);
  91. checkSelectedOptions(1, [option3]);
  92. document.body.removeChild(select);
  93. option1.selected = true;
  94. checkSelectedOptions(2, [option1, option3]);
  95. </script>
  96. </pre>
  97. </body>
  98. </html>