allchildren.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 pattern to match any children of a tag.
  16. *
  17. * @author robbyw@google.com (Robby Walker)
  18. */
  19. goog.provide('goog.dom.pattern.AllChildren');
  20. goog.require('goog.dom.pattern.AbstractPattern');
  21. goog.require('goog.dom.pattern.MatchType');
  22. /**
  23. * Pattern object that matches any nodes at or below the current tree depth.
  24. *
  25. * @constructor
  26. * @extends {goog.dom.pattern.AbstractPattern}
  27. */
  28. goog.dom.pattern.AllChildren = function() {
  29. /**
  30. * Tracks the matcher's depth to detect the end of the tag.
  31. *
  32. * @private {number}
  33. */
  34. this.depth_ = 0;
  35. };
  36. goog.inherits(goog.dom.pattern.AllChildren, goog.dom.pattern.AbstractPattern);
  37. /**
  38. * Test whether the given token is on the same level.
  39. *
  40. * @param {Node} token Token to match against.
  41. * @param {goog.dom.TagWalkType} type The type of token.
  42. * @return {goog.dom.pattern.MatchType} {@code MATCHING} if the token is on the
  43. * same level or deeper and {@code BACKTRACK_MATCH} if not.
  44. * @override
  45. */
  46. goog.dom.pattern.AllChildren.prototype.matchToken = function(token, type) {
  47. this.depth_ += type;
  48. if (this.depth_ >= 0) {
  49. return goog.dom.pattern.MatchType.MATCHING;
  50. } else {
  51. this.depth_ = 0;
  52. return goog.dom.pattern.MatchType.BACKTRACK_MATCH;
  53. }
  54. };
  55. /**
  56. * Reset any internal state this pattern keeps.
  57. * @override
  58. */
  59. goog.dom.pattern.AllChildren.prototype.reset = function() {
  60. this.depth_ = 0;
  61. };