| 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 browser range namespace and interface, as |
| 17 |
* well as several useful utility functions. |
| 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'); |
| 26 |
goog.provide('goog.dom.browserrange.Error'); |
| 27 |
|
| 28 |
goog.require('goog.dom'); |
| 29 |
goog.require('goog.dom.BrowserFeature'); |
| 30 |
goog.require('goog.dom.NodeType'); |
| 31 |
goog.require('goog.dom.browserrange.GeckoRange'); |
| 32 |
goog.require('goog.dom.browserrange.IeRange'); |
| 33 |
goog.require('goog.dom.browserrange.OperaRange'); |
| 34 |
goog.require('goog.dom.browserrange.W3cRange'); |
| 35 |
goog.require('goog.dom.browserrange.WebKitRange'); |
| 36 |
goog.require('goog.userAgent'); |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* Common error constants. |
| 41 |
* @enum {string} |
| 42 |
*/ |
| 43 |
goog.dom.browserrange.Error = { |
| 44 |
NOT_IMPLEMENTED: 'Not Implemented' |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
// NOTE(robbyw): While it would be nice to eliminate the duplicate switches |
| 49 |
// below, doing so uncovers bugs in the JsCompiler in which |
| 50 |
// necessary code is stripped out. |
| 51 |
|
| 52 |
|
| 53 |
/** |
| 54 |
* Static method that returns the proper type of browser range. |
| 55 |
* @param {Range|TextRange} range A browser range object. |
| 56 |
* @return {!goog.dom.browserrange.AbstractRange} A wrapper object. |
| 57 |
*/ |
| 58 |
goog.dom.browserrange.createRange = function(range) { |
| 59 |
if (goog.dom.BrowserFeature.LEGACY_IE_RANGES) { |
| 60 |
return new goog.dom.browserrange.IeRange( |
| 61 |
/** @type {TextRange} */ (range), |
| 62 |
goog.dom.getOwnerDocument(range.parentElement())); |
| 63 |
} else if (goog.userAgent.WEBKIT) { |
| 64 |
return new goog.dom.browserrange.WebKitRange( |
| 65 |
/** @type {Range} */ (range)); |
| 66 |
} else if (goog.userAgent.GECKO) { |
| 67 |
return new goog.dom.browserrange.GeckoRange( |
| 68 |
/** @type {Range} */ (range)); |
| 69 |
} else if (goog.userAgent.OPERA) { |
| 70 |
return new goog.dom.browserrange.OperaRange( |
| 71 |
/** @type {Range} */ (range)); |
| 72 |
} else { |
| 73 |
// Default other browsers, including Opera, to W3c ranges. |
| 74 |
return new goog.dom.browserrange.W3cRange( |
| 75 |
/** @type {Range} */ (range)); |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
/** |
| 81 |
* Static method that returns the proper type of browser range. |
| 82 |
* @param {Node} node The node to select. |
| 83 |
* @return {!goog.dom.browserrange.AbstractRange} A wrapper object. |
| 84 |
*/ |
| 85 |
goog.dom.browserrange.createRangeFromNodeContents = function(node) { |
| 86 |
if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9)) { |
| 87 |
return goog.dom.browserrange.IeRange.createFromNodeContents(node); |
| 88 |
} else if (goog.userAgent.WEBKIT) { |
| 89 |
return goog.dom.browserrange.WebKitRange.createFromNodeContents(node); |
| 90 |
} else if (goog.userAgent.GECKO) { |
| 91 |
return goog.dom.browserrange.GeckoRange.createFromNodeContents(node); |
| 92 |
} else if (goog.userAgent.OPERA) { |
| 93 |
return goog.dom.browserrange.OperaRange.createFromNodeContents(node); |
| 94 |
} else { |
| 95 |
// Default other browsers to W3c ranges. |
| 96 |
return goog.dom.browserrange.W3cRange.createFromNodeContents(node); |
| 97 |
} |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
/** |
| 102 |
* Static method that returns the proper type of browser range. |
| 103 |
* @param {Node} startNode The node to start with. |
| 104 |
* @param {number} startOffset The offset within the node to start. This is |
| 105 |
* either the index into the childNodes array for element startNodes or |
| 106 |
* the index into the character array for text startNodes. |
| 107 |
* @param {Node} endNode The node to end with. |
| 108 |
* @param {number} endOffset The offset within the node to end. This is |
| 109 |
* either the index into the childNodes array for element endNodes or |
| 110 |
* the index into the character array for text endNodes. |
| 111 |
* @return {!goog.dom.browserrange.AbstractRange} A wrapper object. |
| 112 |
*/ |
| 113 |
goog.dom.browserrange.createRangeFromNodes = function( |
| 114 |
startNode, startOffset, endNode, endOffset) { |
| 115 |
if (goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(9)) { |
| 116 |
return goog.dom.browserrange.IeRange.createFromNodes( |
| 117 |
startNode, startOffset, endNode, endOffset); |
| 118 |
} else if (goog.userAgent.WEBKIT) { |
| 119 |
return goog.dom.browserrange.WebKitRange.createFromNodes( |
| 120 |
startNode, startOffset, endNode, endOffset); |
| 121 |
} else if (goog.userAgent.GECKO) { |
| 122 |
return goog.dom.browserrange.GeckoRange.createFromNodes( |
| 123 |
startNode, startOffset, endNode, endOffset); |
| 124 |
} else if (goog.userAgent.OPERA) { |
| 125 |
return goog.dom.browserrange.OperaRange.createFromNodes( |
| 126 |
startNode, startOffset, endNode, endOffset); |
| 127 |
} else { |
| 128 |
// Default other browsers to W3c ranges. |
| 129 |
return goog.dom.browserrange.W3cRange.createFromNodes( |
| 130 |
startNode, startOffset, endNode, endOffset); |
| 131 |
} |
| 132 |
}; |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* Tests whether the given node can contain a range end point. |
| 137 |
* @param {Node} node The node to check. |
| 138 |
* @return {boolean} Whether the given node can contain a range end point. |
| 139 |
*/ |
| 140 |
goog.dom.browserrange.canContainRangeEndpoint = function(node) { |
| 141 |
// NOTE(user, bloom): This is not complete, as divs with style - |
| 142 |
// 'display:inline-block' or 'position:absolute' can also not contain range |
| 143 |
// endpoints. A more complete check is to see if that element can be partially |
| 144 |
// selected (can be container) or not. |
| 145 |
return goog.dom.canHaveChildren(node) || |
| 146 |
node.nodeType == goog.dom.NodeType.TEXT; |
| 147 |
}; |