Element (engine/view)
@ckeditor/ckeditor5-engine/src/view/element
View element.
The editing engine does not define a fixed semantics of its elements (it is "DTD-free").
This is why the type of the Element
need to
be defined by the feature developer. When creating an element you should use one of the following methods:
writer.createContainerElement()
in order to create aContainerElement
,writer.createAttributeElement()
in order to create aAttributeElement
,writer.createEmptyElement()
in order to create aEmptyElement
.writer.createUIElement()
in order to create aUIElement
.writer.createEditableElement()
in order to create aEditableElement
.
Note that for view elements which are not created from the model, like elements from mutations, paste or
data.set it is not possible to define the type of the element, so
these will be instances of the Element
.
Filtering
Properties
-
childCount : Number
readonly
Number of element's children.
-
View document that owns this node, or
null
if the node is inside document fragment. -
index : Number | null
readonly inherited
Index of the node in the parent element or null if the node has no parent.
Accessing this property throws an error if this node's parent element does not contain it. This means that view tree got broken.
-
isEmpty : Boolean
readonly
Is
true
if there are no nodes inside this element,false
otherwise. -
name : String
readonly
Name of the element.
-
nextSibling : Node | null
readonly inherited
Node's next sibling, or
null
if it is the last child. -
parent : Element | DocumentFragment | null
readonly inherited
Parent element. Null by default. Set by
_insertChild
. -
previousSibling : Node | null
readonly inherited
Node's previous sibling, or
null
if it is the first child. -
root : Node | DocumentFragment
readonly inherited
Top-most ancestor of the node. If the node has no parent it is the root itself.
-
_attrs : Map
protected
Map of attributes, where attributes names are keys and attributes values are values.
-
Array of child nodes.
-
_classes : Set
protected
Set of classes associated with element instance.
-
_customProperties
protected
Map of custom properties. Custom properties can be added to element instance, will be cloned but not rendered into DOM.
-
_styles : Set
protected
Map of styles.
Methods
-
constructor( name, [ attrs ], [ children ] )
protected
Creates a view element.
Attributes can be passed in various formats:
new Element( 'div', { 'class': 'editor', 'contentEditable': 'true' } ); // object new Element( 'div', [ [ 'class', 'editor' ], [ 'contentEditable', 'true' ] ] ); // map-like iterator new Element( 'div', mapOfAttributes ); // map
Note: Constructor of this class shouldn't be used directly in the code. Use the
createAttributeElement
for inline element,createContainerElement
for block element,createEditableElement
for editable element,createEmptyElement
for empty element orcreateUIElement
for UI element instead.Parameters
name : String
Node name.
[ attrs ] : Object | Iterable
Collection of attributes.
[ children ] : Node | Iterable.<Node>
List of nodes to be inserted into created element.
Overrides: Node#constructor -
_fireChange( type, node )
inherited
-
_removeChildren( index, [ howMany ] ) → Array.<Node>
-
_removeClass( className )
Removes specified class.
element._removeClass( 'foo' ); // Removes 'foo' class. element._removeClass( [ 'foo', 'bar' ] ); // Removes both 'foo' and 'bar' classes.
Parameters
className : Array.<String> | String
Fires
-
findAncestor( patterns ) → Element | null
Returns ancestor element that match specified pattern. Provided patterns should be compatible with Matcher as it is used internally.
-
getAncestors( options = { [options.includeSelf], [options.parentFirst] } ) → Array
inherited
Returns ancestors array of this node.
Parameters
options : Object
Options object.
Properties[ options.includeSelf ] : Boolean
When set to
true
this node will be also included in parent's array.Defaults to
false
[ options.parentFirst ] : Boolean
When set to
true
, array will be sorted from node's parent to root element, otherwise root element will be the first item in the array.Defaults to
false
Returns
Array
Array with ancestors.
-
getAttribute( key ) → String | undefined
Gets attribute by key. If attribute is not present - returns undefined.
Parameters
key : String
Attribute key.
Returns
String | undefined
Attribute value.
-
getAttributeKeys() → Iterable.<String>
Returns an iterator that contains the keys for attributes. Order of inserting attributes is not preserved.
Returns
Iterable.<String>
Keys for attributes.
-
getAttributes() → Iterable.<*>
Returns iterator that iterates over this element's attributes.
Attributes are returned as arrays containing two items. First one is attribute key and second is attribute value. This format is accepted by native
Map
object and also can be passed inNode
constructor.Returns
Iterable.<*>
-
Gets child at the given index.
-
getChildIndex( node ) → Number
Gets index of the given child node. Returns
-1
if child node is not found. -
getChildren() → Iterable.<Node>
Gets child nodes iterator.
Returns
Iterable.<Node>
Child nodes iterator.
-
getClassNames() → Iterable.<String>
Returns iterator that contains all class names.
Returns
Iterable.<String>
-
getCommonAncestor( node, options = { [options.includeSelf] } ) → Element | DocumentFragment | null
inherited
Returns a
Element
orDocumentFragment
which is a common ancestor of both nodes.Parameters
node : Node
The second node.
options : Object
Options object.
Properties[ options.includeSelf ] : Boolean
When set to
true
both nodes will be considered "ancestors" too. Which means that if e.g. node A is inside B, then their common ancestor will be B.Defaults to
false
Returns
Element | DocumentFragment | null
-
getCustomProperties() → Iterable.<*>
Returns an iterator which iterates over this element's custom properties. Iterator provides
[ key, value ]
pairs for each stored property.Returns
Iterable.<*>
-
getCustomProperty( key ) → *
Returns the custom property value for the given key.
Parameters
key : String | Symbol
Returns
*
-
Returns block filler offset or
null
if block filler is not needed. -
getIdentity() → String
Returns identity string based on element's name, styles, classes and other attributes. Two elements that are similar will have same identity string. It has the following format:
'name class="class1,class2" style="style1:value1;style2:value2" attr1="val1" attr2="val2"'
For example:
const element = writer.createContainerElement( 'foo', { banana: '10', apple: '20', style: 'color: red; border-color: white;', class: 'baz' } ); // returns 'foo class="baz" style="border-color:white;color:red" apple="20" banana="10"' element.getIdentity();
NOTE: Classes, styles and other attributes are sorted alphabetically.
Returns
String
-
getPath() → Array.<Number>
inherited
Gets a path to the node. The path is an array containing indices of consecutive ancestors of this node, beginning from root, down to this node's index.
const abc = new Text( 'abc' ); const foo = new Text( 'foo' ); const h1 = new Element( 'h1', null, new Text( 'header' ) ); const p = new Element( 'p', null, [ abc, foo ] ); const div = new Element( 'div', null, [ h1, p ] ); foo.getPath(); // Returns [ 1, 3 ]. `foo` is in `p` which is in `div`. `p` starts at offset 1, while `foo` at 3. h1.getPath(); // Returns [ 0 ]. div.getPath(); // Returns [].
Returns
Array.<Number>
The path.
-
getStyle( property ) → String | undefined
Returns style value for given property. Undefined is returned if style does not exist.
Parameters
property : String
Returns
String | undefined
-
getStyleNames() → Iterable.<String>
Returns iterator that contains all style names.
Returns
Iterable.<String>
-
hasAttribute( key ) → Boolean
Returns a boolean indicating whether an attribute with the specified key exists in the element.
Parameters
key : String
Attribute key.
Returns
Boolean
true
if attribute with the specified key exists in the element, false otherwise.
-
hasClass( className )
Returns true if class is present. If more then one class is provided - returns true only when all classes are present.
element.hasClass( 'foo' ); // Returns true if 'foo' class is present. element.hasClass( 'foo', 'bar' ); // Returns true if 'foo' and 'bar' classes are both present.
Parameters
className : String
-
hasStyle( property )
Returns true if style keys are present. If more then one style property is provided - returns true only when all properties are present.
element.hasStyle( 'color' ); // Returns true if 'border-top' style is present. element.hasStyle( 'color', 'border-top' ); // Returns true if 'color' and 'border-top' styles are both present.
Parameters
property : String
-
is( type, [ name ] ) → Boolean
-
isAfter( node ) → Boolean
inherited
Returns whether this node is after given node.
false
is returned if nodes are in different trees (for example, in differentDocumentFragment
s). -
isBefore( node ) → Boolean
inherited
Returns whether this node is before given node.
false
is returned if nodes are in different trees (for example, in differentDocumentFragment
s). -
isSimilar( otherElement ) → Boolean
Checks if this element is similar to other element. Both elements should have the same name and attributes to be considered as similar. Two similar elements can contain different set of children nodes.
-
toJSON() → Object
inherited
Custom toJSON method to solve child-parent circular dependencies.
Returns
Object
Clone of this object with the parent property removed.
-
_addClass( className )
protected
Adds specified class.
element._addClass( 'foo' ); // Adds 'foo' class. element._addClass( [ 'foo', 'bar' ] ); // Adds 'foo' and 'bar' classes.
Parameters
className : Array.<String> | String
Fires
-
_appendChild( items ) → Number
protected
-
Clones provided element.
Parameters
[ deep ] : Boolean
If set to
true
clones element and all its children recursively. When set tofalse
, element will be cloned without any children.Defaults to
false
Returns
Element
Clone of this element.
Overrides: Node#_clone -
_insertChild( index, items ) → Number
protected
-
_remove()
protected inherited
Removes node from parent.
-
_removeAttribute( key ) → Boolean
protected
Removes attribute from the element.
Parameters
key : String
Attribute key.
Returns
Boolean
Returns true if an attribute existed and has been removed.
Fires
-
_removeCustomProperty( key ) → Boolean
protected
Removes the custom property stored under the given key.
Parameters
key : String | Symbol
Returns
Boolean
Returns true if property was removed.
-
_removeStyle( property )
protected
Removes specified style.
element._removeStyle( 'color' ); // Removes 'color' style. element._removeStyle( [ 'color', 'border-top' ] ); // Removes both 'color' and 'border-top' styles.
Parameters
property : Array.<String> | String
Fires
-
_setAttribute( key, value )
protected
Adds or overwrite attribute with a specified key and value.
-
_setCustomProperty( key, value )
protected
Sets a custom property. Unlike attributes, custom properties are not rendered to the DOM, so they can be used to add special data to elements.
Parameters
key : String | Symbol
value : *
-
_setStyle( property, [ value ] )
protected
Adds style to the element.
element._setStyle( 'color', 'red' ); element._setStyle( { color: 'red', position: 'fixed' } );
Parameters
property : String | Object
Property name or object with key - value pairs.
[ value ] : String
Value to set. This parameter is ignored if object is provided as the first parameter.
Fires
Events
-
change( eventInfo )
inherited
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
-
change:attributes( eventInfo, changedNode )
inherited
-
change:children( eventInfo, changedNode )
inherited
-
change:text( eventInfo, changedNode )
inherited
Fired when text nodes data changes.