callback.js 2.6 KB

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