UpcastConversionApi (engine/conversion)
@ckeditor/ckeditor5-engine/src/conversion/upcastdispatcher
A set of conversion utilities available as the third parameter of the upcast dispatcher's events.
Filtering
Properties
-
consumable : ViewConsumable
module:engine/conversion/upcastdispatcher~UpcastConversionApi#consumable
Stores information about what parts of the processed view item are still waiting to be handled. After a piece of view item was converted, an appropriate consumable value should be consumed.
-
The model's schema instance.
-
store : unknown
module:engine/conversion/upcastdispatcher~UpcastConversionApi#store
Custom data stored by converters for the conversion process. Custom properties of this object can be defined and use to pass parameters between converters.
The difference between this property and the
data
parameter of event-element is that thedata
parameters allow you to pass parameters within a single event andstore
within the whole conversion. -
The
Writer
instance used to manipulate the data during conversion.
Methods
-
convertChildren( viewElement, positionOrElement ) → object
module:engine/conversion/upcastdispatcher~UpcastConversionApi#convertChildren
Starts the conversion of all children of a given item by firing appropriate events for all the children.
Parameters
viewElement : Element | DocumentFragment
An element whose children should be converted.
positionOrElement : Element | Position
A position or an element of the conversion.
Returns
object
The conversion result:
result.modelRange
The model range containing the results of the conversion of all children of the given item. When no child was converted, the range is collapsed.result.modelCursor
The position where the conversion should be continued.
Fires
-
convertItem( viewItem, modelCursor ) → object
module:engine/conversion/upcastdispatcher~UpcastConversionApi#convertItem
Starts the conversion of a given item by firing an appropriate event.
Every fired event is passed (as the first parameter) an object with the
modelRange
property. Every event may set and/or modify that property. When all callbacks are done, the final value of themodelRange
property is returned by this method. ThemodelRange
must be a model range ornull
(as set by default).Parameters
Returns
object
The conversion result:
result.modelRange
The model range containing the result of the item conversion, created and modified by callbacks attached to the fired event, ornull
if the conversion result was incorrect.result.modelCursor
The position where the conversion should be continued.
Fires
-
getSplitParts( modelElement ) → Array<Element>
module:engine/conversion/upcastdispatcher~UpcastConversionApi#getSplitParts
Returns all the split parts of the given
element
that were created during upcasting through usingsplitToAllowedParent
. It enables you to easily track these elements and continue processing them after they are split during the conversion of their children.<paragraph>Foo<imageBlock />bar<imageBlock />baz</paragraph> -> <paragraph>Foo</paragraph><imageBlock /><paragraph>bar</paragraph><imageBlock /><paragraph>baz</paragraph>
For a reference to any of above paragraphs, the function will return all three paragraphs (the original element included), sorted in the order of their creation (the original element is the first one).
If the given
element
was not split, an array with a single element is returned.A usage example in the converter code:
const myElement = conversionApi.writer.createElement( 'myElement' ); // Children conversion may split `myElement`. conversionApi.convertChildren( data.viewItem, data.modelCursor ); const splitParts = conversionApi.getSplitParts( myElement ); const lastSplitPart = splitParts[ splitParts.length - 1 ]; // Setting `data.modelRange` basing on split parts: data.modelRange = conversionApi.writer.createRange( conversionApi.writer.createPositionBefore( myElement ), conversionApi.writer.createPositionAfter( lastSplitPart ) ); // Setting `data.modelCursor` to continue after the last split element: data.modelCursor = conversionApi.writer.createPositionAfter( lastSplitPart );
Tip: If you are unable to get a reference to the original element (for example because the code is split into multiple converters or even classes) but it has already been converted, you may want to check the first element in
data.modelRange
. This is a common situation if an attribute converter is separated from an element converter.Note: This is an advanced method. For most cases
safeInsert
andupdateConversionResult
should be used.Parameters
modelElement : Element
Returns
Array<Element>
-
keepEmptyElement( modelElement ) → void
module:engine/conversion/upcastdispatcher~UpcastConversionApi#keepEmptyElement
Mark an element that was created during splitting to not get removed on conversion end even if it is empty.
Note: This is an advanced method. For most cases you will not need to keep the split empty element.
Parameters
modelElement : Element
Returns
void
-
safeInsert( modelNode, position ) → boolean
module:engine/conversion/upcastdispatcher~UpcastConversionApi#safeInsert
Safely inserts an element to the document, checking the schema to find an allowed parent for an element that you are going to insert, starting from the given position. If the current parent does not allow to insert the element but one of the ancestors does, then splits the nodes to allowed parent.
If the schema allows to insert the node in a given position, nothing is split.
If it was not possible to find an allowed parent,
false
is returned and nothing is split.Otherwise, ancestors are split.
For instance, if
<imageBlock>
is not allowed in<paragraph>
but is allowed in$root
:<paragraph>foo[]bar</paragraph> -> safe insert for `<imageBlock>` will split -> <paragraph>foo</paragraph>[]<paragraph>bar</paragraph>
Example usage:
const myElement = conversionApi.writer.createElement( 'myElement' ); if ( !conversionApi.safeInsert( myElement, data.modelCursor ) ) { return; }
The split result is saved and
updateConversionResult
should be used to update the conversion data.Parameters
modelNode : Node
The node to insert.
position : Position
The position where an element is going to be inserted.
Returns
boolean
The split result. If it was not possible to find an allowed position,
false
is returned.
-
splitToAllowedParent( modelNode, modelCursor ) → null | object
module:engine/conversion/upcastdispatcher~UpcastConversionApi#splitToAllowedParent
Checks the schema to find an allowed parent for an element that is going to be inserted starting from the given position. If the current parent does not allow inserting an element but one of the ancestors does, the method splits nodes to allowed parent.
If the schema allows inserting the node in the given position, nothing is split and an object with that position is returned.
If it was not possible to find an allowed parent,
null
is returned and nothing is split.Otherwise, ancestors are split and an object with a position and the copy of the split element is returned.
For instance, if
<imageBlock>
is not allowed in<paragraph>
but is allowed in$root
:<paragraph>foo[]bar</paragraph> -> split for `<imageBlock>` -> <paragraph>foo</paragraph>[]<paragraph>bar</paragraph>
In the example above, the position between
<paragraph>
elements will be returned asposition
and the secondparagraph
ascursorParent
.Note: This is an advanced method. For most cases
safeInsert
andupdateConversionResult
should be used.Parameters
modelNode : Node
The node to insert.
modelCursor : Position
The position where the element is going to be inserted.
Returns
null | object
The split result. If it was not possible to find an allowed position,
null
is returned.position
The position between split elements.cursorParent
The element inside which the cursor should be placed to continue the conversion. When the element is not defined it means that there was no split.
-
updateConversionResult( modelElement, data ) → void
module:engine/conversion/upcastdispatcher~UpcastConversionApi#updateConversionResult
Updates the conversion result and sets a proper
modelRange
and the nextmodelCursor
after the conversion. Used together withsafeInsert
, it enables you to easily convert elements without worrying if the node was split during the conversion of its children.A usage example in converter code:
const myElement = conversionApi.writer.createElement( 'myElement' ); if ( !conversionApi.safeInsert( myElement, data.modelCursor ) ) { return; } // Children conversion may split `myElement`. conversionApi.convertChildren( data.viewItem, myElement ); conversionApi.updateConversionResult( myElement, data );
Parameters
modelElement : Element
data : UpcastConversionData<DocumentFragment | Item>
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.