@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
@@ -2,19 +2,15 @@
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/model/element
8
7
  */
9
-
10
8
  import Node from './node';
11
9
  import NodeList from './nodelist';
12
10
  import Text from './text';
13
11
  import TextProxy from './textproxy';
14
12
  import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
15
-
16
13
  // @if CK_DEBUG_ENGINE // const { stringifyMap, convertMapToStringifiedObject, convertMapToTags } = require( '../dev-utils/utils' );
17
-
18
14
  /**
19
15
  * Model element. Type of {@link module:engine/model/node~Node node} that has a {@link module:engine/model/element~Element#name name} and
20
16
  * {@link module:engine/model/element~Element#getChildren child nodes}.
@@ -24,415 +20,315 @@ import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
24
20
  * @extends module:engine/model/node~Node
25
21
  */
26
22
  export default class Element extends Node {
27
- /**
28
- * Creates a model element.
29
- *
30
- * **Note:** Constructor of this class shouldn't be used directly in the code.
31
- * Use the {@link module:engine/model/writer~Writer#createElement} method instead.
32
- *
33
- * @protected
34
- * @param {String} name Element's name.
35
- * @param {Object} [attrs] Element's attributes. See {@link module:utils/tomap~toMap} for a list of accepted values.
36
- * @param {module:engine/model/node~Node|Iterable.<module:engine/model/node~Node>} [children]
37
- * One or more nodes to be inserted as children of created element.
38
- */
39
- constructor( name, attrs, children ) {
40
- super( attrs );
41
-
42
- /**
43
- * Element name.
44
- *
45
- * @readonly
46
- * @member {String} module:engine/model/element~Element#name
47
- */
48
- this.name = name;
49
-
50
- /**
51
- * List of children nodes.
52
- *
53
- * @private
54
- * @member {module:engine/model/nodelist~NodeList} module:engine/model/element~Element#_children
55
- */
56
- this._children = new NodeList();
57
-
58
- if ( children ) {
59
- this._insertChild( 0, children );
60
- }
61
- }
62
-
63
- /**
64
- * Number of this element's children.
65
- *
66
- * @readonly
67
- * @type {Number}
68
- */
69
- get childCount() {
70
- return this._children.length;
71
- }
72
-
73
- /**
74
- * Sum of {@link module:engine/model/node~Node#offsetSize offset sizes} of all of this element's children.
75
- *
76
- * @readonly
77
- * @type {Number}
78
- */
79
- get maxOffset() {
80
- return this._children.maxOffset;
81
- }
82
-
83
- /**
84
- * Is `true` if there are no nodes inside this element, `false` otherwise.
85
- *
86
- * @readonly
87
- * @type {Boolean}
88
- */
89
- get isEmpty() {
90
- return this.childCount === 0;
91
- }
92
-
93
- /**
94
- * Checks whether this object is of the given.
95
- *
96
- * element.is( 'element' ); // -> true
97
- * element.is( 'node' ); // -> true
98
- * element.is( 'model:element' ); // -> true
99
- * element.is( 'model:node' ); // -> true
100
- *
101
- * element.is( 'view:element' ); // -> false
102
- * element.is( 'documentSelection' ); // -> false
103
- *
104
- * Assuming that the object being checked is an element, you can also check its
105
- * {@link module:engine/model/element~Element#name name}:
106
- *
107
- * element.is( 'element', 'imageBlock' ); // -> true if this is an <imageBlock> element
108
- * element.is( 'element', 'imageBlock' ); // -> same as above
109
- * text.is( 'element', 'imageBlock' ); -> false
110
- *
111
- * {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
112
- *
113
- * @param {String} type Type to check.
114
- * @param {String} [name] Element name.
115
- * @returns {Boolean}
116
- */
117
- is( type, name = null ) {
118
- if ( !name ) {
119
- return type === 'element' || type === 'model:element' ||
120
- // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
121
- type === 'node' || type === 'model:node';
122
- }
123
-
124
- return name === this.name && ( type === 'element' || type === 'model:element' );
125
- }
126
-
127
- /**
128
- * Gets the child at the given index.
129
- *
130
- * @param {Number} index Index of child.
131
- * @returns {module:engine/model/node~Node} Child node.
132
- */
133
- getChild( index ) {
134
- return this._children.getNode( index );
135
- }
136
-
137
- /**
138
- * Returns an iterator that iterates over all of this element's children.
139
- *
140
- * @returns {Iterable.<module:engine/model/node~Node>}
141
- */
142
- getChildren() {
143
- return this._children[ Symbol.iterator ]();
144
- }
145
-
146
- /**
147
- * Returns an index of the given child node. Returns `null` if given node is not a child of this element.
148
- *
149
- * @param {module:engine/model/node~Node} node Child node to look for.
150
- * @returns {Number} Child node's index in this element.
151
- */
152
- getChildIndex( node ) {
153
- return this._children.getNodeIndex( node );
154
- }
155
-
156
- /**
157
- * Returns the starting offset of given child. Starting offset is equal to the sum of
158
- * {@link module:engine/model/node~Node#offsetSize offset sizes} of all node's siblings that are before it. Returns `null` if
159
- * given node is not a child of this element.
160
- *
161
- * @param {module:engine/model/node~Node} node Child node to look for.
162
- * @returns {Number} Child node's starting offset.
163
- */
164
- getChildStartOffset( node ) {
165
- return this._children.getNodeStartOffset( node );
166
- }
167
-
168
- /**
169
- * Returns index of a node that occupies given offset. If given offset is too low, returns `0`. If given offset is
170
- * too high, returns {@link module:engine/model/element~Element#getChildIndex index after last child}.
171
- *
172
- * const textNode = new Text( 'foo' );
173
- * const pElement = new Element( 'p' );
174
- * const divElement = new Element( [ textNode, pElement ] );
175
- * divElement.offsetToIndex( -1 ); // Returns 0, because offset is too low.
176
- * divElement.offsetToIndex( 0 ); // Returns 0, because offset 0 is taken by `textNode` which is at index 0.
177
- * divElement.offsetToIndex( 1 ); // Returns 0, because `textNode` has `offsetSize` equal to 3, so it occupies offset 1 too.
178
- * divElement.offsetToIndex( 2 ); // Returns 0.
179
- * divElement.offsetToIndex( 3 ); // Returns 1.
180
- * divElement.offsetToIndex( 4 ); // Returns 2. There are no nodes at offset 4, so last available index is returned.
181
- *
182
- * @param {Number} offset Offset to look for.
183
- * @returns {Number}
184
- */
185
- offsetToIndex( offset ) {
186
- return this._children.offsetToIndex( offset );
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}
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
- * Returns the parent element of the given name. Returns null if the element is not inside the desired parent.
212
- *
213
- * @param {String} parentName The name of the parent element to find.
214
- * @param {Object} [options] Options object.
215
- * @param {Boolean} [options.includeSelf=false] When set to `true` this node will be also included while searching.
216
- * @returns {module:engine/model/element~Element|null}
217
- */
218
- findAncestor( parentName, options = { includeSelf: false } ) {
219
- let parent = options.includeSelf ? this : this.parent;
220
-
221
- while ( parent ) {
222
- if ( parent.name === parentName ) {
223
- return parent;
224
- }
225
-
226
- parent = parent.parent;
227
- }
228
-
229
- return null;
230
- }
231
-
232
- /**
233
- * Converts `Element` instance to plain object and returns it. Takes care of converting all of this element's children.
234
- *
235
- * @returns {Object} `Element` instance converted to plain object.
236
- */
237
- toJSON() {
238
- const json = super.toJSON();
239
-
240
- json.name = this.name;
241
-
242
- if ( this._children.length > 0 ) {
243
- json.children = [];
244
-
245
- for ( const node of this._children ) {
246
- json.children.push( node.toJSON() );
247
- }
248
- }
249
-
250
- return json;
251
- }
252
-
253
- /**
254
- * Creates a copy of this element and returns it. Created element has the same name and attributes as the original element.
255
- * If clone is deep, the original element's children are also cloned. If not, then empty element is returned.
256
- *
257
- * @protected
258
- * @param {Boolean} [deep=false] If set to `true` clones element and all its children recursively. When set to `false`,
259
- * element will be cloned without any child.
260
- */
261
- _clone( deep = false ) {
262
- const children = deep ? Array.from( this._children ).map( node => node._clone( true ) ) : null;
263
-
264
- return new Element( this.name, this.getAttributes(), children );
265
- }
266
-
267
- /**
268
- * {@link module:engine/model/element~Element#_insertChild Inserts} one or more nodes at the end of this element.
269
- *
270
- * @see module:engine/model/writer~Writer#append
271
- * @protected
272
- * @param {module:engine/model/item~Item|Iterable.<module:engine/model/item~Item>} nodes Nodes to be inserted.
273
- */
274
- _appendChild( nodes ) {
275
- this._insertChild( this.childCount, nodes );
276
- }
277
-
278
- /**
279
- * Inserts one or more nodes at the given index and sets {@link module:engine/model/node~Node#parent parent} of these nodes
280
- * to this element.
281
- *
282
- * @see module:engine/model/writer~Writer#insert
283
- * @protected
284
- * @param {Number} index Index at which nodes should be inserted.
285
- * @param {module:engine/model/item~Item|Iterable.<module:engine/model/item~Item>} items Items to be inserted.
286
- */
287
- _insertChild( index, items ) {
288
- const nodes = normalize( items );
289
-
290
- for ( const node of nodes ) {
291
- // If node that is being added to this element is already inside another element, first remove it from the old parent.
292
- if ( node.parent !== null ) {
293
- node._remove();
294
- }
295
-
296
- node.parent = this;
297
- }
298
-
299
- this._children._insertNodes( index, nodes );
300
- }
301
-
302
- /**
303
- * Removes one or more nodes starting at the given index and sets
304
- * {@link module:engine/model/node~Node#parent parent} of these nodes to `null`.
305
- *
306
- * @see module:engine/model/writer~Writer#remove
307
- * @protected
308
- * @param {Number} index Index of the first node to remove.
309
- * @param {Number} [howMany=1] Number of nodes to remove.
310
- * @returns {Array.<module:engine/model/node~Node>} Array containing removed nodes.
311
- */
312
- _removeChildren( index, howMany = 1 ) {
313
- const nodes = this._children._removeNodes( index, howMany );
314
-
315
- for ( const node of nodes ) {
316
- node.parent = null;
317
- }
318
-
319
- return nodes;
320
- }
321
-
322
- /**
323
- * Creates an `Element` instance from given plain object (i.e. parsed JSON string).
324
- * Converts `Element` children to proper nodes.
325
- *
326
- * @param {Object} json Plain object to be converted to `Element`.
327
- * @returns {module:engine/model/element~Element} `Element` instance created using given plain object.
328
- */
329
- static fromJSON( json ) {
330
- let children = null;
331
-
332
- if ( json.children ) {
333
- children = [];
334
-
335
- for ( const child of json.children ) {
336
- if ( child.name ) {
337
- // If child has name property, it is an Element.
338
- children.push( Element.fromJSON( child ) );
339
- } else {
340
- // Otherwise, it is a Text node.
341
- children.push( Text.fromJSON( child ) );
342
- }
343
- }
344
- }
345
-
346
- return new Element( json.name, json.attributes, children );
347
- }
348
-
349
- // @if CK_DEBUG_ENGINE // toString() {
350
- // @if CK_DEBUG_ENGINE // return `<${ this.rootName || this.name }>`;
351
- // @if CK_DEBUG_ENGINE // }
352
-
353
- // @if CK_DEBUG_ENGINE // log() {
354
- // @if CK_DEBUG_ENGINE // console.log( 'ModelElement: ' + this );
355
- // @if CK_DEBUG_ENGINE // }
356
-
357
- // @if CK_DEBUG_ENGINE // logExtended() {
358
- // @if CK_DEBUG_ENGINE // console.log( `ModelElement: ${ this }, ${ this.childCount } children,
359
- // @if CK_DEBUG_ENGINE // attrs: ${ convertMapToStringifiedObject( this.getAttributes() ) }` );
360
- // @if CK_DEBUG_ENGINE // }
361
-
362
- // @if CK_DEBUG_ENGINE // logAll() {
363
- // @if CK_DEBUG_ENGINE // console.log( '--------------------' );
364
- // @if CK_DEBUG_ENGINE //
365
- // @if CK_DEBUG_ENGINE // this.logExtended();
366
- // @if CK_DEBUG_ENGINE // console.log( 'List of children:' );
367
- // @if CK_DEBUG_ENGINE //
368
- // @if CK_DEBUG_ENGINE // for ( const child of this.getChildren() ) {
369
- // @if CK_DEBUG_ENGINE // child.log();
370
- // @if CK_DEBUG_ENGINE // }
371
- // @if CK_DEBUG_ENGINE // }
372
-
373
- // @if CK_DEBUG_ENGINE // printTree( level = 0) {
374
- // @if CK_DEBUG_ENGINE // let string = '';
375
-
376
- // @if CK_DEBUG_ENGINE // string += '\t'.repeat( level );
377
- // @if CK_DEBUG_ENGINE // string += `<${ this.rootName || this.name }${ convertMapToTags( this.getAttributes() ) }>`;
378
-
379
- // @if CK_DEBUG_ENGINE // for ( const child of this.getChildren() ) {
380
- // @if CK_DEBUG_ENGINE // string += '\n';
381
-
382
- // @if CK_DEBUG_ENGINE // if ( child.is( '$text' ) ) {
383
- // @if CK_DEBUG_ENGINE // const textAttrs = convertMapToTags( child._attrs );
384
-
385
- // @if CK_DEBUG_ENGINE // string += '\t'.repeat( level + 1 );
386
-
387
- // @if CK_DEBUG_ENGINE // if ( textAttrs !== '' ) {
388
- // @if CK_DEBUG_ENGINE // string += `<$text${ textAttrs }>` + child.data + '</$text>';
389
- // @if CK_DEBUG_ENGINE // } else {
390
- // @if CK_DEBUG_ENGINE // string += child.data;
391
- // @if CK_DEBUG_ENGINE // }
392
- // @if CK_DEBUG_ENGINE // } else {
393
- // @if CK_DEBUG_ENGINE // string += child.printTree( level + 1 );
394
- // @if CK_DEBUG_ENGINE // }
395
- // @if CK_DEBUG_ENGINE // }
396
-
397
- // @if CK_DEBUG_ENGINE // if ( this.childCount ) {
398
- // @if CK_DEBUG_ENGINE // string += '\n' + '\t'.repeat( level );
399
- // @if CK_DEBUG_ENGINE // }
400
-
401
- // @if CK_DEBUG_ENGINE // string += `</${ this.rootName || this.name }>`;
402
-
403
- // @if CK_DEBUG_ENGINE // return string;
404
- // @if CK_DEBUG_ENGINE // }
405
-
406
- // @if CK_DEBUG_ENGINE // logTree() {
407
- // @if CK_DEBUG_ENGINE // console.log( this.printTree() );
408
- // @if CK_DEBUG_ENGINE // }
23
+ /**
24
+ * Creates a model element.
25
+ *
26
+ * **Note:** Constructor of this class shouldn't be used directly in the code.
27
+ * Use the {@link module:engine/model/writer~Writer#createElement} method instead.
28
+ *
29
+ * @protected
30
+ * @param {String} name Element's name.
31
+ * @param {Object} [attrs] Element's attributes. See {@link module:utils/tomap~toMap} for a list of accepted values.
32
+ * @param {module:engine/model/node~Node|Iterable.<module:engine/model/node~Node>} [children]
33
+ * One or more nodes to be inserted as children of created element.
34
+ */
35
+ constructor(name, attrs, children) {
36
+ super(attrs);
37
+ /**
38
+ * Element name.
39
+ *
40
+ * @readonly
41
+ * @member {String} module:engine/model/element~Element#name
42
+ */
43
+ this.name = name;
44
+ /**
45
+ * List of children nodes.
46
+ *
47
+ * @private
48
+ * @member {module:engine/model/nodelist~NodeList} module:engine/model/element~Element#_children
49
+ */
50
+ this._children = new NodeList();
51
+ if (children) {
52
+ this._insertChild(0, children);
53
+ }
54
+ }
55
+ /**
56
+ * Number of this element's children.
57
+ *
58
+ * @readonly
59
+ * @type {Number}
60
+ */
61
+ get childCount() {
62
+ return this._children.length;
63
+ }
64
+ /**
65
+ * Sum of {@link module:engine/model/node~Node#offsetSize offset sizes} of all of this element's children.
66
+ *
67
+ * @readonly
68
+ * @type {Number}
69
+ */
70
+ get maxOffset() {
71
+ return this._children.maxOffset;
72
+ }
73
+ /**
74
+ * Is `true` if there are no nodes inside this element, `false` otherwise.
75
+ *
76
+ * @readonly
77
+ * @type {Boolean}
78
+ */
79
+ get isEmpty() {
80
+ return this.childCount === 0;
81
+ }
82
+ /**
83
+ * Gets the child at the given index.
84
+ *
85
+ * @param {Number} index Index of child.
86
+ * @returns {module:engine/model/node~Node|null} Child node.
87
+ */
88
+ getChild(index) {
89
+ return this._children.getNode(index);
90
+ }
91
+ /**
92
+ * Returns an iterator that iterates over all of this element's children.
93
+ *
94
+ * @returns {Iterable.<module:engine/model/node~Node>}
95
+ */
96
+ getChildren() {
97
+ return this._children[Symbol.iterator]();
98
+ }
99
+ /**
100
+ * Returns an index of the given child node. Returns `null` if given node is not a child of this element.
101
+ *
102
+ * @param {module:engine/model/node~Node} node Child node to look for.
103
+ * @returns {Number|null} Child node's index in this element.
104
+ */
105
+ getChildIndex(node) {
106
+ return this._children.getNodeIndex(node);
107
+ }
108
+ /**
109
+ * Returns the starting offset of given child. Starting offset is equal to the sum of
110
+ * {@link module:engine/model/node~Node#offsetSize offset sizes} of all node's siblings that are before it. Returns `null` if
111
+ * given node is not a child of this element.
112
+ *
113
+ * @param {module:engine/model/node~Node} node Child node to look for.
114
+ * @returns {Number|null} Child node's starting offset.
115
+ */
116
+ getChildStartOffset(node) {
117
+ return this._children.getNodeStartOffset(node);
118
+ }
119
+ /**
120
+ * Returns index of a node that occupies given offset. If given offset is too low, returns `0`. If given offset is
121
+ * too high, returns {@link module:engine/model/element~Element#getChildIndex index after last child}.
122
+ *
123
+ * const textNode = new Text( 'foo' );
124
+ * const pElement = new Element( 'p' );
125
+ * const divElement = new Element( [ textNode, pElement ] );
126
+ * divElement.offsetToIndex( -1 ); // Returns 0, because offset is too low.
127
+ * divElement.offsetToIndex( 0 ); // Returns 0, because offset 0 is taken by `textNode` which is at index 0.
128
+ * divElement.offsetToIndex( 1 ); // Returns 0, because `textNode` has `offsetSize` equal to 3, so it occupies offset 1 too.
129
+ * divElement.offsetToIndex( 2 ); // Returns 0.
130
+ * divElement.offsetToIndex( 3 ); // Returns 1.
131
+ * divElement.offsetToIndex( 4 ); // Returns 2. There are no nodes at offset 4, so last available index is returned.
132
+ *
133
+ * @param {Number} offset Offset to look for.
134
+ * @returns {Number}
135
+ */
136
+ offsetToIndex(offset) {
137
+ return this._children.offsetToIndex(offset);
138
+ }
139
+ /**
140
+ * Returns a descendant node by its path relative to this element.
141
+ *
142
+ * // <this>a<b>c</b></this>
143
+ * this.getNodeByPath( [ 0 ] ); // -> "a"
144
+ * this.getNodeByPath( [ 1 ] ); // -> <b>
145
+ * this.getNodeByPath( [ 1, 0 ] ); // -> "c"
146
+ *
147
+ * @param {Array.<Number>} relativePath Path of the node to find, relative to this element.
148
+ * @returns {module:engine/model/node~Node}
149
+ */
150
+ getNodeByPath(relativePath) {
151
+ // eslint-disable-next-line @typescript-eslint/no-this-alias, consistent-this
152
+ let node = this;
153
+ for (const index of relativePath) {
154
+ node = node.getChild(node.offsetToIndex(index));
155
+ }
156
+ return node;
157
+ }
158
+ /**
159
+ * Returns the parent element of the given name. Returns null if the element is not inside the desired parent.
160
+ *
161
+ * @param {String} parentName The name of the parent element to find.
162
+ * @param {Object} [options] Options object.
163
+ * @param {Boolean} [options.includeSelf=false] When set to `true` this node will be also included while searching.
164
+ * @returns {module:engine/model/element~Element|null}
165
+ */
166
+ findAncestor(parentName, options = {}) {
167
+ let parent = options.includeSelf ? this : this.parent;
168
+ while (parent) {
169
+ if (parent.name === parentName) {
170
+ return parent;
171
+ }
172
+ parent = parent.parent;
173
+ }
174
+ return null;
175
+ }
176
+ /**
177
+ * Converts `Element` instance to plain object and returns it. Takes care of converting all of this element's children.
178
+ *
179
+ * @returns {Object} `Element` instance converted to plain object.
180
+ */
181
+ toJSON() {
182
+ const json = super.toJSON();
183
+ json.name = this.name;
184
+ if (this._children.length > 0) {
185
+ json.children = [];
186
+ for (const node of this._children) {
187
+ json.children.push(node.toJSON());
188
+ }
189
+ }
190
+ return json;
191
+ }
192
+ /**
193
+ * Creates a copy of this element and returns it. Created element has the same name and attributes as the original element.
194
+ * If clone is deep, the original element's children are also cloned. If not, then empty element is returned.
195
+ *
196
+ * @internal
197
+ * @protected
198
+ * @param {Boolean} [deep=false] If set to `true` clones element and all its children recursively. When set to `false`,
199
+ * element will be cloned without any child.
200
+ */
201
+ _clone(deep = false) {
202
+ const children = deep ? Array.from(this._children).map(node => node._clone(true)) : undefined;
203
+ return new Element(this.name, this.getAttributes(), children);
204
+ }
205
+ /**
206
+ * {@link module:engine/model/element~Element#_insertChild Inserts} one or more nodes at the end of this element.
207
+ *
208
+ * @see module:engine/model/writer~Writer#append
209
+ * @internal
210
+ * @protected
211
+ * @param {String|module:engine/model/item~Item|Iterable.<String|module:engine/model/item~Item>} nodes Nodes to be inserted.
212
+ */
213
+ _appendChild(nodes) {
214
+ this._insertChild(this.childCount, nodes);
215
+ }
216
+ /**
217
+ * Inserts one or more nodes at the given index and sets {@link module:engine/model/node~Node#parent parent} of these nodes
218
+ * to this element.
219
+ *
220
+ * @see module:engine/model/writer~Writer#insert
221
+ * @internal
222
+ * @protected
223
+ * @param {Number} index Index at which nodes should be inserted.
224
+ * @param {String|module:engine/model/item~Item|Iterable.<String|module:engine/model/item~Item>} items Items to be inserted.
225
+ */
226
+ _insertChild(index, items) {
227
+ const nodes = normalize(items);
228
+ for (const node of nodes) {
229
+ // If node that is being added to this element is already inside another element, first remove it from the old parent.
230
+ if (node.parent !== null) {
231
+ node._remove();
232
+ }
233
+ node.parent = this;
234
+ }
235
+ this._children._insertNodes(index, nodes);
236
+ }
237
+ /**
238
+ * Removes one or more nodes starting at the given index and sets
239
+ * {@link module:engine/model/node~Node#parent parent} of these nodes to `null`.
240
+ *
241
+ * @see module:engine/model/writer~Writer#remove
242
+ * @protected
243
+ * @param {Number} index Index of the first node to remove.
244
+ * @param {Number} [howMany=1] Number of nodes to remove.
245
+ * @returns {Array.<module:engine/model/node~Node>} Array containing removed nodes.
246
+ */
247
+ _removeChildren(index, howMany = 1) {
248
+ const nodes = this._children._removeNodes(index, howMany);
249
+ for (const node of nodes) {
250
+ node.parent = null;
251
+ }
252
+ return nodes;
253
+ }
254
+ /**
255
+ * Creates an `Element` instance from given plain object (i.e. parsed JSON string).
256
+ * Converts `Element` children to proper nodes.
257
+ *
258
+ * @param {Object} json Plain object to be converted to `Element`.
259
+ * @returns {module:engine/model/element~Element} `Element` instance created using given plain object.
260
+ */
261
+ static fromJSON(json) {
262
+ let children;
263
+ if (json.children) {
264
+ children = [];
265
+ for (const child of json.children) {
266
+ if (child.name) {
267
+ // If child has name property, it is an Element.
268
+ children.push(Element.fromJSON(child));
269
+ }
270
+ else {
271
+ // Otherwise, it is a Text node.
272
+ children.push(Text.fromJSON(child));
273
+ }
274
+ }
275
+ }
276
+ return new Element(json.name, json.attributes, children);
277
+ }
409
278
  }
410
-
279
+ /**
280
+ * Checks whether this object is of the given.
281
+ *
282
+ * element.is( 'element' ); // -> true
283
+ * element.is( 'node' ); // -> true
284
+ * element.is( 'model:element' ); // -> true
285
+ * element.is( 'model:node' ); // -> true
286
+ *
287
+ * element.is( 'view:element' ); // -> false
288
+ * element.is( 'documentSelection' ); // -> false
289
+ *
290
+ * Assuming that the object being checked is an element, you can also check its
291
+ * {@link module:engine/model/element~Element#name name}:
292
+ *
293
+ * element.is( 'element', 'imageBlock' ); // -> true if this is an <imageBlock> element
294
+ * element.is( 'element', 'imageBlock' ); // -> same as above
295
+ * text.is( 'element', 'imageBlock' ); -> false
296
+ *
297
+ * {@link module:engine/model/node~Node#is Check the entire list of model objects} which implement the `is()` method.
298
+ *
299
+ * @param {String} type Type to check.
300
+ * @param {String} [name] Element name.
301
+ * @returns {Boolean}
302
+ */
303
+ Element.prototype.is = function (type, name) {
304
+ if (!name) {
305
+ return type === 'element' || type === 'model:element' ||
306
+ // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
307
+ type === 'node' || type === 'model:node';
308
+ }
309
+ return name === this.name && (type === 'element' || type === 'model:element');
310
+ };
411
311
  // Converts strings to Text and non-iterables to arrays.
412
312
  //
413
313
  // @param {String|module:engine/model/item~Item|Iterable.<String|module:engine/model/item~Item>}
414
314
  // @returns {Iterable.<module:engine/model/node~Node>}
415
- function normalize( nodes ) {
416
- // Separate condition because string is iterable.
417
- if ( typeof nodes == 'string' ) {
418
- return [ new Text( nodes ) ];
419
- }
420
-
421
- if ( !isIterable( nodes ) ) {
422
- nodes = [ nodes ];
423
- }
424
-
425
- // Array.from to enable .map() on non-arrays.
426
- return Array.from( nodes )
427
- .map( node => {
428
- if ( typeof node == 'string' ) {
429
- return new Text( node );
430
- }
431
-
432
- if ( node instanceof TextProxy ) {
433
- return new Text( node.data, node.getAttributes() );
434
- }
435
-
436
- return node;
437
- } );
315
+ function normalize(nodes) {
316
+ // Separate condition because string is iterable.
317
+ if (typeof nodes == 'string') {
318
+ return [new Text(nodes)];
319
+ }
320
+ if (!isIterable(nodes)) {
321
+ nodes = [nodes];
322
+ }
323
+ // Array.from to enable .map() on non-arrays.
324
+ return Array.from(nodes)
325
+ .map(node => {
326
+ if (typeof node == 'string') {
327
+ return new Text(node);
328
+ }
329
+ if (node instanceof TextProxy) {
330
+ return new Text(node.data, node.getAttributes());
331
+ }
332
+ return node;
333
+ });
438
334
  }