Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/third_party/closure/goog/dom/nodeiterator.js
4007 views
1
/**
2
* @license
3
* Copyright The Closure Library Authors.
4
* SPDX-License-Identifier: Apache-2.0
5
*/
6
7
/**
8
* @fileoverview Iterator subclass for DOM tree traversal.
9
*/
10
11
goog.provide('goog.dom.NodeIterator');
12
13
goog.require('goog.dom.TagIterator');
14
goog.require('goog.iter');
15
16
17
18
/**
19
* A DOM tree traversal iterator.
20
*
21
* Starting with the given node, the iterator walks the DOM in order, reporting
22
* events for each node. The iterator acts as a prefix iterator:
23
*
24
* <pre>
25
* &lt;div&gt;1&lt;span&gt;2&lt;/span&gt;3&lt;/div&gt;
26
* </pre>
27
*
28
* Will return the following nodes:
29
*
30
* <code>[div, 1, span, 2, 3]</code>
31
*
32
* With the following depths
33
*
34
* <code>[1, 1, 2, 2, 1]</code>
35
*
36
* Imagining <code>|</code> represents iterator position, the traversal stops at
37
* each of the following locations:
38
*
39
* <pre>&lt;div&gt;|1|&lt;span&gt;|2|&lt;/span&gt;3|&lt;/div&gt;</pre>
40
*
41
* The iterator can also be used in reverse mode, which will return the nodes
42
* and states in the opposite order. The depths will be slightly different
43
* since, like in normal mode, the depth is computed *after* the last move.
44
*
45
* Lastly, it is possible to create an iterator that is unconstrained, meaning
46
* that it will continue iterating until the end of the document instead of
47
* until exiting the start node.
48
*
49
* @param {Node=} opt_node The start node. Defaults to an empty iterator.
50
* @param {boolean=} opt_reversed Whether to traverse the tree in reverse.
51
* @param {boolean=} opt_unconstrained Whether the iterator is not constrained
52
* to the starting node and its children.
53
* @param {number=} opt_depth The starting tree depth.
54
* @constructor
55
* @extends {goog.dom.TagIterator}
56
* @final
57
*/
58
goog.dom.NodeIterator = function(
59
opt_node, opt_reversed, opt_unconstrained, opt_depth) {
60
'use strict';
61
goog.dom.TagIterator.call(
62
this, opt_node, opt_reversed, opt_unconstrained, null, opt_depth);
63
};
64
goog.inherits(goog.dom.NodeIterator, goog.dom.TagIterator);
65
66
67
/**
68
* Moves to the next position in the DOM tree.
69
* @return {!IIterableResult<!Node>}
70
* @override
71
*/
72
goog.dom.NodeIterator.prototype.next = function() {
73
'use strict';
74
do {
75
// also updates `this.node` reference on iteration.
76
const it = goog.dom.NodeIterator.superClass_.next.call(this);
77
if (it.done) return it;
78
} while (this.isEndTag());
79
80
return goog.iter.createEs6IteratorYield(/** @type {!Node} */ (this.node));
81
};
82
83