| 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 Useful callback functions for the DOM matcher. |
| 17 |
* |
| 18 |
* @author [email protected] (Robby Walker) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.dom.pattern.callback'); |
| 22 |
|
| 23 |
goog.require('goog.dom'); |
| 24 |
goog.require('goog.dom.TagWalkType'); |
| 25 |
goog.require('goog.iter'); |
| 26 |
|
| 27 |
|
| 28 |
/** |
| 29 |
* Callback function for use in {@link goog.dom.pattern.Matcher.addPattern} |
| 30 |
* that removes the matched node from the tree. Should be used in conjunciton |
| 31 |
* with a {@link goog.dom.pattern.StartTag} pattern. |
| 32 |
* |
| 33 |
* @param {Node} node The node matched by the pattern. |
| 34 |
* @param {goog.dom.TagIterator} position The position where the match |
| 35 |
* finished. |
| 36 |
* @return {boolean} Returns true to indicate tree changes were made. |
| 37 |
*/ |
| 38 |
goog.dom.pattern.callback.removeNode = function(node, position) { |
| 39 |
// Find out which position would be next. |
| 40 |
position.setPosition(node, goog.dom.TagWalkType.END_TAG); |
| 41 |
|
| 42 |
goog.iter.nextOrValue(position, null); |
| 43 |
|
| 44 |
// Remove the node. |
| 45 |
goog.dom.removeNode(node); |
| 46 |
|
| 47 |
// Correct for the depth change. |
| 48 |
position.depth -= 1; |
| 49 |
|
| 50 |
// Indicate that we made position/tree changes. |
| 51 |
return true; |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Callback function for use in {@link goog.dom.pattern.Matcher.addPattern} |
| 57 |
* that removes the matched node from the tree and replaces it with its |
| 58 |
* children. Should be used in conjunction with a |
| 59 |
* {@link goog.dom.pattern.StartTag} pattern. |
| 60 |
* |
| 61 |
* @param {Element} node The node matched by the pattern. |
| 62 |
* @param {goog.dom.TagIterator} position The position where the match |
| 63 |
* finished. |
| 64 |
* @return {boolean} Returns true to indicate tree changes were made. |
| 65 |
*/ |
| 66 |
goog.dom.pattern.callback.flattenElement = function(node, position) { |
| 67 |
// Find out which position would be next. |
| 68 |
position.setPosition( |
| 69 |
node, node.firstChild ? goog.dom.TagWalkType.START_TAG : |
| 70 |
goog.dom.TagWalkType.END_TAG); |
| 71 |
|
| 72 |
goog.iter.nextOrValue(position, null); |
| 73 |
|
| 74 |
// Flatten the node. |
| 75 |
goog.dom.flattenElement(node); |
| 76 |
|
| 77 |
// Correct for the depth change. |
| 78 |
position.depth -= 1; |
| 79 |
|
| 80 |
// Indicate that we made position/tree changes. |
| 81 |
return true; |
| 82 |
}; |