Range (engine/view)
@ckeditor/ckeditor5-engine/src/view/range
Tree view range.
Filtering
Properties
-
End position.
-
isCollapsed : Boolean
Returns whether the range is collapsed, that is it start and end positions are equal.
-
isFlat : Boolean
-
Range root element.
-
Start position.
Methods
-
constructor( start, [ end ] )
-
Symbol.iterator() → Iterable.<TreeWalkerValue>
Iterable interface.
Iterates over all view items that are in this range and returns them together with additional information like length or positions, grouped as
TreeWalkerValue
.This iterator uses TreeWalker with
boundaries
set to this range andignoreElementEnd
option set totrue
.Returns
Iterable.<TreeWalkerValue>
-
containsPosition( position ) → Boolean
-
containsRange( otherRange, [ loose ] ) → Boolean
Checks whether this range contains given range.
Parameters
otherRange : Range
Range to check.
[ loose ] : Boolean
Whether the check is loose or strict. If the check is strict (
false
), compared range cannot start or end at the same position as this range boundaries. If the check is loose (true
), compared range can start, end or even be equal to this range. Note that collapsed ranges are always compared in strict mode.Defaults to
false
Returns
Boolean
true
if given range boundaries are contained by this range,false
otherwise.
-
getCommonAncestor() → Node | DocumentFragment | null
Returns a
Node
orDocumentFragment
which is a common ancestor of range's both ends (in which the entire range is contained).Returns
Node | DocumentFragment | null
-
getDifference( otherRange ) → Array.<Range>
Computes which part(s) of this range is not a part of given range. Returned array contains zero, one or two ranges.
Examples:
let foo = new Text( 'foo' ); let img = new ContainerElement( 'img' ); let bar = new Text( 'bar' ); let p = new ContainerElement( 'p', null, [ foo, img, bar ] ); let range = new Range( new Position( foo, 2 ), new Position( bar, 1 ); // "o", img, "b" are in range. let otherRange = new Range( new Position( foo, 1 ), new Position( bar, 2 ); "oo", img, "ba" are in range. let transformed = range.getDifference( otherRange ); // transformed array has no ranges because `otherRange` contains `range` otherRange = new Range( new Position( foo, 1 ), new Position( p, 2 ); // "oo", img are in range. transformed = range.getDifference( otherRange ); // transformed array has one range: from ( p, 2 ) to ( bar, 1 ) otherRange = new Range( new Position( p, 1 ), new Position( p, 2 ) ); // img is in range. transformed = range.getDifference( otherRange ); // transformed array has two ranges: from ( foo, 1 ) to ( p, 1 ) and from ( p, 2 ) to ( bar, 1 )
Parameters
otherRange : Range
Range to differentiate against.
Returns
Array.<Range>
The difference between ranges.
-
getEnlarged() → Range
Creates a maximal range that has the same content as this range but is expanded in both ways (at the beginning and at the end).
For example:
<p>Foo</p><p><b>{Bar}</b></p> -> <p>Foo</p>[<p><b>Bar</b>]</p> <p><b>foo</b>{bar}<span></span></p> -> <p><b>foo[</b>bar<span></span>]</p>
Note that in the sample above:
<p>
have type ofContainerElement
,<b>
have type ofAttributeElement
,<span>
have type ofUIElement
.
Returns
Range
Enlarged range.
-
getIntersection( otherRange ) → Range | null
Returns an intersection of this range and given range. Intersection is a common part of both of those ranges. If ranges has no common part, returns
null
.Examples:
let foo = new Text( 'foo' ); let img = new ContainerElement( 'img' ); let bar = new Text( 'bar' ); let p = new ContainerElement( 'p', null, [ foo, img, bar ] ); let range = new Range( new Position( foo, 2 ), new Position( bar, 1 ); // "o", img, "b" are in range. let otherRange = new Range( new Position( foo, 1 ), new Position( p, 2 ); // "oo", img are in range. let transformed = range.getIntersection( otherRange ); // range from ( foo, 1 ) to ( p, 2 ). otherRange = new Range( new Position( bar, 1 ), new Position( bar, 3 ); "ar" is in range. transformed = range.getIntersection( otherRange ); // null - no common part.
Parameters
otherRange : Range
Range to check for intersection.
Returns
Range | null
A common part of given ranges or
null
if ranges have no common part.
-
Returns an iterator that iterates over all view items that are in this range and returns them.
This method uses
TreeWalker
withboundaries
set to this range andignoreElementEnd
option set totrue
. However it returns only items, notTreeWalkerValue
.You may specify additional options for the tree walker. See
TreeWalker
for a full list of available options.Parameters
options : Object
Object with configuration options. See
TreeWalker
.
Returns
Iterable.<Item>
-
getPositions( options ) → Iterable.<Position>
Returns an iterator that iterates over all positions that are boundaries or contained in this range.
This method uses
TreeWalker
withboundaries
set to this range. However it returns only positions, notTreeWalkerValue
.You may specify additional options for the tree walker. See
TreeWalker
for a full list of available options.Parameters
options : Object
Object with configuration options. See
TreeWalker
.
Returns
Iterable.<Position>
-
getTrimmed() → Range
Creates a minimum range that has the same content as this range but is trimmed in both ways (at the beginning and at the end).
For example:
<p>Foo</p>[<p><b>Bar</b>]</p> -> <p>Foo</p><p><b>{Bar}</b></p> <p><b>foo[</b>bar<span></span>]</p> -> <p><b>foo</b>{bar}<span></span></p>
Note that in the sample above:
<p>
have type ofContainerElement
,<b>
have type ofAttributeElement
,<span>
have type ofUIElement
.
Returns
Range
Shrink range.
-
getWalker( options = { [options.startPosition], [options.singleCharacters], [options.shallow], [options.ignoreElementEnd] } )
Creates a TreeWalker instance with this range as a boundary.
Parameters
options : Object
Object with configuration options. See
TreeWalker
.Properties[ options.startPosition ] : Position
[ options.singleCharacters ] : Boolean
-
Defaults to
false
[ options.shallow ] : Boolean
-
Defaults to
false
[ options.ignoreElementEnd ] : Boolean
-
Defaults to
false
-
isEqual( otherRange ) → Boolean
Two ranges are equal if their start and end positions are equal.
Parameters
otherRange : Range
Range to compare with.
Returns
Boolean
true
if ranges are equal,false
otherwise
-
isIntersecting( otherRange ) → Boolean
Checks and returns whether this range intersects with given range.
Static methods
-
createCollapsedAt( itemOrPosition, [ offset ] )
static
-
createFromParentsAndOffsets( startElement, startOffset, endElement, endOffset ) → Range
static
Creates a range from given parents and offsets.
Parameters
startElement : Node | DocumentFragment
Start position parent element.
startOffset : Number
Start position offset.
endElement : Node | DocumentFragment
End position parent element.
endOffset : Number
End position offset.
Returns
Range
Created range.
-
createFromPositionAndShift( position, shift ) → Range
static
-
createFromRange( range ) → Range
static