@ckeditor/ckeditor5-html-support 36.0.1 → 37.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +2 -2
  2. package/build/html-support.js +1 -1
  3. package/ckeditor5-metadata.json +2 -2
  4. package/package.json +42 -36
  5. package/src/augmentation.d.ts +33 -0
  6. package/src/augmentation.js +5 -0
  7. package/src/conversionutils.d.ts +42 -0
  8. package/src/conversionutils.js +57 -77
  9. package/src/converters.d.ts +56 -0
  10. package/src/converters.js +104 -156
  11. package/src/datafilter.d.ts +250 -0
  12. package/src/datafilter.js +566 -782
  13. package/src/dataschema.d.ts +169 -0
  14. package/src/dataschema.js +143 -229
  15. package/src/fullpage.d.ts +21 -0
  16. package/src/fullpage.js +65 -86
  17. package/src/generalhtmlsupport.d.ts +88 -0
  18. package/src/generalhtmlsupport.js +244 -327
  19. package/src/generalhtmlsupportconfig.d.ts +67 -0
  20. package/src/generalhtmlsupportconfig.js +5 -0
  21. package/src/htmlcomment.d.ts +72 -0
  22. package/src/htmlcomment.js +175 -239
  23. package/src/htmlpagedataprocessor.d.ts +22 -0
  24. package/src/htmlpagedataprocessor.js +53 -76
  25. package/src/index.d.ts +25 -0
  26. package/src/index.js +1 -2
  27. package/src/integrations/codeblock.d.ts +22 -0
  28. package/src/integrations/codeblock.js +87 -115
  29. package/src/integrations/customelement.d.ts +25 -0
  30. package/src/integrations/customelement.js +127 -160
  31. package/src/integrations/documentlist.d.ts +26 -0
  32. package/src/integrations/documentlist.js +154 -191
  33. package/src/integrations/dualcontent.d.ts +44 -0
  34. package/src/integrations/dualcontent.js +92 -128
  35. package/src/integrations/heading.d.ts +25 -0
  36. package/src/integrations/heading.js +41 -54
  37. package/src/integrations/image.d.ts +25 -0
  38. package/src/integrations/image.js +154 -212
  39. package/src/integrations/integrationutils.d.ts +15 -0
  40. package/src/integrations/integrationutils.js +21 -0
  41. package/src/integrations/mediaembed.d.ts +25 -0
  42. package/src/integrations/mediaembed.js +101 -147
  43. package/src/integrations/script.d.ts +25 -0
  44. package/src/integrations/script.js +45 -67
  45. package/src/integrations/style.d.ts +25 -0
  46. package/src/integrations/style.js +45 -67
  47. package/src/integrations/table.d.ts +22 -0
  48. package/src/integrations/table.js +113 -160
  49. package/src/schemadefinitions.d.ts +13 -0
  50. package/src/schemadefinitions.js +846 -835
package/src/datafilter.js CHANGED
@@ -2,818 +2,602 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module html-support/datafilter
8
7
  */
9
-
10
8
  /* globals document */
11
-
12
- import DataSchema from './dataschema';
13
-
14
9
  import { Plugin } from 'ckeditor5/src/core';
15
10
  import { Matcher } from 'ckeditor5/src/engine';
16
11
  import { priorities, CKEditorError } from 'ckeditor5/src/utils';
17
12
  import { Widget } from 'ckeditor5/src/widget';
18
- import {
19
- viewToModelObjectConverter,
20
- toObjectWidgetConverter,
21
- createObjectView,
22
-
23
- viewToAttributeInlineConverter,
24
- attributeToViewInlineConverter,
25
-
26
- viewToModelBlockAttributeConverter,
27
- modelToViewBlockAttributeConverter
28
- } from './converters';
13
+ import { viewToModelObjectConverter, toObjectWidgetConverter, createObjectView, viewToAttributeInlineConverter, attributeToViewInlineConverter, viewToModelBlockAttributeConverter, modelToViewBlockAttributeConverter } from './converters';
14
+ import { default as DataSchema } from './dataschema';
29
15
  import { isPlainObject, pull as removeItemFromArray } from 'lodash-es';
30
-
31
16
  import '../theme/datafilter.css';
32
-
33
17
  /**
34
18
  * Allows to validate elements and element attributes registered by {@link module:html-support/dataschema~DataSchema}.
35
19
  *
36
20
  * To enable registered element in the editor, use {@link module:html-support/datafilter~DataFilter#allowElement} method:
37
21
  *
38
- * dataFilter.allowElement( 'section' );
22
+ * ```ts
23
+ * dataFilter.allowElement( 'section' );
24
+ * ```
39
25
  *
40
26
  * You can also allow or disallow specific element attributes:
41
27
  *
42
- * // Allow `data-foo` attribute on `section` element.
43
- * dataFilter.allowAttributes( {
44
- * name: 'section',
45
- * attributes: {
46
- * 'data-foo': true
47
- * }
48
- * } );
28
+ * ```ts
29
+ * // Allow `data-foo` attribute on `section` element.
30
+ * dataFilter.allowAttributes( {
31
+ * name: 'section',
32
+ * attributes: {
33
+ * 'data-foo': true
34
+ * }
35
+ * } );
49
36
  *
50
- * // Disallow `color` style attribute on 'section' element.
51
- * dataFilter.disallowAttributes( {
52
- * name: 'section',
53
- * styles: {
54
- * color: /[\s\S]+/
55
- * }
56
- * } );
37
+ * // Disallow `color` style attribute on 'section' element.
38
+ * dataFilter.disallowAttributes( {
39
+ * name: 'section',
40
+ * styles: {
41
+ * color: /[\s\S]+/
42
+ * }
43
+ * } );
44
+ * ```
57
45
  *
58
46
  * To apply the information about allowed and disallowed attributes in custom integration plugin,
59
47
  * use the {@link module:html-support/datafilter~DataFilter#processViewAttributes `processViewAttributes()`} method.
60
- *
61
- * @extends module:core/plugin~Plugin
62
48
  */
63
49
  export default class DataFilter extends Plugin {
64
- constructor( editor ) {
65
- super( editor );
66
-
67
- /**
68
- * An instance of the {@link module:html-support/dataschema~DataSchema}.
69
- *
70
- * @readonly
71
- * @private
72
- * @member {module:html-support/dataschema~DataSchema} #_dataSchema
73
- */
74
- this._dataSchema = editor.plugins.get( 'DataSchema' );
75
-
76
- /**
77
- * {@link module:engine/view/matcher~Matcher Matcher} instance describing rules upon which
78
- * content attributes should be allowed.
79
- *
80
- * @readonly
81
- * @private
82
- * @member {module:engine/view/matcher~Matcher} #_allowedAttributes
83
- */
84
- this._allowedAttributes = new Matcher();
85
-
86
- /**
87
- * {@link module:engine/view/matcher~Matcher Matcher} instance describing rules upon which
88
- * content attributes should be disallowed.
89
- *
90
- * @readonly
91
- * @private
92
- * @member {module:engine/view/matcher~Matcher} #_disallowedAttributes
93
- */
94
- this._disallowedAttributes = new Matcher();
95
-
96
- /**
97
- * Allowed element definitions by {@link module:html-support/datafilter~DataFilter#allowElement} method.
98
- *
99
- * @readonly
100
- * @private
101
- * @member {Set.<module:html-support/dataschema~DataSchemaDefinition>} #_allowedElements
102
- */
103
- this._allowedElements = new Set();
104
-
105
- /**
106
- * Disallowed element names by {@link module:html-support/datafilter~DataFilter#disallowElement} method.
107
- *
108
- * @readonly
109
- * @private
110
- * @member {Set.<String>} #_disallowedElements
111
- */
112
- this._disallowedElements = new Set();
113
-
114
- /**
115
- * Indicates if {@link module:engine/controller/datacontroller~DataController editor's data controller}
116
- * data has been already initialized.
117
- *
118
- * @private
119
- * @member {Boolean} [#_dataInitialized=false]
120
- */
121
- this._dataInitialized = false;
122
-
123
- /**
124
- * Cached map of coupled attributes. Keys are the feature attributes names
125
- * and values are arrays with coupled GHS attributes names.
126
- *
127
- * @private
128
- * @member {Map.<String,Array>}
129
- */
130
- this._coupledAttributes = null;
131
-
132
- this._registerElementsAfterInit();
133
- this._registerElementHandlers();
134
- this._registerModelPostFixer();
135
- }
136
-
137
- /**
138
- * @inheritDoc
139
- */
140
- static get pluginName() {
141
- return 'DataFilter';
142
- }
143
-
144
- /**
145
- * @inheritDoc
146
- */
147
- static get requires() {
148
- return [ DataSchema, Widget ];
149
- }
150
-
151
- /**
152
- * Load a configuration of one or many elements, where their attributes should be allowed.
153
- *
154
- * **Note**: Rules will be applied just before next data pipeline data init or set.
155
- *
156
- * @param {Array.<module:engine/view/matcher~MatcherPattern>} config Configuration of elements
157
- * that should have their attributes accepted in the editor.
158
- */
159
- loadAllowedConfig( config ) {
160
- for ( const pattern of config ) {
161
- // MatcherPattern allows omitting `name` to widen the search of elements.
162
- // Let's keep it consistent and match every element if a `name` has not been provided.
163
- const elementName = pattern.name || /[\s\S]+/;
164
- const rules = splitRules( pattern );
165
-
166
- this.allowElement( elementName );
167
-
168
- rules.forEach( pattern => this.allowAttributes( pattern ) );
169
- }
170
- }
171
-
172
- /**
173
- * Load a configuration of one or many elements, where their attributes should be disallowed.
174
- *
175
- * **Note**: Rules will be applied just before next data pipeline data init or set.
176
- *
177
- * @param {Array.<module:engine/view/matcher~MatcherPattern>} config Configuration of elements
178
- * that should have their attributes rejected from the editor.
179
- */
180
- loadDisallowedConfig( config ) {
181
- for ( const pattern of config ) {
182
- // MatcherPattern allows omitting `name` to widen the search of elements.
183
- // Let's keep it consistent and match every element if a `name` has not been provided.
184
- const elementName = pattern.name || /[\s\S]+/;
185
- const rules = splitRules( pattern );
186
-
187
- // Disallow element itself if there is no other rules.
188
- if ( rules.length == 0 ) {
189
- this.disallowElement( elementName );
190
- } else {
191
- rules.forEach( pattern => this.disallowAttributes( pattern ) );
192
- }
193
- }
194
- }
195
-
196
- /**
197
- * Allow the given element in the editor context.
198
- *
199
- * This method will only allow elements described by the {@link module:html-support/dataschema~DataSchema} used
200
- * to create data filter.
201
- *
202
- * **Note**: Rules will be applied just before next data pipeline data init or set.
203
- *
204
- * @param {String|RegExp} viewName String or regular expression matching view name.
205
- */
206
- allowElement( viewName ) {
207
- for ( const definition of this._dataSchema.getDefinitionsForView( viewName, true ) ) {
208
- if ( this._allowedElements.has( definition ) ) {
209
- continue;
210
- }
211
-
212
- this._allowedElements.add( definition );
213
-
214
- // We need to wait for all features to be initialized before we can register
215
- // element, so we can access existing features model schemas.
216
- // If the data has not been initialized yet, _registerElementsAfterInit() method will take care of
217
- // registering elements.
218
- if ( this._dataInitialized ) {
219
- // Defer registration to the next data pipeline data set so any disallow rules could be applied
220
- // even if added after allow rule (disallowElement).
221
- this.editor.data.once( 'set', () => {
222
- this._fireRegisterEvent( definition );
223
- }, {
224
- // With the highest priority listener we are able to register elements right before
225
- // running data conversion.
226
- priority: priorities.get( 'highest' ) + 1
227
- } );
228
- }
229
-
230
- // Reset cached map to recalculate it on the next usage.
231
- this._coupledAttributes = null;
232
- }
233
- }
234
-
235
- /**
236
- * Disallow the given element in the editor context.
237
- *
238
- * This method will only disallow elements described by the {@link module:html-support/dataschema~DataSchema} used
239
- * to create data filter.
240
- *
241
- * @param {String|RegExp} viewName String or regular expression matching view name.
242
- */
243
- disallowElement( viewName ) {
244
- for ( const definition of this._dataSchema.getDefinitionsForView( viewName, false ) ) {
245
- this._disallowedElements.add( definition.view );
246
- }
247
- }
248
-
249
- /**
250
- * Allow the given attributes for view element allowed by {@link #allowElement} method.
251
- *
252
- * @param {module:engine/view/matcher~MatcherPattern} config Pattern matching all attributes which should be allowed.
253
- */
254
- allowAttributes( config ) {
255
- this._allowedAttributes.add( config );
256
- }
257
-
258
- /**
259
- * Disallow the given attributes for view element allowed by {@link #allowElement} method.
260
- *
261
- * @param {module:engine/view/matcher~MatcherPattern} config Pattern matching all attributes which should be disallowed.
262
- */
263
- disallowAttributes( config ) {
264
- this._disallowedAttributes.add( config );
265
- }
266
-
267
- /**
268
- * Processes all allowed and disallowed attributes on the view element by consuming them and returning the allowed ones.
269
- *
270
- * This method applies the configuration set up by {@link #allowAttributes `allowAttributes()`}
271
- * and {@link #disallowAttributes `disallowAttributes()`} over the given view element by consuming relevant attributes.
272
- * It returns the allowed attributes that were found on the given view element for further processing by integration code.
273
- *
274
- * dispatcher.on( 'element:myElement', ( evt, data, conversionApi ) => {
275
- * // Get rid of disallowed and extract all allowed attributes from a viewElement.
276
- * const viewAttributes = dataFilter.processViewAttributes( data.viewItem, conversionApi );
277
- * // Do something with them, i.e. store inside a model as a dictionary.
278
- * if ( viewAttributes ) {
279
- * conversionApi.writer.setAttribute( 'htmlAttributesOfMyElement', viewAttributes, data.modelRange );
280
- * }
281
- * } );
282
- *
283
- * @see module:engine/conversion/viewconsumable~ViewConsumable#consume
284
- * @param {module:engine/view/element~Element} viewElement
285
- * @param {module:engine/conversion/upcastdispatcher~UpcastConversionApi} conversionApi
286
- * @returns {Object} [result]
287
- * @returns {Object} result.attributes Set with matched attribute names.
288
- * @returns {Object} result.styles Set with matched style names.
289
- * @returns {Array.<String>} result.classes Set with matched class names.
290
- */
291
- processViewAttributes( viewElement, conversionApi ) {
292
- // Make sure that the disabled attributes are handled before the allowed attributes are called.
293
- // For example, for block images the <figure> converter triggers conversion for <img> first and then for other elements, i.e. <a>.
294
- consumeAttributes( viewElement, conversionApi, this._disallowedAttributes );
295
-
296
- return consumeAttributes( viewElement, conversionApi, this._allowedAttributes );
297
- }
298
-
299
- /**
300
- * Registers elements allowed by {@link module:html-support/datafilter~DataFilter#allowElement} method
301
- * once {@link module:engine/controller/datacontroller~DataController editor's data controller} is initialized.
302
- *
303
- * @private
304
- */
305
- _registerElementsAfterInit() {
306
- this.editor.data.on( 'init', () => {
307
- this._dataInitialized = true;
308
-
309
- for ( const definition of this._allowedElements ) {
310
- this._fireRegisterEvent( definition );
311
- }
312
- }, {
313
- // With highest priority listener we are able to register elements right before
314
- // running data conversion. Also:
315
- // * Make sure that priority is higher than the one used by `RealTimeCollaborationClient`,
316
- // as RTC is stopping event propagation.
317
- // * Make sure no other features hook into this event before GHS because otherwise the
318
- // downcast conversion (for these features) could run before GHS registered its converters
319
- // (https://github.com/ckeditor/ckeditor5/issues/11356).
320
- priority: priorities.get( 'highest' ) + 1
321
- } );
322
- }
323
-
324
- /**
325
- * Registers default element handlers.
326
- *
327
- * @private
328
- */
329
- _registerElementHandlers() {
330
- this.on( 'register', ( evt, definition ) => {
331
- const schema = this.editor.model.schema;
332
-
333
- // Object element should be only registered for new features.
334
- // If the model schema is already registered, it should be handled by
335
- // #_registerBlockElement() or #_registerObjectElement() attribute handlers.
336
- if ( definition.isObject && !schema.isRegistered( definition.model ) ) {
337
- this._registerObjectElement( definition );
338
- } else if ( definition.isBlock ) {
339
- this._registerBlockElement( definition );
340
- } else if ( definition.isInline ) {
341
- this._registerInlineElement( definition );
342
- } else {
343
- /**
344
- * The definition cannot be handled by the data filter.
345
- *
346
- * Make sure that the registered definition is correct.
347
- *
348
- * @error data-filter-invalid-definition
349
- */
350
- throw new CKEditorError(
351
- 'data-filter-invalid-definition',
352
- null,
353
- definition
354
- );
355
- }
356
-
357
- evt.stop();
358
- }, { priority: 'lowest' } );
359
- }
360
-
361
- /**
362
- * Registers a model post-fixer that is removing coupled GHS attributes of inline elements. Those attributes
363
- * are removed if a coupled feature attribute is removed.
364
- *
365
- * For example, consider following HTML:
366
- *
367
- * <a href="foo.html" id="myId">bar</a>
368
- *
369
- * Which would be upcasted to following text node in the model:
370
- *
371
- * <$text linkHref="foo.html" htmlA="{ attributes: { id: 'myId' } }">bar</$text>
372
- *
373
- * When the user removes the link from that text (using UI), only `linkHref` attribute would be removed:
374
- *
375
- * <$text htmlA="{ attributes: { id: 'myId' } }">bar</$text>
376
- *
377
- * The `htmlA` attribute would stay in the model and would cause GHS to generate an `<a>` element.
378
- * This is incorrect from UX point of view, as the user wanted to remove the whole link (not only `href`).
379
- *
380
- * @private
381
- */
382
- _registerModelPostFixer() {
383
- const model = this.editor.model;
384
-
385
- model.document.registerPostFixer( writer => {
386
- const changes = model.document.differ.getChanges();
387
- let changed = false;
388
-
389
- const coupledAttributes = this._getCoupledAttributesMap();
390
-
391
- for ( const change of changes ) {
392
- // Handle only attribute removals.
393
- if ( change.type != 'attribute' || change.attributeNewValue !== null ) {
394
- continue;
395
- }
396
-
397
- // Find a list of coupled GHS attributes.
398
- const attributeKeys = coupledAttributes.get( change.attributeKey );
399
-
400
- if ( !attributeKeys ) {
401
- continue;
402
- }
403
-
404
- // Remove the coupled GHS attributes on the same range as the feature attribute was removed.
405
- for ( const { item } of change.range.getWalker( { shallow: true } ) ) {
406
- for ( const attributeKey of attributeKeys ) {
407
- if ( item.hasAttribute( attributeKey ) ) {
408
- writer.removeAttribute( attributeKey, item );
409
- changed = true;
410
- }
411
- }
412
- }
413
- }
414
-
415
- return changed;
416
- } );
417
- }
418
-
419
- /**
420
- * Collects the map of coupled attributes. The returned map is keyed by the feature attribute name
421
- * and coupled GHS attribute names are stored in the value array .
422
- *
423
- * @private
424
- * @returns {Map.<String,Array>}
425
- */
426
- _getCoupledAttributesMap() {
427
- if ( this._coupledAttributes ) {
428
- return this._coupledAttributes;
429
- }
430
-
431
- this._coupledAttributes = new Map();
432
-
433
- for ( const definition of this._allowedElements ) {
434
- if ( definition.coupledAttribute && definition.model ) {
435
- const attributeNames = this._coupledAttributes.get( definition.coupledAttribute );
436
-
437
- if ( attributeNames ) {
438
- attributeNames.push( definition.model );
439
- } else {
440
- this._coupledAttributes.set( definition.coupledAttribute, [ definition.model ] );
441
- }
442
- }
443
- }
444
- }
445
-
446
- /**
447
- * Fires `register` event for the given element definition.
448
- *
449
- * @private
450
- * @param {module:html-support/dataschema~DataSchemaDefinition} definition
451
- */
452
- _fireRegisterEvent( definition ) {
453
- if ( definition.view && this._disallowedElements.has( definition.view ) ) {
454
- return;
455
- }
456
-
457
- this.fire( definition.view ? `register:${ definition.view }` : 'register', definition );
458
- }
459
-
460
- /**
461
- * Registers object element and attribute converters for the given data schema definition.
462
- *
463
- * @private
464
- * @param {module:html-support/dataschema~DataSchemaDefinition} definition
465
- */
466
- _registerObjectElement( definition ) {
467
- const editor = this.editor;
468
- const schema = editor.model.schema;
469
- const conversion = editor.conversion;
470
- const { view: viewName, model: modelName } = definition;
471
-
472
- schema.register( modelName, definition.modelSchema );
473
-
474
- /* istanbul ignore next: paranoid check */
475
- if ( !viewName ) {
476
- return;
477
- }
478
-
479
- schema.extend( definition.model, {
480
- allowAttributes: [ 'htmlAttributes', 'htmlContent' ]
481
- } );
482
-
483
- // Store element content in special `$rawContent` custom property to
484
- // avoid editor's data filtering mechanism.
485
- editor.data.registerRawContentMatcher( {
486
- name: viewName
487
- } );
488
-
489
- conversion.for( 'upcast' ).elementToElement( {
490
- view: viewName,
491
- model: viewToModelObjectConverter( definition ),
492
- // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
493
- // this listener is called before it. If not, some elements will be transformed into a paragraph.
494
- converterPriority: priorities.get( 'low' ) + 1
495
- } );
496
- conversion.for( 'upcast' ).add( viewToModelBlockAttributeConverter( definition, this ) );
497
-
498
- conversion.for( 'editingDowncast' ).elementToStructure( {
499
- model: {
500
- name: modelName,
501
- attributes: [
502
- 'htmlAttributes'
503
- ]
504
- },
505
- view: toObjectWidgetConverter( editor, definition )
506
- } );
507
-
508
- conversion.for( 'dataDowncast' ).elementToElement( {
509
- model: modelName,
510
- view: ( modelElement, { writer } ) => {
511
- return createObjectView( viewName, modelElement, writer );
512
- }
513
- } );
514
- conversion.for( 'dataDowncast' ).add( modelToViewBlockAttributeConverter( definition ) );
515
- }
516
-
517
- /**
518
- * Registers block element and attribute converters for the given data schema definition.
519
- *
520
- * @private
521
- * @param {module:html-support/dataschema~DataSchemaBlockElementDefinition} definition
522
- */
523
- _registerBlockElement( definition ) {
524
- const editor = this.editor;
525
- const schema = editor.model.schema;
526
- const conversion = editor.conversion;
527
- const { view: viewName, model: modelName } = definition;
528
-
529
- if ( !schema.isRegistered( definition.model ) ) {
530
- schema.register( definition.model, definition.modelSchema );
531
-
532
- if ( !viewName ) {
533
- return;
534
- }
535
-
536
- conversion.for( 'upcast' ).elementToElement( {
537
- model: modelName,
538
- view: viewName,
539
- // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
540
- // this listener is called before it. If not, some elements will be transformed into a paragraph.
541
- converterPriority: priorities.get( 'low' ) + 1
542
- } );
543
-
544
- conversion.for( 'downcast' ).elementToElement( {
545
- model: modelName,
546
- view: viewName
547
- } );
548
- }
549
-
550
- if ( !viewName ) {
551
- return;
552
- }
553
-
554
- schema.extend( definition.model, {
555
- allowAttributes: 'htmlAttributes'
556
- } );
557
-
558
- conversion.for( 'upcast' ).add( viewToModelBlockAttributeConverter( definition, this ) );
559
- conversion.for( 'downcast' ).add( modelToViewBlockAttributeConverter( definition ) );
560
- }
561
-
562
- /**
563
- * Registers inline element and attribute converters for the given data schema definition.
564
- *
565
- * Extends `$text` model schema to allow the given definition model attribute and its properties.
566
- *
567
- * @private
568
- * @param {module:html-support/dataschema~DataSchemaInlineElementDefinition} definition
569
- */
570
- _registerInlineElement( definition ) {
571
- const editor = this.editor;
572
- const schema = editor.model.schema;
573
- const conversion = editor.conversion;
574
- const attributeKey = definition.model;
575
-
576
- schema.extend( '$text', {
577
- allowAttributes: attributeKey
578
- } );
579
-
580
- if ( definition.attributeProperties ) {
581
- schema.setAttributeProperties( attributeKey, definition.attributeProperties );
582
- }
583
-
584
- conversion.for( 'upcast' ).add( viewToAttributeInlineConverter( definition, this ) );
585
-
586
- conversion.for( 'downcast' ).attributeToElement( {
587
- model: attributeKey,
588
- view: attributeToViewInlineConverter( definition )
589
- } );
590
- }
591
-
592
- /**
593
- * Fired when {@link module:html-support/datafilter~DataFilter} is registering element and attribute
594
- * converters for the {@link module:html-support/dataschema~DataSchemaDefinition element definition}.
595
- *
596
- * The event also accepts {@link module:html-support/dataschema~DataSchemaDefinition#view} value
597
- * as an event namespace, e.g. `register:span`.
598
- *
599
- * dataFilter.on( 'register', ( evt, definition ) => {
600
- * editor.model.schema.register( definition.model, definition.modelSchema );
601
- * editor.conversion.elementToElement( { model: definition.model, view: definition.view } );
602
- *
603
- * evt.stop();
604
- * } );
605
- *
606
- * dataFilter.on( 'register:span', ( evt, definition ) => {
607
- * editor.model.schema.extend( '$text', { allowAttributes: 'htmlSpan' } );
608
- *
609
- * editor.conversion.for( 'upcast' ).elementToAttribute( { view: 'span', model: 'htmlSpan' } );
610
- * editor.conversion.for( 'downcast' ).attributeToElement( { view: 'span', model: 'htmlSpan' } );
611
- *
612
- * evt.stop();
613
- * }, { priority: 'high' } )
614
- *
615
- * @event register
616
- * @param {module:html-support/dataschema~DataSchemaDefinition} definition
617
- */
50
+ constructor(editor) {
51
+ super(editor);
52
+ this._dataSchema = editor.plugins.get('DataSchema');
53
+ this._allowedAttributes = new Matcher();
54
+ this._disallowedAttributes = new Matcher();
55
+ this._allowedElements = new Set();
56
+ this._disallowedElements = new Set();
57
+ this._dataInitialized = false;
58
+ this._coupledAttributes = null;
59
+ this._registerElementsAfterInit();
60
+ this._registerElementHandlers();
61
+ this._registerModelPostFixer();
62
+ }
63
+ /**
64
+ * @inheritDoc
65
+ */
66
+ static get pluginName() {
67
+ return 'DataFilter';
68
+ }
69
+ /**
70
+ * @inheritDoc
71
+ */
72
+ static get requires() {
73
+ return [DataSchema, Widget];
74
+ }
75
+ /**
76
+ * Load a configuration of one or many elements, where their attributes should be allowed.
77
+ *
78
+ * **Note**: Rules will be applied just before next data pipeline data init or set.
79
+ *
80
+ * @param config Configuration of elements that should have their attributes accepted in the editor.
81
+ */
82
+ loadAllowedConfig(config) {
83
+ for (const pattern of config) {
84
+ // MatcherPattern allows omitting `name` to widen the search of elements.
85
+ // Let's keep it consistent and match every element if a `name` has not been provided.
86
+ const elementName = pattern.name || /[\s\S]+/;
87
+ const rules = splitRules(pattern);
88
+ this.allowElement(elementName);
89
+ rules.forEach(pattern => this.allowAttributes(pattern));
90
+ }
91
+ }
92
+ /**
93
+ * Load a configuration of one or many elements, where their attributes should be disallowed.
94
+ *
95
+ * **Note**: Rules will be applied just before next data pipeline data init or set.
96
+ *
97
+ * @param config Configuration of elements that should have their attributes rejected from the editor.
98
+ */
99
+ loadDisallowedConfig(config) {
100
+ for (const pattern of config) {
101
+ // MatcherPattern allows omitting `name` to widen the search of elements.
102
+ // Let's keep it consistent and match every element if a `name` has not been provided.
103
+ const elementName = pattern.name || /[\s\S]+/;
104
+ const rules = splitRules(pattern);
105
+ // Disallow element itself if there is no other rules.
106
+ if (rules.length == 0) {
107
+ this.disallowElement(elementName);
108
+ }
109
+ else {
110
+ rules.forEach(pattern => this.disallowAttributes(pattern));
111
+ }
112
+ }
113
+ }
114
+ /**
115
+ * Allow the given element in the editor context.
116
+ *
117
+ * This method will only allow elements described by the {@link module:html-support/dataschema~DataSchema} used
118
+ * to create data filter.
119
+ *
120
+ * **Note**: Rules will be applied just before next data pipeline data init or set.
121
+ *
122
+ * @param viewName String or regular expression matching view name.
123
+ */
124
+ allowElement(viewName) {
125
+ for (const definition of this._dataSchema.getDefinitionsForView(viewName, true)) {
126
+ if (this._allowedElements.has(definition)) {
127
+ continue;
128
+ }
129
+ this._allowedElements.add(definition);
130
+ // We need to wait for all features to be initialized before we can register
131
+ // element, so we can access existing features model schemas.
132
+ // If the data has not been initialized yet, _registerElementsAfterInit() method will take care of
133
+ // registering elements.
134
+ if (this._dataInitialized) {
135
+ // Defer registration to the next data pipeline data set so any disallow rules could be applied
136
+ // even if added after allow rule (disallowElement).
137
+ this.editor.data.once('set', () => {
138
+ this._fireRegisterEvent(definition);
139
+ }, {
140
+ // With the highest priority listener we are able to register elements right before
141
+ // running data conversion.
142
+ priority: priorities.get('highest') + 1
143
+ });
144
+ }
145
+ // Reset cached map to recalculate it on the next usage.
146
+ this._coupledAttributes = null;
147
+ }
148
+ }
149
+ /**
150
+ * Disallow the given element in the editor context.
151
+ *
152
+ * This method will only disallow elements described by the {@link module:html-support/dataschema~DataSchema} used
153
+ * to create data filter.
154
+ *
155
+ * @param viewName String or regular expression matching view name.
156
+ */
157
+ disallowElement(viewName) {
158
+ for (const definition of this._dataSchema.getDefinitionsForView(viewName, false)) {
159
+ this._disallowedElements.add(definition.view);
160
+ }
161
+ }
162
+ /**
163
+ * Allow the given attributes for view element allowed by {@link #allowElement} method.
164
+ *
165
+ * @param config Pattern matching all attributes which should be allowed.
166
+ */
167
+ allowAttributes(config) {
168
+ this._allowedAttributes.add(config);
169
+ }
170
+ /**
171
+ * Disallow the given attributes for view element allowed by {@link #allowElement} method.
172
+ *
173
+ * @param config Pattern matching all attributes which should be disallowed.
174
+ */
175
+ disallowAttributes(config) {
176
+ this._disallowedAttributes.add(config);
177
+ }
178
+ /**
179
+ * Processes all allowed and disallowed attributes on the view element by consuming them and returning the allowed ones.
180
+ *
181
+ * This method applies the configuration set up by {@link #allowAttributes `allowAttributes()`}
182
+ * and {@link #disallowAttributes `disallowAttributes()`} over the given view element by consuming relevant attributes.
183
+ * It returns the allowed attributes that were found on the given view element for further processing by integration code.
184
+ *
185
+ * ```ts
186
+ * dispatcher.on( 'element:myElement', ( evt, data, conversionApi ) => {
187
+ * // Get rid of disallowed and extract all allowed attributes from a viewElement.
188
+ * const viewAttributes = dataFilter.processViewAttributes( data.viewItem, conversionApi );
189
+ * // Do something with them, i.e. store inside a model as a dictionary.
190
+ * if ( viewAttributes ) {
191
+ * conversionApi.writer.setAttribute( 'htmlAttributesOfMyElement', viewAttributes, data.modelRange );
192
+ * }
193
+ * } );
194
+ * ```
195
+ *
196
+ * @see module:engine/conversion/viewconsumable~ViewConsumable#consume
197
+ *
198
+ * @returns Object with following properties:
199
+ * - attributes Set with matched attribute names.
200
+ * - styles Set with matched style names.
201
+ * - classes Set with matched class names.
202
+ */
203
+ processViewAttributes(viewElement, conversionApi) {
204
+ // Make sure that the disabled attributes are handled before the allowed attributes are called.
205
+ // For example, for block images the <figure> converter triggers conversion for <img> first and then for other elements, i.e. <a>.
206
+ consumeAttributes(viewElement, conversionApi, this._disallowedAttributes);
207
+ return consumeAttributes(viewElement, conversionApi, this._allowedAttributes);
208
+ }
209
+ /**
210
+ * Registers elements allowed by {@link module:html-support/datafilter~DataFilter#allowElement} method
211
+ * once {@link module:engine/controller/datacontroller~DataController editor's data controller} is initialized.
212
+ */
213
+ _registerElementsAfterInit() {
214
+ this.editor.data.on('init', () => {
215
+ this._dataInitialized = true;
216
+ for (const definition of this._allowedElements) {
217
+ this._fireRegisterEvent(definition);
218
+ }
219
+ }, {
220
+ // With highest priority listener we are able to register elements right before
221
+ // running data conversion. Also:
222
+ // * Make sure that priority is higher than the one used by `RealTimeCollaborationClient`,
223
+ // as RTC is stopping event propagation.
224
+ // * Make sure no other features hook into this event before GHS because otherwise the
225
+ // downcast conversion (for these features) could run before GHS registered its converters
226
+ // (https://github.com/ckeditor/ckeditor5/issues/11356).
227
+ priority: priorities.get('highest') + 1
228
+ });
229
+ }
230
+ /**
231
+ * Registers default element handlers.
232
+ */
233
+ _registerElementHandlers() {
234
+ this.on('register', (evt, definition) => {
235
+ const schema = this.editor.model.schema;
236
+ // Object element should be only registered for new features.
237
+ // If the model schema is already registered, it should be handled by
238
+ // #_registerBlockElement() or #_registerObjectElement() attribute handlers.
239
+ if (definition.isObject && !schema.isRegistered(definition.model)) {
240
+ this._registerObjectElement(definition);
241
+ }
242
+ else if (definition.isBlock) {
243
+ this._registerBlockElement(definition);
244
+ }
245
+ else if (definition.isInline) {
246
+ this._registerInlineElement(definition);
247
+ }
248
+ else {
249
+ /**
250
+ * The definition cannot be handled by the data filter.
251
+ *
252
+ * Make sure that the registered definition is correct.
253
+ *
254
+ * @error data-filter-invalid-definition
255
+ */
256
+ throw new CKEditorError('data-filter-invalid-definition', null, definition);
257
+ }
258
+ evt.stop();
259
+ }, { priority: 'lowest' });
260
+ }
261
+ /**
262
+ * Registers a model post-fixer that is removing coupled GHS attributes of inline elements. Those attributes
263
+ * are removed if a coupled feature attribute is removed.
264
+ *
265
+ * For example, consider following HTML:
266
+ *
267
+ * ```html
268
+ * <a href="foo.html" id="myId">bar</a>
269
+ * ```
270
+ *
271
+ * Which would be upcasted to following text node in the model:
272
+ *
273
+ * ```html
274
+ * <$text linkHref="foo.html" htmlA="{ attributes: { id: 'myId' } }">bar</$text>
275
+ * ```
276
+ *
277
+ * When the user removes the link from that text (using UI), only `linkHref` attribute would be removed:
278
+ *
279
+ * ```html
280
+ * <$text htmlA="{ attributes: { id: 'myId' } }">bar</$text>
281
+ * ```
282
+ *
283
+ * The `htmlA` attribute would stay in the model and would cause GHS to generate an `<a>` element.
284
+ * This is incorrect from UX point of view, as the user wanted to remove the whole link (not only `href`).
285
+ */
286
+ _registerModelPostFixer() {
287
+ const model = this.editor.model;
288
+ model.document.registerPostFixer(writer => {
289
+ const changes = model.document.differ.getChanges();
290
+ let changed = false;
291
+ const coupledAttributes = this._getCoupledAttributesMap();
292
+ for (const change of changes) {
293
+ // Handle only attribute removals.
294
+ if (change.type != 'attribute' || change.attributeNewValue !== null) {
295
+ continue;
296
+ }
297
+ // Find a list of coupled GHS attributes.
298
+ const attributeKeys = coupledAttributes.get(change.attributeKey);
299
+ if (!attributeKeys) {
300
+ continue;
301
+ }
302
+ // Remove the coupled GHS attributes on the same range as the feature attribute was removed.
303
+ for (const { item } of change.range.getWalker({ shallow: true })) {
304
+ for (const attributeKey of attributeKeys) {
305
+ if (item.hasAttribute(attributeKey)) {
306
+ writer.removeAttribute(attributeKey, item);
307
+ changed = true;
308
+ }
309
+ }
310
+ }
311
+ }
312
+ return changed;
313
+ });
314
+ }
315
+ /**
316
+ * Collects the map of coupled attributes. The returned map is keyed by the feature attribute name
317
+ * and coupled GHS attribute names are stored in the value array.
318
+ */
319
+ _getCoupledAttributesMap() {
320
+ if (this._coupledAttributes) {
321
+ return this._coupledAttributes;
322
+ }
323
+ this._coupledAttributes = new Map();
324
+ for (const definition of this._allowedElements) {
325
+ if (definition.coupledAttribute && definition.model) {
326
+ const attributeNames = this._coupledAttributes.get(definition.coupledAttribute);
327
+ if (attributeNames) {
328
+ attributeNames.push(definition.model);
329
+ }
330
+ else {
331
+ this._coupledAttributes.set(definition.coupledAttribute, [definition.model]);
332
+ }
333
+ }
334
+ }
335
+ return this._coupledAttributes;
336
+ }
337
+ /**
338
+ * Fires `register` event for the given element definition.
339
+ */
340
+ _fireRegisterEvent(definition) {
341
+ if (definition.view && this._disallowedElements.has(definition.view)) {
342
+ return;
343
+ }
344
+ this.fire(definition.view ? `register:${definition.view}` : 'register', definition);
345
+ }
346
+ /**
347
+ * Registers object element and attribute converters for the given data schema definition.
348
+ */
349
+ _registerObjectElement(definition) {
350
+ const editor = this.editor;
351
+ const schema = editor.model.schema;
352
+ const conversion = editor.conversion;
353
+ const { view: viewName, model: modelName } = definition;
354
+ schema.register(modelName, definition.modelSchema);
355
+ /* istanbul ignore next: paranoid check */
356
+ if (!viewName) {
357
+ return;
358
+ }
359
+ schema.extend(definition.model, {
360
+ allowAttributes: ['htmlAttributes', 'htmlContent']
361
+ });
362
+ // Store element content in special `$rawContent` custom property to
363
+ // avoid editor's data filtering mechanism.
364
+ editor.data.registerRawContentMatcher({
365
+ name: viewName
366
+ });
367
+ conversion.for('upcast').elementToElement({
368
+ view: viewName,
369
+ model: viewToModelObjectConverter(definition),
370
+ // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
371
+ // this listener is called before it. If not, some elements will be transformed into a paragraph.
372
+ converterPriority: priorities.get('low') + 1
373
+ });
374
+ conversion.for('upcast').add(viewToModelBlockAttributeConverter(definition, this));
375
+ conversion.for('editingDowncast').elementToStructure({
376
+ model: {
377
+ name: modelName,
378
+ attributes: [
379
+ 'htmlAttributes'
380
+ ]
381
+ },
382
+ view: toObjectWidgetConverter(editor, definition)
383
+ });
384
+ conversion.for('dataDowncast').elementToElement({
385
+ model: modelName,
386
+ view: (modelElement, { writer }) => {
387
+ return createObjectView(viewName, modelElement, writer);
388
+ }
389
+ });
390
+ conversion.for('dataDowncast').add(modelToViewBlockAttributeConverter(definition));
391
+ }
392
+ /**
393
+ * Registers block element and attribute converters for the given data schema definition.
394
+ */
395
+ _registerBlockElement(definition) {
396
+ const editor = this.editor;
397
+ const schema = editor.model.schema;
398
+ const conversion = editor.conversion;
399
+ const { view: viewName, model: modelName } = definition;
400
+ if (!schema.isRegistered(definition.model)) {
401
+ schema.register(definition.model, definition.modelSchema);
402
+ if (!viewName) {
403
+ return;
404
+ }
405
+ conversion.for('upcast').elementToElement({
406
+ model: modelName,
407
+ view: viewName,
408
+ // With a `low` priority, `paragraph` plugin auto-paragraphing mechanism is executed. Make sure
409
+ // this listener is called before it. If not, some elements will be transformed into a paragraph.
410
+ converterPriority: priorities.get('low') + 1
411
+ });
412
+ conversion.for('downcast').elementToElement({
413
+ model: modelName,
414
+ view: viewName
415
+ });
416
+ }
417
+ if (!viewName) {
418
+ return;
419
+ }
420
+ schema.extend(definition.model, {
421
+ allowAttributes: 'htmlAttributes'
422
+ });
423
+ conversion.for('upcast').add(viewToModelBlockAttributeConverter(definition, this));
424
+ conversion.for('downcast').add(modelToViewBlockAttributeConverter(definition));
425
+ }
426
+ /**
427
+ * Registers inline element and attribute converters for the given data schema definition.
428
+ *
429
+ * Extends `$text` model schema to allow the given definition model attribute and its properties.
430
+ */
431
+ _registerInlineElement(definition) {
432
+ const editor = this.editor;
433
+ const schema = editor.model.schema;
434
+ const conversion = editor.conversion;
435
+ const attributeKey = definition.model;
436
+ schema.extend('$text', {
437
+ allowAttributes: attributeKey
438
+ });
439
+ if (definition.attributeProperties) {
440
+ schema.setAttributeProperties(attributeKey, definition.attributeProperties);
441
+ }
442
+ conversion.for('upcast').add(viewToAttributeInlineConverter(definition, this));
443
+ conversion.for('downcast').attributeToElement({
444
+ model: attributeKey,
445
+ view: attributeToViewInlineConverter(definition)
446
+ });
447
+ }
618
448
  }
619
-
620
- // Matches and consumes the given view attributes.
621
- //
622
- // @private
623
- // @param {module:engine/view/element~Element} viewElement
624
- // @param {module:engine/conversion/upcastdispatcher~UpcastConversionApi} conversionApi
625
- // @param {module:engine/view/matcher~Matcher Matcher} matcher
626
- // @returns {Object} [result]
627
- // @returns {Object} result.attributes
628
- // @returns {Object} result.styles
629
- // @returns {Array.<String>} result.classes
630
- function consumeAttributes( viewElement, conversionApi, matcher ) {
631
- const matches = consumeAttributeMatches( viewElement, conversionApi, matcher );
632
- const { attributes, styles, classes } = mergeMatchResults( matches );
633
- const viewAttributes = {};
634
-
635
- // Remove invalid DOM element attributes.
636
- if ( attributes.size ) {
637
- for ( const key of attributes ) {
638
- if ( !isValidAttributeName( key ) ) {
639
- attributes.delete( key );
640
- }
641
- }
642
- }
643
-
644
- if ( attributes.size ) {
645
- viewAttributes.attributes = iterableToObject( attributes, key => viewElement.getAttribute( key ) );
646
- }
647
-
648
- if ( styles.size ) {
649
- viewAttributes.styles = iterableToObject( styles, key => viewElement.getStyle( key ) );
650
- }
651
-
652
- if ( classes.size ) {
653
- viewAttributes.classes = Array.from( classes );
654
- }
655
-
656
- if ( !Object.keys( viewAttributes ).length ) {
657
- return null;
658
- }
659
-
660
- return viewAttributes;
449
+ /**
450
+ * Matches and consumes the given view attributes.
451
+ */
452
+ function consumeAttributes(viewElement, conversionApi, matcher) {
453
+ const matches = consumeAttributeMatches(viewElement, conversionApi, matcher);
454
+ const { attributes, styles, classes } = mergeMatchResults(matches);
455
+ const viewAttributes = {};
456
+ // Remove invalid DOM element attributes.
457
+ if (attributes.size) {
458
+ for (const key of attributes) {
459
+ if (!isValidAttributeName(key)) {
460
+ attributes.delete(key);
461
+ }
462
+ }
463
+ }
464
+ if (attributes.size) {
465
+ viewAttributes.attributes = iterableToObject(attributes, key => viewElement.getAttribute(key));
466
+ }
467
+ if (styles.size) {
468
+ viewAttributes.styles = iterableToObject(styles, key => viewElement.getStyle(key));
469
+ }
470
+ if (classes.size) {
471
+ viewAttributes.classes = Array.from(classes);
472
+ }
473
+ if (!Object.keys(viewAttributes).length) {
474
+ return null;
475
+ }
476
+ return viewAttributes;
661
477
  }
662
-
663
- // Consumes matched attributes.
664
- //
665
- // @private
666
- // @param {module:engine/view/element~Element} viewElement
667
- // @param {module:engine/conversion/upcastdispatcher~UpcastConversionApi} conversionApi
668
- // @param {module:engine/view/matcher~Matcher Matcher} matcher
669
- // @returns {Array.<Object>} Array with match information about found attributes.
670
- function consumeAttributeMatches( viewElement, { consumable }, matcher ) {
671
- const matches = matcher.matchAll( viewElement ) || [];
672
- const consumedMatches = [];
673
-
674
- for ( const match of matches ) {
675
- removeConsumedAttributes( consumable, viewElement, match );
676
-
677
- // We only want to consume attributes, so element can be still processed by other converters.
678
- delete match.match.name;
679
-
680
- consumable.consume( viewElement, match.match );
681
- consumedMatches.push( match );
682
- }
683
-
684
- return consumedMatches;
478
+ /**
479
+ * Consumes matched attributes.
480
+ *
481
+ * @returns Array with match information about found attributes.
482
+ */
483
+ function consumeAttributeMatches(viewElement, { consumable }, matcher) {
484
+ const matches = matcher.matchAll(viewElement) || [];
485
+ const consumedMatches = [];
486
+ for (const match of matches) {
487
+ removeConsumedAttributes(consumable, viewElement, match);
488
+ // We only want to consume attributes, so element can be still processed by other converters.
489
+ delete match.match.name;
490
+ consumable.consume(viewElement, match.match);
491
+ consumedMatches.push(match);
492
+ }
493
+ return consumedMatches;
685
494
  }
686
-
687
- // Removes attributes from the given match that were already consumed by other converters.
688
- //
689
- // @private
690
- // @param {module:engine/view/element~Element} viewElement
691
- // @param {module:engine/conversion/viewconsumable~ViewConsumable} consumable
692
- // @param {Object} match
693
- function removeConsumedAttributes( consumable, viewElement, match ) {
694
- for ( const key of [ 'attributes', 'classes', 'styles' ] ) {
695
- const attributes = match.match[ key ];
696
-
697
- if ( !attributes ) {
698
- continue;
699
- }
700
-
701
- // Iterating over a copy of an array so removing items doesn't influence iteration.
702
- for ( const value of Array.from( attributes ) ) {
703
- if ( !consumable.test( viewElement, ( { [ key ]: [ value ] } ) ) ) {
704
- removeItemFromArray( attributes, value );
705
- }
706
- }
707
- }
495
+ /**
496
+ * Removes attributes from the given match that were already consumed by other converters.
497
+ */
498
+ function removeConsumedAttributes(consumable, viewElement, match) {
499
+ for (const key of ['attributes', 'classes', 'styles']) {
500
+ const attributes = match.match[key];
501
+ if (!attributes) {
502
+ continue;
503
+ }
504
+ // Iterating over a copy of an array so removing items doesn't influence iteration.
505
+ for (const value of Array.from(attributes)) {
506
+ if (!consumable.test(viewElement, ({ [key]: [value] }))) {
507
+ removeItemFromArray(attributes, value);
508
+ }
509
+ }
510
+ }
708
511
  }
709
-
710
- // Merges the result of {@link module:engine/view/matcher~Matcher#matchAll} method.
711
- //
712
- // @private
713
- // @param {Array.<Object>} matches
714
- // @returns {Object} result
715
- // @returns {Set.<Object>} result.attributes Set with matched attribute names.
716
- // @returns {Set.<Object>} result.styles Set with matched style names.
717
- // @returns {Set.<String>} result.classes Set with matched class names.
718
- function mergeMatchResults( matches ) {
719
- const matchResult = {
720
- attributes: new Set(),
721
- classes: new Set(),
722
- styles: new Set()
723
- };
724
-
725
- for ( const match of matches ) {
726
- for ( const key in matchResult ) {
727
- const values = match.match[ key ] || [];
728
-
729
- values.forEach( value => matchResult[ key ].add( value ) );
730
- }
731
- }
732
-
733
- return matchResult;
512
+ /**
513
+ * Merges the result of {@link module:engine/view/matcher~Matcher#matchAll} method.
514
+ *
515
+ * @param matches
516
+ * @returns Object with following properties:
517
+ * - attributes Set with matched attribute names.
518
+ * - styles Set with matched style names.
519
+ * - classes Set with matched class names.
520
+ */
521
+ function mergeMatchResults(matches) {
522
+ const matchResult = {
523
+ attributes: new Set(),
524
+ classes: new Set(),
525
+ styles: new Set()
526
+ };
527
+ for (const match of matches) {
528
+ for (const key in matchResult) {
529
+ const values = match.match[key] || [];
530
+ values.forEach(value => (matchResult[key]).add(value));
531
+ }
532
+ }
533
+ return matchResult;
734
534
  }
735
-
736
- // Converts the given iterable object into an object.
737
- //
738
- // @private
739
- // @param {Iterable.<String>} iterable
740
- // @param {Function} getValue Should result with value for the given object key.
741
- // @returns {Object}
742
- function iterableToObject( iterable, getValue ) {
743
- const attributesObject = {};
744
-
745
- for ( const prop of iterable ) {
746
- const value = getValue( prop );
747
- if ( value !== undefined ) {
748
- attributesObject[ prop ] = getValue( prop );
749
- }
750
- }
751
-
752
- return attributesObject;
535
+ /**
536
+ * Converts the given iterable object into an object.
537
+ */
538
+ function iterableToObject(iterable, getValue) {
539
+ const attributesObject = {};
540
+ for (const prop of iterable) {
541
+ const value = getValue(prop);
542
+ if (value !== undefined) {
543
+ attributesObject[prop] = getValue(prop);
544
+ }
545
+ }
546
+ return attributesObject;
753
547
  }
754
-
755
- // Matcher by default has to match **all** patterns to count it as an actual match. Splitting the pattern
756
- // into separate patterns means that any matched pattern will be count as a match.
757
- //
758
- // @private
759
- // @param {module:engine/view/matcher~MatcherPattern} pattern Pattern to split.
760
- // @param {String} attributeName Name of the attribute to split (e.g. 'attributes', 'classes', 'styles').
761
- // @returns {Array.<module:engine/view/matcher~MatcherPattern>}
762
- function splitPattern( pattern, attributeName ) {
763
- const { name } = pattern;
764
-
765
- if ( isPlainObject( pattern[ attributeName ] ) ) {
766
- return Object.entries( pattern[ attributeName ] ).map(
767
- ( [ key, value ] ) => ( {
768
- name,
769
- [ attributeName ]: {
770
- [ key ]: value
771
- }
772
- } ) );
773
- }
774
-
775
- if ( Array.isArray( pattern[ attributeName ] ) ) {
776
- return pattern[ attributeName ].map(
777
- value => ( {
778
- name,
779
- [ attributeName ]: [ value ]
780
- } )
781
- );
782
- }
783
-
784
- return [ pattern ];
548
+ /**
549
+ * Matcher by default has to match **all** patterns to count it as an actual match. Splitting the pattern
550
+ * into separate patterns means that any matched pattern will be count as a match.
551
+ *
552
+ * @param pattern Pattern to split.
553
+ * @param attributeName Name of the attribute to split (e.g. 'attributes', 'classes', 'styles').
554
+ */
555
+ function splitPattern(pattern, attributeName) {
556
+ const { name } = pattern;
557
+ const attributeValue = pattern[attributeName];
558
+ if (isPlainObject(attributeValue)) {
559
+ return Object.entries(attributeValue).map(([key, value]) => ({
560
+ name,
561
+ [attributeName]: {
562
+ [key]: value
563
+ }
564
+ }));
565
+ }
566
+ if (Array.isArray(attributeValue)) {
567
+ return attributeValue.map(value => ({
568
+ name,
569
+ [attributeName]: [value]
570
+ }));
571
+ }
572
+ return [pattern];
785
573
  }
786
-
787
- // Rules are matched in conjunction (AND operation), but we want to have a match if *any* of the rules is matched (OR operation).
788
- // By splitting the rules we force the latter effect.
789
- //
790
- // @private
791
- // @param {module:engine/view/matcher~MatcherPattern} rules
792
- // @returns {Array.<module:engine/view/matcher~MatcherPattern>}
793
- function splitRules( rules ) {
794
- const { name, attributes, classes, styles } = rules;
795
- const splittedRules = [];
796
-
797
- if ( attributes ) {
798
- splittedRules.push( ...splitPattern( { name, attributes }, 'attributes' ) );
799
- }
800
- if ( classes ) {
801
- splittedRules.push( ...splitPattern( { name, classes }, 'classes' ) );
802
- }
803
- if ( styles ) {
804
- splittedRules.push( ...splitPattern( { name, styles }, 'styles' ) );
805
- }
806
-
807
- return splittedRules;
574
+ /**
575
+ * Rules are matched in conjunction (AND operation), but we want to have a match if *any* of the rules is matched (OR operation).
576
+ * By splitting the rules we force the latter effect.
577
+ */
578
+ function splitRules(rules) {
579
+ const { name, attributes, classes, styles } = rules;
580
+ const splittedRules = [];
581
+ if (attributes) {
582
+ splittedRules.push(...splitPattern({ name, attributes }, 'attributes'));
583
+ }
584
+ if (classes) {
585
+ splittedRules.push(...splitPattern({ name, classes }, 'classes'));
586
+ }
587
+ if (styles) {
588
+ splittedRules.push(...splitPattern({ name, styles }, 'styles'));
589
+ }
590
+ return splittedRules;
808
591
  }
809
-
810
- // Returns true if name is valid for a DOM attribute name.
811
- function isValidAttributeName( name ) {
812
- try {
813
- document.createAttribute( name );
814
- } catch ( error ) {
815
- return false;
816
- }
817
-
818
- return true;
592
+ /**
593
+ * Returns true if name is valid for a DOM attribute name.
594
+ */
595
+ function isValidAttributeName(name) {
596
+ try {
597
+ document.createAttribute(name);
598
+ }
599
+ catch (error) {
600
+ return false;
601
+ }
602
+ return true;
819
603
  }