@ckeditor/ckeditor5-engine 35.0.1 → 35.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +176 -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 +980 -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 +757 -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 +199 -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,31 +2,23 @@
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/controller/datacontroller
8
7
  */
9
-
10
- import mix from '@ckeditor/ckeditor5-utils/src/mix';
11
- import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
8
+ import { Observable } from '@ckeditor/ckeditor5-utils/src/observablemixin';
12
9
  import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
13
-
10
+ import { Emitter } from '@ckeditor/ckeditor5-utils/src/emittermixin';
14
11
  import Mapper from '../conversion/mapper';
15
-
16
12
  import DowncastDispatcher from '../conversion/downcastdispatcher';
17
13
  import { insertAttributesAndChildren, insertText } from '../conversion/downcasthelpers';
18
-
19
14
  import UpcastDispatcher from '../conversion/upcastdispatcher';
20
15
  import { convertText, convertToModelFragment } from '../conversion/upcasthelpers';
21
-
22
16
  import ViewDocumentFragment from '../view/documentfragment';
23
17
  import ViewDocument from '../view/document';
24
18
  import ViewDowncastWriter from '../view/downcastwriter';
25
-
26
19
  import ModelRange from '../model/range';
27
20
  import { autoParagraphEmptyRoots } from '../model/utils/autoparagraphing';
28
21
  import HtmlDataProcessor from '../dataprocessor/htmldataprocessor';
29
-
30
22
  /**
31
23
  * Controller for the data pipeline. The data pipeline controls how data is retrieved from the document
32
24
  * and set inside it. Hence, the controller features two methods which allow to {@link ~DataController#get get}
@@ -42,562 +34,476 @@ import HtmlDataProcessor from '../dataprocessor/htmldataprocessor';
42
34
  *
43
35
  * editor.data.get( { rootName: 'customRoot' } ); // -> '<p>Hello!</p>'
44
36
  *
45
- * @mixes module:utils/observablemixin~ObservableMixin
37
+ * @mixes module:utils/emittermixin~EmitterMixin
46
38
  */
47
- export default class DataController {
48
- /**
49
- * Creates a data controller instance.
50
- *
51
- * @param {module:engine/model/model~Model} model Data model.
52
- * @param {module:engine/view/stylesmap~StylesProcessor} stylesProcessor The styles processor instance.
53
- */
54
- constructor( model, stylesProcessor ) {
55
- /**
56
- * Data model.
57
- *
58
- * @readonly
59
- * @member {module:engine/model/model~Model}
60
- */
61
- this.model = model;
62
-
63
- /**
64
- * Mapper used for the conversion. It has no permanent bindings, because these are created while getting data and
65
- * ae cleared directly after the data are converted. However, the mapper is defined as a class property, because
66
- * it needs to be passed to the `DowncastDispatcher` as a conversion API.
67
- *
68
- * @readonly
69
- * @member {module:engine/conversion/mapper~Mapper}
70
- */
71
- this.mapper = new Mapper();
72
-
73
- /**
74
- * Downcast dispatcher used by the {@link #get get method}. Downcast converters should be attached to it.
75
- *
76
- * @readonly
77
- * @member {module:engine/conversion/downcastdispatcher~DowncastDispatcher}
78
- */
79
- this.downcastDispatcher = new DowncastDispatcher( {
80
- mapper: this.mapper,
81
- schema: model.schema
82
- } );
83
- this.downcastDispatcher.on( 'insert:$text', insertText(), { priority: 'lowest' } );
84
- this.downcastDispatcher.on( 'insert', insertAttributesAndChildren(), { priority: 'lowest' } );
85
-
86
- /**
87
- * Upcast dispatcher used by the {@link #set set method}. Upcast converters should be attached to it.
88
- *
89
- * @readonly
90
- * @member {module:engine/conversion/upcastdispatcher~UpcastDispatcher}
91
- */
92
- this.upcastDispatcher = new UpcastDispatcher( {
93
- schema: model.schema
94
- } );
95
-
96
- /**
97
- * The view document used by the data controller.
98
- *
99
- * @readonly
100
- * @member {module:engine/view/document~Document}
101
- */
102
- this.viewDocument = new ViewDocument( stylesProcessor );
103
-
104
- /**
105
- * Styles processor used during the conversion.
106
- *
107
- * @readonly
108
- * @member {module:engine/view/stylesmap~StylesProcessor}
109
- */
110
- this.stylesProcessor = stylesProcessor;
111
-
112
- /**
113
- * Data processor used specifically for HTML conversion.
114
- *
115
- * @readonly
116
- * @member {module:engine/dataprocessor/htmldataprocessor~HtmlDataProcessor} #htmlProcessor
117
- */
118
- this.htmlProcessor = new HtmlDataProcessor( this.viewDocument );
119
-
120
- /**
121
- * Data processor used during the conversion.
122
- * Same instance as {@link #htmlProcessor} by default. Can be replaced at run time to handle different format, e.g. XML or Markdown.
123
- *
124
- * @member {module:engine/dataprocessor/dataprocessor~DataProcessor} #processor
125
- */
126
- this.processor = this.htmlProcessor;
127
-
128
- /**
129
- * The view downcast writer just for data conversion purposes, i.e. to modify
130
- * the {@link #viewDocument}.
131
- *
132
- * @private
133
- * @readonly
134
- * @member {module:engine/view/downcastwriter~DowncastWriter}
135
- */
136
- this._viewWriter = new ViewDowncastWriter( this.viewDocument );
137
-
138
- // Define default converters for text and elements.
139
- //
140
- // Note that if there is no default converter for the element it will be skipped, for instance `<b>foo</b>` will be
141
- // converted to nothing. We therefore add `convertToModelFragment` as a last converter so it converts children of that
142
- // element to the document fragment so `<b>foo</b>` will still be converted to `foo` even if there is no converter for `<b>`.
143
- this.upcastDispatcher.on( 'text', convertText(), { priority: 'lowest' } );
144
- this.upcastDispatcher.on( 'element', convertToModelFragment(), { priority: 'lowest' } );
145
- this.upcastDispatcher.on( 'documentFragment', convertToModelFragment(), { priority: 'lowest' } );
146
-
147
- this.decorate( 'init' );
148
- this.decorate( 'set' );
149
- this.decorate( 'get' );
150
-
151
- // Fire the `ready` event when the initialization has completed. Such low-level listener offers the possibility
152
- // to plug into the initialization pipeline without interrupting the initialization flow.
153
- this.on( 'init', () => {
154
- this.fire( 'ready' );
155
- }, { priority: 'lowest' } );
156
-
157
- // Fix empty roots after DataController is 'ready' (note that the init method could be decorated and stopped).
158
- // We need to handle this event because initial data could be empty and the post-fixer would not get triggered.
159
- this.on( 'ready', () => {
160
- this.model.enqueueChange( { isUndoable: false }, autoParagraphEmptyRoots );
161
- }, { priority: 'lowest' } );
162
- }
163
-
164
- /**
165
- * Returns the model's data converted by downcast dispatchers attached to {@link #downcastDispatcher} and
166
- * formatted by the {@link #processor data processor}.
167
- *
168
- * @fires get
169
- * @param {Object} [options] Additional configuration for the retrieved data. `DataController` provides two optional
170
- * properties: `rootName` and `trim`. Other properties of this object are specified by various editor features.
171
- * @param {String} [options.rootName='main'] Root name.
172
- * @param {String} [options.trim='empty'] Whether returned data should be trimmed. This option is set to `empty` by default,
173
- * which means whenever editor content is considered empty, an empty string will be returned. To turn off trimming completely
174
- * use `'none'`. In such cases the exact content will be returned (for example a `<p>&nbsp;</p>` for an empty editor).
175
- * @returns {String} Output data.
176
- */
177
- get( options = {} ) {
178
- const { rootName = 'main', trim = 'empty' } = options;
179
-
180
- if ( !this._checkIfRootsExists( [ rootName ] ) ) {
181
- /**
182
- * Cannot get data from a non-existing root. This error is thrown when {@link #get DataController#get() method}
183
- * is called with a non-existent root name. For example, if there is an editor instance with only `main` root,
184
- * calling {@link #get} like:
185
- *
186
- * data.get( { rootName: 'root2' } );
187
- *
188
- * will throw this error.
189
- *
190
- * @error datacontroller-get-non-existent-root
191
- */
192
- throw new CKEditorError( 'datacontroller-get-non-existent-root', this );
193
- }
194
-
195
- const root = this.model.document.getRoot( rootName );
196
-
197
- if ( trim === 'empty' && !this.model.hasContent( root, { ignoreWhitespaces: true } ) ) {
198
- return '';
199
- }
200
-
201
- return this.stringify( root, options );
202
- }
203
-
204
- /**
205
- * Returns the content of the given {@link module:engine/model/element~Element model's element} or
206
- * {@link module:engine/model/documentfragment~DocumentFragment model document fragment} converted by the downcast converters
207
- * attached to the {@link #downcastDispatcher} and formatted by the {@link #processor data processor}.
208
- *
209
- * @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} modelElementOrFragment
210
- * The element whose content will be stringified.
211
- * @param {Object} [options] Additional configuration passed to the conversion process.
212
- * @returns {String} Output data.
213
- */
214
- stringify( modelElementOrFragment, options = {} ) {
215
- // Model -> view.
216
- const viewDocumentFragment = this.toView( modelElementOrFragment, options );
217
-
218
- // View -> data.
219
- return this.processor.toData( viewDocumentFragment );
220
- }
221
-
222
- /**
223
- * Returns the content of the given {@link module:engine/model/element~Element model element} or
224
- * {@link module:engine/model/documentfragment~DocumentFragment model document fragment} converted by the downcast
225
- * converters attached to {@link #downcastDispatcher} into a
226
- * {@link module:engine/view/documentfragment~DocumentFragment view document fragment}.
227
- *
228
- * @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} modelElementOrFragment
229
- * Element or document fragment whose content will be converted.
230
- * @param {Object} [options={}] Additional configuration that will be available through the
231
- * {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi#options} during the conversion process.
232
- * @returns {module:engine/view/documentfragment~DocumentFragment} Output view DocumentFragment.
233
- */
234
- toView( modelElementOrFragment, options = {} ) {
235
- const viewDocument = this.viewDocument;
236
- const viewWriter = this._viewWriter;
237
-
238
- // Clear bindings so the call to this method returns correct results.
239
- this.mapper.clearBindings();
240
-
241
- // First, convert elements.
242
- const modelRange = ModelRange._createIn( modelElementOrFragment );
243
- const viewDocumentFragment = new ViewDocumentFragment( viewDocument );
244
-
245
- this.mapper.bindElements( modelElementOrFragment, viewDocumentFragment );
246
-
247
- // Prepare list of markers.
248
- // For document fragment, simply take the markers assigned to this document fragment.
249
- // For model root, all markers in that root will be taken.
250
- // For model element, we need to check which markers are intersecting with this element and relatively modify the markers' ranges.
251
- // Collapsed markers at element boundary, although considered as not intersecting with the element, will also be returned.
252
- const markers = modelElementOrFragment.is( 'documentFragment' ) ?
253
- modelElementOrFragment.markers :
254
- _getMarkersRelativeToElement( modelElementOrFragment );
255
-
256
- this.downcastDispatcher.convert( modelRange, markers, viewWriter, options );
257
-
258
- return viewDocumentFragment;
259
- }
260
-
261
- /**
262
- * Sets the initial input data parsed by the {@link #processor data processor} and
263
- * converted by the {@link #upcastDispatcher view-to-model converters}.
264
- * Initial data can be only set to a document whose {@link module:engine/model/document~Document#version} is equal 0.
265
- *
266
- * **Note** This method is {@link module:utils/observablemixin~ObservableMixin#decorate decorated} which is
267
- * used by e.g. collaborative editing plugin that syncs remote data on init.
268
- *
269
- * When data is passed as a string, it is initialized on the default `main` root:
270
- *
271
- * dataController.init( '<p>Foo</p>' ); // Initializes data on the `main` root only, as no other is specified.
272
- *
273
- * To initialize data on a different root or multiple roots at once, an object containing `rootName` - `data` pairs should be passed:
274
- *
275
- * dataController.init( { main: '<p>Foo</p>', title: '<h1>Bar</h1>' } ); // Initializes data on both the `main` and `title` roots.
276
- *
277
- * @fires init
278
- * @param {String|Object.<String,String>} data Input data as a string or an object containing the `rootName` - `data`
279
- * pairs to initialize data on multiple roots at once.
280
- * @returns {Promise} Promise that is resolved after the data is set on the editor.
281
- */
282
- init( data ) {
283
- if ( this.model.document.version ) {
284
- /**
285
- * Cannot set initial data to a non-empty {@link module:engine/model/document~Document}.
286
- * Initial data should be set once, during the {@link module:core/editor/editor~Editor} initialization,
287
- * when the {@link module:engine/model/document~Document#version} is equal 0.
288
- *
289
- * @error datacontroller-init-document-not-empty
290
- */
291
- throw new CKEditorError( 'datacontroller-init-document-not-empty', this );
292
- }
293
-
294
- let initialData = {};
295
- if ( typeof data === 'string' ) {
296
- initialData.main = data; // Default root is 'main'. To initiate data on a different root, object should be passed.
297
- } else {
298
- initialData = data;
299
- }
300
-
301
- if ( !this._checkIfRootsExists( Object.keys( initialData ) ) ) {
302
- /**
303
- * Cannot init data on a non-existent root. This error is thrown when {@link #init DataController#init() method}
304
- * is called with non-existent root name. For example, if there is an editor instance with only `main` root,
305
- * calling {@link #init} like:
306
- *
307
- * data.init( { main: '<p>Foo</p>', root2: '<p>Bar</p>' } );
308
- *
309
- * will throw this error.
310
- *
311
- * @error datacontroller-init-non-existent-root
312
- */
313
- throw new CKEditorError( 'datacontroller-init-non-existent-root', this );
314
- }
315
-
316
- this.model.enqueueChange( { isUndoable: false }, writer => {
317
- for ( const rootName of Object.keys( initialData ) ) {
318
- const modelRoot = this.model.document.getRoot( rootName );
319
- writer.insert( this.parse( initialData[ rootName ], modelRoot ), modelRoot, 0 );
320
- }
321
- } );
322
-
323
- return Promise.resolve();
324
- }
325
-
326
- /**
327
- * Sets the input data parsed by the {@link #processor data processor} and
328
- * converted by the {@link #upcastDispatcher view-to-model converters}.
329
- * This method can be used any time to replace existing editor data with the new one without clearing the
330
- * {@link module:engine/model/document~Document#history document history}.
331
- *
332
- * This method also creates a batch with all the changes applied. If all you need is to parse data, use
333
- * the {@link #parse} method.
334
- *
335
- * When data is passed as a string it is set on the default `main` root:
336
- *
337
- * dataController.set( '<p>Foo</p>' ); // Sets data on the `main` root, as no other is specified.
338
- *
339
- * To set data on a different root or multiple roots at once, an object containing `rootName` - `data` pairs should be passed:
340
- *
341
- * dataController.set( { main: '<p>Foo</p>', title: '<h1>Bar</h1>' } ); // Sets data on the `main` and `title` roots as specified.
342
- *
343
- * To set the data with a preserved undo stack and add the change to the undo stack, set `{ isUndoable: true }` as a `batchType` option.
344
- *
345
- * dataController.set( '<p>Foo</p>', { batchType: { isUndoable: true } } );
346
- *
347
- * @fires set
348
- * @param {String|Object.<String,String>} data Input data as a string or an object containing the `rootName` - `data`
349
- * pairs to set data on multiple roots at once.
350
- * @param {Object} [options={}] Options for setting data.
351
- * @param {Object} [options.batchType] The batch type that will be used to create a batch for the changes applied by this method.
352
- * By default, the batch will be set as {@link module:engine/model/batch~Batch#isUndoable not undoable} and the undo stack will be
353
- * cleared after the new data is applied (all undo steps will be removed). If the batch type `isUndoable` flag is be set to `true`,
354
- * the undo stack will be preserved instead and not cleared when new data is applied.
355
- */
356
- set( data, options = {} ) {
357
- let newData = {};
358
-
359
- if ( typeof data === 'string' ) {
360
- newData.main = data; // The default root is 'main'. To set data on a different root, an object should be passed.
361
- } else {
362
- newData = data;
363
- }
364
-
365
- if ( !this._checkIfRootsExists( Object.keys( newData ) ) ) {
366
- /**
367
- * Cannot set data on a non-existent root. This error is thrown when the {@link #set DataController#set() method}
368
- * is called with non-existent root name. For example, if there is an editor instance with only the default `main` root,
369
- * calling {@link #set} like:
370
- *
371
- * data.set( { main: '<p>Foo</p>', root2: '<p>Bar</p>' } );
372
- *
373
- * will throw this error.
374
- *
375
- * @error datacontroller-set-non-existent-root
376
- */
377
- throw new CKEditorError( 'datacontroller-set-non-existent-root', this );
378
- }
379
-
380
- this.model.enqueueChange( options.batchType || {}, writer => {
381
- writer.setSelection( null );
382
- writer.removeSelectionAttribute( this.model.document.selection.getAttributeKeys() );
383
-
384
- for ( const rootName of Object.keys( newData ) ) {
385
- // Save to model.
386
- const modelRoot = this.model.document.getRoot( rootName );
387
-
388
- writer.remove( writer.createRangeIn( modelRoot ) );
389
- writer.insert( this.parse( newData[ rootName ], modelRoot ), modelRoot, 0 );
390
- }
391
- } );
392
- }
393
-
394
- /**
395
- * Returns the data parsed by the {@link #processor data processor} and then converted by upcast converters
396
- * attached to the {@link #upcastDispatcher}.
397
- *
398
- * @see #set
399
- * @param {String} data Data to parse.
400
- * @param {module:engine/model/schema~SchemaContextDefinition} [context='$root'] Base context in which the view will
401
- * be converted to the model. See: {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher#convert}.
402
- * @returns {module:engine/model/documentfragment~DocumentFragment} Parsed data.
403
- */
404
- parse( data, context = '$root' ) {
405
- // data -> view
406
- const viewDocumentFragment = this.processor.toView( data );
407
-
408
- // view -> model
409
- return this.toModel( viewDocumentFragment, context );
410
- }
411
-
412
- /**
413
- * Returns the result of the given {@link module:engine/view/element~Element view element} or
414
- * {@link module:engine/view/documentfragment~DocumentFragment view document fragment} converted by the
415
- * {@link #upcastDispatcher view-to-model converters}, wrapped by {@link module:engine/model/documentfragment~DocumentFragment}.
416
- *
417
- * When marker elements were converted during the conversion process, it will be set as a document fragment's
418
- * {@link module:engine/model/documentfragment~DocumentFragment#markers static markers map}.
419
- *
420
- * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} viewElementOrFragment
421
- * The element or document fragment whose content will be converted.
422
- * @param {module:engine/model/schema~SchemaContextDefinition} [context='$root'] Base context in which the view will
423
- * be converted to the model. See: {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher#convert}.
424
- * @returns {module:engine/model/documentfragment~DocumentFragment} Output document fragment.
425
- */
426
- toModel( viewElementOrFragment, context = '$root' ) {
427
- return this.model.change( writer => {
428
- return this.upcastDispatcher.convert( viewElementOrFragment, writer, context );
429
- } );
430
- }
431
-
432
- /**
433
- * Adds the style processor normalization rules.
434
- *
435
- * You can implement your own rules as well as use one of the available processor rules:
436
- *
437
- * * background: {@link module:engine/view/styles/background~addBackgroundRules}
438
- * * border: {@link module:engine/view/styles/border~addBorderRules}
439
- * * margin: {@link module:engine/view/styles/margin~addMarginRules}
440
- * * padding: {@link module:engine/view/styles/padding~addPaddingRules}
441
- *
442
- * @param {Function} callback
443
- */
444
- addStyleProcessorRules( callback ) {
445
- callback( this.stylesProcessor );
446
- }
447
-
448
- /**
449
- * Registers a {@link module:engine/view/matcher~MatcherPattern} on an {@link #htmlProcessor htmlProcessor}
450
- * and a {@link #processor processor} for view elements whose content should be treated as raw data
451
- * and not processed during the conversion from DOM to view elements.
452
- *
453
- * The raw data can be later accessed by the {@link module:engine/view/element~Element#getCustomProperty view element custom property}
454
- * `"$rawContent"`.
455
- *
456
- * @param {module:engine/view/matcher~MatcherPattern} pattern Pattern matching all view elements whose content should
457
- * be treated as a raw data.
458
- */
459
- registerRawContentMatcher( pattern ) {
460
- // No need to register the pattern if both the `htmlProcessor` and `processor` are the same instances.
461
- if ( this.processor && this.processor !== this.htmlProcessor ) {
462
- this.processor.registerRawContentMatcher( pattern );
463
- }
464
-
465
- this.htmlProcessor.registerRawContentMatcher( pattern );
466
- }
467
-
468
- /**
469
- * Removes all event listeners set by the DataController.
470
- */
471
- destroy() {
472
- this.stopListening();
473
- }
474
-
475
- /**
476
- * Checks whether all provided root names are actually existing editor roots.
477
- *
478
- * @private
479
- * @param {Array.<String>} rootNames Root names to check.
480
- * @returns {Boolean} Whether all provided root names are existing editor roots.
481
- */
482
- _checkIfRootsExists( rootNames ) {
483
- for ( const rootName of rootNames ) {
484
- if ( !this.model.document.getRootNames().includes( rootName ) ) {
485
- return false;
486
- }
487
- }
488
-
489
- return true;
490
- }
491
-
492
- /**
493
- * Event fired once the data initialization has finished.
494
- *
495
- * @event ready
496
- */
497
-
498
- /**
499
- * An event fired after the {@link #init `init()` method} was run. It can be {@link #listenTo listened to} in order to adjust or modify
500
- * the initialization flow. However, if the `init` event is stopped or prevented, the {@link #event:ready `ready` event}
501
- * should be fired manually.
502
- *
503
- * The `init` event is fired by the decorated {@link #init} method.
504
- * See {@link module:utils/observablemixin~ObservableMixin#decorate} for more information and samples.
505
- *
506
- * @event init
507
- */
508
-
509
- /**
510
- * An event fired after {@link #set set() method} has been run.
511
- *
512
- * The `set` event is fired by the decorated {@link #set} method.
513
- * See {@link module:utils/observablemixin~ObservableMixin#decorate} for more information and samples.
514
- *
515
- * @event set
516
- */
517
-
518
- /**
519
- * Event fired after the {@link #get get() method} has been run.
520
- *
521
- * The `get` event is fired by the decorated {@link #get} method.
522
- * See {@link module:utils/observablemixin~ObservableMixin#decorate} for more information and samples.
523
- *
524
- * @event get
525
- */
39
+ export default class DataController extends Emitter {
40
+ /**
41
+ * Creates a data controller instance.
42
+ *
43
+ * @param {module:engine/model/model~Model} model Data model.
44
+ * @param {module:engine/view/stylesmap~StylesProcessor} stylesProcessor The styles processor instance.
45
+ */
46
+ constructor(model, stylesProcessor) {
47
+ super();
48
+ /**
49
+ * Data model.
50
+ *
51
+ * @readonly
52
+ * @member {module:engine/model/model~Model}
53
+ */
54
+ this.model = model;
55
+ /**
56
+ * Mapper used for the conversion. It has no permanent bindings, because these are created while getting data and
57
+ * ae cleared directly after the data are converted. However, the mapper is defined as a class property, because
58
+ * it needs to be passed to the `DowncastDispatcher` as a conversion API.
59
+ *
60
+ * @readonly
61
+ * @member {module:engine/conversion/mapper~Mapper}
62
+ */
63
+ this.mapper = new Mapper();
64
+ /**
65
+ * Downcast dispatcher used by the {@link #get get method}. Downcast converters should be attached to it.
66
+ *
67
+ * @readonly
68
+ * @member {module:engine/conversion/downcastdispatcher~DowncastDispatcher}
69
+ */
70
+ this.downcastDispatcher = new DowncastDispatcher({
71
+ mapper: this.mapper,
72
+ schema: model.schema
73
+ });
74
+ this.downcastDispatcher.on('insert:$text', insertText(), { priority: 'lowest' });
75
+ this.downcastDispatcher.on('insert', insertAttributesAndChildren(), { priority: 'lowest' });
76
+ /**
77
+ * Upcast dispatcher used by the {@link #set set method}. Upcast converters should be attached to it.
78
+ *
79
+ * @readonly
80
+ * @member {module:engine/conversion/upcastdispatcher~UpcastDispatcher}
81
+ */
82
+ this.upcastDispatcher = new UpcastDispatcher({
83
+ schema: model.schema
84
+ });
85
+ /**
86
+ * The view document used by the data controller.
87
+ *
88
+ * @readonly
89
+ * @member {module:engine/view/document~Document}
90
+ */
91
+ this.viewDocument = new ViewDocument(stylesProcessor);
92
+ /**
93
+ * Styles processor used during the conversion.
94
+ *
95
+ * @readonly
96
+ * @member {module:engine/view/stylesmap~StylesProcessor}
97
+ */
98
+ this.stylesProcessor = stylesProcessor;
99
+ /**
100
+ * Data processor used specifically for HTML conversion.
101
+ *
102
+ * @readonly
103
+ * @member {module:engine/dataprocessor/htmldataprocessor~HtmlDataProcessor} #htmlProcessor
104
+ */
105
+ this.htmlProcessor = new HtmlDataProcessor(this.viewDocument);
106
+ /**
107
+ * Data processor used during the conversion.
108
+ * Same instance as {@link #htmlProcessor} by default. Can be replaced at run time to handle different format, e.g. XML or Markdown.
109
+ *
110
+ * @member {module:engine/dataprocessor/dataprocessor~DataProcessor} #processor
111
+ */
112
+ this.processor = this.htmlProcessor;
113
+ /**
114
+ * The view downcast writer just for data conversion purposes, i.e. to modify
115
+ * the {@link #viewDocument}.
116
+ *
117
+ * @private
118
+ * @readonly
119
+ * @member {module:engine/view/downcastwriter~DowncastWriter}
120
+ */
121
+ this._viewWriter = new ViewDowncastWriter(this.viewDocument);
122
+ // Define default converters for text and elements.
123
+ //
124
+ // Note that if there is no default converter for the element it will be skipped, for instance `<b>foo</b>` will be
125
+ // converted to nothing. We therefore add `convertToModelFragment` as a last converter so it converts children of that
126
+ // element to the document fragment so `<b>foo</b>` will still be converted to `foo` even if there is no converter for `<b>`.
127
+ this.upcastDispatcher.on('text', convertText(), { priority: 'lowest' });
128
+ this.upcastDispatcher.on('element', convertToModelFragment(), { priority: 'lowest' });
129
+ this.upcastDispatcher.on('documentFragment', convertToModelFragment(), { priority: 'lowest' });
130
+ Observable.prototype.decorate.call(this, 'init');
131
+ Observable.prototype.decorate.call(this, 'set');
132
+ Observable.prototype.decorate.call(this, 'get');
133
+ // Fire the `ready` event when the initialization has completed. Such low-level listener offers the possibility
134
+ // to plug into the initialization pipeline without interrupting the initialization flow.
135
+ this.on('init', () => {
136
+ this.fire('ready');
137
+ }, { priority: 'lowest' });
138
+ // Fix empty roots after DataController is 'ready' (note that the init method could be decorated and stopped).
139
+ // We need to handle this event because initial data could be empty and the post-fixer would not get triggered.
140
+ this.on('ready', () => {
141
+ this.model.enqueueChange({ isUndoable: false }, autoParagraphEmptyRoots);
142
+ }, { priority: 'lowest' });
143
+ }
144
+ /**
145
+ * Returns the model's data converted by downcast dispatchers attached to {@link #downcastDispatcher} and
146
+ * formatted by the {@link #processor data processor}.
147
+ *
148
+ * @fires get
149
+ * @param {Object} [options] Additional configuration for the retrieved data. `DataController` provides two optional
150
+ * properties: `rootName` and `trim`. Other properties of this object are specified by various editor features.
151
+ * @param {String} [options.rootName='main'] Root name.
152
+ * @param {String} [options.trim='empty'] Whether returned data should be trimmed. This option is set to `empty` by default,
153
+ * which means whenever editor content is considered empty, an empty string will be returned. To turn off trimming completely
154
+ * use `'none'`. In such cases the exact content will be returned (for example a `<p>&nbsp;</p>` for an empty editor).
155
+ * @returns {String} Output data.
156
+ */
157
+ get(options = {}) {
158
+ const { rootName = 'main', trim = 'empty' } = options;
159
+ if (!this._checkIfRootsExists([rootName])) {
160
+ /**
161
+ * Cannot get data from a non-existing root. This error is thrown when {@link #get DataController#get() method}
162
+ * is called with a non-existent root name. For example, if there is an editor instance with only `main` root,
163
+ * calling {@link #get} like:
164
+ *
165
+ * data.get( { rootName: 'root2' } );
166
+ *
167
+ * will throw this error.
168
+ *
169
+ * @error datacontroller-get-non-existent-root
170
+ */
171
+ throw new CKEditorError('datacontroller-get-non-existent-root', this);
172
+ }
173
+ const root = this.model.document.getRoot(rootName);
174
+ if (trim === 'empty' && !this.model.hasContent(root, { ignoreWhitespaces: true })) {
175
+ return '';
176
+ }
177
+ return this.stringify(root, options);
178
+ }
179
+ /**
180
+ * Returns the content of the given {@link module:engine/model/element~Element model's element} or
181
+ * {@link module:engine/model/documentfragment~DocumentFragment model document fragment} converted by the downcast converters
182
+ * attached to the {@link #downcastDispatcher} and formatted by the {@link #processor data processor}.
183
+ *
184
+ * @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} modelElementOrFragment
185
+ * The element whose content will be stringified.
186
+ * @param {Object} [options] Additional configuration passed to the conversion process.
187
+ * @returns {String} Output data.
188
+ */
189
+ stringify(modelElementOrFragment, options = {}) {
190
+ // Model -> view.
191
+ const viewDocumentFragment = this.toView(modelElementOrFragment, options);
192
+ // View -> data.
193
+ return this.processor.toData(viewDocumentFragment);
194
+ }
195
+ /**
196
+ * Returns the content of the given {@link module:engine/model/element~Element model element} or
197
+ * {@link module:engine/model/documentfragment~DocumentFragment model document fragment} converted by the downcast
198
+ * converters attached to {@link #downcastDispatcher} into a
199
+ * {@link module:engine/view/documentfragment~DocumentFragment view document fragment}.
200
+ *
201
+ * @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} modelElementOrFragment
202
+ * Element or document fragment whose content will be converted.
203
+ * @param {Object} [options={}] Additional configuration that will be available through the
204
+ * {@link module:engine/conversion/downcastdispatcher~DowncastConversionApi#options} during the conversion process.
205
+ * @returns {module:engine/view/documentfragment~DocumentFragment} Output view DocumentFragment.
206
+ */
207
+ toView(modelElementOrFragment, options = {}) {
208
+ const viewDocument = this.viewDocument;
209
+ const viewWriter = this._viewWriter;
210
+ // Clear bindings so the call to this method returns correct results.
211
+ this.mapper.clearBindings();
212
+ // First, convert elements.
213
+ const modelRange = ModelRange._createIn(modelElementOrFragment);
214
+ const viewDocumentFragment = new ViewDocumentFragment(viewDocument);
215
+ this.mapper.bindElements(modelElementOrFragment, viewDocumentFragment);
216
+ // Prepare list of markers.
217
+ // For document fragment, simply take the markers assigned to this document fragment.
218
+ // For model root, all markers in that root will be taken.
219
+ // For model element, we need to check which markers are intersecting with this element and relatively modify the markers' ranges.
220
+ // Collapsed markers at element boundary, although considered as not intersecting with the element, will also be returned.
221
+ const markers = modelElementOrFragment.is('documentFragment') ?
222
+ modelElementOrFragment.markers :
223
+ _getMarkersRelativeToElement(modelElementOrFragment);
224
+ this.downcastDispatcher.convert(modelRange, markers, viewWriter, options);
225
+ return viewDocumentFragment;
226
+ }
227
+ /**
228
+ * Sets the initial input data parsed by the {@link #processor data processor} and
229
+ * converted by the {@link #upcastDispatcher view-to-model converters}.
230
+ * Initial data can be only set to a document whose {@link module:engine/model/document~Document#version} is equal 0.
231
+ *
232
+ * **Note** This method is {@link module:utils/observablemixin~ObservableMixin#decorate decorated} which is
233
+ * used by e.g. collaborative editing plugin that syncs remote data on init.
234
+ *
235
+ * When data is passed as a string, it is initialized on the default `main` root:
236
+ *
237
+ * dataController.init( '<p>Foo</p>' ); // Initializes data on the `main` root only, as no other is specified.
238
+ *
239
+ * To initialize data on a different root or multiple roots at once, an object containing `rootName` - `data` pairs should be passed:
240
+ *
241
+ * dataController.init( { main: '<p>Foo</p>', title: '<h1>Bar</h1>' } ); // Initializes data on both the `main` and `title` roots.
242
+ *
243
+ * @fires init
244
+ * @param {String|Object.<String,String>} data Input data as a string or an object containing the `rootName` - `data`
245
+ * pairs to initialize data on multiple roots at once.
246
+ * @returns {Promise} Promise that is resolved after the data is set on the editor.
247
+ */
248
+ init(data) {
249
+ if (this.model.document.version) {
250
+ /**
251
+ * Cannot set initial data to a non-empty {@link module:engine/model/document~Document}.
252
+ * Initial data should be set once, during the {@link module:core/editor/editor~Editor} initialization,
253
+ * when the {@link module:engine/model/document~Document#version} is equal 0.
254
+ *
255
+ * @error datacontroller-init-document-not-empty
256
+ */
257
+ throw new CKEditorError('datacontroller-init-document-not-empty', this);
258
+ }
259
+ let initialData = {};
260
+ if (typeof data === 'string') {
261
+ initialData.main = data; // Default root is 'main'. To initiate data on a different root, object should be passed.
262
+ }
263
+ else {
264
+ initialData = data;
265
+ }
266
+ if (!this._checkIfRootsExists(Object.keys(initialData))) {
267
+ /**
268
+ * Cannot init data on a non-existent root. This error is thrown when {@link #init DataController#init() method}
269
+ * is called with non-existent root name. For example, if there is an editor instance with only `main` root,
270
+ * calling {@link #init} like:
271
+ *
272
+ * data.init( { main: '<p>Foo</p>', root2: '<p>Bar</p>' } );
273
+ *
274
+ * will throw this error.
275
+ *
276
+ * @error datacontroller-init-non-existent-root
277
+ */
278
+ throw new CKEditorError('datacontroller-init-non-existent-root', this);
279
+ }
280
+ this.model.enqueueChange({ isUndoable: false }, writer => {
281
+ for (const rootName of Object.keys(initialData)) {
282
+ const modelRoot = this.model.document.getRoot(rootName);
283
+ writer.insert(this.parse(initialData[rootName], modelRoot), modelRoot, 0);
284
+ }
285
+ });
286
+ return Promise.resolve();
287
+ }
288
+ /**
289
+ * Sets the input data parsed by the {@link #processor data processor} and
290
+ * converted by the {@link #upcastDispatcher view-to-model converters}.
291
+ * This method can be used any time to replace existing editor data with the new one without clearing the
292
+ * {@link module:engine/model/document~Document#history document history}.
293
+ *
294
+ * This method also creates a batch with all the changes applied. If all you need is to parse data, use
295
+ * the {@link #parse} method.
296
+ *
297
+ * When data is passed as a string it is set on the default `main` root:
298
+ *
299
+ * dataController.set( '<p>Foo</p>' ); // Sets data on the `main` root, as no other is specified.
300
+ *
301
+ * To set data on a different root or multiple roots at once, an object containing `rootName` - `data` pairs should be passed:
302
+ *
303
+ * dataController.set( { main: '<p>Foo</p>', title: '<h1>Bar</h1>' } ); // Sets data on the `main` and `title` roots as specified.
304
+ *
305
+ * To set the data with a preserved undo stack and add the change to the undo stack, set `{ isUndoable: true }` as a `batchType` option.
306
+ *
307
+ * dataController.set( '<p>Foo</p>', { batchType: { isUndoable: true } } );
308
+ *
309
+ * @fires set
310
+ * @param {String|Object.<String,String>} data Input data as a string or an object containing the `rootName` - `data`
311
+ * pairs to set data on multiple roots at once.
312
+ * @param {Object} [options={}] Options for setting data.
313
+ * @param {Object} [options.batchType] The batch type that will be used to create a batch for the changes applied by this method.
314
+ * By default, the batch will be set as {@link module:engine/model/batch~Batch#isUndoable not undoable} and the undo stack will be
315
+ * cleared after the new data is applied (all undo steps will be removed). If the batch type `isUndoable` flag is be set to `true`,
316
+ * the undo stack will be preserved instead and not cleared when new data is applied.
317
+ */
318
+ set(data, options = {}) {
319
+ let newData = {};
320
+ if (typeof data === 'string') {
321
+ newData.main = data; // The default root is 'main'. To set data on a different root, an object should be passed.
322
+ }
323
+ else {
324
+ newData = data;
325
+ }
326
+ if (!this._checkIfRootsExists(Object.keys(newData))) {
327
+ /**
328
+ * Cannot set data on a non-existent root. This error is thrown when the {@link #set DataController#set() method}
329
+ * is called with non-existent root name. For example, if there is an editor instance with only the default `main` root,
330
+ * calling {@link #set} like:
331
+ *
332
+ * data.set( { main: '<p>Foo</p>', root2: '<p>Bar</p>' } );
333
+ *
334
+ * will throw this error.
335
+ *
336
+ * @error datacontroller-set-non-existent-root
337
+ */
338
+ throw new CKEditorError('datacontroller-set-non-existent-root', this);
339
+ }
340
+ this.model.enqueueChange(options.batchType || {}, writer => {
341
+ writer.setSelection(null);
342
+ writer.removeSelectionAttribute(this.model.document.selection.getAttributeKeys());
343
+ for (const rootName of Object.keys(newData)) {
344
+ // Save to model.
345
+ const modelRoot = this.model.document.getRoot(rootName);
346
+ writer.remove(writer.createRangeIn(modelRoot));
347
+ writer.insert(this.parse(newData[rootName], modelRoot), modelRoot, 0);
348
+ }
349
+ });
350
+ }
351
+ /**
352
+ * Returns the data parsed by the {@link #processor data processor} and then converted by upcast converters
353
+ * attached to the {@link #upcastDispatcher}.
354
+ *
355
+ * @see #set
356
+ * @param {String} data Data to parse.
357
+ * @param {module:engine/model/schema~SchemaContextDefinition} [context='$root'] Base context in which the view will
358
+ * be converted to the model. See: {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher#convert}.
359
+ * @returns {module:engine/model/documentfragment~DocumentFragment} Parsed data.
360
+ */
361
+ parse(data, context = '$root') {
362
+ // data -> view
363
+ const viewDocumentFragment = this.processor.toView(data);
364
+ // view -> model
365
+ return this.toModel(viewDocumentFragment, context);
366
+ }
367
+ /**
368
+ * Returns the result of the given {@link module:engine/view/element~Element view element} or
369
+ * {@link module:engine/view/documentfragment~DocumentFragment view document fragment} converted by the
370
+ * {@link #upcastDispatcher view-to-model converters}, wrapped by {@link module:engine/model/documentfragment~DocumentFragment}.
371
+ *
372
+ * When marker elements were converted during the conversion process, it will be set as a document fragment's
373
+ * {@link module:engine/model/documentfragment~DocumentFragment#markers static markers map}.
374
+ *
375
+ * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} viewElementOrFragment
376
+ * The element or document fragment whose content will be converted.
377
+ * @param {module:engine/model/schema~SchemaContextDefinition} [context='$root'] Base context in which the view will
378
+ * be converted to the model. See: {@link module:engine/conversion/upcastdispatcher~UpcastDispatcher#convert}.
379
+ * @returns {module:engine/model/documentfragment~DocumentFragment} Output document fragment.
380
+ */
381
+ toModel(viewElementOrFragment, context = '$root') {
382
+ return this.model.change(writer => {
383
+ return this.upcastDispatcher.convert(viewElementOrFragment, writer, context);
384
+ });
385
+ }
386
+ /**
387
+ * Adds the style processor normalization rules.
388
+ *
389
+ * You can implement your own rules as well as use one of the available processor rules:
390
+ *
391
+ * * background: {@link module:engine/view/styles/background~addBackgroundRules}
392
+ * * border: {@link module:engine/view/styles/border~addBorderRules}
393
+ * * margin: {@link module:engine/view/styles/margin~addMarginRules}
394
+ * * padding: {@link module:engine/view/styles/padding~addPaddingRules}
395
+ *
396
+ * @param {Function} callback
397
+ */
398
+ addStyleProcessorRules(callback) {
399
+ callback(this.stylesProcessor);
400
+ }
401
+ /**
402
+ * Registers a {@link module:engine/view/matcher~MatcherPattern} on an {@link #htmlProcessor htmlProcessor}
403
+ * and a {@link #processor processor} for view elements whose content should be treated as raw data
404
+ * and not processed during the conversion from DOM to view elements.
405
+ *
406
+ * The raw data can be later accessed by the {@link module:engine/view/element~Element#getCustomProperty view element custom property}
407
+ * `"$rawContent"`.
408
+ *
409
+ * @param {module:engine/view/matcher~MatcherPattern} pattern Pattern matching all view elements whose content should
410
+ * be treated as a raw data.
411
+ */
412
+ registerRawContentMatcher(pattern) {
413
+ // No need to register the pattern if both the `htmlProcessor` and `processor` are the same instances.
414
+ if (this.processor && this.processor !== this.htmlProcessor) {
415
+ this.processor.registerRawContentMatcher(pattern);
416
+ }
417
+ this.htmlProcessor.registerRawContentMatcher(pattern);
418
+ }
419
+ /**
420
+ * Removes all event listeners set by the DataController.
421
+ */
422
+ destroy() {
423
+ this.stopListening();
424
+ }
425
+ /**
426
+ * Checks whether all provided root names are actually existing editor roots.
427
+ *
428
+ * @private
429
+ * @param {Array.<String>} rootNames Root names to check.
430
+ * @returns {Boolean} Whether all provided root names are existing editor roots.
431
+ */
432
+ _checkIfRootsExists(rootNames) {
433
+ for (const rootName of rootNames) {
434
+ if (!this.model.document.getRootNames().includes(rootName)) {
435
+ return false;
436
+ }
437
+ }
438
+ return true;
439
+ }
526
440
  }
527
-
528
- mix( DataController, ObservableMixin );
529
-
530
441
  // Helper function for downcast conversion.
531
442
  //
532
443
  // Takes a document element (element that is added to a model document) and checks which markers are inside it. If the marker is collapsed
533
444
  // at element boundary, it is considered as contained inside the element and marker range is returned. Otherwise, if the marker is
534
445
  // intersecting with the element, the intersection is returned.
535
- function _getMarkersRelativeToElement( element ) {
536
- const result = [];
537
- const doc = element.root.document;
538
-
539
- if ( !doc ) {
540
- return new Map();
541
- }
542
-
543
- const elementRange = ModelRange._createIn( element );
544
-
545
- for ( const marker of doc.model.markers ) {
546
- const markerRange = marker.getRange();
547
-
548
- const isMarkerCollapsed = markerRange.isCollapsed;
549
- const isMarkerAtElementBoundary = markerRange.start.isEqual( elementRange.start ) || markerRange.end.isEqual( elementRange.end );
550
-
551
- if ( isMarkerCollapsed && isMarkerAtElementBoundary ) {
552
- result.push( [ marker.name, markerRange ] );
553
- } else {
554
- const updatedMarkerRange = elementRange.getIntersection( markerRange );
555
-
556
- if ( updatedMarkerRange ) {
557
- result.push( [ marker.name, updatedMarkerRange ] );
558
- }
559
- }
560
- }
561
-
562
- // Sort the markers in a stable fashion to ensure that the order in which they are
563
- // added to the model's marker collection does not affect how they are
564
- // downcast. One particular use case that we are targeting here, is one where
565
- // two markers are adjacent but not overlapping, such as an insertion/deletion
566
- // suggestion pair representing the replacement of a range of text. In this
567
- // case, putting the markers in DOM order causes the first marker's end to be
568
- // serialized right after the second marker's start, while putting the markers
569
- // in reverse DOM order causes it to be right before the second marker's
570
- // start. So, we sort these in a way that ensures non-intersecting ranges are in
571
- // reverse DOM order, and intersecting ranges are in something approximating
572
- // reverse DOM order (since reverse DOM order doesn't have a precise meaning
573
- // when working with intersecting ranges).
574
- result.sort( ( [ n1, r1 ], [ n2, r2 ] ) => {
575
- if ( r1.end.compareWith( r2.start ) !== 'after' ) {
576
- // m1.end <= m2.start -- m1 is entirely <= m2
577
- return 1;
578
- } else if ( r1.start.compareWith( r2.end ) !== 'before' ) {
579
- // m1.start >= m2.end -- m1 is entirely >= m2
580
- return -1;
581
- } else {
582
- // they overlap, so use their start positions as the primary sort key and
583
- // end positions as the secondary sort key
584
- switch ( r1.start.compareWith( r2.start ) ) {
585
- case 'before':
586
- return 1;
587
- case 'after':
588
- return -1;
589
- default:
590
- switch ( r1.end.compareWith( r2.end ) ) {
591
- case 'before':
592
- return 1;
593
- case 'after':
594
- return -1;
595
- default:
596
- return n2.localeCompare( n1 );
597
- }
598
- }
599
- }
600
- } );
601
-
602
- return new Map( result );
446
+ function _getMarkersRelativeToElement(element) {
447
+ const result = [];
448
+ const doc = element.root.document;
449
+ if (!doc) {
450
+ return new Map();
451
+ }
452
+ const elementRange = ModelRange._createIn(element);
453
+ for (const marker of doc.model.markers) {
454
+ const markerRange = marker.getRange();
455
+ const isMarkerCollapsed = markerRange.isCollapsed;
456
+ const isMarkerAtElementBoundary = markerRange.start.isEqual(elementRange.start) || markerRange.end.isEqual(elementRange.end);
457
+ if (isMarkerCollapsed && isMarkerAtElementBoundary) {
458
+ result.push([marker.name, markerRange]);
459
+ }
460
+ else {
461
+ const updatedMarkerRange = elementRange.getIntersection(markerRange);
462
+ if (updatedMarkerRange) {
463
+ result.push([marker.name, updatedMarkerRange]);
464
+ }
465
+ }
466
+ }
467
+ // Sort the markers in a stable fashion to ensure that the order in which they are
468
+ // added to the model's marker collection does not affect how they are
469
+ // downcast. One particular use case that we are targeting here, is one where
470
+ // two markers are adjacent but not overlapping, such as an insertion/deletion
471
+ // suggestion pair representing the replacement of a range of text. In this
472
+ // case, putting the markers in DOM order causes the first marker's end to be
473
+ // serialized right after the second marker's start, while putting the markers
474
+ // in reverse DOM order causes it to be right before the second marker's
475
+ // start. So, we sort these in a way that ensures non-intersecting ranges are in
476
+ // reverse DOM order, and intersecting ranges are in something approximating
477
+ // reverse DOM order (since reverse DOM order doesn't have a precise meaning
478
+ // when working with intersecting ranges).
479
+ result.sort(([n1, r1], [n2, r2]) => {
480
+ if (r1.end.compareWith(r2.start) !== 'after') {
481
+ // m1.end <= m2.start -- m1 is entirely <= m2
482
+ return 1;
483
+ }
484
+ else if (r1.start.compareWith(r2.end) !== 'before') {
485
+ // m1.start >= m2.end -- m1 is entirely >= m2
486
+ return -1;
487
+ }
488
+ else {
489
+ // they overlap, so use their start positions as the primary sort key and
490
+ // end positions as the secondary sort key
491
+ switch (r1.start.compareWith(r2.start)) {
492
+ case 'before':
493
+ return 1;
494
+ case 'after':
495
+ return -1;
496
+ default:
497
+ switch (r1.end.compareWith(r2.end)) {
498
+ case 'before':
499
+ return 1;
500
+ case 'after':
501
+ return -1;
502
+ default:
503
+ return n2.localeCompare(n1);
504
+ }
505
+ }
506
+ }
507
+ });
508
+ return new Map(result);
603
509
  }