| 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 patterns. Allows for description of complex DOM patterns |
| 17 |
* using regular expression like constructs. |
| 18 |
* |
| 19 |
* @author [email protected] (Robby Walker) |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.dom.pattern'); |
| 23 |
goog.provide('goog.dom.pattern.MatchType'); |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* Utility function to match a string against either a string or a regular |
| 28 |
* expression. |
| 29 |
* |
| 30 |
* @param {string|RegExp} obj Either a string or a regular expression. |
| 31 |
* @param {string} str The string to match. |
| 32 |
* @return {boolean} Whether the strings are equal, or if the string matches |
| 33 |
* the regular expression. |
| 34 |
*/ |
| 35 |
goog.dom.pattern.matchStringOrRegex = function(obj, str) { |
| 36 |
if (goog.isString(obj)) { |
| 37 |
// Match a string |
| 38 |
return str == obj; |
| 39 |
} else { |
| 40 |
// Match a regular expression |
| 41 |
return !!(str && str.match(obj)); |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
/** |
| 47 |
* Utility function to match a DOM attribute against either a string or a |
| 48 |
* regular expression. Conforms to the interface spec for |
| 49 |
* {@link goog.object#every}. |
| 50 |
* |
| 51 |
* @param {string|RegExp} elem Either a string or a regular expression. |
| 52 |
* @param {string} index The attribute name to match. |
| 53 |
* @param {Object} orig The original map of matches to test. |
| 54 |
* @return {boolean} Whether the strings are equal, or if the attribute matches |
| 55 |
* the regular expression. |
| 56 |
* @this {Element} Called using goog.object every on an Element. |
| 57 |
*/ |
| 58 |
goog.dom.pattern.matchStringOrRegexMap = function(elem, index, orig) { |
| 59 |
return goog.dom.pattern.matchStringOrRegex( |
| 60 |
elem, index in this ? |
| 61 |
this[index] : |
| 62 |
(this.getAttribute ? this.getAttribute(index) : null)); |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* When matched to a token, a pattern may return any of the following statuses: |
| 68 |
* <ol> |
| 69 |
* <li><code>NO_MATCH</code> - The pattern does not match. This is the only |
| 70 |
* value that evaluates to <code>false</code> in a boolean context. |
| 71 |
* <li><code>MATCHING</code> - The token is part of an incomplete match. |
| 72 |
* <li><code>MATCH</code> - The token completes a match. |
| 73 |
* <li><code>BACKTRACK_MATCH</code> - The token does not match, but indicates |
| 74 |
* the end of a repetitive match. For instance, in regular expressions, |
| 75 |
* the pattern <code>/a+/</code> would match <code>'aaaaaaaab'</code>. |
| 76 |
* Every <code>'a'</code> token would give a status of |
| 77 |
* <code>MATCHING</code> while the <code>'b'</code> token would give a |
| 78 |
* status of <code>BACKTRACK_MATCH</code>. |
| 79 |
* </ol> |
| 80 |
* @enum {number} |
| 81 |
*/ |
| 82 |
goog.dom.pattern.MatchType = { |
| 83 |
NO_MATCH: 0, |
| 84 |
MATCHING: 1, |
| 85 |
MATCH: 2, |
| 86 |
BACKTRACK_MATCH: 3 |
| 87 |
}; |