pattern.js 3.1 KB

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