2 "This test checks the behavior of the add() method on the select.options object.<br>" +
3 "It covers both the the one-argument (1.x) and two-argument (2.x) signatures of the add() method."
6 div
= document
.createElement("div");
7 sel
= document
.createElement("select");
8 sel
.setAttribute("id", "select1");
10 sel
= document
.createElement("select");
11 sel
.setAttribute("id", "select2");
13 document
.body
.insertBefore(div
, document
.getElementById("console").nextSibling
);
15 debug("1.1 Add Option to empty Options");
16 var select1
= document
.getElementById("select1");
17 var option1
= document
.createElement("OPTION");
18 select1
.options
.add(option1
);
20 option1
.textContent
= "A";
21 shouldBe("select1.options.length", "1");
22 shouldBe("select1.selectedIndex", "0");
23 shouldBe("select1.options[0].value", "'1'");
24 shouldBe("select1.options[0].textContent", "'A'");
27 debug("1.2 Add Option to non-empty Options");
28 option1
= document
.createElement("OPTION");
29 select1
.options
.add(option1
);
31 option1
.textContent
= "B";
32 shouldBe("select1.options.length", "2");
33 shouldBe("select1.selectedIndex", "0");
34 shouldBe("select1.options[0].value", "'1'");
35 shouldBe("select1.options[0].textContent", "'A'");
36 shouldBe("select1.options[1].value", "'2'");
37 shouldBe("select1.options[1].textContent", "'B'");
40 debug("1.3 Add Option after setting parameters");
41 option1
= document
.createElement("OPTION");
43 option1
.textContent
= "C";
44 select1
.options
.add(option1
);
45 shouldBe("select1.options.length", "3");
46 shouldBe("select1.selectedIndex", "0");
47 shouldBe("select1.options[0].value", "'1'");
48 shouldBe("select1.options[0].textContent", "'A'");
49 shouldBe("select1.options[1].value", "'2'");
50 shouldBe("select1.options[1].textContent", "'B'");
51 shouldBe("select1.options[2].value", "'3'");
52 shouldBe("select1.options[2].textContent", "'C'");
55 debug("1.4 Add a non-Option element");
56 option1
= document
.createElement("DIV");
57 shouldThrow("select1.options.add(option1)");
58 shouldBe("select1.options.length", "3");
59 shouldBe("select1.selectedIndex", "0");
62 debug("1.5 Add a non-element (string)");
64 shouldThrow("select1.options.add(option1)");
65 shouldBe("select1.options.length", "3");
66 shouldBe("select1.selectedIndex", "0");
69 debug("1.6 Add a non-element (number)");
71 shouldThrow("select1.options.add(option1)");
72 shouldBe("select1.options.length", "3");
73 shouldBe("select1.selectedIndex", "0");
76 debug("1.7 Add a non-element (boolean)");
78 shouldThrow("select1.options.add(option1)");
79 shouldBe("select1.options.length", "3");
80 shouldBe("select1.selectedIndex", "0");
83 debug("1.8 Add undefined");
85 shouldThrow("select1.options.add(option1)");
86 shouldBe("select1.options.length", "3");
87 shouldBe("select1.selectedIndex", "0");
90 debug("1.9 Add null");
92 shouldThrow("select1.options.add(option1)");
93 shouldBe("select1.options.length", "3");
94 shouldBe("select1.selectedIndex", "0");
97 debug("1.10 Add negative infinity");
99 shouldThrow("select1.options.add(option1)");
100 shouldBe("select1.options.length", "3");
101 shouldBe("select1.selectedIndex", "0");
104 debug("1.11 Add NaN");
106 shouldThrow("select1.options.add(option1)");
107 shouldBe("select1.options.length", "3");
108 shouldBe("select1.selectedIndex", "0");
111 debug("1.12 Add positive infinity");
113 shouldThrow("select1.options.add(option1)");
114 shouldBe("select1.options.length", "3");
115 shouldBe("select1.selectedIndex", "0");
118 debug("2.1 Add Option to empty Options");
119 var select2
= document
.getElementById("select2");
120 var option2
= document
.createElement("OPTION");
121 select2
.options
.add(option2
, 0);
123 option2
.textContent
= "A";
124 shouldBe("select2.options.length", "1");
125 shouldBe("select2.selectedIndex", "0");
126 shouldBe("select2.options[0].value", "'1'");
127 shouldBe("select2.options[0].textContent", "'A'");
130 debug("2.2 Add Option after setting parameters");
131 option2
= document
.createElement("OPTION");
133 option2
.textContent
= "B";
134 select2
.options
.add(option2
, 1);
135 shouldBe("select2.options.length", "2");
136 shouldBe("select2.selectedIndex", "0");
137 shouldBe("select2.options[0].value", "'1'");
138 shouldBe("select2.options[0].textContent", "'A'");
139 shouldBe("select2.options[1].value", "'2'");
140 shouldBe("select2.options[1].textContent", "'B'");
143 debug("2.3 Insert Option at beginning of Options");
144 option2
= document
.createElement("OPTION");
145 select2
.options
.add(option2
, 0);
147 option2
.textContent
= "Z";
148 shouldBe("select2.options.length", "3");
149 shouldBe("select2.selectedIndex", "1");
150 shouldBe("select2.options[0].value", "'0'");
151 shouldBe("select2.options[0].textContent", "'Z'");
152 shouldBe("select2.options[1].value", "'1'");
153 shouldBe("select2.options[1].textContent", "'A'");
154 shouldBe("select2.options[2].value", "'2'");
155 shouldBe("select2.options[2].textContent", "'B'");
158 debug("2.4 Insert Option in middle of Options");
159 option2
= document
.createElement("OPTION");
160 select2
.options
.add(option2
, 2);
161 option2
.value
= "1.5";
162 option2
.textContent
= "A.5";
163 shouldBe("select2.options.length", "4");
164 shouldBe("select2.selectedIndex", "1");
165 shouldBe("select2.options[0].value", "'0'");
166 shouldBe("select2.options[0].textContent", "'Z'");
167 shouldBe("select2.options[1].value", "'1'");
168 shouldBe("select2.options[1].textContent", "'A'");
169 shouldBe("select2.options[2].value", "'1.5'");
170 shouldBe("select2.options[2].textContent", "'A.5'");
171 shouldBe("select2.options[3].value", "'2'");
172 shouldBe("select2.options[3].textContent", "'B'");
175 debug("2.5 Insert Option at end of Options");
176 option2
= document
.createElement("OPTION");
177 select2
.options
.add(option2
, 4);
179 option2
.textContent
= "C";
180 shouldBe("select2.options.length", "5");
181 shouldBe("select2.selectedIndex", "1");
182 shouldBe("select2.options[0].value", "'0'");
183 shouldBe("select2.options[0].textContent", "'Z'");
184 shouldBe("select2.options[1].value", "'1'");
185 shouldBe("select2.options[1].textContent", "'A'");
186 shouldBe("select2.options[2].value", "'1.5'");
187 shouldBe("select2.options[2].textContent", "'A.5'");
188 shouldBe("select2.options[3].value", "'2'");
189 shouldBe("select2.options[3].textContent", "'B'");
190 shouldBe("select2.options[4].value", "'3'");
191 shouldBe("select2.options[4].textContent", "'C'");
194 debug("2.6 Insert Option beyond the end of Options");
195 option2
= document
.createElement("OPTION");
196 select2
.options
.add(option2
, 6);
198 option2
.textContent
= "D";
199 shouldBe("select2.options.length", "6");
200 shouldBe("select2.selectedIndex", "1");
201 shouldBe("select2.options[0].value", "'0'");
202 shouldBe("select2.options[0].textContent", "'Z'");
203 shouldBe("select2.options[1].value", "'1'");
204 shouldBe("select2.options[1].textContent", "'A'");
205 shouldBe("select2.options[2].value", "'1.5'");
206 shouldBe("select2.options[2].textContent", "'A.5'");
207 shouldBe("select2.options[3].value", "'2'");
208 shouldBe("select2.options[3].textContent", "'B'");
209 shouldBe("select2.options[4].value", "'3'");
210 shouldBe("select2.options[4].textContent", "'C'");
211 shouldBe("select2.options[5].value", "'4'");
212 shouldBe("select2.options[5].textContent", "'D'");
215 debug("2.7 Add an Option at index -1");
216 option2
= document
.createElement("OPTION");
217 select2
.options
.add(option2
, -1);
219 option2
.textContent
= "E";
220 shouldBe("select2.options.length", "7");
221 shouldBe("select2.selectedIndex", "1");
222 shouldBe("select2.options[0].value", "'0'");
223 shouldBe("select2.options[0].textContent", "'Z'");
224 shouldBe("select2.options[1].value", "'1'");
225 shouldBe("select2.options[1].textContent", "'A'");
226 shouldBe("select2.options[2].value", "'1.5'");
227 shouldBe("select2.options[2].textContent", "'A.5'");
228 shouldBe("select2.options[3].value", "'2'");
229 shouldBe("select2.options[3].textContent", "'B'");
230 shouldBe("select2.options[4].value", "'3'");
231 shouldBe("select2.options[4].textContent", "'C'");
232 shouldBe("select2.options[5].value", "'4'");
233 shouldBe("select2.options[5].textContent", "'D'");
234 shouldBe("select2.options[6].value", "'5'");
235 shouldBe("select2.options[6].textContent", "'E'");
238 debug("2.8 Add an Option at index -2");
239 option2
= document
.createElement("OPTION");
240 shouldNotThrow("select2.options.add(option2, -2)");
241 shouldBe("select2.options.length", "8");
242 shouldBe("select2.selectedIndex", "1");
245 debug("2.9 Add an Option at index -Infinity");
246 option2
= document
.createElement("OPTION");
247 shouldNotThrow("select2.options.add(option2, -1/0)");
248 shouldBe("select2.options.length", "9");
249 shouldBe("select2.selectedIndex", "2");
252 debug("2.10 Add an Option at index NaN");
253 option2
= document
.createElement("OPTION");
254 shouldNotThrow("select2.options.add(option2, 0/0)");
255 shouldBe("select2.options.length", "10");
256 shouldBe("select2.selectedIndex", "3");
259 debug("2.11 Add an Option at index Infinity");
260 option2
= document
.createElement("OPTION");
261 shouldNotThrow("select2.options.add(option2, 1/0)");
262 shouldBe("select2.options.length", "11");
263 shouldBe("select2.selectedIndex", "4");
266 debug("2.12 Add a non-Option element");
267 option2
= document
.createElement("DIV");
268 shouldThrow("select2.options.add(option2, 1)");
269 shouldBe("select2.options.length", "11");
270 shouldBe("select2.selectedIndex", "4");
273 debug("2.13 Add a non-element (string)");
275 shouldThrow("select2.options.add(option2, 1)");
276 shouldBe("select2.options.length", "11");
277 shouldBe("select2.selectedIndex", "4");
280 debug("2.14 Add a non-element (number)");
282 shouldThrow("select2.options.add(option2, 1)");
283 shouldBe("select2.options.length", "11");
284 shouldBe("select2.selectedIndex", "4");
287 debug("2.15 Add a non-element (boolean)");
289 shouldThrow("select2.options.add(option2, 1)");
290 shouldBe("select2.options.length", "11");
291 shouldBe("select2.selectedIndex", "4");
294 debug("2.16 Add undefined");
296 shouldThrow("select2.options.add(option2, 1)");
297 shouldBe("select2.options.length", "11");
298 shouldBe("select2.selectedIndex", "4");
301 debug("2.17 Add null");
303 shouldThrow("select2.options.add(option2, 1)");
304 shouldBe("select2.options.length", "11");
305 shouldBe("select2.selectedIndex", "4");
308 debug("2.18 Add negative infinity");
310 shouldThrow("select2.options.add(option2, 1)");
311 shouldBe("select2.options.length", "11");
312 shouldBe("select2.selectedIndex", "4");
315 debug("2.19 Add NaN");
317 shouldThrow("select2.options.add(option2, 1)");
318 shouldBe("select2.options.length", "11");
319 shouldBe("select2.selectedIndex", "4");
322 debug("2.20 Add positive infinity");
324 shouldThrow("select2.options.add(option2, 1)");
325 shouldBe("select2.options.length", "11");
326 shouldBe("select2.selectedIndex", "4");