content/html/document/test/test_documentAll.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/html/document/test/test_documentAll.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,164 @@
     1.4 +<html>
     1.5 +<!--
     1.6 +Tests for document.all
     1.7 +-->
     1.8 +<head>
     1.9 +  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    1.10 +  <title>Tests for document.all</title>
    1.11 +  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
    1.12 +  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
    1.13 +</head>
    1.14 +<body>
    1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=259332">Mozilla Bug 259332</a>
    1.16 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=393629">Mozilla Bug 393629</a>
    1.17 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=448904">Mozilla Bug 448904</a>
    1.18 +<p id="display">
    1.19 +</p>
    1.20 +<div id="content" style="display: none">
    1.21 +  <a id="id1">A</a>
    1.22 +  <a id="id2">B</a>
    1.23 +  <a id="id2">C</a>
    1.24 +  <a id="id3">D</a>
    1.25 +  <a id="id3">E</a>
    1.26 +  <a id="id3">F</a>
    1.27 +</div>
    1.28 +<iframe id="subframe" src="data:text/html,<span id='x'></span>"
    1.29 +        style="display: none"></iframe>
    1.30 +<pre id="test">
    1.31 +<script class="testbody" type="text/javascript">
    1.32 +
    1.33 +p = document.getElementById("content");
    1.34 +
    1.35 +// Test that several elements with the same id or name behave correctly
    1.36 +function testNumSame() {
    1.37 +  is(document.all.id0, null, "no ids");
    1.38 +  is(document.all.id1, p.children[0], "one id");
    1.39 +  is(document.all.id2[0], p.children[1], "two ids");
    1.40 +  is(document.all.id2[1], p.children[2], "two ids");
    1.41 +  is(document.all.id2.length, 2, "two length");
    1.42 +  is(document.all.id3[0], p.children[3], "three ids");
    1.43 +  is(document.all.id3[1], p.children[4], "three ids");
    1.44 +  is(document.all.id3[2], p.children[5], "three ids");
    1.45 +  is(document.all.id3.length, 3, "three length");
    1.46 +}
    1.47 +testNumSame();
    1.48 +p.innerHTML = p.innerHTML.replace("id=", "name=", "g");
    1.49 +testNumSame();
    1.50 +
    1.51 +
    1.52 +// Test that dynamic changes behave properly
    1.53 +
    1.54 +// Add two elements and check that they are added to the correct lists
    1.55 +child = Array.prototype.slice.call(p.children);
    1.56 +child[6] = document.createElement("a");
    1.57 +child[6].id = "id0";
    1.58 +p.appendChild(child[6]);
    1.59 +child[7] = document.createElement("a");
    1.60 +child[7].id = "id1";
    1.61 +p.appendChild(child[7]);
    1.62 +is(document.all.id0, child[6], "now one id");
    1.63 +is(document.all.id1[0], child[0], "now two ids");
    1.64 +is(document.all.id1[1], child[7], "now two ids");
    1.65 +is(document.all.id1.length, 2, "now two length");
    1.66 +
    1.67 +// Remove and element and check that the list shrinks
    1.68 +rC(child[1]);
    1.69 +is(document.all.id2, child[2], "now just one id");
    1.70 +
    1.71 +// Change an id and check that its removed and added to the correct lists
    1.72 +child[4].name = "id1";
    1.73 +is(document.all.id1[0], child[0], "now three ids");
    1.74 +is(document.all.id1[1], child[4], "now three ids");
    1.75 +is(document.all.id1[2], child[7], "now three ids");
    1.76 +is(document.all.id1.length, 3, "now three length");
    1.77 +is(document.all.id3[1], child[5], "now just two ids");
    1.78 +is(document.all.id3.length, 2, "now two length");
    1.79 +
    1.80 +// Remove all elements from a list and check that it goes empty
    1.81 +id3list = document.all.id3;
    1.82 +rC(child[3]);
    1.83 +is(id3list.length, 1, "now one length");
    1.84 +rC(child[5]);
    1.85 +is(document.all.id3, null, "now none");
    1.86 +is(id3list.length, 0, "now none length");
    1.87 +
    1.88 +// Give an element both a name and id and check that it appears in two lists
    1.89 +p.insertBefore(child[1], child[2]); // restore previously removed
    1.90 +id1list = document.all.id1;
    1.91 +id2list = document.all.id2;
    1.92 +child[1].id = "id1";
    1.93 +is(id1list[0], child[0], "now four ids");
    1.94 +is(id1list[1], child[1], "now four ids");
    1.95 +is(id1list[2], child[4], "now four ids");
    1.96 +is(id1list[3], child[7], "now four ids");
    1.97 +is(id1list.length, 4, "now four length");
    1.98 +is(id2list[0], child[1], "still two ids");
    1.99 +is(id2list[1], child[2], "still two ids");
   1.100 +is(id2list.length, 2, "still two length");
   1.101 +
   1.102 +
   1.103 +// Check that document.all behaves list a list of all elements
   1.104 +allElems = document.getElementsByTagName("*");
   1.105 +ok(testArraysSame(document.all, allElems), "arrays same");
   1.106 +length = document.all.length;
   1.107 +expectedLength = length + p.getElementsByTagName("*").length + 1;
   1.108 +p.appendChild(p.cloneNode(true));
   1.109 +ok(testArraysSame(document.all, allElems), "arrays still same");
   1.110 +is(document.all.length, expectedLength, "grew correctly");
   1.111 +
   1.112 +// Check which elements the 'name' attribute works on
   1.113 +var elementNames =
   1.114 +  ['applet','abbr','acronym','address','area','a','b','base',
   1.115 +   'bgsound','big','blockquote','br','canvas','center','cite','code',
   1.116 +   'col','colgroup','dd','del','dfn','dir','div','dir','dl','dt','em','embed',
   1.117 +   'fieldset','font','form','frame','frameset','head','i','iframe','img',
   1.118 +   'input','ins','isindex','kbd','keygen','label','li','legend','link','menu',
   1.119 +   'multicol','noscript','noframes','object','spacer','table','td','td','th',
   1.120 +   'thead','tfoot','tr','textarea','select','option','spacer','param',
   1.121 +   'marquee','hr','title','hx','tt','u','ul','var','wbr','sub','sup','cite',
   1.122 +   'code','q','nobr','ol','p','pre','s','samp','small','body','html','map',
   1.123 +   'bdo','legend','listing','style','script','tbody','caption','meta',
   1.124 +   'optgroup','button','span','strike','strong','td'].sort();
   1.125 +var hasName =
   1.126 +  ['applet','a','embed','form','iframe','img','input','object','textarea',
   1.127 +   'select','map','meta','button'].sort();
   1.128 +
   1.129 +elementNames.forEach(function (name) {
   1.130 +  nameval = 'namefor' + name;
   1.131 +
   1.132 +  e = document.createElement(name);
   1.133 +  p.appendChild(e);
   1.134 +  e.setAttribute('name', nameval);
   1.135 +
   1.136 +  if (name == hasName[0]) {
   1.137 +    is(document.all[nameval], e, "should have name");
   1.138 +    hasName.shift();
   1.139 +  }
   1.140 +  else {
   1.141 +    is(document.all[nameval], null, "shouldn't have name");
   1.142 +  }
   1.143 +});
   1.144 +is(hasName.length, 0, "found all names");
   1.145 +
   1.146 +SimpleTest.waitForExplicitFinish();
   1.147 +addLoadEvent(function() {
   1.148 +  var subdoc = $("subframe").contentDocument;
   1.149 +  is(subdoc.all.x, subdoc.body.firstChild,
   1.150 +     "document.all should work in a subdocument");
   1.151 +  SimpleTest.finish();
   1.152 +});
   1.153 +
   1.154 +// Utility functions
   1.155 +function rC(node) {
   1.156 +  node.parentNode.removeChild(node);
   1.157 +}
   1.158 +function testArraysSame(a1, a2) {
   1.159 +  return Array.prototype.every.call(a1, function(e, index) {
   1.160 +    return a2[index] === e;
   1.161 +  }) && a1.length == a2.length;
   1.162 +}
   1.163 +</script>
   1.164 +</pre>
   1.165 +</body>
   1.166 +</html>
   1.167 +

mercurial