@ckeditor/ckeditor5-engine 44.2.1 → 44.3.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.
- package/dist/index.js +1677 -1258
- package/dist/index.js.map +1 -1
- package/package.json +2 -4
- package/src/conversion/downcasthelpers.js +14 -42
- package/src/conversion/viewconsumable.d.ts +65 -97
- package/src/conversion/viewconsumable.js +217 -215
- package/src/view/attributeelement.d.ts +14 -0
- package/src/view/attributeelement.js +26 -0
- package/src/view/domconverter.js +72 -7
- package/src/view/downcastwriter.d.ts +32 -20
- package/src/view/downcastwriter.js +18 -142
- package/src/view/element.d.ts +309 -17
- package/src/view/element.js +432 -137
- package/src/view/matcher.d.ts +38 -16
- package/src/view/matcher.js +91 -166
- package/src/view/stylesmap.d.ts +68 -6
- package/src/view/stylesmap.js +144 -8
- package/src/view/tokenlist.d.ts +109 -0
- package/src/view/tokenlist.js +196 -0
package/src/view/matcher.d.ts
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* @module engine/view/matcher
|
|
7
7
|
*/
|
|
8
8
|
import type Element from './element.js';
|
|
9
|
+
import { type Consumables } from '../conversion/viewconsumable.js';
|
|
9
10
|
/**
|
|
10
11
|
* View matcher class.
|
|
11
12
|
* Instance of this class can be used to find {@link module:engine/view/element~Element elements} that match given pattern.
|
|
@@ -61,16 +62,25 @@ export default class Matcher {
|
|
|
61
62
|
* pattern: <pattern used to match found element>,
|
|
62
63
|
* match: {
|
|
63
64
|
* name: true,
|
|
64
|
-
* attributes: [
|
|
65
|
-
*
|
|
66
|
-
*
|
|
65
|
+
* attributes: [
|
|
66
|
+
* [ 'title' ],
|
|
67
|
+
* [ 'href' ],
|
|
68
|
+
* [ 'class', 'foo' ],
|
|
69
|
+
* [ 'style', 'color' ],
|
|
70
|
+
* [ 'style', 'position' ]
|
|
71
|
+
* ]
|
|
67
72
|
* }
|
|
68
73
|
* }
|
|
69
74
|
* ```
|
|
70
75
|
*
|
|
76
|
+
* You could use the `match` field from the above returned object as an input for the
|
|
77
|
+
* {@link module:engine/conversion/viewconsumable~ViewConsumable#test `ViewConsumable#test()`} and
|
|
78
|
+
* {@link module:engine/conversion/viewconsumable~ViewConsumable#consume `ViewConsumable#consume()`} methods.
|
|
79
|
+
*
|
|
71
80
|
* @see module:engine/view/matcher~Matcher#add
|
|
72
81
|
* @see module:engine/view/matcher~Matcher#matchAll
|
|
73
82
|
* @param element View element to match against stored patterns.
|
|
83
|
+
* @returns The match information about found element or `null`.
|
|
74
84
|
*/
|
|
75
85
|
match(...element: Array<Element>): MatchResult | null;
|
|
76
86
|
/**
|
|
@@ -91,7 +101,22 @@ export default class Matcher {
|
|
|
91
101
|
* @returns Element name trying to match.
|
|
92
102
|
*/
|
|
93
103
|
getElementName(): string | null;
|
|
104
|
+
/**
|
|
105
|
+
* Returns match information if {@link module:engine/view/element~Element element} is matching provided pattern.
|
|
106
|
+
* If element cannot be matched to provided pattern - returns `null`.
|
|
107
|
+
*
|
|
108
|
+
* @returns Returns object with match information or null if element is not matching.
|
|
109
|
+
*/
|
|
110
|
+
private _isElementMatching;
|
|
94
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Returns true if the given `item` matches the pattern.
|
|
114
|
+
*
|
|
115
|
+
* @internal
|
|
116
|
+
* @param pattern A pattern representing a key/value we want to match.
|
|
117
|
+
* @param item An actual item key/value (e.g. `'src'`, `'background-color'`, `'ck-widget'`) we're testing against pattern.
|
|
118
|
+
*/
|
|
119
|
+
export declare function isPatternMatched(pattern: true | string | RegExp, item: string): unknown;
|
|
95
120
|
/**
|
|
96
121
|
* An entity that is a valid pattern recognized by a matcher. `MatcherPattern` is used by {@link ~Matcher} to recognize
|
|
97
122
|
* if a view element fits in a group of view elements described by the pattern.
|
|
@@ -332,7 +357,7 @@ export default class Matcher {
|
|
|
332
357
|
* const size = fontSize.match( /(\d+)/px );
|
|
333
358
|
*
|
|
334
359
|
* if ( size && Number( size[ 1 ] ) > 26 ) {
|
|
335
|
-
* return { name: true,
|
|
360
|
+
* return { name: true, styles: [ 'font-size' ] };
|
|
336
361
|
* }
|
|
337
362
|
* }
|
|
338
363
|
*
|
|
@@ -347,7 +372,7 @@ export type MatcherPattern = string | RegExp | MatcherFunctionPattern | MatcherO
|
|
|
347
372
|
/**
|
|
348
373
|
* A function describing `MatcherPattern`. See {@link ~MatcherPattern} for examples and other options.
|
|
349
374
|
*/
|
|
350
|
-
export type MatcherFunctionPattern = (element: Element) => Match | null;
|
|
375
|
+
export type MatcherFunctionPattern = (element: Element) => Match | Consumables | null;
|
|
351
376
|
/**
|
|
352
377
|
* An object describing `MatcherPattern`. See {@link ~MatcherPattern} for examples and other options.
|
|
353
378
|
*/
|
|
@@ -356,7 +381,6 @@ export interface MatcherObjectPattern {
|
|
|
356
381
|
* View element name to match.
|
|
357
382
|
*/
|
|
358
383
|
name?: string | RegExp;
|
|
359
|
-
key?: string;
|
|
360
384
|
/**
|
|
361
385
|
* View element's classes to match.
|
|
362
386
|
*/
|
|
@@ -379,17 +403,10 @@ export interface Match {
|
|
|
379
403
|
*/
|
|
380
404
|
name?: boolean;
|
|
381
405
|
/**
|
|
382
|
-
* Array
|
|
383
|
-
|
|
384
|
-
attributes?: Array<string>;
|
|
385
|
-
/**
|
|
386
|
-
* Array with matched class names.
|
|
387
|
-
*/
|
|
388
|
-
classes?: Array<string>;
|
|
389
|
-
/**
|
|
390
|
-
* Array with matched style names.
|
|
406
|
+
* Array of matching tuples: attribute name, and optional token for tokenized attributes.
|
|
407
|
+
* Note that there could be multiple entries for the same attribute with different tokens (class names or style properties).
|
|
391
408
|
*/
|
|
392
|
-
|
|
409
|
+
attributes?: Array<[string, string?]>;
|
|
393
410
|
}
|
|
394
411
|
/**
|
|
395
412
|
* The result of {@link ~Matcher#match}.
|
|
@@ -415,6 +432,11 @@ export type PropertyPatterns<ValuePattern = string | RegExp> = true | string | R
|
|
|
415
432
|
export type AttributePatterns = PropertyPatterns;
|
|
416
433
|
export type StylePatterns = PropertyPatterns;
|
|
417
434
|
export type ClassPatterns = PropertyPatterns<never>;
|
|
435
|
+
export type NormalizedPropertyPattern = [
|
|
436
|
+
true | string | RegExp,
|
|
437
|
+
true | string | RegExp,
|
|
438
|
+
(true | string | RegExp)?
|
|
439
|
+
];
|
|
418
440
|
/**
|
|
419
441
|
* The key-value matcher pattern is missing key or value. Both must be present.
|
|
420
442
|
* Refer the documentation: {@link module:engine/view/matcher~MatcherPattern}.
|
package/src/view/matcher.js
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
4
|
*/
|
|
5
5
|
import { logWarning } from '@ckeditor/ckeditor5-utils';
|
|
6
|
+
import { normalizeConsumables } from '../conversion/viewconsumable.js';
|
|
6
7
|
/**
|
|
7
8
|
* View matcher class.
|
|
8
9
|
* Instance of this class can be used to find {@link module:engine/view/element~Element elements} that match given pattern.
|
|
@@ -68,21 +69,30 @@ export default class Matcher {
|
|
|
68
69
|
* pattern: <pattern used to match found element>,
|
|
69
70
|
* match: {
|
|
70
71
|
* name: true,
|
|
71
|
-
* attributes: [
|
|
72
|
-
*
|
|
73
|
-
*
|
|
72
|
+
* attributes: [
|
|
73
|
+
* [ 'title' ],
|
|
74
|
+
* [ 'href' ],
|
|
75
|
+
* [ 'class', 'foo' ],
|
|
76
|
+
* [ 'style', 'color' ],
|
|
77
|
+
* [ 'style', 'position' ]
|
|
78
|
+
* ]
|
|
74
79
|
* }
|
|
75
80
|
* }
|
|
76
81
|
* ```
|
|
77
82
|
*
|
|
83
|
+
* You could use the `match` field from the above returned object as an input for the
|
|
84
|
+
* {@link module:engine/conversion/viewconsumable~ViewConsumable#test `ViewConsumable#test()`} and
|
|
85
|
+
* {@link module:engine/conversion/viewconsumable~ViewConsumable#consume `ViewConsumable#consume()`} methods.
|
|
86
|
+
*
|
|
78
87
|
* @see module:engine/view/matcher~Matcher#add
|
|
79
88
|
* @see module:engine/view/matcher~Matcher#matchAll
|
|
80
89
|
* @param element View element to match against stored patterns.
|
|
90
|
+
* @returns The match information about found element or `null`.
|
|
81
91
|
*/
|
|
82
92
|
match(...element) {
|
|
83
93
|
for (const singleElement of element) {
|
|
84
94
|
for (const pattern of this._patterns) {
|
|
85
|
-
const match =
|
|
95
|
+
const match = this._isElementMatching(singleElement, pattern);
|
|
86
96
|
if (match) {
|
|
87
97
|
return {
|
|
88
98
|
element: singleElement,
|
|
@@ -108,7 +118,7 @@ export default class Matcher {
|
|
|
108
118
|
const results = [];
|
|
109
119
|
for (const singleElement of element) {
|
|
110
120
|
for (const pattern of this._patterns) {
|
|
111
|
-
const match =
|
|
121
|
+
const match = this._isElementMatching(singleElement, pattern);
|
|
112
122
|
if (match) {
|
|
113
123
|
results.push({
|
|
114
124
|
element: singleElement,
|
|
@@ -134,48 +144,61 @@ export default class Matcher {
|
|
|
134
144
|
const name = pattern.name;
|
|
135
145
|
return (typeof pattern != 'function' && name && !(name instanceof RegExp)) ? name : null;
|
|
136
146
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
match.name = matchName(pattern.name, element.name);
|
|
153
|
-
if (!match.name) {
|
|
154
|
-
return null;
|
|
147
|
+
/**
|
|
148
|
+
* Returns match information if {@link module:engine/view/element~Element element} is matching provided pattern.
|
|
149
|
+
* If element cannot be matched to provided pattern - returns `null`.
|
|
150
|
+
*
|
|
151
|
+
* @returns Returns object with match information or null if element is not matching.
|
|
152
|
+
*/
|
|
153
|
+
_isElementMatching(element, pattern) {
|
|
154
|
+
// If pattern is provided as function - return result of that function;
|
|
155
|
+
if (typeof pattern == 'function') {
|
|
156
|
+
const match = pattern(element);
|
|
157
|
+
// In some places we use Matcher with callback pattern that returns boolean.
|
|
158
|
+
if (!match || typeof match != 'object') {
|
|
159
|
+
return match;
|
|
160
|
+
}
|
|
161
|
+
return normalizeConsumables(match);
|
|
155
162
|
}
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
163
|
+
const match = {};
|
|
164
|
+
// Check element's name.
|
|
165
|
+
if (pattern.name) {
|
|
166
|
+
match.name = matchName(pattern.name, element.name);
|
|
167
|
+
if (!match.name) {
|
|
168
|
+
return null;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
const attributesMatch = [];
|
|
172
|
+
// Check element's attributes.
|
|
173
|
+
if (pattern.attributes && !matchAttributes(pattern.attributes, element, attributesMatch)) {
|
|
161
174
|
return null;
|
|
162
175
|
}
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (pattern.classes) {
|
|
166
|
-
match.classes = matchClasses(pattern.classes, element);
|
|
167
|
-
if (!match.classes) {
|
|
176
|
+
// Check element's classes.
|
|
177
|
+
if (pattern.classes && !matchClasses(pattern.classes, element, attributesMatch)) {
|
|
168
178
|
return null;
|
|
169
179
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
if (pattern.styles) {
|
|
173
|
-
match.styles = matchStyles(pattern.styles, element);
|
|
174
|
-
if (!match.styles) {
|
|
180
|
+
// Check element's styles.
|
|
181
|
+
if (pattern.styles && !matchStyles(pattern.styles, element, attributesMatch)) {
|
|
175
182
|
return null;
|
|
176
183
|
}
|
|
184
|
+
// Note the `attributesMatch` array is populated by the above calls.
|
|
185
|
+
if (attributesMatch.length) {
|
|
186
|
+
match.attributes = attributesMatch;
|
|
187
|
+
}
|
|
188
|
+
return match;
|
|
177
189
|
}
|
|
178
|
-
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Returns true if the given `item` matches the pattern.
|
|
193
|
+
*
|
|
194
|
+
* @internal
|
|
195
|
+
* @param pattern A pattern representing a key/value we want to match.
|
|
196
|
+
* @param item An actual item key/value (e.g. `'src'`, `'background-color'`, `'ck-widget'`) we're testing against pattern.
|
|
197
|
+
*/
|
|
198
|
+
export function isPatternMatched(pattern, item) {
|
|
199
|
+
return pattern === true ||
|
|
200
|
+
pattern === item ||
|
|
201
|
+
pattern instanceof RegExp && !!String(item).match(pattern);
|
|
179
202
|
}
|
|
180
203
|
/**
|
|
181
204
|
* Checks if name can be matched by provided pattern.
|
|
@@ -190,92 +213,8 @@ function matchName(pattern, name) {
|
|
|
190
213
|
return pattern === name;
|
|
191
214
|
}
|
|
192
215
|
/**
|
|
193
|
-
*
|
|
194
|
-
*
|
|
195
|
-
* Patterns can be provided in a following ways:
|
|
196
|
-
* - a boolean value matches any attribute with any value (or no value):
|
|
197
|
-
*
|
|
198
|
-
* ```ts
|
|
199
|
-
* pattern: true
|
|
200
|
-
* ```
|
|
201
|
-
*
|
|
202
|
-
* - a RegExp expression or object matches any attribute name:
|
|
203
|
-
*
|
|
204
|
-
* ```ts
|
|
205
|
-
* pattern: /h[1-6]/
|
|
206
|
-
* ```
|
|
207
|
-
*
|
|
208
|
-
* - an object matches any attribute that has the same name as the object item's key, where object item's value is:
|
|
209
|
-
* - equal to `true`, which matches any attribute value:
|
|
210
|
-
*
|
|
211
|
-
* ```ts
|
|
212
|
-
* pattern: {
|
|
213
|
-
* required: true
|
|
214
|
-
* }
|
|
215
|
-
* ```
|
|
216
|
-
*
|
|
217
|
-
* - a string that is equal to attribute value:
|
|
218
|
-
*
|
|
219
|
-
* ```ts
|
|
220
|
-
* pattern: {
|
|
221
|
-
* rel: 'nofollow'
|
|
222
|
-
* }
|
|
223
|
-
* ```
|
|
224
|
-
*
|
|
225
|
-
* - a regular expression that matches attribute value,
|
|
226
|
-
*
|
|
227
|
-
* ```ts
|
|
228
|
-
* pattern: {
|
|
229
|
-
* src: /^https/
|
|
230
|
-
* }
|
|
231
|
-
* ```
|
|
232
|
-
*
|
|
233
|
-
* - an array with items, where the item is:
|
|
234
|
-
* - a string that is equal to attribute value:
|
|
235
|
-
*
|
|
236
|
-
* ```ts
|
|
237
|
-
* pattern: [ 'data-property-1', 'data-property-2' ],
|
|
238
|
-
* ```
|
|
239
|
-
*
|
|
240
|
-
* - an object with `key` and `value` property, where `key` is a regular expression matching attribute name and
|
|
241
|
-
* `value` is either regular expression matching attribute value or a string equal to attribute value:
|
|
242
|
-
*
|
|
243
|
-
* ```ts
|
|
244
|
-
* pattern: [
|
|
245
|
-
* { key: /^data-property-/, value: true },
|
|
246
|
-
* // or:
|
|
247
|
-
* { key: /^data-property-/, value: 'foobar' },
|
|
248
|
-
* // or:
|
|
249
|
-
* { key: /^data-property-/, value: /^foo/ }
|
|
250
|
-
* ]
|
|
251
|
-
* ```
|
|
252
|
-
*
|
|
253
|
-
* @param patterns Object with information about attributes to match.
|
|
254
|
-
* @param keys Attribute, style or class keys.
|
|
255
|
-
* @param valueGetter A function providing value for a given item key.
|
|
256
|
-
* @returns Returns array with matched attribute names or `null` if no attributes were matched.
|
|
257
|
-
*/
|
|
258
|
-
function matchPatterns(patterns, keys, valueGetter) {
|
|
259
|
-
const normalizedPatterns = normalizePatterns(patterns);
|
|
260
|
-
const normalizedItems = Array.from(keys);
|
|
261
|
-
const match = [];
|
|
262
|
-
normalizedPatterns.forEach(([patternKey, patternValue]) => {
|
|
263
|
-
normalizedItems.forEach(itemKey => {
|
|
264
|
-
if (isKeyMatched(patternKey, itemKey) &&
|
|
265
|
-
isValueMatched(patternValue, itemKey, valueGetter)) {
|
|
266
|
-
match.push(itemKey);
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
// Return matches only if there are at least as many of them as there are patterns.
|
|
271
|
-
// The RegExp pattern can match more than one item.
|
|
272
|
-
if (!normalizedPatterns.length || match.length < normalizedPatterns.length) {
|
|
273
|
-
return undefined;
|
|
274
|
-
}
|
|
275
|
-
return match;
|
|
276
|
-
}
|
|
277
|
-
/**
|
|
278
|
-
* Bring all the possible pattern forms to an array of arrays where first item is a key and second is a value.
|
|
216
|
+
* Bring all the possible pattern forms to an array of tuples where first item is a key, second is a value,
|
|
217
|
+
* and third optional is a token value.
|
|
279
218
|
*
|
|
280
219
|
* Examples:
|
|
281
220
|
*
|
|
@@ -340,67 +279,53 @@ function matchPatterns(patterns, keys, valueGetter) {
|
|
|
340
279
|
*
|
|
341
280
|
* @returns Returns an array of objects or null if provided patterns were not in an expected form.
|
|
342
281
|
*/
|
|
343
|
-
function normalizePatterns(patterns) {
|
|
282
|
+
function normalizePatterns(patterns, prefix) {
|
|
344
283
|
if (Array.isArray(patterns)) {
|
|
345
284
|
return patterns.map(pattern => {
|
|
346
285
|
if (typeof pattern !== 'object' || pattern instanceof RegExp) {
|
|
347
|
-
return
|
|
286
|
+
return prefix ?
|
|
287
|
+
[prefix, pattern, true] :
|
|
288
|
+
[pattern, true];
|
|
348
289
|
}
|
|
349
290
|
if (pattern.key === undefined || pattern.value === undefined) {
|
|
350
291
|
// Documented at the end of matcher.js.
|
|
351
292
|
logWarning('matcher-pattern-missing-key-or-value', pattern);
|
|
352
293
|
}
|
|
353
|
-
return
|
|
294
|
+
return prefix ?
|
|
295
|
+
[prefix, pattern.key, pattern.value] :
|
|
296
|
+
[pattern.key, pattern.value];
|
|
354
297
|
});
|
|
355
298
|
}
|
|
356
299
|
if (typeof patterns !== 'object' || patterns instanceof RegExp) {
|
|
357
|
-
return [
|
|
300
|
+
return [
|
|
301
|
+
prefix ?
|
|
302
|
+
[prefix, patterns, true] :
|
|
303
|
+
[patterns, true]
|
|
304
|
+
];
|
|
358
305
|
}
|
|
359
|
-
// Below we do what Object.entries() does, but faster
|
|
306
|
+
// Below we do what Object.entries() does, but faster
|
|
360
307
|
const normalizedPatterns = [];
|
|
361
308
|
for (const key in patterns) {
|
|
362
309
|
// Replace with Object.hasOwn() when we upgrade to es2022.
|
|
363
310
|
if (Object.prototype.hasOwnProperty.call(patterns, key)) {
|
|
364
|
-
normalizedPatterns.push(
|
|
311
|
+
normalizedPatterns.push(prefix ?
|
|
312
|
+
[prefix, key, patterns[key]] :
|
|
313
|
+
[key, patterns[key]]);
|
|
365
314
|
}
|
|
366
315
|
}
|
|
367
316
|
return normalizedPatterns;
|
|
368
317
|
}
|
|
369
|
-
/**
|
|
370
|
-
* @param patternKey A pattern representing a key we want to match.
|
|
371
|
-
* @param itemKey An actual item key (e.g. `'src'`, `'background-color'`, `'ck-widget'`) we're testing against pattern.
|
|
372
|
-
*/
|
|
373
|
-
function isKeyMatched(patternKey, itemKey) {
|
|
374
|
-
return patternKey === true ||
|
|
375
|
-
patternKey === itemKey ||
|
|
376
|
-
patternKey instanceof RegExp && itemKey.match(patternKey);
|
|
377
|
-
}
|
|
378
|
-
/**
|
|
379
|
-
* @param patternValue A pattern representing a value we want to match.
|
|
380
|
-
* @param itemKey An item key, e.g. `background`, `href`, 'rel', etc.
|
|
381
|
-
* @param valueGetter A function used to provide a value for a given `itemKey`.
|
|
382
|
-
*/
|
|
383
|
-
function isValueMatched(patternValue, itemKey, valueGetter) {
|
|
384
|
-
if (patternValue === true) {
|
|
385
|
-
return true;
|
|
386
|
-
}
|
|
387
|
-
const itemValue = valueGetter(itemKey);
|
|
388
|
-
// For now, the reducers are not returning the full tree of properties.
|
|
389
|
-
// Casting to string preserves the old behavior until the root cause is fixed.
|
|
390
|
-
// More can be found in https://github.com/ckeditor/ckeditor5/issues/10399.
|
|
391
|
-
return patternValue === itemValue ||
|
|
392
|
-
patternValue instanceof RegExp && !!String(itemValue).match(patternValue);
|
|
393
|
-
}
|
|
394
318
|
/**
|
|
395
319
|
* Checks if attributes of provided element can be matched against provided patterns.
|
|
396
320
|
*
|
|
397
321
|
* @param patterns Object with information about attributes to match. Each key of the object will be
|
|
398
322
|
* used as attribute name. Value of each key can be a string or regular expression to match against attribute value.
|
|
399
323
|
* @param element Element which attributes will be tested.
|
|
324
|
+
* @param match An array to populate with matching tuples.
|
|
400
325
|
* @returns Returns array with matched attribute names or `null` if no attributes were matched.
|
|
401
326
|
*/
|
|
402
|
-
function matchAttributes(patterns, element) {
|
|
403
|
-
|
|
327
|
+
function matchAttributes(patterns, element, match) {
|
|
328
|
+
let excludeAttributes;
|
|
404
329
|
// `style` and `class` attribute keys are deprecated. Only allow them in object pattern
|
|
405
330
|
// for backward compatibility.
|
|
406
331
|
if (typeof patterns === 'object' && !(patterns instanceof RegExp) && !Array.isArray(patterns)) {
|
|
@@ -414,21 +339,20 @@ function matchAttributes(patterns, element) {
|
|
|
414
339
|
}
|
|
415
340
|
}
|
|
416
341
|
else {
|
|
417
|
-
|
|
418
|
-
attributeKeys.delete('class');
|
|
342
|
+
excludeAttributes = ['class', 'style'];
|
|
419
343
|
}
|
|
420
|
-
return
|
|
344
|
+
return element._collectAttributesMatch(normalizePatterns(patterns), match, excludeAttributes);
|
|
421
345
|
}
|
|
422
346
|
/**
|
|
423
347
|
* Checks if classes of provided element can be matched against provided patterns.
|
|
424
348
|
*
|
|
425
349
|
* @param patterns Array of strings or regular expressions to match against element's classes.
|
|
426
350
|
* @param element Element which classes will be tested.
|
|
351
|
+
* @param match An array to populate with matching tuples.
|
|
427
352
|
* @returns Returns array with matched class names or `null` if no classes were matched.
|
|
428
353
|
*/
|
|
429
|
-
function matchClasses(patterns, element) {
|
|
430
|
-
|
|
431
|
-
return matchPatterns(patterns, element.getClassNames(), /* istanbul ignore next -- @preserve */ () => { });
|
|
354
|
+
function matchClasses(patterns, element, match) {
|
|
355
|
+
return element._collectAttributesMatch(normalizePatterns(patterns, 'class'), match);
|
|
432
356
|
}
|
|
433
357
|
/**
|
|
434
358
|
* Checks if styles of provided element can be matched against provided patterns.
|
|
@@ -436,10 +360,11 @@ function matchClasses(patterns, element) {
|
|
|
436
360
|
* @param patterns Object with information about styles to match. Each key of the object will be
|
|
437
361
|
* used as style name. Value of each key can be a string or regular expression to match against style value.
|
|
438
362
|
* @param element Element which styles will be tested.
|
|
363
|
+
* @param match An array to populate with matching tuples.
|
|
439
364
|
* @returns Returns array with matched style names or `null` if no styles were matched.
|
|
440
365
|
*/
|
|
441
|
-
function matchStyles(patterns, element) {
|
|
442
|
-
return
|
|
366
|
+
function matchStyles(patterns, element, match) {
|
|
367
|
+
return element._collectAttributesMatch(normalizePatterns(patterns, 'style'), match);
|
|
443
368
|
}
|
|
444
369
|
/**
|
|
445
370
|
* The key-value matcher pattern is missing key or value. Both must be present.
|
package/src/view/stylesmap.d.ts
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
* @license Copyright (c) 2003-2025, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
|
|
4
4
|
*/
|
|
5
|
+
import type { ElementAttributeValue } from './element.js';
|
|
6
|
+
import { type ArrayOrItem } from '@ckeditor/ckeditor5-utils';
|
|
5
7
|
/**
|
|
6
8
|
* Styles map. Allows handling (adding, removing, retrieving) a set of style rules (usually, of an element).
|
|
7
9
|
*/
|
|
8
|
-
export default class StylesMap {
|
|
10
|
+
export default class StylesMap implements ElementAttributeValue {
|
|
9
11
|
/**
|
|
10
12
|
* Keeps an internal representation of styles map. Normalized styles are kept as object tree to allow unified modification and
|
|
11
13
|
* value access model using lodash's get, set, unset, etc methods.
|
|
@@ -44,7 +46,7 @@ export default class StylesMap {
|
|
|
44
46
|
* styles.setTo( 'border:1px solid blue;margin-top:1px;' );
|
|
45
47
|
* ```
|
|
46
48
|
*/
|
|
47
|
-
setTo(inlineStyle: string):
|
|
49
|
+
setTo(inlineStyle: string): this;
|
|
48
50
|
/**
|
|
49
51
|
* Checks if a given style is set.
|
|
50
52
|
*
|
|
@@ -181,9 +183,9 @@ export default class StylesMap {
|
|
|
181
183
|
* styles.toString(); // -> 'margin-bottom:1px;margin-left:1px;'
|
|
182
184
|
* ```
|
|
183
185
|
*
|
|
184
|
-
* @param
|
|
186
|
+
* @param names Style name or an array of names.
|
|
185
187
|
*/
|
|
186
|
-
remove(
|
|
188
|
+
remove(names: ArrayOrItem<string>): void;
|
|
187
189
|
/**
|
|
188
190
|
* Returns a normalized style object or a single value.
|
|
189
191
|
*
|
|
@@ -210,7 +212,7 @@ export default class StylesMap {
|
|
|
210
212
|
*
|
|
211
213
|
* @param name Style name.
|
|
212
214
|
*/
|
|
213
|
-
getNormalized(name?: string): StyleValue;
|
|
215
|
+
getNormalized(name?: string): StyleValue | undefined;
|
|
214
216
|
/**
|
|
215
217
|
* Returns a normalized style string. Styles are sorted by name.
|
|
216
218
|
*
|
|
@@ -311,14 +313,74 @@ export default class StylesMap {
|
|
|
311
313
|
* @param expand Expand shorthand style properties and all return equivalent style representations.
|
|
312
314
|
*/
|
|
313
315
|
getStyleNames(expand?: boolean): Array<string>;
|
|
316
|
+
/**
|
|
317
|
+
* Alias for {@link #getStyleNames}.
|
|
318
|
+
*/
|
|
319
|
+
keys(): Array<string>;
|
|
314
320
|
/**
|
|
315
321
|
* Removes all styles.
|
|
316
322
|
*/
|
|
317
323
|
clear(): void;
|
|
324
|
+
/**
|
|
325
|
+
* Returns `true` if both attributes have the same styles.
|
|
326
|
+
*/
|
|
327
|
+
isSimilar(other: StylesMap): boolean;
|
|
318
328
|
/**
|
|
319
329
|
* Returns normalized styles entries for further processing.
|
|
320
330
|
*/
|
|
321
331
|
getStylesEntries(): Array<PropertyDescriptor>;
|
|
332
|
+
/**
|
|
333
|
+
* Clones the attribute value.
|
|
334
|
+
*
|
|
335
|
+
* @internal
|
|
336
|
+
*/
|
|
337
|
+
_clone(): this;
|
|
338
|
+
/**
|
|
339
|
+
* Used by the {@link module:engine/view/matcher~Matcher Matcher} to collect matching styles.
|
|
340
|
+
*
|
|
341
|
+
* @internal
|
|
342
|
+
* @param tokenPattern The matched style name pattern.
|
|
343
|
+
* @param valuePattern The matched style value pattern.
|
|
344
|
+
* @returns An array of matching tokens (style names).
|
|
345
|
+
*/
|
|
346
|
+
_getTokensMatch(tokenPattern: true | string | RegExp, valuePattern: true | string | RegExp): Array<string> | undefined;
|
|
347
|
+
/**
|
|
348
|
+
* Returns a list of consumables for the attribute. This includes related styles.
|
|
349
|
+
*
|
|
350
|
+
* Could be filtered by the given style name.
|
|
351
|
+
*
|
|
352
|
+
* @internal
|
|
353
|
+
*/
|
|
354
|
+
_getConsumables(name?: string): Array<string>;
|
|
355
|
+
/**
|
|
356
|
+
* Used by {@link module:engine/view/element~Element#_canMergeAttributesFrom} to verify if the given attribute can be merged without
|
|
357
|
+
* conflicts into the attribute.
|
|
358
|
+
*
|
|
359
|
+
* This method is indirectly used by the {@link module:engine/view/downcastwriter~DowncastWriter} while down-casting
|
|
360
|
+
* an {@link module:engine/view/attributeelement~AttributeElement} to merge it with other AttributeElement.
|
|
361
|
+
*
|
|
362
|
+
* @internal
|
|
363
|
+
*/
|
|
364
|
+
_canMergeFrom(other: StylesMap): boolean;
|
|
365
|
+
/**
|
|
366
|
+
* Used by {@link module:engine/view/element~Element#_mergeAttributesFrom} to merge a given attribute into the attribute.
|
|
367
|
+
*
|
|
368
|
+
* This method is indirectly used by the {@link module:engine/view/downcastwriter~DowncastWriter} while down-casting
|
|
369
|
+
* an {@link module:engine/view/attributeelement~AttributeElement} to merge it with other AttributeElement.
|
|
370
|
+
*
|
|
371
|
+
* @internal
|
|
372
|
+
*/
|
|
373
|
+
_mergeFrom(other: StylesMap): void;
|
|
374
|
+
/**
|
|
375
|
+
* Used by {@link module:engine/view/element~Element#_canSubtractAttributesOf} to verify if the given attribute can be fully
|
|
376
|
+
* subtracted from the attribute.
|
|
377
|
+
*
|
|
378
|
+
* This method is indirectly used by the {@link module:engine/view/downcastwriter~DowncastWriter} while down-casting
|
|
379
|
+
* an {@link module:engine/view/attributeelement~AttributeElement} to unwrap the AttributeElement.
|
|
380
|
+
*
|
|
381
|
+
* @internal
|
|
382
|
+
*/
|
|
383
|
+
_isMatching(other: StylesMap): boolean;
|
|
322
384
|
/**
|
|
323
385
|
* Removes empty objects upon removing an entry from internal object.
|
|
324
386
|
*/
|
|
@@ -377,7 +439,7 @@ export declare class StylesProcessor {
|
|
|
377
439
|
* @param name Name of style property.
|
|
378
440
|
* @param styles Object holding normalized styles.
|
|
379
441
|
*/
|
|
380
|
-
getNormalized(name: string | undefined, styles: Styles): StyleValue;
|
|
442
|
+
getNormalized(name: string | undefined, styles: Styles): StyleValue | undefined;
|
|
381
443
|
/**
|
|
382
444
|
* Returns a reduced form of style property form normalized object.
|
|
383
445
|
*
|