API Docs for: 3.11.0-git
Show:

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 optional

    Native Range object to wrap. If not provided, a new Range will be created.

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:

Range:

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:

Node instance containing a document fragment with a copy of this range's contents.

collapse

(
  • [options]
)
chainable

Collapses this range by setting the start and end points to the same position, thus resulting in an empty range.

Parameters:

  • [options] Object optional

    Options.

    • [toStart=false] Boolean optional

      If true, this range will be collapsed by moving the end point to the start point. Otherwise, the start point will be moved to the end point.

compare

(
  • otherRange
  • [options]
)
Number

Compares the start or end boundary of this range with the start or end boundary of another range.

Parameters:

  • otherRange Range

    Range to compare to.

  • [options] Object optional

    Options.

    • [myPoint='start'] String optional

      Specifies 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 optional

      Specifies 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:

Number:

-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]
)
Node

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 optional

    Node 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 optional

    Offset 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:

Node that contains the end point of this range.

endOffset

(
  • [offset]
)
Number

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 optional

    Offset 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:

Number:

Offset of this selection's end position inside the end node.

expand

(
  • [options]
)
chainable

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 optional

      If 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]
)
chainable

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 optional

      If 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]
)
chainable

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 optional

    If 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:

Node instance containing a document fragment with this range's contents.

insertNode

(
  • node
)
Node

Inserts a node at the start of this range.

Parameters:

Returns:

Node:

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:

Boolean:

true if this range is collapsed, false otherwise.

isEquivalent

(
  • otherRange
)
Boolean

Returns true if this range has the same boundaries as otherRange, false otherwise.

Parameters:

  • otherRange Range

    Range to compare this range to.

Returns:

Boolean:

true if this range has the same boundaries as otherRange, false otherwise.

isInsideNode

(
  • node
)
Boolean

Returns true if this range is entirely contained within the given node.

Parameters:

Returns:

Boolean:

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:

Node:

Nearest common ancestor node that fully contains all nodes within this range.

selectNode

(
  • node
)
chainable

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:

selectNodeContents

(
  • node
)
chainable

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:

shrink

(
  • [options]
)
chainable

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

Parameters:

  • [options] Object optional
    • [trim=false] Boolean optional

      If true whitespace will be ignored when shrinking the start and end containers. Offsets will be set to exclude any leading whitespace from the startContainer and trailing whitespace from the endContainer.

shrinkEnd

(
  • [options]
)
chainable

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.

Parameters:

  • [options] Object optional
    • [trim=false] Boolean optional

      If true whitespace will be ignored when shrinking the endContainer and an offset will be set to exclude any trailing whitespace from the shrunken endContainer.

shrinkStart

(
  • [options]
)
chainable

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.

Parameters:

  • [options] Object optional
    • [trim=false] Boolean optional

      If true whitespace will be ignored when shrinking the startContainer and an offset will be set to exclude any leading whitespace from the shrunken startContainer.

startNode

(
  • [node]
  • [offset=0]
)
Node

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 optional

    Node 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 optional

    Offset 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:

Node that contains the start point of this range.

startOffset

(
  • [offset]
)
Number

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 optional

    Offset 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:

Number:

Offset of this range's start position inside the start node.

toHTML

() HTML

Returns the HTML content of this range.

Returns:

HTML:

HTML content of this range.

toString

() String

Returns the plain text content of this range.

Returns:

String:

Plain text content of this range.

traverse

(
  • callback
  • [thisObj]
)
chainable

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

Parameters:

  • callback Function

    Callback function.

    • node Node

      Node instance.

  • [thisObj] Object optional

    this object to use when calling the callback function.

wrap

(
  • html
  • [options]
)
Node

Wraps this range in the specified HTML and returns the new wrapper node.

Parameters:

  • html HTML

    HTML string.

  • [options] Object optional

    Options.

    • [includeWrapper=false] Boolean optional

      If true, this range will be updated to include the new wrapper node.

Returns:

Node:

New wrapper node.