node-iterator.js
7.15 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
"use strict";
const domSymbolTree = require("./helpers/internal-constants").domSymbolTree;
const defineGetter = require("../utils").defineGetter;
const INTERNAL = Symbol("NodeIterator internal");
module.exports = function (core) {
// https://dom.spec.whatwg.org/#interface-nodeiterator
function NodeIteratorInternal(document, root, whatToShow, filter) {
this.active = true;
this.document = document;
this.root = root;
this.referenceNode = root;
this.pointerBeforeReferenceNode = true;
this.whatToShow = whatToShow;
this.filter = filter;
}
NodeIteratorInternal.prototype.throwIfNotActive = function () {
// (only thrown for getters/methods that are affected by removing steps)
if (!this.active) {
throw Error("This NodeIterator is no longer active. " +
"More than " + this.document._activeNodeIteratorsMax +
" iterators are being used concurrently. " +
"You can increase the 'concurrentNodeIterators' option to " +
"make this error go away."
);
// Alternatively, you can pester Ecma to add support for weak references,
// the DOM standard assumes the implementor has control over object life cycles.
}
};
NodeIteratorInternal.prototype.traverse = function (next) {
let node = this.referenceNode;
let beforeNode = this.pointerBeforeReferenceNode;
do {
if (next) {
if (!beforeNode) {
node = domSymbolTree.following(node, { root: this.root });
if (!node) {
return null;
}
}
beforeNode = false;
} else { // previous
if (beforeNode) {
node = domSymbolTree.preceding(node, { root: this.root });
if (!node) {
return null;
}
}
beforeNode = true;
}
}
while (this.filterNode(node) !== core.NodeFilter.FILTER_ACCEPT);
this.pointerBeforeReferenceNode = beforeNode;
this.referenceNode = node;
return node;
};
NodeIteratorInternal.prototype.filterNode = function (node) {
const n = node.nodeType - 1;
if (!(this.whatToShow & (1 << n))) {
return core.NodeFilter.FILTER_SKIP;
}
let ret = core.NodeFilter.FILTER_ACCEPT;
const filter = this.filter;
if (typeof filter === "function") {
ret = filter(node);
} else if (filter && typeof filter.acceptNode === "function") {
ret = filter.acceptNode(node);
}
if (ret === true) {
return core.NodeFilter.FILTER_ACCEPT;
} else if (ret === false) {
return core.NodeFilter.FILTER_REJECT;
}
return ret;
};
NodeIteratorInternal.prototype.runRemovingSteps = function (oldNode, oldParent, oldPreviousSibling) {
if (oldNode.contains(this.root)) {
return;
}
// If oldNode is not an inclusive ancestor of the referenceNode
// attribute value, terminate these steps.
if (!oldNode.contains(this.referenceNode)) {
return;
}
if (this.pointerBeforeReferenceNode) {
// Let nextSibling be oldPreviousSibling’s next sibling, if oldPreviousSibling is non-null,
// and oldParent’s first child otherwise.
const nextSibling = oldPreviousSibling ?
oldPreviousSibling.nextSibling :
oldParent.firstChild;
// If nextSibling is non-null, set the referenceNode attribute to nextSibling
// and terminate these steps.
if (nextSibling) {
this.referenceNode = nextSibling;
return;
}
// Let next be the first node following oldParent (excluding any children of oldParent).
const next = domSymbolTree.following(oldParent, { skipChildren: true });
// If root is an inclusive ancestor of next, set the referenceNode
// attribute to next and terminate these steps.
if (this.root.contains(next)) {
this.referenceNode = next;
return;
}
// Otherwise, set the pointerBeforeReferenceNode attribute to false.
this.pointerBeforeReferenceNode = false;
// Note: Steps are not terminated here.
}
// Set the referenceNode attribute to the last inclusive descendant in tree order of oldPreviousSibling,
// if oldPreviousSibling is non-null, and to oldParent otherwise.
this.referenceNode = oldPreviousSibling ?
domSymbolTree.lastInclusiveDescendant(oldPreviousSibling) :
oldParent;
};
core.Document._removingSteps.push((document, oldNode, oldParent, oldPreviousSibling) => {
for (let i = 0; i < document._activeNodeIterators.length; ++i) {
const internal = document._activeNodeIterators[i];
internal.runRemovingSteps(oldNode, oldParent, oldPreviousSibling);
}
});
core.Document.prototype.createNodeIterator = function (root, whatToShow, filter) {
if (!root) {
throw new TypeError("Not enough arguments to Document.createNodeIterator.");
}
if (filter === undefined) {
filter = null;
}
if (filter !== null &&
typeof filter !== "function" &&
typeof filter.acceptNode !== "function") {
throw new TypeError("Argument 3 of Document.createNodeIterator should be a function or implement NodeFilter.");
}
const document = root._ownerDocument;
whatToShow = whatToShow === undefined ?
core.NodeFilter.SHOW_ALL :
(whatToShow & core.NodeFilter.SHOW_ALL) >>> 0; // >>> makes sure the result is unsigned
filter = filter || null;
const it = Object.create(core.NodeIterator.prototype);
const internal = new NodeIteratorInternal(document, root, whatToShow, filter);
it[INTERNAL] = internal;
document._activeNodeIterators.push(internal);
while (document._activeNodeIterators.length > document._activeNodeIteratorsMax) {
const internalOther = document._activeNodeIterators.shift();
internalOther.active = false;
}
return it;
};
core.NodeIterator = function NodeIterator() {
throw new TypeError("Illegal constructor");
};
defineGetter(core.NodeIterator.prototype, "root", function () {
return this[INTERNAL].root;
});
defineGetter(core.NodeIterator.prototype, "referenceNode", function () {
const internal = this[INTERNAL];
internal.throwIfNotActive();
return internal.referenceNode;
});
defineGetter(core.NodeIterator.prototype, "pointerBeforeReferenceNode", function () {
const internal = this[INTERNAL];
internal.throwIfNotActive();
return internal.pointerBeforeReferenceNode;
});
defineGetter(core.NodeIterator.prototype, "whatToShow", function () {
return this[INTERNAL].whatToShow;
});
defineGetter(core.NodeIterator.prototype, "filter", function () {
return this[INTERNAL].filter;
});
core.NodeIterator.prototype.previousNode = function () {
const internal = this[INTERNAL];
internal.throwIfNotActive();
return internal.traverse(false);
};
core.NodeIterator.prototype.nextNode = function () {
const internal = this[INTERNAL];
internal.throwIfNotActive();
return internal.traverse(true);
};
core.NodeIterator.prototype.detach = function () {
// noop
};
core.NodeIterator.prototype.toString = function () {
return "[object NodeIterator]";
};
};