Report an issue
Class

Schema (engine/model)

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

class

The model's schema. It defines allowed and disallowed structures of nodes as well as nodes' attributes. The schema is usually defined by features and based on them the editing framework and features make decisions how to change and process the model.

The instance of schema is available in editor.model.schema.

Schema definitions

Schema defines allowed model structures and allowed attributes separately. They are also checked separately by using the checkChild and checkAttribute methods.

Defining allowed structures

When a feature introduces a model element it should register it in the schema. Besides defining that such an element may exist in the model, the feature also needs to define where this element may be placed:

schema.register( 'myElement', {
    allowIn: '$root'
} );

This lets the schema know that <myElement> may be a child of the <$root> element. $root is one of generic nodes defined by the editing framework. By default, the editor names the main root element a <$root>, so the above definition allows <myElement> in the main editor element.

In other words, this would be correct:

<$root><myElement></myElement></$root>

While this would not be correct:

<$root><foo><myElement></myElement></foo></$root>
Generic items

There are three basic generic items: $root, $block and $text. They are defined as follows:

this.schema.register( '$root', {
    isLimit: true
} );
this.schema.register( '$block', {
    allowIn: '$root',
    isBlock: true
} );
this.schema.register( '$text', {
    allowIn: '$block'
} );

These definitions can then be reused by features to create their own definitions in a more extensible way. For example, the Paragraph feature will define its item as:

schema.register( 'paragraph', {
    inheritAllFrom: '$block'
} );

Which translates to:

schema.register( 'paragraph', {
    allowWhere: '$block',
    allowContentOf: '$block',
    allowAttributesOf: '$block',
    inheritTypesFrom: '$block'
} );

Which can be read as:

  • The <paragraph> element will be allowed in elements in which <$block> is allowed (e.g. in <$root>).
  • The <paragraph> element will allow all nodes which are allowed in <$block> (e.g. $text).
  • The <paragraph> element will allow all attributes allowed on <$block>.
  • The <paragraph> element will inherit all is* properties of <$block> (e.g. isBlock).

Thanks to the fact that <paragraph>'s definition is inherited from <$block> other features can use the <$block> type to indirectly extend <paragraph>'s definition. For example, the BlockQuote feature does this:

schema.register( 'blockQuote', {
    allowWhere: '$block',
    allowContentOf: '$root'
} );

Thanks to that, despite the fact that block quote and paragraph features know nothing about themselves, paragraphs will be allowed in block quotes and block quotes will be allowed in all places where blocks are. So if anyone will register a <section> element (with allowContentOf: '$root' rule), that <section> elements will allow block quotes too.

The side effect of such a definition inheritance is that now <blockQuote> is allowed in <blockQuote> which needs to be resolved by a callback which will disallow this specific structure.

You can read more about the format of an item definition in SchemaItemDefinition.

Defining advanced rules in checkChild()'s callbacks

The checkChild method which is the base method used to check whether some element is allowed in a given structure is a decorated method. It means that you can add listeners to implement your specific rules which are not limited by the declarative API.

Those listeners can be added either by listening directly to the event-checkChild event or by using the handy addChildCheck method.

For instance, the block quote feature defines such a listener to disallow nested <blockQuote> structures:

schema.addChildCheck( context, childDefinition ) => {
    // Note that context is automatically normalized to SchemaContext instance and
    // child to its definition (SchemaCompiledItemDefinition).

    // If checkChild() is called with a context that ends with blockQuote and blockQuote as a child
    // to check, make the checkChild() method return false.
    if ( context.endsWith( 'blockQuote' ) && childDefinition.name == 'blockQuote' ) {
        return false;
    }
} );
Defining attributes

TODO

Implementing additional constraints

Schema's capabilities were limited to simple (and atomic) checkChild and checkAttribute checks on purpose. One may imagine that schema should support defining more complex rules such as "element <x> must be always followed by <y>". While it is feasible to create an API which would enable feeding the schema with such definitions, it is unfortunately unrealistic to then expect that every editing feature will consider those rules when processing the model. It is also unrealistic to expect that it will be done automatically by the schema and the editing engine themselves.

For instance, let's get back to the "element <x> must be always followed by <y>" rule and this initial content:

<$root>
    <x>foo</x>
    <y>bar[bom</y>
    <z>bom]bar</z>
</$root>

Now, imagine that the user presses the "block quote" button. Usually it would wrap the two selected blocks (<y> and <z>) with a <blockQuote> element:

<$root>
    <x>foo</x>
    <blockQuote>
        <y>bar[bom</y>
        <z>bom]bar</z>
    </blockQuote>
</$root>

But it turns out that this creates an incorrect structure – <x> is not followed by <y> anymore.

What should happen instead? There are at least 4 possible solutions: the block quote feature should not be applicable in such a context, someone should create a new <y> right after <x>, <x> should be moved inside <blockQuote> together with <y> or vice versa.

While this is a relatively simple scenario (unlike most real-time collaboration scenarios), it turns out that it's already hard to say what should happen and who should react to fix this content.

Therefore, if your editor needs to implement such rules, you should do that through model's post-fixers fixing incorrect content or actively prevent such situations (e.g. by disabling certain features). It means that those constraints will be defined specifically for your scenario by your code which makes their implementation much easier.

So the answer for who and how should implement additional constraints is your features or your editor through CKEditor 5's rich and open API.

Filtering

Methods

  • Creates schema instance.

  • addAttributeCheck( callback )

    Allows registering a callback to the checkAttribute method calls.

    Callbacks allow you to implement rules which are not otherwise possible to achieve by using the declarative API of SchemaItemDefinition. For example, by using this method you can disallow attribute if node to which it is applied is contained within some other element (e.g. you want to disallow bold on $text within heading1).

    This method is a shorthand for using the event-checkAttribute event. For even better control, you can use that event instead.

    Example:

    // Disallow bold on $text inside heading1.
    schema.addChildCheck( ( context, attributeName ) => {
        if ( context.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
            return false;
        }
    } );

    Which translates to:

    schema.on( 'checkAttribute', ( evt, args ) => {
        const context = args[ 0 ];
        const attributeName = args[ 1 ];
    
        if ( context.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
            // Prevent next listeners from being called.
            evt.stop();
            // Set the checkAttribute()'s return value.
            evt.return = false;
        }
    }, { priority: 'high' } );

    Parameters

    callback : function

    The callback to be called. It is called with two parameters: SchemaContext (context) instance and attribute name. The callback may return true/false to override checkAttribute()'s return value. If it does not return a boolean value, the default algorithm (or other callbacks) will define checkAttribute()'s return value.

  • addChildCheck( callback )

    Allows registering a callback to the checkChild method calls.

    Callbacks allow you to implement rules which are not otherwise possible to achieve by using the declarative API of SchemaItemDefinition. For example, by using this method you can disallow elements in specific contexts.

    This method is a shorthand for using the event-checkChild event. For even better control, you can use that event instead.

    Example:

    // Disallow heading1 directly inside a blockQuote.
    schema.addChildCheck( ( context, childDefinition ) => {
        if ( context.endsWith( 'blockQuote' ) && childDefinition.name == 'heading1' ) {
            return false;
        }
    } );

    Which translates to:

    schema.on( 'checkChild', ( evt, args ) => {
        const context = args[ 0 ];
        const childDefinition = args[ 1 ];
    
        if ( context.endsWith( 'blockQuote' ) && childDefinition && childDefinition.name == 'heading1' ) {
            // Prevent next listeners from being called.
            evt.stop();
            // Set the checkChild()'s return value.
            evt.return = false;
        }
    }, { priority: 'high' } );

    Parameters

    callback : function

    The callback to be called. It is called with two parameters: SchemaContext (context) instance and SchemaCompiledItemDefinition (child-to-check definition). The callback may return true/false to override checkChild()'s return value. If it does not return a boolean value, the default algorithm (or other callbacks) will define checkChild()'s return value.

  • bind( bindProperties ) → Object

    mixed

    Binds observable properties to another objects implementing Observable interface (like Model).

    Once bound, the observable will immediately share the current state of properties of the observable it is bound to and react to the changes to these properties in the future.

    Note: To release the binding use unbind.

    Using bind().to() chain:

    A.bind( 'a' ).to( B );
    A.bind( 'a' ).to( B, 'b' );
    A.bind( 'a', 'b' ).to( B, 'c', 'd' );
    A.bind( 'a' ).to( B, 'b', C, 'd', ( b, d ) => b + d );

    It is also possible to bind to the same property in a observables collection using bind().toMany() chain:

    A.bind( 'a' ).toMany( [ B, C, D ], 'x', ( a, b, c ) => a + b + c );
    A.bind( 'a' ).toMany( [ B, C, D ], 'x', ( ...x ) => x.every( x => x ) );

    Parameters

    bindProperties : String

    Observable properties that will be bound to another observable(s).

    Returns

    Object

    The bind chain with the to() and toMany() methods.

  • checkAttribute( context, attributeName )

    Checks whether the given attribute can be applied in the given context (on the last item of the context).

    schema.checkAttribute( textNode, 'bold' ); // -> false
    
    schema.extend( '$text', {
        allowAttributes: 'bold'
    } );
    schema.checkAttribute( textNode, 'bold' ); // -> true

    Parameters

    context : SchemaContextDefinition

    Context in which the attribute will be checked.

    attributeName : String

    Fires

  • checkAttributeInSelection( selection, attribute ) → Boolean

    Checks whether the attribute is allowed in selection:

    • if the selection is not collapsed, then checks if the attribute is allowed on any of nodes in that range,
    • if the selection is collapsed, then checks if on the selection position there's a text with the specified attribute allowed.

    Parameters

    selection : Selection | DocumentSelection

    Selection which will be checked.

    attribute : String

    The name of the attribute to check.

    Returns

    Boolean
  • checkChild( context, def )

    Checks whether the given node (child) can be a child of the given context.

    schema.checkChild( model.document.getRoot(), paragraph ); // -> false
    
    schema.register( 'paragraph', {
        allowIn: '$root'
    } );
    schema.checkChild( model.document.getRoot(), paragraph ); // -> true

    Parameters

    context : SchemaContextDefinition

    Context in which the child will be checked.

    def : Node | String

    The child to check.

    Fires

  • checkMerge( positionOrBaseElement, elementToMerge ) → Boolean

    Checks whether the given element (elementToMerge) can be merged with the specified base element (positionOrBaseElement).

    In other words – whether elementToMerge's children are allowed in the positionOrBaseElement.

    This check ensures that elements merged with Writer#merge() will be valid.

    Instead of elements, you can pass the instance of Position class as the positionOrBaseElement. It means that the elements before and after the position will be checked whether they can be merged.

    Parameters

    positionOrBaseElement : Position | Element

    The position or base element to which the elementToMerge will be merged.

    elementToMerge : Element

    The element to merge. Required if positionOrBaseElement is a element.

    Returns

    Boolean
  • decorate( methodName )

    mixed

    Turns the given methods of this object into event-based ones. This means that the new method will fire an event (named after the method) and the original action will be plugged as a listener to that event.

    This is a very simplified method decoration. Itself it doesn't change the behavior of a method (expect adding the event), but it allows to modify it later on by listening to the method's event.

    For example, in order to cancel the method execution one can stop the event:

    class Foo {
        constructor() {
            this.decorate( 'method' );
        }
    
        method() {
            console.log( 'called!' );
        }
    }
    
    const foo = new Foo();
    foo.on( 'method', ( evt ) => {
        evt.stop();
    }, { priority: 'high' } );
    
    foo.method(); // Nothing is logged.

    Note: we used a high priority listener here to execute this callback before the one which calls the original method (which used the default priority).

    It's also possible to change the return value:

    foo.on( 'method', ( evt ) => {
        evt.return = 'Foo!';
    } );
    
    foo.method(); // -> 'Foo'

    Finally, it's possible to access and modify the parameters:

    method( a, b ) {
        console.log( `${ a }, ${ b }`  );
    }
    
    // ...
    
    foo.on( 'method', ( evt, args ) => {
        args[ 0 ] = 3;
    
        console.log( args[ 1 ] ); // -> 2
    }, { priority: 'high' } );
    
    foo.method( 1, 2 ); // -> '3, 2'

    Parameters

    methodName : String

    Name of the method to decorate.

  • delegate( events ) → EmitterMixinDelegateChain

    mixed

    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
  • extend( itemName, definition )

    Extends a registered item's definition.

    Extending properties such as allowIn will add more items to the existing properties, while redefining properties such as isBlock will override the previously defined ones.

    schema.register( 'foo', {
        allowIn: '$root',
        isBlock: true;
    } );
    schema.extend( 'foo', {
        allowIn: 'blockQuote',
        isBlock: false
    } );
    
    schema.getDefinition( 'foo' );
    //    {
    //        allowIn: [ '$root', 'blockQuote' ],
    //         isBlock: false
    //    }

    Parameters

    itemName : String
    definition : SchemaItemDefinition
  • findAllowedParent() → Element | null

    Tries to find position ancestors that allows to insert given node. It starts searching from the given position and goes node by node to the top of the model tree as long as limit element, object element or top-most ancestor won't be reached.

    Returns

    Element | null

    element Allowed parent or null if nothing was found.

  • fire( eventOrInfo, [ args ] ) → *

    mixed

    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).

  • Returns a definition of the given item or undefined if item is not registered.

    Parameters

    item : Item | SchemaContextItem | String

    Returns

    SchemaCompiledItemDefinition
  • Returns all registered items.

    Returns

    Object.<String, SchemaCompiledItemDefinition>
  • getLimitElement( selection ) → Element

    Returns the lowest limit element containing the entire selection or the root otherwise.

    Parameters

    selection : Selection | DocumentSelection

    Selection which returns the common ancestor.

    Returns

    Element
  • getNearestSelectionRange( position, [ direction ] ) → Range | null

    Basing on given the position, finds and returns a range which is nearest to that position and is a correct range for selection.

    The correct selection range might be collapsed when it is located in a position where the text node can be placed. Non-collapsed range is returned when selection can be placed around element marked as an "object" in the schema.

    Direction of searching for the nearest correct selection range can be specified as:

    • both - searching will be performed in both ways,
    • forward - searching will be performed only forward,
    • backward - searching will be performed only backward.

    When valid selection range cannot be found, null is returned.

    Parameters

    position : Position

    Reference position where new selection range should be looked for.

    [ direction ] : 'both' | 'forward' | 'backward'

    Search direction.

    Defaults to 'both'

    Returns

    Range | null

    Nearest selection range or null if one cannot be found.

  • getValidRanges( ranges, attribute ) → Array.<Range>

    Transforms the given set of ranges into a set of ranges where the given attribute is allowed (and can be applied).

    Parameters

    ranges : Array.<Range>

    Ranges to be validated.

    attribute : String

    The name of the attribute to check.

    Returns

    Array.<Range>

    Ranges in which the attribute is allowed.

  • isBlock( item )

    Returns true if the given item is defined to be a block by SchemaItemDefinition's isBlock property.

    schema.isBlock( 'paragraph' ); // -> true
    schema.isBlock( '$root' ); // -> false
    
    const paragraphElement = writer.createElement( 'paragraph' );
    schema.isBlock( paragraphElement ); // -> true

    Parameters

    item : Item | SchemaContextItem | String
  • isLimit( item )

    Returns true if the given item is defined to be a limit element by SchemaItemDefinition's isLimit property.

    schema.isLimit( 'paragraph' ); // -> false
    schema.isLimit( '$root' ); // -> true
    schema.isLimit( editor.model.document.getRoot() ); // -> true

    Parameters

    item : Item | SchemaContextItem | String
  • isObject( item )

    Returns true if the given item is defined to be a object element by SchemaItemDefinition's isObject property.

    schema.isObject( 'paragraph' ); // -> false
    schema.isObject( 'image' ); // -> true
    
    const imageElement = writer.createElement( 'image' );
    schema.isObject( imageElement ); // -> true

    Parameters

    item : Item | SchemaContextItem | String
  • isRegistered( item )

    Returns true if the given item is registered in the schema.

    schema.isRegistered( 'paragraph' ); // -> true
    schema.isRegistered( editor.model.document.getRoot() ); // -> true
    schema.isRegistered( 'foo' ); // -> false

    Parameters

    item : Item | SchemaContextItem | String
  • listenTo( emitter, event, callback, [ options ] = { [options.priority] } )

    mixed

    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 )

    mixed

    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] } )

    mixed

    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] } )

    mixed

    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 {}

  • register( itemName, definition )

    Registers schema item. Can only be called once for every item name.

    schema.register( 'paragraph', {
        inheritAllFrom: '$block'
    } );

    Parameters

    itemName : String
    definition : SchemaItemDefinition
  • removeDisallowedAttributes( nodes, writer )

    Removes attributes disallowed by the schema.

    Parameters

    nodes : Iterable.<Node>

    Nodes that will be filtered.

    writer : Writer
  • set( name, [ value ] )

    mixed

    Creates and sets the value of an observable property of this object. Such an property becomes a part of the state and is be observable.

    It accepts also a single object literal containing key/value pairs with properties to be set.

    This method throws the observable-set-cannot-override error if the observable instance already have a property with the given property name. This prevents from mistakenly overriding existing properties and methods, but means that foo.set( 'bar', 1 ) may be slightly slower than foo.bar = 1.

    Parameters

    name : String | Object

    The property's name or object with name=>value pairs.

    [ value ] : *

    The property's value (if name was passed in the first parameter).

  • stopDelegating( [ event ], [ emitter ] )

    mixed

    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 ] )

    mixed

    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.

  • unbind( [ unbindProperties ] )

    mixed

    Removes the binding created with bind.

    A.unbind( 'a' );
    A.unbind();

    Parameters

    [ unbindProperties ] : String

    Observable properties to be unbound. All the bindings will be released if no properties provided.

  • _checkContextMatch( def, context, contextItemIndex )

    private

    Parameters

    def : SchemaCompiledItemDefinition
    context : SchemaContext
    contextItemIndex : Number
  • _clearCache()

    private

  • _compile()

    private

Events

  • change:{property}( eventInfo, name, value, oldValue )

    mixed

    Fired when a property changed value.

    observable.set( 'prop', 1 );
    
    observable.on( 'change:prop', ( evt, propertyName, newValue, oldValue ) => {
        console.log( `${ propertyName } has changed from ${ oldValue } to ${ newValue }` );
    } )
    
    observable.prop = 2; // -> 'prop has changed from 1 to 2'

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    name : String

    The property name.

    value : *

    The new property value.

    oldValue : *

    The previous property value.

  • checkAttribute( eventInfo, args )

    Event fired when the checkAttribute method is called. It allows plugging in additional behavior – e.g. implementing rules which cannot be defined using the declarative SchemaItemDefinition interface.

    Note: The addAttributeCheck method is a more handy way to register callbacks. Internally, it registers a listener to this event but comes with a simpler API and it is the recommended choice in most of the cases.

    The checkAttribute method fires an event because it's decorated with it. Thanks to that you can use this event in a various way, but the most important use case is overriding standard behaviour of the checkAttribute() method. Let's see a typical listener template:

    schema.on( 'checkAttribute', ( evt, args ) => {
        const context = args[ 0 ];
        const attributeName = args[ 1 ];
    }, { priority: 'high' } );

    The listener is added with a high priority to be executed before the default method is really called. The args callback parameter contains arguments passed to checkAttribute( context, attributeName ). However, the context parameter is already normalized to a SchemaContext instance, so you don't have to worry about the various ways how context may be passed to checkAttribute().

    So, in order to implement a rule "disallow bold in a text which is in a heading1 you can add such a listener:

    schema.on( 'checkAttribute', ( evt, args ) => {
        const context = args[ 0 ];
        const atributeName = args[ 1 ];
    
        if ( context.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
            // Prevent next listeners from being called.
            evt.stop();
            // Set the checkAttribute()'s return value.
            evt.return = false;
        }
    }, { priority: 'high' } );

    Allowing attributes in specific contexts will be a far less common use case, because it's normally handled by allowAttributes rule from SchemaItemDefinition but if you have a complex scenario where bold should be allowed only in element foo which must be in element bar, then this would be the way:

    schema.on( 'checkAttribute', ( evt, args ) => {
        const context = args[ 0 ];
        const atributeName = args[ 1 ];
    
        if ( context.endsWith( 'bar foo $text' ) && attributeName == 'bold' ) {
            // Prevent next listeners from being called.
            evt.stop();
            // Set the checkAttribute()'s return value.
            evt.return = true;
        }
    }, { priority: 'high' } );

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    args : Array

    The checkAttribute()'s arguments.

  • checkChild( eventInfo, args )

    Event fired when the checkChild method is called. It allows plugging in additional behavior – e.g. implementing rules which cannot be defined using the declarative SchemaItemDefinition interface.

    Note: The addChildCheck method is a more handy way to register callbacks. Internally, it registers a listener to this event but comes with a simpler API and it is the recommended choice in most of the cases.

    The checkChild method fires an event because it is decorated with it. Thanks to that you can use this event in a various way, but the most important use case is overriding standard behaviour of the checkChild() method. Let's see a typical listener template:

    schema.on( 'checkChild', ( evt, args ) => {
        const context = args[ 0 ];
        const childDefinition = args[ 1 ];
    }, { priority: 'high' } );

    The listener is added with a high priority to be executed before the default method is really called. The args callback parameter contains arguments passed to checkChild( context, child ). However, the context parameter is already normalized to a SchemaContext instance and child to a SchemaCompiledItemDefinition instance, so you don't have to worry about the various ways how context and child may be passed to checkChild().

    Note: childDefinition may be undefined if checkChild() was called with a non-registered element.

    So, in order to implement a rule "disallow heading1 in blockQuote" you can add such a listener:

    schema.on( 'checkChild', ( evt, args ) => {
        const context = args[ 0 ];
        const childDefinition = args[ 1 ];
    
        if ( context.endsWith( 'blockQuote' ) && childDefinition && childDefinition.name == 'heading1' ) {
            // Prevent next listeners from being called.
            evt.stop();
            // Set the checkChild()'s return value.
            evt.return = false;
        }
    }, { priority: 'high' } );

    Allowing elements in specific contexts will be a far less common use case, because it's normally handled by allowIn rule from SchemaItemDefinition but if you have a complex scenario where listItem should be allowed only in element foo which must be in element bar, then this would be the way:

    schema.on( 'checkChild', ( evt, args ) => {
        const context = args[ 0 ];
        const childDefinition = args[ 1 ];
    
        if ( context.endsWith( 'bar foo' ) && childDefinition.name == 'listItem' ) {
            // Prevent next listeners from being called.
            evt.stop();
            // Set the checkChild()'s return value.
            evt.return = true;
        }
    }, { priority: 'high' } );

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    args : Array

    The checkChild()'s arguments.