The goog.dom.AbstractRange Class

goog.dom.AbstractRange()

Creates a new selection with no properties. Do not use this constructor - use one of the goog.dom.Range.from* methods instead.

.__iterator__(opt_keys)

Returns a RangeIterator over the contents of the range. Regardless of the direction of the range, the iterator will move in document order.

opt_keys {boolean=}
Unused for this iterator.
returns {goog.dom.RangeIterator}
An iterator over tags in the range.

.clone()

returns {goog.dom.AbstractRange}
A clone of this range.

.collapse(toAnchor)

Collapses the range to one of its boundary points.

toAnchor {boolean}
Whether to collapse to the anchor of the range.

.containsNode(node, opt_allowPartial)

Tests if this range contains the given node.

node {Node}
The node to test for.
opt_allowPartial {boolean=}
If not set or false, the node must be entirely contained in the selection for this function to return true.
returns {boolean}
Whether this range contains the given node.

.containsRange(range, opt_allowPartial)

Tests if this range contains the given range.

range {goog.dom.AbstractRange}
The range to test.
opt_allowPartial {boolean=}
If true, the range can be partially contained in the selection, otherwise the range must be entirely contained.
returns {boolean}
Whether this range contains the given range.

.getAnchorNode()

returns {Node}
The element or text node the range is anchored at.

.getAnchorOffset()

returns {number}
The offset into the node the range is anchored at. For text nodes, this is an offset into the node value. For elements, this is an offset into the childNodes array.

.getBrowserRangeObject()

returns {Range|TextRange}
The native browser range object.

.getBrowserSelectionForWindow(win)

Gets the browser native selection object from the given window.

win {Window}
The window to get the selection object from.
returns {Object}
The browser native selection object, or null if it could not be retrieved.

.getContainer()

returns {Node}
The deepest node that contains the entire range.

.getContainerElement()

Returns the deepest element in the tree that contains the entire range.

returns {Element}
The deepest element that contains the entire range.

.getDocument()

returns {Document}
The document this selection is a part of.

.getEndNode()

returns {Node}
The element or text node the range ends in.

.getEndOffset()

returns {number}
The offset into the node the range ends in. For text nodes, this is an offset into the node value. For elements, this is an offset into the childNodes array.

.getFocusNode()

returns {Node}
The element or text node the range is focused at - i.e. where the cursor is.

.getFocusOffset()

returns {number}
The offset into the node the range is focused at - i.e. where the cursor is. For text nodes, this is an offset into the node value. For elements, this is an offset into the childNodes array.

.getHtmlFragment()

Returns the HTML fragment this range selects. This is slow on all browsers. The HTML fragment may not be valid HTML, for instance if the user selects from a to b inclusively in the following html: >div<a>/div<b This method will return a</div>b If you need valid HTML, use {@link #getValidHtml} instead.

returns {string}
HTML fragment of the range, does not include context containing elements.

.getPastableHtml()

Returns pastable HTML for this range. This guarantees that any child items that must have specific ancestors will have them, for instance all TDs will be contained in a TR in a TBODY in a TABLE and all LIs will be contained in a UL or OL as appropriate. This is semi-fast on all browsers.

returns {string}
Pastable HTML of the range, including context containing elements.

.getStartNode()

returns {Node}
The element or text node the range starts in. For text ranges, the range comprises all text between the start and end position. For other types of range, start and end give bounds of the range but do not imply all nodes in those bounds are selected.

.getStartOffset()

returns {number}
The offset into the node the range starts in. For text nodes, this is an offset into the node value. For elements, this is an offset into the childNodes array.

.getText()

returns {string}
The text content of the range.

.getTextRange(i)

Get the i-th text range in this range. The behavior is undefined if i >= getTextRangeCount or i < 0.

i {number}
The range number to retrieve.
returns {goog.dom.TextRange}
The i-th text range.

.getTextRangeCount()

returns {number}
The number of text ranges in this range.

.getTextRanges()

Gets an array of all text ranges this range is comprised of. For non-multi ranges, returns a single element array containing this.

returns {Array.<goog.dom.TextRange>}
Array of text ranges.

.getType()

returns {goog.dom.RangeType}
The type of range represented by this object.

.getValidHtml()

Returns valid HTML for this range. This is fast on IE, and semi-fast on other browsers.

returns {string}
Valid HTML of the range, including context containing elements.

.getWindow()

returns {Window}
The window this selection is a part of.

.insertNode(node, before)

Inserts a node before (or after) the range. The range may be disrupted beyond recovery because of the way this splits nodes.

node {Node}
The node to insert.
before {boolean}
True to insert before, false to insert after.
returns {Node}
The node added to the document. This may be different than the node parameter because on IE we have to clone it.

.isCollapsed()

returns {boolean}
Whether the range is collapsed.

.isNativeControlRange(range)

Tests if the given Object is a controlRange.

range {Object}
The range object to test.
returns {boolean}
Whether the given Object is a controlRange.

.isRangeInDocument()

Tests whether this range is valid (i.e. whether its endpoints are still in the document). A range becomes invalid when, after this object was created, either one or both of its endpoints are removed from the document. Use of an invalid range can lead to runtime errors, particularly in IE.

returns {boolean}
Whether the range is valid.

.isReversed()

returns {boolean}
Whether the selection is reversed.

.removeContents()

Removes the contents of the range from the document.

.replaceContentsWithNode(node)

Replaces the range contents with (possibly a copy of) the given node. The range may be disrupted beyond recovery because of the way this splits nodes.

node {Node}
The node to insert.
returns {Node}
The node added to the document. This may be different than the node parameter because on IE we have to clone it.

.saveUsingCarets()

Saves the range using HTML carets. As long as the carets remained in the HTML, the range can be restored...even when the HTML is copied across documents.

returns {goog.dom.SavedCaretRange?}
A range representation that can be restored as long as carets are not removed. Returns null if carets could not be created.

.saveUsingDom()

Saves the range so that if the start and end nodes are left alone, it can be restored.

returns {goog.dom.SavedRange}
A range representation that can be restored as long as the endpoint nodes of the selection are not modified.

.select()

Sets this range as the selection in its window.

.setBrowserRangeObject(nativeRange)

Sets the native browser range object, overwriting any state this range was storing.

nativeRange {Range|TextRange}
The native browser range object.
returns {boolean}
Whether the given range was accepted. If not, the caller will need to call goog.dom.Range.createFromBrowserRange to create a new range object.

.surroundWithNodes(startNode, endNode)

Surrounds this range with the two given nodes. The range may be disrupted beyond recovery because of the way this splits nodes.

startNode {Element}
The node to insert at the start.
endNode {Element}
The node to insert at the end.