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