4 <script src=
"../../resources/js-test.js"></script>
5 <div id=
"container"></div>
9 description("This tests verifies that namedItem and named getter returns the first matched item for all but all, options, and form controls collections.");
11 function createElementWithId(name
, id
, attributes
) {
12 var element
= document
.createElement(name
);
14 for (var attr
in attributes
)
15 element
.setAttribute(attr
, attributes
[attr
]);
19 function insertElementWithId(name
, id
, attributes
) {
20 var element
= createElementWithId(name
, id
, attributes
);
21 document
.getElementById('container').appendChild(element
);
25 function removeTestElements() {
26 document
.getElementById('container').innerHTML
= '';
27 document
.querySelector('form').innerHTML
= '';
31 debug('document.all');
32 shouldBeTrue("document.all instanceof HTMLAllCollection");
33 shouldBeTrue("document.all instanceof HTMLCollection");
34 shouldBe("initialLength = document.all.length; elements = [insertElementWithId('b', 'foo'), insertElementWithId('q', 'foo')];\n"
35 + " document.all.length", "initialLength + 2;");
36 shouldBe("document.all['foo'].length", "2");
37 shouldBe("document.all['foo'][0]", "elements[0]");
38 shouldBe("document.all['foo'][1]", "elements[1]");
39 shouldBe("elements[0].parentNode.removeChild(elements[0]); document.all['foo']", 'elements[1]');
40 shouldBeUndefined("document.all['no-such-element']");
43 var form
= document
.querySelector('form');
44 debug("form.elements");
45 shouldBeTrue("form.elements instanceof HTMLFormControlsCollection");
46 shouldBeTrue("form.elements instanceof HTMLCollection");
47 shouldBe("form.elements.length", "0");
48 shouldBe("elements = [createElementWithId('input', 'foo'), createElementWithId('input', 'foo')];\n"
49 + " form.appendChild(elements[0]); form.elements.length", "1");
50 shouldBe("form.elements['foo']", "elements[0]");
51 shouldBe("form.appendChild(elements[1]); form.elements.length", "2");
52 shouldBe("form.elements['foo'].toString()", "'[object RadioNodeList]'");
53 shouldBe("form.elements['foo'].length", "2");
54 shouldBe("form.elements['foo'][0]", "elements[0]");
55 shouldBe("form.elements['foo'][1]", "elements[1]");
56 shouldBe("form.removeChild(elements[0]); form.elements['foo']", "elements[1]");
57 shouldBe("removeTestElements(); form.elements.length", "0");
58 shouldBeUndefined("form.elements['no-such-element']");
61 debug("select.options");
62 shouldBe("form.appendChild(createElementWithId('select', 'bar')); form.elements.length", "1");
63 shouldBeTrue("select = form.elements[0]; select.options instanceof HTMLOptionsCollection");
64 shouldBeTrue("select.options instanceof HTMLCollection");
65 shouldBe("select.options.length", "0");
66 shouldBe("elements = [createElementWithId('option', 'foo'), createElementWithId('option', 'foo')];\n"
67 + " select.appendChild(elements[0]); select.options.length", "1");
68 shouldBe("select.options['foo']", "elements[0]");
69 shouldBe("select.appendChild(elements[1]); select.options.length", "2");
70 shouldBe("select.options['foo'].toString()", "'[object NodeList]'");
71 shouldBe("select.options['foo'].length", "2");
72 shouldBe("select.options['foo'][0]", "elements[0]");
73 shouldBe("select.options['foo'][1]", "elements[1]");
74 shouldBe("select.removeChild(elements[0]); select.options['foo']", "elements[1]");
75 shouldBe("select.innerHTML = ''; select.options.length", "0");
76 shouldBe("removeTestElements(); form.elements.length", "0");
77 shouldBeUndefined("select.options['no-such-element']");
80 function testFirstItemReturnsFirstMatch(collection
, initialLength
, elementNames
, attributes
) {
82 shouldBe(collection
+ ".length", initialLength
.toString());
84 for (var i
= 0; i
< elementNames
.length
; i
++) {
85 var attrs
= attributes
? ", " + JSON
.stringify(attributes
) : '';
86 shouldBe("elements[" + i
+ "] = insertElementWithId('" + elementNames
[i
] + "', 'foo'" + attrs
+ "); "
87 + collection
+ ".length", (initialLength
+ i
+ 1).toString());
89 shouldBe(collection
+ "['foo']", "elements[0]");
90 shouldBe("removeTestElements(); " + collection
+ ".length", initialLength
.toString());
94 testFirstItemReturnsFirstMatch('document.images', 0, ['img', 'img']);
95 testFirstItemReturnsFirstMatch('document.applets', 0, ['object', 'object'], {'type': 'application/x-java-applet'});
96 testFirstItemReturnsFirstMatch('document.embeds', 0, ['embed', 'embed']);
97 testFirstItemReturnsFirstMatch('document.forms', 1, ['form', 'form']);
98 testFirstItemReturnsFirstMatch('document.links', 0, ['a', 'a', 'area'], {'href': 'some url'});
99 testFirstItemReturnsFirstMatch('document.anchors', 0, ['a', 'a'], {'name': 'some name'});
100 testFirstItemReturnsFirstMatch('document.scripts', 2, ['script', 'script']);
102 var successfullyParsed
= true;