@ckeditor/ckeditor5-engine 34.2.0 → 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 (125) hide show
  1. package/CHANGELOG.md +823 -0
  2. package/LICENSE.md +4 -0
  3. package/package.json +32 -25
  4. package/src/controller/datacontroller.js +467 -561
  5. package/src/controller/editingcontroller.js +168 -204
  6. package/src/conversion/conversion.js +541 -565
  7. package/src/conversion/conversionhelpers.js +24 -28
  8. package/src/conversion/downcastdispatcher.js +457 -686
  9. package/src/conversion/downcasthelpers.js +1583 -1965
  10. package/src/conversion/mapper.js +518 -707
  11. package/src/conversion/modelconsumable.js +240 -283
  12. package/src/conversion/upcastdispatcher.js +372 -718
  13. package/src/conversion/upcasthelpers.js +707 -818
  14. package/src/conversion/viewconsumable.js +524 -581
  15. package/src/dataprocessor/basichtmlwriter.js +12 -16
  16. package/src/dataprocessor/dataprocessor.js +5 -0
  17. package/src/dataprocessor/htmldataprocessor.js +101 -117
  18. package/src/dataprocessor/htmlwriter.js +1 -18
  19. package/src/dataprocessor/xmldataprocessor.js +117 -138
  20. package/src/dev-utils/model.js +260 -352
  21. package/src/dev-utils/operationreplayer.js +106 -126
  22. package/src/dev-utils/utils.js +34 -51
  23. package/src/dev-utils/view.js +632 -753
  24. package/src/index.js +0 -11
  25. package/src/model/batch.js +111 -127
  26. package/src/model/differ.js +988 -1233
  27. package/src/model/document.js +340 -449
  28. package/src/model/documentfragment.js +327 -364
  29. package/src/model/documentselection.js +996 -1189
  30. package/src/model/element.js +306 -410
  31. package/src/model/history.js +224 -262
  32. package/src/model/item.js +5 -0
  33. package/src/model/liveposition.js +84 -145
  34. package/src/model/liverange.js +108 -185
  35. package/src/model/markercollection.js +379 -480
  36. package/src/model/model.js +883 -1034
  37. package/src/model/node.js +419 -463
  38. package/src/model/nodelist.js +175 -201
  39. package/src/model/operation/attributeoperation.js +153 -182
  40. package/src/model/operation/detachoperation.js +64 -83
  41. package/src/model/operation/insertoperation.js +135 -166
  42. package/src/model/operation/markeroperation.js +114 -140
  43. package/src/model/operation/mergeoperation.js +163 -191
  44. package/src/model/operation/moveoperation.js +157 -187
  45. package/src/model/operation/nooperation.js +28 -38
  46. package/src/model/operation/operation.js +106 -125
  47. package/src/model/operation/operationfactory.js +30 -34
  48. package/src/model/operation/renameoperation.js +109 -135
  49. package/src/model/operation/rootattributeoperation.js +155 -188
  50. package/src/model/operation/splitoperation.js +196 -232
  51. package/src/model/operation/transform.js +1833 -2204
  52. package/src/model/operation/utils.js +140 -204
  53. package/src/model/position.js +899 -1053
  54. package/src/model/range.js +910 -1028
  55. package/src/model/rootelement.js +77 -97
  56. package/src/model/schema.js +1189 -1835
  57. package/src/model/selection.js +745 -862
  58. package/src/model/text.js +90 -114
  59. package/src/model/textproxy.js +204 -240
  60. package/src/model/treewalker.js +316 -397
  61. package/src/model/typecheckable.js +16 -0
  62. package/src/model/utils/autoparagraphing.js +32 -44
  63. package/src/model/utils/deletecontent.js +334 -418
  64. package/src/model/utils/findoptimalinsertionrange.js +25 -36
  65. package/src/model/utils/getselectedcontent.js +96 -118
  66. package/src/model/utils/insertcontent.js +654 -773
  67. package/src/model/utils/insertobject.js +96 -119
  68. package/src/model/utils/modifyselection.js +120 -158
  69. package/src/model/utils/selection-post-fixer.js +153 -201
  70. package/src/model/writer.js +1305 -1474
  71. package/src/view/attributeelement.js +189 -225
  72. package/src/view/containerelement.js +75 -85
  73. package/src/view/document.js +172 -215
  74. package/src/view/documentfragment.js +200 -249
  75. package/src/view/documentselection.js +338 -367
  76. package/src/view/domconverter.js +1371 -1613
  77. package/src/view/downcastwriter.js +1747 -2076
  78. package/src/view/editableelement.js +81 -97
  79. package/src/view/element.js +739 -890
  80. package/src/view/elementdefinition.js +5 -0
  81. package/src/view/emptyelement.js +82 -92
  82. package/src/view/filler.js +35 -50
  83. package/src/view/item.js +5 -0
  84. package/src/view/matcher.js +260 -559
  85. package/src/view/node.js +274 -360
  86. package/src/view/observer/arrowkeysobserver.js +19 -28
  87. package/src/view/observer/bubblingemittermixin.js +120 -263
  88. package/src/view/observer/bubblingeventinfo.js +47 -55
  89. package/src/view/observer/clickobserver.js +7 -13
  90. package/src/view/observer/compositionobserver.js +14 -24
  91. package/src/view/observer/domeventdata.js +57 -67
  92. package/src/view/observer/domeventobserver.js +40 -64
  93. package/src/view/observer/fakeselectionobserver.js +81 -96
  94. package/src/view/observer/focusobserver.js +45 -61
  95. package/src/view/observer/inputobserver.js +7 -13
  96. package/src/view/observer/keyobserver.js +17 -27
  97. package/src/view/observer/mouseobserver.js +7 -14
  98. package/src/view/observer/mutationobserver.js +220 -315
  99. package/src/view/observer/observer.js +81 -102
  100. package/src/view/observer/selectionobserver.js +191 -246
  101. package/src/view/observer/tabobserver.js +23 -36
  102. package/src/view/placeholder.js +128 -173
  103. package/src/view/position.js +350 -401
  104. package/src/view/range.js +453 -513
  105. package/src/view/rawelement.js +85 -112
  106. package/src/view/renderer.js +874 -1014
  107. package/src/view/rooteditableelement.js +80 -90
  108. package/src/view/selection.js +608 -689
  109. package/src/view/styles/background.js +43 -44
  110. package/src/view/styles/border.js +220 -276
  111. package/src/view/styles/margin.js +8 -17
  112. package/src/view/styles/padding.js +8 -16
  113. package/src/view/styles/utils.js +127 -160
  114. package/src/view/stylesmap.js +728 -905
  115. package/src/view/text.js +102 -126
  116. package/src/view/textproxy.js +144 -170
  117. package/src/view/treewalker.js +383 -479
  118. package/src/view/typecheckable.js +19 -0
  119. package/src/view/uielement.js +166 -187
  120. package/src/view/upcastwriter.js +395 -449
  121. package/src/view/view.js +569 -664
  122. package/src/dataprocessor/dataprocessor.jsdoc +0 -64
  123. package/src/model/item.jsdoc +0 -14
  124. package/src/view/elementdefinition.jsdoc +0 -59
  125. package/src/view/item.jsdoc +0 -14
package/src/model/node.js CHANGED
@@ -2,17 +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
-
5
+ /* eslint-disable @typescript-eslint/no-unused-vars */
6
6
  /**
7
7
  * @module engine/model/node
8
8
  */
9
-
9
+ import TypeCheckable from './typecheckable';
10
10
  import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
11
11
  import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
12
12
  import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';
13
13
  // To check if component is loaded more than once.
14
14
  import '@ckeditor/ckeditor5-utils/src/version';
15
-
16
15
  /**
17
16
  * Model node. Most basic structure of model tree.
18
17
  *
@@ -41,467 +40,424 @@ import '@ckeditor/ckeditor5-utils/src/version';
41
40
  * In case of {@link module:engine/model/element~Element element node}, adding and removing children also counts as changing a node and
42
41
  * follows same rules.
43
42
  */
44
- export default class Node {
45
- /**
46
- * Creates a model node.
47
- *
48
- * This is an abstract class, so this constructor should not be used directly.
49
- *
50
- * @abstract
51
- * @param {Object} [attrs] Node's attributes. See {@link module:utils/tomap~toMap} for a list of accepted values.
52
- */
53
- constructor( attrs ) {
54
- /**
55
- * Parent of this node. It could be {@link module:engine/model/element~Element}
56
- * or {@link module:engine/model/documentfragment~DocumentFragment}.
57
- * Equals to `null` if the node has no parent.
58
- *
59
- * @readonly
60
- * @member {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment|null}
61
- */
62
- this.parent = null;
63
-
64
- /**
65
- * Attributes set on this node.
66
- *
67
- * @private
68
- * @member {Map} module:engine/model/node~Node#_attrs
69
- */
70
- this._attrs = toMap( attrs );
71
- }
72
-
73
- /**
74
- * Index of this node in it's parent or `null` if the node has no parent.
75
- *
76
- * Accessing this property throws an error if this node's parent element does not contain it.
77
- * This means that model tree got broken.
78
- *
79
- * @readonly
80
- * @type {Number|null}
81
- */
82
- get index() {
83
- let pos;
84
-
85
- if ( !this.parent ) {
86
- return null;
87
- }
88
-
89
- if ( ( pos = this.parent.getChildIndex( this ) ) === null ) {
90
- throw new CKEditorError( 'model-node-not-found-in-parent', this );
91
- }
92
-
93
- return pos;
94
- }
95
-
96
- /**
97
- * Offset at which this node starts in it's parent. It is equal to the sum of {@link #offsetSize offsetSize}
98
- * of all it's previous siblings. Equals to `null` if node has no parent.
99
- *
100
- * Accessing this property throws an error if this node's parent element does not contain it.
101
- * This means that model tree got broken.
102
- *
103
- * @readonly
104
- * @type {Number|null}
105
- */
106
- get startOffset() {
107
- let pos;
108
-
109
- if ( !this.parent ) {
110
- return null;
111
- }
112
-
113
- if ( ( pos = this.parent.getChildStartOffset( this ) ) === null ) {
114
- throw new CKEditorError( 'model-node-not-found-in-parent', this );
115
- }
116
-
117
- return pos;
118
- }
119
-
120
- /**
121
- * Offset size of this node. Represents how much "offset space" is occupied by the node in it's parent.
122
- * It is important for {@link module:engine/model/position~Position position}. When node has `offsetSize` greater than `1`, position
123
- * can be placed between that node start and end. `offsetSize` greater than `1` is for nodes that represents more
124
- * than one entity, i.e. {@link module:engine/model/text~Text text node}.
125
- *
126
- * @readonly
127
- * @type {Number}
128
- */
129
- get offsetSize() {
130
- return 1;
131
- }
132
-
133
- /**
134
- * Offset at which this node ends in it's parent. It is equal to the sum of this node's
135
- * {@link module:engine/model/node~Node#startOffset start offset} and {@link #offsetSize offset size}.
136
- * Equals to `null` if the node has no parent.
137
- *
138
- * @readonly
139
- * @type {Number|null}
140
- */
141
- get endOffset() {
142
- if ( !this.parent ) {
143
- return null;
144
- }
145
-
146
- return this.startOffset + this.offsetSize;
147
- }
148
-
149
- /**
150
- * Node's next sibling or `null` if the node is a last child of it's parent or if the node has no parent.
151
- *
152
- * @readonly
153
- * @type {module:engine/model/node~Node|null}
154
- */
155
- get nextSibling() {
156
- const index = this.index;
157
-
158
- return ( index !== null && this.parent.getChild( index + 1 ) ) || null;
159
- }
160
-
161
- /**
162
- * Node's previous sibling or `null` if the node is a first child of it's parent or if the node has no parent.
163
- *
164
- * @readonly
165
- * @type {module:engine/model/node~Node|null}
166
- */
167
- get previousSibling() {
168
- const index = this.index;
169
-
170
- return ( index !== null && this.parent.getChild( index - 1 ) ) || null;
171
- }
172
-
173
- /**
174
- * The top-most ancestor of the node. If node has no parent it is the root itself. If the node is a part
175
- * of {@link module:engine/model/documentfragment~DocumentFragment}, it's `root` is equal to that `DocumentFragment`.
176
- *
177
- * @readonly
178
- * @type {module:engine/model/node~Node|module:engine/model/documentfragment~DocumentFragment}
179
- */
180
- get root() {
181
- let root = this; // eslint-disable-line consistent-this
182
-
183
- while ( root.parent ) {
184
- root = root.parent;
185
- }
186
-
187
- return root;
188
- }
189
-
190
- /**
191
- * Returns true if the node is in a tree rooted in the document (is a descendant of one of its roots).
192
- *
193
- * @returns {Boolean}
194
- */
195
- isAttached() {
196
- return this.root.is( 'rootElement' );
197
- }
198
-
199
- /**
200
- * Gets path to the node. The path is an array containing starting offsets of consecutive ancestors of this node,
201
- * beginning from {@link module:engine/model/node~Node#root root}, down to this node's starting offset. The path can be used to
202
- * create {@link module:engine/model/position~Position Position} instance.
203
- *
204
- * const abc = new Text( 'abc' );
205
- * const foo = new Text( 'foo' );
206
- * const h1 = new Element( 'h1', null, new Text( 'header' ) );
207
- * const p = new Element( 'p', null, [ abc, foo ] );
208
- * const div = new Element( 'div', null, [ h1, p ] );
209
- * foo.getPath(); // Returns [ 1, 3 ]. `foo` is in `p` which is in `div`. `p` starts at offset 1, while `foo` at 3.
210
- * h1.getPath(); // Returns [ 0 ].
211
- * div.getPath(); // Returns [].
212
- *
213
- * @returns {Array.<Number>} The path.
214
- */
215
- getPath() {
216
- const path = [];
217
- let node = this; // eslint-disable-line consistent-this
218
-
219
- while ( node.parent ) {
220
- path.unshift( node.startOffset );
221
- node = node.parent;
222
- }
223
-
224
- return path;
225
- }
226
-
227
- /**
228
- * Returns ancestors array of this node.
229
- *
230
- * @param {Object} options Options object.
231
- * @param {Boolean} [options.includeSelf=false] When set to `true` this node will be also included in parent's array.
232
- * @param {Boolean} [options.parentFirst=false] When set to `true`, array will be sorted from node's parent to root element,
233
- * otherwise root element will be the first item in the array.
234
- * @returns {Array} Array with ancestors.
235
- */
236
- getAncestors( options = { includeSelf: false, parentFirst: false } ) {
237
- const ancestors = [];
238
- let parent = options.includeSelf ? this : this.parent;
239
-
240
- while ( parent ) {
241
- ancestors[ options.parentFirst ? 'push' : 'unshift' ]( parent );
242
- parent = parent.parent;
243
- }
244
-
245
- return ancestors;
246
- }
247
-
248
- /**
249
- * Returns a {@link module:engine/model/element~Element} or {@link module:engine/model/documentfragment~DocumentFragment}
250
- * which is a common ancestor of both nodes.
251
- *
252
- * @param {module:engine/model/node~Node} node The second node.
253
- * @param {Object} options Options object.
254
- * @param {Boolean} [options.includeSelf=false] When set to `true` both nodes will be considered "ancestors" too.
255
- * Which means that if e.g. node A is inside B, then their common ancestor will be B.
256
- * @returns {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment|null}
257
- */
258
- getCommonAncestor( node, options = {} ) {
259
- const ancestorsA = this.getAncestors( options );
260
- const ancestorsB = node.getAncestors( options );
261
-
262
- let i = 0;
263
-
264
- while ( ancestorsA[ i ] == ancestorsB[ i ] && ancestorsA[ i ] ) {
265
- i++;
266
- }
267
-
268
- return i === 0 ? null : ancestorsA[ i - 1 ];
269
- }
270
-
271
- /**
272
- * Returns whether this node is before given node. `false` is returned if nodes are in different trees (for example,
273
- * in different {@link module:engine/model/documentfragment~DocumentFragment}s).
274
- *
275
- * @param {module:engine/model/node~Node} node Node to compare with.
276
- * @returns {Boolean}
277
- */
278
- isBefore( node ) {
279
- // Given node is not before this node if they are same.
280
- if ( this == node ) {
281
- return false;
282
- }
283
-
284
- // Return `false` if it is impossible to compare nodes.
285
- if ( this.root !== node.root ) {
286
- return false;
287
- }
288
-
289
- const thisPath = this.getPath();
290
- const nodePath = node.getPath();
291
-
292
- const result = compareArrays( thisPath, nodePath );
293
-
294
- switch ( result ) {
295
- case 'prefix':
296
- return true;
297
-
298
- case 'extension':
299
- return false;
300
-
301
- default:
302
- return thisPath[ result ] < nodePath[ result ];
303
- }
304
- }
305
-
306
- /**
307
- * Returns whether this node is after given node. `false` is returned if nodes are in different trees (for example,
308
- * in different {@link module:engine/model/documentfragment~DocumentFragment}s).
309
- *
310
- * @param {module:engine/model/node~Node} node Node to compare with.
311
- * @returns {Boolean}
312
- */
313
- isAfter( node ) {
314
- // Given node is not before this node if they are same.
315
- if ( this == node ) {
316
- return false;
317
- }
318
-
319
- // Return `false` if it is impossible to compare nodes.
320
- if ( this.root !== node.root ) {
321
- return false;
322
- }
323
-
324
- // In other cases, just check if the `node` is before, and return the opposite.
325
- return !this.isBefore( node );
326
- }
327
-
328
- /**
329
- * Checks if the node has an attribute with given key.
330
- *
331
- * @param {String} key Key of attribute to check.
332
- * @returns {Boolean} `true` if attribute with given key is set on node, `false` otherwise.
333
- */
334
- hasAttribute( key ) {
335
- return this._attrs.has( key );
336
- }
337
-
338
- /**
339
- * Gets an attribute value for given key or `undefined` if that attribute is not set on node.
340
- *
341
- * @param {String} key Key of attribute to look for.
342
- * @returns {*} Attribute value or `undefined`.
343
- */
344
- getAttribute( key ) {
345
- return this._attrs.get( key );
346
- }
347
-
348
- /**
349
- * Returns iterator that iterates over this node's attributes.
350
- *
351
- * Attributes are returned as arrays containing two items. First one is attribute key and second is attribute value.
352
- * This format is accepted by native `Map` object and also can be passed in `Node` constructor.
353
- *
354
- * @returns {Iterable.<*>}
355
- */
356
- getAttributes() {
357
- return this._attrs.entries();
358
- }
359
-
360
- /**
361
- * Returns iterator that iterates over this node's attribute keys.
362
- *
363
- * @returns {Iterable.<String>}
364
- */
365
- getAttributeKeys() {
366
- return this._attrs.keys();
367
- }
368
-
369
- /**
370
- * Converts `Node` to plain object and returns it.
371
- *
372
- * @returns {Object} `Node` converted to plain object.
373
- */
374
- toJSON() {
375
- const json = {};
376
-
377
- // Serializes attributes to the object.
378
- // attributes = { a: 'foo', b: 1, c: true }.
379
- if ( this._attrs.size ) {
380
- json.attributes = Array.from( this._attrs ).reduce( ( result, attr ) => {
381
- result[ attr[ 0 ] ] = attr[ 1 ];
382
-
383
- return result;
384
- }, {} );
385
- }
386
-
387
- return json;
388
- }
389
-
390
- /**
391
- * Checks whether this object is of the given type.
392
- *
393
- * This method is useful when processing model objects that are of unknown type. For example, a function
394
- * may return a {@link module:engine/model/documentfragment~DocumentFragment} or a {@link module:engine/model/node~Node}
395
- * that can be either a text node or an element. This method can be used to check what kind of object is returned.
396
- *
397
- * someObject.is( 'element' ); // -> true if this is an element
398
- * someObject.is( 'node' ); // -> true if this is a node (a text node or an element)
399
- * someObject.is( 'documentFragment' ); // -> true if this is a document fragment
400
- *
401
- * Since this method is also available on a range of view objects, you can prefix the type of the object with
402
- * `model:` or `view:` to check, for example, if this is the model's or view's element:
403
- *
404
- * modelElement.is( 'model:element' ); // -> true
405
- * modelElement.is( 'view:element' ); // -> false
406
- *
407
- * By using this method it is also possible to check a name of an element:
408
- *
409
- * imageElement.is( 'element', 'imageBlock' ); // -> true
410
- * imageElement.is( 'element', 'imageBlock' ); // -> same as above
411
- * imageElement.is( 'model:element', 'imageBlock' ); // -> same as above, but more precise
412
- *
413
- * The list of model objects which implement the `is()` method:
414
- *
415
- * * {@link module:engine/model/node~Node#is `Node#is()`}
416
- * * {@link module:engine/model/text~Text#is `Text#is()`}
417
- * * {@link module:engine/model/element~Element#is `Element#is()`}
418
- * * {@link module:engine/model/rootelement~RootElement#is `RootElement#is()`}
419
- * * {@link module:engine/model/position~Position#is `Position#is()`}
420
- * * {@link module:engine/model/liveposition~LivePosition#is `LivePosition#is()`}
421
- * * {@link module:engine/model/range~Range#is `Range#is()`}
422
- * * {@link module:engine/model/liverange~LiveRange#is `LiveRange#is()`}
423
- * * {@link module:engine/model/documentfragment~DocumentFragment#is `DocumentFragment#is()`}
424
- * * {@link module:engine/model/selection~Selection#is `Selection#is()`}
425
- * * {@link module:engine/model/documentselection~DocumentSelection#is `DocumentSelection#is()`}
426
- * * {@link module:engine/model/markercollection~Marker#is `Marker#is()`}
427
- * * {@link module:engine/model/textproxy~TextProxy#is `TextProxy#is()`}
428
- *
429
- * @method #is
430
- * @param {String} type Type to check.
431
- * @returns {Boolean}
432
- */
433
- is( type ) {
434
- return type === 'node' || type === 'model:node';
435
- }
436
-
437
- /**
438
- * Creates a copy of this node, that is a node with exactly same attributes, and returns it.
439
- *
440
- * @protected
441
- * @returns {module:engine/model/node~Node} Node with same attributes as this node.
442
- */
443
- _clone() {
444
- return new Node( this._attrs );
445
- }
446
-
447
- /**
448
- * Removes this node from it's parent.
449
- *
450
- * @see module:engine/model/writer~Writer#remove
451
- * @protected
452
- */
453
- _remove() {
454
- this.parent._removeChildren( this.index );
455
- }
456
-
457
- /**
458
- * Sets attribute on the node. If attribute with the same key already is set, it's value is overwritten.
459
- *
460
- * @see module:engine/model/writer~Writer#setAttribute
461
- * @protected
462
- * @param {String} key Key of attribute to set.
463
- * @param {*} value Attribute value.
464
- */
465
- _setAttribute( key, value ) {
466
- this._attrs.set( key, value );
467
- }
468
-
469
- /**
470
- * Removes all attributes from the node and sets given attributes.
471
- *
472
- * @see module:engine/model/writer~Writer#setAttributes
473
- * @protected
474
- * @param {Object} [attrs] Attributes to set. See {@link module:utils/tomap~toMap} for a list of accepted values.
475
- */
476
- _setAttributesTo( attrs ) {
477
- this._attrs = toMap( attrs );
478
- }
479
-
480
- /**
481
- * Removes an attribute with given key from the node.
482
- *
483
- * @see module:engine/model/writer~Writer#removeAttribute
484
- * @protected
485
- * @param {String} key Key of attribute to remove.
486
- * @returns {Boolean} `true` if the attribute was set on the element, `false` otherwise.
487
- */
488
- _removeAttribute( key ) {
489
- return this._attrs.delete( key );
490
- }
491
-
492
- /**
493
- * Removes all attributes from the node.
494
- *
495
- * @see module:engine/model/writer~Writer#clearAttributes
496
- * @protected
497
- */
498
- _clearAttributes() {
499
- this._attrs.clear();
500
- }
43
+ export default class Node extends TypeCheckable {
44
+ /**
45
+ * Creates a model node.
46
+ *
47
+ * This is an abstract class, so this constructor should not be used directly.
48
+ *
49
+ * @abstract
50
+ * @param {Object} [attrs] Node's attributes. See {@link module:utils/tomap~toMap} for a list of accepted values.
51
+ */
52
+ constructor(attrs) {
53
+ super();
54
+ /**
55
+ * Parent of this node. It could be {@link module:engine/model/element~Element}
56
+ * or {@link module:engine/model/documentfragment~DocumentFragment}.
57
+ * Equals to `null` if the node has no parent.
58
+ *
59
+ * @readonly
60
+ * @member {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment|null}
61
+ */
62
+ this.parent = null;
63
+ /**
64
+ * Attributes set on this node.
65
+ *
66
+ * @private
67
+ * @member {Map} module:engine/model/node~Node#_attrs
68
+ */
69
+ this._attrs = toMap(attrs);
70
+ }
71
+ /**
72
+ * {@link module:engine/model/document~Document Document} that owns this root element.
73
+ *
74
+ * @readonly
75
+ * @type {module:engine/model/document~Document|null}
76
+ */
77
+ get document() {
78
+ return null;
79
+ }
80
+ /**
81
+ * Index of this node in it's parent or `null` if the node has no parent.
82
+ *
83
+ * Accessing this property throws an error if this node's parent element does not contain it.
84
+ * This means that model tree got broken.
85
+ *
86
+ * @readonly
87
+ * @type {Number|null}
88
+ */
89
+ get index() {
90
+ let pos;
91
+ if (!this.parent) {
92
+ return null;
93
+ }
94
+ if ((pos = this.parent.getChildIndex(this)) === null) {
95
+ throw new CKEditorError('model-node-not-found-in-parent', this);
96
+ }
97
+ return pos;
98
+ }
99
+ /**
100
+ * Offset at which this node starts in it's parent. It is equal to the sum of {@link #offsetSize offsetSize}
101
+ * of all it's previous siblings. Equals to `null` if node has no parent.
102
+ *
103
+ * Accessing this property throws an error if this node's parent element does not contain it.
104
+ * This means that model tree got broken.
105
+ *
106
+ * @readonly
107
+ * @type {Number|null}
108
+ */
109
+ get startOffset() {
110
+ let pos;
111
+ if (!this.parent) {
112
+ return null;
113
+ }
114
+ if ((pos = this.parent.getChildStartOffset(this)) === null) {
115
+ throw new CKEditorError('model-node-not-found-in-parent', this);
116
+ }
117
+ return pos;
118
+ }
119
+ /**
120
+ * Offset size of this node. Represents how much "offset space" is occupied by the node in it's parent.
121
+ * It is important for {@link module:engine/model/position~Position position}. When node has `offsetSize` greater than `1`, position
122
+ * can be placed between that node start and end. `offsetSize` greater than `1` is for nodes that represents more
123
+ * than one entity, i.e. {@link module:engine/model/text~Text text node}.
124
+ *
125
+ * @readonly
126
+ * @type {Number}
127
+ */
128
+ get offsetSize() {
129
+ return 1;
130
+ }
131
+ /**
132
+ * Offset at which this node ends in it's parent. It is equal to the sum of this node's
133
+ * {@link module:engine/model/node~Node#startOffset start offset} and {@link #offsetSize offset size}.
134
+ * Equals to `null` if the node has no parent.
135
+ *
136
+ * @readonly
137
+ * @type {Number|null}
138
+ */
139
+ get endOffset() {
140
+ if (!this.parent) {
141
+ return null;
142
+ }
143
+ return this.startOffset + this.offsetSize;
144
+ }
145
+ /**
146
+ * Node's next sibling or `null` if the node is a last child of it's parent or if the node has no parent.
147
+ *
148
+ * @readonly
149
+ * @type {module:engine/model/node~Node|null}
150
+ */
151
+ get nextSibling() {
152
+ const index = this.index;
153
+ return (index !== null && this.parent.getChild(index + 1)) || null;
154
+ }
155
+ /**
156
+ * Node's previous sibling or `null` if the node is a first child of it's parent or if the node has no parent.
157
+ *
158
+ * @readonly
159
+ * @type {module:engine/model/node~Node|null}
160
+ */
161
+ get previousSibling() {
162
+ const index = this.index;
163
+ return (index !== null && this.parent.getChild(index - 1)) || null;
164
+ }
165
+ /**
166
+ * The top-most ancestor of the node. If node has no parent it is the root itself. If the node is a part
167
+ * of {@link module:engine/model/documentfragment~DocumentFragment}, it's `root` is equal to that `DocumentFragment`.
168
+ *
169
+ * @readonly
170
+ * @type {module:engine/model/node~Node|module:engine/model/documentfragment~DocumentFragment}
171
+ */
172
+ get root() {
173
+ // eslint-disable-next-line @typescript-eslint/no-this-alias, consistent-this
174
+ let root = this;
175
+ while (root.parent) {
176
+ root = root.parent;
177
+ }
178
+ return root;
179
+ }
180
+ /**
181
+ * Returns true if the node is in a tree rooted in the document (is a descendant of one of its roots).
182
+ *
183
+ * @returns {Boolean}
184
+ */
185
+ isAttached() {
186
+ return this.root.is('rootElement');
187
+ }
188
+ /**
189
+ * Gets path to the node. The path is an array containing starting offsets of consecutive ancestors of this node,
190
+ * beginning from {@link module:engine/model/node~Node#root root}, down to this node's starting offset. The path can be used to
191
+ * create {@link module:engine/model/position~Position Position} instance.
192
+ *
193
+ * const abc = new Text( 'abc' );
194
+ * const foo = new Text( 'foo' );
195
+ * const h1 = new Element( 'h1', null, new Text( 'header' ) );
196
+ * const p = new Element( 'p', null, [ abc, foo ] );
197
+ * const div = new Element( 'div', null, [ h1, p ] );
198
+ * foo.getPath(); // Returns [ 1, 3 ]. `foo` is in `p` which is in `div`. `p` starts at offset 1, while `foo` at 3.
199
+ * h1.getPath(); // Returns [ 0 ].
200
+ * div.getPath(); // Returns [].
201
+ *
202
+ * @returns {Array.<Number>} The path.
203
+ */
204
+ getPath() {
205
+ const path = [];
206
+ // eslint-disable-next-line @typescript-eslint/no-this-alias, consistent-this
207
+ let node = this;
208
+ while (node.parent) {
209
+ path.unshift(node.startOffset);
210
+ node = node.parent;
211
+ }
212
+ return path;
213
+ }
214
+ /**
215
+ * Returns ancestors array of this node.
216
+ *
217
+ * @param {Object} options Options object.
218
+ * @param {Boolean} [options.includeSelf=false] When set to `true` this node will be also included in parent's array.
219
+ * @param {Boolean} [options.parentFirst=false] When set to `true`, array will be sorted from node's parent to root element,
220
+ * otherwise root element will be the first item in the array.
221
+ * @returns {Array} Array with ancestors.
222
+ */
223
+ getAncestors(options = {}) {
224
+ const ancestors = [];
225
+ let parent = options.includeSelf ? this : this.parent;
226
+ while (parent) {
227
+ ancestors[options.parentFirst ? 'push' : 'unshift'](parent);
228
+ parent = parent.parent;
229
+ }
230
+ return ancestors;
231
+ }
232
+ /**
233
+ * Returns a {@link module:engine/model/element~Element} or {@link module:engine/model/documentfragment~DocumentFragment}
234
+ * which is a common ancestor of both nodes.
235
+ *
236
+ * @param {module:engine/model/node~Node} node The second node.
237
+ * @param {Object} options Options object.
238
+ * @param {Boolean} [options.includeSelf=false] When set to `true` both nodes will be considered "ancestors" too.
239
+ * Which means that if e.g. node A is inside B, then their common ancestor will be B.
240
+ * @returns {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment|null}
241
+ */
242
+ getCommonAncestor(node, options = {}) {
243
+ const ancestorsA = this.getAncestors(options);
244
+ const ancestorsB = node.getAncestors(options);
245
+ let i = 0;
246
+ while (ancestorsA[i] == ancestorsB[i] && ancestorsA[i]) {
247
+ i++;
248
+ }
249
+ return i === 0 ? null : ancestorsA[i - 1];
250
+ }
251
+ /**
252
+ * Returns whether this node is before given node. `false` is returned if nodes are in different trees (for example,
253
+ * in different {@link module:engine/model/documentfragment~DocumentFragment}s).
254
+ *
255
+ * @param {module:engine/model/node~Node} node Node to compare with.
256
+ * @returns {Boolean}
257
+ */
258
+ isBefore(node) {
259
+ // Given node is not before this node if they are same.
260
+ if (this == node) {
261
+ return false;
262
+ }
263
+ // Return `false` if it is impossible to compare nodes.
264
+ if (this.root !== node.root) {
265
+ return false;
266
+ }
267
+ const thisPath = this.getPath();
268
+ const nodePath = node.getPath();
269
+ const result = compareArrays(thisPath, nodePath);
270
+ switch (result) {
271
+ case 'prefix':
272
+ return true;
273
+ case 'extension':
274
+ return false;
275
+ default:
276
+ return thisPath[result] < nodePath[result];
277
+ }
278
+ }
279
+ /**
280
+ * Returns whether this node is after given node. `false` is returned if nodes are in different trees (for example,
281
+ * in different {@link module:engine/model/documentfragment~DocumentFragment}s).
282
+ *
283
+ * @param {module:engine/model/node~Node} node Node to compare with.
284
+ * @returns {Boolean}
285
+ */
286
+ isAfter(node) {
287
+ // Given node is not before this node if they are same.
288
+ if (this == node) {
289
+ return false;
290
+ }
291
+ // Return `false` if it is impossible to compare nodes.
292
+ if (this.root !== node.root) {
293
+ return false;
294
+ }
295
+ // In other cases, just check if the `node` is before, and return the opposite.
296
+ return !this.isBefore(node);
297
+ }
298
+ /**
299
+ * Checks if the node has an attribute with given key.
300
+ *
301
+ * @param {String} key Key of attribute to check.
302
+ * @returns {Boolean} `true` if attribute with given key is set on node, `false` otherwise.
303
+ */
304
+ hasAttribute(key) {
305
+ return this._attrs.has(key);
306
+ }
307
+ /**
308
+ * Gets an attribute value for given key or `undefined` if that attribute is not set on node.
309
+ *
310
+ * @param {String} key Key of attribute to look for.
311
+ * @returns {*} Attribute value or `undefined`.
312
+ */
313
+ getAttribute(key) {
314
+ return this._attrs.get(key);
315
+ }
316
+ /**
317
+ * Returns iterator that iterates over this node's attributes.
318
+ *
319
+ * Attributes are returned as arrays containing two items. First one is attribute key and second is attribute value.
320
+ * This format is accepted by native `Map` object and also can be passed in `Node` constructor.
321
+ *
322
+ * @returns {Iterable.<*>}
323
+ */
324
+ getAttributes() {
325
+ return this._attrs.entries();
326
+ }
327
+ /**
328
+ * Returns iterator that iterates over this node's attribute keys.
329
+ *
330
+ * @returns {Iterable.<String>}
331
+ */
332
+ getAttributeKeys() {
333
+ return this._attrs.keys();
334
+ }
335
+ /**
336
+ * Converts `Node` to plain object and returns it.
337
+ *
338
+ * @returns {Object} `Node` converted to plain object.
339
+ */
340
+ toJSON() {
341
+ const json = {};
342
+ // Serializes attributes to the object.
343
+ // attributes = { a: 'foo', b: 1, c: true }.
344
+ if (this._attrs.size) {
345
+ json.attributes = Array.from(this._attrs).reduce((result, attr) => {
346
+ result[attr[0]] = attr[1];
347
+ return result;
348
+ }, {});
349
+ }
350
+ return json;
351
+ }
352
+ /**
353
+ * Creates a copy of this node, that is a node with exactly same attributes, and returns it.
354
+ *
355
+ * @internal
356
+ * @protected
357
+ * @returns {module:engine/model/node~Node} Node with same attributes as this node.
358
+ */
359
+ _clone(_deep) {
360
+ return new Node(this._attrs);
361
+ }
362
+ /**
363
+ * Removes this node from it's parent.
364
+ *
365
+ * @internal
366
+ * @see module:engine/model/writer~Writer#remove
367
+ * @protected
368
+ */
369
+ _remove() {
370
+ this.parent._removeChildren(this.index);
371
+ }
372
+ /**
373
+ * Sets attribute on the node. If attribute with the same key already is set, it's value is overwritten.
374
+ *
375
+ * @see module:engine/model/writer~Writer#setAttribute
376
+ * @internal
377
+ * @protected
378
+ * @param {String} key Key of attribute to set.
379
+ * @param {*} value Attribute value.
380
+ */
381
+ _setAttribute(key, value) {
382
+ this._attrs.set(key, value);
383
+ }
384
+ /**
385
+ * Removes all attributes from the node and sets given attributes.
386
+ *
387
+ * @see module:engine/model/writer~Writer#setAttributes
388
+ * @internal
389
+ * @protected
390
+ * @param {Object} [attrs] Attributes to set. See {@link module:utils/tomap~toMap} for a list of accepted values.
391
+ */
392
+ _setAttributesTo(attrs) {
393
+ this._attrs = toMap(attrs);
394
+ }
395
+ /**
396
+ * Removes an attribute with given key from the node.
397
+ *
398
+ * @see module:engine/model/writer~Writer#removeAttribute
399
+ * @internal
400
+ * @protected
401
+ * @param {String} key Key of attribute to remove.
402
+ * @returns {Boolean} `true` if the attribute was set on the element, `false` otherwise.
403
+ */
404
+ _removeAttribute(key) {
405
+ return this._attrs.delete(key);
406
+ }
407
+ /**
408
+ * Removes all attributes from the node.
409
+ *
410
+ * @see module:engine/model/writer~Writer#clearAttributes
411
+ * @internal
412
+ * @protected
413
+ */
414
+ _clearAttributes() {
415
+ this._attrs.clear();
416
+ }
501
417
  }
502
-
503
418
  /**
504
- * The node's parent does not contain this node.
419
+ * Checks whether this object is of the given type.
505
420
  *
506
- * @error model-node-not-found-in-parent
421
+ * This method is useful when processing model objects that are of unknown type. For example, a function
422
+ * may return a {@link module:engine/model/documentfragment~DocumentFragment} or a {@link module:engine/model/node~Node}
423
+ * that can be either a text node or an element. This method can be used to check what kind of object is returned.
424
+ *
425
+ * someObject.is( 'element' ); // -> true if this is an element
426
+ * someObject.is( 'node' ); // -> true if this is a node (a text node or an element)
427
+ * someObject.is( 'documentFragment' ); // -> true if this is a document fragment
428
+ *
429
+ * Since this method is also available on a range of view objects, you can prefix the type of the object with
430
+ * `model:` or `view:` to check, for example, if this is the model's or view's element:
431
+ *
432
+ * modelElement.is( 'model:element' ); // -> true
433
+ * modelElement.is( 'view:element' ); // -> false
434
+ *
435
+ * By using this method it is also possible to check a name of an element:
436
+ *
437
+ * imageElement.is( 'element', 'imageBlock' ); // -> true
438
+ * imageElement.is( 'element', 'imageBlock' ); // -> same as above
439
+ * imageElement.is( 'model:element', 'imageBlock' ); // -> same as above, but more precise
440
+ *
441
+ * The list of model objects which implement the `is()` method:
442
+ *
443
+ * * {@link module:engine/model/node~Node#is `Node#is()`}
444
+ * * {@link module:engine/model/text~Text#is `Text#is()`}
445
+ * * {@link module:engine/model/element~Element#is `Element#is()`}
446
+ * * {@link module:engine/model/rootelement~RootElement#is `RootElement#is()`}
447
+ * * {@link module:engine/model/position~Position#is `Position#is()`}
448
+ * * {@link module:engine/model/liveposition~LivePosition#is `LivePosition#is()`}
449
+ * * {@link module:engine/model/range~Range#is `Range#is()`}
450
+ * * {@link module:engine/model/liverange~LiveRange#is `LiveRange#is()`}
451
+ * * {@link module:engine/model/documentfragment~DocumentFragment#is `DocumentFragment#is()`}
452
+ * * {@link module:engine/model/selection~Selection#is `Selection#is()`}
453
+ * * {@link module:engine/model/documentselection~DocumentSelection#is `DocumentSelection#is()`}
454
+ * * {@link module:engine/model/markercollection~Marker#is `Marker#is()`}
455
+ * * {@link module:engine/model/textproxy~TextProxy#is `TextProxy#is()`}
456
+ *
457
+ * @method #is
458
+ * @param {String} type Type to check.
459
+ * @returns {Boolean}
507
460
  */
461
+ Node.prototype.is = function (type) {
462
+ return type === 'node' || type === 'model:node';
463
+ };