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