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