ViewDowncastWriter
View downcast writer.
It provides a set of methods used to manipulate view nodes.
Do not create an instance of this writer manually. To modify a view structure, use the View#change() block.
The ViewDowncastWriter is designed to work with semantic views which are the views that were/are being downcasted from the model. To work with ordinary views (e.g. parsed from a pasted content) use the upcast writer.
Read more about changing the view in the Changing the view section of the Editing engine architecture guide.
Properties
document : ViewDocumentreadonlymodule:engine/view/downcastwriter~ViewDowncastWriter#documentThe view document instance in which this writer operates.
_cloneGroups : Map<string | number, Set<ViewAttributeElement>>privatereadonlymodule:engine/view/downcastwriter~ViewDowncastWriter#_cloneGroupsHolds references to the attribute groups that share the same id. The keys are
ids, the values areSets holdingViewAttributeElements._slotFactory : null | ( writer: ViewDowncastWriter, modeOrFilter: 'children' | DowncastSlotFilter ) => ViewElementprivatemodule:engine/view/downcastwriter~ViewDowncastWriter#_slotFactoryThe slot factory used by the
elementToStructuredowncast helper.
Methods
constructor( document )module:engine/view/downcastwriter~ViewDowncastWriter#constructoraddClass( className, element ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#addClassAdds specified class to the element.
writer.addClass( 'foo', linkElement ); writer.addClass( [ 'foo', 'bar' ], linkElement );Copy codeParameters
className : string | Array<string>element : ViewElement
Returns
void
breakAttributes( positionOrRange ) → ViewPosition | ViewRangemodule:engine/view/downcastwriter~ViewDowncastWriter#breakAttributesBreaks attribute elements at the provided position or at the boundaries of a provided range. It breaks attribute elements up to their first ancestor that is a container element.
In following examples
<p>is a container,<b>and<u>are attribute elements:<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>Copy codeNote: DocumentFragment is treated like a container.
Note: The difference between breakAttributes() and breakContainer() is that
breakAttributes()breaks all attribute elements that are ancestors of a givenposition, up to the first encountered container element.breakContainer()assumes that a givenpositionis directly in the container element and breaks that container element.Throws the
view-writer-invalid-range-containerCKEditorError when the start and end positions of a passed range are not placed inside same parent container.Throws the
view-writer-cannot-break-empty-elementCKEditorError when trying to break attributes inside an ViewEmptyElement.Throws the
view-writer-cannot-break-ui-elementCKEditorError when trying to break attributes inside a UIElement.Parameters
positionOrRange : ViewPosition | ViewRangeThe position where to break attribute elements.
Returns
ViewPosition | ViewRangeThe new position or range, after breaking the attribute elements.
Related:
breakContainer( position ) → ViewPositionmodule:engine/view/downcastwriter~ViewDowncastWriter#breakContainerBreaks a container view element into two, at the given position. The position has to be directly inside the container element and cannot be in the root. It does not break the conrainer view element if the position is at the beginning or at the end of its 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>^Copy codeNote: The difference between breakAttributes() and breakContainer() is that
breakAttributes()breaks all attribute elements that are ancestors of a givenposition, up to the first encountered container element.breakContainer()assumes that the givenpositionis directly in the container element and breaks that container element.Parameters
position : ViewPositionThe position where to break the element.
Returns
ViewPositionThe position between broken elements. If an element has not been broken, the returned position is placed either before or after it.
Related:
clear( range, element ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#clearRemoves matching elements from given range.
Throws CKEditorError
view-writer-invalid-range-containerwhen start and end positions are not placed inside same parent container.Parameters
range : ViewRangeRange to clear.
element : ViewElementElement to remove.
Returns
void
clearClonedElementsGroup( groupName ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#clearClonedElementsGroupCleans up memory by removing obsolete cloned elements group from the writer.
Should be used whenever all attribute elements with the same id are going to be removed from the view and the group will no longer be needed.
Cloned elements group are not removed automatically in case if the group is still needed after all its elements were removed from the view.
Keep in mind that group names are equal to the
idproperty of the attribute element.Parameters
groupName : stringName of the group to clear.
Returns
void
createAttributeElement( name, [ attributes ], options = { [options.id], [options.priority], [options.renderUnsafeAttributes] } ) → ViewAttributeElementmodule:engine/view/downcastwriter~ViewDowncastWriter#createAttributeElementCreates a new
ViewAttributeElement.writer.createAttributeElement( 'strong' ); writer.createAttributeElement( 'a', { href: 'foo.bar' } ); // 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: 'my-marker' }, { id: 'marker:my' } );Copy codeParameters
name : stringName of the element.
[ attributes ] : ViewElementAttributesElement's attributes.
options : objectElement's options.
Properties[ options.id ] : string | numberElement's id.
[ options.priority ] : numberElement's priority.
[ options.renderUnsafeAttributes ] : Array<string>A list of attribute names that should be rendered in the editing pipeline even though they would normally be filtered out by unsafe attribute detection mechanisms.
Defaults to
{}
Returns
ViewAttributeElementCreated element.
createContainerElement( name, attributes, children, [ options ] = { [options.renderUnsafeAttributes] } ) → ViewContainerElementmodule:engine/view/downcastwriter~ViewDowncastWriter#createContainerElement:WITH_CHILDRENCreates a new
ViewContainerElementwith children.// Create element with children. writer.createContainerElement( 'figure', { class: 'image' }, [ writer.createEmptyElement( 'img' ), writer.createContainerElement( 'figcaption' ) ] ); // Create element with specific options. writer.createContainerElement( 'figure', { class: 'image' }, [ writer.createEmptyElement( 'img' ), writer.createContainerElement( 'figcaption' ) ], { renderUnsafeAttributes: [ 'foo' ] } );Copy codeParameters
name : stringName of the element.
attributes : ViewElementAttributesElements attributes.
children : ViewNode | Iterable<ViewNode>A node or a list of nodes to be inserted into the created element. If no children were specified, element's
optionscan be passed in this argument.[ options ] : objectElement's options.
Properties[ options.renderUnsafeAttributes ] : Array<string>A list of attribute names that should be rendered in the editing pipeline even though they would normally be filtered out by unsafe attribute detection mechanisms.
Returns
ViewContainerElementCreated element.
createContainerElement( name, [ attributes ], [ options ] = { [options.renderUnsafeAttributes] } ) → ViewContainerElementmodule:engine/view/downcastwriter~ViewDowncastWriter#createContainerElement:WITHOUT_CHILDRENCreates a new
ViewContainerElement.writer.createContainerElement( 'p' ); // Create element with custom attributes. writer.createContainerElement( 'div', { id: 'foo-bar', 'data-baz': '123' } ); // Create element with custom styles. writer.createContainerElement( 'p', { style: 'font-weight: bold; padding-bottom: 10px' } ); // Create element with custom classes. writer.createContainerElement( 'p', { class: 'foo bar baz' } ); // Create element with specific options. writer.createContainerElement( 'span', { class: 'placeholder' }, { renderUnsafeAttributes: [ 'foo' ] } );Copy codeParameters
name : stringName of the element.
[ attributes ] : ViewElementAttributesElements attributes.
[ options ] : objectElement's options.
Properties[ options.renderUnsafeAttributes ] : Array<string>A list of attribute names that should be rendered in the editing pipeline even though they would normally be filtered out by unsafe attribute detection mechanisms.
Returns
ViewContainerElementCreated element.
createDocumentFragment( [ children ] ) → ViewDocumentFragmentmodule:engine/view/downcastwriter~ViewDowncastWriter#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.
createEditableElement( name, [ attributes ], options = { [options.renderUnsafeAttributes] } ) → ViewEditableElementmodule:engine/view/downcastwriter~ViewDowncastWriter#createEditableElementCreates a new
ViewEditableElement.writer.createEditableElement( 'div' ); writer.createEditableElement( 'div', { id: 'foo-1234' } );Copy codeNote: The editable element is to be used in the editing pipeline. Usually, together with
toWidgetEditable().Parameters
name : stringName of the element.
[ attributes ] : ViewElementAttributesElements attributes.
options : objectElement's options.
Properties[ options.renderUnsafeAttributes ] : Array<string>A list of attribute names that should be rendered in the editing pipeline even though they would normally be filtered out by unsafe attribute detection mechanisms.
Defaults to
{}
Returns
ViewEditableElementCreated element.
createEmptyElement( name, [ attributes ], options = { [options.renderUnsafeAttributes] } ) → ViewEmptyElementmodule:engine/view/downcastwriter~ViewDowncastWriter#createEmptyElementCreates a new
ViewEmptyElement.writer.createEmptyElement( 'img' ); writer.createEmptyElement( 'img', { id: 'foo-1234' } );Copy codeParameters
name : stringName of the element.
[ attributes ] : ViewElementAttributesElements attributes.
options : objectElement's options.
Properties[ options.renderUnsafeAttributes ] : Array<string>A list of attribute names that should be rendered in the editing pipeline even though they would normally be filtered out by unsafe attribute detection mechanisms.
Defaults to
{}
Returns
ViewEmptyElementCreated element.
createPositionAfter( item ) → ViewPositionmodule:engine/view/downcastwriter~ViewDowncastWriter#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/downcastwriter~ViewDowncastWriter#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 the first parameter is a view item.
Returns
createPositionBefore( item ) → ViewPositionmodule:engine/view/downcastwriter~ViewDowncastWriter#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/downcastwriter~ViewDowncastWriter#createRangeCreates a range spanning from
startposition toendposition.Note: This factory method creates its own
ViewPositioninstances basing on passed values.Parameters
start : ViewPositionStart position.
[ end ] : null | ViewPositionEnd position. If not set, range will be collapsed at
startposition.
Returns
createRangeIn( element ) → ViewRangemodule:engine/view/downcastwriter~ViewDowncastWriter#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/downcastwriter~ViewDowncastWriter#createRangeOncreateRawElement( name, [ attributes ], [ renderFunction ], options = { [options.renderUnsafeAttributes] } ) → ViewRawElementmodule:engine/view/downcastwriter~ViewDowncastWriter#createRawElementCreates a new
ViewRawElement.writer.createRawElement( 'span', { id: 'foo-1234' }, function( domElement ) { domElement.innerHTML = '<b>This is the raw content of the raw element.</b>'; } );Copy codeRaw elements work as data containers ("wrappers", "sandboxes") but their children are not managed or even recognized by the editor. This encapsulation allows integrations to maintain custom DOM structures in the editor content without, for instance, worrying about compatibility with other editor features. Raw elements are a perfect tool for integration with external frameworks and data sources.
Unlike UI elements, raw elements act like "real" editor content (similar to
ViewContainerElementorViewEmptyElement), and they are considered by the editor selection.You should not use raw elements to render the UI in the editor content. Check out
#createUIElement()instead.Parameters
name : stringThe name of the element.
[ attributes ] : ViewElementAttributesElement attributes.
[ renderFunction ] : ( domElement: HTMLElement, domConverter: ViewDomConverter ) => voidA custom render function.
options : objectElement's options.
Properties[ options.renderUnsafeAttributes ] : Array<string>A list of attribute names that should be rendered in the editing pipeline even though they would normally be filtered out by unsafe attribute detection mechanisms.
Defaults to
{}
Returns
ViewRawElementThe created element.
createSelection( [ selectable ], [ option ] ) → ViewSelectionmodule:engine/view/downcastwriter~ViewDowncastWriter#createSelection:SELECTABLECreates 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>[ option ] : ViewSelectionOptions
Returns
createSelection( selectable, placeOrOffset, [ options ] ) → ViewSelectionmodule:engine/view/downcastwriter~ViewDowncastWriter#createSelection:NODE_OFFSETCreates new
ViewSelectioninstance.// Creates collapsed selection at the position of given item and offset. const paragraph = writer.createContainerElement( 'p' ); 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
createSlot( modeOrFilter ) → ViewElementmodule:engine/view/downcastwriter~ViewDowncastWriter#createSlotCreates placeholders for child elements of the
elementToStructure()conversion helper.const viewSlot = conversionApi.writer.createSlot(); const viewPosition = conversionApi.writer.createPositionAt( viewElement, 0 ); conversionApi.writer.insert( viewPosition, viewSlot );Copy codeIt could be filtered down to a specific subset of children (only
<foo>model elements in this case):const viewSlot = conversionApi.writer.createSlot( node => node.is( 'element', 'foo' ) ); const viewPosition = conversionApi.writer.createPositionAt( viewElement, 0 ); conversionApi.writer.insert( viewPosition, viewSlot );Copy codeWhile providing a filtered slot, make sure to provide slots for all child nodes. A single node cannot be downcasted into multiple slots.
Note: You should not change the order of nodes. View elements should be in the same order as model nodes.
Parameters
modeOrFilter : 'children' | DowncastSlotFilterThe filter for child nodes.
Defaults to
'children'
Returns
ViewElementThe slot element to be placed in to the view structure while processing
elementToStructure().
createText( data ) → ViewTextmodule:engine/view/downcastwriter~ViewDowncastWriter#createTextcreateUIElement( name, [ attributes ], [ renderFunction ] ) → ViewUIElementmodule:engine/view/downcastwriter~ViewDowncastWriter#createUIElementCreates a new
ViewUIElement.writer.createUIElement( 'span' ); writer.createUIElement( 'span', { id: 'foo-1234' } );Copy codeA custom render function can be provided as the third parameter:
writer.createUIElement( 'span', null, function( domDocument ) { const domElement = this.toDomElement( domDocument ); domElement.innerHTML = '<b>this is ui element</b>'; return domElement; } );Copy codeUnlike raw elements, UI elements are by no means editor content, for instance, they are ignored by the editor selection system.
You should not use UI elements as data containers. Check out
createRawElementinstead.Parameters
name : stringThe name of the element.
[ attributes ] : ViewElementAttributesElement attributes.
[ renderFunction ] : ( this: ViewUIElement, domDocument: Document, domConverter: ViewDomConverter ) => HTMLElementA custom render function.
Returns
ViewUIElementThe created element.
module:engine/view/downcastwriter~ViewDowncastWriter#insertInserts a 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, ViewAttributeElements, ViewContainerElements, ViewEmptyElements, RawElements or UIElements.Parameters
position : ViewPositionInsertion position.
nodes : ViewNode | Iterable<ViewNode>Node or nodes to insert.
Returns
ViewRangeRange around inserted nodes.
mergeAttributes( position ) → ViewPositionmodule:engine/view/downcastwriter~ViewDowncastWriter#mergeAttributesMerges 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>Copy codeIt 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>Copy codeNote: Difference between mergeAttributes and mergeContainers is that
mergeAttributesmerges two attribute elements or text nodes whilemergeContainermerges two container elements.Parameters
position : ViewPositionMerge position.
Returns
ViewPositionPosition after merge.
Related:
mergeContainers( position ) → ViewPositionmodule:engine/view/downcastwriter~ViewDowncastWriter#mergeContainersMerges 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>Copy codeNote: Difference between mergeAttributes and mergeContainers is that
mergeAttributesmerges two attribute elements or text nodes whilemergeContainermerges two container elements.Parameters
position : ViewPositionMerge position.
Returns
ViewPositionPosition after merge.
Related:
module:engine/view/downcastwriter~ViewDowncastWriter#moveMoves 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
sourceRange : ViewRangeRange containing nodes to move.
targetPosition : ViewPositionPosition to insert.
Returns
remove( rangeOrItem ) → ViewDocumentFragmentmodule:engine/view/downcastwriter~ViewDowncastWriter#removeRemoves provided range from the container.
Throws CKEditorError
view-writer-invalid-range-containerwhen start and end positions are not placed inside same parent container.Parameters
rangeOrItem : ViewRange | ViewItemRange to remove from container or an item to remove. If range is provided, after removing, it will be updated to a collapsed range showing the new position.
Returns
ViewDocumentFragmentDocument fragment containing removed nodes.
removeAttribute( key, tokens, element ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#removeAttributeRemoves specified tokens from an attribute value (for example class names, style properties). If resulting attribute become empty, the whole attribute is removed.
writer.removeAttribute( 'class', 'foo', linkElement );Copy codeParameters
key : stringAttribute key.
tokens : ArrayOrItem<string>Tokens to partly remove from attribute value. For example class names or style property names.
element : ViewElementThe element to remove an attribute of.
Returns
void
removeAttribute( key, element ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#removeAttributeRemoves attribute from the element.
writer.removeAttribute( 'href', linkElement );Copy codeParameters
key : stringAttribute key.
element : ViewElementThe element to remove an attribute of.
Returns
void
removeClass( className, element ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#removeClassRemoves specified class from the element.
writer.removeClass( 'foo', linkElement ); writer.removeClass( [ 'foo', 'bar' ], linkElement );Copy codeParameters
className : string | Array<string>element : ViewElement
Returns
void
removeCustomProperty( key, element ) → booleanmodule:engine/view/downcastwriter~ViewDowncastWriter#removeCustomPropertyRemoves a custom property stored under the given key.
Parameters
key : string | symbolelement : ViewElement | ViewDocumentFragment
Returns
booleanReturns true if property was removed.
removeStyle( property, element ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#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>element : ViewElement
Returns
void
rename( newName, viewElement ) → ViewContainerElementmodule:engine/view/downcastwriter~ViewDowncastWriter#renameRenames 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
newName : stringNew name for element.
viewElement : ViewContainerElementElement to be renamed.
Returns
ViewContainerElementElement created due to rename.
setAttribute( key, value, overwrite, element ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#setAttributeAdds or overwrites the element's attribute with a specified key and value. Note that for tokenized attributes it allows the
resetparameter to specify if the previous attribute value should be overwritten or a new token (class name, style property) should be added.writer.setAttribute( 'href', 'http://ckeditor.com', linkElement ); writer.setAttribute( 'class', 'foo', false, element );Copy codeParameters
key : stringThe attribute key.
value : unknownThe attribute value.
overwrite : booleanWhether tokenized attribute should overwrite the attribute value or just add a token.
element : ViewElementThe element to set an attribute on.
Returns
void
setAttribute( key, value, element ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#setAttributeAdds or overwrites the element's attribute with a specified key and value.
writer.setAttribute( 'href', 'http://ckeditor.com', linkElement );Copy codeParameters
key : stringThe attribute key.
value : unknownThe attribute value.
element : ViewElementThe element to set an attribute on.
Returns
void
setCustomProperty( key, value, element ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#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 | symbolvalue : unknownelement : ViewElement | ViewDocumentFragment
Returns
void
setSelection( selectable, [ options ] ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#setSelection:SELECTABLESets selection's ranges and direction to the specified location based on the given selectable.
Usage:
// Sets selection to the given range. const range = writer.createRange( start, end ); writer.setSelection( range ); // Sets backward selection to the given range. const range = writer.createRange( start, end ); writer.setSelection( range ); // Sets selection to given ranges. const ranges = [ writer.createRange( start1, end2 ), writer.createRange( start2, end2 ) ]; writer.setSelection( range ); // Sets selection to the other selection. const otherSelection = writer.createSelection(); writer.setSelection( otherSelection ); // Sets collapsed selection at the given position. const position = writer.createPositionFromPath( root, path ); writer.setSelection( position ); // Removes all ranges. writer.setSelection( null );Copy codeViewDowncastWriter#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' } );Copy codeSee also:
setSelection( node, placeOrOffset, options ).Parameters
selectable : null | ViewPosition | ViewRange | ViewSelection | ViewDocumentSelection | Iterable<ViewRange>[ options ] : ViewSelectionOptions
Returns
void
setSelection( selectable, placeOrOffset, [ options ] ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#setSelection:NODE_OFFSETSets selection's ranges and direction to the specified location based on the given selectable.
Usage:
// Sets collapsed selection at the position of given item and offset. const paragraph = writer.createContainerElement( 'p' ); writer.setSelection( paragraph, offset );Copy codeCreates 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' );Copy codeCreates a range on the item which starts before the item and ends just after the item.
writer.setSelection( paragraph, 'on' );Copy codeViewDowncastWriter#setSelection()allow passing additional options (backward,fakeandlabel) as the last argument.// Sets selection as backward. writer.setSelection( element, 'in', { 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( element, 'in', { 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( element, 'in', { fake: true, label: 'foo' } );Copy codeSee also:
setSelection( selectable, options ).Parameters
selectable : ViewNodeplaceOrOffset : ViewPlaceOrOffset[ options ] : ViewSelectionOptions
Returns
void
setSelectionFocus( itemOrPosition, [ offset ] ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#setSelectionFocusMoves selection's focus to the specified location.
The location can be specified in the same form as view.createPositionAt() parameters.
Parameters
itemOrPosition : ViewPosition | ViewItem[ offset ] : ViewPositionOffsetOffset or one of the flags. Used only when the first parameter is a view item.
Returns
void
setStyle( property, element ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#setStyle:OBJECTAdds many styles to the element.
writer.setStyle( { color: 'red', position: 'fixed' }, element );Copy codeNote: The passed style can be normalized if a particular style processor rule is enabled. See
StylesMap#set()for details.Parameters
property : Record<string, string>Object with key - value pairs.
element : ViewElementElement to set styles on.
Returns
void
setStyle( property, value, element ) → voidmodule:engine/view/downcastwriter~ViewDowncastWriter#setStyle:KEY_VALUEAdds style to the element.
writer.setStyle( 'color', 'red', element );Copy codeNote: The passed style can be normalized if a particular style processor rule is enabled. See
StylesMap#set()for details.Parameters
property : stringProperty name.
value : stringValue to set.
element : ViewElementElement to set styles on.
Returns
void
module:engine/view/downcastwriter~ViewDowncastWriter#unwrapUnwraps 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 : ViewRangeattribute : ViewAttributeElement
Returns
module:engine/view/downcastwriter~ViewDowncastWriter#wrapWraps elements within range with provided ViewAttributeElement. 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 ViewAttributeElement.Throws
CKEditorErrorview-writer-wrap-nonselection-collapsed-rangewhen passed range is collapsed and different than view selection.Parameters
range : ViewRangeRange to wrap.
attribute : ViewAttributeElementAttribute element to use as wrapper.
Returns
ViewRangerange Range after wrapping, spanning over wrapping attribute element.
_clearSlotFactory() → voidinternalmodule:engine/view/downcastwriter~ViewDowncastWriter#_clearSlotFactory_registerSlotFactory( slotFactory ) → voidinternalmodule:engine/view/downcastwriter~ViewDowncastWriter#_registerSlotFactoryRegisters a slot factory.
Parameters
slotFactory : ( writer: ViewDowncastWriter, modeOrFilter: 'children' | DowncastSlotFilter ) => ViewElementThe slot factory.
Returns
void
_addToClonedElementsGroup( element ) → voidprivatemodule:engine/view/downcastwriter~ViewDowncastWriter#_addToClonedElementsGroupStores 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 : ViewNodeAttribute element to save.
Returns
void
_breakAttributes( position, forceSplitText ) → ViewPositionprivatemodule:engine/view/downcastwriter~ViewDowncastWriter#_breakAttributesHelper function used by other
ViewDowncastWritermethods. Breaks attribute elements at given position.Throws CKEditorError
view-writer-cannot-break-empty-elementwhen break position is placed inside ViewEmptyElement.Throws CKEditorError
view-writer-cannot-break-ui-elementwhen break position is placed inside UIElement.Parameters
position : ViewPositionPosition 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
ViewPositionNew position after breaking the attributes.
_breakAttributesRange( range, forceSplitText ) → ViewRangeprivatemodule:engine/view/downcastwriter~ViewDowncastWriter#_breakAttributesRangeHelper function used by other
ViewDowncastWritermethods. Breaks attribute elements at the boundaries of given range.Parameters
range : ViewRangeRange 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
ViewRangeNew range with located at break positions.
_insertNodes( position, nodes, breakAttributes ) → ViewRangeprivatemodule:engine/view/downcastwriter~ViewDowncastWriter#_insertNodesInserts a node or nodes at the specified position. Takes care of breaking attributes before insertion and merging them afterwards if requested by the breakAttributes param.
Parameters
position : ViewPositionInsertion position.
nodes : Iterable<ViewNode>Node or nodes to insert.
breakAttributes : booleanWhether attributes should be broken.
Returns
ViewRangeRange around inserted nodes.
_removeFromClonedElementsGroup( element ) → voidprivatemodule:engine/view/downcastwriter~ViewDowncastWriter#_removeFromClonedElementsGroupRemoves 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 : ViewNodeAttribute element to remove.
Returns
void
_unwrapChildren( parent, startOffset, endOffset, unwrapElement ) → ViewRangeprivatemodule:engine/view/downcastwriter~ViewDowncastWriter#_unwrapChildrenUnwraps children from provided
unwrapElement. Only children contained inparentelement betweenstartOffsetandendOffsetwill be unwrapped.Parameters
parent : ViewElementstartOffset : numberendOffset : numberunwrapElement : ViewAttributeElement
Returns
_wrapChildren( parent, startOffset, endOffset, wrapElement ) → ViewRangeprivatemodule:engine/view/downcastwriter~ViewDowncastWriter#_wrapChildrenWraps children with provided
wrapElement. Only children contained inparentelement betweenstartOffsetandendOffsetwill be wrapped.Parameters
parent : ViewElementstartOffset : numberendOffset : numberwrapElement : ViewAttributeElement
Returns
_wrapPosition( position, attribute ) → ViewPositionprivatemodule:engine/view/downcastwriter~ViewDowncastWriter#_wrapPositionHelper 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 ViewAttributeElement.Parameters
position : ViewPositionattribute : ViewAttributeElement
Returns
ViewPositionNew position after wrapping.
_wrapRange( range, attribute ) → ViewRangeprivatemodule:engine/view/downcastwriter~ViewDowncastWriter#_wrapRangeHelper 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 ViewAttributeElement.Parameters
range : ViewRangeattribute : ViewAttributeElement
Returns
ViewRangeNew range after wrapping, spanning over wrapping attribute element.