| 1 |
// Copyright 2009 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 Opera 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 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
goog.provide('goog.dom.browserrange.OperaRange'); |
| 25 |
|
| 26 |
goog.require('goog.dom.browserrange.W3cRange'); |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* The constructor for Opera specific browser ranges. |
| 32 |
* @param {Range} range The range object. |
| 33 |
* @constructor |
| 34 |
* @extends {goog.dom.browserrange.W3cRange} |
| 35 |
* @final |
| 36 |
*/ |
| 37 |
goog.dom.browserrange.OperaRange = function(range) { |
| 38 |
goog.dom.browserrange.W3cRange.call(this, range); |
| 39 |
}; |
| 40 |
goog.inherits(goog.dom.browserrange.OperaRange, goog.dom.browserrange.W3cRange); |
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* Creates a range object that selects the given node's text. |
| 45 |
* @param {Node} node The node to select. |
| 46 |
* @return {!goog.dom.browserrange.OperaRange} A Opera range wrapper object. |
| 47 |
*/ |
| 48 |
goog.dom.browserrange.OperaRange.createFromNodeContents = function(node) { |
| 49 |
return new goog.dom.browserrange.OperaRange( |
| 50 |
goog.dom.browserrange.W3cRange.getBrowserRangeForNode(node)); |
| 51 |
}; |
| 52 |
|
| 53 |
|
| 54 |
/** |
| 55 |
* Creates a range object that selects between the given nodes. |
| 56 |
* @param {Node} startNode The node to start with. |
| 57 |
* @param {number} startOffset The offset within the node to start. |
| 58 |
* @param {Node} endNode The node to end with. |
| 59 |
* @param {number} endOffset The offset within the node to end. |
| 60 |
* @return {!goog.dom.browserrange.OperaRange} A wrapper object. |
| 61 |
*/ |
| 62 |
goog.dom.browserrange.OperaRange.createFromNodes = function( |
| 63 |
startNode, startOffset, endNode, endOffset) { |
| 64 |
return new goog.dom.browserrange.OperaRange( |
| 65 |
goog.dom.browserrange.W3cRange.getBrowserRangeForNodes( |
| 66 |
startNode, startOffset, endNode, endOffset)); |
| 67 |
}; |
| 68 |
|
| 69 |
|
| 70 |
/** @override */ |
| 71 |
goog.dom.browserrange.OperaRange.prototype.selectInternal = function( |
| 72 |
selection, reversed) { |
| 73 |
// Avoid using addRange as we have to removeAllRanges first, which |
| 74 |
// blurs editable fields in Opera. |
| 75 |
selection.collapse(this.getStartNode(), this.getStartOffset()); |
| 76 |
if (this.getEndNode() != this.getStartNode() || |
| 77 |
this.getEndOffset() != this.getStartOffset()) { |
| 78 |
selection.extend(this.getEndNode(), this.getEndOffset()); |
| 79 |
} |
| 80 |
// This can happen if the range isn't in an editable field. |
| 81 |
if (selection.rangeCount == 0) { |
| 82 |
selection.addRange(this.range_); |
| 83 |
} |
| 84 |
}; |