Debian Codegraph @

closure-library/closure/goog/dom/pattern/fulltag.js

raw
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.FullTag');
22
23 goog.require('goog.dom.pattern.MatchType');
24 goog.require('goog.dom.pattern.StartTag');
25 goog.require('goog.dom.pattern.Tag');
26
27
28
29 /**
30 * Pattern object that matches a full tag including all its children.
31 *
32 * @param {string|RegExp} tag Name of the tag. Also will accept a regular
33 * expression to match against the tag name.
34 * @param {Object=} opt_attrs Optional map of attribute names to desired values.
35 * This pattern will only match when all attributes are present and match
36 * the string or regular expression value provided here.
37 * @param {Object=} opt_styles Optional map of CSS style names to desired
38 * values. This pattern will only match when all styles are present and
39 * match the string or regular expression value provided here.
40 * @param {Function=} opt_test Optional function that takes the element as a
41 * parameter and returns true if this pattern should match it.
42 * @constructor
43 * @extends {goog.dom.pattern.StartTag}
44 * @final
45 */
46 goog.dom.pattern.FullTag = function(tag, opt_attrs, opt_styles, opt_test) {
47 /**
48 * Tracks the matcher's depth to detect the end of the tag.
49 *
50 * @private {number}
51 */
52 this.depth_ = 0;
53
54 goog.dom.pattern.FullTag.base(
55 this, 'constructor', tag, opt_attrs, opt_styles, opt_test);
56 };
57 goog.inherits(goog.dom.pattern.FullTag, goog.dom.pattern.StartTag);
58
59
60 /**
61 * Test whether the given token is a start tag token which matches the tag name,
62 * style, and attributes provided in the constructor.
63 *
64 * @param {Node} token Token to match against.
65 * @param {goog.dom.TagWalkType} type The type of token.
66 * @return {goog.dom.pattern.MatchType} <code>MATCH</code> at the end of our
67 * tag, <code>MATCHING</code> if we are within the tag, and
68 * <code>NO_MATCH</code> if the starting tag does not match.
69 * @override
70 */
71 goog.dom.pattern.FullTag.prototype.matchToken = function(token, type) {
72 if (!this.depth_) {
73 // If we have not yet started, make sure we match as a StartTag.
74 if (goog.dom.pattern.Tag.prototype.matchToken.call(this, token, type)) {
75 this.depth_ = type;
76 return goog.dom.pattern.MatchType.MATCHING;
77
78 } else {
79 return goog.dom.pattern.MatchType.NO_MATCH;
80 }
81 } else {
82 this.depth_ += type;
83
84 return this.depth_ ? goog.dom.pattern.MatchType.MATCHING :
85 goog.dom.pattern.MatchType.MATCH;
86 }
87 };