<html>
<head>
<script src="../../resources/js-test-pre.js"></script>
<script>
var elem;
var ns = "http://not-html.test";

function testDocumentProperty(attributeName, elementName, base) {
  var elem;
  if (typeof base == 'undefined')
    base = 0;

  elem = document.createElementNS(ns, elementName);
  document.body.appendChild(elem);
  shouldBe("document." + attributeName + ".length", base + "");
  document.body.removeChild(elem);

  elem = document.createElement(elementName);
  document.body.appendChild(elem);
  shouldBe("document." + attributeName + ".length", base + 1 + "");
  document.body.removeChild(elem);
}

function testDocumentPropertyWithAttribute(attributeName, elementName, elementAttributeName, base) {
  var elem;
  if (typeof base == 'undefined')
    base = 0;

  elem = document.createElementNS(ns, elementName);
  elem.setAttribute(elementAttributeName, "foo");
  document.body.appendChild(elem);
  shouldBe("document." + attributeName + ".length", base + "");
  document.body.removeChild(elem);

  elem = document.createElement(elementName);
  elem.setAttribute(elementAttributeName, "foo");
  document.body.appendChild(elem);
  shouldBe("document." + attributeName + ".length", base + 1 + "");
  document.body.removeChild(elem);
}

function testElementProperty(elementName, attributeName, subelementName, base) {
  var subelem;
  if (typeof base == 'undefined')
    base = 0;

  elem = document.createElement(elementName);
  subelem = document.createElementNS(ns, subelementName);
  elem.appendChild(subelem);
  shouldBe("elem." + attributeName + ".length", base + "");
  elem.removeChild(subelem);

  subelem = document.createElement(subelementName);
  elem.appendChild(subelem);
  shouldBe("elem." + attributeName + ".length", base + 1 + "");
  elem.removeChild(subelem);
}

function runTest() {
  if (window.testRunner)
    window.testRunner.dumpAsText();

  description('Tests that HTMLCollection only properly contains HTML elements');

  var elem;
  select = document.createElement("select");
  elem = document.createElementNS(ns, "option");
  select.appendChild(elem);
  shouldBe("select.options.length", "0");
  shouldBe("select.selectedOptions.length", "0");

  elem = document.createElement("option");
  select.appendChild(elem);
  shouldBe("select.options.length", "1");

  testDocumentProperty("images", "img");
  testDocumentProperty("forms", "form");
  testDocumentProperty("embeds", "embed");

  // Note that this is run before the final script element on this page is inserted
  testDocumentProperty("scripts", "script", 3);

  testDocumentPropertyWithAttribute("links", "a", "href");
  testDocumentPropertyWithAttribute("links", "area", "href");
  testDocumentPropertyWithAttribute("anchors", "a", "name");

  testElementProperty("map", "areas", "area");
  testElementProperty("table", "rows", "tr");
  testElementProperty("table", "tBodies", "tbody");
  testElementProperty("tr", "cells", "td");
  testElementProperty("thead", "rows", "tr");
  testElementProperty("tbody", "rows", "tr");
  testElementProperty("tfoot", "rows", "tr");
}
</script>
</head>
<body>
  <script>runTest();</script>
  <script src="../../resources/js-test-post.js"></script>
</body>
</html>
