Debian Codegraph @

mixmark-io-domino/lib/TreeWalker.js

raw symbols SCIP
1 "use strict";
2 module.exports = TreeWalker;
3
4 var Node = require('./Node');
5 var NodeFilter = require('./NodeFilter');
6 var NodeTraversal = require('./NodeTraversal');
7 var utils = require('./utils');
8
9 var mapChild = {
10 first: 'firstChild',
11 last: 'lastChild',
12 next: 'firstChild',
13 previous: 'lastChild'
14 };
15
16 var mapSibling = {
17 first: 'nextSibling',
18 last: 'previousSibling',
19 next: 'nextSibling',
20 previous: 'previousSibling'
21 };
22
23 /* Private methods and helpers */
24
25 /**
26 * @spec https://dom.spec.whatwg.org/#concept-traverse-children
27 * @method
28 * @access private
29 * @param {TreeWalker} tw
30 * @param {string} type One of 'first' or 'last'.
31 * @return {Node|null}
32 */
33 function traverseChildren(tw, type) {
34 var child, node, parent, result, sibling;
35 node = tw._currentNode[mapChild[type]];
36 while (node !== null) {
37 result = tw._internalFilter(node);
38 if (result === NodeFilter.FILTER_ACCEPT) {
39 tw._currentNode = node;
40 return node;
41 }
42 if (result === NodeFilter.FILTER_SKIP) {
43 child = node[mapChild[type]];
44 if (child !== null) {
45 node = child;
46 continue;
47 }
48 }
49 while (node !== null) {
50 sibling = node[mapSibling[type]];
51 if (sibling !== null) {
52 node = sibling;
53 break;
54 }
55 parent = node.parentNode;
56 if (parent === null || parent === tw.root || parent === tw._currentNode) {
57 return null;
58 } else {
59 node = parent;
60 }
61 }
62 }
63 return null;
64 }
65
66 /**
67 * @spec https://dom.spec.whatwg.org/#concept-traverse-siblings
68 * @method
69 * @access private
70 * @param {TreeWalker} tw
71 * @param {TreeWalker} type One of 'next' or 'previous'.
72 * @return {Node|nul}
73 */
74 function traverseSiblings(tw, type) {
75 var node, result, sibling;
76 node = tw._currentNode;
77 if (node === tw.root) {
78 return null;
79 }
80 while (true) {
81 sibling = node[mapSibling[type]];
82 while (sibling !== null) {
83 node = sibling;
84 result = tw._internalFilter(node);
85 if (result === NodeFilter.FILTER_ACCEPT) {
86 tw._currentNode = node;
87 return node;
88 }
89 sibling = node[mapChild[type]];
90 if (result === NodeFilter.FILTER_REJECT || sibling === null) {
91 sibling = node[mapSibling[type]];
92 }
93 }
94 node = node.parentNode;
95 if (node === null || node === tw.root) {
96 return null;
97 }
98 if (tw._internalFilter(node) === NodeFilter.FILTER_ACCEPT) {
99 return null;
100 }
101 }
102 }
103
104
105 /* Public API */
106
107 /**
108 * Latest version: https://dom.spec.whatwg.org/#treewalker
109 *
110 * @constructor
111 * @param {Node} root
112 * @param {number} whatToShow [optional]
113 * @param {Function|NodeFilter} filter [optional]
114 * @throws Error
115 */
116 function TreeWalker(root, whatToShow, filter) {
117 if (!root || !root.nodeType) {
118 utils.NotSupportedError();
119 }
120
121 // Read-only properties
122 this._root = root;
123 this._whatToShow = Number(whatToShow) || 0;
124 this._filter = filter || null;
125 this._active = false;
126 // Read-write property
127 this._currentNode = root;
128 }
129
130 Object.defineProperties(TreeWalker.prototype, {
131 root: { get: function() { return this._root; } },
132 whatToShow: { get: function() { return this._whatToShow; } },
133 filter: { get: function() { return this._filter; } },
134
135 currentNode: {
136 get: function currentNode() {
137 return this._currentNode;
138 },
139 set: function setCurrentNode(v) {
140 if (!(v instanceof Node)) {
141 throw new TypeError("Not a Node"); // `null` is also not a node
142 }
143 this._currentNode = v;
144 },
145 },
146
147 /**
148 * @method
149 * @param {Node} node
150 * @return {Number} Constant NodeFilter.FILTER_ACCEPT,
151 * NodeFilter.FILTER_REJECT or NodeFilter.FILTER_SKIP.
152 */
153 _internalFilter: { value: function _internalFilter(node) {
154 /* jshint bitwise: false */
155 var result, filter;
156 if (this._active) {
157 utils.InvalidStateError();
158 }
159
160 // Maps nodeType to whatToShow
161 if (!(((1 << (node.nodeType - 1)) & this._whatToShow))) {
162 return NodeFilter.FILTER_SKIP;
163 }
164
165 filter = this._filter;
166 if (filter === null) {
167 result = NodeFilter.FILTER_ACCEPT;
168 } else {
169 this._active = true;
170 try {
171 if (typeof filter === 'function') {
172 result = filter(node);
173 } else {
174 result = filter.acceptNode(node);
175 }
176 } finally {
177 this._active = false;
178 }
179 }
180
181 // Note that coercing to a number means that
182 // `true` becomes `1` (which is NodeFilter.FILTER_ACCEPT)
183 // `false` becomes `0` (neither accept, reject, or skip)
184 return (+result);
185 }},
186
187 /**
188 * @spec https://dom.spec.whatwg.org/#dom-treewalker-parentnode
189 * @based on WebKit's TreeWalker::parentNode
190 * https://trac.webkit.org/browser/webkit/trunk/Source/WebCore/dom/TreeWalker.cpp?rev=220453#L50
191 * @method
192 * @return {Node|null}
193 */
194 parentNode: { value: function parentNode() {
195 var node = this._currentNode;
196 while (node !== this.root) {
197 node = node.parentNode;
198 if (node === null) {
199 return null;
200 }
201 if (this._internalFilter(node) === NodeFilter.FILTER_ACCEPT) {
202 this._currentNode = node;
203 return node;
204 }
205 }
206 return null;
207 }},
208
209 /**
210 * @spec https://dom.spec.whatwg.org/#dom-treewalker-firstchild
211 * @method
212 * @return {Node|null}
213 */
214 firstChild: { value: function firstChild() {
215 return traverseChildren(this, 'first');
216 }},
217
218 /**
219 * @spec https://dom.spec.whatwg.org/#dom-treewalker-lastchild
220 * @method
221 * @return {Node|null}
222 */
223 lastChild: { value: function lastChild() {
224 return traverseChildren(this, 'last');
225 }},
226
227 /**
228 * @spec http://www.w3.org/TR/dom/#dom-treewalker-previoussibling
229 * @method
230 * @return {Node|null}
231 */
232 previousSibling: { value: function previousSibling() {
233 return traverseSiblings(this, 'previous');
234 }},
235
236 /**
237 * @spec http://www.w3.org/TR/dom/#dom-treewalker-nextsibling
238 * @method
239 * @return {Node|null}
240 */
241 nextSibling: { value: function nextSibling() {
242 return traverseSiblings(this, 'next');
243 }},
244
245 /**
246 * @spec https://dom.spec.whatwg.org/#dom-treewalker-previousnode
247 * @based on WebKit's TreeWalker::previousNode
248 * https://trac.webkit.org/browser/webkit/trunk/Source/WebCore/dom/TreeWalker.cpp?rev=220453#L181
249 * @method
250 * @return {Node|null}
251 */
252 previousNode: { value: function previousNode() {
253 var node, result, previousSibling, lastChild;
254 node = this._currentNode;
255 while (node !== this._root) {
256 for (previousSibling = node.previousSibling;
257 previousSibling;
258 previousSibling = node.previousSibling) {
259 node = previousSibling;
260 result = this._internalFilter(node);
261 if (result === NodeFilter.FILTER_REJECT) {
262 continue;
263 }
264 for (lastChild = node.lastChild;
265 lastChild;
266 lastChild = node.lastChild) {
267 node = lastChild;
268 result = this._internalFilter(node);
269 if (result === NodeFilter.FILTER_REJECT) {
270 break;
271 }
272 }
273 if (result === NodeFilter.FILTER_ACCEPT) {
274 this._currentNode = node;
275 return node;
276 }
277 }
278 if (node === this.root || node.parentNode === null) {
279 return null;
280 }
281 node = node.parentNode;
282 if (this._internalFilter(node) === NodeFilter.FILTER_ACCEPT) {
283 this._currentNode = node;
284 return node;
285 }
286 }
287 return null;
288 }},
289
290 /**
291 * @spec https://dom.spec.whatwg.org/#dom-treewalker-nextnode
292 * @based on WebKit's TreeWalker::nextNode
293 * https://trac.webkit.org/browser/webkit/trunk/Source/WebCore/dom/TreeWalker.cpp?rev=220453#L228
294 * @method
295 * @return {Node|null}
296 */
297 nextNode: { value: function nextNode() {
298 var node, result, firstChild, nextSibling;
299 node = this._currentNode;
300 result = NodeFilter.FILTER_ACCEPT;
301
302 CHILDREN:
303 while (true) {
304 for (firstChild = node.firstChild;
305 firstChild;
306 firstChild = node.firstChild) {
307 node = firstChild;
308 result = this._internalFilter(node);
309 if (result === NodeFilter.FILTER_ACCEPT) {
310 this._currentNode = node;
311 return node;
312 } else if (result === NodeFilter.FILTER_REJECT) {
313 break;
314 }
315 }
316 for (nextSibling = NodeTraversal.nextSkippingChildren(node, this.root);
317 nextSibling;
318 nextSibling = NodeTraversal.nextSkippingChildren(node, this.root)) {
319 node = nextSibling;
320 result = this._internalFilter(node);
321 if (result === NodeFilter.FILTER_ACCEPT) {
322 this._currentNode = node;
323 return node;
324 } else if (result === NodeFilter.FILTER_SKIP) {
325 continue CHILDREN;
326 }
327 }
328 return null;
329 }
330 }},
331
332 /** For compatibility with web-platform-tests. */
333 toString: { value: function toString() {
334 return "[object TreeWalker]";
335 }},
336 });