Path: blob/trunk/third_party/closure/goog/dom/iter.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 Iterators over DOM nodes.16*17* @author [email protected] (Robby Walker)18*/1920goog.provide('goog.dom.iter.AncestorIterator');21goog.provide('goog.dom.iter.ChildIterator');22goog.provide('goog.dom.iter.SiblingIterator');2324goog.require('goog.iter.Iterator');25goog.require('goog.iter.StopIteration');26272829/**30* Iterator over a Node's siblings.31* @param {Node} node The node to start with.32* @param {boolean=} opt_includeNode Whether to return the given node as the33* first return value from next.34* @param {boolean=} opt_reverse Whether to traverse siblings in reverse35* document order.36* @constructor37* @extends {goog.iter.Iterator}38*/39goog.dom.iter.SiblingIterator = function(node, opt_includeNode, opt_reverse) {40/**41* The current node, or null if iteration is finished.42* @type {Node}43* @private44*/45this.node_ = node;4647/**48* Whether to iterate in reverse.49* @type {boolean}50* @private51*/52this.reverse_ = !!opt_reverse;5354if (node && !opt_includeNode) {55this.next();56}57};58goog.inherits(goog.dom.iter.SiblingIterator, goog.iter.Iterator);596061/** @override */62goog.dom.iter.SiblingIterator.prototype.next = function() {63var node = this.node_;64if (!node) {65throw goog.iter.StopIteration;66}67this.node_ = this.reverse_ ? node.previousSibling : node.nextSibling;68return node;69};70717273/**74* Iterator over an Element's children.75* @param {Element} element The element to iterate over.76* @param {boolean=} opt_reverse Optionally traverse children from last to77* first.78* @param {number=} opt_startIndex Optional starting index.79* @constructor80* @extends {goog.dom.iter.SiblingIterator}81* @final82*/83goog.dom.iter.ChildIterator = function(element, opt_reverse, opt_startIndex) {84if (!goog.isDef(opt_startIndex)) {85opt_startIndex = opt_reverse && element.childNodes.length ?86element.childNodes.length - 1 :870;88}89goog.dom.iter.SiblingIterator.call(90this, element.childNodes[opt_startIndex], true, opt_reverse);91};92goog.inherits(goog.dom.iter.ChildIterator, goog.dom.iter.SiblingIterator);93949596/**97* Iterator over a Node's ancestors, stopping after the document body.98* @param {Node} node The node to start with.99* @param {boolean=} opt_includeNode Whether to return the given node as the100* first return value from next.101* @constructor102* @extends {goog.iter.Iterator}103* @final104*/105goog.dom.iter.AncestorIterator = function(node, opt_includeNode) {106/**107* The current node, or null if iteration is finished.108* @type {Node}109* @private110*/111this.node_ = node;112113if (node && !opt_includeNode) {114this.next();115}116};117goog.inherits(goog.dom.iter.AncestorIterator, goog.iter.Iterator);118119120/** @override */121goog.dom.iter.AncestorIterator.prototype.next = function() {122var node = this.node_;123if (!node) {124throw goog.iter.StopIteration;125}126this.node_ = node.parentNode;127return node;128};129130131