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