Chromium Code Reviews| Index: LayoutTests/fast/dom/NodeIterator/NodeIterator-basic.html |
| diff --git a/LayoutTests/fast/dom/NodeIterator/NodeIterator-basic.html b/LayoutTests/fast/dom/NodeIterator/NodeIterator-basic.html |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..181cf29986bc6fab3c68a2f3efc62fe7b4e35c24 |
| --- /dev/null |
| +++ b/LayoutTests/fast/dom/NodeIterator/NodeIterator-basic.html |
| @@ -0,0 +1,190 @@ |
| +<!DOCTYPE html> |
| +<html> |
| +<head> |
| +<title>NodeIterator: Basic test</title> |
| +<script src="../../../resources/testharness.js"></script> |
| +<script src="../../../resources/testharnessreport.js"></script> |
| +<link rel="stylesheet" href="../../../resources/testharness.css"> |
| +</head> |
| +<body> |
| +<p>This test checks the basic functionality of NodeIterator.</p> |
| +<script> |
| +function createSampleDOM() |
| +{ |
| + // Tree order: #a -> "b" -> #c -> #d -> "e" -> #f -> "g" -> <!--h--> -> "i" -> <!--j-->. |
| + var div = document.createElement('div'); |
| + div.id = 'a'; |
| + div.innerHTML = 'b<div id="c"><div id="d">e<span id="f">g<!--h--></span>i</div><!--j--></div>'; |
| + return div; |
| +} |
| + |
| +test(function () |
| +{ |
| + var root = createSampleDOM(); |
| + var iterator = document.createNodeIterator(root); |
| + assert_equals(iterator.toString(), '[object NodeIterator]'); |
| + assert_equals(iterator.root, root); |
| + assert_equals(iterator.referenceNode, root); |
| + assert_equals(iterator.pointerBeforeReferenceNode, true); |
| + assert_equals(iterator.whatToShow, 0xFFFFFFFF); |
| + assert_equals(iterator.filter, null); |
| + assert_readonly(iterator, 'root'); |
| + assert_readonly(iterator, 'referenceNode'); |
| + assert_readonly(iterator, 'pointerBeforeReferenceNode'); |
| + assert_readonly(iterator, 'whatToShow'); |
| + assert_readonly(iterator, 'filter'); |
| + assert_idl_attribute(iterator, 'nextNode'); |
| + assert_idl_attribute(iterator, 'previousNode'); |
| + assert_idl_attribute(iterator, 'detach'); |
| +}, 'Construct a NodeIterator by document.createNodeIterator().'); |
| + |
| +test(function () |
| +{ |
| + assert_throws(new TypeError(), function () { document.createNodeIterator(); }); |
| + assert_throws('NOT_SUPPORTED_ERR', function () { document.createNodeIterator(null); }); |
| + assert_throws('NOT_SUPPORTED_ERR', function () { document.createNodeIterator(undefined); }); |
| + assert_throws('NOT_SUPPORTED_ERR', function () { document.createNodeIterator(new Object()); }); |
| + assert_throws('NOT_SUPPORTED_ERR', function () { document.createNodeIterator(1); }); |
| +}, 'Give an invalid root node to document.createNodeIterator().'); |
| + |
| +// |expected| should be an object indicating the expected type of node. |
| +function assert_node(actual, expected) |
| +{ |
| + if (expected.type === 'element') { |
| + assert_equals(actual.nodeType, Node.ELEMENT_NODE, ''); |
| + assert_equals(actual.id, expected.id); |
| + } else if (expected.type == 'text') { |
| + assert_equals(actual.nodeType, Node.TEXT_NODE); |
| + assert_equals(actual.nodeValue, expected.content); |
| + } else if (expected.type == 'comment') { |
| + assert_equals(actual.nodeType, Node.COMMENT_NODE); |
| + assert_equals(actual.nodeValue, expected.content); |
| + } else { |
| + assert_unreached(); |
| + } |
| +} |
| + |
| +// |expectedNodes| should be an array of objects that can be passed to |assert_node|. |
| +function testIteratorForwardAndBackward(iterator, expectedNodes) |
| +{ |
| + assert_equals(iterator.referenceNode, iterator.root); |
| + assert_equals(iterator.pointerBeforeReferenceNode, true); |
| + |
| + // Going forward. |
| + var index = 0; |
| + var node; |
| + while (node = iterator.nextNode()) { |
| + assert_node(node, expectedNodes[index]); |
| + assert_node(iterator.referenceNode, expectedNodes[index]); |
| + assert_equals(iterator.pointerBeforeReferenceNode, false); |
| + ++index; |
| + } |
| + |
| + assert_equals(index, expectedNodes.length); |
| + assert_equals(node, null); |
| + assert_node(iterator.referenceNode, expectedNodes[expectedNodes.length - 1]); |
| + assert_equals(iterator.pointerBeforeReferenceNode, false); |
| + |
| + // Going backward. |
| + --index; |
| + while (node = iterator.previousNode()) { |
| + assert_node(node, expectedNodes[index]); |
| + assert_node(iterator.referenceNode, expectedNodes[index]); |
| + assert_equals(iterator.pointerBeforeReferenceNode, true); |
| + --index; |
| + } |
| + |
| + assert_equals(index, -1); |
| + assert_equals(node, null); |
| + assert_node(iterator.referenceNode, expectedNodes[0]); |
| + assert_equals(iterator.pointerBeforeReferenceNode, true); |
| +} |
| + |
| +test(function () |
| +{ |
| + var expected = [ |
| + { 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.
|
| + { type: 'text', content: 'b' }, |
| + { type: 'element', id: 'c' }, |
| + { type: 'element', id: 'd' }, |
| + { type: 'text', content: 'e' }, |
| + { type: 'element', id: 'f' }, |
| + { type: 'text', content: 'g' }, |
| + { type: 'comment', content: 'h' }, |
| + { type: 'text', content: 'i' }, |
| + { type: 'comment', content: 'j' }, |
| + ]; |
| + var iterator = document.createNodeIterator(createSampleDOM()); |
| + testIteratorForwardAndBackward(iterator, expected); |
| +}, 'Iterate over all nodes forward then backward.'); |
| + |
| +test(function () |
| +{ |
| + var expected = [ |
| + { type: 'element', id: 'a' }, |
| + { type: 'element', id: 'c' }, |
| + { type: 'element', id: 'd' }, |
| + { type: 'element', id: 'f' }, |
| + ]; |
| + var iterator = document.createNodeIterator(createSampleDOM(), NodeFilter.SHOW_ELEMENT); |
| + testIteratorForwardAndBackward(iterator, expected); |
| +}, 'Iterate over all elements forward then backward.'); |
| + |
| +test(function () |
| +{ |
| + var expected = [ |
| + { type: 'text', content: 'b' }, |
| + { type: 'text', content: 'e' }, |
| + { type: 'text', content: 'g' }, |
| + { type: 'text', content: 'i' }, |
| + ]; |
| + var iterator = document.createNodeIterator(createSampleDOM(), NodeFilter.SHOW_TEXT); |
| + testIteratorForwardAndBackward(iterator, expected); |
| +}, 'Iterate over all text nodes forward then backward.'); |
| + |
| +test(function () |
| +{ |
| + var expected = [ |
| + { type: 'comment', content: 'h' }, |
| + { type: 'comment', content: 'j' }, |
| + ]; |
| + var iterator = document.createNodeIterator(createSampleDOM(), NodeFilter.SHOW_COMMENT); |
| + testIteratorForwardAndBackward(iterator, expected); |
| +}, 'Iterate over all comment nodes forward then backward.'); |
| + |
| +test(function () |
| +{ |
| + var iterator = document.createNodeIterator(createSampleDOM(), 0); |
| + assert_equals(iterator.referenceNode, iterator.root); |
| + assert_equals(iterator.pointerBeforeReferenceNode, true); |
| + |
| + assert_equals(iterator.nextNode(), null); |
| + assert_equals(iterator.referenceNode, iterator.root); |
| + assert_equals(iterator.pointerBeforeReferenceNode, true); |
| + |
| + assert_equals(iterator.previousNode(), null); |
| + assert_equals(iterator.referenceNode, iterator.root); |
| + assert_equals(iterator.pointerBeforeReferenceNode, true); |
| +}, 'Test the behavior of NodeIterator when no nodes match with the given filter.'); |
| + |
| +test(function () |
| +{ |
| + var expected = [ |
| + { type: 'text', content: 'e' }, |
| + { type: 'element', id: 'f' }, |
| + { type: 'comment', content: 'j' }, |
| + ]; |
| + var filter = function (node) { |
| + if (node.nodeType === Node.ELEMENT_NODE && node.id === 'f' || |
| + node.nodeType === Node.TEXT_NODE && node.nodeValue === 'e' || |
| + node.nodeType === Node.COMMENT_NODE && node.nodeValue === 'j') { |
| + return NodeFilter.FILTER_ACCEPT; |
| + } |
| + return NodeFilter.FILTER_REJECT; |
| + }; |
| + var iterator = document.createNodeIterator(createSampleDOM(), NodeFilter.SHOW_ALL, filter); |
| + testIteratorForwardAndBackward(iterator, expected); |
| +}, 'Test the behavior of NodeIterator when NodeFilter is specified.'); |
| +</script> |
| +</body> |
| +</html> |