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