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
155
156
157
158
159
160
|
<!DOCTYPE HTML>
<html>
<head>
<title>DOM Traversal: NodeIterator: Mutations (3/x)</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css">
</head>
<p id="display"></p>
<div id="content" style="display: none">
<span id=root><span id=B></span><span id=C></span><span id=D></span><span id=E><span id=E1><span id=E11></span></span></span></span>
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
function removeNode(n) {
n.parentNode.removeChild(n);
}
var initInner = $('content').innerHTML;
var content = $('content');
function resetContent() {
content.innerHTML = initInner;
var checkIt = document.createNodeIterator(content, NodeFilter.SHOW_ELEMENT,
testNodeFilter);
var node;
while ((node = checkIt.nextNode()) != null) {
if (node.id) {
window[node.id] = node;
}
}
}
function makeSpan(id) {
var e = document.createElement('span');
e.id = id;
return e;
}
function testNodeFilter(n) {
if (n.tagName == 'SPAN')
return NodeFilter.FILTER_ACCEPT;
return NodeFilter.FILTER_SKIP;
}
function checkseq(it, root, expect) {
var checkIt = document.createNodeIterator(root, NodeFilter.SHOW_ELEMENT,
testNodeFilter);
var printedPointer = (it.referenceNode == undefined);
var string = '';
var node;
while ((node = checkIt.nextNode()) != null) {
if (!printedPointer && it.referenceNode == node) {
printedPointer = true;
var s = '[' + node.id + '] ';
if (it.pointerBeforeReferenceNode)
string += "* " + s;
else
string += s + "* ";
} else {
string += node.id + " ";
}
}
is(string.slice(0, -1), expect, "sequence check");
}
resetContent();
var it = document.createNodeIterator(E, NodeFilter.SHOW_ELEMENT,
testNodeFilter);
checkseq(it, root, "root B C D * [E] E1 E11");
removeNode(C);
checkseq(it, root, "root B D * [E] E1 E11");
it.nextNode();
removeNode(D);
checkseq(it, root, "root B [E] * E1 E11");
it.nextNode();
removeNode(B);
checkseq(it, root, "root E [E1] * E11");
it.nextNode();
checkseq(it, root, "root E E1 [E11] *");
it.nextNode();
checkseq(it, root, "root E E1 [E11] *");
it.previousNode();
it.previousNode();
it.previousNode();
it.previousNode();
it.previousNode();
checkseq(it, root, "root * [E] E1 E11");
resetContent();
it = document.createNodeIterator(E, NodeFilter.SHOW_ELEMENT,
testNodeFilter);
checkseq(it, root, "root B C D * [E] E1 E11");
it.nextNode();
it.nextNode();
checkseq(it, root, "root B C D E [E1] * E11");
it.previousNode();
it.previousNode();
checkseq(it, root, "root B C D * [E] E1 E11");
removeNode(D);
removeNode(B);
checkseq(it, root, "root C * [E] E1 E11");
n = makeSpan('n');
root.insertBefore(n, E);
checkseq(it, root, "root C n * [E] E1 E11");
n2 = makeSpan('n2');
root.insertBefore(n2, C);
checkseq(it, root, "root n2 C n * [E] E1 E11");
resetContent();
it = document.createNodeIterator(E, NodeFilter.SHOW_ELEMENT,
testNodeFilter);
checkseq(it, root, "root B C D * [E] E1 E11");
removeNode(root);
checkseq(it, root, "root B C D * [E] E1 E11");
removeNode(B);
checkseq(it, root, "root C D * [E] E1 E11");
removeNode(D);
checkseq(it, root, "root C * [E] E1 E11");
it.nextNode();
it.nextNode();
it.nextNode();
checkseq(it, root, "root C E E1 [E11] *");
removeNode(E1);
checkseq(it, root, "root C [E] *");
n = makeSpan('n');
root.insertBefore(n, E);
checkseq(it, root, "root C n [E] *");
n2 = makeSpan('n2');
E.appendChild(n2);
checkseq(it, root, "root C n [E] * n2");
it.nextNode();
checkseq(it, root, "root C n E [n2] *");
removeNode(E);
checkseq(it, root, "root C n");
</script>
</pre>
</body>
</html>
|