@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.
- package/CHANGELOG.md +823 -0
- package/LICENSE.md +4 -0
- package/package.json +32 -25
- package/src/controller/datacontroller.js +467 -561
- package/src/controller/editingcontroller.js +168 -204
- package/src/conversion/conversion.js +541 -565
- package/src/conversion/conversionhelpers.js +24 -28
- package/src/conversion/downcastdispatcher.js +457 -686
- package/src/conversion/downcasthelpers.js +1583 -1965
- package/src/conversion/mapper.js +518 -707
- package/src/conversion/modelconsumable.js +240 -283
- package/src/conversion/upcastdispatcher.js +372 -718
- package/src/conversion/upcasthelpers.js +707 -818
- package/src/conversion/viewconsumable.js +524 -581
- package/src/dataprocessor/basichtmlwriter.js +12 -16
- package/src/dataprocessor/dataprocessor.js +5 -0
- package/src/dataprocessor/htmldataprocessor.js +101 -117
- package/src/dataprocessor/htmlwriter.js +1 -18
- package/src/dataprocessor/xmldataprocessor.js +117 -138
- package/src/dev-utils/model.js +260 -352
- package/src/dev-utils/operationreplayer.js +106 -126
- package/src/dev-utils/utils.js +34 -51
- package/src/dev-utils/view.js +632 -753
- package/src/index.js +0 -11
- package/src/model/batch.js +111 -127
- package/src/model/differ.js +988 -1233
- package/src/model/document.js +340 -449
- package/src/model/documentfragment.js +327 -364
- package/src/model/documentselection.js +996 -1189
- package/src/model/element.js +306 -410
- package/src/model/history.js +224 -262
- package/src/model/item.js +5 -0
- package/src/model/liveposition.js +84 -145
- package/src/model/liverange.js +108 -185
- package/src/model/markercollection.js +379 -480
- package/src/model/model.js +883 -1034
- package/src/model/node.js +419 -463
- package/src/model/nodelist.js +175 -201
- package/src/model/operation/attributeoperation.js +153 -182
- package/src/model/operation/detachoperation.js +64 -83
- package/src/model/operation/insertoperation.js +135 -166
- package/src/model/operation/markeroperation.js +114 -140
- package/src/model/operation/mergeoperation.js +163 -191
- package/src/model/operation/moveoperation.js +157 -187
- package/src/model/operation/nooperation.js +28 -38
- package/src/model/operation/operation.js +106 -125
- package/src/model/operation/operationfactory.js +30 -34
- package/src/model/operation/renameoperation.js +109 -135
- package/src/model/operation/rootattributeoperation.js +155 -188
- package/src/model/operation/splitoperation.js +196 -232
- package/src/model/operation/transform.js +1833 -2204
- package/src/model/operation/utils.js +140 -204
- package/src/model/position.js +899 -1053
- package/src/model/range.js +910 -1028
- package/src/model/rootelement.js +77 -97
- package/src/model/schema.js +1189 -1835
- package/src/model/selection.js +745 -862
- package/src/model/text.js +90 -114
- package/src/model/textproxy.js +204 -240
- package/src/model/treewalker.js +316 -397
- package/src/model/typecheckable.js +16 -0
- package/src/model/utils/autoparagraphing.js +32 -44
- package/src/model/utils/deletecontent.js +334 -418
- package/src/model/utils/findoptimalinsertionrange.js +25 -36
- package/src/model/utils/getselectedcontent.js +96 -118
- package/src/model/utils/insertcontent.js +654 -773
- package/src/model/utils/insertobject.js +96 -119
- package/src/model/utils/modifyselection.js +120 -158
- package/src/model/utils/selection-post-fixer.js +153 -201
- package/src/model/writer.js +1305 -1474
- package/src/view/attributeelement.js +189 -225
- package/src/view/containerelement.js +75 -85
- package/src/view/document.js +172 -215
- package/src/view/documentfragment.js +200 -249
- package/src/view/documentselection.js +338 -367
- package/src/view/domconverter.js +1371 -1613
- package/src/view/downcastwriter.js +1747 -2076
- package/src/view/editableelement.js +81 -97
- package/src/view/element.js +739 -890
- package/src/view/elementdefinition.js +5 -0
- package/src/view/emptyelement.js +82 -92
- package/src/view/filler.js +35 -50
- package/src/view/item.js +5 -0
- package/src/view/matcher.js +260 -559
- package/src/view/node.js +274 -360
- package/src/view/observer/arrowkeysobserver.js +19 -28
- package/src/view/observer/bubblingemittermixin.js +120 -263
- package/src/view/observer/bubblingeventinfo.js +47 -55
- package/src/view/observer/clickobserver.js +7 -13
- package/src/view/observer/compositionobserver.js +14 -24
- package/src/view/observer/domeventdata.js +57 -67
- package/src/view/observer/domeventobserver.js +40 -64
- package/src/view/observer/fakeselectionobserver.js +81 -96
- package/src/view/observer/focusobserver.js +45 -61
- package/src/view/observer/inputobserver.js +7 -13
- package/src/view/observer/keyobserver.js +17 -27
- package/src/view/observer/mouseobserver.js +7 -14
- package/src/view/observer/mutationobserver.js +220 -315
- package/src/view/observer/observer.js +81 -102
- package/src/view/observer/selectionobserver.js +191 -246
- package/src/view/observer/tabobserver.js +23 -36
- package/src/view/placeholder.js +128 -173
- package/src/view/position.js +350 -401
- package/src/view/range.js +453 -513
- package/src/view/rawelement.js +85 -112
- package/src/view/renderer.js +874 -1014
- package/src/view/rooteditableelement.js +80 -90
- package/src/view/selection.js +608 -689
- package/src/view/styles/background.js +43 -44
- package/src/view/styles/border.js +220 -276
- package/src/view/styles/margin.js +8 -17
- package/src/view/styles/padding.js +8 -16
- package/src/view/styles/utils.js +127 -160
- package/src/view/stylesmap.js +728 -905
- package/src/view/text.js +102 -126
- package/src/view/textproxy.js +144 -170
- package/src/view/treewalker.js +383 -479
- package/src/view/typecheckable.js +19 -0
- package/src/view/uielement.js +166 -187
- package/src/view/upcastwriter.js +395 -449
- package/src/view/view.js +569 -664
- package/src/dataprocessor/dataprocessor.jsdoc +0 -64
- package/src/model/item.jsdoc +0 -14
- package/src/view/elementdefinition.jsdoc +0 -59
- package/src/view/item.jsdoc +0 -14
package/src/model/element.js
CHANGED
|
@@ -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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
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(
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
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
|
}
|