1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview DOM pattern to match a tag and all of its children.
*
* @author robbyw@google.com (Robby Walker)
*/
goog.provide('goog.dom.pattern.Repeat');
goog.require('goog.dom.NodeType');
goog.require('goog.dom.pattern.AbstractPattern');
goog.require('goog.dom.pattern.MatchType');
/**
* Pattern object that matches a repetition of another pattern.
* @param {goog.dom.pattern.AbstractPattern} pattern The pattern to
* repetitively match.
* @param {number=} opt_minimum The minimum number of times to match. Defaults
* to 0.
* @param {number=} opt_maximum The maximum number of times to match. Defaults
* to unlimited.
* @constructor
* @extends {goog.dom.pattern.AbstractPattern}
* @final
*/
goog.dom.pattern.Repeat = function(pattern, opt_minimum, opt_maximum) {
/**
* Pattern to repetitively match.
*
* @private {goog.dom.pattern.AbstractPattern}
*/
this.pattern_ = pattern;
/**
* Minimum number of times to match the pattern.
*
* @private {number}
*/
this.minimum_ = opt_minimum || 0;
/**
* Optional maximum number of times to match the pattern. A {@code null} value
* will be treated as infinity.
*
* @private {?number}
*/
this.maximum_ = opt_maximum || null;
/**
* The matched nodes.
*
* @type {Array<Node>}
*/
this.matches = [];
/**
* Number of times the pattern has matched.
*
* @type {number}
*/
this.count = 0;
/**
* Whether the pattern has recently matched or failed to match and will need
* to be reset when starting a new round of matches.
*
* @private {boolean}
*/
this.needsReset_ = false;
};
goog.inherits(goog.dom.pattern.Repeat, goog.dom.pattern.AbstractPattern);
/**
* Test whether the given token continues a repeated series of matches of the
* pattern given in the constructor.
*
* @param {Node} token Token to match against.
* @param {goog.dom.TagWalkType} type The type of token.
* @return {goog.dom.pattern.MatchType} <code>MATCH</code> if the pattern
* matches, <code>BACKTRACK_MATCH</code> if the pattern does not match
* but already had accumulated matches, <code>MATCHING</code> if the pattern
* starts a match, and <code>NO_MATCH</code> if the pattern does not match.
* @suppress {missingProperties} See the broken line below.
* @override
*/
goog.dom.pattern.Repeat.prototype.matchToken = function(token, type) {
// Reset if we're starting a new match
if (this.needsReset_) {
this.reset();
}
// If the option is set, ignore any whitespace only text nodes
if (token.nodeType == goog.dom.NodeType.TEXT &&
token.nodeValue.match(/^\s+$/)) {
return goog.dom.pattern.MatchType.MATCHING;
}
switch (this.pattern_.matchToken(token, type)) {
case goog.dom.pattern.MatchType.MATCH:
// Record the first token we match.
if (this.count == 0) {
this.matchedNode = token;
}
// Mark the match
this.count++;
// Add to the list
this.matches.push(this.pattern_.matchedNode);
// Check if this match hits our maximum
if (this.maximum_ !== null && this.count == this.maximum_) {
this.needsReset_ = true;
return goog.dom.pattern.MatchType.MATCH;
} else {
return goog.dom.pattern.MatchType.MATCHING;
}
case goog.dom.pattern.MatchType.MATCHING:
// This can happen when our child pattern is a sequence or a repetition.
return goog.dom.pattern.MatchType.MATCHING;
case goog.dom.pattern.MatchType.BACKTRACK_MATCH:
// This happens if our child pattern is repetitive too.
// TODO(robbyw): Backtrack further if necessary.
this.count++;
// NOTE(nicksantos): This line of code is broken. this.patterns_ doesn't
// exist, and this.currentPosition_ doesn't exist. When this is fixed,
// remove the missingProperties suppression above.
if (this.currentPosition_ == this.patterns_.length) {
this.needsReset_ = true;
return goog.dom.pattern.MatchType.BACKTRACK_MATCH;
} else {
// Retry the same token on the next iteration of the child pattern.
return this.matchToken(token, type);
}
default:
this.needsReset_ = true;
if (this.count >= this.minimum_) {
return goog.dom.pattern.MatchType.BACKTRACK_MATCH;
} else {
return goog.dom.pattern.MatchType.NO_MATCH;
}
}
};
/**
* Reset any internal state this pattern keeps.
* @override
*/
goog.dom.pattern.Repeat.prototype.reset = function() {
this.pattern_.reset();
this.count = 0;
this.needsReset_ = false;
this.matches.length = 0;
};
|