childmatches.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2007 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 DOM pattern to match any children of a tag, and
  16. * specifically collect those that match a child pattern.
  17. *
  18. * @author robbyw@google.com (Robby Walker)
  19. */
  20. goog.provide('goog.dom.pattern.ChildMatches');
  21. goog.require('goog.dom.pattern.AllChildren');
  22. goog.require('goog.dom.pattern.MatchType');
  23. /**
  24. * Pattern object that matches any nodes at or below the current tree depth.
  25. *
  26. * @param {goog.dom.pattern.AbstractPattern} childPattern Pattern to collect
  27. * child matches of.
  28. * @param {number=} opt_minimumMatches Enforce a minimum nuber of matches.
  29. * Defaults to 0.
  30. * @constructor
  31. * @extends {goog.dom.pattern.AllChildren}
  32. * @final
  33. */
  34. goog.dom.pattern.ChildMatches = function(childPattern, opt_minimumMatches) {
  35. /**
  36. * The child pattern to collect matches from.
  37. *
  38. * @private {goog.dom.pattern.AbstractPattern}
  39. */
  40. this.childPattern_ = childPattern;
  41. /**
  42. * Array of matched child nodes.
  43. *
  44. * @type {Array<Node>}
  45. */
  46. this.matches = [];
  47. /**
  48. * Minimum number of matches.
  49. *
  50. * @private {number}
  51. */
  52. this.minimumMatches_ = opt_minimumMatches || 0;
  53. /**
  54. * Whether the pattern has recently matched or failed to match and will need
  55. * to be reset when starting a new round of matches.
  56. *
  57. * @private {boolean}
  58. */
  59. this.needsReset_ = false;
  60. goog.dom.pattern.ChildMatches.base(this, 'constructor');
  61. };
  62. goog.inherits(goog.dom.pattern.ChildMatches, goog.dom.pattern.AllChildren);
  63. /**
  64. * Test whether the given token is on the same level.
  65. *
  66. * @param {Node} token Token to match against.
  67. * @param {goog.dom.TagWalkType} type The type of token.
  68. * @return {goog.dom.pattern.MatchType} {@code MATCHING} if the token is on the
  69. * same level or deeper and {@code BACKTRACK_MATCH} if not.
  70. * @override
  71. */
  72. goog.dom.pattern.ChildMatches.prototype.matchToken = function(token, type) {
  73. // Defer resets so we maintain our matches array until the last possible time.
  74. if (this.needsReset_) {
  75. this.reset();
  76. }
  77. // Call the super-method to ensure we stay in the child tree.
  78. var status =
  79. goog.dom.pattern.AllChildren.prototype.matchToken.apply(this, arguments);
  80. switch (status) {
  81. case goog.dom.pattern.MatchType.MATCHING:
  82. var backtrack = false;
  83. switch (this.childPattern_.matchToken(token, type)) {
  84. case goog.dom.pattern.MatchType.BACKTRACK_MATCH:
  85. backtrack = true;
  86. case goog.dom.pattern.MatchType.MATCH:
  87. // Collect the match.
  88. this.matches.push(this.childPattern_.matchedNode);
  89. break;
  90. default:
  91. // Keep trying if we haven't hit a terminal state.
  92. break;
  93. }
  94. if (backtrack) {
  95. // The only interesting result is a MATCH, since BACKTRACK_MATCH means
  96. // we are hitting an infinite loop on something like a Repeat(0).
  97. if (this.childPattern_.matchToken(token, type) ==
  98. goog.dom.pattern.MatchType.MATCH) {
  99. this.matches.push(this.childPattern_.matchedNode);
  100. }
  101. }
  102. return goog.dom.pattern.MatchType.MATCHING;
  103. case goog.dom.pattern.MatchType.BACKTRACK_MATCH:
  104. // TODO(robbyw): this should return something like BACKTRACK_NO_MATCH
  105. // when we don't meet our minimum.
  106. this.needsReset_ = true;
  107. return (this.matches.length >= this.minimumMatches_) ?
  108. goog.dom.pattern.MatchType.BACKTRACK_MATCH :
  109. goog.dom.pattern.MatchType.NO_MATCH;
  110. default:
  111. this.needsReset_ = true;
  112. return status;
  113. }
  114. };
  115. /**
  116. * Reset any internal state this pattern keeps.
  117. * @override
  118. */
  119. goog.dom.pattern.ChildMatches.prototype.reset = function() {
  120. this.needsReset_ = false;
  121. this.matches.length = 0;
  122. this.childPattern_.reset();
  123. goog.dom.pattern.AllChildren.prototype.reset.call(this);
  124. };