6 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
7 <title>Tests for document.all
</title>
8 <script type=
"text/javascript" src=
"/MochiKit/MochiKit.js"></script>
9 <script type=
"text/javascript" src=
"/tests/SimpleTest/SimpleTest.js"></script>
10 <link rel=
"stylesheet" type=
"text/css" href=
"/tests/SimpleTest/test.css" />
13 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=259332">Mozilla Bug
259332</a>
14 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=393629">Mozilla Bug
393629</a>
15 <a target=
"_blank" href=
"https://bugzilla.mozilla.org/show_bug.cgi?id=448904">Mozilla Bug
448904</a>
18 <div id=
"content" style=
"display: none">
27 <script class=
"testbody" type=
"text/javascript">
29 p = document.getElementById(
"content");
31 // Test that several elements with the same id or name behave correctly
32 function testNumSame() {
33 is(document.all.id0, null,
"no ids");
34 is(document.all.id1, p.children[
0],
"one id");
35 is(document.all.id2[
0], p.children[
1],
"two ids");
36 is(document.all.id2[
1], p.children[
2],
"two ids");
37 is(document.all.id2.length,
2,
"two length");
38 is(document.all.id3[
0], p.children[
3],
"three ids");
39 is(document.all.id3[
1], p.children[
4],
"three ids");
40 is(document.all.id3[
2], p.children[
5],
"three ids");
41 is(document.all.id3.length,
3,
"three length");
44 p.innerHTML = p.innerHTML.replace(
"id=",
"name=",
"g");
48 // Test that dynamic changes behave properly
50 // Add two elements and check that they are added to the correct lists
51 child = Array.prototype.slice.call(p.children);
52 child[
6] = document.createElement(
"a");
54 p.appendChild(child[
6]);
55 child[
7] = document.createElement(
"a");
57 p.appendChild(child[
7]);
58 is(document.all.id0, child[
6],
"now one id");
59 is(document.all.id1[
0], child[
0],
"now two ids");
60 is(document.all.id1[
1], child[
7],
"now two ids");
61 is(document.all.id1.length,
2,
"now two length");
63 // Remove and element and check that the list shrinks
65 is(document.all.id2, child[
2],
"now just one id");
67 // Change an id and check that its removed and added to the correct lists
68 child[
4].name =
"id1";
69 is(document.all.id1[
0], child[
0],
"now three ids");
70 is(document.all.id1[
1], child[
4],
"now three ids");
71 is(document.all.id1[
2], child[
7],
"now three ids");
72 is(document.all.id1.length,
3,
"now three length");
73 is(document.all.id3[
1], child[
5],
"now just two ids");
74 is(document.all.id3.length,
2,
"now two length");
76 // Remove all elements from a list and check that it goes empty
77 id3list = document.all.id3;
79 is(id3list.length,
1,
"now one length");
81 is(document.all.id3, null,
"now none");
82 is(id3list.length,
0,
"now none length");
84 // Give an element both a name and id and check that it appears in two lists
85 p.insertBefore(child[
1], child[
2]); // restore previously removed
86 id1list = document.all.id1;
87 id2list = document.all.id2;
89 is(id1list[
0], child[
0],
"now four ids");
90 is(id1list[
1], child[
1],
"now four ids");
91 is(id1list[
2], child[
4],
"now four ids");
92 is(id1list[
3], child[
7],
"now four ids");
93 is(id1list.length,
4,
"now four length");
94 is(id2list[
0], child[
1],
"still two ids");
95 is(id2list[
1], child[
2],
"still two ids");
96 is(id2list.length,
2,
"still two length");
99 // Check that document.all behaves list a list of all elements
100 allElems = document.getElementsByTagName(
"*");
101 ok(testArraysSame(document.all, allElems),
"arrays same");
102 length = document.all.length;
103 expectedLength = length + p.getElementsByTagName(
"*").length +
1;
104 p.appendChild(p.cloneNode(true));
105 ok(testArraysSame(document.all, allElems),
"arrays still same");
106 is(document.all.length, expectedLength,
"grew correctly");
108 // Check which elements the 'name' attribute works on
110 ['applet','abbr','acronym','address','area','a','b','base','basefont',
111 'bgsound','big','blink','blockquote','br','canvas','center','cite','code',
112 'col','colgroup','dd','del','dfn','dir','div','dir','dl','dt','em','embed',
113 'fieldset','font','form','frame','frameset','head','i','iframe','img',
114 'input','ins','isindex','kbd','keygen','label','li','legend','link','menu',
115 'multicol','noscript','noframes','object','spacer','table','td','td','th',
116 'thead','tfoot','tr','textarea','select','option','spacer','param',
117 'marquee','hr','title','hx','tt','u','ul','var','wbr','sub','sup','cite',
118 'code','q','nobr','ol','p','pre','s','samp','small','body','html','map',
119 'bdo','legend','listing','style','script','tbody','caption','meta',
120 'optgroup','button','span','strike','strong','td'].sort();
122 ['applet','a','embed','form','iframe','img','input','object','textarea',
123 'select','map','meta','button'].sort();
125 elementNames.forEach(function (name) {
126 nameval = 'namefor' + name;
128 e = document.createElement(name);
130 e.setAttribute('name', nameval);
132 if (name == hasName[
0]) {
133 is(document.all[nameval], e,
"should have name");
137 is(document.all[nameval], null,
"shouldn't have name");
140 is(hasName.length,
0,
"found all names");
144 node.parentNode.removeChild(node);
146 function testArraysSame(a1, a2) {
147 return Array.prototype.every.call(a1, function(e, index) {
148 return a2[index] === e;
149 }) && a1.length == a2.length;