test.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 Callback object that tests if a pattern matches at least once.
  16. *
  17. * @author robbyw@google.com (Robby Walker)
  18. */
  19. goog.provide('goog.dom.pattern.callback.Test');
  20. goog.require('goog.iter.StopIteration');
  21. /**
  22. * Callback class for testing for at least one match.
  23. * @constructor
  24. * @final
  25. */
  26. goog.dom.pattern.callback.Test = function() {
  27. /**
  28. * Whether or not the pattern matched.
  29. *
  30. * @type {boolean}
  31. */
  32. this.matched = false;
  33. /**
  34. * The callback function. Suitable as a callback for
  35. * {@link goog.dom.pattern.Matcher}.
  36. * @private {Function}
  37. */
  38. this.callback_ = null;
  39. };
  40. /**
  41. * Get a bound callback function that is suitable as a callback for
  42. * {@link goog.dom.pattern.Matcher}.
  43. *
  44. * @return {!Function} A callback function.
  45. */
  46. goog.dom.pattern.callback.Test.prototype.getCallback = function() {
  47. if (!this.callback_) {
  48. this.callback_ = goog.bind(function(node, position) {
  49. // Mark our match.
  50. this.matched = true;
  51. // Stop searching.
  52. throw goog.iter.StopIteration;
  53. }, this);
  54. }
  55. return this.callback_;
  56. };
  57. /**
  58. * Reset the counter.
  59. */
  60. goog.dom.pattern.callback.Test.prototype.reset = function() {
  61. this.matched = false;
  62. };