Range: comparePoint() method
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since March 2017.
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.
Syntax
comparePoint(referenceNode, offset)
Parameters
referenceNode
-
The
Node
that theoffset
is relative to. offset
-
An integer greater than or equal to zero describing the position inside
referenceNode
of the point to be checked. IfreferenceNode
is aNode
of typeText
,Comment
, orCDATASection
, thenoffset
is the number of characters from the start ofreferenceNode
. For otherNode
types,offset
is the number of child nodes from the start of thereferenceNode
.
Return value
A number.
-1
if the point specified by thereferenceNode
andoffset
is before the start of thisRange
.0
if the point specified by thereferenceNode
andoffset
is within thisRange
(including the start and end points of the range).1
if the point specified by thereferenceNode
andoffset
is after the end of thisRange
.
Examples
const text = new Text("0123456789");
const thisRange = new Range();
thisRange.setStart(text, 1);
thisRange.setEnd(text, 6);
thisRange.comparePoint(text, 3); // 0
thisRange.comparePoint(text, 0); // -1
thisRange.comparePoint(text, 6); // 0
thisRange.comparePoint(text, 7); // 1
Specifications
Specification |
---|
DOM # ref-for-dom-range-comparepoint① |