Position (engine/model)
@ckeditor/ckeditor5-engine/src/model/position
Represents a position in the model tree.
A position is represented by its root
and
a path
in that root.
You can create position instances via its constructor or the createPosition*()
factory methods of
Model
and Writer
.
Note: Position is based on offsets, not indexes. This means that a position between two text nodes
foo
and bar
has offset 3
, not 1
. See path
for more information.
Since a position in the model is represented by a position root and position path it is possible to create positions placed in non-existing places. This requirement is important for operational transformation algorithms.
Also, operations kept in the document history are storing positions (and ranges) which were correct when those operations were applied, but may not be correct after the document has changed.
When changes are applied to the model, it may also happen that position parent
will change even if position path has not changed. Keep in mind, that if a position leads to non-existing element,
parent
and some other properties and methods will throw errors.
In most cases, position with wrong path is caused by an error in code, but it is sometimes needed, as described above.
Filtering
Properties
-
index : Number
readonly
-
isAtEnd : Boolean
readonly
Is
true
if position is at the end of its parent,false
otherwise. -
isAtStart : Boolean
readonly
Is
true
if position is at the beginning of its parent,false
otherwise. -
Node directly after this position or
null
if this position is in text node. -
nodeBefore : Node | null
readonly
Node directly before this position or
null
if this position is in text node. -
offset : Number
-
parent : Element | DocumentFragment
readonly
Parent element of this position.
Keep in mind that
parent
value is calculated when the property is accessed. If position path leads to a non-existing element,parent
property will throw error.Also it is a good idea to cache
parent
property if it is used frequently in an algorithm (i.e. in a long loop). -
path : Array.<Number>
readonly
Position of the node in the tree. Path contains offsets, not indexes.
Position can be placed before, after or in a node if that node has
offsetSize
greater than1
. Items in position path are starting offsets of position ancestors, starting from direct root children, down to the position offset in it's parent.foo
andbar
are representing text nodes. Since text nodes has offset size greater than1
you can place position offset between their start and end: -
root : Element | DocumentFragment
readonly
Root of the position path.
-
Position stickiness. See
PositionStickiness
. -
Returns text node instance in which this position is placed or
null
if this position is not in a text node.
Methods
-
constructor( root, path, [ stickiness ] )
Creates a position.
Parameters
root : Element | DocumentFragment
Root of the position.
path : Array.<Number>
Position path. See
path
.[ stickiness ] : PositionStickiness
Position stickiness. See
PositionStickiness
.Defaults to
'toNone'
-
Returns a new position that is equal to current position.
Returns
-
compareWith( otherPosition ) → PositionRelation
Checks whether this position is before or after given position.
This method is safe to use it on non-existing positions (for example during operational transformation).
Parameters
otherPosition : Position
Position to compare with.
Returns
-
findAncestor( parentName ) → Element | null
Returns the parent element of the given name. Returns null if the position is not inside the desired parent.
-
getAncestors() → Array.<Item>
Returns ancestors array of this position, that is this position's parent and its ancestors.
Returns
Array.<Item>
Array with ancestors.
-
getCommonAncestor( position ) → Element | DocumentFragment | null
Returns an
Element
orDocumentFragment
which is a common ancestor of both positions. The roots of these two positions must be identical. -
getCommonPath( position ) → Array.<Number>
Returns the slice of two position paths which is identical. The roots of these two paths must be identical.
This method is safe to use it on non-existing positions (for example during operational transformation).
Parameters
position : Position
The second position.
Returns
Array.<Number>
The common path.
-
getLastMatchingPosition( skip, options ) → Position
Gets the farthest position which matches the callback using TreeWalker.
For example:
Parameters
skip : function
Callback function. Gets
TreeWalkerValue
and should returntrue
if the value should be skipped orfalse
if not.options : Object
Object with configuration options. See
TreeWalker
.
Returns
Position
The position after the last item which matches the
skip
callback test.
-
getParentPath() → Array.<Number>
Returns a path to this position's parent. Parent path is equal to position path but without the last item.
This method is safe to use it on non-existing positions (for example during operational transformation).
Returns
Array.<Number>
Path to the parent.
-
getShiftedBy( shift ) → Position
Returns a new instance of
Position
, that has same parent but it's offset is shifted byshift
value (can be a negative value).This method is safe to use it on non-existing positions (for example during operational transformation).
Parameters
shift : Number
Offset shift. Can be a negative value.
Returns
Position
Shifted position.
-
getTransformedByOperation( operation ) → Position
Returns a copy of this position that is transformed by given
operation
.The new position's parameters are updated accordingly to the effect of the
operation
.For example, if
n
nodes are inserted before the position, the returned positionoffset
will be increased byn
. If the position was in a merged element, it will be accordingly moved to the new element, etc.This method is safe to use it on non-existing positions (for example during operational transformation).
Parameters
operation : Operation
Operation to transform by.
Returns
Position
Transformed position.
-
hasSameParentAs( position ) → Boolean
Checks if two positions are in the same parent.
This method is safe to use it on non-existing positions (for example during operational transformation).
Parameters
position : Position
Position to compare with.
Returns
Boolean
true
if positions have the same parent,false
otherwise.
-
is( type ) → Boolean
Checks whether this object is of the given.
Check the entire list of model objects which implement the
is()
method.Parameters
type : String
Returns
Boolean
-
isAfter( otherPosition ) → Boolean
Checks whether this position is after given position.
This method is safe to use it on non-existing positions (for example during operational transformation).
Parameters
otherPosition : Position
Position to compare with.
Returns
Boolean
True if this position is after given position.
Related:
-
isBefore( otherPosition ) → Boolean
Checks whether this position is before given position.
Note: watch out when using negation of the value returned by this method, because the negation will also be
true
if positions are in different roots and you might not expect this. You should probably usea.isAfter( b ) || a.isEqual( b )
or!a.isBefore( p ) && a.root == b.root
in most scenarios. If your condition uses multipleisAfter
andisBefore
checks, build them so they do not use negated values, i.e.:or, if you have only one if-branch:
rather than:
This method is safe to use it on non-existing positions (for example during operational transformation).
Parameters
otherPosition : Position
Position to compare with.
Returns
Boolean
True if this position is before given position.
-
isEqual( otherPosition ) → Boolean
Checks whether this position is equal to given position.
This method is safe to use it on non-existing positions (for example during operational transformation).
Parameters
otherPosition : Position
Position to compare with.
Returns
Boolean
True if positions are same.
-
isTouching( otherPosition ) → Boolean
Checks whether this position is touching given position. Positions touch when there are no text nodes or empty nodes in a range between them. Technically, those positions are not equal but in many cases they are very similar or even indistinguishable.
Parameters
otherPosition : Position
Position to compare with.
Returns
Boolean
True if positions touch.
-
_getCombined( source, target ) → Position
protected
Returns a new position that is a combination of this position and given positions.
The combined position is a copy of this position transformed by moving a range starting at
source
position to thetarget
position. It is expected that this position is inside the moved range.Example:
Explanation:
We have a position
[ 2, 3, 1 ]
and move some nodes from[ 2, 2 ]
to[ 1, 1, 3 ]
. The original position was inside moved nodes and now should point to the new place. The moved nodes will be after positions[ 1, 1, 3 ]
,[ 1, 1, 4 ]
,[ 1, 1, 5 ]
. Since our position was in the second moved node, the transformed position will be in a sub-tree of a node at[ 1, 1, 4 ]
. Looking at original path, we took care of[ 2, 3 ]
part of it. Now we have to add the rest of the original path to the transformed path. Finally, the transformed position will point to[ 1, 1, 4, 1 ]
.Parameters
source : Position
Beginning of the moved range.
target : Position
Position where the range is moved.
Returns
Position
Combined position.
-
_getTransformedByDeletion( deletePosition, howMany ) → Position | null
protected
Returns a copy of this position that is updated by removing
howMany
nodes starting fromdeletePosition
. It may happen that this position is in a removed node. If that is the case,null
is returned instead. -
_getTransformedByInsertOperation( operation ) → Position
protected
Returns a copy of this position transformed by an insert operation.
-
_getTransformedByInsertion( insertPosition, howMany ) → Position
protected
Returns a copy of this position that is updated by inserting
howMany
nodes atinsertPosition
. -
_getTransformedByMergeOperation( operation ) → Position
protected
Returns a copy of this position transformed by merge operation.
-
_getTransformedByMove( sourcePosition, targetPosition, howMany ) → Position
protected
Returns a copy of this position that is updated by moving
howMany
nodes fromsourcePosition
totargetPosition
. -
_getTransformedByMoveOperation( operation ) → Position
protected
Returns a copy of this position transformed by a move operation.
-
_getTransformedBySplitOperation( operation ) → Position
protected
Returns a copy of this position transformed by a split operation.
Static methods
-
Creates a
Position
instance from given plain object (i.e. parsed JSON string). -
_createAfter( item, [ stickiness ] ) → Position
protected static
Creates a new position, after given model item.
Parameters
item : Item
Item after which the position should be placed.
[ stickiness ] : PositionStickiness
Position stickiness.
Defaults to
'toNone'
Returns
-
_createAt( itemOrPosition, [ offset ], [ stickiness ] )
protected static
Creates position at the given location. The location can be specified as:
- a position,
- parent element and offset (offset defaults to
0
), - parent element and
'end'
(sets position at the end of that element), - model item and
'before'
or'after'
(sets position before or after given model item).
This method is a shortcut to other factory methods such as:
Parameters
itemOrPosition : Item | Position
[ offset ] : Number | 'end' | 'before' | 'after'
Offset or one of the flags. Used only when the first parameter is a model item.
[ stickiness ] : PositionStickiness
Position stickiness. Used only when the first parameter is a model item.
Defaults to
'toNone'
-
_createBefore( item, [ stickiness ] ) → Position
protected static
Creates a new position, before the given model item.
Parameters
item : Item
Item before which the position should be placed.
[ stickiness ] : PositionStickiness
Position stickiness.
Defaults to
'toNone'
Returns
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.