operarange.js 2.9 KB

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