Interface

UpcastConversionApi (engine/conversion)

@ckeditor/ckeditor5-engine/src/conversion/upcastdispatcher

interface

A set of conversion utilities available as the third parameter of the upcast dispatcher's events.

Filtering

Properties

  • consumable : ViewConsumable

    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.

  • schema : Schema

    The model's schema instance.

  • store : Object

    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 the data parameters allow you to pass parameters within a single event and store within the whole conversion.

  • writer : Writer

    The Writer instance used to manipulate the data during conversion.

Methods

  • convertChildren( viewItem, positionOrElement ) → Object

    Starts the conversion of all children of a given item by firing appropriate events for all the children.

    Parameters

    viewItem : Item

    An element whose children should be converted.

    positionOrElement : Position | Element

    A position or an element of the conversion.

    Returns

    Object

    result The conversion result.

    Range

    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.

    Position

    result.modelCursor The position where the conversion should be continued.

    Fires

  • convertItem( viewItem, modelCursor ) → Object

    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 the modelRange property is returned by this method. The modelRange must be a model range or null (as set by default).

    Parameters

    viewItem : Item

    Item to convert.

    modelCursor : Position

    The conversion position.

    Returns

    Object

    result The conversion result.

    Range | null

    result.modelRange The model range containing the result of the item conversion, created and modified by callbacks attached to the fired event, or null if the conversion result was incorrect.

    Position

    result.modelCursor The position where the conversion should be continued.

    Fires

  • getSplitParts( element ) → Array.<Element>

    Returns all the split parts of the given element that were created during upcasting through using splitToAllowedParent. 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 and updateConversionResult should be used.

    Parameters

    element : Element

    Returns

    Array.<Element>
  • safeInsert( node, position ) → Boolean

    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

    node : 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( position, node ) → Object | null

    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 as position and the second paragraph as cursorParent.

    Note: This is an advanced method. For most cases safeInsert and updateConversionResult should be used.

    Parameters

    position : Position

    The position where the element is going to be inserted.

    node : Node

    The node to insert.

    Returns

    Object | null

    The split result. If it was not possible to find an allowed position, null is returned.

    Position

    The position between split elements.

    Element

    [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( element, data, conversionApi )

    Updates the conversion result and sets a proper modelRange and the next modelCursor after the conversion. Used together with safeInsert, 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

    element : Element
    data : UpcastConversionData

    Conversion data.

    conversionApi : UpcastConversionApi

    Conversion utilities to be used by the callback.