OLD | NEW |
(Empty) | |
| 1 <!DOCTYPE html> |
| 2 <html> |
| 3 <head> |
| 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"> |
| 8 </head> |
| 9 <body> |
| 10 <p>This test checks the basic functionality of NodeIterator.</p> |
| 11 <script> |
| 12 function createSampleDOM() |
| 13 { |
| 14 // Tree order: #a -> "b" -> #c -> #d -> "e" -> #f -> "g" -> <!--h--> -> "i"
-> <!--j-->. |
| 15 var div = document.createElement('div'); |
| 16 div.id = 'a'; |
| 17 div.innerHTML = 'b<div id="c"><div id="d">e<span id="f">g<!--h--></span>i</d
iv><!--j--></div>'; |
| 18 return div; |
| 19 } |
| 20 |
| 21 test(function () |
| 22 { |
| 23 var root = createSampleDOM(); |
| 24 var iterator = document.createNodeIterator(root); |
| 25 assert_equals(iterator.toString(), '[object NodeIterator]'); |
| 26 assert_equals(iterator.root, root); |
| 27 assert_equals(iterator.referenceNode, root); |
| 28 assert_equals(iterator.pointerBeforeReferenceNode, true); |
| 29 assert_equals(iterator.whatToShow, 0xFFFFFFFF); |
| 30 assert_equals(iterator.filter, null); |
| 31 assert_readonly(iterator, 'root'); |
| 32 assert_readonly(iterator, 'referenceNode'); |
| 33 assert_readonly(iterator, 'pointerBeforeReferenceNode'); |
| 34 assert_readonly(iterator, 'whatToShow'); |
| 35 assert_readonly(iterator, 'filter'); |
| 36 assert_idl_attribute(iterator, 'nextNode'); |
| 37 assert_idl_attribute(iterator, 'previousNode'); |
| 38 assert_idl_attribute(iterator, 'detach'); |
| 39 }, 'Construct a NodeIterator by document.createNodeIterator().'); |
| 40 |
| 41 test(function () |
| 42 { |
| 43 assert_throws(new TypeError(), function () { document.createNodeIterator();
}); |
| 44 assert_throws('NOT_SUPPORTED_ERR', function () { document.createNodeIterator
(null); }); |
| 45 assert_throws('NOT_SUPPORTED_ERR', function () { document.createNodeIterator
(undefined); }); |
| 46 assert_throws('NOT_SUPPORTED_ERR', function () { document.createNodeIterator
(new Object()); }); |
| 47 assert_throws('NOT_SUPPORTED_ERR', function () { document.createNodeIterator
(1); }); |
| 48 }, 'Give an invalid root node to document.createNodeIterator().'); |
| 49 |
| 50 // |expected| should be an object indicating the expected type of node. |
| 51 function assert_node(actual, expected) |
| 52 { |
| 53 assert_true(actual instanceof expected.type, |
| 54 'Node type mismatch: actual = ' + actual.nodeType + ', expected
= ' + expected.nodeType); |
| 55 if (typeof(expected.id) !== 'undefined') |
| 56 assert_equals(actual.id, expected.id); |
| 57 if (typeof(expected.nodeValue) !== 'undefined') |
| 58 assert_equals(actual.nodeValue, expected.nodeValue); |
| 59 } |
| 60 |
| 61 // |expectedNodes| should be an array of objects that can be passed to |assert_n
ode|. |
| 62 function testIteratorForwardAndBackward(iterator, expectedNodes) |
| 63 { |
| 64 assert_equals(iterator.referenceNode, iterator.root); |
| 65 assert_equals(iterator.pointerBeforeReferenceNode, true); |
| 66 |
| 67 // Going forward. |
| 68 var index = 0; |
| 69 var node; |
| 70 while (node = iterator.nextNode()) { |
| 71 assert_node(node, expectedNodes[index]); |
| 72 assert_node(iterator.referenceNode, expectedNodes[index]); |
| 73 assert_equals(iterator.pointerBeforeReferenceNode, false); |
| 74 ++index; |
| 75 } |
| 76 |
| 77 assert_equals(index, expectedNodes.length); |
| 78 assert_equals(node, null); |
| 79 assert_node(iterator.referenceNode, expectedNodes[expectedNodes.length - 1])
; |
| 80 assert_equals(iterator.pointerBeforeReferenceNode, false); |
| 81 |
| 82 // Going backward. |
| 83 --index; |
| 84 while (node = iterator.previousNode()) { |
| 85 assert_node(node, expectedNodes[index]); |
| 86 assert_node(iterator.referenceNode, expectedNodes[index]); |
| 87 assert_equals(iterator.pointerBeforeReferenceNode, true); |
| 88 --index; |
| 89 } |
| 90 |
| 91 assert_equals(index, -1); |
| 92 assert_equals(node, null); |
| 93 assert_node(iterator.referenceNode, expectedNodes[0]); |
| 94 assert_equals(iterator.pointerBeforeReferenceNode, true); |
| 95 } |
| 96 |
| 97 test(function () |
| 98 { |
| 99 var expected = [ |
| 100 { type: Element, id: 'a' }, |
| 101 { type: Text, nodeValue: 'b' }, |
| 102 { type: Element, id: 'c' }, |
| 103 { type: Element, id: 'd' }, |
| 104 { type: Text, nodeValue: 'e' }, |
| 105 { type: Element, id: 'f' }, |
| 106 { type: Text, nodeValue: 'g' }, |
| 107 { type: Comment, nodeValue: 'h' }, |
| 108 { type: Text, nodeValue: 'i' }, |
| 109 { type: Comment, nodeValue: 'j' }, |
| 110 ]; |
| 111 var iterator = document.createNodeIterator(createSampleDOM()); |
| 112 testIteratorForwardAndBackward(iterator, expected); |
| 113 }, 'Iterate over all nodes forward then backward.'); |
| 114 |
| 115 test(function () |
| 116 { |
| 117 var expected = [ |
| 118 { type: Element, id: 'a' }, |
| 119 { type: Element, id: 'c' }, |
| 120 { type: Element, id: 'd' }, |
| 121 { type: Element, id: 'f' }, |
| 122 ]; |
| 123 var iterator = document.createNodeIterator(createSampleDOM(), NodeFilter.SHO
W_ELEMENT); |
| 124 testIteratorForwardAndBackward(iterator, expected); |
| 125 }, 'Iterate over all elements forward then backward.'); |
| 126 |
| 127 test(function () |
| 128 { |
| 129 var expected = [ |
| 130 { type: Text, nodeValue: 'b' }, |
| 131 { type: Text, nodeValue: 'e' }, |
| 132 { type: Text, nodeValue: 'g' }, |
| 133 { type: Text, nodeValue: 'i' }, |
| 134 ]; |
| 135 var iterator = document.createNodeIterator(createSampleDOM(), NodeFilter.SHO
W_TEXT); |
| 136 testIteratorForwardAndBackward(iterator, expected); |
| 137 }, 'Iterate over all text nodes forward then backward.'); |
| 138 |
| 139 test(function () |
| 140 { |
| 141 var expected = [ |
| 142 { type: Comment, nodeValue: 'h' }, |
| 143 { type: Comment, nodeValue: 'j' }, |
| 144 ]; |
| 145 var iterator = document.createNodeIterator(createSampleDOM(), NodeFilter.SHO
W_COMMENT); |
| 146 testIteratorForwardAndBackward(iterator, expected); |
| 147 }, 'Iterate over all comment nodes forward then backward.'); |
| 148 |
| 149 test(function () |
| 150 { |
| 151 var iterator = document.createNodeIterator(createSampleDOM(), 0); |
| 152 assert_equals(iterator.referenceNode, iterator.root); |
| 153 assert_equals(iterator.pointerBeforeReferenceNode, true); |
| 154 |
| 155 assert_equals(iterator.nextNode(), null); |
| 156 assert_equals(iterator.referenceNode, iterator.root); |
| 157 assert_equals(iterator.pointerBeforeReferenceNode, true); |
| 158 |
| 159 assert_equals(iterator.previousNode(), null); |
| 160 assert_equals(iterator.referenceNode, iterator.root); |
| 161 assert_equals(iterator.pointerBeforeReferenceNode, true); |
| 162 }, 'Test the behavior of NodeIterator when no nodes match with the given filter.
'); |
| 163 |
| 164 test(function () |
| 165 { |
| 166 var expected = [ |
| 167 { type: Text, nodeValue: 'e' }, |
| 168 { type: Element, id: 'f' }, |
| 169 { type: Comment, nodeValue: 'j' }, |
| 170 ]; |
| 171 var filter = function (node) { |
| 172 if (node.nodeType === Node.ELEMENT_NODE && node.id === 'f' || |
| 173 node.nodeType === Node.TEXT_NODE && node.nodeValue === 'e' || |
| 174 node.nodeType === Node.COMMENT_NODE && node.nodeValue === 'j') { |
| 175 return NodeFilter.FILTER_ACCEPT; |
| 176 } |
| 177 return NodeFilter.FILTER_REJECT; |
| 178 }; |
| 179 var iterator = document.createNodeIterator(createSampleDOM(), NodeFilter.SHO
W_ALL, filter); |
| 180 testIteratorForwardAndBackward(iterator, expected); |
| 181 }, 'Test the behavior of NodeIterator when NodeFilter is specified.'); |
| 182 </script> |
| 183 </body> |
| 184 </html> |
OLD | NEW |