summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/dom/traversal/TreeWalker-basic.html
blob: d1147637b139cdd8d215e51f2bc4a2d8cd1012f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<!DOCTYPE html>
<html>
<!--
Test adapted from https://dxr.mozilla.org/chromium/source/src/third_party/WebKit/LayoutTests/fast/dom/TreeWalker/TreeWalker-basic.html
-->
<head>
<title>TreeWalker: Basic test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="traversal-support.js"></script>
<div id=log></div>
</head>
<body>
<p>This test checks the basic functionality of TreeWalker.</p>
<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>';

    div.appendChild(document.createTextNode("b"));

    var c = document.createElement("div");
    c.id = 'c';
    div.appendChild(c);

    var d = document.createElement("div");
    d.id = 'd';
    c.appendChild(d);

    var e = document.createTextNode("e");
    d.appendChild(e);

    var f = document.createElement("span");
    f.id = 'f';
    d.appendChild(f);

    var g = document.createTextNode("g");
    f.appendChild(g);

    var h = document.createComment("h");
    f.appendChild(h);

    var i = document.createTextNode("i");
    d.appendChild(i);

    var j = document.createComment("j");
    c.appendChild(j);

    return div;
}

function check_walker(walker, root, whatToShowValue)
{
    whatToShowValue = whatToShowValue === undefined ? 0xFFFFFFFF : whatToShowValue;

    assert_equals(walker.toString(), '[object TreeWalker]', 'toString');
    assert_equals(walker.root, root, 'root');
    assert_equals(walker.whatToShow, whatToShowValue, 'whatToShow');
    assert_equals(walker.filter, null, 'filter');
    assert_equals(walker.currentNode, root, 'currentNode');
    assert_readonly(walker, 'root');
    assert_readonly(walker, 'whatToShow');
    assert_readonly(walker, 'filter');
}

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, null, null);
    check_walker(walker, root, 0);
}, 'Construct a TreeWalker by document.createTreeWalker(root, null, null).');

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(new TypeError(), function () { document.createTreeWalker(); });
    assert_throws(new TypeError(), function () { document.createTreeWalker(null); });
    assert_throws(new TypeError(), function () { document.createTreeWalker(undefined); });
    assert_throws(new TypeError(), function () { document.createTreeWalker(new Object()); });
    assert_throws(new TypeError(), function () { document.createTreeWalker(1); });
}, 'Give an invalid root node to document.createTreeWalker().');

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

    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.');

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>