Module

engine/model/utils/selection-post-fixer

@ckeditor/ckeditor5-engine/src/model/utils/selection-post-fixer

module

Filtering

Functions

  • injectSelectionPostFixer( model ) → void

    Injects selection post-fixer to the model.

    The role of the selection post-fixer is to ensure that the selection is in a correct place after a change() block was executed.

    The correct position means that:

    • All collapsed selection ranges are in a place where the Schema allows a $text.
    • None of the selection's non-collapsed ranges crosses a limit element boundary (a range must be rooted within one limit element).
    • Only selectable elements can be selected from the outside (e.g. [<paragraph>foo</paragraph>] is invalid). This rule applies independently to both selection ends, so this selection is correct: <paragraph>f[oo</paragraph><imageBlock></imageBlock>].

    If the position is not correct, the post-fixer will automatically correct it.

    Fixing a non-collapsed selection

    See as an example a selection that starts in a P1 element and ends inside the text of a TD element ([ and ] are range boundaries and (l) denotes an element defined as isLimit=true):

    root
     |- element P1
     |   |- "foo"                                      root
     |- element TABLE (l)                   P1         TABLE             P2
     |   |- element TR (l)                 f o[o     TR      TR         b a r
     |   |   |- element TD (l)                       TD      TD
     |   |       |- "aaa"                          a]a a    b b b
     |   |- element TR (l)
     |   |   |- element TD (l)                           ||
     |   |       |- "bbb"                                ||
     |- element P2                                       VV
     |   |- "bar"
                                                       root
                                            P1         TABLE]            P2
                                           f o[o     TR      TR         b a r
                                                     TD      TD
                                                   a a a    b b b
    

    In the example above, the TABLE, TR and TD are defined as isLimit=true in the schema. The range which is not contained within a single limit element must be expanded to select the outermost limit element. The range end is inside the text node of the TD element. As the TD element is a child of the TR and TABLE elements, where both are defined as isLimit=true in the schema, the range must be expanded to select the whole TABLE element.

    Note If the selection contains multiple ranges, the method returns a minimal set of ranges that are not intersecting after expanding them to select isLimit=true elements.

    Parameters

    model : Model

    Returns

    void
  • mergeIntersectingRanges( ranges ) → Array<Range>

    Returns a minimal non-intersecting array of ranges without duplicates.

    Parameters

    ranges : Array<Range>

    Ranges to merge.

    Returns

    Array<Range>

    Array of unique and non-intersecting ranges.

  • tryFixingRange( range, schema ) → Range | null

    Tries fixing a range if it's incorrect.

    Note: This helper is used by the selection post-fixer and to fix the beforeinput target ranges.

    Parameters

    range : Range
    schema : Schema

    Returns

    Range | null

    Returns fixed range or null if range is valid.