2 <title>NodeIterator tests</title>
3 <link rel="author" title="Aryeh Gregor" href=ayg@aryeh.name>
4 <meta name=timeout content=long>
6 <script src=/resources/testharness.js></script>
7 <script src=/resources/testharnessreport.js></script>
8 <script src=../common.js></script>
12 function check_iter(iter, root, whatToShowValue) {
13 whatToShowValue = whatToShowValue === undefined ? 0xFFFFFFFF : whatToShowValue;
15 assert_equals(iter.toString(), '[object NodeIterator]', 'toString');
16 assert_equals(iter.root, root, 'root');
17 assert_equals(iter.whatToShow, whatToShowValue, 'whatToShow');
18 assert_equals(iter.filter, null, 'filter');
19 assert_equals(iter.referenceNode, root, 'referenceNode');
20 assert_equals(iter.pointerBeforeReferenceNode, true, 'pointerBeforeReferenceNode');
21 assert_readonly(iter, 'root');
22 assert_readonly(iter, 'whatToShow');
23 assert_readonly(iter, 'filter');
24 assert_readonly(iter, 'referenceNode');
25 assert_readonly(iter, 'pointerBeforeReferenceNode');
29 var iter = document.createNodeIterator(document);
32 }, "detach() should be a no-op");
35 var iter = document.createNodeIterator(document);
36 check_iter(iter, document);
37 }, "createNodeIterator() parameter defaults");
40 var iter = document.createNodeIterator(document, null, null);
41 check_iter(iter, document, 0);
42 }, "createNodeIterator() with null as arguments");
45 var iter = document.createNodeIterator(document, undefined, undefined);
46 check_iter(iter, document);
47 }, "createNodeIterator() with undefined as arguments");
50 var iter = document.createNodeIterator(document, NodeFilter.SHOW_ALL,
51 function() { throw {name: "failed"} });
52 assert_throws({name: "failed"}, function() { iter.nextNode() });
53 }, "Propagate exception from filter function");
57 var iter = document.createNodeIterator(document, NodeFilter.SHOW_ALL,
59 if (iter.referenceNode != document && depth == 0) {
63 return NodeFilter.FILTER_ACCEPT;
67 assert_throws("InvalidStateError", function() { iter.nextNode() });
69 assert_throws("InvalidStateError", function() { iter.previousNode() });
70 }, "Recursive filters need to throw");
72 function testIterator(root, whatToShow, filter) {
73 var iter = document.createNodeIterator(root, whatToShow, filter);
75 assert_equals(iter.root, root, ".root");
76 assert_equals(iter.referenceNode, root, "Initial .referenceNode");
77 assert_equals(iter.pointerBeforeReferenceNode, true,
78 ".pointerBeforeReferenceNode");
79 assert_equals(iter.whatToShow, whatToShow, ".whatToShow");
80 assert_equals(iter.filter, filter, ".filter");
82 var expectedReferenceNode = root;
83 var expectedBeforeNode = true;
84 // "Let node be the value of the referenceNode attribute."
86 // "Let before node be the value of the pointerBeforeReferenceNode
88 var beforeNode = true;
90 // Each loop iteration runs nextNode() once.
94 // "If before node is false, let node be the first node following node
95 // in the iterator collection. If there is no such node return null."
96 node = nextNode(node);
97 if (!isInclusiveDescendant(node, root)) {
102 // "If before node is true, set it to false."
105 // "Filter node and let result be the return value.
107 // "If result is FILTER_ACCEPT, go to the next step in the overall set of
110 // "Otherwise, run these substeps again."
111 if (!((1 << (node.nodeType - 1)) & whatToShow)
112 || (filter && filter(node) != NodeFilter.FILTER_ACCEPT)) {
116 // "Set the referenceNode attribute to node, set the
117 // pointerBeforeReferenceNode attribute to before node, and return node."
118 expectedReferenceNode = node;
119 expectedBeforeNode = beforeNode;
124 assert_equals(iter.nextNode(), node, ".nextNode() " + i + " time(s)");
125 assert_equals(iter.referenceNode, expectedReferenceNode,
126 ".referenceNode after nextNode() " + i + " time(s)");
127 assert_equals(iter.pointerBeforeReferenceNode, expectedBeforeNode,
128 ".pointerBeforeReferenceNode after nextNode() " + i + " time(s)");
133 // Same but for previousNode() (mostly copy-pasted, oh well)
134 var iter = document.createNodeIterator(root, whatToShow, filter);
136 var expectedReferenceNode = root;
137 var expectedBeforeNode = true;
138 // "Let node be the value of the referenceNode attribute."
140 // "Let before node be the value of the pointerBeforeReferenceNode
142 var beforeNode = true;
144 // Each loop iteration runs previousNode() once.
148 // "If before node is true, let node be the first node preceding node
149 // in the iterator collection. If there is no such node return null."
150 node = previousNode(node);
151 if (!isInclusiveDescendant(node, root)) {
156 // "If before node is false, set it to true."
159 // "Filter node and let result be the return value.
161 // "If result is FILTER_ACCEPT, go to the next step in the overall set of
164 // "Otherwise, run these substeps again."
165 if (!((1 << (node.nodeType - 1)) & whatToShow)
166 || (filter && filter(node) != NodeFilter.FILTER_ACCEPT)) {
170 // "Set the referenceNode attribute to node, set the
171 // pointerBeforeReferenceNode attribute to before node, and return node."
172 expectedReferenceNode = node;
173 expectedBeforeNode = beforeNode;
178 assert_equals(iter.previousNode(), node, ".previousNode() " + i + " time(s)");
179 assert_equals(iter.referenceNode, expectedReferenceNode,
180 ".referenceNode after previousNode() " + i + " time(s)");
181 assert_equals(iter.pointerBeforeReferenceNode, expectedBeforeNode,
182 ".pointerBeforeReferenceNode after previousNode() " + i + " time(s)");
191 "NodeFilter.SHOW_ELEMENT",
192 "NodeFilter.SHOW_ATTRIBUTE",
193 "NodeFilter.SHOW_ELEMENT | NodeFilter.SHOW_DOCUMENT",
198 "(function(node) { return true })",
199 "(function(node) { return false })",
200 "(function(node) { return node.nodeName[0] == '#' })",
204 for (var i = 0; i < testNodes.length; i++) {
205 for (var j = 0; j < whatToShows.length; j++) {
206 for (var k = 0; k < callbacks.length; k++) {
208 "document.createNodeIterator(" + testNodes[i]
209 + ", " + whatToShows[j] + ", " + callbacks[k] + ")",
210 eval(testNodes[i]), eval(whatToShows[j]), eval(callbacks[k])
216 generate_tests(testIterator, tests);
218 testDiv.style.display = "none";