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