Path: blob/trunk/third_party/closure/goog/dom/nodeiterator.js
2868 views
// Copyright 2008 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Iterator subclass for DOM tree traversal.16*17* @author [email protected] (Robby Walker)18*/1920goog.provide('goog.dom.NodeIterator');2122goog.require('goog.dom.TagIterator');23242526/**27* A DOM tree traversal iterator.28*29* Starting with the given node, the iterator walks the DOM in order, reporting30* events for each node. The iterator acts as a prefix iterator:31*32* <pre>33* <div>1<span>2</span>3</div>34* </pre>35*36* Will return the following nodes:37*38* <code>[div, 1, span, 2, 3]</code>39*40* With the following depths41*42* <code>[1, 1, 2, 2, 1]</code>43*44* Imagining <code>|</code> represents iterator position, the traversal stops at45* each of the following locations:46*47* <pre><div>|1|<span>|2|</span>3|</div></pre>48*49* The iterator can also be used in reverse mode, which will return the nodes50* and states in the opposite order. The depths will be slightly different51* since, like in normal mode, the depth is computed *after* the last move.52*53* Lastly, it is possible to create an iterator that is unconstrained, meaning54* that it will continue iterating until the end of the document instead of55* until exiting the start node.56*57* @param {Node=} opt_node The start node. Defaults to an empty iterator.58* @param {boolean=} opt_reversed Whether to traverse the tree in reverse.59* @param {boolean=} opt_unconstrained Whether the iterator is not constrained60* to the starting node and its children.61* @param {number=} opt_depth The starting tree depth.62* @constructor63* @extends {goog.dom.TagIterator}64* @final65*/66goog.dom.NodeIterator = function(67opt_node, opt_reversed, opt_unconstrained, opt_depth) {68goog.dom.TagIterator.call(69this, opt_node, opt_reversed, opt_unconstrained, null, opt_depth);70};71goog.inherits(goog.dom.NodeIterator, goog.dom.TagIterator);727374/**75* Moves to the next position in the DOM tree.76* @return {Node} Returns the next node, or throws a goog.iter.StopIteration77* exception if the end of the iterator's range has been reached.78* @override79*/80goog.dom.NodeIterator.prototype.next = function() {81do {82goog.dom.NodeIterator.superClass_.next.call(this);83} while (this.isEndTag());8485return this.node;86};878889