content/html/document/test/test_documentAll.html

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial