The goog.dom.TextRange Class

goog.dom.TextRange
> goog.dom.AbstractRange

goog.dom.TextRange()

Create a new text selection with no properties. Do not use this constructor: use one of the goog.dom.Range.createFrom* methods instead.

.__iterator__(opt_keys)

Returns a TextRangeIterator 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.TextRangeIterator}
An iterator over tags in the range.

.clone()

returns {goog.dom.TextRange}
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)

Inherited from goog.dom.AbstractRange .

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.

.createFromBrowserRange(range, opt_isReversed)

Create a new range wrapper from the given browser range object. Do not use this method directly - please use goog.dom.Range.createFrom* instead.

range {Range|TextRange}
The browser range object.
opt_isReversed {boolean=}
Whether the focus node is before the anchor node.
returns {goog.dom.TextRange}
A range wrapper object.

.createFromNodeContents(node, opt_isReversed)

Create a new range wrapper that selects the given node's text. Do not use this method directly - please use goog.dom.Range.createFrom* instead.

node {Node}
The node to select.
opt_isReversed {boolean=}
Whether the focus node is before the anchor node.
returns {goog.dom.TextRange}
A range wrapper object.

.createFromNodes(anchorNode, anchorOffset, focusNode, focusOffset)

Create a new range wrapper that selects the area between the given nodes, accounting for the given offsets. Do not use this method directly - please use goog.dom.Range.createFrom* instead.

anchorNode {Node}
The node to start with.
anchorOffset {number}
The offset within the node to start.
focusNode {Node}
The node to end with.
focusOffset {number}
The offset within the node to end.
returns {goog.dom.TextRange}
A range wrapper object.

.getAnchorNode()

Inherited from goog.dom.AbstractRange .

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

.getAnchorOffset()

Inherited from goog.dom.AbstractRange .

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.

.getContainer()

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

.getContainerElement()

Inherited from goog.dom.AbstractRange .

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

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

.getDocument()

Inherited from goog.dom.AbstractRange .

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()

Inherited from goog.dom.AbstractRange .

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

.getFocusOffset()

Inherited from goog.dom.AbstractRange .

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()

Inherited from goog.dom.AbstractRange .

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()

Inherited from goog.dom.AbstractRange .

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.

.isAttachedNode(node)

Tests if the given node is in a document.

node {Node}
The node to check.
returns {boolean}
Whether the given node is in the given document.

.isCollapsed()

returns {boolean}
Whether the range is collapsed.

.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.

.moveToNodes(startNode, startOffset, endNode, endOffset, isReversed)

Moves a TextRange to the provided nodes and offsets.

startNode {Node}
The node to start with.
startOffset {number}
The offset within the node to start.
endNode {Node}
The node to end with.
endOffset {number}
The offset within the node to end.
isReversed {boolean}
Whether the range is reversed.

.removeContents()

Removes the contents of the range from the document.

.replaceContentsWithNode(node)

Inherited from goog.dom.AbstractRange .

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()

Inherited from goog.dom.AbstractRange .

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.

.surroundContents(element)

Surrounds the text range with the specified element (on Mozilla) or with a clone of the specified element (on IE). Returns a reference to the surrounding element if the operation was successful; returns null if the operation failed.

element {Element}
The element with which the selection is to be surrounded.
returns {Element}
The surrounding element (same as the argument on Mozilla, but not on IE), or null if unsuccessful.

.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.