Range (engine/model)
@ckeditor/ckeditor5-engine/src/model/range
Filtering
Properties
- 
                        
                        
End position.
 - 
                        
                        
Range root element.
 - 
                        
                        
Start position.
 
Methods
- 
                        
constructor( start, [ end ] )module:engine/model/range~Range#constructorCreates a range spanning from
startposition toendposition.Parameters
 - 
                        
Symbol.iterator() → IterableIterator<TreeWalkerValue>module:engine/model/range~Range#Symbol.iteratorIterable 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 theElements that are entered into when iterating over this range.This iterator uses
TreeWalkerwithboundariesset to this range andignoreElementEndoption set totrue.Returns
IterableIterator<TreeWalkerValue>
 - 
                        
clone() → thismodule:engine/model/range~Range#clone - 
                        
containsItem( item ) → booleanmodule:engine/model/range~Range#containsItem - 
                        
containsPosition( position ) → booleanmodule:engine/model/range~Range#containsPosition - 
                        
containsRange( otherRange, loose ) → booleanmodule:engine/model/range~Range#containsRangeChecks whether this range contains given range.
Parameters
otherRange : RangeRange to check.
loose : booleanWhether 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
booleantrueif given range boundaries are contained by this range,falseotherwise.
 - 
                        
getCommonAncestor() → null | Element | DocumentFragmentmodule:engine/model/range~Range#getCommonAncestorReturns an
ElementorDocumentFragmentwhich is a common ancestor of the range's both ends (in which the entire range is contained).Returns
null | Element | DocumentFragment
 - 
                        
getContainedElement() → null | Elementmodule:engine/model/range~Range#getContainedElement - 
                        
getDifference( otherRange ) → Array<Range>module:engine/model/range~Range#getDifferenceComputes 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 : RangeRange to differentiate against.
Returns
Array<Range>The difference between ranges.
 - 
                        
getIntersection( otherRange ) → null | Rangemodule:engine/model/range~Range#getIntersectionReturns 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 : RangeRange to check for intersection.
Returns
null | RangeA common part of given ranges or
nullif ranges have no common part.
 - 
                        
                        
Returns an iterator that iterates over all items that are in this range and returns them.
This method uses
TreeWalkerwithboundariesset to this range andignoreElementEndoption set totrue. However it returns only model items, notTreeWalkerValue.You may specify additional options for the tree walker. See
TreeWalkerfor a full list of available options.Parameters
options : TreeWalkerOptionsObject with configuration options. See
TreeWalker.Defaults to
{}
Returns
IterableIterator<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 : RangeRange to be joined.
loose : booleanWhether 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
null | RangeA sum of given ranges or
nullif ranges have no common part.
 - 
                        
getMinimalFlatRanges() → Array<Range>module:engine/model/range~Range#getMinimalFlatRangesComputes 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
Helements 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 ) → IterableIterator<Position>module:engine/model/range~Range#getPositionsReturns an iterator that iterates over all positions that are boundaries or contained in this range.
This method uses
TreeWalkerwithboundariesset to this range. However it returns only positions, notTreeWalkerValue.You may specify additional options for the tree walker. See
TreeWalkerfor a full list of available options.Parameters
options : TreeWalkerOptionsObject with configuration options. See
TreeWalker.Defaults to
{}
Returns
IterableIterator<Position>
 - 
                        
getTransformedByOperation( operation ) → Array<Range>module:engine/model/range~Range#getTransformedByOperationReturns 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
Rangeinstances.Parameters
operation : OperationOperation 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 ) → TreeWalkermodule:engine/model/range~Range#getWalkerCreates 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 : TreeWalkerOptionsObject with configuration options. See
TreeWalker.Defaults to
{}
Returns
 - 
                        
                        
Checks whether the object is of type
Elementor its subclass.element.is( 'element' ); // -> true element.is( 'node' ); // -> true element.is( 'model:element' ); // -> true element.is( 'model:node' ); // -> true element.is( 'view:element' ); // -> false element.is( 'documentSelection' ); // -> falseAssuming that the object being checked is an element, you can also check its name:
element.is( 'element', 'imageBlock' ); // -> true if this is an <imageBlock> element text.is( 'element', 'imageBlock' ); -> falseParameters
type : 'element' | 'model:element'
Returns
this is Element | RootElement
 - 
                        
                        
Checks whether the object is of type
Text.text.is( '$text' ); // -> true text.is( 'node' ); // -> true text.is( 'model:$text' ); // -> true text.is( 'model:node' ); // -> true text.is( 'view:$text' ); // -> false text.is( 'documentSelection' ); // -> falseNote: Until version 20.0.0 this method wasn't accepting
'$text'type. The legacy'text'type is still accepted for backward compatibility.Parameters
type : '$text' | 'model:$text'
Returns
this is Text
 - 
                        
                        
Checks whether the object is of type
RootElement.rootElement.is( 'rootElement' ); // -> true rootElement.is( 'element' ); // -> true rootElement.is( 'node' ); // -> true rootElement.is( 'model:rootElement' ); // -> true rootElement.is( 'model:element' ); // -> true rootElement.is( 'model:node' ); // -> true rootElement.is( 'view:element' ); // -> false rootElement.is( 'documentFragment' ); // -> falseAssuming that the object being checked is an element, you can also check its name:
rootElement.is( 'rootElement', '$root' ); // -> same as aboveParameters
type : 'rootElement' | 'model:rootElement'
Returns
this is RootElement
 - 
                        
                        
Checks whether the object is of type
LiveRange.liveRange.is( 'range' ); // -> true liveRange.is( 'model:range' ); // -> true liveRange.is( 'liveRange' ); // -> true liveRange.is( 'model:liveRange' ); // -> true liveRange.is( 'view:range' ); // -> false liveRange.is( 'documentSelection' ); // -> falseParameters
type : 'liveRange' | 'model:liveRange'
Returns
this is LiveRange
 - 
                        
                        
Checks whether the object is of type
Rangeor its subclass.range.is( 'range' ); // -> true range.is( 'model:range' ); // -> true range.is( 'view:range' ); // -> false range.is( 'documentSelection' ); // -> falseParameters
type : 'range' | 'model:range'
Returns
 - 
                        
                        
Checks whether the object is of type
LivePosition.livePosition.is( 'position' ); // -> true livePosition.is( 'model:position' ); // -> true livePosition.is( 'liveposition' ); // -> true livePosition.is( 'model:livePosition' ); // -> true livePosition.is( 'view:position' ); // -> false livePosition.is( 'documentSelection' ); // -> falseParameters
type : 'livePosition' | 'model:livePosition'
Returns
this is LivePosition
 - 
                        
                        
Checks whether the object is of type
Positionor its subclass.position.is( 'position' ); // -> true position.is( 'model:position' ); // -> true position.is( 'view:position' ); // -> false position.is( 'documentSelection' ); // -> falseParameters
type : 'position' | 'model:position'
Returns
this is Position | LivePosition
 - 
                        
inherited
is( type ) → this is Selection | DocumentSelectionmodule:engine/model/range~Range#is:SELECTIONChecks whether the object is of type
SelectionorDocumentSelection.selection.is( 'selection' ); // -> true selection.is( 'model:selection' ); // -> true selection.is( 'view:selection' ); // -> false selection.is( 'range' ); // -> falseParameters
type : 'selection' | 'model:selection'
Returns
this is Selection | DocumentSelection
 - 
                        
                        
Checks whether the object is of type
Elementor its subclass and has the specifiedname.element.is( 'element', 'imageBlock' ); // -> true if this is an <imageBlock> element text.is( 'element', 'imageBlock' ); -> falseType parameters
N : extends string
Parameters
type : 'element' | 'model:element'name : N
Returns
boolean
 - 
                        
                        
Checks whether the object is of type
RootElementand has the specifiedname.rootElement.is( 'rootElement', '$root' );Type parameters
N : extends string
Parameters
type : 'rootElement' | 'model:rootElement'name : N
Returns
boolean
 - 
                        
                        
Checks whether the object is of type
TextProxy.textProxy.is( '$textProxy' ); // -> true textProxy.is( 'model:$textProxy' ); // -> true textProxy.is( 'view:$textProxy' ); // -> false textProxy.is( 'range' ); // -> falseNote: Until version 20.0.0 this method wasn't accepting
'$textProxy'type. The legacy'textProxt'type is still accepted for backward compatibility.Parameters
type : '$textProxy' | 'model:$textProxy'
Returns
this is TextProxy
 - 
                        
inherited
is( type ) → this is DocumentSelectionmodule:engine/model/range~Range#is:DOCUMENT_SELECTIONChecks whether the object is of type
DocumentSelection.selection.is( 'selection' ); // -> true selection.is( 'documentSelection' ); // -> true selection.is( 'model:selection' ); // -> true selection.is( 'model:documentSelection' ); // -> true selection.is( 'view:selection' ); // -> false selection.is( 'element' ); // -> false selection.is( 'node' ); // -> falseParameters
type : 'documentSelection' | 'model:documentSelection'
Returns
this is DocumentSelection
 - 
                        
                        
Checks whether the object is of type
DocumentFragment.docFrag.is( 'documentFragment' ); // -> true docFrag.is( 'model:documentFragment' ); // -> true docFrag.is( 'view:documentFragment' ); // -> false docFrag.is( 'element' ); // -> false docFrag.is( 'node' ); // -> falseParameters
type : 'documentFragment' | 'model:documentFragment'
Returns
this is DocumentFragment
 - 
                        
inherited
is( type ) → this is Node | Text | Element | RootElementmodule:engine/model/range~Range#is:NODEChecks whether the object is of type
Nodeor its subclass.This method is useful when processing model objects that are of unknown type. For example, a function may return a
DocumentFragmentor aNodethat can be either a text node or an element. This method can be used to check what kind of object is returned.someObject.is( 'element' ); // -> true if this is an element someObject.is( 'node' ); // -> true if this is a node (a text node or an element) someObject.is( 'documentFragment' ); // -> true if this is a document fragmentSince this method is also available on a range of view objects, you can prefix the type of the object with
model:orview:to check, for example, if this is the model's or view's element:modelElement.is( 'model:element' ); // -> true modelElement.is( 'view:element' ); // -> falseBy using this method it is also possible to check a name of an element:
imageElement.is( 'element', 'imageBlock' ); // -> true imageElement.is( 'element', 'imageBlock' ); // -> same as above imageElement.is( 'model:element', 'imageBlock' ); // -> same as above, but more preciseParameters
type : 'node' | 'model:node'
Returns
this is Node | Text | Element | RootElement
 - 
                        
isEqual( otherRange ) → booleanmodule:engine/model/range~Range#isEqual - 
                        
isIntersecting( otherRange ) → booleanmodule:engine/model/range~Range#isIntersectingChecks and returns whether this range intersects with given range.
Parameters
otherRange : RangeRange to compare with.
Returns
booleantrueif ranges intersect,falseotherwise.
 - 
                        
toJSON() → unknownmodule:engine/model/range~Range#toJSONConverts
Rangeto plain object and returns it.Returns
unknownNodeconverted to plain object.
 - 
                        
internal
_getTransformedByDeletion( deletePosition, howMany ) → null | Rangemodule:engine/model/range~Range#_getTransformedByDeletionReturns a copy of this range that is transformed by deletion of
howManynodes fromdeletePosition.If the deleted range is intersecting with the transformed range, the transformed range will be shrank.
If the deleted range contains transformed range,
nullwill be returned.Parameters
deletePosition : PositionPosition from which nodes are removed.
howMany : numberHow many nodes are removed.
Returns
null | RangeResult of the transformation.
 - 
                        
internal
_getTransformedByInsertOperation( operation, spread ) → Array<Range>module:engine/model/range~Range#_getTransformedByInsertOperationReturns 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 : InsertOperationspread : boolean- 
                                          
Defaults to
false 
Returns
Array<Range>
 - 
                        
internal
_getTransformedByInsertion( insertPosition, howMany, spread ) → Array<Range>module:engine/model/range~Range#_getTransformedByInsertionReturns an array containing one or two ranges that are a result of transforming this range by inserting
howManynodes atinsertPosition. Two ranges are returned if the insertion was inside this range andspreadis 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 : PositionPosition where nodes are inserted.
howMany : numberHow many nodes are inserted.
spread : booleanFlag indicating whether this range should be spread if insertion was inside the range. Defaults to
false.Defaults to
false
Returns
Array<Range>Result of the transformation.
 - 
                        
internal
_getTransformedByMergeOperation( operation ) → Rangemodule:engine/model/range~Range#_getTransformedByMergeOperationReturns 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
 - 
                        
internal
_getTransformedByMove( sourcePosition, targetPosition, howMany, spread ) → Array<Range>module:engine/model/range~Range#_getTransformedByMoveReturns an array containing ranges that are a result of transforming this range by moving
howManynodes fromsourcePositiontotargetPosition.Parameters
sourcePosition : PositionPosition from which nodes are moved.
targetPosition : PositionPosition to where nodes are moved.
howMany : numberHow many nodes are moved.
spread : booleanWhether the range should be spread if the move points inside the range.
Defaults to
false
Returns
Array<Range>Result of the transformation.
 - 
                        
internal
_getTransformedByMoveOperation( operation, spread ) → Array<Range>module:engine/model/range~Range#_getTransformedByMoveOperationReturns 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 : MoveOperationspread : boolean- 
                                          
Defaults to
false 
Returns
Array<Range>
 - 
                        
internal
_getTransformedBySplitOperation( operation ) → Rangemodule:engine/model/range~Range#_getTransformedBySplitOperationReturns 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
- 
                        
internal static
_createFromPositionAndShift( position, shift ) → Rangemodule:engine/model/range~Range._createFromPositionAndShift - 
                        
internal static
_createFromRanges( ranges ) → Rangemodule:engine/model/range~Range._createFromRangesCombines 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
RangeCombined 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.
Parameters
element : Element | DocumentFragmentElement which is a parent for the range.
Returns
 - 
                        
                        
Creates a range that starts before given model item and ends after it.
Parameters
item : Item
Returns
 
Every day, we work hard to keep our documentation complete. Have you spotted outdated information? Is something missing? Please report it via our issue tracker.
With the release of version 42.0.0, we have rewritten much of our documentation to reflect the new import paths and features. We appreciate your feedback to help us ensure its accuracy and completeness.