@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,22 +2,17 @@
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
  import Matcher from '../view/matcher';
7
6
  import ConversionHelpers from './conversionhelpers';
8
-
9
7
  import { cloneDeep } from 'lodash-es';
10
-
11
8
  import priorities from '@ckeditor/ckeditor5-utils/src/priorities';
12
9
  import { isParagraphable, wrapInParagraph } from '../model/utils/autoparagraphing';
13
-
14
10
  /**
15
11
  * Contains the {@link module:engine/view/view view} to {@link module:engine/model/model model} converters for
16
12
  * {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher}.
17
13
  *
18
14
  * @module engine/conversion/upcasthelpers
19
15
  */
20
-
21
16
  /**
22
17
  * Upcast conversion helper functions.
23
18
  *
@@ -26,399 +21,394 @@ import { isParagraphable, wrapInParagraph } from '../model/utils/autoparagraphin
26
21
  * @extends module:engine/conversion/conversionhelpers~ConversionHelpers
27
22
  */
28
23
  export default class UpcastHelpers extends ConversionHelpers {
29
- /**
30
- * View element to model element conversion helper.
31
- *
32
- * This conversion results in creating a model element. For example,
33
- * view `<p>Foo</p>` becomes `<paragraph>Foo</paragraph>` in the model.
34
- *
35
- * Keep in mind that the element will be inserted only if it is allowed
36
- * by {@link module:engine/model/schema~Schema schema} configuration.
37
- *
38
- * editor.conversion.for( 'upcast' ).elementToElement( {
39
- * view: 'p',
40
- * model: 'paragraph'
41
- * } );
42
- *
43
- * editor.conversion.for( 'upcast' ).elementToElement( {
44
- * view: 'p',
45
- * model: 'paragraph',
46
- * converterPriority: 'high'
47
- * } );
48
- *
49
- * editor.conversion.for( 'upcast' ).elementToElement( {
50
- * view: {
51
- * name: 'p',
52
- * classes: 'fancy'
53
- * },
54
- * model: 'fancyParagraph'
55
- * } );
56
- *
57
- * editor.conversion.for( 'upcast' ).elementToElement( {
58
- * view: {
59
- * name: 'p',
60
- * classes: 'heading'
61
- * },
62
- * model: ( viewElement, conversionApi ) => {
63
- * const modelWriter = conversionApi.writer;
64
- *
65
- * return modelWriter.createElement( 'heading', { level: viewElement.getAttribute( 'data-level' ) } );
66
- * }
67
- * } );
68
- *
69
- * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
70
- * to the conversion process.
71
- *
72
- * @method #elementToElement
73
- * @param {Object} config Conversion configuration.
74
- * @param {module:engine/view/matcher~MatcherPattern} [config.view] Pattern matching all view elements which should be converted. If not
75
- * set, the converter will fire for every view element.
76
- * @param {String|module:engine/model/element~Element|Function} config.model Name of the model element, a model element instance or a
77
- * function that takes a view element and {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API}
78
- * and returns a model element. The model element will be inserted in the model.
79
- * @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
80
- * @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
81
- */
82
- elementToElement( config ) {
83
- return this.add( upcastElementToElement( config ) );
84
- }
85
-
86
- /**
87
- * View element to model attribute conversion helper.
88
- *
89
- * This conversion results in setting an attribute on a model node. For example, view `<strong>Foo</strong>` becomes
90
- * `Foo` {@link module:engine/model/text~Text model text node} with `bold` attribute set to `true`.
91
- *
92
- * This helper is meant to set a model attribute on all the elements that are inside the converted element:
93
- *
94
- * <strong>Foo</strong> --> <strong><p>Foo</p></strong> --> <paragraph><$text bold="true">Foo</$text></paragraph>
95
- *
96
- * Above is a sample of HTML code, that goes through autoparagraphing (first step) and then is converted (second step).
97
- * Even though `<strong>` is over `<p>` element, `bold="true"` was added to the text. See
98
- * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#attributeToAttribute} for comparison.
99
- *
100
- * Keep in mind that the attribute will be set only if it is allowed by {@link module:engine/model/schema~Schema schema} configuration.
101
- *
102
- * editor.conversion.for( 'upcast' ).elementToAttribute( {
103
- * view: 'strong',
104
- * model: 'bold'
105
- * } );
106
- *
107
- * editor.conversion.for( 'upcast' ).elementToAttribute( {
108
- * view: 'strong',
109
- * model: 'bold',
110
- * converterPriority: 'high'
111
- * } );
112
- *
113
- * editor.conversion.for( 'upcast' ).elementToAttribute( {
114
- * view: {
115
- * name: 'span',
116
- * classes: 'bold'
117
- * },
118
- * model: 'bold'
119
- * } );
120
- *
121
- * editor.conversion.for( 'upcast' ).elementToAttribute( {
122
- * view: {
123
- * name: 'span',
124
- * classes: [ 'styled', 'styled-dark' ]
125
- * },
126
- * model: {
127
- * key: 'styled',
128
- * value: 'dark'
129
- * }
130
- * } );
131
- *
132
- * editor.conversion.for( 'upcast' ).elementToAttribute( {
133
- * view: {
134
- * name: 'span',
135
- * styles: {
136
- * 'font-size': /[\s\S]+/
137
- * }
138
- * },
139
- * model: {
140
- * key: 'fontSize',
141
- * value: ( viewElement, conversionApi ) => {
142
- * const fontSize = viewElement.getStyle( 'font-size' );
143
- * const value = fontSize.substr( 0, fontSize.length - 2 );
144
- *
145
- * if ( value <= 10 ) {
146
- * return 'small';
147
- * } else if ( value > 12 ) {
148
- * return 'big';
149
- * }
150
- *
151
- * return null;
152
- * }
153
- * }
154
- * } );
155
- *
156
- * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
157
- * to the conversion process.
158
- *
159
- * @method #elementToAttribute
160
- * @param {Object} config Conversion configuration.
161
- * @param {module:engine/view/matcher~MatcherPattern} config.view Pattern matching all view elements which should be converted.
162
- * @param {String|Object} config.model Model attribute key or an object with `key` and `value` properties, describing
163
- * the model attribute. `value` property may be set as a function that takes a view element and
164
- * {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API} and returns the value.
165
- * If `String` is given, the model attribute value will be set to `true`.
166
- * @param {module:utils/priorities~PriorityString} [config.converterPriority='low'] Converter priority.
167
- * @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
168
- */
169
- elementToAttribute( config ) {
170
- return this.add( upcastElementToAttribute( config ) );
171
- }
172
-
173
- /**
174
- * View attribute to model attribute conversion helper.
175
- *
176
- * This conversion results in setting an attribute on a model node. For example, view `<img src="foo.jpg"></img>` becomes
177
- * `<imageBlock source="foo.jpg"></imageBlock>` in the model.
178
- *
179
- * This helper is meant to convert view attributes from view elements which got converted to the model, so the view attribute
180
- * is set only on the corresponding model node:
181
- *
182
- * <div class="dark"><div>foo</div></div> --> <div dark="true"><div>foo</div></div>
183
- *
184
- * Above, `class="dark"` attribute is added only to the `<div>` elements that has it. This is in contrary to
185
- * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToAttribute} which sets attributes for
186
- * all the children in the model:
187
- *
188
- * <strong>Foo</strong> --> <strong><p>Foo</p></strong> --> <paragraph><$text bold="true">Foo</$text></paragraph>
189
- *
190
- * Above is a sample of HTML code, that goes through autoparagraphing (first step) and then is converted (second step).
191
- * Even though `<strong>` is over `<p>` element, `bold="true"` was added to the text.
192
- *
193
- * Keep in mind that the attribute will be set only if it is allowed by {@link module:engine/model/schema~Schema schema} configuration.
194
- *
195
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
196
- * view: 'src',
197
- * model: 'source'
198
- * } );
199
- *
200
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
201
- * view: { key: 'src' },
202
- * model: 'source'
203
- * } );
204
- *
205
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
206
- * view: { key: 'src' },
207
- * model: 'source',
208
- * converterPriority: 'normal'
209
- * } );
210
- *
211
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
212
- * view: {
213
- * key: 'data-style',
214
- * value: /[\s\S]+/
215
- * },
216
- * model: 'styled'
217
- * } );
218
- *
219
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
220
- * view: {
221
- * name: 'img',
222
- * key: 'class',
223
- * value: 'styled-dark'
224
- * },
225
- * model: {
226
- * key: 'styled',
227
- * value: 'dark'
228
- * }
229
- * } );
230
- *
231
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
232
- * view: {
233
- * key: 'class',
234
- * value: /styled-[\S]+/
235
- * },
236
- * model: {
237
- * key: 'styled'
238
- * value: ( viewElement, conversionApi ) => {
239
- * const regexp = /styled-([\S]+)/;
240
- * const match = viewElement.getAttribute( 'class' ).match( regexp );
241
- *
242
- * return match[ 1 ];
243
- * }
244
- * }
245
- * } );
246
- *
247
- * Converting styles works a bit differently as it requires `view.styles` to be an object and by default
248
- * a model attribute will be set to `true` by such a converter. You can set the model attribute to any value by providing the `value`
249
- * callback that returns the desired value.
250
- *
251
- * // Default conversion of font-weight style will result in setting bold attribute to true.
252
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
253
- * view: {
254
- * styles: {
255
- * 'font-weight': 'bold'
256
- * }
257
- * },
258
- * model: 'bold'
259
- * } );
260
- *
261
- * // This converter will pass any style value to the `lineHeight` model attribute.
262
- * editor.conversion.for( 'upcast' ).attributeToAttribute( {
263
- * view: {
264
- * styles: {
265
- * 'line-height': /[\s\S]+/
266
- * }
267
- * },
268
- * model: {
269
- * key: 'lineHeight',
270
- * value: ( viewElement, conversionApi ) => viewElement.getStyle( 'line-height' )
271
- * }
272
- * } );
273
- *
274
- * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
275
- * to the conversion process.
276
- *
277
- * @method #attributeToAttribute
278
- * @param {Object} config Conversion configuration.
279
- * @param {String|Object} config.view Specifies which view attribute will be converted. If a `String` is passed,
280
- * attributes with given key will be converted. If an `Object` is passed, it must have a required `key` property,
281
- * specifying view attribute key, and may have an optional `value` property, specifying view attribute value and optional `name`
282
- * property specifying a view element name from/on which the attribute should be converted. `value` can be given as a `String`,
283
- * a `RegExp` or a function callback, that takes view attribute value as the only parameter and returns `Boolean`.
284
- * @param {String|Object} config.model Model attribute key or an object with `key` and `value` properties, describing
285
- * the model attribute. `value` property may be set as a function that takes a view element and
286
- * {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API} and returns the value.
287
- * If `String` is given, the model attribute value will be same as view attribute value.
288
- * @param {module:utils/priorities~PriorityString} [config.converterPriority='low'] Converter priority.
289
- * @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
290
- */
291
- attributeToAttribute( config ) {
292
- return this.add( upcastAttributeToAttribute( config ) );
293
- }
294
-
295
- /**
296
- * View element to model marker conversion helper.
297
- *
298
- * This conversion results in creating a model marker. For example, if the marker was stored in a view as an element:
299
- * `<p>Fo<span data-marker="comment" data-comment-id="7"></span>o</p><p>B<span data-marker="comment" data-comment-id="7"></span>ar</p>`,
300
- * after the conversion is done, the marker will be available in
301
- * {@link module:engine/model/model~Model#markers model document markers}.
302
- *
303
- * **Note**: When this helper is used in the data upcast in combination with
304
- * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToData `#markerToData()`} in the data downcast,
305
- * then invalid HTML code (e.g. a span between table cells) may be produced by the latter converter.
306
- *
307
- * In most of the cases, the {@link #dataToMarker} should be used instead.
308
- *
309
- * editor.conversion.for( 'upcast' ).elementToMarker( {
310
- * view: 'marker-search',
311
- * model: 'search'
312
- * } );
313
- *
314
- * editor.conversion.for( 'upcast' ).elementToMarker( {
315
- * view: 'marker-search',
316
- * model: 'search',
317
- * converterPriority: 'high'
318
- * } );
319
- *
320
- * editor.conversion.for( 'upcast' ).elementToMarker( {
321
- * view: 'marker-search',
322
- * model: ( viewElement, conversionApi ) => 'comment:' + viewElement.getAttribute( 'data-comment-id' )
323
- * } );
324
- *
325
- * editor.conversion.for( 'upcast' ).elementToMarker( {
326
- * view: {
327
- * name: 'span',
328
- * attributes: {
329
- * 'data-marker': 'search'
330
- * }
331
- * },
332
- * model: 'search'
333
- * } );
334
- *
335
- * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
336
- * to the conversion process.
337
- *
338
- * @method #elementToMarker
339
- * @param {Object} config Conversion configuration.
340
- * @param {module:engine/view/matcher~MatcherPattern} config.view Pattern matching all view elements which should be converted.
341
- * @param {String|Function} config.model Name of the model marker, or a function that takes a view element and returns
342
- * a model marker name.
343
- * @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
344
- * @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
345
- */
346
- elementToMarker( config ) {
347
- return this.add( upcastElementToMarker( config ) );
348
- }
349
-
350
- /**
351
- * View-to-model marker conversion helper.
352
- *
353
- * Converts view data created by {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToData `#markerToData()`}
354
- * back to a model marker.
355
- *
356
- * This converter looks for specific view elements and view attributes that mark marker boundaries. See
357
- * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToData `#markerToData()`} to learn what view data
358
- * is expected by this converter.
359
- *
360
- * The `config.view` property is equal to the marker group name to convert.
361
- *
362
- * By default, this converter creates markers with the `group:name` name convention (to match the default `markerToData` conversion).
363
- *
364
- * The conversion configuration can take a function that will generate a marker name.
365
- * If such function is set as the `config.model` parameter, it is passed the `name` part from the view element or attribute and it is
366
- * expected to return a string with the marker name.
367
- *
368
- * Basic usage:
369
- *
370
- * // Using the default conversion.
371
- * // In this case, all markers from the `comment` group will be converted.
372
- * // The conversion will look for `<comment-start>` and `<comment-end>` tags and
373
- * // `data-comment-start-before`, `data-comment-start-after`,
374
- * // `data-comment-end-before` and `data-comment-end-after` attributes.
375
- * editor.conversion.for( 'upcast' ).dataToMarker( {
376
- * view: 'comment'
377
- * } );
378
- *
379
- * An example of a model that may be generated by this conversion:
380
- *
381
- * // View:
382
- * <p>Foo<comment-start name="commentId:uid"></comment-start>bar</p>
383
- * <figure data-comment-end-after="commentId:uid" class="image"><img src="abc.jpg" /></figure>
384
- *
385
- * // Model:
386
- * <paragraph>Foo[bar</paragraph>
387
- * <imageBlock src="abc.jpg"></imageBlock>]
388
- *
389
- * Where `[]` are boundaries of a marker that will receive the `comment:commentId:uid` name.
390
- *
391
- * Other examples of usage:
392
- *
393
- * // Using a custom function which is the same as the default conversion:
394
- * editor.conversion.for( 'upcast' ).dataToMarker( {
395
- * view: 'comment',
396
- * model: ( name, conversionApi ) => 'comment:' + name,
397
- * } );
398
- *
399
- * // Using the converter priority:
400
- * editor.conversion.for( 'upcast' ).dataToMarker( {
401
- * view: 'comment',
402
- * model: ( name, conversionApi ) => 'comment:' + name,
403
- * converterPriority: 'high'
404
- * } );
405
- *
406
- * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
407
- * to the conversion process.
408
- *
409
- * @method #dataToMarker
410
- * @param {Object} config Conversion configuration.
411
- * @param {String} config.view The marker group name to convert.
412
- * @param {Function} [config.model] A function that takes the `name` part from the view element or attribute and
413
- * {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API} and returns the marker name.
414
- * @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
415
- * @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
416
- */
417
- dataToMarker( config ) {
418
- return this.add( upcastDataToMarker( config ) );
419
- }
24
+ /**
25
+ * View element to model element conversion helper.
26
+ *
27
+ * This conversion results in creating a model element. For example,
28
+ * view `<p>Foo</p>` becomes `<paragraph>Foo</paragraph>` in the model.
29
+ *
30
+ * Keep in mind that the element will be inserted only if it is allowed
31
+ * by {@link module:engine/model/schema~Schema schema} configuration.
32
+ *
33
+ * editor.conversion.for( 'upcast' ).elementToElement( {
34
+ * view: 'p',
35
+ * model: 'paragraph'
36
+ * } );
37
+ *
38
+ * editor.conversion.for( 'upcast' ).elementToElement( {
39
+ * view: 'p',
40
+ * model: 'paragraph',
41
+ * converterPriority: 'high'
42
+ * } );
43
+ *
44
+ * editor.conversion.for( 'upcast' ).elementToElement( {
45
+ * view: {
46
+ * name: 'p',
47
+ * classes: 'fancy'
48
+ * },
49
+ * model: 'fancyParagraph'
50
+ * } );
51
+ *
52
+ * editor.conversion.for( 'upcast' ).elementToElement( {
53
+ * view: {
54
+ * name: 'p',
55
+ * classes: 'heading'
56
+ * },
57
+ * model: ( viewElement, conversionApi ) => {
58
+ * const modelWriter = conversionApi.writer;
59
+ *
60
+ * return modelWriter.createElement( 'heading', { level: viewElement.getAttribute( 'data-level' ) } );
61
+ * }
62
+ * } );
63
+ *
64
+ * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
65
+ * to the conversion process.
66
+ *
67
+ * @method #elementToElement
68
+ * @param {Object} config Conversion configuration.
69
+ * @param {module:engine/view/matcher~MatcherPattern} [config.view] Pattern matching all view elements which should be converted. If not
70
+ * set, the converter will fire for every view element.
71
+ * @param {String|module:engine/model/element~Element|Function} config.model Name of the model element, a model element instance or a
72
+ * function that takes a view element and {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API}
73
+ * and returns a model element. The model element will be inserted in the model.
74
+ * @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
75
+ * @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
76
+ */
77
+ elementToElement(config) {
78
+ return this.add(upcastElementToElement(config));
79
+ }
80
+ /**
81
+ * View element to model attribute conversion helper.
82
+ *
83
+ * This conversion results in setting an attribute on a model node. For example, view `<strong>Foo</strong>` becomes
84
+ * `Foo` {@link module:engine/model/text~Text model text node} with `bold` attribute set to `true`.
85
+ *
86
+ * This helper is meant to set a model attribute on all the elements that are inside the converted element:
87
+ *
88
+ * <strong>Foo</strong> --> <strong><p>Foo</p></strong> --> <paragraph><$text bold="true">Foo</$text></paragraph>
89
+ *
90
+ * Above is a sample of HTML code, that goes through autoparagraphing (first step) and then is converted (second step).
91
+ * Even though `<strong>` is over `<p>` element, `bold="true"` was added to the text. See
92
+ * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#attributeToAttribute} for comparison.
93
+ *
94
+ * Keep in mind that the attribute will be set only if it is allowed by {@link module:engine/model/schema~Schema schema} configuration.
95
+ *
96
+ * editor.conversion.for( 'upcast' ).elementToAttribute( {
97
+ * view: 'strong',
98
+ * model: 'bold'
99
+ * } );
100
+ *
101
+ * editor.conversion.for( 'upcast' ).elementToAttribute( {
102
+ * view: 'strong',
103
+ * model: 'bold',
104
+ * converterPriority: 'high'
105
+ * } );
106
+ *
107
+ * editor.conversion.for( 'upcast' ).elementToAttribute( {
108
+ * view: {
109
+ * name: 'span',
110
+ * classes: 'bold'
111
+ * },
112
+ * model: 'bold'
113
+ * } );
114
+ *
115
+ * editor.conversion.for( 'upcast' ).elementToAttribute( {
116
+ * view: {
117
+ * name: 'span',
118
+ * classes: [ 'styled', 'styled-dark' ]
119
+ * },
120
+ * model: {
121
+ * key: 'styled',
122
+ * value: 'dark'
123
+ * }
124
+ * } );
125
+ *
126
+ * editor.conversion.for( 'upcast' ).elementToAttribute( {
127
+ * view: {
128
+ * name: 'span',
129
+ * styles: {
130
+ * 'font-size': /[\s\S]+/
131
+ * }
132
+ * },
133
+ * model: {
134
+ * key: 'fontSize',
135
+ * value: ( viewElement, conversionApi ) => {
136
+ * const fontSize = viewElement.getStyle( 'font-size' );
137
+ * const value = fontSize.substr( 0, fontSize.length - 2 );
138
+ *
139
+ * if ( value <= 10 ) {
140
+ * return 'small';
141
+ * } else if ( value > 12 ) {
142
+ * return 'big';
143
+ * }
144
+ *
145
+ * return null;
146
+ * }
147
+ * }
148
+ * } );
149
+ *
150
+ * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
151
+ * to the conversion process.
152
+ *
153
+ * @method #elementToAttribute
154
+ * @param {Object} config Conversion configuration.
155
+ * @param {module:engine/view/matcher~MatcherPattern} config.view Pattern matching all view elements which should be converted.
156
+ * @param {String|Object} config.model Model attribute key or an object with `key` and `value` properties, describing
157
+ * the model attribute. `value` property may be set as a function that takes a view element and
158
+ * {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API} and returns the value.
159
+ * If `String` is given, the model attribute value will be set to `true`.
160
+ * @param {module:utils/priorities~PriorityString} [config.converterPriority='low'] Converter priority.
161
+ * @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
162
+ */
163
+ elementToAttribute(config) {
164
+ return this.add(upcastElementToAttribute(config));
165
+ }
166
+ /**
167
+ * View attribute to model attribute conversion helper.
168
+ *
169
+ * This conversion results in setting an attribute on a model node. For example, view `<img src="foo.jpg"></img>` becomes
170
+ * `<imageBlock source="foo.jpg"></imageBlock>` in the model.
171
+ *
172
+ * This helper is meant to convert view attributes from view elements which got converted to the model, so the view attribute
173
+ * is set only on the corresponding model node:
174
+ *
175
+ * <div class="dark"><div>foo</div></div> --> <div dark="true"><div>foo</div></div>
176
+ *
177
+ * Above, `class="dark"` attribute is added only to the `<div>` elements that has it. This is in contrary to
178
+ * {@link module:engine/conversion/upcasthelpers~UpcastHelpers#elementToAttribute} which sets attributes for
179
+ * all the children in the model:
180
+ *
181
+ * <strong>Foo</strong> --> <strong><p>Foo</p></strong> --> <paragraph><$text bold="true">Foo</$text></paragraph>
182
+ *
183
+ * Above is a sample of HTML code, that goes through autoparagraphing (first step) and then is converted (second step).
184
+ * Even though `<strong>` is over `<p>` element, `bold="true"` was added to the text.
185
+ *
186
+ * Keep in mind that the attribute will be set only if it is allowed by {@link module:engine/model/schema~Schema schema} configuration.
187
+ *
188
+ * editor.conversion.for( 'upcast' ).attributeToAttribute( {
189
+ * view: 'src',
190
+ * model: 'source'
191
+ * } );
192
+ *
193
+ * editor.conversion.for( 'upcast' ).attributeToAttribute( {
194
+ * view: { key: 'src' },
195
+ * model: 'source'
196
+ * } );
197
+ *
198
+ * editor.conversion.for( 'upcast' ).attributeToAttribute( {
199
+ * view: { key: 'src' },
200
+ * model: 'source',
201
+ * converterPriority: 'normal'
202
+ * } );
203
+ *
204
+ * editor.conversion.for( 'upcast' ).attributeToAttribute( {
205
+ * view: {
206
+ * key: 'data-style',
207
+ * value: /[\s\S]+/
208
+ * },
209
+ * model: 'styled'
210
+ * } );
211
+ *
212
+ * editor.conversion.for( 'upcast' ).attributeToAttribute( {
213
+ * view: {
214
+ * name: 'img',
215
+ * key: 'class',
216
+ * value: 'styled-dark'
217
+ * },
218
+ * model: {
219
+ * key: 'styled',
220
+ * value: 'dark'
221
+ * }
222
+ * } );
223
+ *
224
+ * editor.conversion.for( 'upcast' ).attributeToAttribute( {
225
+ * view: {
226
+ * key: 'class',
227
+ * value: /styled-[\S]+/
228
+ * },
229
+ * model: {
230
+ * key: 'styled'
231
+ * value: ( viewElement, conversionApi ) => {
232
+ * const regexp = /styled-([\S]+)/;
233
+ * const match = viewElement.getAttribute( 'class' ).match( regexp );
234
+ *
235
+ * return match[ 1 ];
236
+ * }
237
+ * }
238
+ * } );
239
+ *
240
+ * Converting styles works a bit differently as it requires `view.styles` to be an object and by default
241
+ * a model attribute will be set to `true` by such a converter. You can set the model attribute to any value by providing the `value`
242
+ * callback that returns the desired value.
243
+ *
244
+ * // Default conversion of font-weight style will result in setting bold attribute to true.
245
+ * editor.conversion.for( 'upcast' ).attributeToAttribute( {
246
+ * view: {
247
+ * styles: {
248
+ * 'font-weight': 'bold'
249
+ * }
250
+ * },
251
+ * model: 'bold'
252
+ * } );
253
+ *
254
+ * // This converter will pass any style value to the `lineHeight` model attribute.
255
+ * editor.conversion.for( 'upcast' ).attributeToAttribute( {
256
+ * view: {
257
+ * styles: {
258
+ * 'line-height': /[\s\S]+/
259
+ * }
260
+ * },
261
+ * model: {
262
+ * key: 'lineHeight',
263
+ * value: ( viewElement, conversionApi ) => viewElement.getStyle( 'line-height' )
264
+ * }
265
+ * } );
266
+ *
267
+ * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
268
+ * to the conversion process.
269
+ *
270
+ * @method #attributeToAttribute
271
+ * @param {Object} config Conversion configuration.
272
+ * @param {String|Object} config.view Specifies which view attribute will be converted. If a `String` is passed,
273
+ * attributes with given key will be converted. If an `Object` is passed, it must have a required `key` property,
274
+ * specifying view attribute key, and may have an optional `value` property, specifying view attribute value and optional `name`
275
+ * property specifying a view element name from/on which the attribute should be converted. `value` can be given as a `String`,
276
+ * a `RegExp` or a function callback, that takes view attribute value as the only parameter and returns `Boolean`.
277
+ * @param {String|Object} config.model Model attribute key or an object with `key` and `value` properties, describing
278
+ * the model attribute. `value` property may be set as a function that takes a view element and
279
+ * {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API} and returns the value.
280
+ * If `String` is given, the model attribute value will be same as view attribute value.
281
+ * @param {module:utils/priorities~PriorityString} [config.converterPriority='low'] Converter priority.
282
+ * @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
283
+ */
284
+ attributeToAttribute(config) {
285
+ return this.add(upcastAttributeToAttribute(config));
286
+ }
287
+ /**
288
+ * View element to model marker conversion helper.
289
+ *
290
+ * This conversion results in creating a model marker. For example, if the marker was stored in a view as an element:
291
+ * `<p>Fo<span data-marker="comment" data-comment-id="7"></span>o</p><p>B<span data-marker="comment" data-comment-id="7"></span>ar</p>`,
292
+ * after the conversion is done, the marker will be available in
293
+ * {@link module:engine/model/model~Model#markers model document markers}.
294
+ *
295
+ * **Note**: When this helper is used in the data upcast in combination with
296
+ * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToData `#markerToData()`} in the data downcast,
297
+ * then invalid HTML code (e.g. a span between table cells) may be produced by the latter converter.
298
+ *
299
+ * In most of the cases, the {@link #dataToMarker} should be used instead.
300
+ *
301
+ * editor.conversion.for( 'upcast' ).elementToMarker( {
302
+ * view: 'marker-search',
303
+ * model: 'search'
304
+ * } );
305
+ *
306
+ * editor.conversion.for( 'upcast' ).elementToMarker( {
307
+ * view: 'marker-search',
308
+ * model: 'search',
309
+ * converterPriority: 'high'
310
+ * } );
311
+ *
312
+ * editor.conversion.for( 'upcast' ).elementToMarker( {
313
+ * view: 'marker-search',
314
+ * model: ( viewElement, conversionApi ) => 'comment:' + viewElement.getAttribute( 'data-comment-id' )
315
+ * } );
316
+ *
317
+ * editor.conversion.for( 'upcast' ).elementToMarker( {
318
+ * view: {
319
+ * name: 'span',
320
+ * attributes: {
321
+ * 'data-marker': 'search'
322
+ * }
323
+ * },
324
+ * model: 'search'
325
+ * } );
326
+ *
327
+ * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
328
+ * to the conversion process.
329
+ *
330
+ * @method #elementToMarker
331
+ * @param {Object} config Conversion configuration.
332
+ * @param {module:engine/view/matcher~MatcherPattern} config.view Pattern matching all view elements which should be converted.
333
+ * @param {String|Function} config.model Name of the model marker, or a function that takes a view element and returns
334
+ * a model marker name.
335
+ * @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
336
+ * @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
337
+ */
338
+ elementToMarker(config) {
339
+ return this.add(upcastElementToMarker(config));
340
+ }
341
+ /**
342
+ * View-to-model marker conversion helper.
343
+ *
344
+ * Converts view data created by {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToData `#markerToData()`}
345
+ * back to a model marker.
346
+ *
347
+ * This converter looks for specific view elements and view attributes that mark marker boundaries. See
348
+ * {@link module:engine/conversion/downcasthelpers~DowncastHelpers#markerToData `#markerToData()`} to learn what view data
349
+ * is expected by this converter.
350
+ *
351
+ * The `config.view` property is equal to the marker group name to convert.
352
+ *
353
+ * By default, this converter creates markers with the `group:name` name convention (to match the default `markerToData` conversion).
354
+ *
355
+ * The conversion configuration can take a function that will generate a marker name.
356
+ * If such function is set as the `config.model` parameter, it is passed the `name` part from the view element or attribute and it is
357
+ * expected to return a string with the marker name.
358
+ *
359
+ * Basic usage:
360
+ *
361
+ * // Using the default conversion.
362
+ * // In this case, all markers from the `comment` group will be converted.
363
+ * // The conversion will look for `<comment-start>` and `<comment-end>` tags and
364
+ * // `data-comment-start-before`, `data-comment-start-after`,
365
+ * // `data-comment-end-before` and `data-comment-end-after` attributes.
366
+ * editor.conversion.for( 'upcast' ).dataToMarker( {
367
+ * view: 'comment'
368
+ * } );
369
+ *
370
+ * An example of a model that may be generated by this conversion:
371
+ *
372
+ * // View:
373
+ * <p>Foo<comment-start name="commentId:uid"></comment-start>bar</p>
374
+ * <figure data-comment-end-after="commentId:uid" class="image"><img src="abc.jpg" /></figure>
375
+ *
376
+ * // Model:
377
+ * <paragraph>Foo[bar</paragraph>
378
+ * <imageBlock src="abc.jpg"></imageBlock>]
379
+ *
380
+ * Where `[]` are boundaries of a marker that will receive the `comment:commentId:uid` name.
381
+ *
382
+ * Other examples of usage:
383
+ *
384
+ * // Using a custom function which is the same as the default conversion:
385
+ * editor.conversion.for( 'upcast' ).dataToMarker( {
386
+ * view: 'comment',
387
+ * model: ( name, conversionApi ) => 'comment:' + name,
388
+ * } );
389
+ *
390
+ * // Using the converter priority:
391
+ * editor.conversion.for( 'upcast' ).dataToMarker( {
392
+ * view: 'comment',
393
+ * model: ( name, conversionApi ) => 'comment:' + name,
394
+ * converterPriority: 'high'
395
+ * } );
396
+ *
397
+ * See {@link module:engine/conversion/conversion~Conversion#for `conversion.for()`} to learn how to add a converter
398
+ * to the conversion process.
399
+ *
400
+ * @method #dataToMarker
401
+ * @param {Object} config Conversion configuration.
402
+ * @param {String} config.view The marker group name to convert.
403
+ * @param {Function} [config.model] A function that takes the `name` part from the view element or attribute and
404
+ * {@link module:engine/conversion/upcastdispatcher~UpcastConversionApi upcast conversion API} and returns the marker name.
405
+ * @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
406
+ * @returns {module:engine/conversion/upcasthelpers~UpcastHelpers}
407
+ */
408
+ dataToMarker(config) {
409
+ return this.add(upcastDataToMarker(config));
410
+ }
420
411
  }
421
-
422
412
  /**
423
413
  * Function factory, creates a converter that converts {@link module:engine/view/documentfragment~DocumentFragment view document fragment}
424
414
  * or all children of {@link module:engine/view/element~Element} into
@@ -435,58 +425,44 @@ export default class UpcastHelpers extends ConversionHelpers {
435
425
  * {@link module:engine/model/documentfragment~DocumentFragment model fragment} with children of converted view item.
436
426
  */
437
427
  export function convertToModelFragment() {
438
- return ( evt, data, conversionApi ) => {
439
- // Second argument in `consumable.consume` is discarded for ViewDocumentFragment but is needed for ViewElement.
440
- if ( !data.modelRange && conversionApi.consumable.consume( data.viewItem, { name: true } ) ) {
441
- const { modelRange, modelCursor } = conversionApi.convertChildren( data.viewItem, data.modelCursor );
442
-
443
- data.modelRange = modelRange;
444
- data.modelCursor = modelCursor;
445
- }
446
- };
428
+ return (evt, data, conversionApi) => {
429
+ // Second argument in `consumable.consume` is discarded for ViewDocumentFragment but is needed for ViewElement.
430
+ if (!data.modelRange && conversionApi.consumable.consume(data.viewItem, { name: true })) {
431
+ const { modelRange, modelCursor } = conversionApi.convertChildren(data.viewItem, data.modelCursor);
432
+ data.modelRange = modelRange;
433
+ data.modelCursor = modelCursor;
434
+ }
435
+ };
447
436
  }
448
-
449
437
  /**
450
438
  * Function factory, creates a converter that converts {@link module:engine/view/text~Text} to {@link module:engine/model/text~Text}.
451
439
  *
452
440
  * @returns {Function} {@link module:engine/view/text~Text View text} converter.
453
441
  */
454
442
  export function convertText() {
455
- return ( evt, data, { schema, consumable, writer } ) => {
456
- let position = data.modelCursor;
457
-
458
- // When node is already converted then do nothing.
459
- if ( !consumable.test( data.viewItem ) ) {
460
- return;
461
- }
462
-
463
- if ( !schema.checkChild( position, '$text' ) ) {
464
- if ( !isParagraphable( position, '$text', schema ) ) {
465
- return;
466
- }
467
-
468
- // Do not auto-paragraph whitespaces.
469
- if ( data.viewItem.data.trim().length == 0 ) {
470
- return;
471
- }
472
-
473
- position = wrapInParagraph( position, writer );
474
- }
475
-
476
- consumable.consume( data.viewItem );
477
-
478
- const text = writer.createText( data.viewItem.data );
479
-
480
- writer.insert( text, position );
481
-
482
- data.modelRange = writer.createRange(
483
- position,
484
- position.getShiftedBy( text.offsetSize )
485
- );
486
- data.modelCursor = data.modelRange.end;
487
- };
443
+ return (evt, data, { schema, consumable, writer }) => {
444
+ let position = data.modelCursor;
445
+ // When node is already converted then do nothing.
446
+ if (!consumable.test(data.viewItem)) {
447
+ return;
448
+ }
449
+ if (!schema.checkChild(position, '$text')) {
450
+ if (!isParagraphable(position, '$text', schema)) {
451
+ return;
452
+ }
453
+ // Do not auto-paragraph whitespaces.
454
+ if (data.viewItem.data.trim().length == 0) {
455
+ return;
456
+ }
457
+ position = wrapInParagraph(position, writer);
458
+ }
459
+ consumable.consume(data.viewItem);
460
+ const text = writer.createText(data.viewItem.data);
461
+ writer.insert(text, position);
462
+ data.modelRange = writer.createRange(position, position.getShiftedBy(text.offsetSize));
463
+ data.modelCursor = data.modelRange.end;
464
+ };
488
465
  }
489
-
490
466
  /**
491
467
  * Function factory, creates a callback function which converts a {@link module:engine/view/selection~Selection
492
468
  * view selection} taken from the {@link module:engine/view/document~Document#event:selectionChange} event
@@ -501,26 +477,21 @@ export function convertText() {
501
477
  * @param {module:engine/conversion/mapper~Mapper} mapper Conversion mapper.
502
478
  * @returns {Function} {@link module:engine/view/document~Document#event:selectionChange} callback function.
503
479
  */
504
- export function convertSelectionChange( model, mapper ) {
505
- return ( evt, data ) => {
506
- const viewSelection = data.newSelection;
507
-
508
- const ranges = [];
509
-
510
- for ( const viewRange of viewSelection.getRanges() ) {
511
- ranges.push( mapper.toModelRange( viewRange ) );
512
- }
513
-
514
- const modelSelection = model.createSelection( ranges, { backward: viewSelection.isBackward } );
515
-
516
- if ( !modelSelection.isEqual( model.document.selection ) ) {
517
- model.change( writer => {
518
- writer.setSelection( modelSelection );
519
- } );
520
- }
521
- };
480
+ export function convertSelectionChange(model, mapper) {
481
+ return (evt, data) => {
482
+ const viewSelection = data.newSelection;
483
+ const ranges = [];
484
+ for (const viewRange of viewSelection.getRanges()) {
485
+ ranges.push(mapper.toModelRange(viewRange));
486
+ }
487
+ const modelSelection = model.createSelection(ranges, { backward: viewSelection.isBackward });
488
+ if (!modelSelection.isEqual(model.document.selection)) {
489
+ model.change(writer => {
490
+ writer.setSelection(modelSelection);
491
+ });
492
+ }
493
+ };
522
494
  }
523
-
524
495
  // View element to model element conversion helper.
525
496
  //
526
497
  // See {@link ~UpcastHelpers#elementToElement `.elementToElement()` upcast helper} for examples.
@@ -532,19 +503,15 @@ export function convertSelectionChange( model, mapper ) {
532
503
  // instance or a function that takes a view element and returns a model element. The model element will be inserted in the model.
533
504
  // @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
534
505
  // @returns {Function} Conversion helper.
535
- function upcastElementToElement( config ) {
536
- config = cloneDeep( config );
537
-
538
- const converter = prepareToElementConverter( config );
539
-
540
- const elementName = getViewElementNameFromConfig( config.view );
541
- const eventName = elementName ? 'element:' + elementName : 'element';
542
-
543
- return dispatcher => {
544
- dispatcher.on( eventName, converter, { priority: config.converterPriority || 'normal' } );
545
- };
506
+ function upcastElementToElement(config) {
507
+ config = cloneDeep(config);
508
+ const converter = prepareToElementConverter(config);
509
+ const elementName = getViewElementNameFromConfig(config.view);
510
+ const eventName = elementName ? `element:${elementName}` : 'element';
511
+ return (dispatcher) => {
512
+ dispatcher.on(eventName, converter, { priority: config.converterPriority || 'normal' });
513
+ };
546
514
  }
547
-
548
515
  // View element to model attribute conversion helper.
549
516
  //
550
517
  // See {@link ~UpcastHelpers#elementToAttribute `.elementToAttribute()` upcast helper} for examples.
@@ -556,21 +523,16 @@ function upcastElementToElement( config ) {
556
523
  // If `String` is given, the model attribute value will be set to `true`.
557
524
  // @param {module:utils/priorities~PriorityString} [config.converterPriority='low'] Converter priority.
558
525
  // @returns {Function} Conversion helper.
559
- function upcastElementToAttribute( config ) {
560
- config = cloneDeep( config );
561
-
562
- normalizeModelAttributeConfig( config );
563
-
564
- const converter = prepareToAttributeConverter( config, false );
565
-
566
- const elementName = getViewElementNameFromConfig( config.view );
567
- const eventName = elementName ? 'element:' + elementName : 'element';
568
-
569
- return dispatcher => {
570
- dispatcher.on( eventName, converter, { priority: config.converterPriority || 'low' } );
571
- };
526
+ function upcastElementToAttribute(config) {
527
+ config = cloneDeep(config);
528
+ normalizeModelAttributeConfig(config);
529
+ const converter = prepareToAttributeConverter(config, false);
530
+ const elementName = getViewElementNameFromConfig(config.view);
531
+ const eventName = elementName ? `element:${elementName}` : 'element';
532
+ return (dispatcher) => {
533
+ dispatcher.on(eventName, converter, { priority: config.converterPriority || 'low' });
534
+ };
572
535
  }
573
-
574
536
  // View attribute to model attribute conversion helper.
575
537
  //
576
538
  // See {@link ~UpcastHelpers#attributeToAttribute `.attributeToAttribute()` upcast helper} for examples.
@@ -586,24 +548,18 @@ function upcastElementToAttribute( config ) {
586
548
  // If `String` is given, the model attribute value will be same as view attribute value.
587
549
  // @param {module:utils/priorities~PriorityString} [config.converterPriority='low'] Converter priority.
588
550
  // @returns {Function} Conversion helper.
589
- function upcastAttributeToAttribute( config ) {
590
- config = cloneDeep( config );
591
-
592
- let viewKey = null;
593
-
594
- if ( typeof config.view == 'string' || config.view.key ) {
595
- viewKey = normalizeViewAttributeKeyValueConfig( config );
596
- }
597
-
598
- normalizeModelAttributeConfig( config, viewKey );
599
-
600
- const converter = prepareToAttributeConverter( config, true );
601
-
602
- return dispatcher => {
603
- dispatcher.on( 'element', converter, { priority: config.converterPriority || 'low' } );
604
- };
551
+ function upcastAttributeToAttribute(config) {
552
+ config = cloneDeep(config);
553
+ let viewKey = null;
554
+ if (typeof config.view == 'string' || config.view.key) {
555
+ viewKey = normalizeViewAttributeKeyValueConfig(config);
556
+ }
557
+ normalizeModelAttributeConfig(config, viewKey);
558
+ const converter = prepareToAttributeConverter(config, true);
559
+ return (dispatcher) => {
560
+ dispatcher.on('element', converter, { priority: config.converterPriority || 'low' });
561
+ };
605
562
  }
606
-
607
563
  // View element to model marker conversion helper.
608
564
  //
609
565
  // See {@link ~UpcastHelpers#elementToMarker `.elementToMarker()` upcast helper} for examples.
@@ -614,14 +570,10 @@ function upcastAttributeToAttribute( config ) {
614
570
  // a model marker name.
615
571
  // @param {module:utils/priorities~PriorityString} [config.converterPriority='normal'] Converter priority.
616
572
  // @returns {Function} Conversion helper.
617
- function upcastElementToMarker( config ) {
618
- config = cloneDeep( config );
619
-
620
- normalizeElementToMarkerConfig( config );
621
-
622
- return upcastElementToElement( config );
573
+ function upcastElementToMarker(config) {
574
+ const model = normalizeElementToMarkerModelConfig(config.model);
575
+ return upcastElementToElement({ ...config, model });
623
576
  }
624
-
625
577
  // View data to model marker conversion helper.
626
578
  //
627
579
  // See {@link ~UpcastHelpers#dataToMarker} to learn more.
@@ -631,43 +583,41 @@ function upcastElementToMarker( config ) {
631
583
  // @param {Function} [config.model]
632
584
  // @param {module:utils/priorities~PriorityString} [config.converterPriority='normal']
633
585
  // @returns {Function} Conversion helper.
634
- function upcastDataToMarker( config ) {
635
- config = cloneDeep( config );
636
-
637
- // Default conversion.
638
- if ( !config.model ) {
639
- config.model = name => {
640
- return name ? config.view + ':' + name : config.view;
641
- };
642
- }
643
-
644
- const converterStart = prepareToElementConverter( normalizeDataToMarkerConfig( config, 'start' ) );
645
- const converterEnd = prepareToElementConverter( normalizeDataToMarkerConfig( config, 'end' ) );
646
-
647
- return dispatcher => {
648
- dispatcher.on( 'element:' + config.view + '-start', converterStart, { priority: config.converterPriority || 'normal' } );
649
- dispatcher.on( 'element:' + config.view + '-end', converterEnd, { priority: config.converterPriority || 'normal' } );
650
-
651
- // Below is a hack that is needed to properly handle `converterPriority` for both elements and attributes.
652
- // Attribute conversion needs to be performed *after* element conversion.
653
- // This converter handles both element conversion and attribute conversion, which means that if a single
654
- // `config.converterPriority` is used, it will lead to problems. For example, if the `'high'` priority is used,
655
- // the attribute conversion will be performed before a lot of element upcast converters.
656
- // On the other hand, we want to support `config.converterPriority` and converter overwriting.
657
- //
658
- // To make it work, we need to do some extra processing for priority for attribute converter.
659
- // Priority `'low'` value should be the base value and then we will change it depending on `config.converterPriority` value.
660
- //
661
- // This hack probably would not be needed if attributes are upcasted separately.
662
- //
663
- const basePriority = priorities.get( 'low' );
664
- const maxPriority = priorities.get( 'highest' );
665
- const priorityFactor = priorities.get( config.converterPriority ) / maxPriority; // Number in range [ -1, 1 ].
666
-
667
- dispatcher.on( 'element', upcastAttributeToMarker( config ), { priority: basePriority + priorityFactor } );
668
- };
586
+ function upcastDataToMarker(config) {
587
+ config = cloneDeep(config);
588
+ // Default conversion.
589
+ if (!config.model) {
590
+ config.model = name => {
591
+ return name ? config.view + ':' + name : config.view;
592
+ };
593
+ }
594
+ const normalizedConfig = {
595
+ view: config.view,
596
+ model: config.model
597
+ };
598
+ const converterStart = prepareToElementConverter(normalizeDataToMarkerConfig(normalizedConfig, 'start'));
599
+ const converterEnd = prepareToElementConverter(normalizeDataToMarkerConfig(normalizedConfig, 'end'));
600
+ return (dispatcher) => {
601
+ dispatcher.on(`element:${config.view}-start`, converterStart, { priority: config.converterPriority || 'normal' });
602
+ dispatcher.on(`element:${config.view}-end`, converterEnd, { priority: config.converterPriority || 'normal' });
603
+ // Below is a hack that is needed to properly handle `converterPriority` for both elements and attributes.
604
+ // Attribute conversion needs to be performed *after* element conversion.
605
+ // This converter handles both element conversion and attribute conversion, which means that if a single
606
+ // `config.converterPriority` is used, it will lead to problems. For example, if the `'high'` priority is used,
607
+ // the attribute conversion will be performed before a lot of element upcast converters.
608
+ // On the other hand, we want to support `config.converterPriority` and converter overwriting.
609
+ //
610
+ // To make it work, we need to do some extra processing for priority for attribute converter.
611
+ // Priority `'low'` value should be the base value and then we will change it depending on `config.converterPriority` value.
612
+ //
613
+ // This hack probably would not be needed if attributes are upcasted separately.
614
+ //
615
+ const basePriority = priorities.get('low');
616
+ const maxPriority = priorities.get('highest');
617
+ const priorityFactor = priorities.get(config.converterPriority) / maxPriority; // Number in range [ -1, 1 ].
618
+ dispatcher.on('element', upcastAttributeToMarker(normalizedConfig), { priority: basePriority + priorityFactor });
619
+ };
669
620
  }
670
-
671
621
  // Function factory, returns a callback function which converts view attributes to a model marker.
672
622
  //
673
623
  // The converter looks for elements with `data-group-start-before`, `data-group-start-after`, `data-group-end-before`
@@ -678,173 +628,141 @@ function upcastDataToMarker( config ) {
678
628
  // @param {String} config.view
679
629
  // @param {Function} [config.model]
680
630
  // @returns {Function} Marker converter.
681
- function upcastAttributeToMarker( config ) {
682
- return ( evt, data, conversionApi ) => {
683
- const attrName = `data-${ config.view }`;
684
-
685
- // Check if any attribute for the given view item can be consumed before changing the conversion data
686
- // and consuming view items with these attributes.
687
- if (
688
- !conversionApi.consumable.test( data.viewItem, { attributes: attrName + '-end-after' } ) &&
689
- !conversionApi.consumable.test( data.viewItem, { attributes: attrName + '-start-after' } ) &&
690
- !conversionApi.consumable.test( data.viewItem, { attributes: attrName + '-end-before' } ) &&
691
- !conversionApi.consumable.test( data.viewItem, { attributes: attrName + '-start-before' } )
692
- ) {
693
- return;
694
- }
695
-
696
- // This converter wants to add a model element, marking a marker, before/after an element (or maybe even group of elements).
697
- // To do that, we can use `data.modelRange` which is set on an element (or a group of elements) that has been upcasted.
698
- // But, if the processed view element has not been upcasted yet (it does not have been converted), we need to
699
- // fire conversion for its children first, then we will have `data.modelRange` available.
700
- if ( !data.modelRange ) {
701
- Object.assign( data, conversionApi.convertChildren( data.viewItem, data.modelCursor ) );
702
- }
703
-
704
- if ( conversionApi.consumable.consume( data.viewItem, { attributes: attrName + '-end-after' } ) ) {
705
- addMarkerElements( data.modelRange.end, data.viewItem.getAttribute( attrName + '-end-after' ).split( ',' ) );
706
- }
707
-
708
- if ( conversionApi.consumable.consume( data.viewItem, { attributes: attrName + '-start-after' } ) ) {
709
- addMarkerElements( data.modelRange.end, data.viewItem.getAttribute( attrName + '-start-after' ).split( ',' ) );
710
- }
711
-
712
- if ( conversionApi.consumable.consume( data.viewItem, { attributes: attrName + '-end-before' } ) ) {
713
- addMarkerElements( data.modelRange.start, data.viewItem.getAttribute( attrName + '-end-before' ).split( ',' ) );
714
- }
715
-
716
- if ( conversionApi.consumable.consume( data.viewItem, { attributes: attrName + '-start-before' } ) ) {
717
- addMarkerElements( data.modelRange.start, data.viewItem.getAttribute( attrName + '-start-before' ).split( ',' ) );
718
- }
719
-
720
- function addMarkerElements( position, markerViewNames ) {
721
- for ( const markerViewName of markerViewNames ) {
722
- const markerName = config.model( markerViewName, conversionApi );
723
- const element = conversionApi.writer.createElement( '$marker', { 'data-name': markerName } );
724
-
725
- conversionApi.writer.insert( element, position );
726
-
727
- if ( data.modelCursor.isEqual( position ) ) {
728
- data.modelCursor = data.modelCursor.getShiftedBy( 1 );
729
- } else {
730
- data.modelCursor = data.modelCursor._getTransformedByInsertion( position, 1 );
731
- }
732
-
733
- data.modelRange = data.modelRange._getTransformedByInsertion( position, 1 )[ 0 ];
734
- }
735
- }
736
- };
631
+ function upcastAttributeToMarker(config) {
632
+ return (evt, data, conversionApi) => {
633
+ const attrName = `data-${config.view}`;
634
+ // Check if any attribute for the given view item can be consumed before changing the conversion data
635
+ // and consuming view items with these attributes.
636
+ if (!conversionApi.consumable.test(data.viewItem, { attributes: attrName + '-end-after' }) &&
637
+ !conversionApi.consumable.test(data.viewItem, { attributes: attrName + '-start-after' }) &&
638
+ !conversionApi.consumable.test(data.viewItem, { attributes: attrName + '-end-before' }) &&
639
+ !conversionApi.consumable.test(data.viewItem, { attributes: attrName + '-start-before' })) {
640
+ return;
641
+ }
642
+ // This converter wants to add a model element, marking a marker, before/after an element (or maybe even group of elements).
643
+ // To do that, we can use `data.modelRange` which is set on an element (or a group of elements) that has been upcasted.
644
+ // But, if the processed view element has not been upcasted yet (it does not have been converted), we need to
645
+ // fire conversion for its children first, then we will have `data.modelRange` available.
646
+ if (!data.modelRange) {
647
+ Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
648
+ }
649
+ if (conversionApi.consumable.consume(data.viewItem, { attributes: attrName + '-end-after' })) {
650
+ addMarkerElements(data.modelRange.end, data.viewItem.getAttribute(attrName + '-end-after').split(','));
651
+ }
652
+ if (conversionApi.consumable.consume(data.viewItem, { attributes: attrName + '-start-after' })) {
653
+ addMarkerElements(data.modelRange.end, data.viewItem.getAttribute(attrName + '-start-after').split(','));
654
+ }
655
+ if (conversionApi.consumable.consume(data.viewItem, { attributes: attrName + '-end-before' })) {
656
+ addMarkerElements(data.modelRange.start, data.viewItem.getAttribute(attrName + '-end-before').split(','));
657
+ }
658
+ if (conversionApi.consumable.consume(data.viewItem, { attributes: attrName + '-start-before' })) {
659
+ addMarkerElements(data.modelRange.start, data.viewItem.getAttribute(attrName + '-start-before').split(','));
660
+ }
661
+ function addMarkerElements(position, markerViewNames) {
662
+ for (const markerViewName of markerViewNames) {
663
+ const markerName = config.model(markerViewName, conversionApi);
664
+ const element = conversionApi.writer.createElement('$marker', { 'data-name': markerName });
665
+ conversionApi.writer.insert(element, position);
666
+ if (data.modelCursor.isEqual(position)) {
667
+ data.modelCursor = data.modelCursor.getShiftedBy(1);
668
+ }
669
+ else {
670
+ data.modelCursor = data.modelCursor._getTransformedByInsertion(position, 1);
671
+ }
672
+ data.modelRange = data.modelRange._getTransformedByInsertion(position, 1)[0];
673
+ }
674
+ }
675
+ };
737
676
  }
738
-
739
677
  // Helper function for from-view-element conversion. Checks if `config.view` directly specifies converted view element's name
740
678
  // and if so, returns it.
741
679
  //
742
680
  // @param {Object} config Conversion view config.
743
681
  // @returns {String|null} View element name or `null` if name is not directly set.
744
- function getViewElementNameFromConfig( viewConfig ) {
745
- if ( typeof viewConfig == 'string' ) {
746
- return viewConfig;
747
- }
748
-
749
- if ( typeof viewConfig == 'object' && typeof viewConfig.name == 'string' ) {
750
- return viewConfig.name;
751
- }
752
-
753
- return null;
682
+ function getViewElementNameFromConfig(viewConfig) {
683
+ if (typeof viewConfig == 'string') {
684
+ return viewConfig;
685
+ }
686
+ if (typeof viewConfig == 'object' && typeof viewConfig.name == 'string') {
687
+ return viewConfig.name;
688
+ }
689
+ return null;
754
690
  }
755
-
756
691
  // Helper for to-model-element conversion. Takes a config object and returns a proper converter function.
757
692
  //
758
693
  // @param {Object} config Conversion configuration.
759
694
  // @returns {Function} View to model converter.
760
- function prepareToElementConverter( config ) {
761
- const matcher = new Matcher( config.view );
762
-
763
- return ( evt, data, conversionApi ) => {
764
- const matcherResult = matcher.match( data.viewItem );
765
-
766
- if ( !matcherResult ) {
767
- return;
768
- }
769
-
770
- const match = matcherResult.match;
771
-
772
- // Force consuming element's name.
773
- match.name = true;
774
-
775
- if ( !conversionApi.consumable.test( data.viewItem, match ) ) {
776
- return;
777
- }
778
-
779
- const modelElement = getModelElement( config.model, data.viewItem, conversionApi );
780
-
781
- if ( !modelElement ) {
782
- return;
783
- }
784
-
785
- if ( !conversionApi.safeInsert( modelElement, data.modelCursor ) ) {
786
- return;
787
- }
788
-
789
- conversionApi.consumable.consume( data.viewItem, match );
790
- conversionApi.convertChildren( data.viewItem, modelElement );
791
- conversionApi.updateConversionResult( modelElement, data );
792
- };
695
+ function prepareToElementConverter(config) {
696
+ const matcher = new Matcher(config.view);
697
+ return (evt, data, conversionApi) => {
698
+ const matcherResult = matcher.match(data.viewItem);
699
+ if (!matcherResult) {
700
+ return;
701
+ }
702
+ const match = matcherResult.match;
703
+ // Force consuming element's name.
704
+ match.name = true;
705
+ if (!conversionApi.consumable.test(data.viewItem, match)) {
706
+ return;
707
+ }
708
+ const modelElement = getModelElement(config.model, data.viewItem, conversionApi);
709
+ if (!modelElement) {
710
+ return;
711
+ }
712
+ if (!conversionApi.safeInsert(modelElement, data.modelCursor)) {
713
+ return;
714
+ }
715
+ conversionApi.consumable.consume(data.viewItem, match);
716
+ conversionApi.convertChildren(data.viewItem, modelElement);
717
+ conversionApi.updateConversionResult(modelElement, data);
718
+ };
793
719
  }
794
-
795
720
  // Helper function for upcasting-to-element converter. Takes the model configuration, the converted view element
796
721
  // and a writer instance and returns a model element instance to be inserted in the model.
797
722
  //
798
723
  // @param {String|Function|module:engine/model/element~Element} model Model conversion configuration.
799
724
  // @param {module:engine/view/node~Node} input The converted view node.
800
725
  // @param {module:engine/conversion/upcastdispatcher~UpcastConversionApi} conversionApi The upcast conversion API.
801
- function getModelElement( model, input, conversionApi ) {
802
- if ( model instanceof Function ) {
803
- return model( input, conversionApi );
804
- } else {
805
- return conversionApi.writer.createElement( model );
806
- }
726
+ function getModelElement(model, input, conversionApi) {
727
+ if (model instanceof Function) {
728
+ return model(input, conversionApi);
729
+ }
730
+ else {
731
+ return conversionApi.writer.createElement(model);
732
+ }
807
733
  }
808
-
809
734
  // Helper function view-attribute-to-model-attribute helper. Normalizes `config.view` which was set as `String` or
810
735
  // as an `Object` with `key`, `value` and `name` properties. Normalized `config.view` has is compatible with
811
736
  // {@link module:engine/view/matcher~MatcherPattern}.
812
737
  //
813
738
  // @param {Object} config Conversion config.
814
739
  // @returns {String} Key of the converted view attribute.
815
- function normalizeViewAttributeKeyValueConfig( config ) {
816
- if ( typeof config.view == 'string' ) {
817
- config.view = { key: config.view };
818
- }
819
-
820
- const key = config.view.key;
821
- let normalized;
822
-
823
- if ( key == 'class' || key == 'style' ) {
824
- const keyName = key == 'class' ? 'classes' : 'styles';
825
-
826
- normalized = {
827
- [ keyName ]: config.view.value
828
- };
829
- } else {
830
- const value = typeof config.view.value == 'undefined' ? /[\s\S]*/ : config.view.value;
831
-
832
- normalized = {
833
- attributes: {
834
- [ key ]: value
835
- }
836
- };
837
- }
838
-
839
- if ( config.view.name ) {
840
- normalized.name = config.view.name;
841
- }
842
-
843
- config.view = normalized;
844
-
845
- return key;
740
+ function normalizeViewAttributeKeyValueConfig(config) {
741
+ if (typeof config.view == 'string') {
742
+ config.view = { key: config.view };
743
+ }
744
+ const key = config.view.key;
745
+ let normalized;
746
+ if (key == 'class' || key == 'style') {
747
+ const keyName = key == 'class' ? 'classes' : 'styles';
748
+ normalized = {
749
+ [keyName]: config.view.value
750
+ };
751
+ }
752
+ else {
753
+ const value = typeof config.view.value == 'undefined' ? /[\s\S]*/ : config.view.value;
754
+ normalized = {
755
+ attributes: {
756
+ [key]: value
757
+ }
758
+ };
759
+ }
760
+ if (config.view.name) {
761
+ normalized.name = config.view.name;
762
+ }
763
+ config.view = normalized;
764
+ return key;
846
765
  }
847
-
848
766
  // Helper function that normalizes `config.model` in from-model-attribute conversion. `config.model` can be set
849
767
  // as a `String`, an `Object` with only `key` property or an `Object` with `key` and `value` properties. Normalized
850
768
  // `config.model` is an `Object` with `key` and `value` properties.
@@ -852,15 +770,13 @@ function normalizeViewAttributeKeyValueConfig( config ) {
852
770
  // @param {Object} config Conversion config.
853
771
  // @param {String} viewAttributeKeyToCopy Key of the converted view attribute. If it is set, model attribute value
854
772
  // will be equal to view attribute value.
855
- function normalizeModelAttributeConfig( config, viewAttributeKeyToCopy = null ) {
856
- const defaultModelValue = viewAttributeKeyToCopy === null ? true : viewElement => viewElement.getAttribute( viewAttributeKeyToCopy );
857
-
858
- const key = typeof config.model != 'object' ? config.model : config.model.key;
859
- const value = typeof config.model != 'object' || typeof config.model.value == 'undefined' ? defaultModelValue : config.model.value;
860
-
861
- config.model = { key, value };
773
+ function normalizeModelAttributeConfig(config, viewAttributeKeyToCopy = null) {
774
+ const defaultModelValue = viewAttributeKeyToCopy === null ? true :
775
+ (viewElement) => viewElement.getAttribute(viewAttributeKeyToCopy);
776
+ const key = typeof config.model != 'object' ? config.model : config.model.key;
777
+ const value = typeof config.model != 'object' || typeof config.model.value == 'undefined' ? defaultModelValue : config.model.value;
778
+ config.model = { key, value };
862
779
  }
863
-
864
780
  // Helper for to-model-attribute conversion. Takes the model attribute name and conversion configuration and returns
865
781
  // a proper converter function.
866
782
  //
@@ -868,88 +784,74 @@ function normalizeModelAttributeConfig( config, viewAttributeKeyToCopy = null )
868
784
  // @param {Object|Array.<Object>} config Conversion configuration. It is possible to provide multiple configurations in an array.
869
785
  // @param {Boolean} shallow If set to `true` the attribute will be set only on top-level nodes. Otherwise, it will be set
870
786
  // on all elements in the range.
871
- function prepareToAttributeConverter( config, shallow ) {
872
- const matcher = new Matcher( config.view );
873
-
874
- return ( evt, data, conversionApi ) => {
875
- // Converting an attribute of an element that has not been converted to anything does not make sense
876
- // because there will be nowhere to set that attribute on. At this stage, the element should've already
877
- // been converted (https://github.com/ckeditor/ckeditor5/issues/11000).
878
- if ( !data.modelRange && shallow ) {
879
- return;
880
- }
881
-
882
- const match = matcher.match( data.viewItem );
883
-
884
- // If there is no match, this callback should not do anything.
885
- if ( !match ) {
886
- return;
887
- }
888
-
889
- if ( onlyViewNameIsDefined( config.view, data.viewItem ) ) {
890
- match.match.name = true;
891
- } else {
892
- // Do not test `name` consumable because it could get consumed already while upcasting some other attribute
893
- // on the same element (for example <span class="big" style="color: red">foo</span>).
894
- delete match.match.name;
895
- }
896
-
897
- // Try to consume appropriate values from consumable values list.
898
- if ( !conversionApi.consumable.test( data.viewItem, match.match ) ) {
899
- return;
900
- }
901
-
902
- const modelKey = config.model.key;
903
- const modelValue = typeof config.model.value == 'function' ?
904
- config.model.value( data.viewItem, conversionApi ) : config.model.value;
905
-
906
- // Do not convert if attribute building function returned falsy value.
907
- if ( modelValue === null ) {
908
- return;
909
- }
910
-
911
- // Since we are converting to attribute we need a range on which we will set the attribute.
912
- // If the range is not created yet, let's create it by converting children of the current node first.
913
- if ( !data.modelRange ) {
914
- // Convert children and set conversion result as a current data.
915
- Object.assign( data, conversionApi.convertChildren( data.viewItem, data.modelCursor ) );
916
- }
917
-
918
- // Set attribute on current `output`. `Schema` is checked inside this helper function.
919
- const attributeWasSet = setAttributeOn( data.modelRange, { key: modelKey, value: modelValue }, shallow, conversionApi );
920
-
921
- // It may happen that a converter will try to set an attribute that is not allowed in the given context.
922
- // In such a situation we cannot consume the attribute. See: https://github.com/ckeditor/ckeditor5/pull/9249#issuecomment-815658459.
923
- if ( attributeWasSet ) {
924
- // Verify if the element itself wasn't consumed yet. It could be consumed already while upcasting some other attribute
925
- // on the same element (for example <span class="big" style="color: red">foo</span>).
926
- // We need to consume it so other features (especially GHS) won't try to convert it.
927
- // Note that it's not tested by the other element-to-attribute converters whether an element was consumed before
928
- // (in case of converters that the element itself is just a context and not the primary information to convert).
929
- if ( conversionApi.consumable.test( data.viewItem, { name: true } ) ) {
930
- match.match.name = true;
931
- }
932
-
933
- conversionApi.consumable.consume( data.viewItem, match.match );
934
- }
935
- };
787
+ function prepareToAttributeConverter(config, shallow) {
788
+ const matcher = new Matcher(config.view);
789
+ return (evt, data, conversionApi) => {
790
+ // Converting an attribute of an element that has not been converted to anything does not make sense
791
+ // because there will be nowhere to set that attribute on. At this stage, the element should've already
792
+ // been converted (https://github.com/ckeditor/ckeditor5/issues/11000).
793
+ if (!data.modelRange && shallow) {
794
+ return;
795
+ }
796
+ const match = matcher.match(data.viewItem);
797
+ // If there is no match, this callback should not do anything.
798
+ if (!match) {
799
+ return;
800
+ }
801
+ if (onlyViewNameIsDefined(config.view, data.viewItem)) {
802
+ match.match.name = true;
803
+ }
804
+ else {
805
+ // Do not test `name` consumable because it could get consumed already while upcasting some other attribute
806
+ // on the same element (for example <span class="big" style="color: red">foo</span>).
807
+ delete match.match.name;
808
+ }
809
+ // Try to consume appropriate values from consumable values list.
810
+ if (!conversionApi.consumable.test(data.viewItem, match.match)) {
811
+ return;
812
+ }
813
+ const modelKey = config.model.key;
814
+ const modelValue = typeof config.model.value == 'function' ?
815
+ config.model.value(data.viewItem, conversionApi) : config.model.value;
816
+ // Do not convert if attribute building function returned falsy value.
817
+ if (modelValue === null) {
818
+ return;
819
+ }
820
+ // Since we are converting to attribute we need a range on which we will set the attribute.
821
+ // If the range is not created yet, let's create it by converting children of the current node first.
822
+ if (!data.modelRange) {
823
+ // Convert children and set conversion result as a current data.
824
+ Object.assign(data, conversionApi.convertChildren(data.viewItem, data.modelCursor));
825
+ }
826
+ // Set attribute on current `output`. `Schema` is checked inside this helper function.
827
+ const attributeWasSet = setAttributeOn(data.modelRange, { key: modelKey, value: modelValue }, shallow, conversionApi);
828
+ // It may happen that a converter will try to set an attribute that is not allowed in the given context.
829
+ // In such a situation we cannot consume the attribute. See: https://github.com/ckeditor/ckeditor5/pull/9249#issuecomment-815658459.
830
+ if (attributeWasSet) {
831
+ // Verify if the element itself wasn't consumed yet. It could be consumed already while upcasting some other attribute
832
+ // on the same element (for example <span class="big" style="color: red">foo</span>).
833
+ // We need to consume it so other features (especially GHS) won't try to convert it.
834
+ // Note that it's not tested by the other element-to-attribute converters whether an element was consumed before
835
+ // (in case of converters that the element itself is just a context and not the primary information to convert).
836
+ if (conversionApi.consumable.test(data.viewItem, { name: true })) {
837
+ match.match.name = true;
838
+ }
839
+ conversionApi.consumable.consume(data.viewItem, match.match);
840
+ }
841
+ };
936
842
  }
937
-
938
843
  // Helper function that checks if element name should be consumed in attribute converters.
939
844
  //
940
845
  // @param {Object} config Conversion view config.
941
846
  // @returns {Boolean}
942
- function onlyViewNameIsDefined( viewConfig, viewItem ) {
943
- // https://github.com/ckeditor/ckeditor5-engine/issues/1786
944
- const configToTest = typeof viewConfig == 'function' ? viewConfig( viewItem ) : viewConfig;
945
-
946
- if ( typeof configToTest == 'object' && !getViewElementNameFromConfig( configToTest ) ) {
947
- return false;
948
- }
949
-
950
- return !configToTest.classes && !configToTest.attributes && !configToTest.styles;
847
+ function onlyViewNameIsDefined(viewConfig, viewItem) {
848
+ // https://github.com/ckeditor/ckeditor5-engine/issues/1786
849
+ const configToTest = typeof viewConfig == 'function' ? viewConfig(viewItem) : viewConfig;
850
+ if (typeof configToTest == 'object' && !getViewElementNameFromConfig(configToTest)) {
851
+ return false;
852
+ }
853
+ return !configToTest.classes && !configToTest.attributes && !configToTest.styles;
951
854
  }
952
-
953
855
  // Helper function for to-model-attribute converter. Sets model attribute on given range. Checks {@link module:engine/model/schema~Schema}
954
856
  // to ensure proper model structure.
955
857
  //
@@ -961,61 +863,48 @@ function onlyViewNameIsDefined( viewConfig, viewItem ) {
961
863
  // @param {Boolean} shallow If set to `true` the attribute will be set only on top-level nodes. Otherwise, it will be set
962
864
  // on all elements in the range.
963
865
  // @returns {Boolean} `true` if attribute was set on at least one node from given `modelRange`.
964
- function setAttributeOn( modelRange, modelAttribute, shallow, conversionApi ) {
965
- let result = false;
966
-
967
- // Set attribute on each item in range according to Schema.
968
- for ( const node of Array.from( modelRange.getItems( { shallow } ) ) ) {
969
- // Skip if not allowed.
970
- if ( !conversionApi.schema.checkAttribute( node, modelAttribute.key ) ) {
971
- continue;
972
- }
973
-
974
- // Mark the node as consumed even if the attribute will not be updated because it's in a valid context (schema)
975
- // and would be converted if the attribute wouldn't be present. See #8921.
976
- result = true;
977
-
978
- // Do not override the attribute if it's already present.
979
- if ( node.hasAttribute( modelAttribute.key ) ) {
980
- continue;
981
- }
982
-
983
- conversionApi.writer.setAttribute( modelAttribute.key, modelAttribute.value, node );
984
- }
985
-
986
- return result;
866
+ function setAttributeOn(modelRange, modelAttribute, shallow, conversionApi) {
867
+ let result = false;
868
+ // Set attribute on each item in range according to Schema.
869
+ for (const node of Array.from(modelRange.getItems({ shallow }))) {
870
+ // Skip if not allowed.
871
+ if (!conversionApi.schema.checkAttribute(node, modelAttribute.key)) {
872
+ continue;
873
+ }
874
+ // Mark the node as consumed even if the attribute will not be updated because it's in a valid context (schema)
875
+ // and would be converted if the attribute wouldn't be present. See #8921.
876
+ result = true;
877
+ // Do not override the attribute if it's already present.
878
+ if (node.hasAttribute(modelAttribute.key)) {
879
+ continue;
880
+ }
881
+ conversionApi.writer.setAttribute(modelAttribute.key, modelAttribute.value, node);
882
+ }
883
+ return result;
987
884
  }
988
-
989
885
  // Helper function for upcasting-to-marker conversion. Takes the config in a format requested by `upcastElementToMarker()`
990
886
  // function and converts it to a format that is supported by `upcastElementToElement()` function.
991
887
  //
992
888
  // @param {Object} config Conversion configuration.
993
- function normalizeElementToMarkerConfig( config ) {
994
- const oldModel = config.model;
995
-
996
- config.model = ( viewElement, conversionApi ) => {
997
- const markerName = typeof oldModel == 'string' ? oldModel : oldModel( viewElement, conversionApi );
998
-
999
- return conversionApi.writer.createElement( '$marker', { 'data-name': markerName } );
1000
- };
889
+ function normalizeElementToMarkerModelConfig(model) {
890
+ return (viewElement, conversionApi) => {
891
+ const markerName = typeof model == 'string' ? model : model(viewElement, conversionApi);
892
+ return conversionApi.writer.createElement('$marker', { 'data-name': markerName });
893
+ };
1001
894
  }
1002
-
1003
895
  // Helper function for upcasting-to-marker conversion. Takes the config in a format requested by `upcastDataToMarker()`
1004
896
  // function and converts it to a format that is supported by `upcastElementToElement()` function.
1005
897
  //
1006
898
  // @param {Object} config Conversion configuration.
1007
- function normalizeDataToMarkerConfig( config, type ) {
1008
- const configForElements = {};
1009
-
1010
- // Upcast <markerGroup-start> and <markerGroup-end> elements.
1011
- configForElements.view = config.view + '-' + type;
1012
-
1013
- configForElements.model = ( viewElement, conversionApi ) => {
1014
- const viewName = viewElement.getAttribute( 'name' );
1015
- const markerName = config.model( viewName, conversionApi );
1016
-
1017
- return conversionApi.writer.createElement( '$marker', { 'data-name': markerName } );
1018
- };
1019
-
1020
- return configForElements;
899
+ function normalizeDataToMarkerConfig(config, type) {
900
+ const elementCreatorFunction = (viewElement, conversionApi) => {
901
+ const viewName = viewElement.getAttribute('name');
902
+ const markerName = config.model(viewName, conversionApi);
903
+ return conversionApi.writer.createElement('$marker', { 'data-name': markerName });
904
+ };
905
+ return {
906
+ // Upcast <markerGroup-start> and <markerGroup-end> elements.
907
+ view: `${config.view}-${type}`,
908
+ model: elementCreatorFunction
909
+ };
1021
910
  }