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