Path: blob/trunk/third_party/closure/goog/dom/nodeiterator.js
4506 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Iterator subclass for DOM tree traversal.8*/910goog.provide('goog.dom.NodeIterator');1112goog.require('goog.dom.TagIterator');13goog.require('goog.iter');14151617/**18* A DOM tree traversal iterator.19*20* Starting with the given node, the iterator walks the DOM in order, reporting21* events for each node. The iterator acts as a prefix iterator:22*23* <pre>24* <div>1<span>2</span>3</div>25* </pre>26*27* Will return the following nodes:28*29* <code>[div, 1, span, 2, 3]</code>30*31* With the following depths32*33* <code>[1, 1, 2, 2, 1]</code>34*35* Imagining <code>|</code> represents iterator position, the traversal stops at36* each of the following locations:37*38* <pre><div>|1|<span>|2|</span>3|</div></pre>39*40* The iterator can also be used in reverse mode, which will return the nodes41* and states in the opposite order. The depths will be slightly different42* since, like in normal mode, the depth is computed *after* the last move.43*44* Lastly, it is possible to create an iterator that is unconstrained, meaning45* that it will continue iterating until the end of the document instead of46* until exiting the start node.47*48* @param {Node=} opt_node The start node. Defaults to an empty iterator.49* @param {boolean=} opt_reversed Whether to traverse the tree in reverse.50* @param {boolean=} opt_unconstrained Whether the iterator is not constrained51* to the starting node and its children.52* @param {number=} opt_depth The starting tree depth.53* @constructor54* @extends {goog.dom.TagIterator}55* @final56*/57goog.dom.NodeIterator = function(58opt_node, opt_reversed, opt_unconstrained, opt_depth) {59'use strict';60goog.dom.TagIterator.call(61this, opt_node, opt_reversed, opt_unconstrained, null, opt_depth);62};63goog.inherits(goog.dom.NodeIterator, goog.dom.TagIterator);646566/**67* Moves to the next position in the DOM tree.68* @return {!IIterableResult<!Node>}69* @override70*/71goog.dom.NodeIterator.prototype.next = function() {72'use strict';73do {74// also updates `this.node` reference on iteration.75const it = goog.dom.NodeIterator.superClass_.next.call(this);76if (it.done) return it;77} while (this.isEndTag());7879return goog.iter.createEs6IteratorYield(/** @type {!Node} */ (this.node));80};818283