geckorange.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Gecko 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.GeckoRange');
  23. goog.require('goog.dom.browserrange.W3cRange');
  24. /**
  25. * The constructor for Gecko specific browser ranges.
  26. * @param {Range} range The range object.
  27. * @constructor
  28. * @extends {goog.dom.browserrange.W3cRange}
  29. * @final
  30. */
  31. goog.dom.browserrange.GeckoRange = function(range) {
  32. goog.dom.browserrange.W3cRange.call(this, range);
  33. };
  34. goog.inherits(goog.dom.browserrange.GeckoRange, goog.dom.browserrange.W3cRange);
  35. /**
  36. * Creates a range object that selects the given node's text.
  37. * @param {Node} node The node to select.
  38. * @return {!goog.dom.browserrange.GeckoRange} A Gecko range wrapper object.
  39. */
  40. goog.dom.browserrange.GeckoRange.createFromNodeContents = function(node) {
  41. return new goog.dom.browserrange.GeckoRange(
  42. goog.dom.browserrange.W3cRange.getBrowserRangeForNode(node));
  43. };
  44. /**
  45. * Creates a range object that selects between the given nodes.
  46. * @param {Node} startNode The node to start with.
  47. * @param {number} startOffset The offset within the node to start.
  48. * @param {Node} endNode The node to end with.
  49. * @param {number} endOffset The offset within the node to end.
  50. * @return {!goog.dom.browserrange.GeckoRange} A wrapper object.
  51. */
  52. goog.dom.browserrange.GeckoRange.createFromNodes = function(
  53. startNode, startOffset, endNode, endOffset) {
  54. return new goog.dom.browserrange.GeckoRange(
  55. goog.dom.browserrange.W3cRange.getBrowserRangeForNodes(
  56. startNode, startOffset, endNode, endOffset));
  57. };
  58. /** @override */
  59. goog.dom.browserrange.GeckoRange.prototype.selectInternal = function(
  60. selection, reversed) {
  61. if (!reversed || this.isCollapsed()) {
  62. // The base implementation for select() is more robust, and works fine for
  63. // collapsed and forward ranges. This works around
  64. // https://bugzilla.mozilla.org/show_bug.cgi?id=773137, and is tested by
  65. // range_test.html's testFocusedElementDisappears.
  66. goog.dom.browserrange.GeckoRange.base(
  67. this, 'selectInternal', selection, reversed);
  68. } else {
  69. // Reversed selection -- start with a caret on the end node, and extend it
  70. // back to the start. Unfortunately, collapse() fails when focus is
  71. // invalid.
  72. selection.collapse(this.getEndNode(), this.getEndOffset());
  73. selection.extend(this.getStartNode(), this.getStartOffset());
  74. }
  75. };