| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
/** |
| 16 |
* @fileoverview Iterators over DOM nodes. |
| 17 |
* |
| 18 |
* @author [email protected] (Robby Walker) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.dom.iter.AncestorIterator'); |
| 22 |
goog.provide('goog.dom.iter.ChildIterator'); |
| 23 |
goog.provide('goog.dom.iter.SiblingIterator'); |
| 24 |
|
| 25 |
goog.require('goog.iter.Iterator'); |
| 26 |
goog.require('goog.iter.StopIteration'); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Iterator over a Node's siblings. |
| 32 |
* @param {Node} node The node to start with. |
| 33 |
* @param {boolean=} opt_includeNode Whether to return the given node as the |
| 34 |
* first return value from next. |
| 35 |
* @param {boolean=} opt_reverse Whether to traverse siblings in reverse |
| 36 |
* document order. |
| 37 |
* @constructor |
| 38 |
* @extends {goog.iter.Iterator} |
| 39 |
*/ |
| 40 |
goog.dom.iter.SiblingIterator = function(node, opt_includeNode, opt_reverse) { |
| 41 |
/** |
| 42 |
* The current node, or null if iteration is finished. |
| 43 |
* @type {Node} |
| 44 |
* @private |
| 45 |
*/ |
| 46 |
this.node_ = node; |
| 47 |
|
| 48 |
/** |
| 49 |
* Whether to iterate in reverse. |
| 50 |
* @type {boolean} |
| 51 |
* @private |
| 52 |
*/ |
| 53 |
this.reverse_ = !!opt_reverse; |
| 54 |
|
| 55 |
if (node && !opt_includeNode) { |
| 56 |
this.next(); |
| 57 |
} |
| 58 |
}; |
| 59 |
goog.inherits(goog.dom.iter.SiblingIterator, goog.iter.Iterator); |
| 60 |
|
| 61 |
|
| 62 |
/** @override */ |
| 63 |
goog.dom.iter.SiblingIterator.prototype.next = function() { |
| 64 |
var node = this.node_; |
| 65 |
if (!node) { |
| 66 |
throw goog.iter.StopIteration; |
| 67 |
} |
| 68 |
this.node_ = this.reverse_ ? node.previousSibling : node.nextSibling; |
| 69 |
return node; |
| 70 |
}; |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
/** |
| 75 |
* Iterator over an Element's children. |
| 76 |
* @param {Element} element The element to iterate over. |
| 77 |
* @param {boolean=} opt_reverse Optionally traverse children from last to |
| 78 |
* first. |
| 79 |
* @param {number=} opt_startIndex Optional starting index. |
| 80 |
* @constructor |
| 81 |
* @extends {goog.dom.iter.SiblingIterator} |
| 82 |
* @final |
| 83 |
*/ |
| 84 |
goog.dom.iter.ChildIterator = function(element, opt_reverse, opt_startIndex) { |
| 85 |
if (!goog.isDef(opt_startIndex)) { |
| 86 |
opt_startIndex = opt_reverse && element.childNodes.length ? |
| 87 |
element.childNodes.length - 1 : |
| 88 |
0; |
| 89 |
} |
| 90 |
goog.dom.iter.SiblingIterator.call( |
| 91 |
this, element.childNodes[opt_startIndex], true, opt_reverse); |
| 92 |
}; |
| 93 |
goog.inherits(goog.dom.iter.ChildIterator, goog.dom.iter.SiblingIterator); |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
/** |
| 98 |
* Iterator over a Node's ancestors, stopping after the document body. |
| 99 |
* @param {Node} node The node to start with. |
| 100 |
* @param {boolean=} opt_includeNode Whether to return the given node as the |
| 101 |
* first return value from next. |
| 102 |
* @constructor |
| 103 |
* @extends {goog.iter.Iterator} |
| 104 |
* @final |
| 105 |
*/ |
| 106 |
goog.dom.iter.AncestorIterator = function(node, opt_includeNode) { |
| 107 |
/** |
| 108 |
* The current node, or null if iteration is finished. |
| 109 |
* @type {Node} |
| 110 |
* @private |
| 111 |
*/ |
| 112 |
this.node_ = node; |
| 113 |
|
| 114 |
if (node && !opt_includeNode) { |
| 115 |
this.next(); |
| 116 |
} |
| 117 |
}; |
| 118 |
goog.inherits(goog.dom.iter.AncestorIterator, goog.iter.Iterator); |
| 119 |
|
| 120 |
|
| 121 |
/** @override */ |
| 122 |
goog.dom.iter.AncestorIterator.prototype.next = function() { |
| 123 |
var node = this.node_; |
| 124 |
if (!node) { |
| 125 |
throw goog.iter.StopIteration; |
| 126 |
} |
| 127 |
this.node_ = node.parentNode; |
| 128 |
return node; |
| 129 |
}; |