| 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. |
| 17 |
* |
| 18 |
* @author [email protected] (Robby Walker) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.dom.pattern.Tag'); |
| 22 |
|
| 23 |
goog.require('goog.dom.pattern'); |
| 24 |
goog.require('goog.dom.pattern.AbstractPattern'); |
| 25 |
goog.require('goog.dom.pattern.MatchType'); |
| 26 |
goog.require('goog.object'); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Pattern object that matches an tag. |
| 32 |
* |
| 33 |
* @param {string|RegExp} tag Name of the tag. Also will accept a regular |
| 34 |
* expression to match against the tag name. |
| 35 |
* @param {goog.dom.TagWalkType} type Type of token to match. |
| 36 |
* @param {Object=} opt_attrs Optional map of attribute names to desired values. |
| 37 |
* This pattern will only match when all attributes are present and match |
| 38 |
* the string or regular expression value provided here. |
| 39 |
* @param {Object=} opt_styles Optional map of CSS style names to desired |
| 40 |
* values. This pattern will only match when all styles are present and |
| 41 |
* match the string or regular expression value provided here. |
| 42 |
* @param {Function=} opt_test Optional function that takes the element as a |
| 43 |
* parameter and returns true if this pattern should match it. |
| 44 |
* @constructor |
| 45 |
* @extends {goog.dom.pattern.AbstractPattern} |
| 46 |
*/ |
| 47 |
goog.dom.pattern.Tag = function(tag, type, opt_attrs, opt_styles, opt_test) { |
| 48 |
/** |
| 49 |
* The tag to match. |
| 50 |
* |
| 51 |
* @private {string|RegExp} |
| 52 |
*/ |
| 53 |
this.tag_ = goog.isString(tag) ? tag.toUpperCase() : tag; |
| 54 |
|
| 55 |
/** |
| 56 |
* The type of token to match. |
| 57 |
* |
| 58 |
* @private {goog.dom.TagWalkType} |
| 59 |
*/ |
| 60 |
this.type_ = type; |
| 61 |
|
| 62 |
/** |
| 63 |
* The attributes to test for. |
| 64 |
* |
| 65 |
* @private {Object} |
| 66 |
*/ |
| 67 |
this.attrs_ = opt_attrs || null; |
| 68 |
|
| 69 |
/** |
| 70 |
* The styles to test for. |
| 71 |
* |
| 72 |
* @private {Object} |
| 73 |
*/ |
| 74 |
this.styles_ = opt_styles || null; |
| 75 |
|
| 76 |
/** |
| 77 |
* Function that takes the element as a parameter and returns true if this |
| 78 |
* pattern should match it. |
| 79 |
* |
| 80 |
* @private {Function} |
| 81 |
*/ |
| 82 |
this.test_ = opt_test || null; |
| 83 |
}; |
| 84 |
goog.inherits(goog.dom.pattern.Tag, goog.dom.pattern.AbstractPattern); |
| 85 |
|
| 86 |
|
| 87 |
/** |
| 88 |
* Test whether the given token is a tag token which matches the tag name, |
| 89 |
* style, and attributes provided in the constructor. |
| 90 |
* |
| 91 |
* @param {Node} token Token to match against. |
| 92 |
* @param {goog.dom.TagWalkType} type The type of token. |
| 93 |
* @return {goog.dom.pattern.MatchType} <code>MATCH</code> if the pattern |
| 94 |
* matches, <code>NO_MATCH</code> otherwise. |
| 95 |
* @override |
| 96 |
*/ |
| 97 |
goog.dom.pattern.Tag.prototype.matchToken = function(token, type) { |
| 98 |
// Check the direction and tag name. |
| 99 |
if (type == this.type_ && |
| 100 |
goog.dom.pattern.matchStringOrRegex(this.tag_, token.nodeName)) { |
| 101 |
// Check the attributes. |
| 102 |
if (this.attrs_ && |
| 103 |
!goog.object.every( |
| 104 |
this.attrs_, goog.dom.pattern.matchStringOrRegexMap, token)) { |
| 105 |
return goog.dom.pattern.MatchType.NO_MATCH; |
| 106 |
} |
| 107 |
// Check the styles. |
| 108 |
if (this.styles_ && |
| 109 |
!goog.object.every( |
| 110 |
this.styles_, goog.dom.pattern.matchStringOrRegexMap, |
| 111 |
token.style)) { |
| 112 |
return goog.dom.pattern.MatchType.NO_MATCH; |
| 113 |
} |
| 114 |
|
| 115 |
if (this.test_ && !this.test_(token)) { |
| 116 |
return goog.dom.pattern.MatchType.NO_MATCH; |
| 117 |
} |
| 118 |
|
| 119 |
// If we reach this point, we have a match and should save it. |
| 120 |
this.matchedNode = token; |
| 121 |
return goog.dom.pattern.MatchType.MATCH; |
| 122 |
} |
| 123 |
|
| 124 |
return goog.dom.pattern.MatchType.NO_MATCH; |
| 125 |
}; |