| 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 Definition of the WebKit specific range wrapper. Inherits most |
| 17 |
* functionality from W3CRange, but adds exceptions as necessary. |
| 18 |
* |
| 19 |
* DO NOT USE THIS FILE DIRECTLY. Use goog.dom.Range instead. |
| 20 |
* |
| 21 |
* @author [email protected] (Robby Walker) |
| 22 |
*/ |
| 23 |
|
| 24 |
|
| 25 |
goog.provide('goog.dom.browserrange.WebKitRange'); |
| 26 |
|
| 27 |
goog.require('goog.dom.RangeEndpoint'); |
| 28 |
goog.require('goog.dom.browserrange.W3cRange'); |
| 29 |
goog.require('goog.userAgent'); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* The constructor for WebKit specific browser ranges. |
| 35 |
* @param {Range} range The range object. |
| 36 |
* @constructor |
| 37 |
* @extends {goog.dom.browserrange.W3cRange} |
| 38 |
* @final |
| 39 |
*/ |
| 40 |
goog.dom.browserrange.WebKitRange = function(range) { |
| 41 |
goog.dom.browserrange.W3cRange.call(this, range); |
| 42 |
}; |
| 43 |
goog.inherits( |
| 44 |
goog.dom.browserrange.WebKitRange, goog.dom.browserrange.W3cRange); |
| 45 |
|
| 46 |
|
| 47 |
/** |
| 48 |
* Creates a range object that selects the given node's text. |
| 49 |
* @param {Node} node The node to select. |
| 50 |
* @return {!goog.dom.browserrange.WebKitRange} A WebKit range wrapper object. |
| 51 |
*/ |
| 52 |
goog.dom.browserrange.WebKitRange.createFromNodeContents = function(node) { |
| 53 |
return new goog.dom.browserrange.WebKitRange( |
| 54 |
goog.dom.browserrange.W3cRange.getBrowserRangeForNode(node)); |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
/** |
| 59 |
* Creates a range object that selects between the given nodes. |
| 60 |
* @param {Node} startNode The node to start with. |
| 61 |
* @param {number} startOffset The offset within the start node. |
| 62 |
* @param {Node} endNode The node to end with. |
| 63 |
* @param {number} endOffset The offset within the end node. |
| 64 |
* @return {!goog.dom.browserrange.WebKitRange} A wrapper object. |
| 65 |
*/ |
| 66 |
goog.dom.browserrange.WebKitRange.createFromNodes = function( |
| 67 |
startNode, startOffset, endNode, endOffset) { |
| 68 |
return new goog.dom.browserrange.WebKitRange( |
| 69 |
goog.dom.browserrange.W3cRange.getBrowserRangeForNodes( |
| 70 |
startNode, startOffset, endNode, endOffset)); |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
/** @override */ |
| 75 |
goog.dom.browserrange.WebKitRange.prototype.compareBrowserRangeEndpoints = |
| 76 |
function(range, thisEndpoint, otherEndpoint) { |
| 77 |
// Webkit pre-528 has some bugs where compareBoundaryPoints() doesn't work the |
| 78 |
// way it is supposed to, but if we reverse the sense of two comparisons, |
| 79 |
// it works fine. |
| 80 |
// https://bugs.webkit.org/show_bug.cgi?id=20738 |
| 81 |
if (goog.userAgent.isVersionOrHigher('528')) { |
| 82 |
return ( |
| 83 |
goog.dom.browserrange.WebKitRange.superClass_ |
| 84 |
.compareBrowserRangeEndpoints.call( |
| 85 |
this, range, thisEndpoint, otherEndpoint)); |
| 86 |
} |
| 87 |
return this.range_.compareBoundaryPoints( |
| 88 |
otherEndpoint == goog.dom.RangeEndpoint.START ? |
| 89 |
(thisEndpoint == goog.dom.RangeEndpoint.START ? |
| 90 |
goog.global['Range'].START_TO_START : |
| 91 |
goog.global['Range'].END_TO_START) : // Sense reversed |
| 92 |
(thisEndpoint == goog.dom.RangeEndpoint.START ? |
| 93 |
goog.global['Range'].START_TO_END : // Sense reversed |
| 94 |
goog.global['Range'].END_TO_END), |
| 95 |
/** @type {Range} */ (range)); |
| 96 |
}; |
| 97 |
|
| 98 |
|
| 99 |
/** @override */ |
| 100 |
goog.dom.browserrange.WebKitRange.prototype.selectInternal = function( |
| 101 |
selection, reversed) { |
| 102 |
if (reversed) { |
| 103 |
selection.setBaseAndExtent( |
| 104 |
this.getEndNode(), this.getEndOffset(), this.getStartNode(), |
| 105 |
this.getStartOffset()); |
| 106 |
} else { |
| 107 |
selection.setBaseAndExtent( |
| 108 |
this.getStartNode(), this.getStartOffset(), this.getEndNode(), |
| 109 |
this.getEndOffset()); |
| 110 |
} |
| 111 |
}; |