Report an issue
Class

UpcastDispatcher (engine/conversion)

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

class

UpcastDispatcher is a central point of view conversion, which is a process of converting given view document fragment or Element into another structure. In default application, view is converted to model.

During conversion process, for all view nodes from the converted view document fragment, UpcastDispatcher fires corresponding events. Special callbacks called "converters" should listen to UpcastDispatcher for those events.

Each callback, as the second argument, is passed a special object data that has viewItem, modelCursor and modelRange properties. viewItem property contains view node or view document fragment that is converted at the moment and might be handled by the callback. modelRange property should be used to save the result of conversion and is always a Range when conversion result is correct. modelCursor property is a position on which conversion result will be inserted and is a context according to schema will be checked before the conversion. See also convert. It is also shared by reference by all callbacks listening to given event.

The third parameter passed to a callback is an instance of UpcastDispatcher which provides additional tools for converters.

Examples of providing callbacks for UpcastDispatcher:

// Converter for links (<a>).
upcastDispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
    if ( conversionApi.consumable.consume( data.viewItem, { name: true, attributes: [ 'href' ] } ) ) {
        // <a> element is inline and is represented by an attribute in the model.
        // This is why we need to convert only children.
        const { modelRange } = conversionApi.convertChildren( data.viewItem, data.modelCursor );

        for ( let item of modelRange.getItems() ) {
            if ( conversionApi.schema.checkAttribute( item, 'linkHref' ) ) {
                conversionApi.writer.setAttribute( 'linkHref', data.viewItem.getAttribute( 'href' ), item );
            }
        }
    }
} );

// Convert all elements which have no custom converter into paragraph (autoparagraphing).
 data.viewToModel.on( 'element', ( evt, data, conversionApi ) => {
  // When element is already consumed by higher priority converters then do nothing.
  if ( conversionApi.consumable.test( data.viewItem, { name: data.viewItem.name } ) ) {
          const paragraph = conversionApi.writer.createElement( 'paragraph' );

          // Find allowed parent for paragraph that we are going to insert. If current parent does not allow
          // to insert paragraph but one of the ancestors does then split nodes to allowed parent.
          const splitResult = conversionApi.splitToAllowedParent( paragraph, data.modelCursor );

          // When there is no split result it means that we can't insert paragraph in this position.
          if ( splitResult ) {
              // Insert paragraph in allowed position.
              conversionApi.writer.insert( paragraph, splitResult.position );

              // Convert children to paragraph.
              const { modelRange } = conversionApi.convertChildren(
                  data.viewItem,
                  conversionApi.writer.createPositionAt( paragraph, 0 )
              );

                // Set as conversion result, attribute converters may use this property.
              data.modelRange = conversionApi.writer.createRange(
                  conversionApi.writer.createPositionBefore( paragraph ),
                  modelRange.end
              );

              // Continue conversion inside paragraph.
              data.modelCursor = data.modelRange.end;
          }
      }
  }
  }, { priority: 'low' } );

Before each conversion process, UpcastDispatcher fires event-viewCleanup event which can be used to prepare tree view for conversion.

Filtering

Properties

  • conversionApi : ViewConversionApi

    Interface passed by dispatcher to the events callbacks.

  • _removeIfEmpty : Set.<Element>

    protected

    List of elements that will be checked after conversion process and if element in the list will be empty it will be removed from conversion result.

    After conversion process list is cleared.

  • _modelCursor : Position | null

    private

    Position in the temporary structure where the converted content is inserted. The structure reflect the context of the target position where the content will be inserted. This property is build based on the context parameter of the convert method.

Methods

  • constructor( [ conversionApi ] )

    Creates a UpcastDispatcher that operates using passed API.

    Parameters

    [ conversionApi ] : Object

    Additional properties for interface that will be passed to events fired by UpcastDispatcher.

    Related:

  • convert( viewItem, writer, [ context ] ) → DocumentFragment

    Starts the conversion process. The entry point for the conversion.

    Parameters

    viewItem : DocumentFragment | Element

    Part of the view to be converted.

    writer : Writer

    Instance of model writer.

    [ context ] : SchemaContextDefinition

    Elements will be converted according to this context.

    Defaults to ['$root']

    Returns

    DocumentFragment

    Model data that is a result of the conversion process wrapped in DocumentFragment. Converted marker elements will be set as that document fragment's static markers map.

    Fires

  • delegate( events ) → EmitterMixinDelegateChain

    Delegates selected events to another Emitter. For instance:

    emitterA.delegate( 'eventX' ).to( emitterB );
    emitterA.delegate( 'eventX', 'eventY' ).to( emitterC );

    then eventX is delegated (fired by) emitterB and emitterC along with data:

    emitterA.fire( 'eventX', data );

    and eventY is delegated (fired by) emitterC along with data:

    emitterA.fire( 'eventY', data );

    Parameters

    events : String

    Event names that will be delegated to another emitter.

    Returns

    EmitterMixinDelegateChain
  • fire( eventOrInfo, [ args ] ) → *

    Fires an event, executing all callbacks registered for it.

    The first parameter passed to callbacks is an EventInfo object, followed by the optional args provided in the fire() method call.

    Parameters

    eventOrInfo : String | EventInfo

    The name of the event or EventInfo object if event is delegated.

    [ args ] : *

    Additional arguments to be passed to the callbacks.

    Returns

    *

    By default the method returns undefined. However, the return value can be changed by listeners through modification of the evt.return's property (the event info is the first param of every callback).

  • listenTo( emitter, event, callback, [ options ] = { [options.priority] } )

    Registers a callback function to be executed when an event is fired in a specific (emitter) object.

    Events can be grouped in namespaces using :. When namespaced event is fired, it additionally fires all callbacks for that namespace.

    // myEmitter.on( ... ) is a shorthand for myEmitter.listenTo( myEmitter, ... ).
    myEmitter.on( 'myGroup', genericCallback );
    myEmitter.on( 'myGroup:myEvent', specificCallback );
    
    // genericCallback is fired.
    myEmitter.fire( 'myGroup' );
    // both genericCallback and specificCallback are fired.
    myEmitter.fire( 'myGroup:myEvent' );
    // genericCallback is fired even though there are no callbacks for "foo".
    myEmitter.fire( 'myGroup:foo' );

    An event callback can stop the event and set the return value of the fire method.

    Parameters

    emitter : Emitter

    The object that fires the event.

    event : String

    The name of the event.

    callback : function

    The function to be called on event.

    [ options ] : Object

    Additional options.

    Properties
    [ options.priority ] : PriorityString | Number

    The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.

    Defaults to 'normal'

    Defaults to {}

  • off( event, callback )

    Stops executing the callback on the given event. Shorthand for this.stopListening( this, event, callback ).

    Parameters

    event : String

    The name of the event.

    callback : function

    The function to stop being called.

  • on( event, callback, [ options ] = { [options.priority] } )

    Registers a callback function to be executed when an event is fired.

    Shorthand for this.listenTo( this, event, callback, options ) (it makes the emitter listen on itself).

    Parameters

    event : String

    The name of the event.

    callback : function

    The function to be called on event.

    [ options ] : Object

    Additional options.

    Properties
    [ options.priority ] : PriorityString | Number

    The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.

    Defaults to 'normal'

    Defaults to {}

  • once( event, callback, [ options ] = { [options.priority] } )

    Registers a callback function to be executed on the next time the event is fired only. This is similar to calling on followed by off in the callback.

    Parameters

    event : String

    The name of the event.

    callback : function

    The function to be called on event.

    [ options ] : Object

    Additional options.

    Properties
    [ options.priority ] : PriorityString | Number

    The priority of this event callback. The higher the priority value the sooner the callback will be fired. Events having the same priority are called in the order they were added.

    Defaults to 'normal'

    Defaults to {}

  • stopDelegating( [ event ], [ emitter ] )

    Stops delegating events. It can be used at different levels:

    • To stop delegating all events.
    • To stop delegating a specific event to all emitters.
    • To stop delegating a specific event to a specific emitter.

    Parameters

    [ event ] : String

    The name of the event to stop delegating. If omitted, stops it all delegations.

    [ emitter ] : Emitter

    (requires event) The object to stop delegating a particular event to. If omitted, stops delegation of event to all emitters.

  • stopListening( [ emitter ], [ event ], [ callback ] )

    Stops listening for events. It can be used at different levels:

    • To stop listening to a specific callback.
    • To stop listening to a specific event.
    • To stop listening to all events fired by a specific object.
    • To stop listening to all events fired by all objects.

    Parameters

    [ emitter ] : Emitter

    The object to stop listening to. If omitted, stops it for all objects.

    [ event ] : String

    (Requires the emitter) The name of the event to stop listening to. If omitted, stops it for all events from emitter.

    [ callback ] : function

    (Requires the event) The function to be removed from the call list for the given event.

  • _convertChildren()

    private

  • _convertItem()

    private

  • _removeEmptyElements()

    private

    Checks if _removeIfEmpty contains empty elements and remove them. We need to do it smart because there could be elements that are not empty because contains other empty elements and after removing its children they become available to remove. We need to continue iterating over split elements as long as any element will be removed.

  • _splitToAllowedParent()

    private

Events

  • documentFragment( eventInfo )

    Fired when DocumentFragment is converted.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    Related:

  • element( eventInfo, data = { data.viewItem, data.modelCursor, data.modelRange }, conversionApi )

    Fired when Element is converted.

    element is a namespace event for a class of events. Names of actually called events follow this pattern: element:<elementName> where elementName is the name of converted element. This way listeners may listen to all elements conversion or to conversion of specific elements.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    data : Object

    Conversion data. Keep in mind that this object is shared by reference between all callbacks that will be called. This means that callbacks can override values if needed, and those values will be available in other callbacks.

    Properties
    data.viewItem : Item

    Converted item.

    data.modelCursor : Position

    Position where a converter should start changes. Change this value for the next converter to tell where the conversion should continue.

    data.modelRange : Range

    The current state of conversion result. Every change to converted element should be reflected by setting or modifying this property.

    conversionApi : ViewConversionApi

    Conversion utilities to be used by callback.

  • text( eventInfo )

    Fired when Text is converted.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    Related:

  • viewCleanup( eventInfo, viewItem )

    Fired before the first conversion event, at the beginning of upcast (view to model conversion) process.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    viewItem : DocumentFragment | Element

    Part of the view to be converted.