repeat.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 a tag and all of its children.
  16. *
  17. * @author robbyw@google.com (Robby Walker)
  18. */
  19. goog.provide('goog.dom.pattern.Repeat');
  20. goog.require('goog.dom.NodeType');
  21. goog.require('goog.dom.pattern.AbstractPattern');
  22. goog.require('goog.dom.pattern.MatchType');
  23. /**
  24. * Pattern object that matches a repetition of another pattern.
  25. * @param {goog.dom.pattern.AbstractPattern} pattern The pattern to
  26. * repetitively match.
  27. * @param {number=} opt_minimum The minimum number of times to match. Defaults
  28. * to 0.
  29. * @param {number=} opt_maximum The maximum number of times to match. Defaults
  30. * to unlimited.
  31. * @constructor
  32. * @extends {goog.dom.pattern.AbstractPattern}
  33. * @final
  34. */
  35. goog.dom.pattern.Repeat = function(pattern, opt_minimum, opt_maximum) {
  36. /**
  37. * Pattern to repetitively match.
  38. *
  39. * @private {goog.dom.pattern.AbstractPattern}
  40. */
  41. this.pattern_ = pattern;
  42. /**
  43. * Minimum number of times to match the pattern.
  44. *
  45. * @private {number}
  46. */
  47. this.minimum_ = opt_minimum || 0;
  48. /**
  49. * Optional maximum number of times to match the pattern. A {@code null} value
  50. * will be treated as infinity.
  51. *
  52. * @private {?number}
  53. */
  54. this.maximum_ = opt_maximum || null;
  55. /**
  56. * The matched nodes.
  57. *
  58. * @type {Array<Node>}
  59. */
  60. this.matches = [];
  61. /**
  62. * Number of times the pattern has matched.
  63. *
  64. * @type {number}
  65. */
  66. this.count = 0;
  67. /**
  68. * Whether the pattern has recently matched or failed to match and will need
  69. * to be reset when starting a new round of matches.
  70. *
  71. * @private {boolean}
  72. */
  73. this.needsReset_ = false;
  74. };
  75. goog.inherits(goog.dom.pattern.Repeat, goog.dom.pattern.AbstractPattern);
  76. /**
  77. * Test whether the given token continues a repeated series of matches of the
  78. * pattern given in the constructor.
  79. *
  80. * @param {Node} token Token to match against.
  81. * @param {goog.dom.TagWalkType} type The type of token.
  82. * @return {goog.dom.pattern.MatchType} <code>MATCH</code> if the pattern
  83. * matches, <code>BACKTRACK_MATCH</code> if the pattern does not match
  84. * but already had accumulated matches, <code>MATCHING</code> if the pattern
  85. * starts a match, and <code>NO_MATCH</code> if the pattern does not match.
  86. * @suppress {missingProperties} See the broken line below.
  87. * @override
  88. */
  89. goog.dom.pattern.Repeat.prototype.matchToken = function(token, type) {
  90. // Reset if we're starting a new match
  91. if (this.needsReset_) {
  92. this.reset();
  93. }
  94. // If the option is set, ignore any whitespace only text nodes
  95. if (token.nodeType == goog.dom.NodeType.TEXT &&
  96. token.nodeValue.match(/^\s+$/)) {
  97. return goog.dom.pattern.MatchType.MATCHING;
  98. }
  99. switch (this.pattern_.matchToken(token, type)) {
  100. case goog.dom.pattern.MatchType.MATCH:
  101. // Record the first token we match.
  102. if (this.count == 0) {
  103. this.matchedNode = token;
  104. }
  105. // Mark the match
  106. this.count++;
  107. // Add to the list
  108. this.matches.push(this.pattern_.matchedNode);
  109. // Check if this match hits our maximum
  110. if (this.maximum_ !== null && this.count == this.maximum_) {
  111. this.needsReset_ = true;
  112. return goog.dom.pattern.MatchType.MATCH;
  113. } else {
  114. return goog.dom.pattern.MatchType.MATCHING;
  115. }
  116. case goog.dom.pattern.MatchType.MATCHING:
  117. // This can happen when our child pattern is a sequence or a repetition.
  118. return goog.dom.pattern.MatchType.MATCHING;
  119. case goog.dom.pattern.MatchType.BACKTRACK_MATCH:
  120. // This happens if our child pattern is repetitive too.
  121. // TODO(robbyw): Backtrack further if necessary.
  122. this.count++;
  123. // NOTE(nicksantos): This line of code is broken. this.patterns_ doesn't
  124. // exist, and this.currentPosition_ doesn't exist. When this is fixed,
  125. // remove the missingProperties suppression above.
  126. if (this.currentPosition_ == this.patterns_.length) {
  127. this.needsReset_ = true;
  128. return goog.dom.pattern.MatchType.BACKTRACK_MATCH;
  129. } else {
  130. // Retry the same token on the next iteration of the child pattern.
  131. return this.matchToken(token, type);
  132. }
  133. default:
  134. this.needsReset_ = true;
  135. if (this.count >= this.minimum_) {
  136. return goog.dom.pattern.MatchType.BACKTRACK_MATCH;
  137. } else {
  138. return goog.dom.pattern.MatchType.NO_MATCH;
  139. }
  140. }
  141. };
  142. /**
  143. * Reset any internal state this pattern keeps.
  144. * @override
  145. */
  146. goog.dom.pattern.Repeat.prototype.reset = function() {
  147. this.pattern_.reset();
  148. this.count = 0;
  149. this.needsReset_ = false;
  150. this.matches.length = 0;
  151. };