Chromium Code Reviews| 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 if (expected.type === 'element') { | |
| 54 assert_equals(actual.nodeType, Node.ELEMENT_NODE, ''); | |
| 55 assert_equals(actual.id, expected.id); | |
| 56 } else if (expected.type == 'text') { | |
| 57 assert_equals(actual.nodeType, Node.TEXT_NODE); | |
| 58 assert_equals(actual.nodeValue, expected.content); | |
| 59 } else if (expected.type == 'comment') { | |
| 60 assert_equals(actual.nodeType, Node.COMMENT_NODE); | |
| 61 assert_equals(actual.nodeValue, expected.content); | |
| 62 } else { | |
| 63 assert_unreached(); | |
| 64 } | |
| 65 } | |
| 66 | |
| 67 // |expectedNodes| should be an array of objects that can be passed to |assert_n ode|. | |
| 68 function testIteratorForwardAndBackward(iterator, expectedNodes) | |
| 69 { | |
| 70 assert_equals(iterator.referenceNode, iterator.root); | |
| 71 assert_equals(iterator.pointerBeforeReferenceNode, true); | |
| 72 | |
| 73 // Going forward. | |
| 74 var index = 0; | |
| 75 var node; | |
| 76 while (node = iterator.nextNode()) { | |
| 77 assert_node(node, expectedNodes[index]); | |
| 78 assert_node(iterator.referenceNode, expectedNodes[index]); | |
| 79 assert_equals(iterator.pointerBeforeReferenceNode, false); | |
| 80 ++index; | |
| 81 } | |
| 82 | |
| 83 assert_equals(index, expectedNodes.length); | |
| 84 assert_equals(node, null); | |
| 85 assert_node(iterator.referenceNode, expectedNodes[expectedNodes.length - 1]) ; | |
| 86 assert_equals(iterator.pointerBeforeReferenceNode, false); | |
| 87 | |
| 88 // Going backward. | |
| 89 --index; | |
| 90 while (node = iterator.previousNode()) { | |
| 91 assert_node(node, expectedNodes[index]); | |
| 92 assert_node(iterator.referenceNode, expectedNodes[index]); | |
| 93 assert_equals(iterator.pointerBeforeReferenceNode, true); | |
| 94 --index; | |
| 95 } | |
| 96 | |
| 97 assert_equals(index, -1); | |
| 98 assert_equals(node, null); | |
| 99 assert_node(iterator.referenceNode, expectedNodes[0]); | |
| 100 assert_equals(iterator.pointerBeforeReferenceNode, true); | |
| 101 } | |
| 102 | |
| 103 test(function () | |
| 104 { | |
| 105 var expected = [ | |
| 106 { type: 'element', id: 'a' }, | |
|
Hajime Morrita
2013/06/14 00:12:15
This is rather matter of taste, but probably you c
Yuta Kitamura
2013/06/14 01:08:50
Agreed, will fix.
Yuta Kitamura
2013/06/14 01:39:58
Done.
| |
| 107 { type: 'text', content: 'b' }, | |
| 108 { type: 'element', id: 'c' }, | |
| 109 { type: 'element', id: 'd' }, | |
| 110 { type: 'text', content: 'e' }, | |
| 111 { type: 'element', id: 'f' }, | |
| 112 { type: 'text', content: 'g' }, | |
| 113 { type: 'comment', content: 'h' }, | |
| 114 { type: 'text', content: 'i' }, | |
| 115 { type: 'comment', content: 'j' }, | |
| 116 ]; | |
| 117 var iterator = document.createNodeIterator(createSampleDOM()); | |
| 118 testIteratorForwardAndBackward(iterator, expected); | |
| 119 }, 'Iterate over all nodes forward then backward.'); | |
| 120 | |
| 121 test(function () | |
| 122 { | |
| 123 var expected = [ | |
| 124 { type: 'element', id: 'a' }, | |
| 125 { type: 'element', id: 'c' }, | |
| 126 { type: 'element', id: 'd' }, | |
| 127 { type: 'element', id: 'f' }, | |
| 128 ]; | |
| 129 var iterator = document.createNodeIterator(createSampleDOM(), NodeFilter.SHO W_ELEMENT); | |
| 130 testIteratorForwardAndBackward(iterator, expected); | |
| 131 }, 'Iterate over all elements forward then backward.'); | |
| 132 | |
| 133 test(function () | |
| 134 { | |
| 135 var expected = [ | |
| 136 { type: 'text', content: 'b' }, | |
| 137 { type: 'text', content: 'e' }, | |
| 138 { type: 'text', content: 'g' }, | |
| 139 { type: 'text', content: 'i' }, | |
| 140 ]; | |
| 141 var iterator = document.createNodeIterator(createSampleDOM(), NodeFilter.SHO W_TEXT); | |
| 142 testIteratorForwardAndBackward(iterator, expected); | |
| 143 }, 'Iterate over all text nodes forward then backward.'); | |
| 144 | |
| 145 test(function () | |
| 146 { | |
| 147 var expected = [ | |
| 148 { type: 'comment', content: 'h' }, | |
| 149 { type: 'comment', content: 'j' }, | |
| 150 ]; | |
| 151 var iterator = document.createNodeIterator(createSampleDOM(), NodeFilter.SHO W_COMMENT); | |
| 152 testIteratorForwardAndBackward(iterator, expected); | |
| 153 }, 'Iterate over all comment nodes forward then backward.'); | |
| 154 | |
| 155 test(function () | |
| 156 { | |
| 157 var iterator = document.createNodeIterator(createSampleDOM(), 0); | |
| 158 assert_equals(iterator.referenceNode, iterator.root); | |
| 159 assert_equals(iterator.pointerBeforeReferenceNode, true); | |
| 160 | |
| 161 assert_equals(iterator.nextNode(), null); | |
| 162 assert_equals(iterator.referenceNode, iterator.root); | |
| 163 assert_equals(iterator.pointerBeforeReferenceNode, true); | |
| 164 | |
| 165 assert_equals(iterator.previousNode(), null); | |
| 166 assert_equals(iterator.referenceNode, iterator.root); | |
| 167 assert_equals(iterator.pointerBeforeReferenceNode, true); | |
| 168 }, 'Test the behavior of NodeIterator when no nodes match with the given filter. '); | |
| 169 | |
| 170 test(function () | |
| 171 { | |
| 172 var expected = [ | |
| 173 { type: 'text', content: 'e' }, | |
| 174 { type: 'element', id: 'f' }, | |
| 175 { type: 'comment', content: 'j' }, | |
| 176 ]; | |
| 177 var filter = function (node) { | |
| 178 if (node.nodeType === Node.ELEMENT_NODE && node.id === 'f' || | |
| 179 node.nodeType === Node.TEXT_NODE && node.nodeValue === 'e' || | |
| 180 node.nodeType === Node.COMMENT_NODE && node.nodeValue === 'j') { | |
| 181 return NodeFilter.FILTER_ACCEPT; | |
| 182 } | |
| 183 return NodeFilter.FILTER_REJECT; | |
| 184 }; | |
| 185 var iterator = document.createNodeIterator(createSampleDOM(), NodeFilter.SHO W_ALL, filter); | |
| 186 testIteratorForwardAndBackward(iterator, expected); | |
| 187 }, 'Test the behavior of NodeIterator when NodeFilter is specified.'); | |
| 188 </script> | |
| 189 </body> | |
| 190 </html> | |
| OLD | NEW |