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