range.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. * @fileoverview Utilities for working with ranges in HTML documents.
  16. *
  17. * @author robbyw@google.com (Robby Walker)
  18. */
  19. goog.provide('goog.dom.Range');
  20. goog.require('goog.dom');
  21. goog.require('goog.dom.AbstractRange');
  22. goog.require('goog.dom.BrowserFeature');
  23. goog.require('goog.dom.ControlRange');
  24. goog.require('goog.dom.MultiRange');
  25. goog.require('goog.dom.NodeType');
  26. goog.require('goog.dom.TextRange');
  27. /**
  28. * Create a new selection from the given browser window's current selection.
  29. * Note that this object does not auto-update if the user changes their
  30. * selection and should be used as a snapshot.
  31. * @param {Window=} opt_win The window to get the selection of. Defaults to the
  32. * window this class was defined in.
  33. * @return {goog.dom.AbstractRange?} A range wrapper object, or null if there
  34. * was an error.
  35. */
  36. goog.dom.Range.createFromWindow = function(opt_win) {
  37. var sel =
  38. goog.dom.AbstractRange.getBrowserSelectionForWindow(opt_win || window);
  39. return sel && goog.dom.Range.createFromBrowserSelection(sel);
  40. };
  41. /**
  42. * Create a new range wrapper from the given browser selection object. Note
  43. * that this object does not auto-update if the user changes their selection and
  44. * should be used as a snapshot.
  45. * @param {!Object} selection The browser selection object.
  46. * @return {goog.dom.AbstractRange?} A range wrapper object or null if there
  47. * was an error.
  48. */
  49. goog.dom.Range.createFromBrowserSelection = function(selection) {
  50. var range;
  51. var isReversed = false;
  52. if (selection.createRange) {
  53. try {
  54. range = selection.createRange();
  55. } catch (e) {
  56. // Access denied errors can be thrown here in IE if the selection was
  57. // a flash obj or if there are cross domain issues
  58. return null;
  59. }
  60. } else if (selection.rangeCount) {
  61. if (selection.rangeCount > 1) {
  62. return goog.dom.MultiRange.createFromBrowserSelection(
  63. /** @type {!Selection} */ (selection));
  64. } else {
  65. range = selection.getRangeAt(0);
  66. isReversed = goog.dom.Range.isReversed(
  67. selection.anchorNode, selection.anchorOffset, selection.focusNode,
  68. selection.focusOffset);
  69. }
  70. } else {
  71. return null;
  72. }
  73. return goog.dom.Range.createFromBrowserRange(range, isReversed);
  74. };
  75. /**
  76. * Create a new range wrapper from the given browser range object.
  77. * @param {Range|TextRange} range The browser range object.
  78. * @param {boolean=} opt_isReversed Whether the focus node is before the anchor
  79. * node.
  80. * @return {!goog.dom.AbstractRange} A range wrapper object.
  81. */
  82. goog.dom.Range.createFromBrowserRange = function(range, opt_isReversed) {
  83. // Create an IE control range when appropriate.
  84. return goog.dom.AbstractRange.isNativeControlRange(range) ?
  85. goog.dom.ControlRange.createFromBrowserRange(range) :
  86. goog.dom.TextRange.createFromBrowserRange(range, opt_isReversed);
  87. };
  88. /**
  89. * Create a new range wrapper that selects the given node's text.
  90. * @param {Node} node The node to select.
  91. * @param {boolean=} opt_isReversed Whether the focus node is before the anchor
  92. * node.
  93. * @return {!goog.dom.AbstractRange} A range wrapper object.
  94. */
  95. goog.dom.Range.createFromNodeContents = function(node, opt_isReversed) {
  96. return goog.dom.TextRange.createFromNodeContents(node, opt_isReversed);
  97. };
  98. /**
  99. * Create a new range wrapper that represents a caret at the given node,
  100. * accounting for the given offset. This always creates a TextRange, regardless
  101. * of whether node is an image node or other control range type node.
  102. * @param {Node} node The node to place a caret at.
  103. * @param {number} offset The offset within the node to place the caret at.
  104. * @return {!goog.dom.AbstractRange} A range wrapper object.
  105. */
  106. goog.dom.Range.createCaret = function(node, offset) {
  107. return goog.dom.TextRange.createFromNodes(node, offset, node, offset);
  108. };
  109. /**
  110. * Create a new range wrapper that selects the area between the given nodes,
  111. * accounting for the given offsets.
  112. * @param {Node} anchorNode The node to anchor on.
  113. * @param {number} anchorOffset The offset within the node to anchor on.
  114. * @param {Node} focusNode The node to focus on.
  115. * @param {number} focusOffset The offset within the node to focus on.
  116. * @return {!goog.dom.AbstractRange} A range wrapper object.
  117. */
  118. goog.dom.Range.createFromNodes = function(
  119. anchorNode, anchorOffset, focusNode, focusOffset) {
  120. return goog.dom.TextRange.createFromNodes(
  121. anchorNode, anchorOffset, focusNode, focusOffset);
  122. };
  123. /**
  124. * Clears the window's selection.
  125. * @param {Window=} opt_win The window to get the selection of. Defaults to the
  126. * window this class was defined in.
  127. */
  128. goog.dom.Range.clearSelection = function(opt_win) {
  129. var sel =
  130. goog.dom.AbstractRange.getBrowserSelectionForWindow(opt_win || window);
  131. if (!sel) {
  132. return;
  133. }
  134. if (sel.empty) {
  135. // We can't just check that the selection is empty, because IE
  136. // sometimes gets confused.
  137. try {
  138. sel.empty();
  139. } catch (e) {
  140. // Emptying an already empty selection throws an exception in IE
  141. }
  142. } else {
  143. try {
  144. sel.removeAllRanges();
  145. } catch (e) {
  146. // This throws in IE9 if the range has been invalidated; for example, if
  147. // the user clicked on an element which disappeared during the event
  148. // handler.
  149. }
  150. }
  151. };
  152. /**
  153. * Tests if the window has a selection.
  154. * @param {Window=} opt_win The window to check the selection of. Defaults to
  155. * the window this class was defined in.
  156. * @return {boolean} Whether the window has a selection.
  157. */
  158. goog.dom.Range.hasSelection = function(opt_win) {
  159. var sel =
  160. goog.dom.AbstractRange.getBrowserSelectionForWindow(opt_win || window);
  161. return !!sel &&
  162. (goog.dom.BrowserFeature.LEGACY_IE_RANGES ? sel.type != 'None' :
  163. !!sel.rangeCount);
  164. };
  165. /**
  166. * Returns whether the focus position occurs before the anchor position.
  167. * @param {Node} anchorNode The node to anchor on.
  168. * @param {number} anchorOffset The offset within the node to anchor on.
  169. * @param {Node} focusNode The node to focus on.
  170. * @param {number} focusOffset The offset within the node to focus on.
  171. * @return {boolean} Whether the focus position occurs before the anchor
  172. * position.
  173. */
  174. goog.dom.Range.isReversed = function(
  175. anchorNode, anchorOffset, focusNode, focusOffset) {
  176. if (anchorNode == focusNode) {
  177. return focusOffset < anchorOffset;
  178. }
  179. var child;
  180. if (anchorNode.nodeType == goog.dom.NodeType.ELEMENT && anchorOffset) {
  181. child = anchorNode.childNodes[anchorOffset];
  182. if (child) {
  183. anchorNode = child;
  184. anchorOffset = 0;
  185. } else if (goog.dom.contains(anchorNode, focusNode)) {
  186. // If focus node is contained in anchorNode, it must be before the
  187. // end of the node. Hence we are reversed.
  188. return true;
  189. }
  190. }
  191. if (focusNode.nodeType == goog.dom.NodeType.ELEMENT && focusOffset) {
  192. child = focusNode.childNodes[focusOffset];
  193. if (child) {
  194. focusNode = child;
  195. focusOffset = 0;
  196. } else if (goog.dom.contains(focusNode, anchorNode)) {
  197. // If anchor node is contained in focusNode, it must be before the
  198. // end of the node. Hence we are not reversed.
  199. return false;
  200. }
  201. }
  202. return (goog.dom.compareNodeOrder(anchorNode, focusNode) ||
  203. anchorOffset - focusOffset) > 0;
  204. };