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