summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/infrastructure/common-dom-interfaces/collections/htmloptionscollection.html
blob: 130716a9cc70226e89962a2dcc6006b297507464 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<!doctype html>
<title>HTMLOptionsCollection</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/#htmloptionscollection-0">
<select id=a>
  <option>1</option>
  <option>2</option>
  <option>3</option>
<!--Note whitespace is important-->
  <option>4</option>
  <option>5</option>
</select>

<select id=b>
  <option id=b1>1</option>
  <option name=b2>2</option>
  <option id=b3>3</option>
  <option id=b3>4</option>
  <option name=b4>5</option>
  <option name=b4>6</option>
  <option id=b5>7</option>
  <option name=b5>8</option>
  <option id=b6 name=b7>9</option>
  <option id=b6 name=b6>10</option>
  <option id=b8 name=b9>11</option>
</select>

<script>
var a;
var a_opts;
var a_original_innerHTML;
var b;
var b_opts;

setup(function() {
  a = document.getElementById("a");
  a_opts = a.options;
  a_original_innerHTML = a.innerHTML;
  a.innerHTML = a_original_innerHTML;
  b = document.getElementById("b");
  b_opts = b.options;
  b_original_innerHTML = b.innerHTML;
  b.innerHTML = b_original_innerHTML;
})

function assert_values_equals(coll, expected_values, message) {
  actual = [];
  for (var i=0; i<coll.length; i++) {
    actual.push(coll[i].value);
  }
  assert_array_equals(actual, expected_values, message);
}

test(function() {
  assert_equals(5, a_opts.length);
}, "Original length");

test(function() {
  a.innerHTML = a_original_innerHTML;
  a_opts.value = "3";
  a_opts.length = 5;
  assert_equals(a_opts.length, 5);
  assert_equals(a_opts.value, "3");
}, "Setting length to original value has no effect");

test(function() {
  a.innerHTML = a_original_innerHTML;
  a.value = 3;
  a_opts.length = 3;
  assert_equals(3, a_opts.length, "Correct length");
  assert_values_equals(a_opts, ["1","2","3"], "Correct elements remain")
  assert_equals(a_opts.value, "3", "Correct value set");
  assert_equals(a.childNodes.length, 11, "Correct number of child nodes")
}, "Setting length to shorter value");

test(function() {
  a.innerHTML = a_original_innerHTML;
  a.value = 3;
  a_opts.length = 7;
  assert_equals(a_opts.length, 7, "Correct length");
  assert_values_equals(a_opts, ["1","2","3","4","5","",""], "Correct elements inserted")
  assert_equals(a.value, "3", "Correct value set");
  assert_equals(a.childNodes.length, 15, "Correct number of child nodes")
}, "Setting length to longer value");

test(function() {
  a.innerHTML = a_original_innerHTML;
  var newChild = document.createElement("p");
  var newOption = document.createElement("option");
  newOption.textContent = "6";
  newChild.appendChild(newOption);
  a.appendChild(newChild);
  a.value = 3;
  assert_equals(a_opts.length, 5, "Correct length");
  assert_values_equals(a_opts, ["1","2","3","4","5"], "Correct elements inserted")
  assert_equals(a.value, "3", "Correct value set");
}, "Insert <p><option>6</option></p> into <select>");

test(function() {
  a.innerHTML = a_original_innerHTML;
  var newChild = document.createElement("select");
  var newOption = document.createElement("option");
  newOption.textContent = "6";
  newChild.appendChild(newOption);
  a.appendChild(newChild);
  a.value = 3;
  assert_equals(a_opts.length, 5, "Correct length");
  assert_values_equals(a_opts, ["1","2","3","4","5"], "Correct elements inserted")
  assert_equals(a.value, "3", "Correct value set");
}, "Insert <select><option>6</option></select> into <select>");

test(function() {
  //This tests the spec but it is probably wrong here; see bug 12665
  a.innerHTML = a_original_innerHTML;
  var newChild = document.createElement("optgroup");
  var newOption = document.createElement("option");
  newOption.textContent = "6";
  newChild.appendChild(newOption);
  a.appendChild(newChild);
  a.value = 3;
  assert_equals(a_opts.length, 6, "Correct length");
  assert_values_equals(a_opts, ["1","2","3","4","5", "6"], "Correct elements inserted")
  assert_equals(a.value, "3", "Correct value set");
}, "Insert <optgroup><option>6</option></optgroup> into <select>");

test(function() {
  a.innerHTML = a_original_innerHTML;
  var newChild = document.createElement("optgroup");
  var newChild1 = document.createElement("optgroup");
  var newOption = document.createElement("option");
  newOption.textContent = "6";
  newChild.appendChild(newChild1);
  newChild1.appendChild(newOption);
  a.appendChild(newChild);
  a.value = 3;
  assert_equals(a_opts.length, 5, "Correct length");
  assert_values_equals(a_opts, ["1","2","3","4","5"], "Correct elements inserted")
  assert_equals(a.value, "3", "Correct value set");
}, "Insert <optgroup><optgroup><option>6</option></optgroup></optgroup> into <select>");

test(function() {
  assert_equals(b_opts.namedItem("b1").value, "1");
}, "namedItem id attribute");

test(function() {
  assert_equals(b_opts.namedItem("b2").value, "2");
}, "namedItem name attribute");

test(function() {
  assert_equals(b_opts.namedItem("c"), null);
}, "namedItem doesn't match anything");

test(function() {
  assert_equals(b_opts.namedItem("b3").value, "3");
}, "namedItem multiple IDs");

test(function() {
  assert_equals(b_opts.namedItem("b4").value, "5");
}, "namedItem multiple names");

test(function() {
  assert_equals(b_opts.namedItem("b5").value, "7");
}, "namedItem multiple name and ID");

test(function() {
  assert_equals(b_opts.namedItem("b6").value, "9");
}, "namedItem multiple name and ID with multiple attributes");

test(function() {
  assert_equals(b_opts.namedItem("b8").value, "11");
}, "namedItem id attribute multiple attributes one element");

test(function() {
  assert_equals(b_opts.namedItem("b9").value, "11");
}, "namedItem name attribute multiple attributes one element");

test(function() {
  assert_true(b_opts[0] instanceof HTMLOptionElement);
  assert_equals(b_opts[0].innerHTML, "1");
},  "HTMLOptionsCollection [index] method return the item with index");

test(function() {
  assert_true(b_opts["b2"] instanceof HTMLOptionElement);
  assert_equals(b_opts["b2"].innerHTML, "2");
},  "HTMLOptionsCollection [name] method return the item with name");

test(function() {
  assert_true(b_opts.item(0) instanceof HTMLOptionElement);
  assert_equals(b_opts.item(0).innerHTML, "1");
},  "HTMLOptionsCollection.item(index) method return the item with index");

test(function() {
  assert_true(b_opts.item("b2") instanceof HTMLOptionElement);
  assert_equals(b_opts.item("b2").innerHTML, "1");
},  "HTMLOptionsCollection.item(name) method return the item with index 0");

test(function() {
  var b_opts_length = b_opts.length;
  b_opts.add(new Option("2", "2"));
  assert_equals(b_opts[b_opts_length].value, "2");
}, "HTMLOptionsCollection.add method insert HTMLOptionElement Option element");

test(function() {
  var b_opts_length = b_opts.length;
  b_opts.remove(0);
  assert_equals(b_opts.length, b_opts_length - 1);
}, "HTMLOptionsCollection.remove method remove Option element by index");

test(function() {
  var add = document.createElement("p");
  assert_throws_js(TypeError, function() {b_opts.add(add);});
}, "Add non-option to collection");

</script>
<div id=log></div>