webkitrange.js 3.9 KB

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