Constructor
new BoundaryRange(…args)
Create a new range; takes up to two arguments:
Name | Type | Attributes | Description |
---|---|---|---|
args | Range | | <repeatable> | One of these formats:
For more control over initialization, leave args empty and use setStart and setEnd instead. You may also directly manipulate or assign start or end if desired. |
- Source
Members
collapsed :boolean
Check if the range is collapsed in the current DOM. The start/end boundaries must be equal, or start/end must be adjacent to eachother (see Boundary#isEqual and Boundary#isAdjacent). If the start/end anchors are disconnected or out-of-order, it returns false.
- boolean
- Source
end :Boundary
Ending anchor of the range. You can access or assign this directly as needed
- Source
start :Boundary
Starting anchor of the range. You can access or assign this directly as needed
- Source
Methods
cloneRange() → {BoundaryRange}
Make a copy of this range object
- Source
cloned range
- Type:
- BoundaryRange
collapse(toStartopt) → {BoundaryRange}
Collapse the range to one of the boundary points. After calling this method, the start anchor will equal the end: this.start.isEqual(this.end)
(see Boundary#isEqual). If you would like to instead collapse with the start/end anchors adjacent (see Boundary#isAdjacent), then follow with a call to normalize.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
toStart | boolean | <optional> | false |
- Source
modified this
- Type:
- BoundaryRange
contains(other, inclusiveopt) → {boolean}
Check if this range fully contains other
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
other | BoundaryRange | the range to compare with | ||
inclusive | boolean | <optional> | true | whether to consider the range fully contained if one of its start/end anchors equals that of |
- Source
true if other
is contained
- Type:
- boolean
extend(other) → {BoundaryRange}
Extend this range to include the bounds of another BoundaryRange. If the start/end has not been set yet, it will simply copy from other
. Example:
<div id='a'></div> <div id='b'></div>
const ra = (new BoundaryRange()).selectNode(a);
const rb = (new BoundaryRange()).selectNodeContents(b)
ra.extend(rb);
// ra.start == (a, BEFORE_OPEN)
// ra.end == (b, BEFORE_CLOSE)
Name | Type | Description |
---|---|---|
other | BoundaryRange | extend bounds to enclose this range |
- Source
modified this
- Type:
- BoundaryRange
intersects(other, inclusiveopt) → {boolean}
Check if this range intersects with another
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
other | BoundaryRange | the range to compare with | ||
inclusive | boolean | <optional> | false | whether to consider the ranges intersecting if just one of their start/end anchors are equal |
- Source
true if the ranges intersect
- Type:
- boolean
isEqual(other)
Check if range exactly matches another
Name | Type | Description |
---|---|---|
other | BoundaryRange | range to compare with |
- Source
isNull() → {boolean}
Check if the range has been fully set, e.g. neither boundary is null
- Source
true if range is not set, or is only partially set
- Type:
- boolean
normalize(exclusiveopt) → {BoundaryRange}
Every boundary has one adjacent boundary at the same position. On one side you have the AFTER_OPEN/AFTER_CLOSE bounds, and following it will be a BEFORE_OPEN/BEFORE_CLOSE bounds. See Boundary#isAdjacent. The start/end anchors can use either boundary and the range positions will be equivalent; the main difference is the behavior when the DOM is mutated, as the reference nodes will be different. There are two normalization modes:
- exclusive: start/end anchor boundaries are outside the range; e.g. start boundary is AFTER and end boundary is BEFORE type
- inclusive: start/end anchor boundaries are inside the range; e.g. start boundary is BEFORE and end boundary is AFTER type
For example, if you are encoding a range of mutations, you might want to normalize the range to be exclusive; that way, the mutated nodes inside the range will not affect the boundaries.
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
exclusive | boolean | <optional> | true | true for exclusive bounds, or false for inclusive |
- Source
modified this
- Type:
- BoundaryRange
selectNode(node, exclusiveopt) → {BoundaryRange}
Set range to surround a single node
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
node | Node | the node to surround | ||
exclusive | boolean | <optional> | true | see normalize |
- Source
modified this
- Type:
- BoundaryRange
selectNodeContents(node, exclusiveopt) → {BoundaryRange}
Set range to surround the contents of a node. Warning, for CharacterData nodes, you probably want to use selectNode instead, since these nodes cannot have children
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
node | Node | node whose contents to enclose | ||
exclusive | boolean | <optional> | true | see normalize |
- Source
modified this
- Type:
- BoundaryRange
setEnd(…args) → {BoundaryRange}
Update end anchor; equivalent to this.end.set()
Name | Type | Attributes | Description |
---|---|---|---|
args | <repeatable> | forwarded to Boundary#set |
- Source
- See
- Boundary#set for arguments
modified this
- Type:
- BoundaryRange
setStart(…args) → {BoundaryRange}
Update start anchor; equivalent to this.start.set()
Name | Type | Attributes | Description |
---|---|---|---|
args | <repeatable> | forwarded to Boundary#set |
- Source
- See
- Boundary#set for arguments
modified this
- Type:
- BoundaryRange
toRange() → {Range}
Convert to Range
interface. Range's end is set last, so if the resulting range's anchors would be out of order, it would get collapsed to the end anchor. Boundaries inside a CharacterData node are treated as outside for conversion purposes. If the current BoundaryRange isNull, an uninitialized Range will be returned.
- Source
- Type:
- Range
toStaticRange() → {StaticRange}
Convert to StaticRange
interface. Boundaries inside a CharacterData node are treated as outside for conversion purposes. If the current BoundaryRange isNull, an error will be thrown since a StaticRange
cannot be created uninitialized.
- Source
- Type:
- StaticRange