Debian Codegraph @

closure-library/closure/goog/dom/browserrange/geckorange.js

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