chromium/third_party/blink/web_tests/fast/dom/TreeWalker/TreeWalker-basic.html

<!DOCTYPE html>
<html>
<head>
<title>This test checks the basic functionality of TreeWalker.</title>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<link rel="stylesheet" href="../../../resources/testharness.css">
</head>
<body>
<script>
function createSampleDOM()
{
    // Tree structure:
    //             #a
    //             |
    //        +----+----+
    //        |         |
    //       "b"        #c
    //                  |
    //             +----+----+
    //             |         |
    //            #d      <!--j-->
    //             |
    //        +----+----+
    //        |    |    |
    //       "e"  #f   "i"
    //             |
    //          +--+--+
    //          |     |
    //         "g" <!--h-->
    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;
}

function check_walker(walker, root)
{
    assert_equals(walker.toString(), '[object TreeWalker]');
    assert_equals(walker.root, root);
    assert_equals(walker.whatToShow, 0xFFFFFFFF);
    assert_equals(walker.filter, null);
    assert_equals(walker.currentNode, root);
    assert_readonly(walker, 'root');
    assert_readonly(walker, 'whatToShow');
    assert_readonly(walker, 'filter');
    assert_idl_attribute(walker, 'parentNode');
    assert_idl_attribute(walker, 'firstChild');
    assert_idl_attribute(walker, 'lastChild');
    assert_idl_attribute(walker, 'previousSibling');
    assert_idl_attribute(walker, 'nextSibling');
    assert_idl_attribute(walker, 'previousNode');
    assert_idl_attribute(walker, 'nextNode');
}

test(function ()
{
    var root = createSampleDOM();
    var walker = document.createTreeWalker(root);
    check_walker(walker, root);
}, 'Construct a TreeWalker by document.createTreeWalker(root).');

test(function ()
{
    var root = createSampleDOM();
    var walker = document.createTreeWalker(root, undefined, undefined);
    check_walker(walker, root);
}, 'Construct a TreeWalker by document.createTreeWalker(root, undefined, undefined).');

test(function ()
{
    assert_throws_js(TypeError, function () { document.createTreeWalker(); });
    assert_throws_js(TypeError, function () { document.createTreeWalker(null); });
    assert_throws_js(TypeError, function () { document.createTreeWalker(undefined); });
    assert_throws_js(TypeError, function () { document.createTreeWalker(new Object()); });
    assert_throws_js(TypeError, function () { document.createTreeWalker(1); });
}, 'Give an invalid root node to document.createTreeWalker().');

// |expected| should be an object indicating the expected type of node.
function assert_node(actual, expected)
{
    assert_true(actual instanceof expected.type,
                'Node type mismatch: actual = ' + actual.nodeType + ', expected = ' + expected.nodeType);
    if (typeof(expected.id) !== 'undefined')
        assert_equals(actual.id, expected.id);
    if (typeof(expected.nodeValue) !== 'undefined')
        assert_equals(actual.nodeValue, expected.nodeValue);
}

test(function ()
{
    var root = createSampleDOM();
    var walker = document.createTreeWalker(root);
    var f = root.lastChild.firstChild.childNodes[1];  // An element node: div#f.
    var body = document.body;  // An element outside |root|.

    assert_node(walker.currentNode, { type: Element, id: 'a' });
    assert_equals(walker.parentNode(), null);
    assert_node(walker.currentNode, { type: Element, id: 'a' });
    assert_node(walker.firstChild(), { type: Text, nodeValue: 'b' });
    assert_node(walker.currentNode, { type: Text, nodeValue: 'b' });
    assert_node(walker.nextSibling(), { type: Element, id: 'c' });
    assert_node(walker.currentNode, { type: Element, id: 'c' });
    assert_node(walker.lastChild(), { type: Comment, nodeValue: 'j' });
    assert_node(walker.currentNode, { type: Comment, nodeValue: 'j' });
    assert_node(walker.previousSibling(), { type: Element, id: 'd' });
    assert_node(walker.currentNode, { type: Element, id: 'd' });
    assert_node(walker.nextNode(), { type: Text, nodeValue: 'e' });
    assert_node(walker.currentNode, { type: Text, nodeValue: 'e' });
    assert_node(walker.parentNode(), { type: Element, id: 'd' });
    assert_node(walker.currentNode, { type: Element, id: 'd' });
    assert_node(walker.previousNode(), { type: Element, id: 'c' });
    assert_node(walker.currentNode, { type: Element, id: 'c' });
    assert_equals(walker.nextSibling(), null);
    assert_node(walker.currentNode, { type: Element, id: 'c' });
    walker.currentNode = f;
    assert_equals(walker.currentNode, f);
}, 'Walk over nodes.');

// FIXME: Add tests that use |whatToShow| argument and/or |filter| argument.

test(function() {
    var treeWalker = document.createTreeWalker(document.body, 42, null);
    assert_equals(treeWalker.root, document.body);
    assert_equals(treeWalker.currentNode, document.body);
    assert_equals(treeWalker.whatToShow, 42);
    assert_equals(treeWalker.filter, null);
}, "Optional arguments to createTreeWalker should be optional (3 passed, null).");
</script>
</body>
</html>