abstractmultirange.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright 2008 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 Utilities for working with ranges comprised of multiple
  16. * sub-ranges.
  17. *
  18. * @author robbyw@google.com (Robby Walker)
  19. */
  20. goog.provide('goog.dom.AbstractMultiRange');
  21. goog.require('goog.array');
  22. goog.require('goog.dom');
  23. goog.require('goog.dom.AbstractRange');
  24. goog.require('goog.dom.TextRange');
  25. /**
  26. * Creates a new multi range with no properties. Do not use this
  27. * constructor: use one of the goog.dom.Range.createFrom* methods instead.
  28. * @constructor
  29. * @extends {goog.dom.AbstractRange}
  30. */
  31. goog.dom.AbstractMultiRange = function() {};
  32. goog.inherits(goog.dom.AbstractMultiRange, goog.dom.AbstractRange);
  33. /** @override */
  34. goog.dom.AbstractMultiRange.prototype.containsRange = function(
  35. otherRange, opt_allowPartial) {
  36. // TODO(user): This will incorrectly return false if two (or more) adjacent
  37. // elements are both in the control range, and are also in the text range
  38. // being compared to.
  39. var /** !Array<?goog.dom.TextRange> */ ranges = this.getTextRanges();
  40. var otherRanges = otherRange.getTextRanges();
  41. var fn = opt_allowPartial ? goog.array.some : goog.array.every;
  42. return fn(otherRanges, function(otherRange) {
  43. return goog.array.some(ranges, function(range) {
  44. return range.containsRange(otherRange, opt_allowPartial);
  45. });
  46. });
  47. };
  48. /** @override */
  49. goog.dom.AbstractMultiRange.prototype.containsNode = function(
  50. node, opt_allowPartial) {
  51. return this.containsRange(
  52. goog.dom.TextRange.createFromNodeContents(node), opt_allowPartial);
  53. };
  54. /** @override */
  55. goog.dom.AbstractMultiRange.prototype.insertNode = function(node, before) {
  56. if (before) {
  57. goog.dom.insertSiblingBefore(node, this.getStartNode());
  58. } else {
  59. goog.dom.insertSiblingAfter(node, this.getEndNode());
  60. }
  61. return node;
  62. };
  63. /** @override */
  64. goog.dom.AbstractMultiRange.prototype.surroundWithNodes = function(
  65. startNode, endNode) {
  66. this.insertNode(startNode, true);
  67. this.insertNode(endNode, false);
  68. };