@ckeditor/ckeditor5-engine 35.0.1 → 35.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +4 -4
- package/package.json +30 -24
- package/src/controller/datacontroller.js +467 -561
- package/src/controller/editingcontroller.js +168 -204
- package/src/conversion/conversion.js +541 -565
- package/src/conversion/conversionhelpers.js +24 -28
- package/src/conversion/downcastdispatcher.js +457 -686
- package/src/conversion/downcasthelpers.js +1583 -1965
- package/src/conversion/mapper.js +518 -707
- package/src/conversion/modelconsumable.js +240 -283
- package/src/conversion/upcastdispatcher.js +372 -718
- package/src/conversion/upcasthelpers.js +707 -818
- package/src/conversion/viewconsumable.js +524 -581
- package/src/dataprocessor/basichtmlwriter.js +12 -16
- package/src/dataprocessor/dataprocessor.js +5 -0
- package/src/dataprocessor/htmldataprocessor.js +100 -116
- package/src/dataprocessor/htmlwriter.js +1 -18
- package/src/dataprocessor/xmldataprocessor.js +116 -137
- package/src/dev-utils/model.js +260 -352
- package/src/dev-utils/operationreplayer.js +106 -126
- package/src/dev-utils/utils.js +34 -51
- package/src/dev-utils/view.js +632 -753
- package/src/index.js +0 -11
- package/src/model/batch.js +111 -127
- package/src/model/differ.js +988 -1233
- package/src/model/document.js +340 -449
- package/src/model/documentfragment.js +327 -364
- package/src/model/documentselection.js +996 -1189
- package/src/model/element.js +306 -410
- package/src/model/history.js +224 -262
- package/src/model/item.js +5 -0
- package/src/model/liveposition.js +84 -145
- package/src/model/liverange.js +108 -185
- package/src/model/markercollection.js +379 -480
- package/src/model/model.js +883 -1034
- package/src/model/node.js +419 -463
- package/src/model/nodelist.js +175 -201
- package/src/model/operation/attributeoperation.js +153 -182
- package/src/model/operation/detachoperation.js +64 -83
- package/src/model/operation/insertoperation.js +135 -166
- package/src/model/operation/markeroperation.js +114 -140
- package/src/model/operation/mergeoperation.js +163 -191
- package/src/model/operation/moveoperation.js +157 -187
- package/src/model/operation/nooperation.js +28 -38
- package/src/model/operation/operation.js +106 -125
- package/src/model/operation/operationfactory.js +30 -34
- package/src/model/operation/renameoperation.js +109 -135
- package/src/model/operation/rootattributeoperation.js +155 -188
- package/src/model/operation/splitoperation.js +196 -232
- package/src/model/operation/transform.js +1833 -2204
- package/src/model/operation/utils.js +140 -204
- package/src/model/position.js +899 -1053
- package/src/model/range.js +910 -1028
- package/src/model/rootelement.js +77 -97
- package/src/model/schema.js +1189 -1835
- package/src/model/selection.js +745 -862
- package/src/model/text.js +90 -114
- package/src/model/textproxy.js +204 -240
- package/src/model/treewalker.js +316 -397
- package/src/model/typecheckable.js +16 -0
- package/src/model/utils/autoparagraphing.js +32 -44
- package/src/model/utils/deletecontent.js +334 -418
- package/src/model/utils/findoptimalinsertionrange.js +25 -36
- package/src/model/utils/getselectedcontent.js +96 -118
- package/src/model/utils/insertcontent.js +654 -773
- package/src/model/utils/insertobject.js +96 -119
- package/src/model/utils/modifyselection.js +120 -158
- package/src/model/utils/selection-post-fixer.js +153 -201
- package/src/model/writer.js +1305 -1474
- package/src/view/attributeelement.js +189 -225
- package/src/view/containerelement.js +75 -85
- package/src/view/document.js +172 -215
- package/src/view/documentfragment.js +200 -249
- package/src/view/documentselection.js +338 -367
- package/src/view/domconverter.js +1370 -1617
- package/src/view/downcastwriter.js +1747 -2076
- package/src/view/editableelement.js +81 -97
- package/src/view/element.js +739 -890
- package/src/view/elementdefinition.js +5 -0
- package/src/view/emptyelement.js +82 -92
- package/src/view/filler.js +35 -50
- package/src/view/item.js +5 -0
- package/src/view/matcher.js +260 -559
- package/src/view/node.js +274 -360
- package/src/view/observer/arrowkeysobserver.js +19 -28
- package/src/view/observer/bubblingemittermixin.js +120 -263
- package/src/view/observer/bubblingeventinfo.js +47 -55
- package/src/view/observer/clickobserver.js +7 -13
- package/src/view/observer/compositionobserver.js +14 -24
- package/src/view/observer/domeventdata.js +57 -67
- package/src/view/observer/domeventobserver.js +40 -64
- package/src/view/observer/fakeselectionobserver.js +81 -96
- package/src/view/observer/focusobserver.js +45 -61
- package/src/view/observer/inputobserver.js +7 -13
- package/src/view/observer/keyobserver.js +17 -27
- package/src/view/observer/mouseobserver.js +7 -14
- package/src/view/observer/mutationobserver.js +220 -315
- package/src/view/observer/observer.js +81 -102
- package/src/view/observer/selectionobserver.js +191 -246
- package/src/view/observer/tabobserver.js +23 -36
- package/src/view/placeholder.js +128 -173
- package/src/view/position.js +350 -401
- package/src/view/range.js +453 -513
- package/src/view/rawelement.js +85 -112
- package/src/view/renderer.js +874 -1018
- package/src/view/rooteditableelement.js +80 -90
- package/src/view/selection.js +608 -689
- package/src/view/styles/background.js +43 -44
- package/src/view/styles/border.js +220 -276
- package/src/view/styles/margin.js +8 -17
- package/src/view/styles/padding.js +8 -16
- package/src/view/styles/utils.js +127 -160
- package/src/view/stylesmap.js +728 -905
- package/src/view/text.js +102 -126
- package/src/view/textproxy.js +144 -170
- package/src/view/treewalker.js +383 -479
- package/src/view/typecheckable.js +19 -0
- package/src/view/uielement.js +166 -187
- package/src/view/upcastwriter.js +395 -449
- package/src/view/view.js +569 -664
- package/src/dataprocessor/dataprocessor.jsdoc +0 -64
- package/src/model/item.jsdoc +0 -14
- package/src/view/elementdefinition.jsdoc +0 -59
- package/src/view/item.jsdoc +0 -14
|
@@ -2,21 +2,16 @@
|
|
|
2
2
|
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* @module engine/conversion/upcastdispatcher
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
8
|
import ViewConsumable from './viewconsumable';
|
|
11
9
|
import ModelRange from '../model/range';
|
|
12
10
|
import ModelPosition from '../model/position';
|
|
13
|
-
import { SchemaContext } from '../model/schema';
|
|
11
|
+
import { SchemaContext } from '../model/schema'; // eslint-disable-line no-duplicate-imports
|
|
14
12
|
import { isParagraphable, wrapInParagraph } from '../model/utils/autoparagraphing';
|
|
15
|
-
|
|
16
13
|
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
|
|
17
|
-
import
|
|
18
|
-
import mix from '@ckeditor/ckeditor5-utils/src/mix';
|
|
19
|
-
|
|
14
|
+
import { Emitter } from '@ckeditor/ckeditor5-utils/src/emittermixin';
|
|
20
15
|
/**
|
|
21
16
|
* Upcast dispatcher is a central point of the view-to-model conversion, which is a process of
|
|
22
17
|
* converting a given {@link module:engine/view/documentfragment~DocumentFragment view document fragment} or
|
|
@@ -112,723 +107,382 @@ import mix from '@ckeditor/ckeditor5-utils/src/mix';
|
|
|
112
107
|
* @fires text
|
|
113
108
|
* @fires documentFragment
|
|
114
109
|
*/
|
|
115
|
-
export default class UpcastDispatcher {
|
|
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
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
const list = this._splitParts.get( originalPart );
|
|
445
|
-
|
|
446
|
-
this._splitParts.set( splitPart, list );
|
|
447
|
-
list.push( splitPart );
|
|
448
|
-
}
|
|
449
|
-
|
|
450
|
-
/**
|
|
451
|
-
* @private
|
|
452
|
-
* @see module:engine/conversion/upcastdispatcher~UpcastConversionApi#getSplitParts
|
|
453
|
-
*/
|
|
454
|
-
_getSplitParts( element ) {
|
|
455
|
-
let parts;
|
|
456
|
-
|
|
457
|
-
if ( !this._splitParts.has( element ) ) {
|
|
458
|
-
parts = [ element ];
|
|
459
|
-
} else {
|
|
460
|
-
parts = this._splitParts.get( element );
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
return parts;
|
|
464
|
-
}
|
|
465
|
-
|
|
466
|
-
/**
|
|
467
|
-
* Mark an element that were created during the splitting to not get removed on conversion end even if it is empty.
|
|
468
|
-
*
|
|
469
|
-
* @private
|
|
470
|
-
*/
|
|
471
|
-
_keepEmptyElement( element ) {
|
|
472
|
-
this._emptyElementsToKeep.add( element );
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
/**
|
|
476
|
-
* Checks if there are any empty elements created while splitting and removes them.
|
|
477
|
-
*
|
|
478
|
-
* This method works recursively to re-check empty elements again after at least one element was removed in the initial call,
|
|
479
|
-
* as some elements might have become empty after other empty elements were removed from them.
|
|
480
|
-
*
|
|
481
|
-
* @private
|
|
482
|
-
*/
|
|
483
|
-
_removeEmptyElements() {
|
|
484
|
-
let anyRemoved = false;
|
|
485
|
-
|
|
486
|
-
for ( const element of this._splitParts.keys() ) {
|
|
487
|
-
if ( element.isEmpty && !this._emptyElementsToKeep.has( element ) ) {
|
|
488
|
-
this.conversionApi.writer.remove( element );
|
|
489
|
-
this._splitParts.delete( element );
|
|
490
|
-
|
|
491
|
-
anyRemoved = true;
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
if ( anyRemoved ) {
|
|
496
|
-
this._removeEmptyElements();
|
|
497
|
-
}
|
|
498
|
-
}
|
|
499
|
-
|
|
500
|
-
/**
|
|
501
|
-
* Fired before the first conversion event, at the beginning of the upcast (view-to-model conversion) process.
|
|
502
|
-
*
|
|
503
|
-
* @event viewCleanup
|
|
504
|
-
* @param {module:engine/view/documentfragment~DocumentFragment|module:engine/view/element~Element}
|
|
505
|
-
* viewItem A part of the view to be converted.
|
|
506
|
-
*/
|
|
507
|
-
|
|
508
|
-
/**
|
|
509
|
-
* Fired when an {@link module:engine/view/element~Element} is converted.
|
|
510
|
-
*
|
|
511
|
-
* `element` is a namespace event for a class of events. Names of actually called events follow the pattern of
|
|
512
|
-
* `element:<elementName>` where `elementName` is the name of the converted element. This way listeners may listen to
|
|
513
|
-
* a conversion of all or just specific elements.
|
|
514
|
-
*
|
|
515
|
-
* @event element
|
|
516
|
-
* @param {module:engine/conversion/upcastdispatcher~UpcastConversionData} data The conversion data. Keep in mind that this object is
|
|
517
|
-
* shared by reference between all callbacks that will be called. This means that callbacks can override values if needed, and these
|
|
518
|
-
* values will be available in other callbacks.
|
|
519
|
-
* @param {module:engine/conversion/upcastdispatcher~UpcastConversionApi} conversionApi Conversion utilities to be used by the
|
|
520
|
-
* callback.
|
|
521
|
-
*/
|
|
522
|
-
|
|
523
|
-
/**
|
|
524
|
-
* Fired when a {@link module:engine/view/text~Text} is converted.
|
|
525
|
-
*
|
|
526
|
-
* @event text
|
|
527
|
-
* @see #event:element
|
|
528
|
-
*/
|
|
529
|
-
|
|
530
|
-
/**
|
|
531
|
-
* Fired when a {@link module:engine/view/documentfragment~DocumentFragment} is converted.
|
|
532
|
-
*
|
|
533
|
-
* @event documentFragment
|
|
534
|
-
* @see #event:element
|
|
535
|
-
*/
|
|
110
|
+
export default class UpcastDispatcher extends Emitter {
|
|
111
|
+
/**
|
|
112
|
+
* Creates an upcast dispatcher that operates using the passed API.
|
|
113
|
+
*
|
|
114
|
+
* @see module:engine/conversion/upcastdispatcher~UpcastConversionApi
|
|
115
|
+
* @param {Object} [conversionApi] Additional properties for an interface that will be passed to events fired
|
|
116
|
+
* by the upcast dispatcher.
|
|
117
|
+
*/
|
|
118
|
+
constructor(conversionApi) {
|
|
119
|
+
super();
|
|
120
|
+
/**
|
|
121
|
+
* The list of elements that were created during splitting.
|
|
122
|
+
*
|
|
123
|
+
* After the conversion process, the list is cleared.
|
|
124
|
+
*
|
|
125
|
+
* @private
|
|
126
|
+
* @type {Map.<module:engine/model/element~Element,Array.<module:engine/model/element~Element>>}
|
|
127
|
+
*/
|
|
128
|
+
this._splitParts = new Map();
|
|
129
|
+
/**
|
|
130
|
+
* The list of cursor parent elements that were created during splitting.
|
|
131
|
+
*
|
|
132
|
+
* After the conversion process the list is cleared.
|
|
133
|
+
*
|
|
134
|
+
* @private
|
|
135
|
+
* @type {Map.<module:engine/model/element~Element,Array.<module:engine/model/element~Element>>}
|
|
136
|
+
*/
|
|
137
|
+
this._cursorParents = new Map();
|
|
138
|
+
/**
|
|
139
|
+
* The position in the temporary structure where the converted content is inserted. The structure reflects the context of
|
|
140
|
+
* the target position where the content will be inserted. This property is built based on the context parameter of the
|
|
141
|
+
* convert method.
|
|
142
|
+
*
|
|
143
|
+
* @private
|
|
144
|
+
* @type {module:engine/model/position~Position|null}
|
|
145
|
+
*/
|
|
146
|
+
this._modelCursor = null;
|
|
147
|
+
/**
|
|
148
|
+
* The list of elements that were created during the splitting but should not get removed on conversion end even if they are empty.
|
|
149
|
+
*
|
|
150
|
+
* The list is cleared after the conversion process.
|
|
151
|
+
*
|
|
152
|
+
* @private
|
|
153
|
+
* @type {Set.<module:engine/model/element~Element>}
|
|
154
|
+
*/
|
|
155
|
+
this._emptyElementsToKeep = new Set();
|
|
156
|
+
/**
|
|
157
|
+
* An interface passed by the dispatcher to the event callbacks.
|
|
158
|
+
*
|
|
159
|
+
* @member {module:engine/conversion/upcastdispatcher~UpcastConversionApi}
|
|
160
|
+
*/
|
|
161
|
+
this.conversionApi = {
|
|
162
|
+
...conversionApi,
|
|
163
|
+
consumable: null,
|
|
164
|
+
writer: null,
|
|
165
|
+
store: null,
|
|
166
|
+
convertItem: (viewItem, modelCursor) => this._convertItem(viewItem, modelCursor),
|
|
167
|
+
convertChildren: (viewElement, positionOrElement) => this._convertChildren(viewElement, positionOrElement),
|
|
168
|
+
safeInsert: (modelElement, position) => this._safeInsert(modelElement, position),
|
|
169
|
+
updateConversionResult: (modelElement, data) => this._updateConversionResult(modelElement, data),
|
|
170
|
+
// Advanced API - use only if custom position handling is needed.
|
|
171
|
+
splitToAllowedParent: (modelElement, modelCursor) => this._splitToAllowedParent(modelElement, modelCursor),
|
|
172
|
+
getSplitParts: modelElement => this._getSplitParts(modelElement),
|
|
173
|
+
keepEmptyElement: modelElement => this._keepEmptyElement(modelElement)
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Starts the conversion process. The entry point for the conversion.
|
|
178
|
+
*
|
|
179
|
+
* @fires element
|
|
180
|
+
* @fires text
|
|
181
|
+
* @fires documentFragment
|
|
182
|
+
* @param {module:engine/view/documentfragment~DocumentFragment|module:engine/view/element~Element} viewElement
|
|
183
|
+
* The part of the view to be converted.
|
|
184
|
+
* @param {module:engine/model/writer~Writer} writer An instance of the model writer.
|
|
185
|
+
* @param {module:engine/model/schema~SchemaContextDefinition} [context=['$root']] Elements will be converted according to this context.
|
|
186
|
+
* @returns {module:engine/model/documentfragment~DocumentFragment} Model data that is the result of the conversion process
|
|
187
|
+
* wrapped in `DocumentFragment`. Converted marker elements will be set as the document fragment's
|
|
188
|
+
* {@link module:engine/model/documentfragment~DocumentFragment#markers static markers map}.
|
|
189
|
+
*/
|
|
190
|
+
convert(viewElement, writer, context = ['$root']) {
|
|
191
|
+
this.fire('viewCleanup', viewElement);
|
|
192
|
+
// Create context tree and set position in the top element.
|
|
193
|
+
// Items will be converted according to this position.
|
|
194
|
+
this._modelCursor = createContextTree(context, writer);
|
|
195
|
+
// Store writer in conversion as a conversion API
|
|
196
|
+
// to be sure that conversion process will use the same batch.
|
|
197
|
+
this.conversionApi.writer = writer;
|
|
198
|
+
// Create consumable values list for conversion process.
|
|
199
|
+
this.conversionApi.consumable = ViewConsumable.createFrom(viewElement);
|
|
200
|
+
// Custom data stored by converter for conversion process.
|
|
201
|
+
this.conversionApi.store = {};
|
|
202
|
+
// Do the conversion.
|
|
203
|
+
const { modelRange } = this._convertItem(viewElement, this._modelCursor);
|
|
204
|
+
// Conversion result is always a document fragment so let's create it.
|
|
205
|
+
const documentFragment = writer.createDocumentFragment();
|
|
206
|
+
// When there is a conversion result.
|
|
207
|
+
if (modelRange) {
|
|
208
|
+
// Remove all empty elements that were create while splitting.
|
|
209
|
+
this._removeEmptyElements();
|
|
210
|
+
// Move all items that were converted in context tree to the document fragment.
|
|
211
|
+
for (const item of Array.from(this._modelCursor.parent.getChildren())) {
|
|
212
|
+
writer.append(item, documentFragment);
|
|
213
|
+
}
|
|
214
|
+
// Extract temporary markers elements from model and set as static markers collection.
|
|
215
|
+
documentFragment.markers = extractMarkersFromModelFragment(documentFragment, writer);
|
|
216
|
+
}
|
|
217
|
+
// Clear context position.
|
|
218
|
+
this._modelCursor = null;
|
|
219
|
+
// Clear split elements & parents lists.
|
|
220
|
+
this._splitParts.clear();
|
|
221
|
+
this._cursorParents.clear();
|
|
222
|
+
this._emptyElementsToKeep.clear();
|
|
223
|
+
// Clear conversion API.
|
|
224
|
+
this.conversionApi.writer = null;
|
|
225
|
+
this.conversionApi.store = null;
|
|
226
|
+
// Return fragment as conversion result.
|
|
227
|
+
return documentFragment;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* @private
|
|
231
|
+
* @see module:engine/conversion/upcastdispatcher~UpcastConversionApi#convertItem
|
|
232
|
+
*/
|
|
233
|
+
_convertItem(viewItem, modelCursor) {
|
|
234
|
+
const data = { viewItem, modelCursor, modelRange: null };
|
|
235
|
+
if (viewItem.is('element')) {
|
|
236
|
+
this.fire(`element:${viewItem.name}`, data, this.conversionApi);
|
|
237
|
+
}
|
|
238
|
+
else if (viewItem.is('$text')) {
|
|
239
|
+
this.fire('text', data, this.conversionApi);
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
this.fire('documentFragment', data, this.conversionApi);
|
|
243
|
+
}
|
|
244
|
+
// Handle incorrect conversion result.
|
|
245
|
+
if (data.modelRange && !(data.modelRange instanceof ModelRange)) {
|
|
246
|
+
/**
|
|
247
|
+
* Incorrect conversion result was dropped.
|
|
248
|
+
*
|
|
249
|
+
* {@link module:engine/model/range~Range Model range} should be a conversion result.
|
|
250
|
+
*
|
|
251
|
+
* @error view-conversion-dispatcher-incorrect-result
|
|
252
|
+
*/
|
|
253
|
+
throw new CKEditorError('view-conversion-dispatcher-incorrect-result', this);
|
|
254
|
+
}
|
|
255
|
+
return { modelRange: data.modelRange, modelCursor: data.modelCursor };
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* @private
|
|
259
|
+
* @see module:engine/conversion/upcastdispatcher~UpcastConversionApi#convertChildren
|
|
260
|
+
*/
|
|
261
|
+
_convertChildren(viewItem, elementOrModelCursor) {
|
|
262
|
+
let nextModelCursor = elementOrModelCursor.is('position') ?
|
|
263
|
+
elementOrModelCursor : ModelPosition._createAt(elementOrModelCursor, 0);
|
|
264
|
+
const modelRange = new ModelRange(nextModelCursor);
|
|
265
|
+
for (const viewChild of Array.from(viewItem.getChildren())) {
|
|
266
|
+
const result = this._convertItem(viewChild, nextModelCursor);
|
|
267
|
+
if (result.modelRange instanceof ModelRange) {
|
|
268
|
+
modelRange.end = result.modelRange.end;
|
|
269
|
+
nextModelCursor = result.modelCursor;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
return { modelRange, modelCursor: nextModelCursor };
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* @private
|
|
276
|
+
* @see module:engine/conversion/upcastdispatcher~UpcastConversionApi#safeInsert
|
|
277
|
+
*/
|
|
278
|
+
_safeInsert(modelElement, position) {
|
|
279
|
+
// Find allowed parent for element that we are going to insert.
|
|
280
|
+
// If current parent does not allow to insert element but one of the ancestors does
|
|
281
|
+
// then split nodes to allowed parent.
|
|
282
|
+
const splitResult = this._splitToAllowedParent(modelElement, position);
|
|
283
|
+
// When there is no split result it means that we can't insert element to model tree, so let's skip it.
|
|
284
|
+
if (!splitResult) {
|
|
285
|
+
return false;
|
|
286
|
+
}
|
|
287
|
+
// Insert element on allowed position.
|
|
288
|
+
this.conversionApi.writer.insert(modelElement, splitResult.position);
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* @private
|
|
293
|
+
* @see module:engine/conversion/upcastdispatcher~UpcastConversionApi#updateConversionResult
|
|
294
|
+
*/
|
|
295
|
+
_updateConversionResult(modelElement, data) {
|
|
296
|
+
const parts = this._getSplitParts(modelElement);
|
|
297
|
+
const writer = this.conversionApi.writer;
|
|
298
|
+
// Set conversion result range - only if not set already.
|
|
299
|
+
if (!data.modelRange) {
|
|
300
|
+
data.modelRange = writer.createRange(writer.createPositionBefore(modelElement), writer.createPositionAfter(parts[parts.length - 1]));
|
|
301
|
+
}
|
|
302
|
+
const savedCursorParent = this._cursorParents.get(modelElement);
|
|
303
|
+
// Now we need to check where the `modelCursor` should be.
|
|
304
|
+
if (savedCursorParent) {
|
|
305
|
+
// If we split parent to insert our element then we want to continue conversion in the new part of the split parent.
|
|
306
|
+
//
|
|
307
|
+
// before: <allowed><notAllowed>foo[]</notAllowed></allowed>
|
|
308
|
+
// after: <allowed><notAllowed>foo</notAllowed> <converted></converted> <notAllowed>[]</notAllowed></allowed>
|
|
309
|
+
data.modelCursor = writer.createPositionAt(savedCursorParent, 0);
|
|
310
|
+
}
|
|
311
|
+
else {
|
|
312
|
+
// Otherwise just continue after inserted element.
|
|
313
|
+
data.modelCursor = data.modelRange.end;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* @private
|
|
318
|
+
* @see module:engine/conversion/upcastdispatcher~UpcastConversionApi#splitToAllowedParent
|
|
319
|
+
*/
|
|
320
|
+
_splitToAllowedParent(node, modelCursor) {
|
|
321
|
+
const { schema, writer } = this.conversionApi;
|
|
322
|
+
// Try to find allowed parent.
|
|
323
|
+
let allowedParent = schema.findAllowedParent(modelCursor, node);
|
|
324
|
+
if (allowedParent) {
|
|
325
|
+
// When current position parent allows to insert node then return this position.
|
|
326
|
+
if (allowedParent === modelCursor.parent) {
|
|
327
|
+
return { position: modelCursor };
|
|
328
|
+
}
|
|
329
|
+
// When allowed parent is in context tree (it's outside the converted tree).
|
|
330
|
+
if (this._modelCursor.parent.getAncestors().includes(allowedParent)) {
|
|
331
|
+
allowedParent = null;
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
if (!allowedParent) {
|
|
335
|
+
// Check if the node wrapped with a paragraph would be accepted by the schema.
|
|
336
|
+
if (!isParagraphable(modelCursor, node, schema)) {
|
|
337
|
+
return null;
|
|
338
|
+
}
|
|
339
|
+
return {
|
|
340
|
+
position: wrapInParagraph(modelCursor, writer)
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
// Split element to allowed parent.
|
|
344
|
+
const splitResult = this.conversionApi.writer.split(modelCursor, allowedParent);
|
|
345
|
+
// Using the range returned by `model.Writer#split`, we will pair original elements with their split parts.
|
|
346
|
+
//
|
|
347
|
+
// The range returned from the writer spans "over the split" or, precisely saying, from the end of the original element (the one
|
|
348
|
+
// that got split) to the beginning of the other part of that element:
|
|
349
|
+
//
|
|
350
|
+
// <limit><a><b><c>X[]Y</c></b><a></limit> ->
|
|
351
|
+
// <limit><a><b><c>X[</c></b></a><a><b><c>]Y</c></b></a>
|
|
352
|
+
//
|
|
353
|
+
// After the split there cannot be any full node between the positions in `splitRange`. The positions are touching.
|
|
354
|
+
// Also, because of how splitting works, it is easy to notice, that "closing tags" are in the reverse order than "opening tags".
|
|
355
|
+
// Also, since we split all those elements, each of them has to have the other part.
|
|
356
|
+
//
|
|
357
|
+
// With those observations in mind, we will pair the original elements with their split parts by saving "closing tags" and matching
|
|
358
|
+
// them with "opening tags" in the reverse order. For that we can use a stack.
|
|
359
|
+
const stack = [];
|
|
360
|
+
for (const treeWalkerValue of splitResult.range.getWalker()) {
|
|
361
|
+
if (treeWalkerValue.type == 'elementEnd') {
|
|
362
|
+
stack.push(treeWalkerValue.item);
|
|
363
|
+
}
|
|
364
|
+
else {
|
|
365
|
+
// There should not be any text nodes after the element is split, so the only other value is `elementStart`.
|
|
366
|
+
const originalPart = stack.pop();
|
|
367
|
+
const splitPart = treeWalkerValue.item;
|
|
368
|
+
this._registerSplitPair(originalPart, splitPart);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
const cursorParent = splitResult.range.end.parent;
|
|
372
|
+
this._cursorParents.set(node, cursorParent);
|
|
373
|
+
return {
|
|
374
|
+
position: splitResult.position,
|
|
375
|
+
cursorParent
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
/**
|
|
379
|
+
* Registers that a `splitPart` element is a split part of the `originalPart` element.
|
|
380
|
+
*
|
|
381
|
+
* The data set by this method is used by {@link #_getSplitParts} and {@link #_removeEmptyElements}.
|
|
382
|
+
*
|
|
383
|
+
* @private
|
|
384
|
+
* @param {module:engine/model/element~Element} originalPart
|
|
385
|
+
* @param {module:engine/model/element~Element} splitPart
|
|
386
|
+
*/
|
|
387
|
+
_registerSplitPair(originalPart, splitPart) {
|
|
388
|
+
if (!this._splitParts.has(originalPart)) {
|
|
389
|
+
this._splitParts.set(originalPart, [originalPart]);
|
|
390
|
+
}
|
|
391
|
+
const list = this._splitParts.get(originalPart);
|
|
392
|
+
this._splitParts.set(splitPart, list);
|
|
393
|
+
list.push(splitPart);
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* @private
|
|
397
|
+
* @see module:engine/conversion/upcastdispatcher~UpcastConversionApi#getSplitParts
|
|
398
|
+
*/
|
|
399
|
+
_getSplitParts(element) {
|
|
400
|
+
let parts;
|
|
401
|
+
if (!this._splitParts.has(element)) {
|
|
402
|
+
parts = [element];
|
|
403
|
+
}
|
|
404
|
+
else {
|
|
405
|
+
parts = this._splitParts.get(element);
|
|
406
|
+
}
|
|
407
|
+
return parts;
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* Mark an element that were created during the splitting to not get removed on conversion end even if it is empty.
|
|
411
|
+
*
|
|
412
|
+
* @private
|
|
413
|
+
*/
|
|
414
|
+
_keepEmptyElement(element) {
|
|
415
|
+
this._emptyElementsToKeep.add(element);
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Checks if there are any empty elements created while splitting and removes them.
|
|
419
|
+
*
|
|
420
|
+
* This method works recursively to re-check empty elements again after at least one element was removed in the initial call,
|
|
421
|
+
* as some elements might have become empty after other empty elements were removed from them.
|
|
422
|
+
*
|
|
423
|
+
* @private
|
|
424
|
+
*/
|
|
425
|
+
_removeEmptyElements() {
|
|
426
|
+
let anyRemoved = false;
|
|
427
|
+
for (const element of this._splitParts.keys()) {
|
|
428
|
+
if (element.isEmpty && !this._emptyElementsToKeep.has(element)) {
|
|
429
|
+
this.conversionApi.writer.remove(element);
|
|
430
|
+
this._splitParts.delete(element);
|
|
431
|
+
anyRemoved = true;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
if (anyRemoved) {
|
|
435
|
+
this._removeEmptyElements();
|
|
436
|
+
}
|
|
437
|
+
}
|
|
536
438
|
}
|
|
537
|
-
|
|
538
|
-
mix( UpcastDispatcher, EmitterMixin );
|
|
539
|
-
|
|
540
439
|
// Traverses given model item and searches elements which marks marker range. Found element is removed from
|
|
541
440
|
// DocumentFragment but path of this element is stored in a Map which is then returned.
|
|
542
441
|
//
|
|
543
442
|
// @param {module:engine/view/documentfragment~DocumentFragment|module:engine/view/node~Node} modelItem Fragment of model.
|
|
544
443
|
// @returns {Map<String, module:engine/model/range~Range>} List of static markers.
|
|
545
|
-
function extractMarkersFromModelFragment(
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
// Remove marker element from DocumentFragment.
|
|
574
|
-
writer.remove( markerElement );
|
|
575
|
-
}
|
|
576
|
-
|
|
577
|
-
return markers;
|
|
444
|
+
function extractMarkersFromModelFragment(modelItem, writer) {
|
|
445
|
+
const markerElements = new Set();
|
|
446
|
+
const markers = new Map();
|
|
447
|
+
// Create ModelTreeWalker.
|
|
448
|
+
const range = ModelRange._createIn(modelItem).getItems();
|
|
449
|
+
// Walk through DocumentFragment and collect marker elements.
|
|
450
|
+
for (const item of range) {
|
|
451
|
+
// Check if current element is a marker.
|
|
452
|
+
if (item.is('element', '$marker')) {
|
|
453
|
+
markerElements.add(item);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
// Walk through collected marker elements store its path and remove its from the DocumentFragment.
|
|
457
|
+
for (const markerElement of markerElements) {
|
|
458
|
+
const markerName = markerElement.getAttribute('data-name');
|
|
459
|
+
const currentPosition = writer.createPositionBefore(markerElement);
|
|
460
|
+
// When marker of given name is not stored it means that we have found the beginning of the range.
|
|
461
|
+
if (!markers.has(markerName)) {
|
|
462
|
+
markers.set(markerName, new ModelRange(currentPosition.clone()));
|
|
463
|
+
// Otherwise is means that we have found end of the marker range.
|
|
464
|
+
}
|
|
465
|
+
else {
|
|
466
|
+
markers.get(markerName).end = currentPosition.clone();
|
|
467
|
+
}
|
|
468
|
+
// Remove marker element from DocumentFragment.
|
|
469
|
+
writer.remove(markerElement);
|
|
470
|
+
}
|
|
471
|
+
return markers;
|
|
578
472
|
}
|
|
579
|
-
|
|
580
473
|
// Creates model fragment according to given context and returns position in the bottom (the deepest) element.
|
|
581
|
-
function createContextTree(
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
position = ModelPosition._createAt( current, 0 );
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
return position;
|
|
474
|
+
function createContextTree(contextDefinition, writer) {
|
|
475
|
+
let position;
|
|
476
|
+
for (const item of new SchemaContext(contextDefinition)) {
|
|
477
|
+
const attributes = {};
|
|
478
|
+
for (const key of item.getAttributeKeys()) {
|
|
479
|
+
attributes[key] = item.getAttribute(key);
|
|
480
|
+
}
|
|
481
|
+
const current = writer.createElement(item.name, attributes);
|
|
482
|
+
if (position) {
|
|
483
|
+
writer.insert(current, position);
|
|
484
|
+
}
|
|
485
|
+
position = ModelPosition._createAt(current, 0);
|
|
486
|
+
}
|
|
487
|
+
return position;
|
|
601
488
|
}
|
|
602
|
-
|
|
603
|
-
/**
|
|
604
|
-
* A set of conversion utilities available as the third parameter of the
|
|
605
|
-
* {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher upcast dispatcher}'s events.
|
|
606
|
-
*
|
|
607
|
-
* @interface module:engine/conversion/upcastdispatcher~UpcastConversionApi
|
|
608
|
-
*/
|
|
609
|
-
|
|
610
|
-
/**
|
|
611
|
-
* Starts the conversion of a given item by firing an appropriate event.
|
|
612
|
-
*
|
|
613
|
-
* Every fired event is passed (as the first parameter) an object with the `modelRange` property. Every event may set and/or
|
|
614
|
-
* modify that property. When all callbacks are done, the final value of the `modelRange` property is returned by this method.
|
|
615
|
-
* The `modelRange` must be a {@link module:engine/model/range~Range model range} or `null` (as set by default).
|
|
616
|
-
*
|
|
617
|
-
* @method #convertItem
|
|
618
|
-
* @fires module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:element
|
|
619
|
-
* @fires module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:text
|
|
620
|
-
* @fires module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:documentFragment
|
|
621
|
-
* @param {module:engine/view/item~Item} viewItem Item to convert.
|
|
622
|
-
* @param {module:engine/model/position~Position} modelCursor The conversion position.
|
|
623
|
-
* @returns {Object} result The conversion result.
|
|
624
|
-
* @returns {module:engine/model/range~Range|null} result.modelRange The model range containing the result of the item conversion,
|
|
625
|
-
* created and modified by callbacks attached to the fired event, or `null` if the conversion result was incorrect.
|
|
626
|
-
* @returns {module:engine/model/position~Position} result.modelCursor The position where the conversion should be continued.
|
|
627
|
-
*/
|
|
628
|
-
|
|
629
|
-
/**
|
|
630
|
-
* Starts the conversion of all children of a given item by firing appropriate events for all the children.
|
|
631
|
-
*
|
|
632
|
-
* @method #convertChildren
|
|
633
|
-
* @fires module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:element
|
|
634
|
-
* @fires module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:text
|
|
635
|
-
* @fires module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:documentFragment
|
|
636
|
-
* @param {module:engine/view/item~Item} viewItem An element whose children should be converted.
|
|
637
|
-
* @param {module:engine/model/position~Position|module:engine/model/element~Element} positionOrElement A position or an element of
|
|
638
|
-
* the conversion.
|
|
639
|
-
* @returns {Object} result The conversion result.
|
|
640
|
-
* @returns {module:engine/model/range~Range} result.modelRange The model range containing the results of the conversion of all children
|
|
641
|
-
* of the given item. When no child was converted, the range is collapsed.
|
|
642
|
-
* @returns {module:engine/model/position~Position} result.modelCursor The position where the conversion should be continued.
|
|
643
|
-
*/
|
|
644
|
-
|
|
645
|
-
/**
|
|
646
|
-
* Safely inserts an element to the document, checking the {@link module:engine/model/schema~Schema schema} to find an allowed parent for
|
|
647
|
-
* an element that you are going to insert, starting from the given position. If the current parent does not allow to insert the element
|
|
648
|
-
* but one of the ancestors does, then splits the nodes to allowed parent.
|
|
649
|
-
*
|
|
650
|
-
* If the schema allows to insert the node in a given position, nothing is split.
|
|
651
|
-
*
|
|
652
|
-
* If it was not possible to find an allowed parent, `false` is returned and nothing is split.
|
|
653
|
-
*
|
|
654
|
-
* Otherwise, ancestors are split.
|
|
655
|
-
*
|
|
656
|
-
* For instance, if `<imageBlock>` is not allowed in `<paragraph>` but is allowed in `$root`:
|
|
657
|
-
*
|
|
658
|
-
* <paragraph>foo[]bar</paragraph>
|
|
659
|
-
*
|
|
660
|
-
* -> safe insert for `<imageBlock>` will split ->
|
|
661
|
-
*
|
|
662
|
-
* <paragraph>foo</paragraph>[]<paragraph>bar</paragraph>
|
|
663
|
-
*
|
|
664
|
-
* Example usage:
|
|
665
|
-
*
|
|
666
|
-
* const myElement = conversionApi.writer.createElement( 'myElement' );
|
|
667
|
-
*
|
|
668
|
-
* if ( !conversionApi.safeInsert( myElement, data.modelCursor ) ) {
|
|
669
|
-
* return;
|
|
670
|
-
* }
|
|
671
|
-
*
|
|
672
|
-
* The split result is saved and {@link #updateConversionResult} should be used to update the
|
|
673
|
-
* {@link module:engine/conversion/upcastdispatcher~UpcastConversionData conversion data}.
|
|
674
|
-
*
|
|
675
|
-
* @method #safeInsert
|
|
676
|
-
* @param {module:engine/model/node~Node} node The node to insert.
|
|
677
|
-
* @param {module:engine/model/position~Position} position The position where an element is going to be inserted.
|
|
678
|
-
* @returns {Boolean} The split result. If it was not possible to find an allowed position, `false` is returned.
|
|
679
|
-
*/
|
|
680
|
-
|
|
681
|
-
/**
|
|
682
|
-
* Updates the conversion result and sets a proper {@link module:engine/conversion/upcastdispatcher~UpcastConversionData#modelRange} and
|
|
683
|
-
* the next {@link module:engine/conversion/upcastdispatcher~UpcastConversionData#modelCursor} after the conversion.
|
|
684
|
-
* Used together with {@link #safeInsert}, it enables you to easily convert elements without worrying if the node was split
|
|
685
|
-
* during the conversion of its children.
|
|
686
|
-
*
|
|
687
|
-
* A usage example in converter code:
|
|
688
|
-
*
|
|
689
|
-
* const myElement = conversionApi.writer.createElement( 'myElement' );
|
|
690
|
-
*
|
|
691
|
-
* if ( !conversionApi.safeInsert( myElement, data.modelCursor ) ) {
|
|
692
|
-
* return;
|
|
693
|
-
* }
|
|
694
|
-
*
|
|
695
|
-
* // Children conversion may split `myElement`.
|
|
696
|
-
* conversionApi.convertChildren( data.viewItem, myElement );
|
|
697
|
-
*
|
|
698
|
-
* conversionApi.updateConversionResult( myElement, data );
|
|
699
|
-
*
|
|
700
|
-
* @method #updateConversionResult
|
|
701
|
-
* @param {module:engine/model/element~Element} element
|
|
702
|
-
* @param {module:engine/conversion/upcastdispatcher~UpcastConversionData} data Conversion data.
|
|
703
|
-
* @param {module:engine/conversion/upcastdispatcher~UpcastConversionApi} conversionApi Conversion utilities to be used by the callback.
|
|
704
|
-
*/
|
|
705
|
-
|
|
706
|
-
/**
|
|
707
|
-
* Checks the {@link module:engine/model/schema~Schema schema} to find an allowed parent for an element that is going to be inserted
|
|
708
|
-
* starting from the given position. If the current parent does not allow inserting an element but one of the ancestors does, the method
|
|
709
|
-
* splits nodes to allowed parent.
|
|
710
|
-
*
|
|
711
|
-
* If the schema allows inserting the node in the given position, nothing is split and an object with that position is returned.
|
|
712
|
-
*
|
|
713
|
-
* If it was not possible to find an allowed parent, `null` is returned and nothing is split.
|
|
714
|
-
*
|
|
715
|
-
* Otherwise, ancestors are split and an object with a position and the copy of the split element is returned.
|
|
716
|
-
*
|
|
717
|
-
* For instance, if `<imageBlock>` is not allowed in `<paragraph>` but is allowed in `$root`:
|
|
718
|
-
*
|
|
719
|
-
* <paragraph>foo[]bar</paragraph>
|
|
720
|
-
*
|
|
721
|
-
* -> split for `<imageBlock>` ->
|
|
722
|
-
*
|
|
723
|
-
* <paragraph>foo</paragraph>[]<paragraph>bar</paragraph>
|
|
724
|
-
*
|
|
725
|
-
* In the example above, the position between `<paragraph>` elements will be returned as `position` and the second `paragraph`
|
|
726
|
-
* as `cursorParent`.
|
|
727
|
-
*
|
|
728
|
-
* **Note:** This is an advanced method. For most cases {@link #safeInsert} and {@link #updateConversionResult} should be used.
|
|
729
|
-
*
|
|
730
|
-
* @method #splitToAllowedParent
|
|
731
|
-
* @param {module:engine/model/position~Position} position The position where the element is going to be inserted.
|
|
732
|
-
* @param {module:engine/model/node~Node} node The node to insert.
|
|
733
|
-
* @returns {Object|null} The split result. If it was not possible to find an allowed position, `null` is returned.
|
|
734
|
-
* @returns {module:engine/model/position~Position} The position between split elements.
|
|
735
|
-
* @returns {module:engine/model/element~Element} [cursorParent] The element inside which the cursor should be placed to
|
|
736
|
-
* continue the conversion. When the element is not defined it means that there was no split.
|
|
737
|
-
*/
|
|
738
|
-
|
|
739
|
-
/**
|
|
740
|
-
* Returns all the split parts of the given `element` that were created during upcasting through using {@link #splitToAllowedParent}.
|
|
741
|
-
* It enables you to easily track these elements and continue processing them after they are split during the conversion of their children.
|
|
742
|
-
*
|
|
743
|
-
* <paragraph>Foo<imageBlock />bar<imageBlock />baz</paragraph> ->
|
|
744
|
-
* <paragraph>Foo</paragraph><imageBlock /><paragraph>bar</paragraph><imageBlock /><paragraph>baz</paragraph>
|
|
745
|
-
*
|
|
746
|
-
* For a reference to any of above paragraphs, the function will return all three paragraphs (the original element included),
|
|
747
|
-
* sorted in the order of their creation (the original element is the first one).
|
|
748
|
-
*
|
|
749
|
-
* If the given `element` was not split, an array with a single element is returned.
|
|
750
|
-
*
|
|
751
|
-
* A usage example in the converter code:
|
|
752
|
-
*
|
|
753
|
-
* const myElement = conversionApi.writer.createElement( 'myElement' );
|
|
754
|
-
*
|
|
755
|
-
* // Children conversion may split `myElement`.
|
|
756
|
-
* conversionApi.convertChildren( data.viewItem, data.modelCursor );
|
|
757
|
-
*
|
|
758
|
-
* const splitParts = conversionApi.getSplitParts( myElement );
|
|
759
|
-
* const lastSplitPart = splitParts[ splitParts.length - 1 ];
|
|
760
|
-
*
|
|
761
|
-
* // Setting `data.modelRange` basing on split parts:
|
|
762
|
-
* data.modelRange = conversionApi.writer.createRange(
|
|
763
|
-
* conversionApi.writer.createPositionBefore( myElement ),
|
|
764
|
-
* conversionApi.writer.createPositionAfter( lastSplitPart )
|
|
765
|
-
* );
|
|
766
|
-
*
|
|
767
|
-
* // Setting `data.modelCursor` to continue after the last split element:
|
|
768
|
-
* data.modelCursor = conversionApi.writer.createPositionAfter( lastSplitPart );
|
|
769
|
-
*
|
|
770
|
-
* **Tip:** If you are unable to get a reference to the original element (for example because the code is split into multiple converters
|
|
771
|
-
* or even classes) but it has already been converted, you may want to check the first element in `data.modelRange`. This is a common
|
|
772
|
-
* situation if an attribute converter is separated from an element converter.
|
|
773
|
-
*
|
|
774
|
-
* **Note:** This is an advanced method. For most cases {@link #safeInsert} and {@link #updateConversionResult} should be used.
|
|
775
|
-
*
|
|
776
|
-
* @method #getSplitParts
|
|
777
|
-
* @param {module:engine/model/element~Element} element
|
|
778
|
-
* @returns {Array.<module:engine/model/element~Element>}
|
|
779
|
-
*/
|
|
780
|
-
|
|
781
|
-
/**
|
|
782
|
-
* Mark an element that was created during splitting to not get removed on conversion end even if it is empty.
|
|
783
|
-
*
|
|
784
|
-
* **Note:** This is an advanced method. For most cases you will not need to keep the split empty element.
|
|
785
|
-
*
|
|
786
|
-
* @method #keepEmptyElement
|
|
787
|
-
* @param {module:engine/model/element~Element} element
|
|
788
|
-
*/
|
|
789
|
-
|
|
790
|
-
/**
|
|
791
|
-
* Stores information about what parts of the processed view item are still waiting to be handled. After a piece of view item
|
|
792
|
-
* was converted, an appropriate consumable value should be
|
|
793
|
-
* {@link module:engine/conversion/viewconsumable~ViewConsumable#consume consumed}.
|
|
794
|
-
*
|
|
795
|
-
* @member {module:engine/conversion/viewconsumable~ViewConsumable} #consumable
|
|
796
|
-
*/
|
|
797
|
-
|
|
798
|
-
/**
|
|
799
|
-
* Custom data stored by converters for the conversion process. Custom properties of this object can be defined and use to
|
|
800
|
-
* pass parameters between converters.
|
|
801
|
-
*
|
|
802
|
-
* The difference between this property and the `data` parameter of
|
|
803
|
-
* {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher#event:element} is that the `data` parameters allow you
|
|
804
|
-
* to pass parameters within a single event and `store` within the whole conversion.
|
|
805
|
-
*
|
|
806
|
-
* @member {Object} #store
|
|
807
|
-
*/
|
|
808
|
-
|
|
809
|
-
/**
|
|
810
|
-
* The model's schema instance.
|
|
811
|
-
*
|
|
812
|
-
* @member {module:engine/model/schema~Schema} #schema
|
|
813
|
-
*/
|
|
814
|
-
|
|
815
|
-
/**
|
|
816
|
-
* The {@link module:engine/model/writer~Writer} instance used to manipulate the data during conversion.
|
|
817
|
-
*
|
|
818
|
-
* @member {module:engine/model/writer~Writer} #writer
|
|
819
|
-
*/
|
|
820
|
-
|
|
821
|
-
/**
|
|
822
|
-
* Conversion data.
|
|
823
|
-
*
|
|
824
|
-
* **Note:** Keep in mind that this object is shared by reference between all conversion callbacks that will be called.
|
|
825
|
-
* This means that callbacks can override values if needed, and these values will be available in other callbacks.
|
|
826
|
-
*
|
|
827
|
-
* @typedef {Object} module:engine/conversion/upcastdispatcher~UpcastConversionData
|
|
828
|
-
*
|
|
829
|
-
* @property {module:engine/view/item~Item} viewItem The converted item.
|
|
830
|
-
* @property {module:engine/model/position~Position} modelCursor The position where the converter should start changes.
|
|
831
|
-
* Change this value for the next converter to tell where the conversion should continue.
|
|
832
|
-
* @property {module:engine/model/range~Range} [modelRange] The current state of conversion result. Every change to
|
|
833
|
-
* the converted element should be reflected by setting or modifying this property.
|
|
834
|
-
*/
|