-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
schema.js
1712 lines (1544 loc) · 61.1 KB
/
schema.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module engine/model/schema
*/
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
import Range from './range';
import Position from './position';
import Element from './element';
import Text from './text';
import TreeWalker from './treewalker';
/**
* 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 {@link module:engine/model/model~Model#schema `editor.model.schema`}.
*
* Read more about the schema in:
*
* * {@glink framework/guides/architecture/editing-engine#schema Schema} section of the
* {@glink framework/guides/architecture/editing-engine Introduction to the Editing engine architecture}.
* * {@glink framework/guides/deep-dive/schema Schema deep dive} guide.
*
* @mixes module:utils/observablemixin~ObservableMixin
*/
export default class Schema {
/**
* Creates schema instance.
*/
constructor() {
this._sourceDefinitions = {};
/**
* A dictionary containing attribute properties.
*
* @private
* @member {Object.<String,String>}
*/
this._attributeProperties = {};
this.decorate( 'checkChild' );
this.decorate( 'checkAttribute' );
this.on( 'checkAttribute', ( evt, args ) => {
args[ 0 ] = new SchemaContext( args[ 0 ] );
}, { priority: 'highest' } );
this.on( 'checkChild', ( evt, args ) => {
args[ 0 ] = new SchemaContext( args[ 0 ] );
args[ 1 ] = this.getDefinition( args[ 1 ] );
}, { priority: 'highest' } );
}
/**
* Registers schema item. Can only be called once for every item name.
*
* schema.register( 'paragraph', {
* inheritAllFrom: '$block'
* } );
*
* @param {String} itemName
* @param {module:engine/model/schema~SchemaItemDefinition} definition
*/
register( itemName, definition ) {
if ( this._sourceDefinitions[ itemName ] ) {
/**
* A single item cannot be registered twice in the schema.
*
* This situation may happen when:
*
* * Two or more plugins called {@link #register `register()`} with the same name. This will usually mean that
* there is a collision between plugins which try to use the same element in the model. Unfortunately,
* the only way to solve this is by modifying one of these plugins to use a unique model element name.
* * A single plugin was loaded twice. This happens when it is installed by npm/yarn in two versions
* and usually means one or more of the following issues:
* * a version mismatch (two of your dependencies require two different versions of this plugin),
* * incorrect imports (this plugin is somehow imported twice in a way which confuses webpack),
* * mess in `node_modules/` (`rm -rf node_modules/` may help).
*
* **Note:** Check the logged `itemName` to better understand which plugin was duplicated/conflicting.
*
* @param itemName The name of the model element that is being registered twice.
* @error schema-cannot-register-item-twice
*/
throw new CKEditorError(
'schema-cannot-register-item-twice: A single item cannot be registered twice in the schema.',
this,
{
itemName
}
);
}
this._sourceDefinitions[ itemName ] = [
Object.assign( {}, definition )
];
this._clearCache();
}
/**
* Extends a {@link #register 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
* // }
*
* @param {String} itemName
* @param {module:engine/model/schema~SchemaItemDefinition} definition
*/
extend( itemName, definition ) {
if ( !this._sourceDefinitions[ itemName ] ) {
/**
* Cannot extend an item which was not registered yet.
*
* This error happens when a plugin tries to extend the schema definition of an item which was not
* {@link #register registered} yet.
*
* @param itemName The name of the model element which is being extended.
* @error schema-cannot-extend-missing-item
*/
throw new CKEditorError( 'schema-cannot-extend-missing-item: Cannot extend an item which was not registered yet.', this, {
itemName
} );
}
this._sourceDefinitions[ itemName ].push( Object.assign( {}, definition ) );
this._clearCache();
}
/**
* 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 {@link #checkChild `checkChild()`} or {@link #isLimit `isLimit()`})
* in other cases.
*
* @returns {Object.<String,module:engine/model/schema~SchemaCompiledItemDefinition>}
*/
getDefinitions() {
if ( !this._compiledDefinitions ) {
this._compile();
}
return this._compiledDefinitions;
}
/**
* 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 {@link #checkChild `checkChild()`} or {@link #isLimit `isLimit()`})
* in other cases.
*
* @param {module:engine/model/item~Item|module:engine/model/schema~SchemaContextItem|String} item
* @returns {module:engine/model/schema~SchemaCompiledItemDefinition}
*/
getDefinition( item ) {
let itemName;
if ( typeof item == 'string' ) {
itemName = item;
} else if ( item.is && ( item.is( 'text' ) || item.is( 'textProxy' ) ) ) {
itemName = '$text';
}
// Element or module:engine/model/schema~SchemaContextItem.
else {
itemName = item.name;
}
return this.getDefinitions()[ itemName ];
}
/**
* 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
*
* @param {module:engine/model/item~Item|module:engine/model/schema~SchemaContextItem|String} item
*/
isRegistered( item ) {
return !!this.getDefinition( item );
}
/**
* Returns `true` if the given item is defined to be
* a block by the {@link module:engine/model/schema~SchemaItemDefinition}'s `isBlock` property.
*
* schema.isBlock( 'paragraph' ); // -> true
* schema.isBlock( '$root' ); // -> false
*
* const paragraphElement = writer.createElement( 'paragraph' );
* schema.isBlock( paragraphElement ); // -> true
*
* See the {@glink framework/guides/deep-dive/schema#block-elements Block elements} section of the Schema deep dive}
* guide for more details.
*
* @param {module:engine/model/item~Item|module:engine/model/schema~SchemaContextItem|String} item
*/
isBlock( item ) {
const def = this.getDefinition( item );
return !!( def && def.isBlock );
}
/**
* Returns `true` if the given item should be treated as a limit element.
*
* It considers an item to be a limit element if its
* {@link module:engine/model/schema~SchemaItemDefinition}'s
* {@link module:engine/model/schema~SchemaItemDefinition#isLimit `isLimit`} or
* {@link module:engine/model/schema~SchemaItemDefinition#isObject `isObject`} property
* was set to `true`.
*
* schema.isLimit( 'paragraph' ); // -> false
* schema.isLimit( '$root' ); // -> true
* schema.isLimit( editor.model.document.getRoot() ); // -> true
* schema.isLimit( 'image' ); // -> true
*
* See the {@glink framework/guides/deep-dive/schema#limit-elements Limit elements} section of the Schema deep dive}
* guide for more details.
*
* @param {module:engine/model/item~Item|module:engine/model/schema~SchemaContextItem|String} item
*/
isLimit( item ) {
const def = this.getDefinition( item );
if ( !def ) {
return false;
}
return !!( def.isLimit || def.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
* {@link module:engine/model/schema~SchemaItemDefinition}'s
* {@link module:engine/model/schema~SchemaItemDefinition#isObject `isObject`} property
* was set to `true`.
*
* schema.isObject( 'paragraph' ); // -> false
* schema.isObject( 'image' ); // -> true
*
* const imageElement = writer.createElement( 'image' );
* schema.isObject( imageElement ); // -> true
*
* See the {@glink framework/guides/deep-dive/schema#object-elements Object elements} section of the Schema deep dive}
* guide for more details.
*
* @param {module:engine/model/item~Item|module:engine/model/schema~SchemaContextItem|String} item
*/
isObject( item ) {
const def = this.getDefinition( item );
return !!( def && def.isObject );
}
/**
* Returns `true` if the given item is defined to be
* an inline element by the {@link module:engine/model/schema~SchemaItemDefinition}'s `isInline` property.
*
* schema.isInline( 'paragraph' ); // -> false
* schema.isInline( 'softBreak' ); // -> true
*
* const text = writer.createText('foo' );
* schema.isInline( text ); // -> true
*
* See the {@glink framework/guides/deep-dive/schema#inline-elements Inline elements} section of the Schema deep dive}
* guide for more details.
*
* @param {module:engine/model/item~Item|module:engine/model/schema~SchemaContextItem|String} item
*/
isInline( item ) {
const def = this.getDefinition( item );
return !!( def && def.isInline );
}
/**
* 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 return `false` 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.
*
* @fires checkChild
* @param {module:engine/model/schema~SchemaContextDefinition} context The context in which the child will be checked.
* @param {module:engine/model/node~Node|String} def The child to check.
*/
checkChild( context, def ) {
// Note: context and child are already normalized here to a SchemaContext and SchemaCompiledItemDefinition.
if ( !def ) {
return false;
}
return this._checkContextMatch( def, context );
}
/**
* 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
*
* @fires checkAttribute
* @param {module:engine/model/schema~SchemaContextDefinition} context The context in which the attribute will be checked.
* @param {String} attributeName
*/
checkAttribute( context, attributeName ) {
const def = this.getDefinition( context.last );
if ( !def ) {
return false;
}
return def.allowAttributes.includes( attributeName );
}
/**
* Checks whether the given element (`elementToMerge`) can be merged with the specified base element (`positionOrBaseElement`).
*
* In other words — whether `elementToMerge`'s children {@link #checkChild are allowed} in the `positionOrBaseElement`.
*
* This check ensures that elements merged with {@link module:engine/model/writer~Writer#merge `Writer#merge()`}
* will be valid.
*
* Instead of elements, you can pass the instance of the {@link module:engine/model/position~Position} class as the
* `positionOrBaseElement`. It means that the elements before and after the position will be checked whether they can be merged.
*
* @param {module:engine/model/position~Position|module:engine/model/element~Element} positionOrBaseElement The position or base
* element to which the `elementToMerge` will be merged.
* @param {module:engine/model/element~Element} elementToMerge The element to merge. Required if `positionOrBaseElement` is an element.
* @returns {Boolean}
*/
checkMerge( positionOrBaseElement, elementToMerge = null ) {
if ( positionOrBaseElement instanceof Position ) {
const nodeBefore = positionOrBaseElement.nodeBefore;
const nodeAfter = positionOrBaseElement.nodeAfter;
if ( !( nodeBefore instanceof Element ) ) {
/**
* The node before the merge position must be an element.
*
* @error schema-check-merge-no-element-before
*/
throw new CKEditorError(
'schema-check-merge-no-element-before: The node before the merge position must be an element.',
this
);
}
if ( !( nodeAfter instanceof Element ) ) {
/**
* The node after the merge position must be an element.
*
* @error schema-check-merge-no-element-after
*/
throw new CKEditorError(
'schema-check-merge-no-element-after: The node after the merge position must be an element.',
this
);
}
return this.checkMerge( nodeBefore, nodeAfter );
}
for ( const child of elementToMerge.getChildren() ) {
if ( !this.checkChild( positionOrBaseElement, child ) ) {
return false;
}
}
return true;
}
/**
* Allows registering a callback to the {@link #checkChild} method calls.
*
* Callbacks allow you to implement rules which are not otherwise possible to achieve
* by using the declarative API of {@link module:engine/model/schema~SchemaItemDefinition}.
* For example, by using this method you can disallow elements in specific contexts.
*
* This method is a shorthand for using the {@link #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' } );
*
* @param {Function} callback The callback to be called. It is called with two parameters:
* {@link module:engine/model/schema~SchemaContext} (context) instance and
* {@link module:engine/model/schema~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.
*/
addChildCheck( callback ) {
this.on( 'checkChild', ( evt, [ ctx, childDef ] ) => {
// checkChild() was called with a non-registered child.
// In 99% cases such check should return false, so not to overcomplicate all callbacks
// don't even execute them.
if ( !childDef ) {
return;
}
const retValue = callback( ctx, childDef );
if ( typeof retValue == 'boolean' ) {
evt.stop();
evt.return = retValue;
}
}, { priority: 'high' } );
}
/**
* Allows registering a callback to the {@link #checkAttribute} method calls.
*
* Callbacks allow you to implement rules which are not otherwise possible to achieve
* by using the declarative API of {@link module:engine/model/schema~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 {@link #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' } );
*
* @param {Function} callback The callback to be called. It is called with two parameters:
* {@link module:engine/model/schema~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.
*/
addAttributeCheck( callback ) {
this.on( 'checkAttribute', ( evt, [ ctx, attributeName ] ) => {
const retValue = callback( ctx, attributeName );
if ( typeof retValue == 'boolean' ) {
evt.stop();
evt.return = retValue;
}
}, { priority: 'high' } );
}
/**
* This method allows assigning additional metadata to the model attributes. For example,
* {@link module:engine/model/schema~AttributeProperties `AttributeProperties#isFormatting` property} is
* used to mark formatting attributes (like `bold` or `italic`).
*
* // 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
* {@link module:engine/model/schema~AttributeProperties `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 }
*
* @param {String} attributeName A name of the attribute to receive the properties.
* @param {module:engine/model/schema~AttributeProperties} properties A dictionary of properties.
*/
setAttributeProperties( attributeName, properties ) {
this._attributeProperties[ attributeName ] = Object.assign( this.getAttributeProperties( attributeName ), properties );
}
/**
* Returns properties associated with a given model attribute. See {@link #setAttributeProperties `setAttributeProperties()`}.
*
* @param {String} attributeName A name of the attribute.
* @returns {module:engine/model/schema~AttributeProperties}
*/
getAttributeProperties( attributeName ) {
return this._attributeProperties[ attributeName ] || {};
}
/**
* Returns the lowest {@link module:engine/model/schema~Schema#isLimit limit element} containing the entire
* selection/range/position or the root otherwise.
*
* @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection|
* module:engine/model/range~Range|module:engine/model/position~Position} selectionOrRangeOrPosition
* The selection/range/position to check.
* @returns {module:engine/model/element~Element} The lowest limit element containing
* the entire `selectionOrRangeOrPosition`.
*/
getLimitElement( selectionOrRangeOrPosition ) {
let element;
if ( selectionOrRangeOrPosition instanceof Position ) {
element = selectionOrRangeOrPosition.parent;
} else {
const ranges = selectionOrRangeOrPosition instanceof Range ?
[ selectionOrRangeOrPosition ] :
Array.from( selectionOrRangeOrPosition.getRanges() );
// Find the common ancestor for all selection's ranges.
element = ranges
.reduce( ( element, range ) => {
const rangeCommonAncestor = range.getCommonAncestor();
if ( !element ) {
return rangeCommonAncestor;
}
return element.getCommonAncestor( rangeCommonAncestor, { includeSelf: true } );
}, null );
}
while ( !this.isLimit( element ) ) {
if ( element.parent ) {
element = element.parent;
} else {
break;
}
}
return element;
}
/**
* 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.
*
* @param {module:engine/model/selection~Selection|module:engine/model/documentselection~DocumentSelection} selection
* Selection which will be checked.
* @param {String} attribute The name of the attribute to check.
* @returns {Boolean}
*/
checkAttributeInSelection( selection, attribute ) {
if ( selection.isCollapsed ) {
const firstPosition = selection.getFirstPosition();
const context = [
...firstPosition.getAncestors(),
new Text( '', selection.getAttributes() )
];
// Check whether schema allows for a text with the attribute in the selection.
return this.checkAttribute( context, attribute );
} else {
const ranges = selection.getRanges();
// For all ranges, check nodes in them until you find a node that is allowed to have the attribute.
for ( const range of ranges ) {
for ( const value of range ) {
if ( this.checkAttribute( value.item, attribute ) ) {
// If we found a node that is allowed to have the attribute, return true.
return true;
}
}
}
}
// If we haven't found such node, return false.
return false;
}
/**
* Transforms the given set of ranges into a set of ranges where the given attribute is allowed (and can be applied).
*
* @param {Array.<module:engine/model/range~Range>} ranges Ranges to be validated.
* @param {String} attribute The name of the attribute to check.
* @returns {Iterable.<module:engine/model/range~Range>} Ranges in which the attribute is allowed.
*/
* getValidRanges( ranges, attribute ) {
ranges = convertToMinimalFlatRanges( ranges );
for ( const range of ranges ) {
yield* this._getValidRangesForRange( range, attribute );
}
}
/**
* Basing on given `position`, finds and returns a {@link module:engine/model/range~Range 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 {@link module:engine/model/schema~Schema 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.
*
* @param {module:engine/model/position~Position} position Reference position where new selection range should be looked for.
* @param {'both'|'forward'|'backward'} [direction='both'] Search direction.
* @returns {module:engine/model/range~Range|null} Nearest selection range or `null` if one cannot be found.
*/
getNearestSelectionRange( position, direction = 'both' ) {
// Return collapsed range if provided position is valid.
if ( this.checkChild( position, '$text' ) ) {
return new Range( position );
}
let backwardWalker, forwardWalker;
// Never leave a limit element.
const limitElement = position.getAncestors().reverse().find( item => this.isLimit( item ) ) || position.root;
if ( direction == 'both' || direction == 'backward' ) {
backwardWalker = new TreeWalker( {
boundaries: Range._createIn( limitElement ),
startPosition: position,
direction: 'backward'
} );
}
if ( direction == 'both' || direction == 'forward' ) {
forwardWalker = new TreeWalker( {
boundaries: Range._createIn( limitElement ),
startPosition: position
} );
}
for ( const data of combineWalkers( backwardWalker, forwardWalker ) ) {
const type = ( data.walker == backwardWalker ? 'elementEnd' : 'elementStart' );
const value = data.value;
if ( value.type == type && this.isObject( value.item ) ) {
return Range._createOn( value.item );
}
if ( this.checkChild( value.nextPosition, '$text' ) ) {
return new Range( value.nextPosition );
}
}
return null;
}
/**
* 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 {@link module:engine/model/schema~Schema#isLimit limit element}, an
* {@link module:engine/model/schema~Schema#isObject object element} or a topmost ancestor is not reached.
*
* @param {module:engine/model/position~Position} position The position that the search will start from.
* @param {module:engine/model/node~Node|String} node The node for which an allowed parent should be found or its name.
* @returns {module:engine/model/element~Element|null} element Allowed parent or null if nothing was found.
*/
findAllowedParent( position, node ) {
let parent = position.parent;
while ( parent ) {
if ( this.checkChild( parent, node ) ) {
return parent;
}
// Do not split limit elements.
if ( this.isLimit( parent ) ) {
return null;
}
parent = parent.parent;
}
return null;
}
/**
* Removes attributes disallowed by the schema.
*
* @param {Iterable.<module:engine/model/node~Node>} nodes Nodes that will be filtered.
* @param {module:engine/model/writer~Writer} writer
*/
removeDisallowedAttributes( nodes, writer ) {
for ( const node of nodes ) {
// When node is a `Text` it has no children, so just filter it out.
if ( node.is( 'text' ) ) {
removeDisallowedAttributeFromNode( this, node, writer );
}
// In a case of `Element` iterates through positions between nodes inside this element
// and filter out node before the current position, or position parent when position
// is at start of an element. Using positions prevent from omitting merged nodes
// see https://github.com/ckeditor/ckeditor5-engine/issues/1789.
else {
const rangeInNode = Range._createIn( node );
const positionsInRange = rangeInNode.getPositions();
for ( const position of positionsInRange ) {
const item = position.nodeBefore || position.parent;
removeDisallowedAttributeFromNode( this, item, writer );
}
}
}
}
/**
* Creates an instance of the schema context.
*
* @param {module:engine/model/schema~SchemaContextDefinition} context
* @returns {module:engine/model/schema~SchemaContext}
*/
createContext( context ) {
return new SchemaContext( context );
}
/**
* @private
*/
_clearCache() {
this._compiledDefinitions = null;
}
/**
* @private
*/
_compile() {
const compiledDefinitions = {};
const sourceRules = this._sourceDefinitions;
const itemNames = Object.keys( sourceRules );
for ( const itemName of itemNames ) {
compiledDefinitions[ itemName ] = compileBaseItemRule( sourceRules[ itemName ], itemName );
}
for ( const itemName of itemNames ) {
compileAllowContentOf( compiledDefinitions, itemName );
}
for ( const itemName of itemNames ) {
compileAllowWhere( compiledDefinitions, itemName );
}
for ( const itemName of itemNames ) {
compileAllowAttributesOf( compiledDefinitions, itemName );
compileInheritPropertiesFrom( compiledDefinitions, itemName );
}
for ( const itemName of itemNames ) {
cleanUpAllowIn( compiledDefinitions, itemName );
cleanUpAllowAttributes( compiledDefinitions, itemName );
}
this._compiledDefinitions = compiledDefinitions;
}
/**
* @private
* @param {module:engine/model/schema~SchemaCompiledItemDefinition} def
* @param {module:engine/model/schema~SchemaContext} context
* @param {Number} contextItemIndex
*/
_checkContextMatch( def, context, contextItemIndex = context.length - 1 ) {
const contextItem = context.getItem( contextItemIndex );
if ( def.allowIn.includes( contextItem.name ) ) {
if ( contextItemIndex == 0 ) {
return true;
} else {
const parentRule = this.getDefinition( contextItem );
return this._checkContextMatch( parentRule, context, contextItemIndex - 1 );
}
} else {
return false;
}
}
/**
* 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 {@link ~Schema#getValidRanges}.
*
* @private
* @param {module:engine/model/range~Range} range The range to process.
* @param {String} attribute The name of the attribute to check.
* @returns {Iterable.<module:engine/model/range~Range>} Ranges in which the attribute is allowed.
*/
* _getValidRangesForRange( range, attribute ) {
let start = range.start;
let end = range.start;
for ( const item of range.getItems( { shallow: true } ) ) {
if ( item.is( 'element' ) ) {
yield* this._getValidRangesForRange( Range._createIn( item ), attribute );
}
if ( !this.checkAttribute( item, attribute ) ) {
if ( !start.isEqual( end ) ) {
yield new Range( start, end );
}
start = Position._createAfter( item );
}
end = Position._createAfter( item );
}
if ( !start.isEqual( end ) ) {
yield new Range( start, end );
}
}
}
mix( Schema, ObservableMixin );
/**
* Event fired when the {@link #checkChild} method is called. It allows plugging in
* additional behavior, for example implementing rules which cannot be defined using the declarative
* {@link module:engine/model/schema~SchemaItemDefinition} interface.
*
* **Note:** The {@link #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 {@link #checkChild} method fires an event because it is
* {@link module:utils/observablemixin~ObservableMixin#decorate 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 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 {@link module:engine/model/schema~SchemaContext} instance and `child` to a
* {@link module:engine/model/schema~SchemaCompiledItemDefinition} instance, so you do not 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 is normally handled by the
* `allowIn` rule from {@link module:engine/model/schema~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' } );
*
* @event checkChild
* @param {Array} args The `checkChild()`'s arguments.
*/
/**
* Event fired when the {@link #checkAttribute} method is called. It allows plugging in
* additional behavior, for example implementing rules which cannot be defined using the declarative
* {@link module:engine/model/schema~SchemaItemDefinition} interface.
*
* **Note:** The {@link #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 {@link #checkAttribute} method fires an event because it is
* {@link module:utils/observablemixin~ObservableMixin#decorate 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 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 {@link module:engine/model/schema~SchemaContext} instance, so you do not 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 attributeName = args[ 1 ];
*
* if ( context.endsWith( 'heading1 $text' ) && attributeName == 'bold' ) {
* // Prevent next listeners from being called.