Range Class
Provides a friendly cross-browser Range API similar to the API defined in the DOM Range specification.
Constructor
Range
-
[range]
Parameters:
-
[range]
window.Range optionalNative Range object to wrap. If not provided, a new Range will be created.
Item Index
Methods
clone
()
Range
Returns a new Range object with the same boundary points as this range.
The returned Range is a copy, not a reference, so modifying it will not affect this range (and vice versa).
Returns:
New Range object with the same boundary points as this range.
cloneContents
()
Node
Returns a Node instance containing a document fragment with a copy of this range's contents.
Event listeners are not copied.
Element ids are copied. This could lead to duplicate ids, so be careful.
Partially selected nodes will include parent tags to ensure that the fragment is valid.
Returns:
Node instance containing a document fragment with a copy of this range's contents.
collapse
-
[options]
Collapses this range by setting the start and end points to the same position, thus resulting in an empty range.
compare
-
otherRange
-
[options]
Compares the start or end boundary of this range with the start or end boundary of another range.
Parameters:
-
otherRange
RangeRange to compare to.
-
[options]
Object optionalOptions.
-
[myPoint='start']
String optionalSpecifies which boundary point on this range should be used for the comparison. Valid values are 'start' to use this range's start point for the comparison, or 'end' to use this range's end point.
-
[otherPoint='start']
String optionalSpecifies which boundary point on otherRange should be used for the comparison. Valid values are 'start' to use otherRange's start point for the comparison, or 'end' to use otherRange's end point.
-
Returns:
-1, 0, or 1, indicating whether this range's boundary is respectively before, equal to, or after the other range's boundary.
deleteContents
()
chainable
Removes the contents of this range from the DOM.
endNode
-
[node]
-
[offset=0]
Gets or sets the node that contains the end point of this range.
When specifying an offset, you may specify either a number or the string "before" or "after".
A numerical offset will position the end point at that offset inside the node. If node is a text node, the offset will represent a character position. If node can contain child nodes, then the offset will represent a child index.
The offset "before" will cause the end point to be placed immediately before node (not inside it).
The offset "after" will cause the end point to be placed immediately after node (not inside it).
Parameters:
-
[node]
HTMLElement | Node | String optionalNode to set the end point to. May be specified as a Node instance, HTMLElement, or selector string. If not specified, the current end point will be returned.
-
[offset=0]
Number | String optionalOffset position of the new end point relative to the node. If this is a number, it will be used as an offset position inside node. To specify a position immediately before node, use the string "before". To specify a position immediately after node, use the string "after".
Returns:
Node that contains the end point of this range.
endOffset
-
[offset]
Gets or sets the offset of this selection's end position inside the end node.
If the end node is a text node, the offset represents a character position. If the end node can contain child nodes, then the offset represents a child index.
When setting an offset, you may use a numerical offset (which behaves as described above) or the string "before" or "after".
The offset "before" will cause the end point to be placed immediately before the current end node (not inside it).
The offset "after" will cause the end point to be placed immediately after the current end node (not inside it).
Note that setting a "before" or "after" offset will change the end node. To
get the new end node, call endNode()
.
Parameters:
-
[offset]
Number | String optionalOffset position of the new end point relative to the current end node. If this is a number, it will be used as an offset position inside the node. To specify a position immediately before the node, use the string "before". To specify a position immediately after the node, use the string "after".
Returns:
Offset of this selection's end position inside the end node.
expand
-
[options]
Attempts to expand the range to include element nodes while still being equivalent to the current range. Start and End nodes are not guaranteed to be element nodes after this method is executed.
Examples:
[] = startContainer, {} = endContainer, s:e = start/end offset
given the HTML
<div><span>Lorem</span></div>
an initial range of
<div><span>[{Lorem]}</span></div> #text s0 : #text e5
after expansion with stopAt = span
<div><span>[{Lorem]}</span></div> SPAN s0 : SPAN e1
after expansion with stopAt = div
<div>[{<span>Lorem</span>}]</div> DIV s0 : DIV e1
Parameters:
-
[options]
Object optional-
[stopAt]
String | Function | HTMLElement | Node optionalIf provided, does not expand the range past this node. Strings will be used as selectors to match ancestor nodes of the start and end nodes of the range (inclusive). Functions will test ancestors of the start and end nodes of the range (inclusive) and will receive the node being tested as the only argument.
-
expandEnd
-
[options]
Attempts to expand the range end to include element nodes while still being equivalent to the current range. End node is not guaranteed to be an element node after this method is executed.
Parameters:
-
[options]
Object optional-
[stopAt]
String | Function | HTMLElement | Node optionalIf provided, does not expand the range past this node. Strings will be used as selectors to match ancestor nodes of the end node of the range (inclusive). Functions will test ancestors of the end node of the range (inclusive) and will receive the node being tested as the only argument.
-
expandStart
-
[options.stopAt]
Attempts to expand the range start to include element nodes while still being equivalent to the current range. Start node is not guaranteed to be an element node after this method is executed.
Parameters:
-
[options.stopAt]
String | Function | HTMLElement | Node optionalIf provided, does not expand the range past this node. Strings will be used as selectors to match ancestor nodes of the start node of the range (inclusive). Functions will test ancestors of the start node of the range (inclusive) and will receive the node being tested as the only argument.
extractContents
()
Node
Moves this range's contents into a document fragment and returns a Node instance containing that fragment.
Event listeners are not retained.
If this range splits a non-text element, the resulting fragment will include a clone of that element, including its id (if it has one). This could lead to duplicate ids, so be careful.
Returns:
Node instance containing a document fragment with this range's contents.
insertNode
-
node
Inserts a node at the start of this range.
Parameters:
-
node
HTMLElement | Node | StringNode instance, HTMLElement, or selector string of a node to insert.
Returns:
Inserted node.
isCollapsed
()
Boolean
Returns true
if this range is collapsed, false
otherwise.
A true
value means that the start and end points are the same and the
range is empty, whereas a false
value means that the start and end points
are different and the range is not empty.
Returns:
true
if this range is collapsed, false
otherwise.
isEquivalent
-
otherRange
Returns true
if this range has the same boundaries as otherRange,
false
otherwise.
Parameters:
-
otherRange
RangeRange to compare this range to.
Returns:
true
if this range has the same boundaries as
otherRange, false
otherwise.
isInsideNode
-
node
Returns true
if this range is entirely contained within the given node.
Parameters:
-
node
HTMLElement | Node | StringNode instance, HTML element, or selector string of the container.
Returns:
true
if this range is entirely contained within the
given node, false
otherwise.
parentNode
()
Node
Returns the nearest common ancestor node that fully contains all nodes within this range.
Returns:
Nearest common ancestor node that fully contains all nodes within this range.
selectNode
-
node
Sets the Range to contain the Node and its contents.
The parent Node of the start and end of the Range will be the same as the parent of the given node.
Parameters:
-
node
HTMLElement | NodeNode or HTMLElement to select
selectNodeContents
-
node
Sets the Range to contain the contents of a Node.
The parent Node of the start and end of the Range will be the given node. The startOffset is 0, and the endOffset is the number of child Nodes or number of characters contained in the reference node.
Parameters:
-
node
HTMLElement | NodeNode or HTMLElement to select
shrink
-
[options]
Shrinks the start and end containers of the range to just the text nodes containing the selected text.
Useful for normalizing a range after double clicking to select since the range start/end containers vary by browser as explained below:
Chrome and IE use the first text node that contains the selected text as the startContainer with an offset to exclude any leading whitespace.
Firefox will use a text node before the selected text as the startContainer, with a positive offset set to the end of the node. If there is no previous sibling of the selected text or the previous sibling is not a text node, the behavior is the same as Chrome/IE.
Chrome uses the last text node that contains the selected text as the endContainer, with an offset to exclude trailing whitespace.
Firefox follows the mostly the same rules for the endContainer as it does for the startContainer. Any sibling text node after the selected text will be used as the endContainer, but with a 0 offset. If there is no next sibling or the next sibling is not a text node, the endContainer will be the same as the startContainer, with an offset to exclude any trailing whitespace.
IE will aways use a following text node as the endContainer, even if it is a child of a sibling. The offset will include all leading whitespace. If there is no following text node, the endContainer will be the same as the startContainer, with an offset to include all trailing whitespace.
Examples: [] = startContainer, {} = endContainer, s:e = start/end offset
given the HTML Lorem Ipsum Dolor Sit
dbl clicking to select the text Lorem
Chrome: [{Lorem }]<b>Ipsum</b> Dolor Sit
s0:e5
Firefox: [{Lorem }]<b>Ipsum</b> Dolor Sit
s0:e5
IE: [Lorem ]<b>{Ipsum}</b> Dolor Sit
s0:e0
dbl clicking to select the text Ipsum
Chrome: Lorem <b>[{Ipsum}]</b> Dolor Sit
s0:e5
Firefox: [Lorem ]<b>Ipsum</b>{ Dolor Sit}
s6:e0
IE: Lorem <b>[Ipsum]</b>{ Dolor Sit}
s0:e1
dbl clicking to select the text Dolor
Chrome: Lorem <b>Ipsum</b>[{ Dolor Sit}]
s1:e6
Firefox: Lorem <b>Ipsum</b>[{ Dolor Sit}]
s1:e6
IE: Lorem <b>Ipsum</b>[{ Dolor Sit}]
s1:e7
dbl clicking to select the text Sit
Chrome: Lorem <b>Ipsum</b>[{ Dolor Sit}]
s7:e10
Firefox: Lorem <b>Ipsum</b>[{ Dolor Sit}]
s7:e10
IE: Lorem <b>Ipsum</b>[{ Dolor Sit}]
s7:e10
shrinkEnd
-
[options]
Shrinks the endContainer of the range to just the text node containing the selected text.
Browsers are inconsistent in how they define a range, sometimes using offsets of sibling or parent nodes instead of the actual text node containing the selected text.
shrinkStart
-
[options]
Shrinks the startContainer of the range to just the text node containing the selected text.
Browsers are inconsistent in how they define a range, sometimes using offsets of sibling or parent nodes instead of the actual text node containing the selected text.
startNode
-
[node]
-
[offset=0]
Gets or sets the node that contains the start point of this range.
When specifying an offset, you may specify either a number or the string "before" or "after".
A numerical offset will position the start point at that offset inside the node. If node is a text node, the offset will represent a character position. If node can contain child nodes, then the offset will represent a child index.
The offset "before" will cause the start point to be placed immediately before node (not inside it).
The offset "after" will cause the start point to be placed immediately after node (not inside it).
Parameters:
-
[node]
HTMLElement | Node | String optionalNode to set the start point to. May be specified as a Node instance, HTMLElement, or selector string. If not specified, the current start point will be returned.
-
[offset=0]
Number | String optionalOffset position of the new start point relative to the node. If this is a number, it will be used as an offset position inside node. To specify a position immediately before node, use the string "before". To specify a position immediately after node, use the string "after".
Returns:
Node that contains the start point of this range.
startOffset
-
[offset]
Gets or sets the offset of this range's start position inside the start node.
If the start node is a text node, the offset represents a character position. If the start node can contain child nodes, then the offset represents a child index.
When setting an offset, you may use a numerical offset (which behaves as described above) or the string "before" or "after".
The offset "before" will cause the start point to be placed immediately before the current start node (not inside it).
The offset "after" will cause the start point to be placed immediately after the current start node (not inside it).
Note that setting a "before" or "after" offset will change the start node.
To get the new start node, call startNode()
.
Parameters:
-
[offset]
Number | String optionalOffset position of the new start point relative to the current start node. If this is a number, it will be used as an offset position inside the node. To specify a position immediately before the node, use the string "before". To specify a position immediately after the node, use the string "after".
Returns:
Offset of this range's start position inside the start node.
toHTML
()
HTML
Returns the HTML content of this range.
Returns:
HTML content of this range.
toString
()
String
Returns the plain text content of this range.
Returns:
Plain text content of this range.
traverse
-
callback
-
[thisObj]
Traverses the contents of the range, passing each node and its children to the supplied callback in document order.
For example, if the range includes the following HTML...
lorem ipsum <b>dolor <i>sit</i></b> amet
...then this traverse()
call...
range.traverse(function (node) {
console.log(Y.Node.getDOMNode(node));
});
...would result in the following console output:
"lorem ipsum "
<b> element
"dolor "
<i> element
"sit"
" amet"
The entire start and end node will be included even if the range only
includes a portion of them. Use the startOffset()
and endOffset()
methods to determine where the precise boundaries are if necessary.
Returning true
from the callback function will stop traversal