@ckeditor/ckeditor5-engine 35.0.1 → 35.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -4
- package/package.json +30 -24
- package/src/controller/datacontroller.js +467 -561
- package/src/controller/editingcontroller.js +168 -204
- package/src/conversion/conversion.js +541 -565
- package/src/conversion/conversionhelpers.js +24 -28
- package/src/conversion/downcastdispatcher.js +457 -686
- package/src/conversion/downcasthelpers.js +1583 -1965
- package/src/conversion/mapper.js +518 -707
- package/src/conversion/modelconsumable.js +240 -283
- package/src/conversion/upcastdispatcher.js +372 -718
- package/src/conversion/upcasthelpers.js +707 -818
- package/src/conversion/viewconsumable.js +524 -581
- package/src/dataprocessor/basichtmlwriter.js +12 -16
- package/src/dataprocessor/dataprocessor.js +5 -0
- package/src/dataprocessor/htmldataprocessor.js +100 -116
- package/src/dataprocessor/htmlwriter.js +1 -18
- package/src/dataprocessor/xmldataprocessor.js +116 -137
- package/src/dev-utils/model.js +260 -352
- package/src/dev-utils/operationreplayer.js +106 -126
- package/src/dev-utils/utils.js +34 -51
- package/src/dev-utils/view.js +632 -753
- package/src/index.js +0 -11
- package/src/model/batch.js +111 -127
- package/src/model/differ.js +988 -1233
- package/src/model/document.js +340 -449
- package/src/model/documentfragment.js +327 -364
- package/src/model/documentselection.js +996 -1189
- package/src/model/element.js +306 -410
- package/src/model/history.js +224 -262
- package/src/model/item.js +5 -0
- package/src/model/liveposition.js +84 -145
- package/src/model/liverange.js +108 -185
- package/src/model/markercollection.js +379 -480
- package/src/model/model.js +883 -1034
- package/src/model/node.js +419 -463
- package/src/model/nodelist.js +176 -201
- package/src/model/operation/attributeoperation.js +153 -182
- package/src/model/operation/detachoperation.js +64 -83
- package/src/model/operation/insertoperation.js +135 -166
- package/src/model/operation/markeroperation.js +114 -140
- package/src/model/operation/mergeoperation.js +163 -191
- package/src/model/operation/moveoperation.js +157 -187
- package/src/model/operation/nooperation.js +28 -38
- package/src/model/operation/operation.js +106 -125
- package/src/model/operation/operationfactory.js +30 -34
- package/src/model/operation/renameoperation.js +109 -135
- package/src/model/operation/rootattributeoperation.js +155 -188
- package/src/model/operation/splitoperation.js +196 -232
- package/src/model/operation/transform.js +1833 -2204
- package/src/model/operation/utils.js +140 -204
- package/src/model/position.js +980 -1053
- package/src/model/range.js +910 -1028
- package/src/model/rootelement.js +77 -97
- package/src/model/schema.js +1189 -1835
- package/src/model/selection.js +745 -862
- package/src/model/text.js +90 -114
- package/src/model/textproxy.js +204 -240
- package/src/model/treewalker.js +316 -397
- package/src/model/typecheckable.js +16 -0
- package/src/model/utils/autoparagraphing.js +32 -44
- package/src/model/utils/deletecontent.js +334 -418
- package/src/model/utils/findoptimalinsertionrange.js +25 -36
- package/src/model/utils/getselectedcontent.js +96 -118
- package/src/model/utils/insertcontent.js +757 -773
- package/src/model/utils/insertobject.js +96 -119
- package/src/model/utils/modifyselection.js +120 -158
- package/src/model/utils/selection-post-fixer.js +153 -201
- package/src/model/writer.js +1305 -1474
- package/src/view/attributeelement.js +189 -225
- package/src/view/containerelement.js +75 -85
- package/src/view/document.js +172 -215
- package/src/view/documentfragment.js +200 -249
- package/src/view/documentselection.js +338 -367
- package/src/view/domconverter.js +1370 -1617
- package/src/view/downcastwriter.js +1747 -2076
- package/src/view/editableelement.js +81 -97
- package/src/view/element.js +739 -890
- package/src/view/elementdefinition.js +5 -0
- package/src/view/emptyelement.js +82 -92
- package/src/view/filler.js +35 -50
- package/src/view/item.js +5 -0
- package/src/view/matcher.js +260 -559
- package/src/view/node.js +274 -360
- package/src/view/observer/arrowkeysobserver.js +19 -28
- package/src/view/observer/bubblingemittermixin.js +120 -263
- package/src/view/observer/bubblingeventinfo.js +47 -55
- package/src/view/observer/clickobserver.js +7 -13
- package/src/view/observer/compositionobserver.js +14 -24
- package/src/view/observer/domeventdata.js +57 -67
- package/src/view/observer/domeventobserver.js +40 -64
- package/src/view/observer/fakeselectionobserver.js +81 -96
- package/src/view/observer/focusobserver.js +45 -61
- package/src/view/observer/inputobserver.js +7 -13
- package/src/view/observer/keyobserver.js +17 -27
- package/src/view/observer/mouseobserver.js +7 -14
- package/src/view/observer/mutationobserver.js +220 -315
- package/src/view/observer/observer.js +81 -102
- package/src/view/observer/selectionobserver.js +199 -246
- package/src/view/observer/tabobserver.js +23 -36
- package/src/view/placeholder.js +128 -173
- package/src/view/position.js +350 -401
- package/src/view/range.js +453 -513
- package/src/view/rawelement.js +85 -112
- package/src/view/renderer.js +874 -1018
- package/src/view/rooteditableelement.js +80 -90
- package/src/view/selection.js +608 -689
- package/src/view/styles/background.js +43 -44
- package/src/view/styles/border.js +220 -276
- package/src/view/styles/margin.js +8 -17
- package/src/view/styles/padding.js +8 -16
- package/src/view/styles/utils.js +127 -160
- package/src/view/stylesmap.js +728 -905
- package/src/view/text.js +102 -126
- package/src/view/textproxy.js +144 -170
- package/src/view/treewalker.js +383 -479
- package/src/view/typecheckable.js +19 -0
- package/src/view/uielement.js +166 -187
- package/src/view/upcastwriter.js +395 -449
- package/src/view/view.js +569 -664
- package/src/dataprocessor/dataprocessor.jsdoc +0 -64
- package/src/model/item.jsdoc +0 -14
- package/src/view/elementdefinition.jsdoc +0 -59
- package/src/view/item.jsdoc +0 -14
|
@@ -2,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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
*/
|
|
417
|
-
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
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
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
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
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(
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
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(
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
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(
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
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(
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
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(
|
|
618
|
-
|
|
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(
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
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(
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
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(
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
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(
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
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(
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
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(
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
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(
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
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(
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
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(
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
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(
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
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
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
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(
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
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
|
}
|