Class

Differ (engine/model)

@ckeditor/ckeditor5-engine/src/model/differ

class

Calculates the difference between two model states.

Receives operations that are to be applied on the model document. Marks parts of the model document tree which are changed and saves the state of these elements before the change. Then, it compares saved elements with the changed elements, after all changes are applied on the model document. Calculates the diff between saved elements and new ones and returns a change set.

Filtering

Properties

  • readonly

    isEmpty : boolean

    Informs whether there are any changes buffered in Differ.

  • private

    _cachedChanges : null | Array<DiffItem>

    For efficiency purposes, Differ stores the change set returned by the differ after getChanges call. Cache is reset each time a new operation is buffered. If the cache has not been reset, getChanges will return the cached value instead of calculating it again.

    This property stores those changes that did not take place in graveyard root.

  • private

    _cachedChangesWithGraveyard : null | Array<DiffItem>

    For efficiency purposes, Differ stores the change set returned by the differ after the getChanges call. The cache is reset each time a new operation is buffered. If the cache has not been reset, getChanges will return the cached value instead of calculating it again.

    This property stores all changes evaluated by Differ, including those that took place in the graveyard.

  • private

    _changeCount : number

    Stores the number of changes that were processed. Used to order the changes chronologically. It is important when changes are sorted.

  • private readonly

    _changedMarkers : Map<string, object>

    A map that stores all changed markers.

    The keys of the map are marker names. The values of the map are objects with the following properties:

    • oldMarkerData,
    • newMarkerData.
  • private readonly

    _changedRoots : Map<string, DiffItemRoot>

    A map that stores all roots that have been changed.

    The keys are the names of the roots while value represents the changes.

  • private readonly

    _changesInElement : Map<Element | DocumentFragment, Array<ChangeItem>>

    A map that stores changes that happened in a given element.

    The keys of the map are references to the model elements. The values of the map are arrays with changes that were done on this element.

  • private readonly

    _elementSnapshots : Map<Element | DocumentFragment, Array<DifferSnapshot>>

    A map that stores "element's children snapshots". A snapshot is representing children of a given element before the first change was applied on that element. Snapshot items are objects with two properties: name, containing the element name (or '$text' for a text node) and attributes which is a map of the node's attributes.

  • private readonly

    _markerCollection : MarkerCollection

    Reference to the model's marker collection.

  • private

    _refreshedItems : Set<Item>

    Set of model items that were marked to get refreshed in _refreshItem.

Methods

  • constructor( markerCollection )

    Creates a Differ instance.

    Parameters

    markerCollection : MarkerCollection

    Model's marker collection.

  • bufferMarkerChange( markerName, oldMarkerData, newMarkerData ) → void

    Buffers a marker change.

    Parameters

    markerName : string

    The name of the marker that changed.

    oldMarkerData : MarkerData

    Marker data before the change.

    newMarkerData : MarkerData

    Marker data after the change.

    Returns

    void
  • bufferOperation( operationToBuffer ) → void

    Buffers the given operation. An operation has to be buffered before it is executed.

    Parameters

    operationToBuffer : Operation

    An operation to buffer.

    Returns

    void
  • getChangedMarkers() → Array<object>

    Returns all markers which changed.

    Returns

    Array<object>
  • getChangedRoots() → Array<DiffItemRoot>

    Returns all roots that have changed (either were attached, or detached, or their attributes changed).

    Returns

    Array<DiffItemRoot>

    Diff between the old and the new roots state.

  • getChanges( options = { [options.includeChangesInGraveyard] } ) → Array<DiffItem>

    Calculates the diff between the old model tree state (the state before the first buffered operations since the last reset call) and the new model tree state (actual one). It should be called after all buffered operations are executed.

    The diff set is returned as an array of diff items, each describing a change done on the model. The items are sorted by the position on which the change happened. If a position is before another one, it will be on an earlier index in the diff set.

    Note: Elements inside inserted element will not have a separate diff item, only the top most element change will be reported.

    Because calculating the diff is a costly operation, the result is cached. If no new operation was buffered since the previous getChanges call, the next call will return the cached value.

    Parameters

    options : object

    Additional options.

    Properties
    [ options.includeChangesInGraveyard ] : boolean

    If set to true, also changes that happened in the graveyard root will be returned. By default, changes in the graveyard root are not returned.

    Defaults to {}

    Returns

    Array<DiffItem>

    Diff between the old and the new model tree state.

  • getMarkersToAdd() → Array<object>

    Returns all markers which should be added as a result of buffered changes.

    Returns

    Array<object>

    Markers to add. Each array item is an object containing the name and range properties.

  • getMarkersToRemove() → Array<object>

    Returns all markers that should be removed as a result of buffered changes.

    Returns

    Array<object>

    Markers to remove. Each array item is an object containing the name and range properties.

  • getRefreshedItems() → Set<Item>

    Returns a set of model items that were marked to get refreshed.

    Returns

    Set<Item>
  • hasDataChanges() → boolean

    Checks whether some of the buffered changes affect the editor data.

    Types of changes which affect the editor data:

    • model structure changes,
    • attribute changes,
    • a root is added or detached,
    • changes of markers which were defined as affectsData,
    • changes of markers' affectsData property.

    Returns

    boolean
  • reset() → void

    Resets Differ. Removes all buffered changes.

    Returns

    void
  • internal

    _bufferRootLoad( root ) → void

    Buffers all the data related to given root like it was all just added to the editor.

    Following changes are buffered:

    • root is attached,
    • all root content is inserted,
    • all root attributes are added,
    • all markers inside the root are added.

    Parameters

    root : RootElement

    Returns

    void
  • internal

    _refreshItem( item ) → void

    Marks the given item in differ to be "refreshed". It means that the item will be marked as removed and inserted in the differ changes set, so it will be effectively re-converted when the differ changes are handled by a dispatcher.

    Parameters

    item : Item

    Item to refresh.

    Returns

    void
  • private

    _bufferRootAttributeChange( rootName, key, oldValue, newValue ) → void

    Buffers a root attribute change.

    Parameters

    rootName : string
    key : string
    oldValue : unknown
    newValue : unknown

    Returns

    void
  • private

    _bufferRootStateChange( rootName, isAttached ) → void

    Buffers the root state change after the root was attached or detached

    Parameters

    rootName : string
    isAttached : boolean

    Returns

    void
  • private

    _getAttributesDiff( range, oldAttributes, newAttributes ) → Array<object>

    Returns an array of objects where each one is a single attribute change description.

    Parameters

    range : Range

    The range where the change happened.

    oldAttributes : Map<string, unknown>

    A map, map iterator or compatible object that contains attributes before the change.

    newAttributes : Map<string, unknown>

    A map, map iterator or compatible object that contains attributes after the change.

    Returns

    Array<object>

    An array containing one or more diff items.

  • private

    _getChangesForElement( element ) → Array<ChangeItem>

    Gets an array of changes that have already been saved for a given element.

    Parameters

    element : Element | DocumentFragment

    Returns

    Array<ChangeItem>
  • private

    _getInsertDiff( parent, offset, elementSnapshot ) → object

    Returns an object with a single insert change description.

    Parameters

    parent : Element | DocumentFragment

    The element in which the change happened.

    offset : number

    The offset at which change happened.

    elementSnapshot : DifferSnapshot

    The snapshot of the removed element a character.

    Returns

    object

    The diff item.

  • private

    _getRemoveDiff( parent, offset, elementSnapshot ) → object

    Returns an object with a single remove change description.

    Parameters

    parent : Element | DocumentFragment

    The element in which change happened.

    offset : number

    The offset at which change happened.

    elementSnapshot : DifferSnapshot

    The snapshot of the removed element a character.

    Returns

    object

    The diff item.

  • private

    _handleChange( inc, changes ) → void

    For a given newly saved change, compares it with a change already done on the element and modifies the incoming change and/or the old change.

    Parameters

    inc : ChangeItem

    Incoming (new) change.

    changes : Array<ChangeItem>

    An array containing all the changes done on that element.

    Returns

    void
  • private

    _isInInsertedElement( element ) → boolean

    Checks whether given element or any of its parents is an element that is buffered as an inserted element.

    Parameters

    element : Element | DocumentFragment

    Returns

    boolean
  • private

    _makeSnapshot( element ) → void

    Saves a children snapshot for a given element.

    Parameters

    element : Element | DocumentFragment

    Returns

    void
  • private

    _markAttribute( item ) → void

    Saves and handles an attribute change.

    Parameters

    item : Item

    Returns

    void
  • private

    _markChange( parent, changeItem ) → void

    Saves and handles a model change.

    Parameters

    parent : Element | DocumentFragment
    changeItem : ChangeItem

    Returns

    void
  • private

    _markInsert( parent, offset, howMany ) → void

    Saves and handles an insert change.

    Parameters

    parent : Element | DocumentFragment
    offset : number
    howMany : number

    Returns

    void
  • private

    _markRemove( parent, offset, howMany ) → void

    Saves and handles a remove change.

    Parameters

    parent : Element | DocumentFragment
    offset : number
    howMany : number

    Returns

    void
  • private

    _removeAllNestedChanges( parent, offset, howMany ) → void

    Removes deeply all buffered changes that are registered in elements from range specified by parent, offset and howMany.

    Parameters

    parent : Element | DocumentFragment
    offset : number
    howMany : number

    Returns

    void