file_bug357450.js (2485B)
1 /** Test for Bug 357450 */ 2 3 SimpleTest.waitForExplicitFinish(); 4 5 function testGetElements(root, classtestCount) { 6 ok(root.getElementsByClassName, "getElementsByClassName exists"); 7 is( 8 typeof root.getElementsByClassName, 9 "function", 10 "getElementsByClassName is a function" 11 ); 12 13 var nodes = root.getElementsByClassName("f"); 14 15 is(typeof nodes.item, "function"); 16 is(typeof nodes.length, "number"); 17 is(nodes.length, 0, "string with no matching class should get an empty list"); 18 19 nodes = root.getElementsByClassName("foo"); 20 ok(nodes, "should have nodelist object"); 21 22 // HTML5 says ints are allowed in class names 23 // should match int class 24 25 nodes = root.getElementsByClassName("1"); 26 is(nodes[0], $("int-class"), "match integer class name"); 27 nodes = root.getElementsByClassName([1]); 28 is(nodes[0], $("int-class"), "match integer class name 2"); 29 nodes = root.getElementsByClassName(["1 junk"]); 30 31 is(nodes.length, 0, "two classes, but no elements have both"); 32 33 nodes = root.getElementsByClassName("test1"); 34 is(nodes[0], $("test1"), "Id and class name turn up the same node"); 35 nodes = root.getElementsByClassName("test1 test2"); 36 is(nodes.length, 0, "two classes, but no elements have both"); 37 38 // WHATWG examples 39 nodes = document.getElementById("example").getElementsByClassName("aaa"); 40 is(nodes.length, 2, "returns 2 elements"); 41 42 nodes = document.getElementById("example").getElementsByClassName("ccc bbb"); 43 is( 44 nodes.length, 45 1, 46 "only match elements that have all the classes specified in that array. tokenize string arg." 47 ); 48 is(nodes[0], $("p3"), "matched tokenized string"); 49 50 nodes = document.getElementById("example").getElementsByClassName(""); 51 is(nodes.length, 0, "class name with empty string shouldn't return nodes"); 52 53 nodes = root.getElementsByClassName({}); 54 ok(nodes, "bogus arg shouldn't be null"); 55 is(typeof nodes.item, "function"); 56 is(typeof nodes.length, "number"); 57 is(nodes.length, 0, "bogus arg should get an empty nodelist"); 58 } 59 60 addLoadEvent(function () { 61 if (document.getElementsByName) { 62 var anchorNodes = document.getElementsByName("nametest"); 63 is(anchorNodes.length, 1, "getElementsByName still works"); 64 is( 65 anchorNodes[0].getAttribute("name"), 66 "nametest", 67 "getElementsByName still works" 68 ); 69 } 70 testGetElements($("content"), 1); 71 testGetElements(document.documentElement, 3); 72 testGetElements(document, 3); 73 }); 74 addLoadEvent(SimpleTest.finish);