summaryrefslogtreecommitdiffstats
path: root/dom/html/test/forms/test_select_selectedOptions.html
blob: 745e0ba4f39b12998a5b9b62bd0f122e442a5d49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=596681
-->
<head>
  <title>Test for HTMLSelectElement.selectedOptions</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=596681">Mozilla Bug 596681</a>
<p id="display"></p>
<pre id="test">
<script type="application/javascript">

/** Test for HTMLSelectElement's selectedOptions attribute.
 *
 * selectedOptions is a live list of the options that have selectedness of true
 * (not the selected content attribute).
 *
 * See http://www.whatwg.org/html/#dom-select-selectedoptions
 **/

function checkSelectedOptions(size, elements)
{
  is(selectedOptions.length, size,
     "select should have " + size + " selected options");
  for (let i = 0; i < size; ++i) {
    ok(selectedOptions[i], "selected option is valid");
    if (selectedOptions[i]) {
      is(selectedOptions[i].value, elements[i].value, "selected options are correct");
    }
  }
}

let select = document.createElement("select");
document.body.appendChild(select);
let selectedOptions = select.selectedOptions;

ok("selectedOptions" in select,
   "select element should have a selectedOptions IDL attribute");

ok(select.selectedOptions instanceof HTMLCollection,
   "selectedOptions should be an HTMLCollection instance");

let option1 = document.createElement("option");
let option2 = document.createElement("option");
let option3 = document.createElement("option");
option1.id = "option1";
option1.value = "option1";
option2.value = "option2";
option3.value = "option3";

checkSelectedOptions(0, null);

select.add(option1, null);
is(selectedOptions.namedItem("option1").value, "option1", "named getter works");
checkSelectedOptions(1, [option1]);

select.add(option2, null);
checkSelectedOptions(1, [option1]);

select.options[1].selected = true;
checkSelectedOptions(1, [option2]);

select.multiple = true;
checkSelectedOptions(1, [option2]);

select.options[0].selected = true;
checkSelectedOptions(2, [option1, option2]);

option1.selected = false;
// Usinig selected directly on the option should work.
checkSelectedOptions(1, [option2]);

select.remove(1);
select.add(option2, 0);
select.options[0].selected = true;
select.options[1].selected = true;
// Should be in tree order.
checkSelectedOptions(2, [option2, option1]);

select.add(option3, null);
checkSelectedOptions(2, [option2, option1]);

select.options[2].selected = true;
checkSelectedOptions(3, [option2, option1, option3]);

select.length = 0;
option1.selected = false;
option2.selected = false;
option3.selected = false;
var optgroup1 = document.createElement("optgroup");
optgroup1.appendChild(option1);
optgroup1.appendChild(option2);
select.add(optgroup1)
var optgroup2 = document.createElement("optgroup");
optgroup2.appendChild(option3);
select.add(optgroup2);

checkSelectedOptions(0, null);

option2.selected = true;
checkSelectedOptions(1, [option2]);

option3.selected = true;
checkSelectedOptions(2, [option2, option3]);

optgroup1.removeChild(option2);
checkSelectedOptions(1, [option3]);

document.body.removeChild(select);
option1.selected = true;
checkSelectedOptions(2, [option1, option3]);
</script>
</pre>
</body>
</html>