Path: blob/trunk/third_party/closure/goog/dom/pattern/pattern.js
2868 views
// Copyright 2007 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview DOM patterns. Allows for description of complex DOM patterns16* using regular expression like constructs.17*18* @author [email protected] (Robby Walker)19*/2021goog.provide('goog.dom.pattern');22goog.provide('goog.dom.pattern.MatchType');232425/**26* Utility function to match a string against either a string or a regular27* expression.28*29* @param {string|RegExp} obj Either a string or a regular expression.30* @param {string} str The string to match.31* @return {boolean} Whether the strings are equal, or if the string matches32* the regular expression.33*/34goog.dom.pattern.matchStringOrRegex = function(obj, str) {35if (goog.isString(obj)) {36// Match a string37return str == obj;38} else {39// Match a regular expression40return !!(str && str.match(obj));41}42};434445/**46* Utility function to match a DOM attribute against either a string or a47* regular expression. Conforms to the interface spec for48* {@link goog.object#every}.49*50* @param {string|RegExp} elem Either a string or a regular expression.51* @param {string} index The attribute name to match.52* @param {Object} orig The original map of matches to test.53* @return {boolean} Whether the strings are equal, or if the attribute matches54* the regular expression.55* @this {Element} Called using goog.object every on an Element.56*/57goog.dom.pattern.matchStringOrRegexMap = function(elem, index, orig) {58return goog.dom.pattern.matchStringOrRegex(59elem, index in this ?60this[index] :61(this.getAttribute ? this.getAttribute(index) : null));62};636465/**66* When matched to a token, a pattern may return any of the following statuses:67* <ol>68* <li><code>NO_MATCH</code> - The pattern does not match. This is the only69* value that evaluates to <code>false</code> in a boolean context.70* <li><code>MATCHING</code> - The token is part of an incomplete match.71* <li><code>MATCH</code> - The token completes a match.72* <li><code>BACKTRACK_MATCH</code> - The token does not match, but indicates73* the end of a repetitive match. For instance, in regular expressions,74* the pattern <code>/a+/</code> would match <code>'aaaaaaaab'</code>.75* Every <code>'a'</code> token would give a status of76* <code>MATCHING</code> while the <code>'b'</code> token would give a77* status of <code>BACKTRACK_MATCH</code>.78* </ol>79* @enum {number}80*/81goog.dom.pattern.MatchType = {82NO_MATCH: 0,83MATCHING: 1,84MATCH: 2,85BACKTRACK_MATCH: 386};878889