test_documentAll.html (6033B)
1 <html> 2 <!-- 3 Tests for document.all 4 --> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>Tests for document.all</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 10 </head> 11 <body> 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=259332">Mozilla Bug 259332</a> 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=393629">Mozilla Bug 393629</a> 14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=448904">Mozilla Bug 448904</a> 15 <p id="display"> 16 </p> 17 <div id="content" style="display: none"> 18 <a id="id1">A</a> 19 <a id="id2">B</a> 20 <a id="id2">C</a> 21 <a id="id3">D</a> 22 <a id="id3">E</a> 23 <a id="id3">F</a> 24 </div> 25 <iframe id="subframe" srcdoc="<span id='x'></span>" 26 style="display: none"></iframe> 27 <pre id="test"> 28 <script class="testbody" type="text/javascript"> 29 30 p = document.getElementById("content"); 31 32 // Test that several elements with the same id or name behave correctly 33 function testNumSame() { 34 is(document.all.id0, undefined, "no ids"); 35 is(document.all.namedItem("id0"), null, "no ids"); 36 is(document.all.id1, p.children[0], "one id"); 37 is(document.all.id2[0], p.children[1], "two ids"); 38 is(document.all.id2[1], p.children[2], "two ids"); 39 is(document.all.id2.length, 2, "two length"); 40 is(document.all.id3[0], p.children[3], "three ids"); 41 is(document.all.id3[1], p.children[4], "three ids"); 42 is(document.all.id3[2], p.children[5], "three ids"); 43 is(document.all.id3.length, 3, "three length"); 44 } 45 testNumSame(); 46 p.innerHTML = p.innerHTML.replace(/id=/g, "name="); 47 testNumSame(); 48 49 50 // Test that dynamic changes behave properly 51 52 // Add two elements and check that they are added to the correct lists 53 child = Array.prototype.slice.call(p.children); 54 child[6] = document.createElement("a"); 55 child[6].id = "id0"; 56 p.appendChild(child[6]); 57 child[7] = document.createElement("a"); 58 child[7].id = "id1"; 59 p.appendChild(child[7]); 60 is(document.all.id0, child[6], "now one id"); 61 is(document.all.id1[0], child[0], "now two ids"); 62 is(document.all.id1[1], child[7], "now two ids"); 63 is(document.all.id1.length, 2, "now two length"); 64 65 // Remove and element and check that the list shrinks 66 rC(child[1]); 67 is(document.all.id2, child[2], "now just one id"); 68 69 // Change an id and check that its removed and added to the correct lists 70 child[4].name = "id1"; 71 is(document.all.id1[0], child[0], "now three ids"); 72 is(document.all.id1[1], child[4], "now three ids"); 73 is(document.all.id1[2], child[7], "now three ids"); 74 is(document.all.id1.length, 3, "now three length"); 75 is(document.all.id3[1], child[5], "now just two ids"); 76 is(document.all.id3.length, 2, "now two length"); 77 78 // Remove all elements from a list and check that it goes empty 79 id3list = document.all.id3; 80 rC(child[3]); 81 is(id3list.length, 1, "now one length"); 82 rC(child[5]); 83 is(document.all.id3, undefined, "now none"); 84 is(document.all.namedItem("id3"), null, "now none (namedItem)"); 85 is(id3list.length, 0, "now none length"); 86 87 // Give an element both a name and id and check that it appears in two lists 88 p.insertBefore(child[1], child[2]); // restore previously removed 89 id1list = document.all.id1; 90 id2list = document.all.id2; 91 child[1].id = "id1"; 92 is(id1list[0], child[0], "now four ids"); 93 is(id1list[1], child[1], "now four ids"); 94 is(id1list[2], child[4], "now four ids"); 95 is(id1list[3], child[7], "now four ids"); 96 is(id1list.length, 4, "now four length"); 97 is(id2list[0], child[1], "still two ids"); 98 is(id2list[1], child[2], "still two ids"); 99 is(id2list.length, 2, "still two length"); 100 101 102 // Check that document.all behaves list a list of all elements 103 allElems = document.getElementsByTagName("*"); 104 ok(testArraysSame(document.all, allElems), "arrays same"); 105 length = document.all.length; 106 expectedLength = length + p.getElementsByTagName("*").length + 1; 107 p.appendChild(p.cloneNode(true)); 108 ok(testArraysSame(document.all, allElems), "arrays still same"); 109 is(document.all.length, expectedLength, "grew correctly"); 110 111 // Check which elements the 'name' attribute works on 112 var elementNames = 113 ['abbr','acronym','address','area','a','b','base', 114 'bgsound','big','blockquote','br','canvas','center','cite','code', 115 'col','colgroup','dd','del','dfn','dir','div','dir','dl','dt','em','embed', 116 'fieldset','font','form','frame','frameset','head','i','iframe','img', 117 'input','ins','isindex','kbd','keygen','label','li','legend','link','menu', 118 'multicol','noscript','noframes','object','spacer','table','td','td','th', 119 'thead','tfoot','tr','textarea','select','option','spacer','param', 120 'marquee','hr','title','hx','tt','u','ul','var','wbr','sub','sup','cite', 121 'code','q','nobr','ol','p','pre','s','samp','small','body','html','map', 122 'bdo','legend','listing','style','script','tbody','caption','meta', 123 'optgroup','button','span','strike','strong','td'].sort(); 124 var hasName = 125 ['a','embed','form','iframe','img','input','object','textarea', 126 'select','map','meta','button','frame','frameset'].sort(); 127 128 elementNames.forEach(function (name) { 129 nameval = 'namefor' + name; 130 131 e = document.createElement(name); 132 p.appendChild(e); 133 e.setAttribute('name', nameval); 134 135 if (name == hasName[0]) { 136 is(document.all[nameval], e, "should have name"); 137 hasName.shift(); 138 } 139 else { 140 is(document.all[nameval], undefined, "shouldn't have name"); 141 is(document.all.namedItem(nameval), null, "shouldn't have name (namedItem)"); 142 } 143 }); 144 is(hasName.length, 0, "found all names"); 145 146 SimpleTest.waitForExplicitFinish(); 147 addLoadEvent(function() { 148 var subdoc = $("subframe").contentDocument; 149 is(subdoc.all.x, subdoc.body.firstChild, 150 "document.all should work in a subdocument"); 151 SimpleTest.finish(); 152 }); 153 154 // Utility functions 155 function rC(node) { 156 node.remove(); 157 } 158 function testArraysSame(a1, a2) { 159 return Array.prototype.every.call(a1, function(e, index) { 160 return a2[index] === e; 161 }) && a1.length == a2.length; 162 } 163 </script> 164 </pre> 165 </body> 166 </html>