UpcastWriter (engine/view)
@ckeditor/ckeditor5-engine/src/view/upcastwriter
View upcast writer. It provides a set of methods used to manipulate non-semantic view trees.
It should be used only while working on a non-semantic view (e.g. a view created from HTML string on paste). To manipulate a view which was or is being downcasted from the the model use the downcast writer.
Read more about changing the view in the Changing the view section of the Editing engine architecture guide.
Unlike DowncastWriter, which is available in the View#change() block,
UpcastWriter can be created wherever you need it:
const writer = new UpcastWriter( viewDocument );
const text = writer.createText( 'foo!' );
writer.appendChild( text, someViewElement );
Filtering
Properties
-
The view document instance in which this upcast writer operates.
Methods
-
constructor( document )module:engine/view/upcastwriter~UpcastWriter#constructorParameters
document : DocumentThe view document instance in which this upcast writer operates.
-
addClass( className, element ) → voidmodule:engine/view/upcastwriter~UpcastWriter#addClassAdds specified class to the element.
writer.addClass( 'foo', linkElement ); writer.addClass( [ 'foo', 'bar' ], linkElement );Parameters
className : string | Array<string>Single class name or array of class names which will be added.
element : ElementElement for which class will be added.
Returns
void
Related:
-
appendChild( items, element ) → numbermodule:engine/view/upcastwriter~UpcastWriter#appendChildAppends a child node or a list of child nodes at the end of this node and sets the parent of these nodes to this element.
Parameters
items : string | Item | Iterable<( string | Item )>Items to be inserted.
element : Element | DocumentFragmentElement to which items will be appended.
Returns
numberNumber of appended nodes.
Related:
-
createDocumentFragment( [ children ] ) → DocumentFragmentmodule:engine/view/upcastwriter~UpcastWriter#createDocumentFragmentCreates a new
DocumentFragmentinstance.Parameters
[ children ] : Node | Iterable<Node>A list of nodes to be inserted into the created document fragment.
Returns
DocumentFragmentThe created document fragment.
-
createElement( name, [ attrs ], [ children ] ) → Elementmodule:engine/view/upcastwriter~UpcastWriter#createElementCreates a new
Elementinstance.Attributes can be passed in various formats:
upcastWriter.createElement( 'div', { class: 'editor', contentEditable: 'true' } ); // object upcastWriter.createElement( 'div', [ [ 'class', 'editor' ], [ 'contentEditable', 'true' ] ] ); // map-like iterator upcastWriter.createElement( 'div', mapOfAttributes ); // mapParameters
name : stringNode name.
[ attrs ] : ElementAttributesCollection of attributes.
[ children ] : Node | Iterable<Node>A list of nodes to be inserted into created element.
Returns
ElementCreated element.
-
createPositionAfter( item ) → Positionmodule:engine/view/upcastwriter~UpcastWriter#createPositionAfterCreates a new position after given view item.
Parameters
item : ItemView item after which the position should be located.
Returns
-
createPositionAt( itemOrPosition, [ offset ] ) → Positionmodule:engine/view/upcastwriter~UpcastWriter#createPositionAtCreates 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), - view item and
'before'or'after'(sets position before or after given view item).
This method is a shortcut to other constructors such as:
Parameters
itemOrPosition : Position | Item[ offset ] : PositionOffsetOffset or one of the flags. Used only when first parameter is a view item.
Returns
-
createPositionBefore( item ) → Positionmodule:engine/view/upcastwriter~UpcastWriter#createPositionBeforeCreates a new position before given view item.
Parameters
item : ItemView item before which the position should be located.
Returns
-
createRange( start, end ) → Rangemodule:engine/view/upcastwriter~UpcastWriter#createRange -
createRangeIn( element ) → Rangemodule:engine/view/upcastwriter~UpcastWriter#createRangeInCreates 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
-
createRangeOn( item ) → Rangemodule:engine/view/upcastwriter~UpcastWriter#createRangeOn -
createSelection( [ selectable ], [ options ] ) → Selectionmodule:engine/view/upcastwriter~UpcastWriter#createSelection:SELECTABLECreates a new
Selectioninstance.// Creates empty selection without ranges. const selection = writer.createSelection(); // Creates selection at the given range. const range = writer.createRange( start, end ); const selection = writer.createSelection( range ); // Creates selection at the given ranges const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ]; const selection = writer.createSelection( ranges ); // Creates selection from the other selection. const otherSelection = writer.createSelection(); const selection = writer.createSelection( otherSelection ); // Creates selection from the document selection. const selection = writer.createSelection( editor.editing.view.document.selection ); // Creates selection at the given position. const position = writer.createPositionFromPath( root, path ); const selection = writer.createSelection( position );Selection's constructor allow passing additional options (backward,fakeandlabel) as the last argument.// Creates backward selection. const selection = writer.createSelection( range, { backward: true } );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.
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).
// Creates fake selection with label. const selection = writer.createSelection( range, { fake: true, label: 'foo' } );See also:
createSelection( node, placeOrOffset, options ).Parameters
[ selectable ] : null | Position | Range | Selection | DocumentSelection | Iterable<Range>[ options ] : SelectionOptions
Returns
-
createSelection( selectable, placeOrOffset, [ options ] ) → Selectionmodule:engine/view/upcastwriter~UpcastWriter#createSelection:NODE_OFFSETCreates a new
Selectioninstance.// Creates collapsed selection at the position of given item and offset. const paragraph = writer.createContainerElement( 'paragraph' ); const selection = writer.createSelection( 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. const selection = writer.createSelection( paragraph, 'in' ); // Creates a range on an item which starts before the item and ends // just after the item. const selection = writer.createSelection( paragraph, 'on' );Selection's constructor allow passing additional options (backward,fakeandlabel) as the last argument.// Creates backward selection. const selection = writer.createSelection( element, 'in', { backward: true } );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.
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).
// Creates fake selection with label. const selection = writer.createSelection( element, 'in', { fake: true, label: 'foo' } );See also:
createSelection( selectable, options ).Parameters
selectable : NodeplaceOrOffset : PlaceOrOffset[ options ] : SelectionOptions
Returns
-
createText( data ) → Textmodule:engine/view/upcastwriter~UpcastWriter#createText -
insertChild( index, items, element ) → numbermodule:engine/view/upcastwriter~UpcastWriter#insertChildInserts a child node or a list of child nodes on the given index and sets the parent of these nodes to this element.
Parameters
index : numberOffset at which nodes should be inserted.
items : Item | Iterable<Item>Items to be inserted.
element : Element | DocumentFragmentElement to which items will be inserted.
Returns
numberNumber of inserted nodes.
Related:
-
removeAttribute( key, element ) → voidmodule:engine/view/upcastwriter~UpcastWriter#removeAttributeRemoves attribute from the element.
writer.removeAttribute( 'href', linkElement );Parameters
key : stringAttribute key.
element : ElementElement from which attribute will be removed.
Returns
void
Related:
-
removeChildren( index, howMany, element ) → Array<Node>module:engine/view/upcastwriter~UpcastWriter#removeChildrenRemoves the given number of child nodes starting at the given index and set the parent of these nodes to
null.Parameters
index : numberOffset from which nodes will be removed.
howMany : numberNumber of nodes to remove.
element : Element | DocumentFragmentElement which children will be removed.
Returns
Array<Node>The array containing removed nodes.
Related:
-
removeClass( className, element ) → voidmodule:engine/view/upcastwriter~UpcastWriter#removeClassRemoves specified class from the element.
writer.removeClass( 'foo', linkElement ); writer.removeClass( [ 'foo', 'bar' ], linkElement );Parameters
className : string | Array<string>Single class name or array of class names which will be removed.
element : ElementElement from which class will be removed.
Returns
void
Related:
-
removeCustomProperty( key, element ) → booleanmodule:engine/view/upcastwriter~UpcastWriter#removeCustomPropertyRemoves a custom property stored under the given key.
Parameters
key : string | symbolName/key of the custom property to be removed.
element : Element | DocumentFragmentElement from which the custom property will be removed.
Returns
booleanReturns true if property was removed.
Related:
-
removeStyle( property, element ) → voidmodule:engine/view/upcastwriter~UpcastWriter#removeStyleRemoves 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.Note: This method can work with normalized style names if a particular style processor rule is enabled. See
StylesMap#remove()for details.Parameters
property : string | Array<string>Style property name or names to be removed.
element : ElementElement from which style will be removed.
Returns
void
Related:
-
Renames element by creating a copy of a given element but with its name changed and then moving contents of the old element to the new one.
Since this function creates a new element and removes the given one, the new element is returned to keep reference.
Parameters
newName : stringNew element name.
element : ElementElement to be renamed.
Returns
null | ElementNew element or null if the old element was not replaced (happens for detached elements).
-
replace( oldElement, newElement ) → booleanmodule:engine/view/upcastwriter~UpcastWriter#replaceReplaces given element with the new one in the view structure. Will not have effect on detached elements.
Parameters
oldElement : ElementElement which will be replaced.
newElement : ElementElement which will be inserted in the place of the old element.
Returns
booleanWhether old element was successfully replaced.
-
setAttribute( key, value, element ) → voidmodule:engine/view/upcastwriter~UpcastWriter#setAttributeAdds or overwrites element's attribute with a specified key and value.
writer.setAttribute( 'href', 'http://ckeditor.com', linkElement );Parameters
key : stringAttribute key.
value : unknownAttribute value.
element : ElementElement for which attribute will be set.
Returns
void
Related:
-
setCustomProperty( key, value, element ) → voidmodule:engine/view/upcastwriter~UpcastWriter#setCustomPropertySets 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 | symbolCustom property name/key.
value : unknownCustom property value to be stored.
element : Element | DocumentFragmentElement for which custom property will be set.
Returns
void
Related:
-
setStyle( properties, element ) → voidmodule:engine/view/upcastwriter~UpcastWriter#setStyle:OBJECTAdds style to the element.
writer.setStyle( { color: 'red', position: 'fixed' }, element );Note: This method can work with normalized style names if a particular style processor rule is enabled. See
StylesMap#set()for details.Parameters
properties : Record<string, string>Object with key - value pairs.
element : ElementElement for which style will be added.
Returns
void
Related:
-
setStyle( property, value, element ) → voidmodule:engine/view/upcastwriter~UpcastWriter#setStyle:KEY_VALUEAdds style to the element.
writer.setStyle( 'color', 'red', element );Note: This method can work with normalized style names if a particular style processor rule is enabled. See
StylesMap#set()for details.Parameters
property : stringProperty name.
value : stringValue to set.
element : ElementElement for which style will be added.
Returns
void
Related:
-
unwrapElement( element ) → voidmodule:engine/view/upcastwriter~UpcastWriter#unwrapElementRemoves given element from view structure and places its children in its position. It does nothing if element has no parent.
Parameters
element : ElementElement to unwrap.
Returns
void
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.