author L. David Baron <>
Wed, 06 May 2009 13:46:04 -0700
changeset 28048 8f7b73d88f6bbedec99c6c8ef48b243ab922d05c
parent 18404 2062b191c344f5ca4801a21eb432d11818200209
child 40996 3a2552c83446c10f4315ff27940be5d9d45b827e
permissions -rw-r--r--
Switch HTML mochitests from using MochiKit.js to packed.js. (Bug 490955) r=sayrer

Tests for document.all
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Tests for document.all</title>
  <script type="text/javascript" src="/MochiKit/packed.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>        
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
<a target="_blank" href="">Mozilla Bug 259332</a>
<a target="_blank" href="">Mozilla Bug 393629</a>
<a target="_blank" href="">Mozilla Bug 448904</a>
<p id="display">
<div id="content" style="display: none">
  <a id="id1">A</a>
  <a id="id2">B</a>
  <a id="id2">C</a>
  <a id="id3">D</a>
  <a id="id3">E</a>
  <a id="id3">F</a>
<pre id="test">
<script class="testbody" type="text/javascript">

p = document.getElementById("content");

// Test that several elements with the same id or name behave correctly
function testNumSame() {
  is(document.all.id0, null, "no ids");
  is(document.all.id1, p.children[0], "one id");
  is(document.all.id2[0], p.children[1], "two ids");
  is(document.all.id2[1], p.children[2], "two ids");
  is(document.all.id2.length, 2, "two length");
  is(document.all.id3[0], p.children[3], "three ids");
  is(document.all.id3[1], p.children[4], "three ids");
  is(document.all.id3[2], p.children[5], "three ids");
  is(document.all.id3.length, 3, "three length");
p.innerHTML = p.innerHTML.replace("id=", "name=", "g");

// Test that dynamic changes behave properly

// Add two elements and check that they are added to the correct lists
child =;
child[6] = document.createElement("a");
child[6].id = "id0";
child[7] = document.createElement("a");
child[7].id = "id1";
is(document.all.id0, child[6], "now one id");
is(document.all.id1[0], child[0], "now two ids");
is(document.all.id1[1], child[7], "now two ids");
is(document.all.id1.length, 2, "now two length");

// Remove and element and check that the list shrinks
is(document.all.id2, child[2], "now just one id");

// Change an id and check that its removed and added to the correct lists
child[4].name = "id1";
is(document.all.id1[0], child[0], "now three ids");
is(document.all.id1[1], child[4], "now three ids");
is(document.all.id1[2], child[7], "now three ids");
is(document.all.id1.length, 3, "now three length");
is(document.all.id3[1], child[5], "now just two ids");
is(document.all.id3.length, 2, "now two length");

// Remove all elements from a list and check that it goes empty
id3list = document.all.id3;
is(id3list.length, 1, "now one length");
is(document.all.id3, null, "now none");
is(id3list.length, 0, "now none length");

// Give an element both a name and id and check that it appears in two lists
p.insertBefore(child[1], child[2]); // restore previously removed
id1list = document.all.id1;
id2list = document.all.id2;
child[1].id = "id1";
is(id1list[0], child[0], "now four ids");
is(id1list[1], child[1], "now four ids");
is(id1list[2], child[4], "now four ids");
is(id1list[3], child[7], "now four ids");
is(id1list.length, 4, "now four length");
is(id2list[0], child[1], "still two ids");
is(id2list[1], child[2], "still two ids");
is(id2list.length, 2, "still two length");

// Check that document.all behaves list a list of all elements
allElems = document.getElementsByTagName("*");
ok(testArraysSame(document.all, allElems), "arrays same");
length = document.all.length;
expectedLength = length + p.getElementsByTagName("*").length + 1;
ok(testArraysSame(document.all, allElems), "arrays still same");
is(document.all.length, expectedLength, "grew correctly");

// Check which elements the 'name' attribute works on
var elementNames =
var hasName =

elementNames.forEach(function (name) {
  nameval = 'namefor' + name;

  e = document.createElement(name);
  e.setAttribute('name', nameval);

  if (name == hasName[0]) {
    is(document.all[nameval], e, "should have name");
  else {
    is(document.all[nameval], null, "shouldn't have name");
is(hasName.length, 0, "found all names");

// Utility functions
function rC(node) {
function testArraysSame(a1, a2) {
  return, function(e, index) {
    return a2[index] === e;
  }) && a1.length == a2.length;