michael@0: /** Test for Bug 357450 **/ michael@0: michael@0: SimpleTest.waitForExplicitFinish(); michael@0: michael@0: function testGetElements (root, classtestCount) { michael@0: michael@0: ok(root.getElementsByClassName, "getElementsByClassName exists"); michael@0: is(typeof(root.getElementsByClassName), "function", "getElementsByClassName is a function"); michael@0: michael@0: var nodes = root.getElementsByClassName("f"); michael@0: michael@0: is(typeof(nodes.item), "function"); michael@0: is(typeof(nodes.length), "number"); michael@0: is(nodes.length, 0, "string with no matching class should get an empty list"); michael@0: michael@0: nodes = root.getElementsByClassName("foo"); michael@0: ok(nodes, "should have nodelist object"); michael@0: michael@0: // HTML5 says ints are allowed in class names michael@0: // should match int class michael@0: michael@0: nodes = root.getElementsByClassName("1"); michael@0: is(nodes[0], $('int-class'), "match integer class name"); michael@0: nodes = root.getElementsByClassName([1]); michael@0: is(nodes[0], $('int-class'), "match integer class name 2"); michael@0: nodes = root.getElementsByClassName(["1 junk"]); michael@0: michael@0: is(nodes.length, 0, "two classes, but no elements have both"); michael@0: michael@0: nodes = root.getElementsByClassName("test1"); michael@0: is(nodes[0], $('test1'), "Id and class name turn up the same node"); michael@0: nodes = root.getElementsByClassName("test1 test2"); michael@0: is(nodes.length, 0, "two classes, but no elements have both"); michael@0: michael@0: // WHATWG examples michael@0: nodes = document.getElementById('example').getElementsByClassName('aaa'); michael@0: is(nodes.length, 2, "returns 2 elements"); michael@0: michael@0: nodes = document.getElementById('example').getElementsByClassName('ccc bbb') michael@0: is(nodes.length, 1, "only match elements that have all the classes specified in that array. tokenize string arg.") michael@0: is(nodes[0], $('p3'), "matched tokenized string"); michael@0: michael@0: nodes = document.getElementById('example').getElementsByClassName(''); michael@0: is(nodes.length, 0, "class name with empty string shouldn't return nodes"); michael@0: michael@0: nodes = root.getElementsByClassName({}); michael@0: ok(nodes, "bogus arg shouldn't be null"); michael@0: is(typeof(nodes.item), "function"); michael@0: is(typeof(nodes.length), "number"); michael@0: is(nodes.length, 0, "bogus arg should get an empty nodelist"); michael@0: } michael@0: michael@0: addLoadEvent(function() { michael@0: if (document.getElementsByName) { michael@0: var anchorNodes = document.getElementsByName("nametest"); michael@0: is(anchorNodes.length, 1, "getElementsByName still works"); michael@0: is(anchorNodes[0].getAttribute("name"), "nametest", michael@0: "getElementsByName still works"); michael@0: } michael@0: testGetElements($("content"), 1); michael@0: testGetElements(document.documentElement, 3); michael@0: testGetElements(document, 3); michael@0: }); michael@0: addLoadEvent(SimpleTest.finish);