4 <title>NodeIterator: Basic test
</title>
5 <script src=
"../../../resources/testharness.js"></script>
6 <script src=
"../../../resources/testharnessreport.js"></script>
7 <link rel=
"stylesheet" href=
"../../../resources/testharness.css">
10 <p>This test checks the basic functionality of NodeIterator.
</p>
12 function createSampleDOM()
14 // Tree order: #a -> "b" -> #c -> #d -> "e" -> #f -> "g" -> <!--h--> -> "i" -> <!--j-->.
15 var div
= document
.createElement('div');
17 div
.innerHTML
= 'b<div id="c"><div id="d">e<span id="f">g<!--h--></span>i</div><!--j--></div>';
21 function check_iterator(iterator
, root
)
23 assert_equals(iterator
.toString(), '[object NodeIterator]');
24 assert_equals(iterator
.root
, root
);
25 assert_equals(iterator
.referenceNode
, root
);
26 assert_equals(iterator
.pointerBeforeReferenceNode
, true);
27 assert_equals(iterator
.whatToShow
, 0xFFFFFFFF);
28 assert_equals(iterator
.filter
, null);
29 assert_readonly(iterator
, 'root');
30 assert_readonly(iterator
, 'referenceNode');
31 assert_readonly(iterator
, 'pointerBeforeReferenceNode');
32 assert_readonly(iterator
, 'whatToShow');
33 assert_readonly(iterator
, 'filter');
34 assert_idl_attribute(iterator
, 'nextNode');
35 assert_idl_attribute(iterator
, 'previousNode');
36 assert_idl_attribute(iterator
, 'detach');
41 var root
= createSampleDOM();
42 var iterator
= document
.createNodeIterator(root
);
43 check_iterator(iterator
, root
);
44 }, 'Construct a NodeIterator by document.createNodeIterator(root).');
48 var root
= createSampleDOM();
49 var iterator
= document
.createNodeIterator(root
, undefined, undefined);
50 check_iterator(iterator
, root
);
51 }, 'Construct a NodeIterator by document.createNodeIterator(root, undefined, undefined).');
55 assert_throws(new TypeError(), function () { document
.createNodeIterator(); });
56 assert_throws(new TypeError(), function () { document
.createNodeIterator(null); });
57 assert_throws(new TypeError(), function () { document
.createNodeIterator(undefined); });
58 assert_throws(new TypeError(), function () { document
.createNodeIterator(new Object()); });
59 assert_throws(new TypeError(), function () { document
.createNodeIterator(1); });
60 }, 'Give an invalid root node to document.createNodeIterator().');
62 // |expected| should be an object indicating the expected type of node.
63 function assert_node(actual
, expected
)
65 assert_true(actual
instanceof expected
.type
,
66 'Node type mismatch: actual = ' + actual
.nodeType
+ ', expected = ' + expected
.nodeType
);
67 if (typeof(expected
.id
) !== 'undefined')
68 assert_equals(actual
.id
, expected
.id
);
69 if (typeof(expected
.nodeValue
) !== 'undefined')
70 assert_equals(actual
.nodeValue
, expected
.nodeValue
);
73 // |expectedNodes| should be an array of objects that can be passed to |assert_node|.
74 function testIteratorForwardAndBackward(iterator
, expectedNodes
)
76 assert_equals(iterator
.referenceNode
, iterator
.root
);
77 assert_equals(iterator
.pointerBeforeReferenceNode
, true);
82 while (node
= iterator
.nextNode()) {
83 assert_node(node
, expectedNodes
[index
]);
84 assert_node(iterator
.referenceNode
, expectedNodes
[index
]);
85 assert_equals(iterator
.pointerBeforeReferenceNode
, false);
89 assert_equals(index
, expectedNodes
.length
);
90 assert_equals(node
, null);
91 assert_node(iterator
.referenceNode
, expectedNodes
[expectedNodes
.length
- 1]);
92 assert_equals(iterator
.pointerBeforeReferenceNode
, false);
96 while (node
= iterator
.previousNode()) {
97 assert_node(node
, expectedNodes
[index
]);
98 assert_node(iterator
.referenceNode
, expectedNodes
[index
]);
99 assert_equals(iterator
.pointerBeforeReferenceNode
, true);
103 assert_equals(index
, -1);
104 assert_equals(node
, null);
105 assert_node(iterator
.referenceNode
, expectedNodes
[0]);
106 assert_equals(iterator
.pointerBeforeReferenceNode
, true);
110 { type
: Element
, id
: 'a' },
111 { type
: Text
, nodeValue
: 'b' },
112 { type
: Element
, id
: 'c' },
113 { type
: Element
, id
: 'd' },
114 { type
: Text
, nodeValue
: 'e' },
115 { type
: Element
, id
: 'f' },
116 { type
: Text
, nodeValue
: 'g' },
117 { type
: Comment
, nodeValue
: 'h' },
118 { type
: Text
, nodeValue
: 'i' },
119 { type
: Comment
, nodeValue
: 'j' },
124 var iterator
= document
.createNodeIterator(createSampleDOM());
125 testIteratorForwardAndBackward(iterator
, expectedAll
);
126 }, 'Iterate over all nodes forward then backward.');
131 { type
: Element
, id
: 'a' },
132 { type
: Element
, id
: 'c' },
133 { type
: Element
, id
: 'd' },
134 { type
: Element
, id
: 'f' },
136 var iterator
= document
.createNodeIterator(createSampleDOM(), NodeFilter
.SHOW_ELEMENT
);
137 testIteratorForwardAndBackward(iterator
, expected
);
138 }, 'Iterate over all elements forward then backward.');
143 { type
: Text
, nodeValue
: 'b' },
144 { type
: Text
, nodeValue
: 'e' },
145 { type
: Text
, nodeValue
: 'g' },
146 { type
: Text
, nodeValue
: 'i' },
148 var iterator
= document
.createNodeIterator(createSampleDOM(), NodeFilter
.SHOW_TEXT
);
149 testIteratorForwardAndBackward(iterator
, expected
);
150 }, 'Iterate over all text nodes forward then backward.');
155 { type
: Comment
, nodeValue
: 'h' },
156 { type
: Comment
, nodeValue
: 'j' },
158 var iterator
= document
.createNodeIterator(createSampleDOM(), NodeFilter
.SHOW_COMMENT
);
159 testIteratorForwardAndBackward(iterator
, expected
);
160 }, 'Iterate over all comment nodes forward then backward.');
164 var iterator
= document
.createNodeIterator(createSampleDOM(), 0);
165 assert_equals(iterator
.referenceNode
, iterator
.root
);
166 assert_equals(iterator
.pointerBeforeReferenceNode
, true);
168 assert_equals(iterator
.nextNode(), null);
169 assert_equals(iterator
.referenceNode
, iterator
.root
);
170 assert_equals(iterator
.pointerBeforeReferenceNode
, true);
172 assert_equals(iterator
.previousNode(), null);
173 assert_equals(iterator
.referenceNode
, iterator
.root
);
174 assert_equals(iterator
.pointerBeforeReferenceNode
, true);
175 }, 'Test the behavior of NodeIterator when no nodes match with the given filter.');
180 { type
: Text
, nodeValue
: 'e' },
181 { type
: Element
, id
: 'f' },
182 { type
: Comment
, nodeValue
: 'j' },
184 var filter = function (node
) {
185 if (node
.nodeType
=== Node
.ELEMENT_NODE
&& node
.id
=== 'f' ||
186 node
.nodeType
=== Node
.TEXT_NODE
&& node
.nodeValue
=== 'e' ||
187 node
.nodeType
=== Node
.COMMENT_NODE
&& node
.nodeValue
=== 'j') {
188 return NodeFilter
.FILTER_ACCEPT
;
190 return NodeFilter
.FILTER_REJECT
;
192 var iterator
= document
.createNodeIterator(createSampleDOM(), NodeFilter
.SHOW_ALL
, filter
);
193 testIteratorForwardAndBackward(iterator
, expected
);
194 }, 'Test the behavior of NodeIterator when NodeFilter is specified.');
197 var nodeIterator
= document
.createNodeIterator(document
.body
, 42, null);
198 assert_equals(nodeIterator
.root
, document
.body
);
199 assert_equals(nodeIterator
.referenceNode
, document
.body
);
200 assert_equals(nodeIterator
.whatToShow
, 42);
201 assert_equals(nodeIterator
.filter
, null);
202 }, "Optional arguments to createNodeIterator should be optional (3 passed, null).");