@ckeditor/ckeditor5-engine 30.0.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 (117) hide show
  1. package/LICENSE.md +17 -0
  2. package/README.md +30 -0
  3. package/package.json +70 -0
  4. package/src/controller/datacontroller.js +563 -0
  5. package/src/controller/editingcontroller.js +149 -0
  6. package/src/conversion/conversion.js +644 -0
  7. package/src/conversion/conversionhelpers.js +40 -0
  8. package/src/conversion/downcastdispatcher.js +914 -0
  9. package/src/conversion/downcasthelpers.js +1706 -0
  10. package/src/conversion/mapper.js +696 -0
  11. package/src/conversion/modelconsumable.js +329 -0
  12. package/src/conversion/upcastdispatcher.js +807 -0
  13. package/src/conversion/upcasthelpers.js +997 -0
  14. package/src/conversion/viewconsumable.js +623 -0
  15. package/src/dataprocessor/basichtmlwriter.js +32 -0
  16. package/src/dataprocessor/dataprocessor.jsdoc +64 -0
  17. package/src/dataprocessor/htmldataprocessor.js +159 -0
  18. package/src/dataprocessor/htmlwriter.js +22 -0
  19. package/src/dataprocessor/xmldataprocessor.js +161 -0
  20. package/src/dev-utils/model.js +482 -0
  21. package/src/dev-utils/operationreplayer.js +140 -0
  22. package/src/dev-utils/utils.js +103 -0
  23. package/src/dev-utils/view.js +1091 -0
  24. package/src/index.js +52 -0
  25. package/src/model/batch.js +82 -0
  26. package/src/model/differ.js +1282 -0
  27. package/src/model/document.js +483 -0
  28. package/src/model/documentfragment.js +390 -0
  29. package/src/model/documentselection.js +1261 -0
  30. package/src/model/element.js +438 -0
  31. package/src/model/history.js +138 -0
  32. package/src/model/item.jsdoc +14 -0
  33. package/src/model/liveposition.js +182 -0
  34. package/src/model/liverange.js +221 -0
  35. package/src/model/markercollection.js +553 -0
  36. package/src/model/model.js +934 -0
  37. package/src/model/node.js +507 -0
  38. package/src/model/nodelist.js +217 -0
  39. package/src/model/operation/attributeoperation.js +202 -0
  40. package/src/model/operation/detachoperation.js +103 -0
  41. package/src/model/operation/insertoperation.js +188 -0
  42. package/src/model/operation/markeroperation.js +154 -0
  43. package/src/model/operation/mergeoperation.js +216 -0
  44. package/src/model/operation/moveoperation.js +209 -0
  45. package/src/model/operation/nooperation.js +58 -0
  46. package/src/model/operation/operation.js +139 -0
  47. package/src/model/operation/operationfactory.js +49 -0
  48. package/src/model/operation/renameoperation.js +155 -0
  49. package/src/model/operation/rootattributeoperation.js +211 -0
  50. package/src/model/operation/splitoperation.js +254 -0
  51. package/src/model/operation/transform.js +2389 -0
  52. package/src/model/operation/utils.js +292 -0
  53. package/src/model/position.js +1164 -0
  54. package/src/model/range.js +1049 -0
  55. package/src/model/rootelement.js +111 -0
  56. package/src/model/schema.js +1851 -0
  57. package/src/model/selection.js +902 -0
  58. package/src/model/text.js +138 -0
  59. package/src/model/textproxy.js +279 -0
  60. package/src/model/treewalker.js +414 -0
  61. package/src/model/utils/autoparagraphing.js +77 -0
  62. package/src/model/utils/deletecontent.js +528 -0
  63. package/src/model/utils/getselectedcontent.js +150 -0
  64. package/src/model/utils/insertcontent.js +824 -0
  65. package/src/model/utils/modifyselection.js +229 -0
  66. package/src/model/utils/selection-post-fixer.js +297 -0
  67. package/src/model/writer.js +1574 -0
  68. package/src/view/attributeelement.js +274 -0
  69. package/src/view/containerelement.js +123 -0
  70. package/src/view/document.js +221 -0
  71. package/src/view/documentfragment.js +273 -0
  72. package/src/view/documentselection.js +387 -0
  73. package/src/view/domconverter.js +1437 -0
  74. package/src/view/downcastwriter.js +2121 -0
  75. package/src/view/editableelement.js +118 -0
  76. package/src/view/element.js +945 -0
  77. package/src/view/elementdefinition.jsdoc +59 -0
  78. package/src/view/emptyelement.js +119 -0
  79. package/src/view/filler.js +161 -0
  80. package/src/view/item.jsdoc +14 -0
  81. package/src/view/matcher.js +776 -0
  82. package/src/view/node.js +391 -0
  83. package/src/view/observer/arrowkeysobserver.js +58 -0
  84. package/src/view/observer/bubblingemittermixin.js +307 -0
  85. package/src/view/observer/bubblingeventinfo.js +71 -0
  86. package/src/view/observer/clickobserver.js +46 -0
  87. package/src/view/observer/compositionobserver.js +79 -0
  88. package/src/view/observer/domeventdata.js +82 -0
  89. package/src/view/observer/domeventobserver.js +99 -0
  90. package/src/view/observer/fakeselectionobserver.js +118 -0
  91. package/src/view/observer/focusobserver.js +106 -0
  92. package/src/view/observer/inputobserver.js +44 -0
  93. package/src/view/observer/keyobserver.js +83 -0
  94. package/src/view/observer/mouseobserver.js +56 -0
  95. package/src/view/observer/mutationobserver.js +345 -0
  96. package/src/view/observer/observer.js +118 -0
  97. package/src/view/observer/selectionobserver.js +242 -0
  98. package/src/view/placeholder.js +285 -0
  99. package/src/view/position.js +426 -0
  100. package/src/view/range.js +533 -0
  101. package/src/view/rawelement.js +148 -0
  102. package/src/view/renderer.js +1037 -0
  103. package/src/view/rooteditableelement.js +107 -0
  104. package/src/view/selection.js +718 -0
  105. package/src/view/styles/background.js +73 -0
  106. package/src/view/styles/border.js +362 -0
  107. package/src/view/styles/margin.js +41 -0
  108. package/src/view/styles/padding.js +40 -0
  109. package/src/view/styles/utils.js +277 -0
  110. package/src/view/stylesmap.js +938 -0
  111. package/src/view/text.js +147 -0
  112. package/src/view/textproxy.js +199 -0
  113. package/src/view/treewalker.js +496 -0
  114. package/src/view/uielement.js +238 -0
  115. package/src/view/upcastwriter.js +484 -0
  116. package/src/view/view.js +721 -0
  117. package/theme/placeholder.css +27 -0
@@ -0,0 +1,644 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module engine/conversion/conversion
8
+ */
9
+
10
+ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
11
+ import UpcastHelpers from './upcasthelpers';
12
+ import DowncastHelpers from './downcasthelpers';
13
+ import toArray from '@ckeditor/ckeditor5-utils/src/toarray';
14
+
15
+ /**
16
+ * A utility class that helps add converters to upcast and downcast dispatchers.
17
+ *
18
+ * We recommend reading the {@glink framework/guides/architecture/editing-engine Editing engine architecture} guide first to
19
+ * understand the core concepts of the conversion mechanisms.
20
+ *
21
+ * An instance of the conversion manager is available in the
22
+ * {@link module:core/editor/editor~Editor#conversion `editor.conversion`} property
23
+ * and by default has the following groups of dispatchers (i.e. directions of conversion):
24
+ *
25
+ * * `downcast` (editing and data downcasts)
26
+ * * `editingDowncast`
27
+ * * `dataDowncast`
28
+ * * `upcast`
29
+ *
30
+ * # One-way converters
31
+ *
32
+ * To add a converter to a specific group, use the {@link module:engine/conversion/conversion~Conversion#for `for()`}
33
+ * method:
34
+ *
35
+ * // Add a converter to editing downcast and data downcast.
36
+ * editor.conversion.for( 'downcast' ).elementToElement( config ) );
37
+ *
38
+ * // Add a converter to the data pipepline only:
39
+ * editor.conversion.for( 'dataDowncast' ).elementToElement( dataConversionConfig ) );
40
+ *
41
+ * // And a slightly different one for the editing pipeline:
42
+ * editor.conversion.for( 'editingDowncast' ).elementToElement( editingConversionConfig ) );
43
+ *
44
+ * See {@link module:engine/conversion/conversion~Conversion#for `for()`} method documentation to learn more about
45
+ * available conversion helpers and how to use your custom ones.
46
+ *
47
+ * # Two-way converters
48
+ *
49
+ * Besides using one-way converters via the `for()` method, you can also use other methods available in this
50
+ * class to add two-way converters (upcast and downcast):
51
+ *
52
+ * * {@link module:engine/conversion/conversion~Conversion#elementToElement `elementToElement()`} –
53
+ * Model element to view element and vice versa.
54
+ * * {@link module:engine/conversion/conversion~Conversion#attributeToElement `attributeToElement()`} –
55
+ * Model attribute to view element and vice versa.
56
+ * * {@link module:engine/conversion/conversion~Conversion#attributeToAttribute `attributeToAttribute()`} –
57
+ * Model attribute to view element and vice versa.
58
+ */
59
+ export default class Conversion {
60
+ /**
61
+ * Creates a new conversion instance.
62
+ *
63
+ * @param {module:engine/conversion/downcastdispatcher~DowncastDispatcher|
64
+ * Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher>} downcastDispatchers
65
+ * @param {module:engine/conversion/upcastdispatcher~UpcastDispatcher|
66
+ * Array.<module:engine/conversion/upcastdispatcher~UpcastDispatcher>} upcastDispatchers
67
+ */
68
+ constructor( downcastDispatchers, upcastDispatchers ) {
69
+ /**
70
+ * Maps dispatchers group name to ConversionHelpers instances.
71
+ *
72
+ * @private
73
+ * @member {Map.<String,module:engine/conversion/conversionhelpers~ConversionHelpers>}
74
+ */
75
+ this._helpers = new Map();
76
+
77
+ // Define default 'downcast' & 'upcast' dispatchers groups. Those groups are always available as two-way converters needs them.
78
+ this._downcast = toArray( downcastDispatchers );
79
+ this._createConversionHelpers( { name: 'downcast', dispatchers: this._downcast, isDowncast: true } );
80
+
81
+ this._upcast = toArray( upcastDispatchers );
82
+ this._createConversionHelpers( { name: 'upcast', dispatchers: this._upcast, isDowncast: false } );
83
+ }
84
+
85
+ /**
86
+ * Define an alias for registered dispatcher.
87
+ *
88
+ * const conversion = new Conversion(
89
+ * [ dataDowncastDispatcher, editingDowncastDispatcher ],
90
+ * upcastDispatcher
91
+ * );
92
+ *
93
+ * conversion.addAlias( 'dataDowncast', dataDowncastDispatcher );
94
+ *
95
+ * @param {String} alias An alias of a dispatcher.
96
+ * @param {module:engine/conversion/downcastdispatcher~DowncastDispatcher|
97
+ * module:engine/conversion/upcastdispatcher~UpcastDispatcher} dispatcher Dispatcher which should have an alias.
98
+ */
99
+ addAlias( alias, dispatcher ) {
100
+ const isDowncast = this._downcast.includes( dispatcher );
101
+ const isUpcast = this._upcast.includes( dispatcher );
102
+
103
+ if ( !isUpcast && !isDowncast ) {
104
+ /**
105
+ * Trying to register and alias for a dispatcher that nas not been registered.
106
+ *
107
+ * @error conversion-add-alias-dispatcher-not-registered
108
+ */
109
+ throw new CKEditorError(
110
+ 'conversion-add-alias-dispatcher-not-registered',
111
+ this
112
+ );
113
+ }
114
+
115
+ this._createConversionHelpers( { name: alias, dispatchers: [ dispatcher ], isDowncast } );
116
+ }
117
+
118
+ /**
119
+ * Provides a chainable API to assign converters to conversion dispatchers group.
120
+ *
121
+ * If the given group name has not been registered, the
122
+ * {@link module:utils/ckeditorerror~CKEditorError `conversion-for-unknown-group` error} is thrown.
123
+ *
124
+ * You can use conversion helpers available directly in the `for()` chain or your custom ones via
125
+ * the {@link module:engine/conversion/conversionhelpers~ConversionHelpers#add `add()`} method.
126
+ *
127
+ * # Using bulit-in conversion helpers
128
+ *
129
+ * The `for()` chain comes with a set of conversion helpers which you can use like this:
130
+ *
131
+ * editor.conversion.for( 'downcast' )
132
+ * .elementToElement( config1 ) // Adds an element-to-element downcast converter.
133
+ * .attributeToElement( config2 ); // Adds an attribute-to-element downcast converter.
134
+ *
135
+ * editor.conversion.for( 'upcast' )
136
+ * .elementToAttribute( config3 ); // Adds an element-to-attribute upcast converter.
137
+ *
138
+ * Refer to the documentation of built-in conversion helpers to learn about their configuration options.
139
+ *
140
+ * * downcast (model-to-view) conversion helpers:
141
+ *
142
+ * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#elementToElement `elementToElement()`},
143
+ * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#attributeToElement `attributeToElement()`},
144
+ * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#attributeToAttribute `attributeToAttribute()`}.
145
+ * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToElement `markerToElement()`}.
146
+ * * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToHighlight `markerToHighlight()`}.
147
+ *
148
+ * * upcast (view-to-model) conversion helpers:
149
+ *
150
+ * * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToElement `elementToElement()`},
151
+ * * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToAttribute `elementToAttribute()`},
152
+ * * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#attributeToAttribute `attributeToAttribute()`}.
153
+ * * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToMarker `elementToMarker()`}.
154
+ *
155
+ * # Using custom conversion helpers
156
+ *
157
+ * If you need to implement a nontypical converter, you can do so by calling:
158
+ *
159
+ * editor.conversion.for( direction ).add( customHelper );
160
+ *
161
+ * The `.add()` method takes exactly one parameter, which is a function. This function should accept one parameter that
162
+ * is a dispatcher instance. The function should add an actual converter to the passed dispatcher instance.
163
+ *
164
+ * Example:
165
+ *
166
+ * editor.conversion.for( 'upcast' ).add( dispatcher => {
167
+ * dispatcher.on( 'element:a', ( evt, data, conversionApi ) => {
168
+ * // Do something with a view <a> element.
169
+ * } );
170
+ * } );
171
+ *
172
+ * Refer to the documentation of {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher}
173
+ * and {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher} to learn how to write
174
+ * custom converters.
175
+ *
176
+ * @param {String} groupName The name of dispatchers group to add the converters to.
177
+ * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers}
178
+ */
179
+ for( groupName ) {
180
+ if ( !this._helpers.has( groupName ) ) {
181
+ /**
182
+ * Trying to add a converter to an unknown dispatchers group.
183
+ *
184
+ * @error conversion-for-unknown-group
185
+ */
186
+ throw new CKEditorError( 'conversion-for-unknown-group', this );
187
+ }
188
+
189
+ return this._helpers.get( groupName );
190
+ }
191
+
192
+ /**
193
+ * Sets up converters between the model and the view that convert a model element to a view element (and vice versa).
194
+ * For example, the model `<paragraph>Foo</paragraph>` is `<p>Foo</p>` in the view.
195
+ *
196
+ * // A simple conversion from the `paragraph` model element to the `<p>` view element (and vice versa).
197
+ * editor.conversion.elementToElement( { model: 'paragraph', view: 'p' } );
198
+ *
199
+ * // Override other converters by specifying a converter definition with a higher priority.
200
+ * editor.conversion.elementToElement( { model: 'paragraph', view: 'div', converterPriority: 'high' } );
201
+ *
202
+ * // View specified as an object instead of a string.
203
+ * editor.conversion.elementToElement( {
204
+ * model: 'fancyParagraph',
205
+ * view: {
206
+ * name: 'p',
207
+ * classes: 'fancy'
208
+ * }
209
+ * } );
210
+ *
211
+ * // Use `upcastAlso` to define other view elements that should also be converted to a `paragraph` element.
212
+ * editor.conversion.elementToElement( {
213
+ * model: 'paragraph',
214
+ * view: 'p',
215
+ * upcastAlso: [
216
+ * 'div',
217
+ * {
218
+ * // Any element with the `display: block` style.
219
+ * styles: {
220
+ * display: 'block'
221
+ * }
222
+ * }
223
+ * ]
224
+ * } );
225
+ *
226
+ * // `upcastAlso` set as callback enables a conversion of a wide range of different view elements.
227
+ * editor.conversion.elementToElement( {
228
+ * model: 'heading',
229
+ * view: 'h2',
230
+ * // Convert "heading-like" paragraphs to headings.
231
+ * upcastAlso: viewElement => {
232
+ * const fontSize = viewElement.getStyle( 'font-size' );
233
+ *
234
+ * if ( !fontSize ) {
235
+ * return null;
236
+ * }
237
+ *
238
+ * const match = fontSize.match( /(\d+)\s*px/ );
239
+ *
240
+ * if ( !match ) {
241
+ * return null;
242
+ * }
243
+ *
244
+ * const size = Number( match[ 1 ] );
245
+ *
246
+ * if ( size > 26 ) {
247
+ * // Returned value can be an object with the matched properties.
248
+ * // These properties will be "consumed" during the conversion.
249
+ * // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
250
+ *
251
+ * return { name: true, styles: [ 'font-size' ] };
252
+ * }
253
+ *
254
+ * return null;
255
+ * }
256
+ * } );
257
+ *
258
+ * `definition.model` is a `String` with a model element name to convert from or to.
259
+ * See {@link module:engine/conversion/conversion~ConverterDefinition} to learn about other parameters.
260
+ *
261
+ * @param {module:engine/conversion/conversion~ConverterDefinition} definition The converter definition.
262
+ */
263
+ elementToElement( definition ) {
264
+ // Set up downcast converter.
265
+ this.for( 'downcast' ).elementToElement( definition );
266
+
267
+ // Set up upcast converter.
268
+ for ( const { model, view } of _getAllUpcastDefinitions( definition ) ) {
269
+ this.for( 'upcast' )
270
+ .elementToElement( {
271
+ model,
272
+ view,
273
+ converterPriority: definition.converterPriority
274
+ } );
275
+ }
276
+ }
277
+
278
+ /**
279
+ * Sets up converters between the model and the view that convert a model attribute to a view element (and vice versa).
280
+ * For example, a model text node with `"Foo"` as data and the `bold` attribute is `<strong>Foo</strong>` in the view.
281
+ *
282
+ * // A simple conversion from the `bold=true` attribute to the `<strong>` view element (and vice versa).
283
+ * editor.conversion.attributeToElement( { model: 'bold', view: 'strong' } );
284
+ *
285
+ * // Override other converters by specifying a converter definition with a higher priority.
286
+ * editor.conversion.attributeToElement( { model: 'bold', view: 'b', converterPriority: 'high' } );
287
+ *
288
+ * // View specified as an object instead of a string.
289
+ * editor.conversion.attributeToElement( {
290
+ * model: 'bold',
291
+ * view: {
292
+ * name: 'span',
293
+ * classes: 'bold'
294
+ * }
295
+ * } );
296
+ *
297
+ * // Use `config.model.name` to define the conversion only from a given node type, `$text` in this case.
298
+ * // The same attribute on different elements may then be handled by a different converter.
299
+ * editor.conversion.attributeToElement( {
300
+ * model: {
301
+ * key: 'textDecoration',
302
+ * values: [ 'underline', 'lineThrough' ],
303
+ * name: '$text'
304
+ * },
305
+ * view: {
306
+ * underline: {
307
+ * name: 'span',
308
+ * styles: {
309
+ * 'text-decoration': 'underline'
310
+ * }
311
+ * },
312
+ * lineThrough: {
313
+ * name: 'span',
314
+ * styles: {
315
+ * 'text-decoration': 'line-through'
316
+ * }
317
+ * }
318
+ * }
319
+ * } );
320
+ *
321
+ * // Use `upcastAlso` to define other view elements that should also be converted to the `bold` attribute.
322
+ * editor.conversion.attributeToElement( {
323
+ * model: 'bold',
324
+ * view: 'strong',
325
+ * upcastAlso: [
326
+ * 'b',
327
+ * {
328
+ * name: 'span',
329
+ * classes: 'bold'
330
+ * },
331
+ * {
332
+ * name: 'span',
333
+ * styles: {
334
+ * 'font-weight': 'bold'
335
+ * }
336
+ * },
337
+ * viewElement => {
338
+ * const fontWeight = viewElement.getStyle( 'font-weight' );
339
+ *
340
+ * if ( viewElement.is( 'element', 'span' ) && fontWeight && /\d+/.test() && Number( fontWeight ) > 500 ) {
341
+ * // Returned value can be an object with the matched properties.
342
+ * // These properties will be "consumed" during the conversion.
343
+ * // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
344
+ *
345
+ * return {
346
+ * name: true,
347
+ * styles: [ 'font-weight' ]
348
+ * };
349
+ * }
350
+ * }
351
+ * ]
352
+ * } );
353
+ *
354
+ * // Conversion from and to a model attribute key whose value is an enum (`fontSize=big|small`).
355
+ * // `upcastAlso` set as callback enables a conversion of a wide range of different view elements.
356
+ * editor.conversion.attributeToElement( {
357
+ * model: {
358
+ * key: 'fontSize',
359
+ * values: [ 'big', 'small' ]
360
+ * },
361
+ * view: {
362
+ * big: {
363
+ * name: 'span',
364
+ * styles: {
365
+ * 'font-size': '1.2em'
366
+ * }
367
+ * },
368
+ * small: {
369
+ * name: 'span',
370
+ * styles: {
371
+ * 'font-size': '0.8em'
372
+ * }
373
+ * }
374
+ * },
375
+ * upcastAlso: {
376
+ * big: viewElement => {
377
+ * const fontSize = viewElement.getStyle( 'font-size' );
378
+ *
379
+ * if ( !fontSize ) {
380
+ * return null;
381
+ * }
382
+ *
383
+ * const match = fontSize.match( /(\d+)\s*px/ );
384
+ *
385
+ * if ( !match ) {
386
+ * return null;
387
+ * }
388
+ *
389
+ * const size = Number( match[ 1 ] );
390
+ *
391
+ * if ( viewElement.is( 'element', 'span' ) && size > 10 ) {
392
+ * // Returned value can be an object with the matched properties.
393
+ * // These properties will be "consumed" during the conversion.
394
+ * // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
395
+ *
396
+ * return { name: true, styles: [ 'font-size' ] };
397
+ * }
398
+ *
399
+ * return null;
400
+ * },
401
+ * small: viewElement => {
402
+ * const fontSize = viewElement.getStyle( 'font-size' );
403
+ *
404
+ * if ( !fontSize ) {
405
+ * return null;
406
+ * }
407
+ *
408
+ * const match = fontSize.match( /(\d+)\s*px/ );
409
+ *
410
+ * if ( !match ) {
411
+ * return null;
412
+ * }
413
+ *
414
+ * const size = Number( match[ 1 ] );
415
+ *
416
+ * if ( viewElement.is( 'element', 'span' ) && size < 10 ) {
417
+ * // Returned value can be an object with the matched properties.
418
+ * // These properties will be "consumed" during the conversion.
419
+ * // See `engine.view.Matcher~MatcherPattern` and `engine.view.Matcher#match` for more details.
420
+ *
421
+ * return { name: true, styles: [ 'font-size' ] };
422
+ * }
423
+ *
424
+ * return null;
425
+ * }
426
+ * }
427
+ * } );
428
+ *
429
+ * The `definition.model` parameter specifies which model attribute should be converted from or to. It can be a `{ key, value }` object
430
+ * describing the attribute key and value to convert or a `String` specifying just the attribute key (then `value` is set to `true`).
431
+ * See {@link module:engine/conversion/conversion~ConverterDefinition} to learn about other parameters.
432
+ *
433
+ * @param {module:engine/conversion/conversion~ConverterDefinition} definition The converter definition.
434
+ */
435
+ attributeToElement( definition ) {
436
+ // Set up downcast converter.
437
+ this.for( 'downcast' ).attributeToElement( definition );
438
+
439
+ // Set up upcast converter.
440
+ for ( const { model, view } of _getAllUpcastDefinitions( definition ) ) {
441
+ this.for( 'upcast' )
442
+ .elementToAttribute( {
443
+ view,
444
+ model,
445
+ converterPriority: definition.converterPriority
446
+ } );
447
+ }
448
+ }
449
+
450
+ /**
451
+ * Sets up converters between the model and the view that convert a model attribute to a view attribute (and vice versa). For example,
452
+ * `<imageBlock src='foo.jpg'></imageBlock>` is converted to `<img src='foo.jpg'></img>` (the same attribute key and value).
453
+ * This type of converters is intended to be used with {@link module:engine/model/element~Element model element} nodes.
454
+ * To convert text attributes {@link module:engine/conversion/conversion~Conversion#attributeToElement `attributeToElement converter`}
455
+ * should be set up.
456
+ *
457
+ * // A simple conversion from the `source` model attribute to the `src` view attribute (and vice versa).
458
+ * editor.conversion.attributeToAttribute( { model: 'source', view: 'src' } );
459
+ *
460
+ * // Attribute values are strictly specified.
461
+ * editor.conversion.attributeToAttribute( {
462
+ * model: {
463
+ * name: 'imageInline',
464
+ * key: 'aside',
465
+ * values: [ 'aside' ]
466
+ * },
467
+ * view: {
468
+ * aside: {
469
+ * name: 'img',
470
+ * key: 'class',
471
+ * value: [ 'aside', 'half-size' ]
472
+ * }
473
+ * }
474
+ * } );
475
+ *
476
+ * // Set the style attribute.
477
+ * editor.conversion.attributeToAttribute( {
478
+ * model: {
479
+ * name: 'imageInline',
480
+ * key: 'aside',
481
+ * values: [ 'aside' ]
482
+ * },
483
+ * view: {
484
+ * aside: {
485
+ * name: 'img',
486
+ * key: 'style',
487
+ * value: {
488
+ * float: 'right',
489
+ * width: '50%',
490
+ * margin: '5px'
491
+ * }
492
+ * }
493
+ * }
494
+ * } );
495
+ *
496
+ * // Conversion from and to a model attribute key whose value is an enum (`align=right|center`).
497
+ * // Use `upcastAlso` to define other view elements that should also be converted to the `align=right` attribute.
498
+ * editor.conversion.attributeToAttribute( {
499
+ * model: {
500
+ * key: 'align',
501
+ * values: [ 'right', 'center' ]
502
+ * },
503
+ * view: {
504
+ * right: {
505
+ * key: 'class',
506
+ * value: 'align-right'
507
+ * },
508
+ * center: {
509
+ * key: 'class',
510
+ * value: 'align-center'
511
+ * }
512
+ * },
513
+ * upcastAlso: {
514
+ * right: {
515
+ * styles: {
516
+ * 'text-align': 'right'
517
+ * }
518
+ * },
519
+ * center: {
520
+ * styles: {
521
+ * 'text-align': 'center'
522
+ * }
523
+ * }
524
+ * }
525
+ * } );
526
+ *
527
+ * The `definition.model` parameter specifies which model attribute should be converted from and to.
528
+ * It can be a `{ key, [ values ], [ name ] }` object or a `String`, which will be treated like `{ key: definition.model }`.
529
+ * The `key` property is the model attribute key to convert from and to.
530
+ * The `values` are the possible model attribute values. If `values` is not set, the model attribute value will be the same as the
531
+ * view attribute value.
532
+ * If `name` is set, the conversion will be set up only for model elements with the given name.
533
+ *
534
+ * The `definition.view` parameter specifies which view attribute should be converted from and to.
535
+ * It can be a `{ key, value, [ name ] }` object or a `String`, which will be treated like `{ key: definition.view }`.
536
+ * The `key` property is the view attribute key to convert from and to.
537
+ * The `value` is the view attribute value to convert from and to. If `definition.value` is not set, the view attribute value will be
538
+ * the same as the model attribute value.
539
+ * If `key` is `'class'`, `value` can be a `String` or an array of `String`s.
540
+ * If `key` is `'style'`, `value` is an object with key-value pairs.
541
+ * In other cases, `value` is a `String`.
542
+ * If `name` is set, the conversion will be set up only for model elements with the given name.
543
+ * If `definition.model.values` is set, `definition.view` is an object that assigns values from `definition.model.values`
544
+ * to `{ key, value, [ name ] }` objects.
545
+ *
546
+ * `definition.upcastAlso` specifies which other matching view elements should also be upcast to the given model configuration.
547
+ * If `definition.model.values` is set, `definition.upcastAlso` should be an object assigning values from `definition.model.values`
548
+ * to {@link module:engine/view/matcher~MatcherPattern}s or arrays of {@link module:engine/view/matcher~MatcherPattern}s.
549
+ *
550
+ * **Note:** `definition.model` and `definition.view` form should be mirrored, so the same types of parameters should
551
+ * be given in both parameters.
552
+ *
553
+ * @param {Object} definition The converter definition.
554
+ * @param {String|Object} definition.model The model attribute to convert from and to.
555
+ * @param {String|Object} definition.view The view attribute to convert from and to.
556
+ * @param {module:engine/view/matcher~MatcherPattern|Array.<module:engine/view/matcher~MatcherPattern>} [definition.upcastAlso]
557
+ * Any view element matching `definition.upcastAlso` will also be converted to the given model attribute. `definition.upcastAlso`
558
+ * is used only if `config.model.values` is specified.
559
+ */
560
+ attributeToAttribute( definition ) {
561
+ // Set up downcast converter.
562
+ this.for( 'downcast' ).attributeToAttribute( definition );
563
+
564
+ // Set up upcast converter.
565
+ for ( const { model, view } of _getAllUpcastDefinitions( definition ) ) {
566
+ this.for( 'upcast' )
567
+ .attributeToAttribute( {
568
+ view,
569
+ model
570
+ } );
571
+ }
572
+ }
573
+
574
+ /**
575
+ * Creates and caches conversion helpers for given dispatchers group.
576
+ *
577
+ * @private
578
+ * @param {Object} options
579
+ * @param {String} options.name Group name.
580
+ * @param {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
581
+ * module:engine/conversion/upcastdispatcher~UpcastDispatcher>} options.dispatchers
582
+ * @param {Boolean} options.isDowncast
583
+ */
584
+ _createConversionHelpers( { name, dispatchers, isDowncast } ) {
585
+ if ( this._helpers.has( name ) ) {
586
+ /**
587
+ * Trying to register a group name that has already been registered.
588
+ *
589
+ * @error conversion-group-exists
590
+ */
591
+ throw new CKEditorError( 'conversion-group-exists', this );
592
+ }
593
+
594
+ const helpers = isDowncast ? new DowncastHelpers( dispatchers ) : new UpcastHelpers( dispatchers );
595
+
596
+ this._helpers.set( name, helpers );
597
+ }
598
+ }
599
+
600
+ /**
601
+ * Defines how the model should be converted from and to the view.
602
+ *
603
+ * @typedef {Object} module:engine/conversion/conversion~ConverterDefinition
604
+ *
605
+ * @property {*} [model] The model conversion definition. Describes the model element or model attribute to convert. This parameter differs
606
+ * for different functions that accept `ConverterDefinition`. See the description of the function to learn how to set it.
607
+ * @property {module:engine/view/elementdefinition~ElementDefinition|Object} view The definition of the view element to convert from and
608
+ * to. If `model` describes multiple values, `view` is an object that assigns these values (`view` object keys) to view element definitions
609
+ * (`view` object values).
610
+ * @property {module:engine/view/matcher~MatcherPattern|Array.<module:engine/view/matcher~MatcherPattern>} [upcastAlso]
611
+ * Any view element matching `upcastAlso` will also be converted to the model. If `model` describes multiple values, `upcastAlso`
612
+ * is an object that assigns these values (`upcastAlso` object keys) to {@link module:engine/view/matcher~MatcherPattern}s
613
+ * (`upcastAlso` object values).
614
+ * @property {module:utils/priorities~PriorityString} [converterPriority] The converter priority.
615
+ */
616
+
617
+ // Helper function that creates a joint array out of an item passed in `definition.view` and items passed in
618
+ // `definition.upcastAlso`.
619
+ //
620
+ // @param {module:engine/conversion/conversion~ConverterDefinition} definition
621
+ // @returns {Array} Array containing view definitions.
622
+ function* _getAllUpcastDefinitions( definition ) {
623
+ if ( definition.model.values ) {
624
+ for ( const value of definition.model.values ) {
625
+ const model = { key: definition.model.key, value };
626
+ const view = definition.view[ value ];
627
+ const upcastAlso = definition.upcastAlso ? definition.upcastAlso[ value ] : undefined;
628
+
629
+ yield* _getUpcastDefinition( model, view, upcastAlso );
630
+ }
631
+ } else {
632
+ yield* _getUpcastDefinition( definition.model, definition.view, definition.upcastAlso );
633
+ }
634
+ }
635
+
636
+ function* _getUpcastDefinition( model, view, upcastAlso ) {
637
+ yield { model, view };
638
+
639
+ if ( upcastAlso ) {
640
+ for ( const upcastAlsoItem of toArray( upcastAlso ) ) {
641
+ yield { model, view: upcastAlsoItem };
642
+ }
643
+ }
644
+ }
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module engine/conversion/conversionhelpers
8
+ */
9
+
10
+ /**
11
+ * Base class for conversion helpers.
12
+ */
13
+ export default class ConversionHelpers {
14
+ /**
15
+ * Creates a conversion helpers instance.
16
+ *
17
+ * @param {Array.<module:engine/conversion/downcastdispatcher~DowncastDispatcher|
18
+ * module:engine/conversion/upcastdispatcher~UpcastDispatcher>} dispatchers
19
+ */
20
+ constructor( dispatchers ) {
21
+ this._dispatchers = dispatchers;
22
+ }
23
+
24
+ /**
25
+ * Registers a conversion helper.
26
+ *
27
+ * **Note**: See full usage example in the `{@link module:engine/conversion/conversion~Conversion#for conversion.for()}`
28
+ * method description.
29
+ *
30
+ * @param {Function} conversionHelper The function to be called on event.
31
+ * @returns {module:engine/conversion/downcasthelpers~DowncastHelpers|module:engine/conversion/upcasthelpers~UpcastHelpers}
32
+ */
33
+ add( conversionHelper ) {
34
+ for ( const dispatcher of this._dispatchers ) {
35
+ conversionHelper( dispatcher );
36
+ }
37
+
38
+ return this;
39
+ }
40
+ }