Range (engine/view)
@ckeditor/ckeditor5-engine/src/view/range
Range in the view tree. A range is represented by its start and end positions.
In order to create a new position instance use the createPosition*()
factory methods available in:
Filtering
Properties
-
End position.
-
isCollapsed : Boolean
module:engine/view/range~Range#isCollapsed
Returns whether the range is collapsed, that is it start and end positions are equal.
-
isFlat : Boolean
module:engine/view/range~Range#isFlat
-
root : Element | DocumentFragment
module:engine/view/range~Range#root
Range root element.
-
Start position.
Methods
-
constructor( start, [ end ] )
module:engine/view/range~Range#constructor
Creates a range spanning from
start
position toend
position. -
Symbol.iterator() → Iterable.<TreeWalkerValue>
module:engine/view/range~Range#Symbol.iterator
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>
-
Clones this range.
Returns
-
containsPosition( position ) → Boolean
module:engine/view/range~Range#containsPosition
Checks whether this range contains given position.
-
containsRange( otherRange, [ loose ] ) → Boolean
module:engine/view/range~Range#containsRange
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
module:engine/view/range~Range#getCommonAncestor
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
-
getContainedElement() → Element | null
module:engine/view/range~Range#getContainedElement
-
getDifference( otherRange ) → Array.<Range>
module:engine/view/range~Range#getDifference
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 = downcastWriter.createText( 'foo' ); let img = downcastWriter.createContainerElement( 'img' ); let bar = downcastWriter.createText( 'bar' ); let p = downcastWriter.createContainerElement( 'p', null, [ foo, img, bar ] ); let range = view.createRange( view.createPositionAt( foo, 2 ), view.createPositionAt( bar, 1 ); // "o", img, "b" are in range. let otherRange = view.createRange( // "oo", img, "ba" are in range. view.createPositionAt( foo, 1 ), view.createPositionAt( bar, 2 ) ); let transformed = range.getDifference( otherRange ); // transformed array has no ranges because `otherRange` contains `range` otherRange = view.createRange( view.createPositionAt( foo, 1 ), view.createPositionAt( p, 2 ); // "oo", img are in range. transformed = range.getDifference( otherRange ); // transformed array has one range: from ( p, 2 ) to ( bar, 1 ) otherRange = view.createRange( view.createPositionAt( p, 1 ), view.createPositionAt( 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
module:engine/view/range~Range#getEnlarged
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
module:engine/view/range~Range#getIntersection
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 = downcastWriter.createText( 'foo' ); let img = downcastWriter.createContainerElement( 'img' ); let bar = downcastWriter.createText( 'bar' ); let p = downcastWriter.createContainerElement( 'p', null, [ foo, img, bar ] ); let range = view.createRange( view.createPositionAt( foo, 2 ), view.createPositionAt( bar, 1 ); // "o", img, "b" are in range. let otherRange = view.createRange( view.createPositionAt( foo, 1 ), view.createPositionAt( p, 2 ); // "oo", img are in range. let transformed = range.getIntersection( otherRange ); // range from ( foo, 1 ) to ( p, 2 ). otherRange = view.createRange( view.createPositionAt( bar, 1 ), view.createPositionAt( 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>
module:engine/view/range~Range#getPositions
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
module:engine/view/range~Range#getTrimmed
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] } ) → TreeWalker
module:engine/view/range~Range#getWalker
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
Returns
-
is( type ) → Boolean
module:engine/view/range~Range#is
Checks whether this object is of the given type.
range.is( 'range' ); // -> true range.is( 'view:range' ); // -> true range.is( 'model:range' ); // -> false range.is( 'element' ); // -> false range.is( 'selection' ); // -> false
Check the entire list of view objects which implement the
is()
method.Parameters
type : String
Returns
Boolean
-
isEqual( otherRange ) → Boolean
module:engine/view/range~Range#isEqual
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
module:engine/view/range~Range#isIntersecting
Checks and returns whether this range intersects with the given range.
Static methods
-
_createFromParentsAndOffsets( startElement, startOffset, endElement, endOffset ) → Range
module:engine/view/range~Range._createFromParentsAndOffsets
protected static
Creates a range from the 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
module:engine/view/range~Range._createFromPositionAndShift
protected static
Creates a new range, spreading from specified position to a position moved by given
shift
. Ifshift
is a negative value, shifted position is treated as the beginning of the range. -
Creates a range inside an element which starts before the first child of that element and ends after the last child of that element.
-
Creates a range that starts before given view item and ends after it.
Every day, we work hard to keep our documentation complete. Have you spotted an outdated information? Is something missing? Please report it via our issue tracker.