@ckeditor/ckeditor5-engine 30.0.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 (117) hide show
  1. package/LICENSE.md +17 -0
  2. package/README.md +30 -0
  3. package/package.json +70 -0
  4. package/src/controller/datacontroller.js +563 -0
  5. package/src/controller/editingcontroller.js +149 -0
  6. package/src/conversion/conversion.js +644 -0
  7. package/src/conversion/conversionhelpers.js +40 -0
  8. package/src/conversion/downcastdispatcher.js +914 -0
  9. package/src/conversion/downcasthelpers.js +1706 -0
  10. package/src/conversion/mapper.js +696 -0
  11. package/src/conversion/modelconsumable.js +329 -0
  12. package/src/conversion/upcastdispatcher.js +807 -0
  13. package/src/conversion/upcasthelpers.js +997 -0
  14. package/src/conversion/viewconsumable.js +623 -0
  15. package/src/dataprocessor/basichtmlwriter.js +32 -0
  16. package/src/dataprocessor/dataprocessor.jsdoc +64 -0
  17. package/src/dataprocessor/htmldataprocessor.js +159 -0
  18. package/src/dataprocessor/htmlwriter.js +22 -0
  19. package/src/dataprocessor/xmldataprocessor.js +161 -0
  20. package/src/dev-utils/model.js +482 -0
  21. package/src/dev-utils/operationreplayer.js +140 -0
  22. package/src/dev-utils/utils.js +103 -0
  23. package/src/dev-utils/view.js +1091 -0
  24. package/src/index.js +52 -0
  25. package/src/model/batch.js +82 -0
  26. package/src/model/differ.js +1282 -0
  27. package/src/model/document.js +483 -0
  28. package/src/model/documentfragment.js +390 -0
  29. package/src/model/documentselection.js +1261 -0
  30. package/src/model/element.js +438 -0
  31. package/src/model/history.js +138 -0
  32. package/src/model/item.jsdoc +14 -0
  33. package/src/model/liveposition.js +182 -0
  34. package/src/model/liverange.js +221 -0
  35. package/src/model/markercollection.js +553 -0
  36. package/src/model/model.js +934 -0
  37. package/src/model/node.js +507 -0
  38. package/src/model/nodelist.js +217 -0
  39. package/src/model/operation/attributeoperation.js +202 -0
  40. package/src/model/operation/detachoperation.js +103 -0
  41. package/src/model/operation/insertoperation.js +188 -0
  42. package/src/model/operation/markeroperation.js +154 -0
  43. package/src/model/operation/mergeoperation.js +216 -0
  44. package/src/model/operation/moveoperation.js +209 -0
  45. package/src/model/operation/nooperation.js +58 -0
  46. package/src/model/operation/operation.js +139 -0
  47. package/src/model/operation/operationfactory.js +49 -0
  48. package/src/model/operation/renameoperation.js +155 -0
  49. package/src/model/operation/rootattributeoperation.js +211 -0
  50. package/src/model/operation/splitoperation.js +254 -0
  51. package/src/model/operation/transform.js +2389 -0
  52. package/src/model/operation/utils.js +292 -0
  53. package/src/model/position.js +1164 -0
  54. package/src/model/range.js +1049 -0
  55. package/src/model/rootelement.js +111 -0
  56. package/src/model/schema.js +1851 -0
  57. package/src/model/selection.js +902 -0
  58. package/src/model/text.js +138 -0
  59. package/src/model/textproxy.js +279 -0
  60. package/src/model/treewalker.js +414 -0
  61. package/src/model/utils/autoparagraphing.js +77 -0
  62. package/src/model/utils/deletecontent.js +528 -0
  63. package/src/model/utils/getselectedcontent.js +150 -0
  64. package/src/model/utils/insertcontent.js +824 -0
  65. package/src/model/utils/modifyselection.js +229 -0
  66. package/src/model/utils/selection-post-fixer.js +297 -0
  67. package/src/model/writer.js +1574 -0
  68. package/src/view/attributeelement.js +274 -0
  69. package/src/view/containerelement.js +123 -0
  70. package/src/view/document.js +221 -0
  71. package/src/view/documentfragment.js +273 -0
  72. package/src/view/documentselection.js +387 -0
  73. package/src/view/domconverter.js +1437 -0
  74. package/src/view/downcastwriter.js +2121 -0
  75. package/src/view/editableelement.js +118 -0
  76. package/src/view/element.js +945 -0
  77. package/src/view/elementdefinition.jsdoc +59 -0
  78. package/src/view/emptyelement.js +119 -0
  79. package/src/view/filler.js +161 -0
  80. package/src/view/item.jsdoc +14 -0
  81. package/src/view/matcher.js +776 -0
  82. package/src/view/node.js +391 -0
  83. package/src/view/observer/arrowkeysobserver.js +58 -0
  84. package/src/view/observer/bubblingemittermixin.js +307 -0
  85. package/src/view/observer/bubblingeventinfo.js +71 -0
  86. package/src/view/observer/clickobserver.js +46 -0
  87. package/src/view/observer/compositionobserver.js +79 -0
  88. package/src/view/observer/domeventdata.js +82 -0
  89. package/src/view/observer/domeventobserver.js +99 -0
  90. package/src/view/observer/fakeselectionobserver.js +118 -0
  91. package/src/view/observer/focusobserver.js +106 -0
  92. package/src/view/observer/inputobserver.js +44 -0
  93. package/src/view/observer/keyobserver.js +83 -0
  94. package/src/view/observer/mouseobserver.js +56 -0
  95. package/src/view/observer/mutationobserver.js +345 -0
  96. package/src/view/observer/observer.js +118 -0
  97. package/src/view/observer/selectionobserver.js +242 -0
  98. package/src/view/placeholder.js +285 -0
  99. package/src/view/position.js +426 -0
  100. package/src/view/range.js +533 -0
  101. package/src/view/rawelement.js +148 -0
  102. package/src/view/renderer.js +1037 -0
  103. package/src/view/rooteditableelement.js +107 -0
  104. package/src/view/selection.js +718 -0
  105. package/src/view/styles/background.js +73 -0
  106. package/src/view/styles/border.js +362 -0
  107. package/src/view/styles/margin.js +41 -0
  108. package/src/view/styles/padding.js +40 -0
  109. package/src/view/styles/utils.js +277 -0
  110. package/src/view/stylesmap.js +938 -0
  111. package/src/view/text.js +147 -0
  112. package/src/view/textproxy.js +199 -0
  113. package/src/view/treewalker.js +496 -0
  114. package/src/view/uielement.js +238 -0
  115. package/src/view/upcastwriter.js +484 -0
  116. package/src/view/view.js +721 -0
  117. package/theme/placeholder.css +27 -0
@@ -0,0 +1,390 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module module:engine/model/documentfragment
8
+ */
9
+
10
+ import NodeList from './nodelist';
11
+ import Element from './element';
12
+ import Text from './text';
13
+ import TextProxy from './textproxy';
14
+ import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
15
+
16
+ // @if CK_DEBUG_ENGINE // const { stringifyMap } = require( '../dev-utils/utils' );
17
+
18
+ /**
19
+ * DocumentFragment represents a part of model which does not have a common root but its top-level nodes
20
+ * can be seen as siblings. In other words, it is a detached part of model tree, without a root.
21
+ *
22
+ * DocumentFragment has own {@link module:engine/model/markercollection~MarkerCollection}. Markers from this collection
23
+ * will be set to the {@link module:engine/model/model~Model#markers model markers} by a
24
+ * {@link module:engine/model/writer~Writer#insert} function.
25
+ */
26
+ export default class DocumentFragment {
27
+ /**
28
+ * Creates an empty `DocumentFragment`.
29
+ *
30
+ * **Note:** Constructor of this class shouldn't be used directly in the code.
31
+ * Use the {@link module:engine/model/writer~Writer#createDocumentFragment} method instead.
32
+ *
33
+ * @protected
34
+ * @param {module:engine/model/node~Node|Iterable.<module:engine/model/node~Node>} [children]
35
+ * Nodes to be contained inside the `DocumentFragment`.
36
+ */
37
+ constructor( children ) {
38
+ /**
39
+ * DocumentFragment static markers map. This is a list of names and {@link module:engine/model/range~Range ranges}
40
+ * which will be set as Markers to {@link module:engine/model/model~Model#markers model markers collection}
41
+ * when DocumentFragment will be inserted to the document.
42
+ *
43
+ * @readonly
44
+ * @member {Map<String,module:engine/model/range~Range>} module:engine/model/documentfragment~DocumentFragment#markers
45
+ */
46
+ this.markers = new Map();
47
+
48
+ /**
49
+ * List of nodes contained inside the document fragment.
50
+ *
51
+ * @private
52
+ * @member {module:engine/model/nodelist~NodeList} module:engine/model/documentfragment~DocumentFragment#_children
53
+ */
54
+ this._children = new NodeList();
55
+
56
+ if ( children ) {
57
+ this._insertChild( 0, children );
58
+ }
59
+ }
60
+
61
+ /**
62
+ * Returns an iterator that iterates over all nodes contained inside this document fragment.
63
+ *
64
+ * @returns {Iterable.<module:engine/model/node~Node>}
65
+ */
66
+ [ Symbol.iterator ]() {
67
+ return this.getChildren();
68
+ }
69
+
70
+ /**
71
+ * Number of this document fragment's children.
72
+ *
73
+ * @readonly
74
+ * @type {Number}
75
+ */
76
+ get childCount() {
77
+ return this._children.length;
78
+ }
79
+
80
+ /**
81
+ * Sum of {@link module:engine/model/node~Node#offsetSize offset sizes} of all of this document fragment's children.
82
+ *
83
+ * @readonly
84
+ * @type {Number}
85
+ */
86
+ get maxOffset() {
87
+ return this._children.maxOffset;
88
+ }
89
+
90
+ /**
91
+ * Is `true` if there are no nodes inside this document fragment, `false` otherwise.
92
+ *
93
+ * @readonly
94
+ * @type {Boolean}
95
+ */
96
+ get isEmpty() {
97
+ return this.childCount === 0;
98
+ }
99
+
100
+ /**
101
+ * Artificial root of `DocumentFragment`. Returns itself. Added for compatibility reasons.
102
+ *
103
+ * @readonly
104
+ * @type {module:engine/model/documentfragment~DocumentFragment}
105
+ */
106
+ get root() {
107
+ return this;
108
+ }
109
+
110
+ /**
111
+ * Artificial parent of `DocumentFragment`. Returns `null`. Added for compatibility reasons.
112
+ *
113
+ * @readonly
114
+ * @type {null}
115
+ */
116
+ get parent() {
117
+ return null;
118
+ }
119
+
120
+ /**
121
+ * Checks whether this object is of the given type.
122
+ *
123
+ * docFrag.is( 'documentFragment' ); // -> true
124
+ * docFrag.is( 'model:documentFragment' ); // -> true
125
+ *
126
+ * docFrag.is( 'view:documentFragment' ); // -> false
127
+ * docFrag.is( 'element' ); // -> false
128
+ * docFrag.is( 'node' ); // -> false
129
+ *
130
+ * {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
131
+ *
132
+ * @param {String} type
133
+ * @returns {Boolean}
134
+ */
135
+ is( type ) {
136
+ return type === 'documentFragment' || type === 'model:documentFragment';
137
+ }
138
+
139
+ /**
140
+ * Gets the child at the given index. Returns `null` if incorrect index was passed.
141
+ *
142
+ * @param {Number} index Index of child.
143
+ * @returns {module:engine/model/node~Node|null} Child node.
144
+ */
145
+ getChild( index ) {
146
+ return this._children.getNode( index );
147
+ }
148
+
149
+ /**
150
+ * Returns an iterator that iterates over all of this document fragment's children.
151
+ *
152
+ * @returns {Iterable.<module:engine/model/node~Node>}
153
+ */
154
+ getChildren() {
155
+ return this._children[ Symbol.iterator ]();
156
+ }
157
+
158
+ /**
159
+ * Returns an index of the given child node. Returns `null` if given node is not a child of this document fragment.
160
+ *
161
+ * @param {module:engine/model/node~Node} node Child node to look for.
162
+ * @returns {Number|null} Child node's index.
163
+ */
164
+ getChildIndex( node ) {
165
+ return this._children.getNodeIndex( node );
166
+ }
167
+
168
+ /**
169
+ * Returns the starting offset of given child. Starting offset is equal to the sum of
170
+ * {@link module:engine/model/node~Node#offsetSize offset sizes} of all node's siblings that are before it. Returns `null` if
171
+ * given node is not a child of this document fragment.
172
+ *
173
+ * @param {module:engine/model/node~Node} node Child node to look for.
174
+ * @returns {Number|null} Child node's starting offset.
175
+ */
176
+ getChildStartOffset( node ) {
177
+ return this._children.getNodeStartOffset( node );
178
+ }
179
+
180
+ /**
181
+ * Returns path to a `DocumentFragment`, which is an empty array. Added for compatibility reasons.
182
+ *
183
+ * @returns {Array}
184
+ */
185
+ getPath() {
186
+ return [];
187
+ }
188
+
189
+ /**
190
+ * Returns a descendant node by its path relative to this element.
191
+ *
192
+ * // <this>a<b>c</b></this>
193
+ * this.getNodeByPath( [ 0 ] ); // -> "a"
194
+ * this.getNodeByPath( [ 1 ] ); // -> <b>
195
+ * this.getNodeByPath( [ 1, 0 ] ); // -> "c"
196
+ *
197
+ * @param {Array.<Number>} relativePath Path of the node to find, relative to this element.
198
+ * @returns {module:engine/model/node~Node|module:engine/model/documentfragment~DocumentFragment}
199
+ */
200
+ getNodeByPath( relativePath ) {
201
+ let node = this; // eslint-disable-line consistent-this
202
+
203
+ for ( const index of relativePath ) {
204
+ node = node.getChild( node.offsetToIndex( index ) );
205
+ }
206
+
207
+ return node;
208
+ }
209
+
210
+ /**
211
+ * Converts offset "position" to index "position".
212
+ *
213
+ * Returns index of a node that occupies given offset. If given offset is too low, returns `0`. If given offset is
214
+ * too high, returns index after last child}.
215
+ *
216
+ * const textNode = new Text( 'foo' );
217
+ * const pElement = new Element( 'p' );
218
+ * const docFrag = new DocumentFragment( [ textNode, pElement ] );
219
+ * docFrag.offsetToIndex( -1 ); // Returns 0, because offset is too low.
220
+ * docFrag.offsetToIndex( 0 ); // Returns 0, because offset 0 is taken by `textNode` which is at index 0.
221
+ * docFrag.offsetToIndex( 1 ); // Returns 0, because `textNode` has `offsetSize` equal to 3, so it occupies offset 1 too.
222
+ * docFrag.offsetToIndex( 2 ); // Returns 0.
223
+ * docFrag.offsetToIndex( 3 ); // Returns 1.
224
+ * docFrag.offsetToIndex( 4 ); // Returns 2. There are no nodes at offset 4, so last available index is returned.
225
+ *
226
+ * @param {Number} offset Offset to look for.
227
+ * @returns {Number} Index of a node that occupies given offset.
228
+ */
229
+ offsetToIndex( offset ) {
230
+ return this._children.offsetToIndex( offset );
231
+ }
232
+
233
+ /**
234
+ * Converts `DocumentFragment` instance to plain object and returns it.
235
+ * Takes care of converting all of this document fragment's children.
236
+ *
237
+ * @returns {Object} `DocumentFragment` instance converted to plain object.
238
+ */
239
+ toJSON() {
240
+ const json = [];
241
+
242
+ for ( const node of this._children ) {
243
+ json.push( node.toJSON() );
244
+ }
245
+
246
+ return json;
247
+ }
248
+
249
+ /**
250
+ * Creates a `DocumentFragment` instance from given plain object (i.e. parsed JSON string).
251
+ * Converts `DocumentFragment` children to proper nodes.
252
+ *
253
+ * @param {Object} json Plain object to be converted to `DocumentFragment`.
254
+ * @returns {module:engine/model/documentfragment~DocumentFragment} `DocumentFragment` instance created using given plain object.
255
+ */
256
+ static fromJSON( json ) {
257
+ const children = [];
258
+
259
+ for ( const child of json ) {
260
+ if ( child.name ) {
261
+ // If child has name property, it is an Element.
262
+ children.push( Element.fromJSON( child ) );
263
+ } else {
264
+ // Otherwise, it is a Text node.
265
+ children.push( Text.fromJSON( child ) );
266
+ }
267
+ }
268
+
269
+ return new DocumentFragment( children );
270
+ }
271
+
272
+ /**
273
+ * {@link #_insertChild Inserts} one or more nodes at the end of this document fragment.
274
+ *
275
+ * @protected
276
+ * @param {module:engine/model/item~Item|Iterable.<module:engine/model/item~Item>} items Items to be inserted.
277
+ */
278
+ _appendChild( items ) {
279
+ this._insertChild( this.childCount, items );
280
+ }
281
+
282
+ /**
283
+ * Inserts one or more nodes at the given index and sets {@link module:engine/model/node~Node#parent parent} of these nodes
284
+ * to this document fragment.
285
+ *
286
+ * @protected
287
+ * @param {Number} index Index at which nodes should be inserted.
288
+ * @param {module:engine/model/item~Item|Iterable.<module:engine/model/item~Item>} items Items to be inserted.
289
+ */
290
+ _insertChild( index, items ) {
291
+ const nodes = normalize( items );
292
+
293
+ for ( const node of nodes ) {
294
+ // If node that is being added to this element is already inside another element, first remove it from the old parent.
295
+ if ( node.parent !== null ) {
296
+ node._remove();
297
+ }
298
+
299
+ node.parent = this;
300
+ }
301
+
302
+ this._children._insertNodes( index, nodes );
303
+ }
304
+
305
+ /**
306
+ * Removes one or more nodes starting at the given index
307
+ * and sets {@link module:engine/model/node~Node#parent parent} of these nodes to `null`.
308
+ *
309
+ * @protected
310
+ * @param {Number} index Index of the first node to remove.
311
+ * @param {Number} [howMany=1] Number of nodes to remove.
312
+ * @returns {Array.<module:engine/model/node~Node>} Array containing removed nodes.
313
+ */
314
+ _removeChildren( index, howMany = 1 ) {
315
+ const nodes = this._children._removeNodes( index, howMany );
316
+
317
+ for ( const node of nodes ) {
318
+ node.parent = null;
319
+ }
320
+
321
+ return nodes;
322
+ }
323
+
324
+ // @if CK_DEBUG_ENGINE // toString() {
325
+ // @if CK_DEBUG_ENGINE // return 'documentFragment';
326
+ // @if CK_DEBUG_ENGINE // }
327
+
328
+ // @if CK_DEBUG_ENGINE // log() {
329
+ // @if CK_DEBUG_ENGINE // console.log( 'ModelDocumentFragment: ' + this );
330
+ // @if CK_DEBUG_ENGINE // }
331
+
332
+ // @if CK_DEBUG_ENGINE // printTree() {
333
+ // @if CK_DEBUG_ENGINE // let string = 'ModelDocumentFragment: [';
334
+
335
+ // @if CK_DEBUG_ENGINE // for ( const child of this.getChildren() ) {
336
+ // @if CK_DEBUG_ENGINE // string += '\n';
337
+
338
+ // @if CK_DEBUG_ENGINE // if ( child.is( '$text' ) ) {
339
+ // @if CK_DEBUG_ENGINE // const textAttrs = stringifyMap( child._attrs );
340
+
341
+ // @if CK_DEBUG_ENGINE // string += '\t'.repeat( 1 );
342
+
343
+ // @if CK_DEBUG_ENGINE // if ( textAttrs !== '' ) {
344
+ // @if CK_DEBUG_ENGINE // string += `<$text${ textAttrs }>` + child.data + '</$text>';
345
+ // @if CK_DEBUG_ENGINE // } else {
346
+ // @if CK_DEBUG_ENGINE // string += child.data;
347
+ // @if CK_DEBUG_ENGINE // }
348
+ // @if CK_DEBUG_ENGINE // } else {
349
+ // @if CK_DEBUG_ENGINE // string += child.printTree( 1 );
350
+ // @if CK_DEBUG_ENGINE // }
351
+ // @if CK_DEBUG_ENGINE // }
352
+
353
+ // @if CK_DEBUG_ENGINE // string += '\n]';
354
+
355
+ // @if CK_DEBUG_ENGINE // return string;
356
+ // @if CK_DEBUG_ENGINE // }
357
+
358
+ // @if CK_DEBUG_ENGINE // logTree() {
359
+ // @if CK_DEBUG_ENGINE // console.log( this.printTree() );
360
+ // @if CK_DEBUG_ENGINE // }
361
+ }
362
+
363
+ // Converts strings to Text and non-iterables to arrays.
364
+ //
365
+ // @param {String|module:engine/model/item~Item|Iterable.<module:engine/model/item~Item>}
366
+ // @returns {Iterable.<module:engine/model/node~Node>}
367
+ function normalize( nodes ) {
368
+ // Separate condition because string is iterable.
369
+ if ( typeof nodes == 'string' ) {
370
+ return [ new Text( nodes ) ];
371
+ }
372
+
373
+ if ( !isIterable( nodes ) ) {
374
+ nodes = [ nodes ];
375
+ }
376
+
377
+ // Array.from to enable .map() on non-arrays.
378
+ return Array.from( nodes )
379
+ .map( node => {
380
+ if ( typeof node == 'string' ) {
381
+ return new Text( node );
382
+ }
383
+
384
+ if ( node instanceof TextProxy ) {
385
+ return new Text( node.data, node.getAttributes() );
386
+ }
387
+
388
+ return node;
389
+ } );
390
+ }