ViewUpcastWriter
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 ViewDowncastWriter, which is available in the View#change() block, ViewUpcastWriter can be created wherever you need it:
const writer = new ViewUpcastWriter( viewDocument );
const text = writer.createText( 'foo!' );
writer.appendChild( text, someViewElement );
Properties
document : ViewDocumentreadonlymodule:engine/view/upcastwriter~ViewUpcastWriter#documentThe view document instance in which this upcast writer operates.
Methods
constructor( document )module:engine/view/upcastwriter~ViewUpcastWriter#constructorParameters
document : ViewDocumentThe view document instance in which this upcast writer operates.
addClass( className, element ) → voidmodule:engine/view/upcastwriter~ViewUpcastWriter#addClassAdds specified class to the element.
writer.addClass( 'foo', linkElement ); writer.addClass( [ 'foo', 'bar' ], linkElement );Copy codeParameters
className : string | Array<string>Single class name or array of class names which will be added.
element : ViewElementElement for which class will be added.
Returns
void
Related:
appendChild( items, element ) → numbermodule:engine/view/upcastwriter~ViewUpcastWriter#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 | ViewItem | Iterable<( string | ViewItem )>Items to be inserted.
element : ViewElement | ViewDocumentFragmentElement to which items will be appended.
Returns
numberNumber of appended nodes.
Related:
clone( element, deep ) → ViewElementmodule:engine/view/upcastwriter~ViewUpcastWriter#cloneClones the provided element.
Parameters
element : ViewElementElement to be cloned.
deep : booleanIf set to
trueclones element and all its children recursively. When set tofalse, element will be cloned without any children.Defaults to
false
Returns
ViewElementClone of this element.
Related:
createDocumentFragment( [ children ] ) → ViewDocumentFragmentmodule:engine/view/upcastwriter~ViewUpcastWriter#createDocumentFragmentCreates a new
ViewDocumentFragmentinstance.Parameters
[ children ] : ViewNode | Iterable<ViewNode>A list of nodes to be inserted into the created document fragment.
Returns
ViewDocumentFragmentThe created document fragment.
createElement( name, [ attrs ], [ children ] ) → ViewElementmodule:engine/view/upcastwriter~ViewUpcastWriter#createElementCreates a new
ViewElementinstance.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 ); // mapCopy codeParameters
name : stringNode name.
[ attrs ] : ViewElementAttributesCollection of attributes.
[ children ] : ViewNode | Iterable<ViewNode>A list of nodes to be inserted into created element.
Returns
ViewElementCreated element.
createPositionAfter( item ) → ViewPositionmodule:engine/view/upcastwriter~ViewUpcastWriter#createPositionAfterCreates a new position after given view item.
Parameters
item : ViewItemView item after which the position should be located.
Returns
createPositionAt( itemOrPosition, [ offset ] ) → ViewPositionmodule:engine/view/upcastwriter~ViewUpcastWriter#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 : ViewPosition | ViewItem[ offset ] : ViewPositionOffsetOffset or one of the flags. Used only when first parameter is a view item.
Returns
createPositionBefore( item ) → ViewPositionmodule:engine/view/upcastwriter~ViewUpcastWriter#createPositionBeforeCreates a new position before given view item.
Parameters
item : ViewItemView item before which the position should be located.
Returns
createRange( start, end ) → ViewRangemodule:engine/view/upcastwriter~ViewUpcastWriter#createRangeCreates a range spanning from
startposition toendposition.Note: This factory method creates it's own
ViewPositioninstances basing on passed values.Parameters
start : ViewPositionStart position.
end : ViewPositionEnd position. If not set, range will be collapsed at
startposition.
Returns
createRangeIn( element ) → ViewRangemodule:engine/view/upcastwriter~ViewUpcastWriter#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 : ViewElement | ViewDocumentFragmentElement which is a parent for the range.
Returns
createRangeOn( item ) → ViewRangemodule:engine/view/upcastwriter~ViewUpcastWriter#createRangeOncreateSelection( [ selectable ], [ options ] ) → ViewSelectionmodule:engine/view/upcastwriter~ViewUpcastWriter#createSelection:SELECTABLECreates a new
ViewSelectioninstance.// 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 );Copy codeSelection's constructor allow passing additional options (backward,fakeandlabel) as the last argument.// Creates backward selection. const selection = writer.createSelection( range, { backward: true } );Copy codeFake 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' } );Copy codeSee also:
createSelection( node, placeOrOffset, options ).Parameters
[ selectable ] : null | ViewPosition | ViewRange | ViewSelection | ViewDocumentSelection | Iterable<ViewRange>[ options ] : ViewSelectionOptions
Returns
createSelection( selectable, placeOrOffset, [ options ] ) → ViewSelectionmodule:engine/view/upcastwriter~ViewUpcastWriter#createSelection:NODE_OFFSETCreates a new
ViewSelectioninstance.// 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' );Copy codeSelection's constructor allow passing additional options (backward,fakeandlabel) as the last argument.// Creates backward selection. const selection = writer.createSelection( element, 'in', { backward: true } );Copy codeFake 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' } );Copy codeSee also:
createSelection( selectable, options ).Parameters
selectable : ViewNodeplaceOrOffset : ViewPlaceOrOffset[ options ] : ViewSelectionOptions
Returns
createText( data ) → ViewTextmodule:engine/view/upcastwriter~ViewUpcastWriter#createTextinsertChild( index, items, element ) → numbermodule:engine/view/upcastwriter~ViewUpcastWriter#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 : ViewItem | Iterable<ViewItem>Items to be inserted.
element : ViewElement | ViewDocumentFragmentElement to which items will be inserted.
Returns
numberNumber of inserted nodes.
Related:
module:engine/view/upcastwriter~ViewUpcastWriter#removeremoveAttribute( key, element ) → voidmodule:engine/view/upcastwriter~ViewUpcastWriter#removeAttributeRemoves attribute from the element.
writer.removeAttribute( 'href', linkElement );Copy codeParameters
key : stringAttribute key.
element : ViewElementElement from which attribute will be removed.
Returns
void
Related:
removeChildren( index, howMany, element ) → Array<ViewNode>module:engine/view/upcastwriter~ViewUpcastWriter#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 : ViewElement | ViewDocumentFragmentElement which children will be removed.
Returns
Array<ViewNode>The array containing removed nodes.
Related:
removeClass( className, element ) → voidmodule:engine/view/upcastwriter~ViewUpcastWriter#removeClassRemoves specified class from the element.
writer.removeClass( 'foo', linkElement ); writer.removeClass( [ 'foo', 'bar' ], linkElement );Copy codeParameters
className : string | Array<string>Single class name or array of class names which will be removed.
element : ViewElementElement from which class will be removed.
Returns
void
Related:
removeCustomProperty( key, element ) → booleanmodule:engine/view/upcastwriter~ViewUpcastWriter#removeCustomPropertyRemoves a custom property stored under the given key.
Parameters
key : string | symbolName/key of the custom property to be removed.
element : ViewElement | ViewDocumentFragmentElement from which the custom property will be removed.
Returns
booleanReturns true if property was removed.
Related:
removeStyle( property, element ) → voidmodule:engine/view/upcastwriter~ViewUpcastWriter#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.Copy codeNote: 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 : ViewElementElement from which style will be removed.
Returns
void
Related:
rename( newName, element ) → null | ViewElementmodule:engine/view/upcastwriter~ViewUpcastWriter#renameRenames 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 : ViewElementElement to be renamed.
Returns
null | ViewElementNew element or null if the old element was not replaced (happens for detached elements).
replace( oldElement, newElement ) → booleanmodule:engine/view/upcastwriter~ViewUpcastWriter#replaceReplaces given element with the new one in the view structure. Will not have effect on detached elements.
Parameters
oldElement : ViewElementElement which will be replaced.
newElement : ViewElementElement 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~ViewUpcastWriter#setAttributeAdds or overwrites element's attribute with a specified key and value.
writer.setAttribute( 'href', 'http://ckeditor.com', linkElement );Copy codeParameters
key : stringAttribute key.
value : unknownAttribute value.
element : ViewElementElement for which attribute will be set.
Returns
void
Related:
setCustomProperty( key, value, element ) → voidmodule:engine/view/upcastwriter~ViewUpcastWriter#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 : ViewElement | ViewDocumentFragmentElement for which custom property will be set.
Returns
void
Related:
setStyle( properties, element ) → voidmodule:engine/view/upcastwriter~ViewUpcastWriter#setStyle:OBJECTAdds style to the element.
writer.setStyle( { color: 'red', position: 'fixed' }, element );Copy codeNote: 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 : ViewElementElement for which style will be added.
Returns
void
Related:
setStyle( property, value, element ) → voidmodule:engine/view/upcastwriter~ViewUpcastWriter#setStyle:KEY_VALUEAdds style to the element.
writer.setStyle( 'color', 'red', element );Copy codeNote: 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 : ViewElementElement for which style will be added.
Returns
void
Related:
unwrapElement( element ) → voidmodule:engine/view/upcastwriter~ViewUpcastWriter#unwrapElementRemoves given element from view structure and places its children in its position. It does nothing if element has no parent.
Parameters
element : ViewElementElement to unwrap.
Returns
void