@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/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
|
-
|
|
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
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
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
|
-
*
|
|
419
|
+
* Checks whether this object is of the given type.
|
|
505
420
|
*
|
|
506
|
-
*
|
|
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
|
+
};
|