Range

open class Range : AbstractRange(source)

The Range interface represents a fragment of a document that can contain nodes and parts of text nodes.

MDN Reference

Constructors

Link copied to clipboard
constructor()

Types

Link copied to clipboard
object Companion
Link copied to clipboard
sealed interface CompareMethod

Properties

Link copied to clipboard

The read-only collapsed property of the AbstractRange interface returns true if the range's start position and end position are the same.

Link copied to clipboard

The Range.commonAncestorContainer read-only property returns the deepest — or furthest down the document tree — Node that contains both boundary points of the Range. This means that if Range.startContainer and Range.endContainer both refer to the same node, this node is the common ancestor container.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

The read-only endContainer property of the AbstractRange interface returns the Node in which the end of the range is located.

Link copied to clipboard

The endOffset property of the AbstractRange interface returns the offset into the end node of the range's end position.

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

The read-only startContainer property of the AbstractRange interface returns the start Node for the range.

Link copied to clipboard

The read-only startOffset property of the AbstractRange interface returns the offset into the start node of the range's start position.

Functions

Link copied to clipboard

The cloneContents() method of the Range interface copies the selected Node children of the range's commonAncestorContainer and puts them in a new DocumentFragment object.

Link copied to clipboard

The Range.cloneRange() method returns a Range object with boundary points identical to the cloned Range.

Link copied to clipboard
fun collapse(toStart: Boolean = definedExternally)

The collapse() method of the Range interface collapses the Range to one of its boundary points.

Link copied to clipboard

The compareBoundaryPoints() method of the Range interface compares the boundary points of the Range with those of another range.

Link copied to clipboard
fun comparePoint(node: Node, offset: Int): Short

The comparePoint() method of the Range interface determines whether a specified point is before, within, or after the Range. The point is specified by a reference node and an offset within that node.

Link copied to clipboard

The Range.createContextualFragment() method returns a DocumentFragment by invoking the HTML fragment parsing algorithm or the XML fragment parsing algorithm with the start of the range (the parent of the selected node) as the context node. The HTML fragment parsing algorithm is used if the range belongs to a Document whose HTMLness bit is set. In the HTML case, if the context node would be html, for historical reasons the fragment parsing algorithm is invoked with body as the context instead.

Link copied to clipboard

The Range.deleteContents() method removes all completely-selected nodes within this range from the document. For the partially selected nodes at the start or end of the range, only the selected portion of the text is deleted, while the node itself remains intact. Afterwards, the range is collapsed to the end of the last selected node.

Link copied to clipboard
fun detach()

The Range.detach() method does nothing. It used to disable the Range object and enable the browser to release associated resources. The method has been kept for compatibility.

Link copied to clipboard

The extractContents() method of the Range interface is similar to a combination of Range.cloneContents() and Range.deleteContents(). It removes the child Nodes of the range from the document, clones them, and returns them as a new DocumentFragment object. For partially selected nodes, only the selected text is deleted, but all containing parent nodes up to the common ancestor are cloned as well, resulting in two copies of these nodes, one in the original document and one in the extracted fragment.

Link copied to clipboard

The Range.getBoundingClientRect() method returns a DOMRect object that bounds the contents of the range; this is a rectangle enclosing the union of the bounding rectangles for all the elements in the range.

Link copied to clipboard

The Range.getClientRects() method returns a list of DOMRect objects representing the area of the screen occupied by the range. This is created by aggregating the results of calls to Element.getClientRects() for all the elements in the range.

Link copied to clipboard
fun insertNode(node: Node)

The Range.insertNode() method inserts a node at the start of the Range.

Link copied to clipboard

The Range.intersectsNode() method returns a boolean indicating whether the given Node intersects the Range.

Link copied to clipboard
fun isPointInRange(node: Node, offset: Int): Boolean

The isPointInRange() method of the Range interface determines whether a specified point is within the Range. The point is specified by a reference node and an offset within that node. It is equivalent to calling Range.comparePoint() and checking if the result is 0.

Link copied to clipboard
fun selectNode(node: Node)

The Range.selectNode() method 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 referenceNode.

Link copied to clipboard

The Range.selectNodeContents() method sets the Range to contain the contents of a Node.

Link copied to clipboard
fun setEnd(node: Node, offset: Int)

The Range.setEnd() method sets the end position of a Range to be located at the given offset into the specified node. Setting the end point above (higher in the document) than the start point will result in a collapsed range with the start and end points both set to the specified end position.

Link copied to clipboard
fun setEndAfter(node: Node)

The Range.setEndAfter() method sets the end position of a Range relative to another Node. The parent Node of end of the Range will be the same as that for the referenceNode.

Link copied to clipboard
fun setEndBefore(node: Node)

The Range.setEndBefore() method sets the end position of a Range relative to another Node. The parent Node of end of the Range will be the same as that for the referenceNode.

Link copied to clipboard
fun setStart(node: Node, offset: Int)

The Range.setStart() method sets the start position of a Range.

Link copied to clipboard
fun setStartAfter(node: Node)

The Range.setStartAfter() method sets the start position of a Range relative to a Node. The parent Node of the start of the Range will be the same as that for the referenceNode.

Link copied to clipboard
fun setStartBefore(node: Node)

The Range.setStartBefore() method sets the start position of a Range relative to another Node. The parent Node of the start of the Range will be the same as that for the referenceNode.

Link copied to clipboard
fun surroundContents(newParent: Node)

The surroundContents() method of the Range interface surrounds the selected content by a provided node. It extracts the contents of the range, replaces the children of newParent with the extracted contents, inserts newParent at the location of the extracted contents, and makes the range select newParent.