Writer (engine/view)
@ckeditor/ckeditor5-engine/src/view/writer
View writer class. Provides set of methods used to properly manipulate nodes attached to view document. It is not recommended to use it directly. To get an instance of view writer associated with the document use {@link module:engine/view/view~View#change view.change()) method.
Filtering
Properties
-
-
_cloneGroups : Mapprivate
Holds references to the attribute groups that share the same id. The keys are
ids, the values areSets holdingAttributeElements.
Methods
-
addClass( className, element )Adds specified class to the element.
writer.addClass( 'foo', linkElement ); writer.addClass( [ 'foo', 'bar' ], linkElement );Parameters
className : Array.<String> | Stringelement : Element
-
breakAttributes( positionOrRange ) → Position | RangeBreaks attribute nodes at provided position or at boundaries of provided range. It breaks attribute elements inside up to a container element.
In following examples
<p>is a container,<b>and<u>are attribute nodes:<p>foo<b><u>bar{}</u></b></p> -> <p>foo<b><u>bar</u></b>[]</p> <p>foo<b><u>{}bar</u></b></p> -> <p>foo{}<b><u>bar</u></b></p> <p>foo<b><u>b{}ar</u></b></p> -> <p>foo<b><u>b</u></b>[]<b><u>ar</u></b></p> <p><b>fo{o</b><u>ba}r</u></p> -> <p><b>fo</b><b>o</b><u>ba</u><u>r</u></b></p>Note: DocumentFragment is treated like a container.
Note: Difference between breakAttributes and breakContainer is that
breakAttributesbreaks all attribute elements that are ancestors of givenposition, up to the first encountered container element.breakContainerassumes that givenpositionis directly in container element and breaks that container element.Throws CKEditorError
view-writer-invalid-range-containerwhen start and end positions of a passed range are not placed inside same parent container.Throws CKEditorError
view-writer-cannot-break-empty-elementwhen trying to break attributes inside EmptyElement.Throws CKEditorError
view-writer-cannot-break-ui-elementwhen trying to break attributes inside UIElement.Parameters
Returns
-
breakContainer( position ) → PositionBreaks container view element into two, at the given position. Position has to be directly inside container element and cannot be in root. Does not break if position is at the beginning or at the end of it's parent element.
<p>foo^bar</p> -> <p>foo</p><p>bar</p> <div><p>foo</p>^<p>bar</p></div> -> <div><p>foo</p></div><div><p>bar</p></div> <p>^foobar</p> -> ^<p>foobar</p> <p>foobar^</p> -> <p>foobar</p>^Note: Difference between breakAttributes and breakContainer is that
breakAttributesbreaks all attribute elements that are ancestors of givenposition, up to the first encountered container element.breakContainerassumes that givenpositionis directly in container element and breaks that container element.Parameters
position : PositionPosition where to break element.
Returns
PositionPosition between broken elements. If element has not been broken, the returned position is placed either before it or after it.
-
clear( range, element )Removes matching elements from given range.
Throws CKEditorError
view-writer-invalid-range-containerwhen start and end positions are not placed inside same parent container.Parameters
-
createAttributeElement( name, [ attributes ], [ options ] = { [options.priority], [options.id] } ) → AttributeElementCreates new
AttributeElement.writer.createAttributeElement( 'strong' ); writer.createAttributeElement( 'strong', { 'alignment': 'center' } ); // Make `<a>` element contain other attributes element so the `<a>` element is not broken. writer.createAttributeElement( 'a', { href: 'foo.bar' }, { priority: 5 } ); // Set `id` of a marker element so it is not joined or merged with "normal" elements. writer.createAttributeElement( 'span', { class: 'myMarker' }, { id: 'marker:my' } );Parameters
name : StringName of the element.
[ attributes ] : ObjectElement's attributes.
[ options ] : ObjectElement's options.
Properties
Returns
AttributeElementCreated element.
-
createContainerElement( name, [ attributes ] ) → ContainerElementCreates new
ContainerElement.writer.createContainerElement( 'paragraph' ); writer.createContainerElement( 'paragraph', { 'alignment': 'center' } );Parameters
name : StringName of the element.
[ attributes ] : ObjectElements attributes.
Returns
ContainerElementCreated element.
-
createEditableElement( name, [ attributes ] ) → EditableElementCreates new
EditableElement.writer.createEditableElement( 'div' ); writer.createEditableElement( 'div', { 'alignment': 'center' } );Parameters
name : StringName of the element.
[ attributes ] : ObjectElements attributes.
Returns
EditableElementCreated element.
-
createEmptyElement( name, [ attributes ] ) → EmptyElementCreates new
EmptyElement.writer.createEmptyElement( 'img' ); writer.createEmptyElement( 'img', { 'alignment': 'center' } );Parameters
name : StringName of the element.
[ attributes ] : ObjectElements attributes.
Returns
EmptyElementCreated element.
-
createText( data ) → Text -
createUIElement( name, [ attributes ], [ renderFunction ] ) → UIElementCreates new
UIElement.writer.createUIElement( 'span' ); writer.createUIElement( 'span', { 'alignment': 'center' } );Custom render function can be provided as third parameter:
writer.createUIElement( 'span', null, function( domDocument ) { const domElement = this.toDomElement( domDocument ); domElement.innerHTML = '<b>this is ui element</b>'; return domElement; } );Parameters
name : StringName of the element.
[ attributes ] : ObjectElements attributes.
[ renderFunction ] : functionCustom render function.
Returns
UIElementCreated element.
-
Insert node or nodes at specified position. Takes care about breaking attributes before insertion and merging them afterwards.
Throws CKEditorError
view-writer-insert-invalid-nodewhen nodes to insert contains instances that are not Texts, AttributeElements, ContainerElements, EmptyElements or UIElements.Parameters
position : PositionInsertion position.
nodes : Text | AttributeElement | ContainerElement | EmptyElement | UIElement | Iterable.<(Text | AttributeElement | ContainerElement | EmptyElement | UIElement)>Node or nodes to insert.
Returns
RangeRange around inserted nodes.
-
mergeAttributes( position ) → PositionMerges attribute elements. It also merges text nodes if needed. Only similar attribute elements can be merged.
In following examples
<p>is a container and<b>is an attribute element:<p>foo[]bar</p> -> <p>foo{}bar</p> <p><b>foo</b>[]<b>bar</b></p> -> <p><b>foo{}bar</b></p> <p><b foo="bar">a</b>[]<b foo="baz">b</b></p> -> <p><b foo="bar">a</b>[]<b foo="baz">b</b></p>It will also take care about empty attributes when merging:
<p><b>[]</b></p> -> <p>[]</p> <p><b>foo</b><i>[]</i><b>bar</b></p> -> <p><b>foo{}bar</b></p>Note: Difference between mergeAttributes and mergeContainers is that
mergeAttributesmerges two attribute elements or text nodes whilemergeContainermerges two container elements.Parameters
position : PositionMerge position.
Returns
PositionPosition after merge.
-
mergeContainers( position ) → PositionMerges two container elements that are before and after given position. Precisely, the element after the position is removed and it's contents are moved to element before the position.
<p>foo</p>^<p>bar</p> -> <p>foo^bar</p> <div>foo</div>^<p>bar</p> -> <div>foo^bar</div>Note: Difference between mergeAttributes and mergeContainers is that
mergeAttributesmerges two attribute elements or text nodes whilemergeContainermerges two container elements.Parameters
position : PositionMerge position.
Returns
PositionPosition after merge.
-
Moves nodes from provided range to target position.
Throws CKEditorError
view-writer-invalid-range-containerwhen start and end positions are not placed inside same parent container.Parameters
Returns
-
remove( range ) → DocumentFragmentRemoves provided range from the container.
Throws CKEditorError
view-writer-invalid-range-containerwhen start and end positions are not placed inside same parent container.Parameters
range : RangeRange to remove from container. After removing, it will be updated to a collapsed range showing the new position.
Returns
DocumentFragmentDocument fragment containing removed nodes.
-
removeAttribute( key, element )Removes attribute from the element.
writer.removeAttribute( 'href', linkElement );Parameters
key : StringAttribute key.
element : Element
-
removeClass( className, element )Removes specified class from the element.
writer.removeClass( 'foo', linkElement ); writer.removeClass( [ 'foo', 'bar' ], linkElement );Parameters
className : Array.<String> | Stringelement : Element
-
removeCustomProperty( key, element ) → BooleanRemoves a custom property stored under the given key.
Parameters
key : String | Symbolelement : Element
Returns
BooleanReturns true if property was removed.
-
removeStyle( property, element )Removes specified style from the element.
writer.removeStyle( 'color', element ); // Removes 'color' style. writer.removeStyle( [ 'color', 'border-top' ], element ); // Removes both 'color' and 'border-top' styles.Parameters
property : Array.<String> | Stringelement : Element
-
rename( viewElement, newName )Renames element by creating a copy of renamed element but with changed name and then moving contents of the old element to the new one. Keep in mind that this will invalidate all positions which has renamed element as a parent.
New element has to be created because
Element#tagNameproperty in DOM is readonly.Since this function creates a new element and removes the given one, the new element is returned to keep reference.
Parameters
viewElement : ContainerElementElement to be renamed.
newName : StringNew name for element.
-
setAttribute( key, value, element )Adds or overwrite element's attribute with a specified key and value.
writer.setAttribute( 'href', 'http://ckeditor.com', linkElement );Parameters
key : StringAttribute key.
value : StringAttribute value.
element : Element
-
setCustomProperty( key, value, element )Sets a custom property on element. Unlike attributes, custom properties are not rendered to the DOM, so they can be used to add special data to elements.
Parameters
key : String | Symbolvalue : *element : Element
-
setSelection( selectable, [ placeOrOffset ], [ options ] = { [options.backward], [options.fake], [options.label] } )Sets selection's ranges and direction to the specified location based on the given document selection, selection, position, item, range, an iterable of ranges or null.
Usage:
// Sets selection to the given range. const range = new Range( start, end ); writer.setSelection( range ); // Sets backward selection to the given range. const range = new Range( start, end ); writer.setSelection( range ); // Sets selection to given ranges. const ranges = [ new Range( start1, end2 ), new Range( star2, end2 ) ]; writer.setSelection( range ); // Sets selection to the other selection. const otherSelection = new Selection(); writer.setSelection( otherSelection ); // Sets collapsed selection at the given position. const position = new Position( root, path ); writer.setSelection( position ); // Sets collapsed selection at the position of given item and offset. const paragraph = writer.createContainerElement( 'paragraph' ); writer.setSelection( paragraph, offset );Creates a range inside an element which starts before the first child of that element and ends after the last child of that element.
writer.setSelection( paragraph, 'in' );Creates a range on the item which starts before the item and ends just after the item.
writer.setSelection( paragraph, 'on' ); // Removes all ranges. writer.setSelection( null );Writer#setSelection()allow passing additional options (backward,fakeandlabel) as the last argument.// Sets selection as backward. writer.setSelection( range, { backward: true } ); // Sets selection as fake. // Fake selection does not render as browser native selection over selected elements and is hidden to the user. // This way, no native selection UI artifacts are displayed to the user and selection over elements can be // represented in other way, for example by applying proper CSS class. writer.setSelection( range, { fake: true } ); // Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM // (and be properly handled by screen readers). writer.setSelection( range, { fake: true, label: 'foo' } );Parameters
selectable : Selection | Position | Iterable.<Range> | Range | Item | null[ placeOrOffset ] : Number | 'before' | 'end' | 'after' | 'on' | 'in'Sets place or offset of the selection.
[ options ] : Object-
Properties
[ options.backward ] : BooleanSets this selection instance to be backward.
[ options.fake ] : BooleanSets this selection instance to be marked as
fake.[ options.label ] : StringLabel for the fake selection.
-
setSelectionFocus( itemOrPosition, [ offset ] )Moves selection's focus to the specified location.
-
setStyle( property, [ value ], element )Adds style to the element.
writer.setStyle( 'color', 'red', element ); writer.setStyle( { color: 'red', position: 'fixed' }, element );Parameters
property : String | ObjectProperty name or object with key - value pairs.
[ value ] : StringValue to set. This parameter is ignored if object is provided as the first parameter.
element : ElementElement to set styles on.
-
unwrap( range, attribute )Unwraps nodes within provided range from attribute element.
Throws CKEditorError
view-writer-invalid-range-containerwhen start and end positions are not placed inside same parent container.Parameters
range : Rangeattribute : AttributeElement
-
Wraps elements within range with provided AttributeElement. If a collapsed range is provided, it will be wrapped only if it is equal to view selection.
If a collapsed range was passed and is same as selection, the selection will be moved to the inside of the wrapped attribute element.
Throws
CKEditorErrorview-writer-invalid-range-containerwhenstartandendpositions are not placed inside same parent container.Throws
CKEditorErrorview-writer-wrap-invalid-attributewhen passed attribute element is not an instance of {module:engine/view/attributeelement~AttributeElement AttributeElement}.Throws
CKEditorErrorview-writer-wrap-nonselection-collapsed-rangewhen passed range is collapsed and different than view selection.Parameters
range : RangeRange to wrap.
attribute : AttributeElementAttribute element to use as wrapper.
Returns
Rangerange Range after wrapping, spanning over wrapping attribute element.
-
_addToClonedElementsGroup( element )private
Stores the information that an attribute element was added to the tree. Saves the reference to the group in the given element and updates the group, so other elements from the group now keep a reference to the given attribute element.
The clones group can be obtained using
getElementsWithSameId.Does nothing if added element has no id.
Parameters
element : AttributeElementAttribute element to save.
-
_breakAttributes( position, [ forceSplitText ] ) → Positionprivate
Helper function used by other
Writermethods. Breaks attribute elements at given position.Throws CKEditorError
view-writer-cannot-break-empty-elementwhen break position is placed inside EmptyElement.Throws CKEditorError
view-writer-cannot-break-ui-elementwhen break position is placed inside UIElement.Parameters
position : PositionPosition where to break attributes.
[ forceSplitText ] : BooleanIf set to
true, will break text nodes even if they are directly in container element. This behavior will result in incorrect view state, but is needed by other view writing methods which then fixes view state.Defaults to
false
Returns
PositionNew position after breaking the attributes.
-
_breakAttributesRange( range, [ forceSplitText ] ) → Rangeprivate
Helper function used by other
Writermethods. Breaks attribute elements at the boundaries of given range.Parameters
range : RangeRange which
startandendpositions will be used to break attributes.[ forceSplitText ] : BooleanIf set to
true, will break text nodes even if they are directly in container element. This behavior will result in incorrect view state, but is needed by other view writing methods which then fixes view state.Defaults to
false
Returns
RangeNew range with located at break positions.
-
_removeFromClonedElementsGroup( element )private
Removes all the information about the given attribute element from its clones group.
Keep in mind, that the element will still keep a reference to the group (but the group will not keep a reference to it). This allows to reference the whole group even if the element was already removed from the tree.
Does nothing if the element has no id.
Parameters
element : AttributeElementAttribute element to remove.
-
_unwrapAttributeElement( wrapper, toUnwrap ) → Booleanprivate
Unwraps AttributeElement from another by removing corresponding attributes, classes and styles. All attributes, classes and styles from wrapper should be present inside element being unwrapped.
Parameters
wrapper : AttributeElementWrapper AttributeElement.
toUnwrap : AttributeElementAttributeElement to unwrap using wrapper element.
Returns
BooleanReturns
trueif elements are unwrapped.
-
_unwrapChildren( parent, startOffset, endOffset, attribute )private
-
_wrapAttributeElement( wrapper, toWrap ) → Booleanprivate
Wraps one AttributeElement into another by merging them if possible. When merging is possible - all attributes, styles and classes are moved from wrapper element to element being wrapped.
Parameters
wrapper : AttributeElementWrapper AttributeElement.
toWrap : AttributeElementAttributeElement to wrap using wrapper element.
Returns
BooleanReturns
trueif elements are merged.
-
_wrapChildren( parent, startOffset, endOffset, attribute )private
-
_wrapPosition( position, attribute ) → Positionprivate
Helper function for
wrap. Wraps position with provided attribute element. This method will also merge newly added attribute element with its siblings whenever possible.Throws
CKEditorErrorview-writer-wrap-invalid-attributewhen passed attribute element is not an instance of {module:engine/view/attributeelement~AttributeElement AttributeElement}.Parameters
position : Positionattribute : AttributeElement
Returns
PositionNew position after wrapping.
-
_wrapRange( range, attribute ) → Rangeprivate
Helper function for
view.writer.wrap. Wraps range with provided attribute element. This method will also merge newly added attribute element with its siblings whenever possible.Throws
CKEditorErrorview-writer-wrap-invalid-attributewhen passed attribute element is not an instance of {module:engine/view/attributeelement~AttributeElement AttributeElement}.Parameters
range : Rangeattribute : AttributeElement
Returns
RangeNew range after wrapping, spanning over wrapping attribute element.