Schema (engine/model)
@ckeditor/ckeditor5-engine/src/model/schema
The model's schema. It defines the allowed and disallowed structures of nodes as well as nodes' attributes. The schema is usually defined by the features and based on them, the editing framework and features make decisions on how to change and process the model.
The instance of schema is available in editor.model.schema
.
Read more about the schema in:
- The schema section of the Introduction to the Editing engine architecture guide.
- The Schema deep dive guide.
Filtering
Properties
-
_attributeProperties : Object.<String, String>
module:engine/model/schema~Schema#_attributeProperties
private
A dictionary containing attribute properties.
Methods
-
constructor()
module:engine/model/schema~Schema#constructor
Creates a schema instance.
-
addAttributeCheck( callback )
module:engine/model/schema~Schema#addAttributeCheck
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 disallowbold
on$text
withinheading1
).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.addAttributeCheck( ( 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 returntrue/false
to overridecheckAttribute()
's return value. If it does not return a boolean value, the default algorithm (or other callbacks) will definecheckAttribute()
's return value.
-
addChildCheck( callback )
module:engine/model/schema~Schema#addChildCheck
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 andSchemaCompiledItemDefinition
(child-to-check definition). The callback may returntrue/false
to overridecheckChild()
's return value. If it does not return a boolean value, the default algorithm (or other callbacks) will definecheckChild()
's return value.
-
bind( bindProperties ) → Object
module:engine/model/schema~Schema#bind
mixed
Binds observable properties to other objects implementing the
Observable
interface.Read more in the dedicated guide covering the topic of property bindings with some additional examples.
Consider two objects: a
button
and an associatedcommand
(bothObservable
).A simple property binding could be as follows:
button.bind( 'isEnabled' ).to( command, 'isEnabled' );
or even shorter:
button.bind( 'isEnabled' ).to( command );
which works in the following way:
button.isEnabled
instantly equalscommand.isEnabled
,- whenever
command.isEnabled
changes,button.isEnabled
will immediately reflect its value.
Note: To release the binding, use
unbind
.You can also "rename" the property in the binding by specifying the new name in the
to()
chain:button.bind( 'isEnabled' ).to( command, 'isWorking' );
It is possible to bind more than one property at a time to shorten the code:
button.bind( 'isEnabled', 'value' ).to( command );
which corresponds to:
button.bind( 'isEnabled' ).to( command ); button.bind( 'value' ).to( command );
The binding can include more than one observable, combining multiple data sources in a custom callback:
button.bind( 'isEnabled' ).to( command, 'isEnabled', ui, 'isVisible', ( isCommandEnabled, isUIVisible ) => isCommandEnabled && isUIVisible );
Using a custom callback allows processing the value before passing it to the target property:
button.bind( 'isEnabled' ).to( command, 'value', value => value === 'heading1' );
It is also possible to bind to the same property in an array of observables. To bind a
button
to multiple commands (alsoObservables
) so that each and every one of them must be enabled for the button to become enabled, use the following code:button.bind( 'isEnabled' ).toMany( [ commandA, commandB, commandC ], 'isEnabled', ( isAEnabled, isBEnabled, isCEnabled ) => isAEnabled && isBEnabled && isCEnabled );
Parameters
bindProperties : String
Observable properties that will be bound to other observable(s).
Returns
Object
The bind chain with the
to()
andtoMany()
methods.
-
checkAttribute( context, attributeName ) → Boolean
module:engine/model/schema~Schema#checkAttribute
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
The context in which the attribute will be checked.
attributeName : String
Returns
Boolean
Fires
-
checkAttributeInSelection( selection, attribute ) → Boolean
module:engine/model/schema~Schema#checkAttributeInSelection
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 ) → Boolean
module:engine/model/schema~Schema#checkChild
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
Note: When verifying whether the given node can be a child of the given context, the schema also verifies the entire context — from its root to its last element. Therefore, it is possible for
checkChild()
to returnfalse
even though the context's last element can contain the checked child. It happens if one of the context's elements does not allow its child.Parameters
context : SchemaContextDefinition
The context in which the child will be checked.
def : Node | String
The child to check.
Returns
Boolean
Fires
-
checkMerge( positionOrBaseElement, elementToMerge ) → Boolean
module:engine/model/schema~Schema#checkMerge
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 thepositionOrBaseElement
.This check ensures that elements merged with
Writer#merge()
will be valid.Instead of elements, you can pass the instance of the
Position
class as thepositionOrBaseElement
. 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 an element.
Returns
Boolean
-
createContext( context ) → SchemaContext
module:engine/model/schema~Schema#createContext
Creates an instance of the schema context.
-
decorate( methodName )
module:engine/model/schema~Schema#decorate
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.
Read more in the dedicated guide covering the topic of decorating methods with some additional examples.
Decorating the method does not change its behavior (it only adds an event), but it allows to modify it later on by listening to the method's event.
For example, to cancel the method execution the event can be stopped:
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: The high priority listener has been used to execute this particular callback before the one which calls the original method (which uses the "normal" priority).
It is also possible to change the returned value:
foo.on( 'method', ( evt ) => { evt.return = 'Foo!'; } ); foo.method(); // -> 'Foo'
Finally, it is possible to access and modify the arguments the method is called with:
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
module:engine/model/schema~Schema#delegate
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
andemitterC
along withdata
:emitterA.fire( 'eventX', data );
and
eventY
is delegated (fired by)emitterC
along withdata
:emitterA.fire( 'eventY', data );
Parameters
events : String
Event names that will be delegated to another emitter.
Returns
-
extend( itemName, definition )
module:engine/model/schema~Schema#extend
Extends a registered item's definition.
Extending properties such as
allowIn
will add more items to the existing properties, while redefining properties such asisBlock
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( position, node ) → Element | null
module:engine/model/schema~Schema#findAllowedParent
Tries to find position ancestors that allow to insert a given node. It starts searching from the given position and goes node by node to the top of the model tree as long as a limit element, an object element or a topmost ancestor is not reached.
-
fire( eventOrInfo, [ args ] ) → *
module:engine/model/schema~Schema#fire
mixed
Fires an event, executing all callbacks registered for it.
The first parameter passed to callbacks is an
EventInfo
object, followed by the optionalargs
provided in thefire()
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 theevt.return
's property (the event info is the first param of every callback).
-
getAttributeProperties( attributeName ) → AttributeProperties
module:engine/model/schema~Schema#getAttributeProperties
Returns properties associated with a given model attribute. See
setAttributeProperties()
. -
getAttributesWithProperty( node, propertyName, propertyValue ) → Object
module:engine/model/schema~Schema#getAttributesWithProperty
Gets attributes of a node that have a given property.
Parameters
node : Node
Node to get attributes from.
propertyName : String
Name of the property that attribute must have to return it.
propertyValue : Boolean | Symbol | String | Number | Object | null | undefined
Desired value of the property that we want to check. When
undefined
attributes will be returned if they have set a given property no matter what the value is. If specified it will return attributes which given property's value is equal to this parameter.
Returns
Object
Object with attributes' names as key and attributes' values as value.
-
getDefinition( item ) → SchemaCompiledItemDefinition
module:engine/model/schema~Schema#getDefinition
Returns a definition of the given item or
undefined
if an item is not registered.This method should normally be used for reflection purposes (e.g. defining a clone of a certain element, checking a list of all block elements, etc). Use specific methods (such as
checkChild()
orisLimit()
) in other cases.Parameters
item : Item | SchemaContextItem | String
Returns
-
getDefinitions() → Object.<String, SchemaCompiledItemDefinition>
module:engine/model/schema~Schema#getDefinitions
Returns data of all registered items.
This method should normally be used for reflection purposes (e.g. defining a clone of a certain element, checking a list of all block elements, etc). Use specific methods (such as
checkChild()
orisLimit()
) in other cases.Returns
Object.<String, SchemaCompiledItemDefinition>
-
getLimitElement( selectionOrRangeOrPosition ) → Element
module:engine/model/schema~Schema#getLimitElement
Returns the lowest limit element containing the entire selection/range/position or the root otherwise.
Parameters
selectionOrRangeOrPosition : Selection | DocumentSelection | Range | Position
The selection/range/position to check.
Returns
Element
The lowest limit element containing the entire
selectionOrRangeOrPosition
.
-
getNearestSelectionRange( position, [ direction ] ) → Range | null
module:engine/model/schema~Schema#getNearestSelectionRange
Basing on given
position
, finds and returns a range which is nearest to thatposition
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 ) → Iterable.<Range>
module:engine/model/schema~Schema#getValidRanges
Transforms the given set of ranges into a set of ranges where the given attribute is allowed (and can be applied).
-
isBlock( item ) → Boolean
module:engine/model/schema~Schema#isBlock
Returns
true
if the given item is defined to be a block by theSchemaItemDefinition
'sisBlock
property.schema.isBlock( 'paragraph' ); // -> true schema.isBlock( '$root' ); // -> false const paragraphElement = writer.createElement( 'paragraph' ); schema.isBlock( paragraphElement ); // -> true
See the Block elements section of the Schema deep dive guide for more details.
Parameters
item : Item | SchemaContextItem | String
Returns
Boolean
-
isContent( item ) → Boolean
module:engine/model/schema~Schema#isContent
Returns
true
if the given item is defined to be a content by theSchemaItemDefinition
'sisContent
property.schema.isContent( 'paragraph' ); // -> false schema.isContent( 'heading1' ); // -> false schema.isContent( 'imageBlock' ); // -> true schema.isContent( 'horizontalLine' ); // -> true const text = writer.createText( 'foo' ); schema.isContent( text ); // -> true
See the Content elements section of the Schema deep dive guide for more details.
Parameters
item : Item | SchemaContextItem | String
Returns
Boolean
-
isInline( item ) → Boolean
module:engine/model/schema~Schema#isInline
Returns
true
if the given item is defined to be an inline element by theSchemaItemDefinition
'sisInline
property.schema.isInline( 'paragraph' ); // -> false schema.isInline( 'softBreak' ); // -> true const text = writer.createText( 'foo' ); schema.isInline( text ); // -> true
See the Inline elements section of the Schema deep dive guide for more details.
Parameters
item : Item | SchemaContextItem | String
Returns
Boolean
-
isLimit( item ) → Boolean
module:engine/model/schema~Schema#isLimit
Returns
true
if the given item should be treated as a limit element.It considers an item to be a limit element if its
SchemaItemDefinition
'sisLimit
orisObject
property was set totrue
.schema.isLimit( 'paragraph' ); // -> false schema.isLimit( '$root' ); // -> true schema.isLimit( editor.model.document.getRoot() ); // -> true schema.isLimit( 'imageBlock' ); // -> true
See the Limit elements section of the Schema deep dive guide for more details.
Parameters
item : Item | SchemaContextItem | String
Returns
Boolean
-
isObject( item ) → Boolean
module:engine/model/schema~Schema#isObject
Returns
true
if the given item should be treated as an object element.It considers an item to be an object element if its
SchemaItemDefinition
'sisObject
property was set totrue
.schema.isObject( 'paragraph' ); // -> false schema.isObject( 'imageBlock' ); // -> true const imageElement = writer.createElement( 'imageBlock' ); schema.isObject( imageElement ); // -> true
See the Object elements section of the Schema deep dive guide for more details.
Parameters
item : Item | SchemaContextItem | String
Returns
Boolean
-
isRegistered( item ) → Boolean
module:engine/model/schema~Schema#isRegistered
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
Returns
Boolean
-
isSelectable( item ) → Boolean
module:engine/model/schema~Schema#isSelectable
Returns
true
if the given item is defined to be a selectable element by theSchemaItemDefinition
'sisSelectable
property.schema.isSelectable( 'paragraph' ); // -> false schema.isSelectable( 'heading1' ); // -> false schema.isSelectable( 'imageBlock' ); // -> true schema.isSelectable( 'tableCell' ); // -> true const text = writer.createText( 'foo' ); schema.isSelectable( text ); // -> false
See the Selectable elements section of the Schema deep dive guide for more details.
Parameters
item : Item | SchemaContextItem | String
Returns
Boolean
-
listenTo( emitter, event, callback, [ options ] = { [options.priority] } )
module:engine/model/schema~Schema#listenTo
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 )
module:engine/model/schema~Schema#off
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] } )
module:engine/model/schema~Schema#on
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] } )
module:engine/model/schema~Schema#once
mixed
Registers a callback function to be executed on the next time the event is fired only. This is similar to calling
on
followed byoff
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 )
module:engine/model/schema~Schema#register
Registers a schema item. Can only be called once for every item name.
schema.register( 'paragraph', { inheritAllFrom: '$block' } );
Parameters
itemName : String
definition : SchemaItemDefinition
-
removeDisallowedAttributes( nodes, writer )
module:engine/model/schema~Schema#removeDisallowedAttributes
Removes attributes disallowed by the schema.
-
set( name, [ value ] )
module:engine/model/schema~Schema#set
mixed
Creates and sets the value of an observable property of this object. Such a property becomes a part of the state and is 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 has a property with the given property name. This prevents from mistakenly overriding existing properties and methods, but means thatfoo.set( 'bar', 1 )
may be slightly slower thanfoo.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).
-
setAllowedAttributes( node, attributes, writer )
module:engine/model/schema~Schema#setAllowedAttributes
Sets attributes allowed by the schema on a given node.
-
setAttributeProperties( attributeName, properties )
module:engine/model/schema~Schema#setAttributeProperties
This method allows assigning additional metadata to the model attributes. For example,
AttributeProperties#isFormatting
property is used to mark formatting attributes (likebold
oritalic
).// Mark bold as a formatting attribute. schema.setAttributeProperties( 'bold', { isFormatting: true } ); // Override code not to be considered a formatting markup. schema.setAttributeProperties( 'code', { isFormatting: false } );
Properties are not limited to members defined in the
AttributeProperties
type and you can also use custom properties:schema.setAttributeProperties( 'blockQuote', { customProperty: 'value' } );
Subsequent calls with the same attribute will extend its custom properties:
schema.setAttributeProperties( 'blockQuote', { one: 1 } ); schema.setAttributeProperties( 'blockQuote', { two: 2 } ); console.log( schema.getAttributeProperties( 'blockQuote' ) ); // Logs: { one: 1, two: 2 }
Parameters
attributeName : String
A name of the attribute to receive the properties.
properties : AttributeProperties
A dictionary of properties.
-
stopDelegating( [ event ], [ emitter ] )
module:engine/model/schema~Schema#stopDelegating
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 ofevent
to all emitters.
-
stopListening( [ emitter ], [ event ], [ callback ] )
module:engine/model/schema~Schema#stopListening
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 fromemitter
.[ callback ] : function
(Requires the
event
) The function to be removed from the call list for the givenevent
.
-
unbind( [ unbindProperties ] )
module:engine/model/schema~Schema#unbind
mixed
Removes the binding created with
bind
.// Removes the binding for the 'a' property. A.unbind( 'a' ); // Removes bindings for all properties. A.unbind();
Parameters
[ unbindProperties ] : String
Observable properties to be unbound. All the bindings will be released if no properties are provided.
-
_addEventListener( event, callback, [ options ] = { [options.priority] } )
module:engine/model/schema~Schema#_addEventListener
protected mixed
Adds callback to emitter for given event.
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
{}
-
_removeEventListener( event, callback )
module:engine/model/schema~Schema#_removeEventListener
protected mixed
Removes callback from emitter for given event.
Parameters
event : String
The name of the event.
callback : function
The function to stop being called.
-
_checkContextMatch( def, context, contextItemIndex )
module:engine/model/schema~Schema#_checkContextMatch
private
Parameters
def : SchemaCompiledItemDefinition
context : SchemaContext
contextItemIndex : Number
-
_clearCache()
module:engine/model/schema~Schema#_clearCache
private
-
_compile()
module:engine/model/schema~Schema#_compile
private
-
_getValidRangesForRange( range, attribute ) → Iterable.<Range>
module:engine/model/schema~Schema#_getValidRangesForRange
private
Takes a flat range and an attribute name. Traverses the range recursively and deeply to find and return all ranges inside the given range on which the attribute can be applied.
This is a helper function for
getValidRanges
.Parameters
range : Range
The range to process.
attribute : String
The name of the attribute to check.
Returns
Iterable.<Range>
Ranges in which the attribute is allowed.
Events
-
change:{property}( eventInfo, name, value, oldValue )
module:engine/model/schema~Schema#event:change:{property}
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 )
module:engine/model/schema~Schema#event:checkAttribute
Event fired when the
checkAttribute
method is called. It allows plugging in additional behavior, for example implementing rules which cannot be defined using the declarativeSchemaItemDefinition
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 is decorated with it. Thanks to that you can use this event in various ways, but the most important use case is overriding the standard behavior of thecheckAttribute()
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. Theargs
callback parameter contains arguments passed tocheckAttribute( context, attributeName )
. However, thecontext
parameter is already normalized to aSchemaContext
instance, so you do not have to worry about the various ways howcontext
may be passed tocheckAttribute()
.So, in order to implement a rule "disallow
bold
in a text which is in aheading1
, you can add such a listener: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' } );
Allowing attributes in specific contexts will be a far less common use case, because it is normally handled by the
allowAttributes
rule fromSchemaItemDefinition
. But if you have a complex scenario wherebold
should be allowed only in elementfoo
which must be in elementbar
, then this would be the way:schema.on( 'checkAttribute', ( evt, args ) => { const context = args[ 0 ]; const attributeName = 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 )
module:engine/model/schema~Schema#event:checkChild
Event fired when the
checkChild
method is called. It allows plugging in additional behavior, for example implementing rules which cannot be defined using the declarativeSchemaItemDefinition
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 various ways, but the most important use case is overriding standard behavior of thecheckChild()
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. Theargs
callback parameter contains arguments passed tocheckChild( context, child )
. However, thecontext
parameter is already normalized to aSchemaContext
instance andchild
to aSchemaCompiledItemDefinition
instance, so you do not have to worry about the various ways howcontext
andchild
may be passed tocheckChild()
.Note:
childDefinition
may beundefined
ifcheckChild()
was called with a non-registered element.So, in order to implement a rule "disallow
heading1
inblockQuote
", 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 is normally handled by the
allowIn
rule fromSchemaItemDefinition
. But if you have a complex scenario wherelistItem
should be allowed only in elementfoo
which must be in elementbar
, 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.
-
set:{property}( eventInfo, name, value, oldValue )
module:engine/model/schema~Schema#event:set:{property}
mixed
Fired when a property value is going to be set but is not set yet (before the
change
event is fired).You can control the final value of the property by using the event's
return
property.observable.set( 'prop', 1 ); observable.on( 'set:prop', ( evt, propertyName, newValue, oldValue ) => { console.log( `Value is going to be changed from ${ oldValue } to ${ newValue }` ); console.log( `Current property value is ${ observable[ propertyName ] }` ); // Let's override the value. evt.return = 3; } ); observable.on( 'change:prop', ( evt, propertyName, newValue, oldValue ) => { console.log( `Value has changed from ${ oldValue } to ${ newValue }` ); } ); observable.prop = 2; // -> 'Value is going to be changed from 1 to 2' // -> 'Current property value is 1' // -> 'Value has changed from 1 to 3'
Note: The event is fired even when the new value is the same as the old value.
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.
Every day, we work hard to keep our documentation complete. Have you spotted an outdated information? Is something missing? Please report it via our issue tracker.