@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.
Files changed (124) hide show
  1. package/CHANGELOG.md +4 -4
  2. package/package.json +30 -24
  3. package/src/controller/datacontroller.js +467 -561
  4. package/src/controller/editingcontroller.js +168 -204
  5. package/src/conversion/conversion.js +541 -565
  6. package/src/conversion/conversionhelpers.js +24 -28
  7. package/src/conversion/downcastdispatcher.js +457 -686
  8. package/src/conversion/downcasthelpers.js +1583 -1965
  9. package/src/conversion/mapper.js +518 -707
  10. package/src/conversion/modelconsumable.js +240 -283
  11. package/src/conversion/upcastdispatcher.js +372 -718
  12. package/src/conversion/upcasthelpers.js +707 -818
  13. package/src/conversion/viewconsumable.js +524 -581
  14. package/src/dataprocessor/basichtmlwriter.js +12 -16
  15. package/src/dataprocessor/dataprocessor.js +5 -0
  16. package/src/dataprocessor/htmldataprocessor.js +100 -116
  17. package/src/dataprocessor/htmlwriter.js +1 -18
  18. package/src/dataprocessor/xmldataprocessor.js +116 -137
  19. package/src/dev-utils/model.js +260 -352
  20. package/src/dev-utils/operationreplayer.js +106 -126
  21. package/src/dev-utils/utils.js +34 -51
  22. package/src/dev-utils/view.js +632 -753
  23. package/src/index.js +0 -11
  24. package/src/model/batch.js +111 -127
  25. package/src/model/differ.js +988 -1233
  26. package/src/model/document.js +340 -449
  27. package/src/model/documentfragment.js +327 -364
  28. package/src/model/documentselection.js +996 -1189
  29. package/src/model/element.js +306 -410
  30. package/src/model/history.js +224 -262
  31. package/src/model/item.js +5 -0
  32. package/src/model/liveposition.js +84 -145
  33. package/src/model/liverange.js +108 -185
  34. package/src/model/markercollection.js +379 -480
  35. package/src/model/model.js +883 -1034
  36. package/src/model/node.js +419 -463
  37. package/src/model/nodelist.js +175 -201
  38. package/src/model/operation/attributeoperation.js +153 -182
  39. package/src/model/operation/detachoperation.js +64 -83
  40. package/src/model/operation/insertoperation.js +135 -166
  41. package/src/model/operation/markeroperation.js +114 -140
  42. package/src/model/operation/mergeoperation.js +163 -191
  43. package/src/model/operation/moveoperation.js +157 -187
  44. package/src/model/operation/nooperation.js +28 -38
  45. package/src/model/operation/operation.js +106 -125
  46. package/src/model/operation/operationfactory.js +30 -34
  47. package/src/model/operation/renameoperation.js +109 -135
  48. package/src/model/operation/rootattributeoperation.js +155 -188
  49. package/src/model/operation/splitoperation.js +196 -232
  50. package/src/model/operation/transform.js +1833 -2204
  51. package/src/model/operation/utils.js +140 -204
  52. package/src/model/position.js +899 -1053
  53. package/src/model/range.js +910 -1028
  54. package/src/model/rootelement.js +77 -97
  55. package/src/model/schema.js +1189 -1835
  56. package/src/model/selection.js +745 -862
  57. package/src/model/text.js +90 -114
  58. package/src/model/textproxy.js +204 -240
  59. package/src/model/treewalker.js +316 -397
  60. package/src/model/typecheckable.js +16 -0
  61. package/src/model/utils/autoparagraphing.js +32 -44
  62. package/src/model/utils/deletecontent.js +334 -418
  63. package/src/model/utils/findoptimalinsertionrange.js +25 -36
  64. package/src/model/utils/getselectedcontent.js +96 -118
  65. package/src/model/utils/insertcontent.js +654 -773
  66. package/src/model/utils/insertobject.js +96 -119
  67. package/src/model/utils/modifyselection.js +120 -158
  68. package/src/model/utils/selection-post-fixer.js +153 -201
  69. package/src/model/writer.js +1305 -1474
  70. package/src/view/attributeelement.js +189 -225
  71. package/src/view/containerelement.js +75 -85
  72. package/src/view/document.js +172 -215
  73. package/src/view/documentfragment.js +200 -249
  74. package/src/view/documentselection.js +338 -367
  75. package/src/view/domconverter.js +1370 -1617
  76. package/src/view/downcastwriter.js +1747 -2076
  77. package/src/view/editableelement.js +81 -97
  78. package/src/view/element.js +739 -890
  79. package/src/view/elementdefinition.js +5 -0
  80. package/src/view/emptyelement.js +82 -92
  81. package/src/view/filler.js +35 -50
  82. package/src/view/item.js +5 -0
  83. package/src/view/matcher.js +260 -559
  84. package/src/view/node.js +274 -360
  85. package/src/view/observer/arrowkeysobserver.js +19 -28
  86. package/src/view/observer/bubblingemittermixin.js +120 -263
  87. package/src/view/observer/bubblingeventinfo.js +47 -55
  88. package/src/view/observer/clickobserver.js +7 -13
  89. package/src/view/observer/compositionobserver.js +14 -24
  90. package/src/view/observer/domeventdata.js +57 -67
  91. package/src/view/observer/domeventobserver.js +40 -64
  92. package/src/view/observer/fakeselectionobserver.js +81 -96
  93. package/src/view/observer/focusobserver.js +45 -61
  94. package/src/view/observer/inputobserver.js +7 -13
  95. package/src/view/observer/keyobserver.js +17 -27
  96. package/src/view/observer/mouseobserver.js +7 -14
  97. package/src/view/observer/mutationobserver.js +220 -315
  98. package/src/view/observer/observer.js +81 -102
  99. package/src/view/observer/selectionobserver.js +191 -246
  100. package/src/view/observer/tabobserver.js +23 -36
  101. package/src/view/placeholder.js +128 -173
  102. package/src/view/position.js +350 -401
  103. package/src/view/range.js +453 -513
  104. package/src/view/rawelement.js +85 -112
  105. package/src/view/renderer.js +874 -1018
  106. package/src/view/rooteditableelement.js +80 -90
  107. package/src/view/selection.js +608 -689
  108. package/src/view/styles/background.js +43 -44
  109. package/src/view/styles/border.js +220 -276
  110. package/src/view/styles/margin.js +8 -17
  111. package/src/view/styles/padding.js +8 -16
  112. package/src/view/styles/utils.js +127 -160
  113. package/src/view/stylesmap.js +728 -905
  114. package/src/view/text.js +102 -126
  115. package/src/view/textproxy.js +144 -170
  116. package/src/view/treewalker.js +383 -479
  117. package/src/view/typecheckable.js +19 -0
  118. package/src/view/uielement.js +166 -187
  119. package/src/view/upcastwriter.js +395 -449
  120. package/src/view/view.js +569 -664
  121. package/src/dataprocessor/dataprocessor.jsdoc +0 -64
  122. package/src/model/item.jsdoc +0 -14
  123. package/src/view/elementdefinition.jsdoc +0 -59
  124. 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 EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
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
- * Creates an upcast dispatcher that operates using the passed API.
118
- *
119
- * @see module:engine/conversion/upcastdispatcher~UpcastConversionApi
120
- * @param {Object} [conversionApi] Additional properties for an interface that will be passed to events fired
121
- * by the upcast dispatcher.
122
- */
123
- constructor( conversionApi = {} ) {
124
- /**
125
- * The list of elements that were created during splitting.
126
- *
127
- * After the conversion process, the list is cleared.
128
- *
129
- * @private
130
- * @type {Map.<module:engine/model/element~Element,Array.<module:engine/model/element~Element>>}
131
- */
132
- this._splitParts = new Map();
133
-
134
- /**
135
- * The list of cursor parent elements that were created during splitting.
136
- *
137
- * After the conversion process the list is cleared.
138
- *
139
- * @private
140
- * @type {Map.<module:engine/model/element~Element,Array.<module:engine/model/element~Element>>}
141
- */
142
- this._cursorParents = new Map();
143
-
144
- /**
145
- * The position in the temporary structure where the converted content is inserted. The structure reflects the context of
146
- * the target position where the content will be inserted. This property is built based on the context parameter of the
147
- * convert method.
148
- *
149
- * @private
150
- * @type {module:engine/model/position~Position|null}
151
- */
152
- this._modelCursor = null;
153
-
154
- /**
155
- * The list of elements that were created during the splitting but should not get removed on conversion end even if they are empty.
156
- *
157
- * The list is cleared after the conversion process.
158
- *
159
- * @private
160
- * @type {Set.<module:engine/model/element~Element>}
161
- */
162
- this._emptyElementsToKeep = new Set();
163
-
164
- /**
165
- * An interface passed by the dispatcher to the event callbacks.
166
- *
167
- * @member {module:engine/conversion/upcastdispatcher~UpcastConversionApi}
168
- */
169
- this.conversionApi = Object.assign( {}, conversionApi );
170
-
171
- // The below methods are bound to this `UpcastDispatcher` instance and set on `conversionApi`.
172
- // This way only a part of `UpcastDispatcher` API is exposed.
173
- this.conversionApi.convertItem = this._convertItem.bind( this );
174
- this.conversionApi.convertChildren = this._convertChildren.bind( this );
175
- this.conversionApi.safeInsert = this._safeInsert.bind( this );
176
- this.conversionApi.updateConversionResult = this._updateConversionResult.bind( this );
177
- // Advanced API - use only if custom position handling is needed.
178
- this.conversionApi.splitToAllowedParent = this._splitToAllowedParent.bind( this );
179
- this.conversionApi.getSplitParts = this._getSplitParts.bind( this );
180
- this.conversionApi.keepEmptyElement = this._keepEmptyElement.bind( this );
181
- }
182
-
183
- /**
184
- * Starts the conversion process. The entry point for the conversion.
185
- *
186
- * @fires element
187
- * @fires text
188
- * @fires documentFragment
189
- * @param {module:engine/view/documentfragment~DocumentFragment|module:engine/view/element~Element} viewItem
190
- * The part of the view to be converted.
191
- * @param {module:engine/model/writer~Writer} writer An instance of the model writer.
192
- * @param {module:engine/model/schema~SchemaContextDefinition} [context=['$root']] Elements will be converted according to this context.
193
- * @returns {module:engine/model/documentfragment~DocumentFragment} Model data that is the result of the conversion process
194
- * wrapped in `DocumentFragment`. Converted marker elements will be set as the document fragment's
195
- * {@link module:engine/model/documentfragment~DocumentFragment#markers static markers map}.
196
- */
197
- convert( viewItem, writer, context = [ '$root' ] ) {
198
- this.fire( 'viewCleanup', viewItem );
199
-
200
- // Create context tree and set position in the top element.
201
- // Items will be converted according to this position.
202
- this._modelCursor = createContextTree( context, writer );
203
-
204
- // Store writer in conversion as a conversion API
205
- // to be sure that conversion process will use the same batch.
206
- this.conversionApi.writer = writer;
207
-
208
- // Create consumable values list for conversion process.
209
- this.conversionApi.consumable = ViewConsumable.createFrom( viewItem );
210
-
211
- // Custom data stored by converter for conversion process.
212
- this.conversionApi.store = {};
213
-
214
- // Do the conversion.
215
- const { modelRange } = this._convertItem( viewItem, this._modelCursor );
216
-
217
- // Conversion result is always a document fragment so let's create it.
218
- const documentFragment = writer.createDocumentFragment();
219
-
220
- // When there is a conversion result.
221
- if ( modelRange ) {
222
- // Remove all empty elements that were create while splitting.
223
- this._removeEmptyElements();
224
-
225
- // Move all items that were converted in context tree to the document fragment.
226
- for ( const item of Array.from( this._modelCursor.parent.getChildren() ) ) {
227
- writer.append( item, documentFragment );
228
- }
229
-
230
- // Extract temporary markers elements from model and set as static markers collection.
231
- documentFragment.markers = extractMarkersFromModelFragment( documentFragment, writer );
232
- }
233
-
234
- // Clear context position.
235
- this._modelCursor = null;
236
-
237
- // Clear split elements & parents lists.
238
- this._splitParts.clear();
239
- this._cursorParents.clear();
240
- this._emptyElementsToKeep.clear();
241
-
242
- // Clear conversion API.
243
- this.conversionApi.writer = null;
244
- this.conversionApi.store = null;
245
-
246
- // Return fragment as conversion result.
247
- return documentFragment;
248
- }
249
-
250
- /**
251
- * @private
252
- * @see module:engine/conversion/upcastdispatcher~UpcastConversionApi#convertItem
253
- */
254
- _convertItem( viewItem, modelCursor ) {
255
- const data = Object.assign( { viewItem, modelCursor, modelRange: null } );
256
-
257
- if ( viewItem.is( 'element' ) ) {
258
- this.fire( 'element:' + viewItem.name, data, this.conversionApi );
259
- } else if ( viewItem.is( '$text' ) ) {
260
- this.fire( 'text', data, this.conversionApi );
261
- } else {
262
- this.fire( 'documentFragment', data, this.conversionApi );
263
- }
264
-
265
- // Handle incorrect conversion result.
266
- if ( data.modelRange && !( data.modelRange instanceof ModelRange ) ) {
267
- /**
268
- * Incorrect conversion result was dropped.
269
- *
270
- * {@link module:engine/model/range~Range Model range} should be a conversion result.
271
- *
272
- * @error view-conversion-dispatcher-incorrect-result
273
- */
274
- throw new CKEditorError( 'view-conversion-dispatcher-incorrect-result', this );
275
- }
276
-
277
- return { modelRange: data.modelRange, modelCursor: data.modelCursor };
278
- }
279
-
280
- /**
281
- * @private
282
- * @see module:engine/conversion/upcastdispatcher~UpcastConversionApi#convertChildren
283
- */
284
- _convertChildren( viewItem, elementOrModelCursor ) {
285
- let nextModelCursor = elementOrModelCursor.is( 'position' ) ?
286
- elementOrModelCursor : ModelPosition._createAt( elementOrModelCursor, 0 );
287
-
288
- const modelRange = new ModelRange( nextModelCursor );
289
-
290
- for ( const viewChild of Array.from( viewItem.getChildren() ) ) {
291
- const result = this._convertItem( viewChild, nextModelCursor );
292
-
293
- if ( result.modelRange instanceof ModelRange ) {
294
- modelRange.end = result.modelRange.end;
295
- nextModelCursor = result.modelCursor;
296
- }
297
- }
298
-
299
- return { modelRange, modelCursor: nextModelCursor };
300
- }
301
-
302
- /**
303
- * @private
304
- * @see module:engine/conversion/upcastdispatcher~UpcastConversionApi#safeInsert
305
- */
306
- _safeInsert( modelElement, position ) {
307
- // Find allowed parent for element that we are going to insert.
308
- // If current parent does not allow to insert element but one of the ancestors does
309
- // then split nodes to allowed parent.
310
- const splitResult = this._splitToAllowedParent( modelElement, position );
311
-
312
- // When there is no split result it means that we can't insert element to model tree, so let's skip it.
313
- if ( !splitResult ) {
314
- return false;
315
- }
316
-
317
- // Insert element on allowed position.
318
- this.conversionApi.writer.insert( modelElement, splitResult.position );
319
-
320
- return true;
321
- }
322
-
323
- /**
324
- * @private
325
- * @see module:engine/conversion/upcastdispatcher~UpcastConversionApi#updateConversionResult
326
- */
327
- _updateConversionResult( modelElement, data ) {
328
- const parts = this._getSplitParts( modelElement );
329
-
330
- const writer = this.conversionApi.writer;
331
-
332
- // Set conversion result range - only if not set already.
333
- if ( !data.modelRange ) {
334
- data.modelRange = writer.createRange(
335
- writer.createPositionBefore( modelElement ),
336
- writer.createPositionAfter( parts[ parts.length - 1 ] )
337
- );
338
- }
339
-
340
- const savedCursorParent = this._cursorParents.get( modelElement );
341
-
342
- // Now we need to check where the `modelCursor` should be.
343
- if ( savedCursorParent ) {
344
- // If we split parent to insert our element then we want to continue conversion in the new part of the split parent.
345
- //
346
- // before: <allowed><notAllowed>foo[]</notAllowed></allowed>
347
- // after: <allowed><notAllowed>foo</notAllowed> <converted></converted> <notAllowed>[]</notAllowed></allowed>
348
-
349
- data.modelCursor = writer.createPositionAt( savedCursorParent, 0 );
350
- } else {
351
- // Otherwise just continue after inserted element.
352
-
353
- data.modelCursor = data.modelRange.end;
354
- }
355
- }
356
-
357
- /**
358
- * @private
359
- * @see module:engine/conversion/upcastdispatcher~UpcastConversionApi#splitToAllowedParent
360
- */
361
- _splitToAllowedParent( node, modelCursor ) {
362
- const { schema, writer } = this.conversionApi;
363
-
364
- // Try to find allowed parent.
365
- let allowedParent = schema.findAllowedParent( modelCursor, node );
366
-
367
- if ( allowedParent ) {
368
- // When current position parent allows to insert node then return this position.
369
- if ( allowedParent === modelCursor.parent ) {
370
- return { position: modelCursor };
371
- }
372
-
373
- // When allowed parent is in context tree (it's outside the converted tree).
374
- if ( this._modelCursor.parent.getAncestors().includes( allowedParent ) ) {
375
- allowedParent = null;
376
- }
377
- }
378
-
379
- if ( !allowedParent ) {
380
- // Check if the node wrapped with a paragraph would be accepted by the schema.
381
- if ( !isParagraphable( modelCursor, node, schema ) ) {
382
- return null;
383
- }
384
-
385
- return {
386
- position: wrapInParagraph( modelCursor, writer )
387
- };
388
- }
389
-
390
- // Split element to allowed parent.
391
- const splitResult = this.conversionApi.writer.split( modelCursor, allowedParent );
392
-
393
- // Using the range returned by `model.Writer#split`, we will pair original elements with their split parts.
394
- //
395
- // The range returned from the writer spans "over the split" or, precisely saying, from the end of the original element (the one
396
- // that got split) to the beginning of the other part of that element:
397
- //
398
- // <limit><a><b><c>X[]Y</c></b><a></limit> ->
399
- // <limit><a><b><c>X[</c></b></a><a><b><c>]Y</c></b></a>
400
- //
401
- // After the split there cannot be any full node between the positions in `splitRange`. The positions are touching.
402
- // Also, because of how splitting works, it is easy to notice, that "closing tags" are in the reverse order than "opening tags".
403
- // Also, since we split all those elements, each of them has to have the other part.
404
- //
405
- // With those observations in mind, we will pair the original elements with their split parts by saving "closing tags" and matching
406
- // them with "opening tags" in the reverse order. For that we can use a stack.
407
- const stack = [];
408
-
409
- for ( const treeWalkerValue of splitResult.range.getWalker() ) {
410
- if ( treeWalkerValue.type == 'elementEnd' ) {
411
- stack.push( treeWalkerValue.item );
412
- } else {
413
- // There should not be any text nodes after the element is split, so the only other value is `elementStart`.
414
- const originalPart = stack.pop();
415
- const splitPart = treeWalkerValue.item;
416
-
417
- this._registerSplitPair( originalPart, splitPart );
418
- }
419
- }
420
-
421
- const cursorParent = splitResult.range.end.parent;
422
- this._cursorParents.set( node, cursorParent );
423
-
424
- return {
425
- position: splitResult.position,
426
- cursorParent
427
- };
428
- }
429
-
430
- /**
431
- * Registers that a `splitPart` element is a split part of the `originalPart` element.
432
- *
433
- * The data set by this method is used by {@link #_getSplitParts} and {@link #_removeEmptyElements}.
434
- *
435
- * @private
436
- * @param {module:engine/model/element~Element} originalPart
437
- * @param {module:engine/model/element~Element} splitPart
438
- */
439
- _registerSplitPair( originalPart, splitPart ) {
440
- if ( !this._splitParts.has( originalPart ) ) {
441
- this._splitParts.set( originalPart, [ originalPart ] );
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( modelItem, writer ) {
546
- const markerElements = new Set();
547
- const markers = new Map();
548
-
549
- // Create ModelTreeWalker.
550
- const range = ModelRange._createIn( modelItem ).getItems();
551
-
552
- // Walk through DocumentFragment and collect marker elements.
553
- for ( const item of range ) {
554
- // Check if current element is a marker.
555
- if ( item.name == '$marker' ) {
556
- markerElements.add( item );
557
- }
558
- }
559
-
560
- // Walk through collected marker elements store its path and remove its from the DocumentFragment.
561
- for ( const markerElement of markerElements ) {
562
- const markerName = markerElement.getAttribute( 'data-name' );
563
- const currentPosition = writer.createPositionBefore( markerElement );
564
-
565
- // When marker of given name is not stored it means that we have found the beginning of the range.
566
- if ( !markers.has( markerName ) ) {
567
- markers.set( markerName, new ModelRange( currentPosition.clone() ) );
568
- // Otherwise is means that we have found end of the marker range.
569
- } else {
570
- markers.get( markerName ).end = currentPosition.clone();
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( contextDefinition, writer ) {
582
- let position;
583
-
584
- for ( const item of new SchemaContext( contextDefinition ) ) {
585
- const attributes = {};
586
-
587
- for ( const key of item.getAttributeKeys() ) {
588
- attributes[ key ] = item.getAttribute( key );
589
- }
590
-
591
- const current = writer.createElement( item.name, attributes );
592
-
593
- if ( position ) {
594
- writer.append( current, position );
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
- */