Range (engine/model)
@ckeditor/ckeditor5-engine/src/model/range
Filtering
Properties
-
End position.
-
isCollapsed : Boolean
module:engine/model/range~Range#isCollapsed
-
isFlat : Boolean
module:engine/model/range~Range#isFlat
-
root : Element | DocumentFragment
module:engine/model/range~Range#root
Range root element.
-
Start position.
Methods
-
constructor( start, [ end ] )
module:engine/model/range~Range#constructor
Creates a range spanning from
start
position toend
position. -
Symbol.iterator() → Iterable.<TreeWalkerValue>
module:engine/model/range~Range#Symbol.iterator
Iterable interface.
Iterates over all items that are in this range and returns them together with additional information like length or positions, grouped as
TreeWalkerValue
. It iterates over all text contents that are inside the range and all theElement
s that are entered into when iterating over this range.This iterator uses
TreeWalker
withboundaries
set to this range andignoreElementEnd
option set totrue
.Returns
Iterable.<TreeWalkerValue>
-
Returns a new range that is equal to current range.
Returns
-
containsItem( item )
module:engine/model/range~Range#containsItem
-
containsPosition( position ) → Boolean
module:engine/model/range~Range#containsPosition
Checks whether this range contains given position.
-
containsRange( otherRange, [ loose ] ) → Boolean
module:engine/model/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() → Element | DocumentFragment | null
module:engine/model/range~Range#getCommonAncestor
Returns an
Element
orDocumentFragment
which is a common ancestor of the range's both ends (in which the entire range is contained).Returns
Element | DocumentFragment | null
-
getContainedElement() → Element | null
module:engine/model/range~Range#getContainedElement
-
getDifference( otherRange ) → Array.<Range>
module:engine/model/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 range = model.createRange( model.createPositionFromPath( root, [ 2, 7 ] ), model.createPositionFromPath( root, [ 4, 0, 1 ] ) ); let otherRange = model.createRange( model.createPositionFromPath( root, [ 1 ] ), model.createPositionFromPath( root, [ 5 ] ) ); let transformed = range.getDifference( otherRange ); // transformed array has no ranges because `otherRange` contains `range` otherRange = model.createRange( model.createPositionFromPath( root, [ 1 ] ), model.createPositionFromPath( root, [ 3 ] ) ); transformed = range.getDifference( otherRange ); // transformed array has one range: from [ 3 ] to [ 4, 0, 1 ] otherRange = model.createRange( model.createPositionFromPath( root, [ 3 ] ), model.createPositionFromPath( root, [ 4 ] ) ); transformed = range.getDifference( otherRange ); // transformed array has two ranges: from [ 2, 7 ] to [ 3 ] and from [ 4 ] to [ 4, 0, 1 ]
Parameters
otherRange : Range
Range to differentiate against.
Returns
Array.<Range>
The difference between ranges.
-
getIntersection( otherRange ) → Range | null
module:engine/model/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 range = model.createRange( model.createPositionFromPath( root, [ 2, 7 ] ), model.createPositionFromPath( root, [ 4, 0, 1 ] ) ); let otherRange = model.createRange( model.createPositionFromPath( root, [ 1 ] ), model.createPositionFromPath( root, [ 2 ] ) ); let transformed = range.getIntersection( otherRange ); // null - ranges have no common part otherRange = model.createRange( model.createPositionFromPath( root, [ 3 ] ), model.createPositionFromPath( root, [ 5 ] ) ); transformed = range.getIntersection( otherRange ); // range from [ 3 ] to [ 4, 0, 1 ]
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 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 model 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>
-
Returns a range created by joining this range with the given range. If ranges have no common part, returns
null
.Examples:
let range = model.createRange( model.createPositionFromPath( root, [ 2, 7 ] ), model.createPositionFromPath( root, [ 4, 0, 1 ] ) ); let otherRange = model.createRange( model.createPositionFromPath( root, [ 1 ] ), model.createPositionFromPath( root, [ 2 ] ) ); let transformed = range.getJoined( otherRange ); // null - ranges have no common part otherRange = model.createRange( model.createPositionFromPath( root, [ 3 ] ), model.createPositionFromPath( root, [ 5 ] ) ); transformed = range.getJoined( otherRange ); // range from [ 2, 7 ] to [ 5 ]
Parameters
otherRange : Range
Range to be joined.
[ loose ] : Boolean
Whether the intersection check is loose or strict. If the check is strict (
false
), ranges are tested for intersection or whether start/end positions are equal. If the check is loose (true
), compared range is also checked if it's touching current range.Defaults to
false
Returns
Range | null
A sum of given ranges or
null
if ranges have no common part.
-
getMinimalFlatRanges() → Array.<Range>
module:engine/model/range~Range#getMinimalFlatRanges
Computes and returns the smallest set of flat ranges, that covers this range in whole.
See an example of a model structure (
[
and]
are range boundaries):root root |- element DIV DIV P2 P3 DIV | |- element H H P1 f o o b a r H P4 | | |- "fir[st" fir[st lorem se]cond ipsum | |- element P1 | | |- "lorem" || |- element P2 || | |- "foo" VV |- element P3 | |- "bar" root |- element DIV DIV [P2 P3] DIV | |- element H H [P1] f o o b a r H P4 | | |- "se]cond" fir[st] lorem [se]cond ipsum | |- element P4 | | |- "ipsum"
As it can be seen, letters contained in the range are:
stloremfoobarse
, spread across different parents. We are looking for minimal set of flat ranges that contains the same nodes.Minimal flat ranges for above range
( [ 0, 0, 3 ], [ 3, 0, 2 ] )
will be:( [ 0, 0, 3 ], [ 0, 0, 5 ] ) = "st" ( [ 0, 1 ], [ 0, 2 ] ) = element P1 ("lorem") ( [ 1 ], [ 3 ] ) = element P2, element P3 ("foobar") ( [ 3, 0, 0 ], [ 3, 0, 2 ] ) = "se"
Note: if an element is not wholly contained in this range, it won't be returned in any of the returned flat ranges. See in the example how
H
elements at the beginning and at the end of the range were omitted. Only their parts that were wholly in the range were returned.Note: this method is not returning flat ranges that contain no nodes.
Returns
Array.<Range>
Array of flat ranges covering this range.
-
getPositions( options ) → Iterable.<Position>
module:engine/model/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>
-
getTransformedByOperation( operation ) → Array.<Range>
module:engine/model/range~Range#getTransformedByOperation
Returns a range that is a result of transforming this range by given
operation
.Note: transformation may break one range into multiple ranges (for example, when a part of the range is moved to a different part of document tree). For this reason, an array is returned by this method and it may contain one or more
Range
instances.Parameters
operation : Operation
Operation to transform range by.
Returns
Array.<Range>
Range which is the result of transformation.
-
getTransformedByOperations( operations ) → Array.<Range>
module:engine/model/range~Range#getTransformedByOperations
-
getWalker( options = { [options.startPosition], [options.singleCharacters], [options.shallow], [options.ignoreElementEnd] } ) → TreeWalker
module:engine/model/range~Range#getWalker
Creates a TreeWalker instance with this range as a boundary.
For example, to iterate over all items in the entire document root:
// Create a range spanning over the entire root content: const range = editor.model.createRangeIn( editor.model.document.getRoot() ); // Iterate over all items in this range: for ( const value of range.getWalker() ) { console.log( value.item ); }
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/model/range~Range#is
Checks whether this object is of the given.
range.is( 'range' ); // -> true range.is( 'model:range' ); // -> true range.is( 'view:range' ); // -> false range.is( 'documentSelection' ); // -> false
Check the entire list of model objects which implement the
is()
method.Parameters
type : String
Returns
Boolean
-
isEqual( otherRange ) → Boolean
module:engine/model/range~Range#isEqual
-
isIntersecting( otherRange ) → Boolean
module:engine/model/range~Range#isIntersecting
Checks and returns whether this range intersects with given range.
Parameters
otherRange : Range
Range to compare with.
Returns
Boolean
true
if ranges intersect,false
otherwise.
-
toJSON() → Object
module:engine/model/range~Range#toJSON
Converts
Range
to plain object and returns it.Returns
Object
Node
converted to plain object.
-
_getTransformedByDeletion( deletionPosition, howMany ) → Range | null
module:engine/model/range~Range#_getTransformedByDeletion
protected
Returns a copy of this range that is transformed by deletion of
howMany
nodes fromdeletePosition
.If the deleted range is intersecting with the transformed range, the transformed range will be shrank.
If the deleted range contains transformed range,
null
will be returned.Parameters
deletionPosition : Position
Position from which nodes are removed.
howMany : Number
How many nodes are removed.
Returns
Range | null
Result of the transformation.
-
_getTransformedByInsertOperation( operation ) → Array.<Range>
module:engine/model/range~Range#_getTransformedByInsertOperation
protected
Returns a result of transforming a copy of this range by insert operation.
One or more ranges may be returned as a result of this transformation.
Parameters
operation : InsertOperation
Returns
Array.<Range>
-
_getTransformedByInsertion( insertPosition, howMany, [ spread ] ) → Array.<Range>
module:engine/model/range~Range#_getTransformedByInsertion
protected
Returns an array containing one or two ranges that are a result of transforming this range by inserting
howMany
nodes atinsertPosition
. Two ranges are returned if the insertion was inside this range andspread
is set totrue
.Examples:
let range = model.createRange( model.createPositionFromPath( root, [ 2, 7 ] ), model.createPositionFromPath( root, [ 4, 0, 1 ] ) ); let transformed = range._getTransformedByInsertion( model.createPositionFromPath( root, [ 1 ] ), 2 ); // transformed array has one range from [ 4, 7 ] to [ 6, 0, 1 ] transformed = range._getTransformedByInsertion( model.createPositionFromPath( root, [ 4, 0, 0 ] ), 4 ); // transformed array has one range from [ 2, 7 ] to [ 4, 0, 5 ] transformed = range._getTransformedByInsertion( model.createPositionFromPath( root, [ 3, 2 ] ), 4 ); // transformed array has one range, which is equal to original range transformed = range._getTransformedByInsertion( model.createPositionFromPath( root, [ 3, 2 ] ), 4, true ); // transformed array has two ranges: from [ 2, 7 ] to [ 3, 2 ] and from [ 3, 6 ] to [ 4, 0, 1 ]
Parameters
insertPosition : Position
Position where nodes are inserted.
howMany : Number
How many nodes are inserted.
[ spread ] : Boolean
Flag indicating whether this {~Range range} should be spread if insertion was inside the range. Defaults to
false
.Defaults to
false
Returns
Array.<Range>
Result of the transformation.
-
_getTransformedByMergeOperation( operation ) → Range
module:engine/model/range~Range#_getTransformedByMergeOperation
protected
Returns a result of transforming a copy of this range by merge operation.
Always one range is returned. The transformation is done in a way to not break the range.
Parameters
operation : MergeOperation
Returns
-
_getTransformedByMove( sourcePosition, targetPosition, howMany, [ spread ] ) → Array.<Range>
module:engine/model/range~Range#_getTransformedByMove
protected
Returns an array containing ranges that are a result of transforming this range by moving
howMany
nodes fromsourcePosition
totargetPosition
.Parameters
sourcePosition : Position
Position from which nodes are moved.
targetPosition : Position
Position to where nodes are moved.
howMany : Number
How many nodes are moved.
[ spread ] : Boolean
Whether the range should be spread if the move points inside the range.
Defaults to
false
Returns
Array.<Range>
Result of the transformation.
-
_getTransformedByMoveOperation( operation ) → Array.<Range>
module:engine/model/range~Range#_getTransformedByMoveOperation
protected
Returns a result of transforming a copy of this range by move operation.
One or more ranges may be returned as a result of this transformation.
Parameters
operation : MoveOperation
Returns
Array.<Range>
-
_getTransformedBySplitOperation( operation ) → Range
module:engine/model/range~Range#_getTransformedBySplitOperation
protected
Returns a result of transforming a copy of this range by split operation.
Always one range is returned. The transformation is done in a way to not break the range.
Parameters
operation : SplitOperation
Returns
Static methods
-
_createFromRanges( ranges ) → Range
module:engine/model/range~Range._createFromRanges
static
Combines all ranges from the passed array into a one range. At least one range has to be passed. Passed ranges must not have common parts.
The first range from the array is a reference range. If other ranges start or end on the exactly same position where the reference range, they get combined into one range.
[ ][] [ ][ ][ ][ ][] [ ] // Passed ranges, shown sorted [ ] // The result of the function if the first range was a reference range. [ ] // The result of the function if the third-to-seventh range was a reference range. [ ] // The result of the function if the last range was a reference range.
Parameters
ranges : Array.<Range>
Ranges to combine.
Returns
Range
Combined range.
-
Creates a
Range
instance from given plain object (i.e. parsed JSON string). -
_createFromPositionAndShift( position, shift ) → Range
module:engine/model/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 model 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.