@ckeditor/ckeditor5-engine 35.0.1 → 35.1.0
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/CHANGELOG.md +4 -4
- package/package.json +30 -24
- package/src/controller/datacontroller.js +467 -561
- package/src/controller/editingcontroller.js +168 -204
- package/src/conversion/conversion.js +541 -565
- package/src/conversion/conversionhelpers.js +24 -28
- package/src/conversion/downcastdispatcher.js +457 -686
- package/src/conversion/downcasthelpers.js +1583 -1965
- package/src/conversion/mapper.js +518 -707
- package/src/conversion/modelconsumable.js +240 -283
- package/src/conversion/upcastdispatcher.js +372 -718
- package/src/conversion/upcasthelpers.js +707 -818
- package/src/conversion/viewconsumable.js +524 -581
- package/src/dataprocessor/basichtmlwriter.js +12 -16
- package/src/dataprocessor/dataprocessor.js +5 -0
- package/src/dataprocessor/htmldataprocessor.js +100 -116
- package/src/dataprocessor/htmlwriter.js +1 -18
- package/src/dataprocessor/xmldataprocessor.js +116 -137
- package/src/dev-utils/model.js +260 -352
- package/src/dev-utils/operationreplayer.js +106 -126
- package/src/dev-utils/utils.js +34 -51
- package/src/dev-utils/view.js +632 -753
- package/src/index.js +0 -11
- package/src/model/batch.js +111 -127
- package/src/model/differ.js +988 -1233
- package/src/model/document.js +340 -449
- package/src/model/documentfragment.js +327 -364
- package/src/model/documentselection.js +996 -1189
- package/src/model/element.js +306 -410
- package/src/model/history.js +224 -262
- package/src/model/item.js +5 -0
- package/src/model/liveposition.js +84 -145
- package/src/model/liverange.js +108 -185
- package/src/model/markercollection.js +379 -480
- package/src/model/model.js +883 -1034
- package/src/model/node.js +419 -463
- package/src/model/nodelist.js +175 -201
- package/src/model/operation/attributeoperation.js +153 -182
- package/src/model/operation/detachoperation.js +64 -83
- package/src/model/operation/insertoperation.js +135 -166
- package/src/model/operation/markeroperation.js +114 -140
- package/src/model/operation/mergeoperation.js +163 -191
- package/src/model/operation/moveoperation.js +157 -187
- package/src/model/operation/nooperation.js +28 -38
- package/src/model/operation/operation.js +106 -125
- package/src/model/operation/operationfactory.js +30 -34
- package/src/model/operation/renameoperation.js +109 -135
- package/src/model/operation/rootattributeoperation.js +155 -188
- package/src/model/operation/splitoperation.js +196 -232
- package/src/model/operation/transform.js +1833 -2204
- package/src/model/operation/utils.js +140 -204
- package/src/model/position.js +899 -1053
- package/src/model/range.js +910 -1028
- package/src/model/rootelement.js +77 -97
- package/src/model/schema.js +1189 -1835
- package/src/model/selection.js +745 -862
- package/src/model/text.js +90 -114
- package/src/model/textproxy.js +204 -240
- package/src/model/treewalker.js +316 -397
- package/src/model/typecheckable.js +16 -0
- package/src/model/utils/autoparagraphing.js +32 -44
- package/src/model/utils/deletecontent.js +334 -418
- package/src/model/utils/findoptimalinsertionrange.js +25 -36
- package/src/model/utils/getselectedcontent.js +96 -118
- package/src/model/utils/insertcontent.js +654 -773
- package/src/model/utils/insertobject.js +96 -119
- package/src/model/utils/modifyselection.js +120 -158
- package/src/model/utils/selection-post-fixer.js +153 -201
- package/src/model/writer.js +1305 -1474
- package/src/view/attributeelement.js +189 -225
- package/src/view/containerelement.js +75 -85
- package/src/view/document.js +172 -215
- package/src/view/documentfragment.js +200 -249
- package/src/view/documentselection.js +338 -367
- package/src/view/domconverter.js +1370 -1617
- package/src/view/downcastwriter.js +1747 -2076
- package/src/view/editableelement.js +81 -97
- package/src/view/element.js +739 -890
- package/src/view/elementdefinition.js +5 -0
- package/src/view/emptyelement.js +82 -92
- package/src/view/filler.js +35 -50
- package/src/view/item.js +5 -0
- package/src/view/matcher.js +260 -559
- package/src/view/node.js +274 -360
- package/src/view/observer/arrowkeysobserver.js +19 -28
- package/src/view/observer/bubblingemittermixin.js +120 -263
- package/src/view/observer/bubblingeventinfo.js +47 -55
- package/src/view/observer/clickobserver.js +7 -13
- package/src/view/observer/compositionobserver.js +14 -24
- package/src/view/observer/domeventdata.js +57 -67
- package/src/view/observer/domeventobserver.js +40 -64
- package/src/view/observer/fakeselectionobserver.js +81 -96
- package/src/view/observer/focusobserver.js +45 -61
- package/src/view/observer/inputobserver.js +7 -13
- package/src/view/observer/keyobserver.js +17 -27
- package/src/view/observer/mouseobserver.js +7 -14
- package/src/view/observer/mutationobserver.js +220 -315
- package/src/view/observer/observer.js +81 -102
- package/src/view/observer/selectionobserver.js +191 -246
- package/src/view/observer/tabobserver.js +23 -36
- package/src/view/placeholder.js +128 -173
- package/src/view/position.js +350 -401
- package/src/view/range.js +453 -513
- package/src/view/rawelement.js +85 -112
- package/src/view/renderer.js +874 -1018
- package/src/view/rooteditableelement.js +80 -90
- package/src/view/selection.js +608 -689
- package/src/view/styles/background.js +43 -44
- package/src/view/styles/border.js +220 -276
- package/src/view/styles/margin.js +8 -17
- package/src/view/styles/padding.js +8 -16
- package/src/view/styles/utils.js +127 -160
- package/src/view/stylesmap.js +728 -905
- package/src/view/text.js +102 -126
- package/src/view/textproxy.js +144 -170
- package/src/view/treewalker.js +383 -479
- package/src/view/typecheckable.js +19 -0
- package/src/view/uielement.js +166 -187
- package/src/view/upcastwriter.js +395 -449
- package/src/view/view.js +569 -664
- package/src/dataprocessor/dataprocessor.jsdoc +0 -64
- package/src/model/item.jsdoc +0 -14
- package/src/view/elementdefinition.jsdoc +0 -59
- package/src/view/item.jsdoc +0 -14
|
@@ -2,14 +2,11 @@
|
|
|
2
2
|
* @license Copyright (c) 2003-2022, 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 engine/conversion/viewconsumable
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
8
|
import { isArray } from 'lodash-es';
|
|
11
9
|
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
|
|
12
|
-
|
|
13
10
|
/**
|
|
14
11
|
* Class used for handling consumption of view {@link module:engine/view/element~Element elements},
|
|
15
12
|
* {@link module:engine/view/text~Text text nodes} and {@link module:engine/view/documentfragment~DocumentFragment document fragments}.
|
|
@@ -34,282 +31,255 @@ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
|
|
|
34
31
|
* viewConsumable.revert( docFragment ); // Revert already consumed document fragment.
|
|
35
32
|
*/
|
|
36
33
|
export default class ViewConsumable {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
*/
|
|
285
|
-
static createFrom( from, instance ) {
|
|
286
|
-
if ( !instance ) {
|
|
287
|
-
instance = new ViewConsumable( from );
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
if ( from.is( '$text' ) ) {
|
|
291
|
-
instance.add( from );
|
|
292
|
-
|
|
293
|
-
return instance;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// Add `from` itself, if it is an element.
|
|
297
|
-
if ( from.is( 'element' ) ) {
|
|
298
|
-
instance.add( from, ViewConsumable.consumablesFromElement( from ) );
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
if ( from.is( 'documentFragment' ) ) {
|
|
302
|
-
instance.add( from );
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
for ( const child of from.getChildren() ) {
|
|
306
|
-
instance = ViewConsumable.createFrom( child, instance );
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
return instance;
|
|
310
|
-
}
|
|
34
|
+
/**
|
|
35
|
+
* Creates new ViewConsumable.
|
|
36
|
+
*/
|
|
37
|
+
constructor() {
|
|
38
|
+
/**
|
|
39
|
+
* Map of consumable elements. If {@link module:engine/view/element~Element element} is used as a key,
|
|
40
|
+
* {@link module:engine/conversion/viewconsumable~ViewElementConsumables ViewElementConsumables} instance is stored as value.
|
|
41
|
+
* For {@link module:engine/view/text~Text text nodes} and
|
|
42
|
+
* {@link module:engine/view/documentfragment~DocumentFragment document fragments} boolean value is stored as value.
|
|
43
|
+
*
|
|
44
|
+
* @protected
|
|
45
|
+
* @member {Map.<module:engine/conversion/viewconsumable~ViewElementConsumables|Boolean>}
|
|
46
|
+
*/
|
|
47
|
+
this._consumables = new Map();
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Adds {@link module:engine/view/element~Element view element}, {@link module:engine/view/text~Text text node} or
|
|
51
|
+
* {@link module:engine/view/documentfragment~DocumentFragment document fragment} as ready to be consumed.
|
|
52
|
+
*
|
|
53
|
+
* viewConsumable.add( p, { name: true } ); // Adds element's name to consume.
|
|
54
|
+
* viewConsumable.add( p, { attributes: 'name' } ); // Adds element's attribute.
|
|
55
|
+
* viewConsumable.add( p, { classes: 'foobar' } ); // Adds element's class.
|
|
56
|
+
* viewConsumable.add( p, { styles: 'color' } ); // Adds element's style
|
|
57
|
+
* viewConsumable.add( p, { attributes: 'name', styles: 'color' } ); // Adds attribute and style.
|
|
58
|
+
* viewConsumable.add( p, { classes: [ 'baz', 'bar' ] } ); // Multiple consumables can be provided.
|
|
59
|
+
* viewConsumable.add( textNode ); // Adds text node to consume.
|
|
60
|
+
* viewConsumable.add( docFragment ); // Adds document fragment to consume.
|
|
61
|
+
*
|
|
62
|
+
* Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `viewconsumable-invalid-attribute` when `class` or `style`
|
|
63
|
+
* attribute is provided - it should be handled separately by providing actual style/class.
|
|
64
|
+
*
|
|
65
|
+
* viewConsumable.add( p, { attributes: 'style' } ); // This call will throw an exception.
|
|
66
|
+
* viewConsumable.add( p, { styles: 'color' } ); // This is properly handled style.
|
|
67
|
+
*
|
|
68
|
+
* @param {module:engine/view/element~Element|module:engine/view/text~Text|module:engine/view/documentfragment~DocumentFragment} element
|
|
69
|
+
* @param {Object} [consumables] Used only if first parameter is {@link module:engine/view/element~Element view element} instance.
|
|
70
|
+
* @param {Boolean} consumables.name If set to true element's name will be included.
|
|
71
|
+
* @param {String|Array.<String>} consumables.attributes Attribute name or array of attribute names.
|
|
72
|
+
* @param {String|Array.<String>} consumables.classes Class name or array of class names.
|
|
73
|
+
* @param {String|Array.<String>} consumables.styles Style name or array of style names.
|
|
74
|
+
*/
|
|
75
|
+
add(element, consumables) {
|
|
76
|
+
let elementConsumables;
|
|
77
|
+
// For text nodes and document fragments just mark them as consumable.
|
|
78
|
+
if (element.is('$text') || element.is('documentFragment')) {
|
|
79
|
+
this._consumables.set(element, true);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
// For elements create new ViewElementConsumables or update already existing one.
|
|
83
|
+
if (!this._consumables.has(element)) {
|
|
84
|
+
elementConsumables = new ViewElementConsumables(element);
|
|
85
|
+
this._consumables.set(element, elementConsumables);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
elementConsumables = this._consumables.get(element);
|
|
89
|
+
}
|
|
90
|
+
elementConsumables.add(consumables);
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Tests if {@link module:engine/view/element~Element view element}, {@link module:engine/view/text~Text text node} or
|
|
94
|
+
* {@link module:engine/view/documentfragment~DocumentFragment document fragment} can be consumed.
|
|
95
|
+
* It returns `true` when all items included in method's call can be consumed. Returns `false` when
|
|
96
|
+
* first already consumed item is found and `null` when first non-consumable item is found.
|
|
97
|
+
*
|
|
98
|
+
* viewConsumable.test( p, { name: true } ); // Tests element's name.
|
|
99
|
+
* viewConsumable.test( p, { attributes: 'name' } ); // Tests attribute.
|
|
100
|
+
* viewConsumable.test( p, { classes: 'foobar' } ); // Tests class.
|
|
101
|
+
* viewConsumable.test( p, { styles: 'color' } ); // Tests style.
|
|
102
|
+
* viewConsumable.test( p, { attributes: 'name', styles: 'color' } ); // Tests attribute and style.
|
|
103
|
+
* viewConsumable.test( p, { classes: [ 'baz', 'bar' ] } ); // Multiple consumables can be tested.
|
|
104
|
+
* viewConsumable.test( textNode ); // Tests text node.
|
|
105
|
+
* viewConsumable.test( docFragment ); // Tests document fragment.
|
|
106
|
+
*
|
|
107
|
+
* Testing classes and styles as attribute will test if all added classes/styles can be consumed.
|
|
108
|
+
*
|
|
109
|
+
* viewConsumable.test( p, { attributes: 'class' } ); // Tests if all added classes can be consumed.
|
|
110
|
+
* viewConsumable.test( p, { attributes: 'style' } ); // Tests if all added styles can be consumed.
|
|
111
|
+
*
|
|
112
|
+
* @param {module:engine/view/element~Element|module:engine/view/text~Text|module:engine/view/documentfragment~DocumentFragment} element
|
|
113
|
+
* @param {Object} [consumables] Used only if first parameter is {@link module:engine/view/element~Element view element} instance.
|
|
114
|
+
* @param {Boolean} consumables.name If set to true element's name will be included.
|
|
115
|
+
* @param {String|Array.<String>} consumables.attributes Attribute name or array of attribute names.
|
|
116
|
+
* @param {String|Array.<String>} consumables.classes Class name or array of class names.
|
|
117
|
+
* @param {String|Array.<String>} consumables.styles Style name or array of style names.
|
|
118
|
+
* @returns {Boolean|null} Returns `true` when all items included in method's call can be consumed. Returns `false`
|
|
119
|
+
* when first already consumed item is found and `null` when first non-consumable item is found.
|
|
120
|
+
*/
|
|
121
|
+
test(element, consumables) {
|
|
122
|
+
const elementConsumables = this._consumables.get(element);
|
|
123
|
+
if (elementConsumables === undefined) {
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
// For text nodes and document fragments return stored boolean value.
|
|
127
|
+
if (element.is('$text') || element.is('documentFragment')) {
|
|
128
|
+
return elementConsumables;
|
|
129
|
+
}
|
|
130
|
+
// For elements test consumables object.
|
|
131
|
+
return elementConsumables.test(consumables);
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Consumes {@link module:engine/view/element~Element view element}, {@link module:engine/view/text~Text text node} or
|
|
135
|
+
* {@link module:engine/view/documentfragment~DocumentFragment document fragment}.
|
|
136
|
+
* It returns `true` when all items included in method's call can be consumed, otherwise returns `false`.
|
|
137
|
+
*
|
|
138
|
+
* viewConsumable.consume( p, { name: true } ); // Consumes element's name.
|
|
139
|
+
* viewConsumable.consume( p, { attributes: 'name' } ); // Consumes element's attribute.
|
|
140
|
+
* viewConsumable.consume( p, { classes: 'foobar' } ); // Consumes element's class.
|
|
141
|
+
* viewConsumable.consume( p, { styles: 'color' } ); // Consumes element's style.
|
|
142
|
+
* viewConsumable.consume( p, { attributes: 'name', styles: 'color' } ); // Consumes attribute and style.
|
|
143
|
+
* viewConsumable.consume( p, { classes: [ 'baz', 'bar' ] } ); // Multiple consumables can be consumed.
|
|
144
|
+
* viewConsumable.consume( textNode ); // Consumes text node.
|
|
145
|
+
* viewConsumable.consume( docFragment ); // Consumes document fragment.
|
|
146
|
+
*
|
|
147
|
+
* Consuming classes and styles as attribute will test if all added classes/styles can be consumed.
|
|
148
|
+
*
|
|
149
|
+
* viewConsumable.consume( p, { attributes: 'class' } ); // Consume only if all added classes can be consumed.
|
|
150
|
+
* viewConsumable.consume( p, { attributes: 'style' } ); // Consume only if all added styles can be consumed.
|
|
151
|
+
*
|
|
152
|
+
* @param {module:engine/view/element~Element|module:engine/view/text~Text|module:engine/view/documentfragment~DocumentFragment} element
|
|
153
|
+
* @param {Object} [consumables] Used only if first parameter is {@link module:engine/view/element~Element view element} instance.
|
|
154
|
+
* @param {Boolean} consumables.name If set to true element's name will be included.
|
|
155
|
+
* @param {String|Array.<String>} consumables.attributes Attribute name or array of attribute names.
|
|
156
|
+
* @param {String|Array.<String>} consumables.classes Class name or array of class names.
|
|
157
|
+
* @param {String|Array.<String>} consumables.styles Style name or array of style names.
|
|
158
|
+
* @returns {Boolean} Returns `true` when all items included in method's call can be consumed,
|
|
159
|
+
* otherwise returns `false`.
|
|
160
|
+
*/
|
|
161
|
+
consume(element, consumables) {
|
|
162
|
+
if (this.test(element, consumables)) {
|
|
163
|
+
if (element.is('$text') || element.is('documentFragment')) {
|
|
164
|
+
// For text nodes and document fragments set value to false.
|
|
165
|
+
this._consumables.set(element, false);
|
|
166
|
+
}
|
|
167
|
+
else {
|
|
168
|
+
// For elements - consume consumables object.
|
|
169
|
+
this._consumables.get(element).consume(consumables);
|
|
170
|
+
}
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Reverts {@link module:engine/view/element~Element view element}, {@link module:engine/view/text~Text text node} or
|
|
177
|
+
* {@link module:engine/view/documentfragment~DocumentFragment document fragment} so they can be consumed once again.
|
|
178
|
+
* Method does not revert items that were never previously added for consumption, even if they are included in
|
|
179
|
+
* method's call.
|
|
180
|
+
*
|
|
181
|
+
* viewConsumable.revert( p, { name: true } ); // Reverts element's name.
|
|
182
|
+
* viewConsumable.revert( p, { attributes: 'name' } ); // Reverts element's attribute.
|
|
183
|
+
* viewConsumable.revert( p, { classes: 'foobar' } ); // Reverts element's class.
|
|
184
|
+
* viewConsumable.revert( p, { styles: 'color' } ); // Reverts element's style.
|
|
185
|
+
* viewConsumable.revert( p, { attributes: 'name', styles: 'color' } ); // Reverts attribute and style.
|
|
186
|
+
* viewConsumable.revert( p, { classes: [ 'baz', 'bar' ] } ); // Multiple names can be reverted.
|
|
187
|
+
* viewConsumable.revert( textNode ); // Reverts text node.
|
|
188
|
+
* viewConsumable.revert( docFragment ); // Reverts document fragment.
|
|
189
|
+
*
|
|
190
|
+
* Reverting classes and styles as attribute will revert all classes/styles that were previously added for
|
|
191
|
+
* consumption.
|
|
192
|
+
*
|
|
193
|
+
* viewConsumable.revert( p, { attributes: 'class' } ); // Reverts all classes added for consumption.
|
|
194
|
+
* viewConsumable.revert( p, { attributes: 'style' } ); // Reverts all styles added for consumption.
|
|
195
|
+
*
|
|
196
|
+
* @param {module:engine/view/element~Element|module:engine/view/text~Text|module:engine/view/documentfragment~DocumentFragment} element
|
|
197
|
+
* @param {Object} [consumables] Used only if first parameter is {@link module:engine/view/element~Element view element} instance.
|
|
198
|
+
* @param {Boolean} consumables.name If set to true element's name will be included.
|
|
199
|
+
* @param {String|Array.<String>} consumables.attributes Attribute name or array of attribute names.
|
|
200
|
+
* @param {String|Array.<String>} consumables.classes Class name or array of class names.
|
|
201
|
+
* @param {String|Array.<String>} consumables.styles Style name or array of style names.
|
|
202
|
+
*/
|
|
203
|
+
revert(element, consumables) {
|
|
204
|
+
const elementConsumables = this._consumables.get(element);
|
|
205
|
+
if (elementConsumables !== undefined) {
|
|
206
|
+
if (element.is('$text') || element.is('documentFragment')) {
|
|
207
|
+
// For text nodes and document fragments - set consumable to true.
|
|
208
|
+
this._consumables.set(element, true);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
// For elements - revert items from consumables object.
|
|
212
|
+
elementConsumables.revert(consumables);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Creates consumable object from {@link module:engine/view/element~Element view element}. Consumable object will include
|
|
218
|
+
* element's name and all its attributes, classes and styles.
|
|
219
|
+
*
|
|
220
|
+
* @static
|
|
221
|
+
* @param {module:engine/view/element~Element} element
|
|
222
|
+
* @returns {Object} consumables
|
|
223
|
+
*/
|
|
224
|
+
static consumablesFromElement(element) {
|
|
225
|
+
const consumables = {
|
|
226
|
+
element,
|
|
227
|
+
name: true,
|
|
228
|
+
attributes: [],
|
|
229
|
+
classes: [],
|
|
230
|
+
styles: []
|
|
231
|
+
};
|
|
232
|
+
const attributes = element.getAttributeKeys();
|
|
233
|
+
for (const attribute of attributes) {
|
|
234
|
+
// Skip classes and styles - will be added separately.
|
|
235
|
+
if (attribute == 'style' || attribute == 'class') {
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
consumables.attributes.push(attribute);
|
|
239
|
+
}
|
|
240
|
+
const classes = element.getClassNames();
|
|
241
|
+
for (const className of classes) {
|
|
242
|
+
consumables.classes.push(className);
|
|
243
|
+
}
|
|
244
|
+
const styles = element.getStyleNames();
|
|
245
|
+
for (const style of styles) {
|
|
246
|
+
consumables.styles.push(style);
|
|
247
|
+
}
|
|
248
|
+
return consumables;
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Creates {@link module:engine/conversion/viewconsumable~ViewConsumable ViewConsumable} instance from
|
|
252
|
+
* {@link module:engine/view/node~Node node} or {@link module:engine/view/documentfragment~DocumentFragment document fragment}.
|
|
253
|
+
* Instance will contain all elements, child nodes, attributes, styles and classes added for consumption.
|
|
254
|
+
*
|
|
255
|
+
* @static
|
|
256
|
+
* @param {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment} from View node or document fragment
|
|
257
|
+
* from which `ViewConsumable` will be created.
|
|
258
|
+
* @param {module:engine/conversion/viewconsumable~ViewConsumable} [instance] If provided, given `ViewConsumable` instance will be used
|
|
259
|
+
* to add all consumables. It will be returned instead of a new instance.
|
|
260
|
+
*/
|
|
261
|
+
static createFrom(from, instance) {
|
|
262
|
+
if (!instance) {
|
|
263
|
+
instance = new ViewConsumable();
|
|
264
|
+
}
|
|
265
|
+
if (from.is('$text')) {
|
|
266
|
+
instance.add(from);
|
|
267
|
+
return instance;
|
|
268
|
+
}
|
|
269
|
+
// Add `from` itself, if it is an element.
|
|
270
|
+
if (from.is('element')) {
|
|
271
|
+
instance.add(from, ViewConsumable.consumablesFromElement(from));
|
|
272
|
+
}
|
|
273
|
+
if (from.is('documentFragment')) {
|
|
274
|
+
instance.add(from);
|
|
275
|
+
}
|
|
276
|
+
for (const child of from.getChildren()) {
|
|
277
|
+
instance = ViewConsumable.createFrom(child, instance);
|
|
278
|
+
}
|
|
279
|
+
return instance;
|
|
280
|
+
}
|
|
311
281
|
}
|
|
312
|
-
|
|
282
|
+
const CONSUMABLE_TYPES = ['attributes', 'classes', 'styles'];
|
|
313
283
|
/**
|
|
314
284
|
* This is a private helper-class for {@link module:engine/conversion/viewconsumable~ViewConsumable}.
|
|
315
285
|
* It represents and manipulates consumable parts of a single {@link module:engine/view/element~Element}.
|
|
@@ -317,307 +287,280 @@ export default class ViewConsumable {
|
|
|
317
287
|
* @private
|
|
318
288
|
*/
|
|
319
289
|
class ViewElementConsumables {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
/**
|
|
598
|
-
* Helper method that reverts items of a given type: attribute, class or style.
|
|
599
|
-
*
|
|
600
|
-
* @private
|
|
601
|
-
* @param {String} type Type of the consumable item: `attributes`, `classes` or , `styles`.
|
|
602
|
-
* @param {String|Array.<String>} item Consumable item or array of items.
|
|
603
|
-
*/
|
|
604
|
-
_revert( type, item ) {
|
|
605
|
-
const items = isArray( item ) ? item : [ item ];
|
|
606
|
-
const consumables = this._consumables[ type ];
|
|
607
|
-
|
|
608
|
-
for ( const name of items ) {
|
|
609
|
-
if ( type === 'attributes' && ( name === 'class' || name === 'style' ) ) {
|
|
610
|
-
const consumableName = name == 'class' ? 'classes' : 'styles';
|
|
611
|
-
|
|
612
|
-
// If class or style is provided for reverting - revert them all.
|
|
613
|
-
this._revert( consumableName, [ ...this._consumables[ consumableName ].keys() ] );
|
|
614
|
-
} else {
|
|
615
|
-
const value = consumables.get( name );
|
|
616
|
-
|
|
617
|
-
if ( value === false ) {
|
|
618
|
-
consumables.set( name, true );
|
|
619
|
-
}
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
}
|
|
290
|
+
/**
|
|
291
|
+
* Creates ViewElementConsumables instance.
|
|
292
|
+
*
|
|
293
|
+
* @param {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment} from View node or document fragment
|
|
294
|
+
* from which `ViewElementConsumables` is being created.
|
|
295
|
+
*/
|
|
296
|
+
constructor(from) {
|
|
297
|
+
/**
|
|
298
|
+
* @readonly
|
|
299
|
+
* @member {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
|
|
300
|
+
*/
|
|
301
|
+
this.element = from;
|
|
302
|
+
/**
|
|
303
|
+
* Flag indicating if name of the element can be consumed.
|
|
304
|
+
*
|
|
305
|
+
* @private
|
|
306
|
+
* @member {Boolean}
|
|
307
|
+
*/
|
|
308
|
+
this._canConsumeName = null;
|
|
309
|
+
/**
|
|
310
|
+
* Contains maps of element's consumables: attributes, classes and styles.
|
|
311
|
+
*
|
|
312
|
+
* @private
|
|
313
|
+
* @member {Object}
|
|
314
|
+
*/
|
|
315
|
+
this._consumables = {
|
|
316
|
+
attributes: new Map(),
|
|
317
|
+
styles: new Map(),
|
|
318
|
+
classes: new Map()
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Adds consumable parts of the {@link module:engine/view/element~Element view element}.
|
|
323
|
+
* Element's name itself can be marked to be consumed (when element's name is consumed its attributes, classes and
|
|
324
|
+
* styles still could be consumed):
|
|
325
|
+
*
|
|
326
|
+
* consumables.add( { name: true } );
|
|
327
|
+
*
|
|
328
|
+
* Attributes classes and styles:
|
|
329
|
+
*
|
|
330
|
+
* consumables.add( { attributes: 'title', classes: 'foo', styles: 'color' } );
|
|
331
|
+
* consumables.add( { attributes: [ 'title', 'name' ], classes: [ 'foo', 'bar' ] );
|
|
332
|
+
*
|
|
333
|
+
* Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `viewconsumable-invalid-attribute` when `class` or `style`
|
|
334
|
+
* attribute is provided - it should be handled separately by providing `style` and `class` in consumables object.
|
|
335
|
+
*
|
|
336
|
+
* @param {Object} consumables Object describing which parts of the element can be consumed.
|
|
337
|
+
* @param {Boolean} consumables.name If set to `true` element's name will be added as consumable.
|
|
338
|
+
* @param {String|Array.<String>} consumables.attributes Attribute name or array of attribute names to add as consumable.
|
|
339
|
+
* @param {String|Array.<String>} consumables.classes Class name or array of class names to add as consumable.
|
|
340
|
+
* @param {String|Array.<String>} consumables.styles Style name or array of style names to add as consumable.
|
|
341
|
+
*/
|
|
342
|
+
add(consumables) {
|
|
343
|
+
if (consumables.name) {
|
|
344
|
+
this._canConsumeName = true;
|
|
345
|
+
}
|
|
346
|
+
for (const type of CONSUMABLE_TYPES) {
|
|
347
|
+
if (type in consumables) {
|
|
348
|
+
this._add(type, consumables[type]);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Tests if parts of the {@link module:engine/view/node~Node view node} can be consumed.
|
|
354
|
+
*
|
|
355
|
+
* Element's name can be tested:
|
|
356
|
+
*
|
|
357
|
+
* consumables.test( { name: true } );
|
|
358
|
+
*
|
|
359
|
+
* Attributes classes and styles:
|
|
360
|
+
*
|
|
361
|
+
* consumables.test( { attributes: 'title', classes: 'foo', styles: 'color' } );
|
|
362
|
+
* consumables.test( { attributes: [ 'title', 'name' ], classes: [ 'foo', 'bar' ] );
|
|
363
|
+
*
|
|
364
|
+
* @param {Object} consumables Object describing which parts of the element should be tested.
|
|
365
|
+
* @param {Boolean} consumables.name If set to `true` element's name will be tested.
|
|
366
|
+
* @param {String|Array.<String>} consumables.attributes Attribute name or array of attribute names to test.
|
|
367
|
+
* @param {String|Array.<String>} consumables.classes Class name or array of class names to test.
|
|
368
|
+
* @param {String|Array.<String>} consumables.styles Style name or array of style names to test.
|
|
369
|
+
* @returns {Boolean|null} `true` when all tested items can be consumed, `null` when even one of the items
|
|
370
|
+
* was never marked for consumption and `false` when even one of the items was already consumed.
|
|
371
|
+
*/
|
|
372
|
+
test(consumables) {
|
|
373
|
+
// Check if name can be consumed.
|
|
374
|
+
if (consumables.name && !this._canConsumeName) {
|
|
375
|
+
return this._canConsumeName;
|
|
376
|
+
}
|
|
377
|
+
for (const type of CONSUMABLE_TYPES) {
|
|
378
|
+
if (type in consumables) {
|
|
379
|
+
const value = this._test(type, consumables[type]);
|
|
380
|
+
if (value !== true) {
|
|
381
|
+
return value;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
// Return true only if all can be consumed.
|
|
386
|
+
return true;
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Consumes parts of {@link module:engine/view/element~Element view element}. This function does not check if consumable item
|
|
390
|
+
* is already consumed - it consumes all consumable items provided.
|
|
391
|
+
* Element's name can be consumed:
|
|
392
|
+
*
|
|
393
|
+
* consumables.consume( { name: true } );
|
|
394
|
+
*
|
|
395
|
+
* Attributes classes and styles:
|
|
396
|
+
*
|
|
397
|
+
* consumables.consume( { attributes: 'title', classes: 'foo', styles: 'color' } );
|
|
398
|
+
* consumables.consume( { attributes: [ 'title', 'name' ], classes: [ 'foo', 'bar' ] );
|
|
399
|
+
*
|
|
400
|
+
* @param {Object} consumables Object describing which parts of the element should be consumed.
|
|
401
|
+
* @param {Boolean} consumables.name If set to `true` element's name will be consumed.
|
|
402
|
+
* @param {String|Array.<String>} consumables.attributes Attribute name or array of attribute names to consume.
|
|
403
|
+
* @param {String|Array.<String>} consumables.classes Class name or array of class names to consume.
|
|
404
|
+
* @param {String|Array.<String>} consumables.styles Style name or array of style names to consume.
|
|
405
|
+
*/
|
|
406
|
+
consume(consumables) {
|
|
407
|
+
if (consumables.name) {
|
|
408
|
+
this._canConsumeName = false;
|
|
409
|
+
}
|
|
410
|
+
for (const type of CONSUMABLE_TYPES) {
|
|
411
|
+
if (type in consumables) {
|
|
412
|
+
this._consume(type, consumables[type]);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
/**
|
|
417
|
+
* Revert already consumed parts of {@link module:engine/view/element~Element view Element}, so they can be consumed once again.
|
|
418
|
+
* Element's name can be reverted:
|
|
419
|
+
*
|
|
420
|
+
* consumables.revert( { name: true } );
|
|
421
|
+
*
|
|
422
|
+
* Attributes classes and styles:
|
|
423
|
+
*
|
|
424
|
+
* consumables.revert( { attributes: 'title', classes: 'foo', styles: 'color' } );
|
|
425
|
+
* consumables.revert( { attributes: [ 'title', 'name' ], classes: [ 'foo', 'bar' ] );
|
|
426
|
+
*
|
|
427
|
+
* @param {Object} consumables Object describing which parts of the element should be reverted.
|
|
428
|
+
* @param {Boolean} consumables.name If set to `true` element's name will be reverted.
|
|
429
|
+
* @param {String|Array.<String>} consumables.attributes Attribute name or array of attribute names to revert.
|
|
430
|
+
* @param {String|Array.<String>} consumables.classes Class name or array of class names to revert.
|
|
431
|
+
* @param {String|Array.<String>} consumables.styles Style name or array of style names to revert.
|
|
432
|
+
*/
|
|
433
|
+
revert(consumables) {
|
|
434
|
+
if (consumables.name) {
|
|
435
|
+
this._canConsumeName = true;
|
|
436
|
+
}
|
|
437
|
+
for (const type of CONSUMABLE_TYPES) {
|
|
438
|
+
if (type in consumables) {
|
|
439
|
+
this._revert(type, consumables[type]);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
/**
|
|
444
|
+
* Helper method that adds consumables of a given type: attribute, class or style.
|
|
445
|
+
*
|
|
446
|
+
* Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `viewconsumable-invalid-attribute` when `class` or `style`
|
|
447
|
+
* type is provided - it should be handled separately by providing actual style/class type.
|
|
448
|
+
*
|
|
449
|
+
* @private
|
|
450
|
+
* @param {String} type Type of the consumable item: `attributes`, `classes` or `styles`.
|
|
451
|
+
* @param {String|Array.<String>} item Consumable item or array of items.
|
|
452
|
+
*/
|
|
453
|
+
_add(type, item) {
|
|
454
|
+
const items = isArray(item) ? item : [item];
|
|
455
|
+
const consumables = this._consumables[type];
|
|
456
|
+
for (const name of items) {
|
|
457
|
+
if (type === 'attributes' && (name === 'class' || name === 'style')) {
|
|
458
|
+
/**
|
|
459
|
+
* Class and style attributes should be handled separately in
|
|
460
|
+
* {@link module:engine/conversion/viewconsumable~ViewConsumable#add `ViewConsumable#add()`}.
|
|
461
|
+
*
|
|
462
|
+
* What you have done is trying to use:
|
|
463
|
+
*
|
|
464
|
+
* consumables.add( { attributes: [ 'class', 'style' ] } );
|
|
465
|
+
*
|
|
466
|
+
* While each class and style should be registered separately:
|
|
467
|
+
*
|
|
468
|
+
* consumables.add( { classes: 'some-class', styles: 'font-weight' } );
|
|
469
|
+
*
|
|
470
|
+
* @error viewconsumable-invalid-attribute
|
|
471
|
+
*/
|
|
472
|
+
throw new CKEditorError('viewconsumable-invalid-attribute', this);
|
|
473
|
+
}
|
|
474
|
+
consumables.set(name, true);
|
|
475
|
+
if (type === 'styles') {
|
|
476
|
+
for (const alsoName of this.element.document.stylesProcessor.getRelatedStyles(name)) {
|
|
477
|
+
consumables.set(alsoName, true);
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
/**
|
|
483
|
+
* Helper method that tests consumables of a given type: attribute, class or style.
|
|
484
|
+
*
|
|
485
|
+
* @private
|
|
486
|
+
* @param {String} type Type of the consumable item: `attributes`, `classes` or `styles`.
|
|
487
|
+
* @param {String|Array.<String>} item Consumable item or array of items.
|
|
488
|
+
* @returns {Boolean|null} Returns `true` if all items can be consumed, `null` when one of the items cannot be
|
|
489
|
+
* consumed and `false` when one of the items is already consumed.
|
|
490
|
+
*/
|
|
491
|
+
_test(type, item) {
|
|
492
|
+
const items = isArray(item) ? item : [item];
|
|
493
|
+
const consumables = this._consumables[type];
|
|
494
|
+
for (const name of items) {
|
|
495
|
+
if (type === 'attributes' && (name === 'class' || name === 'style')) {
|
|
496
|
+
const consumableName = name == 'class' ? 'classes' : 'styles';
|
|
497
|
+
// Check all classes/styles if class/style attribute is tested.
|
|
498
|
+
const value = this._test(consumableName, [...this._consumables[consumableName].keys()]);
|
|
499
|
+
if (value !== true) {
|
|
500
|
+
return value;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
else {
|
|
504
|
+
const value = consumables.get(name);
|
|
505
|
+
// Return null if attribute is not found.
|
|
506
|
+
if (value === undefined) {
|
|
507
|
+
return null;
|
|
508
|
+
}
|
|
509
|
+
if (!value) {
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return true;
|
|
515
|
+
}
|
|
516
|
+
/**
|
|
517
|
+
* Helper method that consumes items of a given type: attribute, class or style.
|
|
518
|
+
*
|
|
519
|
+
* @private
|
|
520
|
+
* @param {String} type Type of the consumable item: `attributes`, `classes` or `styles`.
|
|
521
|
+
* @param {String|Array.<String>} item Consumable item or array of items.
|
|
522
|
+
*/
|
|
523
|
+
_consume(type, item) {
|
|
524
|
+
const items = isArray(item) ? item : [item];
|
|
525
|
+
const consumables = this._consumables[type];
|
|
526
|
+
for (const name of items) {
|
|
527
|
+
if (type === 'attributes' && (name === 'class' || name === 'style')) {
|
|
528
|
+
const consumableName = name == 'class' ? 'classes' : 'styles';
|
|
529
|
+
// If class or style is provided for consumption - consume them all.
|
|
530
|
+
this._consume(consumableName, [...this._consumables[consumableName].keys()]);
|
|
531
|
+
}
|
|
532
|
+
else {
|
|
533
|
+
consumables.set(name, false);
|
|
534
|
+
if (type == 'styles') {
|
|
535
|
+
for (const toConsume of this.element.document.stylesProcessor.getRelatedStyles(name)) {
|
|
536
|
+
consumables.set(toConsume, false);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Helper method that reverts items of a given type: attribute, class or style.
|
|
544
|
+
*
|
|
545
|
+
* @private
|
|
546
|
+
* @param {String} type Type of the consumable item: `attributes`, `classes` or , `styles`.
|
|
547
|
+
* @param {String|Array.<String>} item Consumable item or array of items.
|
|
548
|
+
*/
|
|
549
|
+
_revert(type, item) {
|
|
550
|
+
const items = isArray(item) ? item : [item];
|
|
551
|
+
const consumables = this._consumables[type];
|
|
552
|
+
for (const name of items) {
|
|
553
|
+
if (type === 'attributes' && (name === 'class' || name === 'style')) {
|
|
554
|
+
const consumableName = name == 'class' ? 'classes' : 'styles';
|
|
555
|
+
// If class or style is provided for reverting - revert them all.
|
|
556
|
+
this._revert(consumableName, [...this._consumables[consumableName].keys()]);
|
|
557
|
+
}
|
|
558
|
+
else {
|
|
559
|
+
const value = consumables.get(name);
|
|
560
|
+
if (value === false) {
|
|
561
|
+
consumables.set(name, true);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
}
|
|
623
566
|
}
|