sequence.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 sequence of other patterns.
  16. *
  17. * @author robbyw@google.com (Robby Walker)
  18. */
  19. goog.provide('goog.dom.pattern.Sequence');
  20. goog.require('goog.dom.NodeType');
  21. goog.require('goog.dom.pattern');
  22. goog.require('goog.dom.pattern.AbstractPattern');
  23. goog.require('goog.dom.pattern.MatchType');
  24. /**
  25. * Pattern object that matches a sequence of other patterns.
  26. *
  27. * @param {Array<goog.dom.pattern.AbstractPattern>} patterns Ordered array of
  28. * patterns to match.
  29. * @param {boolean=} opt_ignoreWhitespace Optional flag to ignore text nodes
  30. * consisting entirely of whitespace. The default is to not ignore them.
  31. * @constructor
  32. * @extends {goog.dom.pattern.AbstractPattern}
  33. * @final
  34. */
  35. goog.dom.pattern.Sequence = function(patterns, opt_ignoreWhitespace) {
  36. /**
  37. * Ordered array of patterns to match.
  38. *
  39. * @type {Array<goog.dom.pattern.AbstractPattern>}
  40. */
  41. this.patterns = patterns;
  42. /**
  43. * Whether or not to ignore whitespace only Text nodes.
  44. *
  45. * @private {boolean}
  46. */
  47. this.ignoreWhitespace_ = !!opt_ignoreWhitespace;
  48. /**
  49. * Position in the patterns array we have reached by successful matches.
  50. *
  51. * @private {number}
  52. */
  53. this.currentPosition_ = 0;
  54. };
  55. goog.inherits(goog.dom.pattern.Sequence, goog.dom.pattern.AbstractPattern);
  56. /**
  57. * Regular expression for breaking text nodes.
  58. * @private {!RegExp}
  59. */
  60. goog.dom.pattern.Sequence.BREAKING_TEXTNODE_RE_ = /^\s*$/;
  61. /**
  62. * Test whether the given token starts, continues, or finishes the sequence
  63. * of patterns given in the constructor.
  64. *
  65. * @param {Node} token Token to match against.
  66. * @param {goog.dom.TagWalkType} type The type of token.
  67. * @return {goog.dom.pattern.MatchType} <code>MATCH</code> if the pattern
  68. * matches, <code>MATCHING</code> if the pattern starts a match, and
  69. * <code>NO_MATCH</code> if the pattern does not match.
  70. * @override
  71. */
  72. goog.dom.pattern.Sequence.prototype.matchToken = function(token, type) {
  73. // If the option is set, ignore any whitespace only text nodes
  74. if (this.ignoreWhitespace_ && token.nodeType == goog.dom.NodeType.TEXT &&
  75. goog.dom.pattern.Sequence.BREAKING_TEXTNODE_RE_.test(token.nodeValue)) {
  76. return goog.dom.pattern.MatchType.MATCHING;
  77. }
  78. switch (this.patterns[this.currentPosition_].matchToken(token, type)) {
  79. case goog.dom.pattern.MatchType.MATCH:
  80. // Record the first token we match.
  81. if (this.currentPosition_ == 0) {
  82. this.matchedNode = token;
  83. }
  84. // Move forward one position.
  85. this.currentPosition_++;
  86. // Check if this is the last position.
  87. if (this.currentPosition_ == this.patterns.length) {
  88. this.reset();
  89. return goog.dom.pattern.MatchType.MATCH;
  90. } else {
  91. return goog.dom.pattern.MatchType.MATCHING;
  92. }
  93. case goog.dom.pattern.MatchType.MATCHING:
  94. // This can happen when our child pattern is a sequence or a repetition.
  95. return goog.dom.pattern.MatchType.MATCHING;
  96. case goog.dom.pattern.MatchType.BACKTRACK_MATCH:
  97. // This means a repetitive match succeeded 1 token ago.
  98. // TODO(robbyw): Backtrack further if necessary.
  99. this.currentPosition_++;
  100. if (this.currentPosition_ == this.patterns.length) {
  101. this.reset();
  102. return goog.dom.pattern.MatchType.BACKTRACK_MATCH;
  103. } else {
  104. // Retry the same token on the next pattern.
  105. return this.matchToken(token, type);
  106. }
  107. default:
  108. this.reset();
  109. return goog.dom.pattern.MatchType.NO_MATCH;
  110. }
  111. };
  112. /**
  113. * Reset any internal state this pattern keeps.
  114. * @override
  115. */
  116. goog.dom.pattern.Sequence.prototype.reset = function() {
  117. if (this.patterns[this.currentPosition_]) {
  118. this.patterns[this.currentPosition_].reset();
  119. }
  120. this.currentPosition_ = 0;
  121. };