DocumentFragment (engine/model)
@ckeditor/ckeditor5-engine/src/model/documentfragment
DocumentFragment represents a part of model which does not have a common root but its top-level nodes can be seen as siblings. In other words, it is a detached part of model tree, without a root.
DocumentFragment has own MarkerCollection
. Markers from this collection
will be set to the model markers by a
insert
function.
Filtering
Properties
-
childCount : Number
module:engine/model/documentfragment~DocumentFragment#childCount
readonly
Number of this document fragment's children.
-
isEmpty : Boolean
module:engine/model/documentfragment~DocumentFragment#isEmpty
readonly
Is
true
if there are no nodes inside this document fragment,false
otherwise. -
DocumentFragment static markers map. This is a list of names and ranges which will be set as Markers to model markers collection when DocumentFragment will be inserted to the document.
-
maxOffset : Number
module:engine/model/documentfragment~DocumentFragment#maxOffset
readonly
Sum of offset sizes of all of this document fragment's children.
-
parent : null
module:engine/model/documentfragment~DocumentFragment#parent
readonly
Artificial parent of
DocumentFragment
. Returnsnull
. Added for compatibility reasons. -
root : DocumentFragment
module:engine/model/documentfragment~DocumentFragment#root
readonly
Artificial root of
DocumentFragment
. Returns itself. Added for compatibility reasons. -
List of nodes contained inside the document fragment.
Methods
-
constructor( [ children ] )
module:engine/model/documentfragment~DocumentFragment#constructor
protected
Creates an empty
DocumentFragment
.Note: Constructor of this class shouldn't be used directly in the code. Use the
createDocumentFragment
method instead.Parameters
-
Symbol.iterator() → Iterable.<Node>
module:engine/model/documentfragment~DocumentFragment#Symbol.iterator
Returns an iterator that iterates over all nodes contained inside this document fragment.
Returns
Iterable.<Node>
-
Gets the child at the given index. Returns
null
if incorrect index was passed. -
getChildIndex( node ) → Number | null
module:engine/model/documentfragment~DocumentFragment#getChildIndex
Returns an index of the given child node. Returns
null
if given node is not a child of this document fragment. -
getChildStartOffset( node ) → Number | null
module:engine/model/documentfragment~DocumentFragment#getChildStartOffset
Returns the starting offset of given child. Starting offset is equal to the sum of offset sizes of all node's siblings that are before it. Returns
null
if given node is not a child of this document fragment. -
getChildren() → Iterable.<Node>
module:engine/model/documentfragment~DocumentFragment#getChildren
Returns an iterator that iterates over all of this document fragment's children.
Returns
Iterable.<Node>
-
getNodeByPath( relativePath ) → Node | DocumentFragment
module:engine/model/documentfragment~DocumentFragment#getNodeByPath
Returns a descendant node by its path relative to this element.
// <this>a<b>c</b></this> this.getNodeByPath( [ 0 ] ); // -> "a" this.getNodeByPath( [ 1 ] ); // -> <b> this.getNodeByPath( [ 1, 0 ] ); // -> "c"
Parameters
relativePath : Array.<Number>
Path of the node to find, relative to this element.
Returns
-
getPath() → Array
module:engine/model/documentfragment~DocumentFragment#getPath
Returns path to a
DocumentFragment
, which is an empty array. Added for compatibility reasons.Returns
Array
-
is( type ) → Boolean
module:engine/model/documentfragment~DocumentFragment#is
Checks whether this object is of the given type.
docFrag.is( 'documentFragment' ); // -> true docFrag.is( 'model:documentFragment' ); // -> true docFrag.is( 'view:documentFragment' ); // -> false docFrag.is( 'element' ); // -> false docFrag.is( 'node' ); // -> false
Check the entire list of model objects which implement the
is()
method.Parameters
type : String
Returns
Boolean
-
offsetToIndex( offset ) → Number
module:engine/model/documentfragment~DocumentFragment#offsetToIndex
Converts offset "position" to index "position".
Returns index of a node that occupies given offset. If given offset is too low, returns
0
. If given offset is too high, returns index after last child}.const textNode = new Text( 'foo' ); const pElement = new Element( 'p' ); const docFrag = new DocumentFragment( [ textNode, pElement ] ); docFrag.offsetToIndex( -1 ); // Returns 0, because offset is too low. docFrag.offsetToIndex( 0 ); // Returns 0, because offset 0 is taken by `textNode` which is at index 0. docFrag.offsetToIndex( 1 ); // Returns 0, because `textNode` has `offsetSize` equal to 3, so it occupies offset 1 too. docFrag.offsetToIndex( 2 ); // Returns 0. docFrag.offsetToIndex( 3 ); // Returns 1. docFrag.offsetToIndex( 4 ); // Returns 2. There are no nodes at offset 4, so last available index is returned.
Parameters
offset : Number
Offset to look for.
Returns
Number
Index of a node that occupies given offset.
-
toJSON() → Object
module:engine/model/documentfragment~DocumentFragment#toJSON
Converts
DocumentFragment
instance to plain object and returns it. Takes care of converting all of this document fragment's children.Returns
Object
DocumentFragment
instance converted to plain object.
-
_appendChild( items )
module:engine/model/documentfragment~DocumentFragment#_appendChild
protected
Inserts one or more nodes at the end of this document fragment.
-
_insertChild( index, items )
module:engine/model/documentfragment~DocumentFragment#_insertChild
protected
Inserts one or more nodes at the given index and sets parent of these nodes to this document fragment.
-
_removeChildren( index, [ howMany ] ) → Array.<Node>
module:engine/model/documentfragment~DocumentFragment#_removeChildren
protected
Static methods
-
fromJSON( json ) → DocumentFragment
module:engine/model/documentfragment~DocumentFragment.fromJSON
static
Creates a
DocumentFragment
instance from given plain object (i.e. parsed JSON string). ConvertsDocumentFragment
children to proper nodes.Parameters
json : Object
Plain object to be converted to
DocumentFragment
.
Returns
DocumentFragment
DocumentFragment
instance created using given plain object.
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.