Path: blob/trunk/third_party/closure/goog/dom/abstractmultirange.js
2868 views
// Copyright 2008 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Utilities for working with ranges comprised of multiple16* sub-ranges.17*18* @author [email protected] (Robby Walker)19*/202122goog.provide('goog.dom.AbstractMultiRange');2324goog.require('goog.array');25goog.require('goog.dom');26goog.require('goog.dom.AbstractRange');27goog.require('goog.dom.TextRange');28293031/**32* Creates a new multi range with no properties. Do not use this33* constructor: use one of the goog.dom.Range.createFrom* methods instead.34* @constructor35* @extends {goog.dom.AbstractRange}36*/37goog.dom.AbstractMultiRange = function() {};38goog.inherits(goog.dom.AbstractMultiRange, goog.dom.AbstractRange);394041/** @override */42goog.dom.AbstractMultiRange.prototype.containsRange = function(43otherRange, opt_allowPartial) {44// TODO(user): This will incorrectly return false if two (or more) adjacent45// elements are both in the control range, and are also in the text range46// being compared to.47var /** !Array<?goog.dom.TextRange> */ ranges = this.getTextRanges();48var otherRanges = otherRange.getTextRanges();4950var fn = opt_allowPartial ? goog.array.some : goog.array.every;51return fn(otherRanges, function(otherRange) {52return goog.array.some(ranges, function(range) {53return range.containsRange(otherRange, opt_allowPartial);54});55});56};575859/** @override */60goog.dom.AbstractMultiRange.prototype.containsNode = function(61node, opt_allowPartial) {62return this.containsRange(63goog.dom.TextRange.createFromNodeContents(node), opt_allowPartial);64};65666768/** @override */69goog.dom.AbstractMultiRange.prototype.insertNode = function(node, before) {70if (before) {71goog.dom.insertSiblingBefore(node, this.getStartNode());72} else {73goog.dom.insertSiblingAfter(node, this.getEndNode());74}75return node;76};777879/** @override */80goog.dom.AbstractMultiRange.prototype.surroundWithNodes = function(81startNode, endNode) {82this.insertNode(startNode, true);83this.insertNode(endNode, false);84};858687