nodeiterator.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. * @fileoverview Iterator subclass for DOM tree traversal.
  16. *
  17. * @author robbyw@google.com (Robby Walker)
  18. */
  19. goog.provide('goog.dom.NodeIterator');
  20. goog.require('goog.dom.TagIterator');
  21. /**
  22. * A DOM tree traversal iterator.
  23. *
  24. * Starting with the given node, the iterator walks the DOM in order, reporting
  25. * events for each node. The iterator acts as a prefix iterator:
  26. *
  27. * <pre>
  28. * &lt;div&gt;1&lt;span&gt;2&lt;/span&gt;3&lt;/div&gt;
  29. * </pre>
  30. *
  31. * Will return the following nodes:
  32. *
  33. * <code>[div, 1, span, 2, 3]</code>
  34. *
  35. * With the following depths
  36. *
  37. * <code>[1, 1, 2, 2, 1]</code>
  38. *
  39. * Imagining <code>|</code> represents iterator position, the traversal stops at
  40. * each of the following locations:
  41. *
  42. * <pre>&lt;div&gt;|1|&lt;span&gt;|2|&lt;/span&gt;3|&lt;/div&gt;</pre>
  43. *
  44. * The iterator can also be used in reverse mode, which will return the nodes
  45. * and states in the opposite order. The depths will be slightly different
  46. * since, like in normal mode, the depth is computed *after* the last move.
  47. *
  48. * Lastly, it is possible to create an iterator that is unconstrained, meaning
  49. * that it will continue iterating until the end of the document instead of
  50. * until exiting the start node.
  51. *
  52. * @param {Node=} opt_node The start node. Defaults to an empty iterator.
  53. * @param {boolean=} opt_reversed Whether to traverse the tree in reverse.
  54. * @param {boolean=} opt_unconstrained Whether the iterator is not constrained
  55. * to the starting node and its children.
  56. * @param {number=} opt_depth The starting tree depth.
  57. * @constructor
  58. * @extends {goog.dom.TagIterator}
  59. * @final
  60. */
  61. goog.dom.NodeIterator = function(
  62. opt_node, opt_reversed, opt_unconstrained, opt_depth) {
  63. goog.dom.TagIterator.call(
  64. this, opt_node, opt_reversed, opt_unconstrained, null, opt_depth);
  65. };
  66. goog.inherits(goog.dom.NodeIterator, goog.dom.TagIterator);
  67. /**
  68. * Moves to the next position in the DOM tree.
  69. * @return {Node} Returns the next node, or throws a goog.iter.StopIteration
  70. * exception if the end of the iterator's range has been reached.
  71. * @override
  72. */
  73. goog.dom.NodeIterator.prototype.next = function() {
  74. do {
  75. goog.dom.NodeIterator.superClass_.next.call(this);
  76. } while (this.isEndTag());
  77. return this.node;
  78. };