@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/view/treewalker.js
CHANGED
|
@@ -2,495 +2,399 @@
|
|
|
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/view/treewalker
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
8
|
import Element from './element';
|
|
11
9
|
import Text from './text';
|
|
12
10
|
import TextProxy from './textproxy';
|
|
13
11
|
import Position from './position';
|
|
14
12
|
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
|
|
15
|
-
|
|
16
13
|
/**
|
|
17
14
|
* Position iterator class. It allows to iterate forward and backward over the document.
|
|
18
15
|
*/
|
|
19
16
|
export default class TreeWalker {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
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
|
-
* Format returned data and adjust `previousPosition` and `nextPosition` if reach the bound of the {@link module:engine/view/text~Text}.
|
|
407
|
-
*
|
|
408
|
-
* @private
|
|
409
|
-
* @param {module:engine/view/treewalker~TreeWalkerValueType} type Type of step.
|
|
410
|
-
* @param {module:engine/view/item~Item} item Item between old and new position.
|
|
411
|
-
* @param {module:engine/view/position~Position} previousPosition Previous position of iterator.
|
|
412
|
-
* @param {module:engine/view/position~Position} nextPosition Next position of iterator.
|
|
413
|
-
* @param {Number} [length] Length of the item.
|
|
414
|
-
* @returns {module:engine/view/treewalker~TreeWalkerValue}
|
|
415
|
-
*/
|
|
416
|
-
_formatReturnValue( type, item, previousPosition, nextPosition, length ) {
|
|
417
|
-
// Text is a specific parent, because contains string instead of children.
|
|
418
|
-
// Walker doesn't enter to the Text except situations when walker is iterating over every single character,
|
|
419
|
-
// or the bound starts/ends inside the Text. So when the position is at the beginning or at the end of the Text
|
|
420
|
-
// we move it just before or just after Text.
|
|
421
|
-
if ( item instanceof TextProxy ) {
|
|
422
|
-
// Position is at the end of Text.
|
|
423
|
-
if ( item.offsetInText + item.data.length == item.textNode.data.length ) {
|
|
424
|
-
if ( this.direction == 'forward' && !( this.boundaries && this.boundaries.end.isEqual( this.position ) ) ) {
|
|
425
|
-
nextPosition = Position._createAfter( item.textNode );
|
|
426
|
-
// When we change nextPosition of returned value we need also update walker current position.
|
|
427
|
-
this.position = nextPosition;
|
|
428
|
-
} else {
|
|
429
|
-
previousPosition = Position._createAfter( item.textNode );
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
// Position is at the begining ot the text.
|
|
434
|
-
if ( item.offsetInText === 0 ) {
|
|
435
|
-
if ( this.direction == 'backward' && !( this.boundaries && this.boundaries.start.isEqual( this.position ) ) ) {
|
|
436
|
-
nextPosition = Position._createBefore( item.textNode );
|
|
437
|
-
// When we change nextPosition of returned value we need also update walker current position.
|
|
438
|
-
this.position = nextPosition;
|
|
439
|
-
} else {
|
|
440
|
-
previousPosition = Position._createBefore( item.textNode );
|
|
441
|
-
}
|
|
442
|
-
}
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
return {
|
|
446
|
-
done: false,
|
|
447
|
-
value: {
|
|
448
|
-
type,
|
|
449
|
-
item,
|
|
450
|
-
previousPosition,
|
|
451
|
-
nextPosition,
|
|
452
|
-
length
|
|
453
|
-
}
|
|
454
|
-
};
|
|
455
|
-
}
|
|
17
|
+
/**
|
|
18
|
+
* Creates a range iterator. All parameters are optional, but you have to specify either `boundaries` or `startPosition`.
|
|
19
|
+
*
|
|
20
|
+
* @constructor
|
|
21
|
+
* @param {TODO ~TreeWalkerOptions} options Object with configuration.
|
|
22
|
+
*/
|
|
23
|
+
constructor(options = {}) {
|
|
24
|
+
if (!options.boundaries && !options.startPosition) {
|
|
25
|
+
/**
|
|
26
|
+
* Neither boundaries nor starting position have been defined.
|
|
27
|
+
*
|
|
28
|
+
* @error view-tree-walker-no-start-position
|
|
29
|
+
*/
|
|
30
|
+
throw new CKEditorError('view-tree-walker-no-start-position', null);
|
|
31
|
+
}
|
|
32
|
+
if (options.direction && options.direction != 'forward' && options.direction != 'backward') {
|
|
33
|
+
/**
|
|
34
|
+
* Only `backward` and `forward` direction allowed.
|
|
35
|
+
*
|
|
36
|
+
* @error view-tree-walker-unknown-direction
|
|
37
|
+
*/
|
|
38
|
+
throw new CKEditorError('view-tree-walker-unknown-direction', options.startPosition, { direction: options.direction });
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Iterator boundaries.
|
|
42
|
+
*
|
|
43
|
+
* When the iterator is walking `'forward'` on the end of boundary or is walking `'backward'`
|
|
44
|
+
* on the start of boundary, then `{ done: true }` is returned.
|
|
45
|
+
*
|
|
46
|
+
* If boundaries are not defined they are set before first and after last child of the root node.
|
|
47
|
+
*
|
|
48
|
+
* @readonly
|
|
49
|
+
* @member {module:engine/view/range~Range} module:engine/view/treewalker~TreeWalker#boundaries
|
|
50
|
+
*/
|
|
51
|
+
this.boundaries = options.boundaries || null;
|
|
52
|
+
/**
|
|
53
|
+
* Iterator position. If start position is not defined then position depends on {@link #direction}. If direction is
|
|
54
|
+
* `'forward'` position starts form the beginning, when direction is `'backward'` position starts from the end.
|
|
55
|
+
*
|
|
56
|
+
* @readonly
|
|
57
|
+
* @member {module:engine/view/position~Position} module:engine/view/treewalker~TreeWalker#position
|
|
58
|
+
*/
|
|
59
|
+
if (options.startPosition) {
|
|
60
|
+
this.position = Position._createAt(options.startPosition);
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
this.position = Position._createAt(options.boundaries[options.direction == 'backward' ? 'end' : 'start']);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Walking direction. Defaults `'forward'`.
|
|
67
|
+
*
|
|
68
|
+
* @readonly
|
|
69
|
+
* @member {'backward'|'forward'} module:engine/view/treewalker~TreeWalker#direction
|
|
70
|
+
*/
|
|
71
|
+
this.direction = options.direction || 'forward';
|
|
72
|
+
/**
|
|
73
|
+
* Flag indicating whether all characters from {@link module:engine/view/text~Text} should be returned as one
|
|
74
|
+
* {@link module:engine/view/text~Text} or one by one as {@link module:engine/view/textproxy~TextProxy}.
|
|
75
|
+
*
|
|
76
|
+
* @readonly
|
|
77
|
+
* @member {Boolean} module:engine/view/treewalker~TreeWalker#singleCharacters
|
|
78
|
+
*/
|
|
79
|
+
this.singleCharacters = !!options.singleCharacters;
|
|
80
|
+
/**
|
|
81
|
+
* Flag indicating whether iterator should enter elements or not. If the iterator is shallow child nodes of any
|
|
82
|
+
* iterated node will not be returned along with `elementEnd` tag.
|
|
83
|
+
*
|
|
84
|
+
* @readonly
|
|
85
|
+
* @member {Boolean} module:engine/view/treewalker~TreeWalker#shallow
|
|
86
|
+
*/
|
|
87
|
+
this.shallow = !!options.shallow;
|
|
88
|
+
/**
|
|
89
|
+
* Flag indicating whether iterator should ignore `elementEnd` tags. If set to `true`, walker will not
|
|
90
|
+
* return a parent node of the start position. Each {@link module:engine/view/element~Element} will be returned once.
|
|
91
|
+
* When set to `false` each element might be returned twice: for `'elementStart'` and `'elementEnd'`.
|
|
92
|
+
*
|
|
93
|
+
* @readonly
|
|
94
|
+
* @member {Boolean} module:engine/view/treewalker~TreeWalker#ignoreElementEnd
|
|
95
|
+
*/
|
|
96
|
+
this.ignoreElementEnd = !!options.ignoreElementEnd;
|
|
97
|
+
/**
|
|
98
|
+
* Start boundary parent.
|
|
99
|
+
*
|
|
100
|
+
* @private
|
|
101
|
+
* @member {module:engine/view/node~Node} module:engine/view/treewalker~TreeWalker#_boundaryStartParent
|
|
102
|
+
*/
|
|
103
|
+
this._boundaryStartParent = this.boundaries ? this.boundaries.start.parent : null;
|
|
104
|
+
/**
|
|
105
|
+
* End boundary parent.
|
|
106
|
+
*
|
|
107
|
+
* @private
|
|
108
|
+
* @member {module:engine/view/node~Node} module:engine/view/treewalker~TreeWalker#_boundaryEndParent
|
|
109
|
+
*/
|
|
110
|
+
this._boundaryEndParent = this.boundaries ? this.boundaries.end.parent : null;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Iterable interface.
|
|
114
|
+
*
|
|
115
|
+
* @returns {Iterable.<module:engine/view/treewalker~TreeWalkerValue>}
|
|
116
|
+
*/
|
|
117
|
+
[Symbol.iterator]() {
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Moves {@link #position} in the {@link #direction} skipping values as long as the callback function returns `true`.
|
|
122
|
+
*
|
|
123
|
+
* For example:
|
|
124
|
+
*
|
|
125
|
+
* walker.skip( value => value.type == 'text' ); // <p>{}foo</p> -> <p>foo[]</p>
|
|
126
|
+
* walker.skip( value => true ); // Move the position to the end: <p>{}foo</p> -> <p>foo</p>[]
|
|
127
|
+
* walker.skip( value => false ); // Do not move the position.
|
|
128
|
+
*
|
|
129
|
+
* @param {Function} skip Callback function. Gets {@link module:engine/view/treewalker~TreeWalkerValue} and should
|
|
130
|
+
* return `true` if the value should be skipped or `false` if not.
|
|
131
|
+
*/
|
|
132
|
+
skip(skip) {
|
|
133
|
+
let done, value, prevPosition;
|
|
134
|
+
do {
|
|
135
|
+
prevPosition = this.position;
|
|
136
|
+
({ done, value } = this.next());
|
|
137
|
+
} while (!done && skip(value));
|
|
138
|
+
if (!done) {
|
|
139
|
+
this.position = prevPosition;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* Gets the next tree walker's value.
|
|
144
|
+
*
|
|
145
|
+
* @returns {module:engine/view/treewalker~TreeWalkerValue} Object implementing iterator interface, returning
|
|
146
|
+
* information about taken step.
|
|
147
|
+
*/
|
|
148
|
+
next() {
|
|
149
|
+
if (this.direction == 'forward') {
|
|
150
|
+
return this._next();
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
return this._previous();
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Makes a step forward in view. Moves the {@link #position} to the next position and returns the encountered value.
|
|
158
|
+
*
|
|
159
|
+
* @private
|
|
160
|
+
* @returns {Object}
|
|
161
|
+
* @returns {Boolean} return.done `true` if iterator is done, `false` otherwise.
|
|
162
|
+
* @returns {module:engine/view/treewalker~TreeWalkerValue} return.value Information about taken step.
|
|
163
|
+
*/
|
|
164
|
+
_next() {
|
|
165
|
+
let position = this.position.clone();
|
|
166
|
+
const previousPosition = this.position;
|
|
167
|
+
const parent = position.parent;
|
|
168
|
+
// We are at the end of the root.
|
|
169
|
+
if (parent.parent === null && position.offset === parent.childCount) {
|
|
170
|
+
return { done: true, value: undefined };
|
|
171
|
+
}
|
|
172
|
+
// We reached the walker boundary.
|
|
173
|
+
if (parent === this._boundaryEndParent && position.offset == this.boundaries.end.offset) {
|
|
174
|
+
return { done: true, value: undefined };
|
|
175
|
+
}
|
|
176
|
+
// Get node just after current position.
|
|
177
|
+
let node;
|
|
178
|
+
// Text is a specific parent because it contains string instead of child nodes.
|
|
179
|
+
if (parent instanceof Text) {
|
|
180
|
+
if (position.isAtEnd) {
|
|
181
|
+
// Prevent returning "elementEnd" for Text node. Skip that value and return the next walker step.
|
|
182
|
+
this.position = Position._createAfter(parent);
|
|
183
|
+
return this._next();
|
|
184
|
+
}
|
|
185
|
+
node = parent.data[position.offset];
|
|
186
|
+
}
|
|
187
|
+
else {
|
|
188
|
+
node = parent.getChild(position.offset);
|
|
189
|
+
}
|
|
190
|
+
if (node instanceof Element) {
|
|
191
|
+
if (!this.shallow) {
|
|
192
|
+
position = new Position(node, 0);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
position.offset++;
|
|
196
|
+
}
|
|
197
|
+
this.position = position;
|
|
198
|
+
return this._formatReturnValue('elementStart', node, previousPosition, position, 1);
|
|
199
|
+
}
|
|
200
|
+
else if (node instanceof Text) {
|
|
201
|
+
if (this.singleCharacters) {
|
|
202
|
+
position = new Position(node, 0);
|
|
203
|
+
this.position = position;
|
|
204
|
+
return this._next();
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
let charactersCount = node.data.length;
|
|
208
|
+
let item;
|
|
209
|
+
// If text stick out of walker range, we need to cut it and wrap in TextProxy.
|
|
210
|
+
if (node == this._boundaryEndParent) {
|
|
211
|
+
charactersCount = this.boundaries.end.offset;
|
|
212
|
+
item = new TextProxy(node, 0, charactersCount);
|
|
213
|
+
position = Position._createAfter(item);
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
item = new TextProxy(node, 0, node.data.length);
|
|
217
|
+
// If not just keep moving forward.
|
|
218
|
+
position.offset++;
|
|
219
|
+
}
|
|
220
|
+
this.position = position;
|
|
221
|
+
return this._formatReturnValue('text', item, previousPosition, position, charactersCount);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else if (typeof node == 'string') {
|
|
225
|
+
let textLength;
|
|
226
|
+
if (this.singleCharacters) {
|
|
227
|
+
textLength = 1;
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
// Check if text stick out of walker range.
|
|
231
|
+
const endOffset = parent === this._boundaryEndParent ? this.boundaries.end.offset : parent.data.length;
|
|
232
|
+
textLength = endOffset - position.offset;
|
|
233
|
+
}
|
|
234
|
+
const textProxy = new TextProxy(parent, position.offset, textLength);
|
|
235
|
+
position.offset += textLength;
|
|
236
|
+
this.position = position;
|
|
237
|
+
return this._formatReturnValue('text', textProxy, previousPosition, position, textLength);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
// `node` is not set, we reached the end of current `parent`.
|
|
241
|
+
position = Position._createAfter(parent);
|
|
242
|
+
this.position = position;
|
|
243
|
+
if (this.ignoreElementEnd) {
|
|
244
|
+
return this._next();
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
return this._formatReturnValue('elementEnd', parent, previousPosition, position);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Makes a step backward in view. Moves the {@link #position} to the previous position and returns the encountered value.
|
|
253
|
+
*
|
|
254
|
+
* @private
|
|
255
|
+
* @returns {Object}
|
|
256
|
+
* @returns {Boolean} return.done True if iterator is done.
|
|
257
|
+
* @returns {module:engine/view/treewalker~TreeWalkerValue} return.value Information about taken step.
|
|
258
|
+
*/
|
|
259
|
+
_previous() {
|
|
260
|
+
let position = this.position.clone();
|
|
261
|
+
const previousPosition = this.position;
|
|
262
|
+
const parent = position.parent;
|
|
263
|
+
// We are at the beginning of the root.
|
|
264
|
+
if (parent.parent === null && position.offset === 0) {
|
|
265
|
+
return { done: true, value: undefined };
|
|
266
|
+
}
|
|
267
|
+
// We reached the walker boundary.
|
|
268
|
+
if (parent == this._boundaryStartParent && position.offset == this.boundaries.start.offset) {
|
|
269
|
+
return { done: true, value: undefined };
|
|
270
|
+
}
|
|
271
|
+
// Get node just before current position.
|
|
272
|
+
let node;
|
|
273
|
+
// Text {@link module:engine/view/text~Text} element is a specific parent because contains string instead of child nodes.
|
|
274
|
+
if (parent instanceof Text) {
|
|
275
|
+
if (position.isAtStart) {
|
|
276
|
+
// Prevent returning "elementStart" for Text node. Skip that value and return the next walker step.
|
|
277
|
+
this.position = Position._createBefore(parent);
|
|
278
|
+
return this._previous();
|
|
279
|
+
}
|
|
280
|
+
node = parent.data[position.offset - 1];
|
|
281
|
+
}
|
|
282
|
+
else {
|
|
283
|
+
node = parent.getChild(position.offset - 1);
|
|
284
|
+
}
|
|
285
|
+
if (node instanceof Element) {
|
|
286
|
+
if (!this.shallow) {
|
|
287
|
+
position = new Position(node, node.childCount);
|
|
288
|
+
this.position = position;
|
|
289
|
+
if (this.ignoreElementEnd) {
|
|
290
|
+
return this._previous();
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
return this._formatReturnValue('elementEnd', node, previousPosition, position);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
position.offset--;
|
|
298
|
+
this.position = position;
|
|
299
|
+
return this._formatReturnValue('elementStart', node, previousPosition, position, 1);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
else if (node instanceof Text) {
|
|
303
|
+
if (this.singleCharacters) {
|
|
304
|
+
position = new Position(node, node.data.length);
|
|
305
|
+
this.position = position;
|
|
306
|
+
return this._previous();
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
let charactersCount = node.data.length;
|
|
310
|
+
let item;
|
|
311
|
+
// If text stick out of walker range, we need to cut it and wrap in TextProxy.
|
|
312
|
+
if (node == this._boundaryStartParent) {
|
|
313
|
+
const offset = this.boundaries.start.offset;
|
|
314
|
+
item = new TextProxy(node, offset, node.data.length - offset);
|
|
315
|
+
charactersCount = item.data.length;
|
|
316
|
+
position = Position._createBefore(item);
|
|
317
|
+
}
|
|
318
|
+
else {
|
|
319
|
+
item = new TextProxy(node, 0, node.data.length);
|
|
320
|
+
// If not just keep moving backward.
|
|
321
|
+
position.offset--;
|
|
322
|
+
}
|
|
323
|
+
this.position = position;
|
|
324
|
+
return this._formatReturnValue('text', item, previousPosition, position, charactersCount);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
else if (typeof node == 'string') {
|
|
328
|
+
let textLength;
|
|
329
|
+
if (!this.singleCharacters) {
|
|
330
|
+
// Check if text stick out of walker range.
|
|
331
|
+
const startOffset = parent === this._boundaryStartParent ? this.boundaries.start.offset : 0;
|
|
332
|
+
textLength = position.offset - startOffset;
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
textLength = 1;
|
|
336
|
+
}
|
|
337
|
+
position.offset -= textLength;
|
|
338
|
+
const textProxy = new TextProxy(parent, position.offset, textLength);
|
|
339
|
+
this.position = position;
|
|
340
|
+
return this._formatReturnValue('text', textProxy, previousPosition, position, textLength);
|
|
341
|
+
}
|
|
342
|
+
else {
|
|
343
|
+
// `node` is not set, we reached the beginning of current `parent`.
|
|
344
|
+
position = Position._createBefore(parent);
|
|
345
|
+
this.position = position;
|
|
346
|
+
return this._formatReturnValue('elementStart', parent, previousPosition, position, 1);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Format returned data and adjust `previousPosition` and `nextPosition` if reach the bound of the {@link module:engine/view/text~Text}.
|
|
351
|
+
*
|
|
352
|
+
* @private
|
|
353
|
+
* @param {module:engine/view/treewalker~TreeWalkerValueType} type Type of step.
|
|
354
|
+
* @param {module:engine/view/item~Item} item Item between old and new position.
|
|
355
|
+
* @param {module:engine/view/position~Position} previousPosition Previous position of iterator.
|
|
356
|
+
* @param {module:engine/view/position~Position} nextPosition Next position of iterator.
|
|
357
|
+
* @param {Number} [length] Length of the item.
|
|
358
|
+
* @returns {module:engine/view/treewalker~TreeWalkerValue}
|
|
359
|
+
*/
|
|
360
|
+
_formatReturnValue(type, item, previousPosition, nextPosition, length) {
|
|
361
|
+
// Text is a specific parent, because contains string instead of children.
|
|
362
|
+
// Walker doesn't enter to the Text except situations when walker is iterating over every single character,
|
|
363
|
+
// or the bound starts/ends inside the Text. So when the position is at the beginning or at the end of the Text
|
|
364
|
+
// we move it just before or just after Text.
|
|
365
|
+
if (item instanceof TextProxy) {
|
|
366
|
+
// Position is at the end of Text.
|
|
367
|
+
if (item.offsetInText + item.data.length == item.textNode.data.length) {
|
|
368
|
+
if (this.direction == 'forward' && !(this.boundaries && this.boundaries.end.isEqual(this.position))) {
|
|
369
|
+
nextPosition = Position._createAfter(item.textNode);
|
|
370
|
+
// When we change nextPosition of returned value we need also update walker current position.
|
|
371
|
+
this.position = nextPosition;
|
|
372
|
+
}
|
|
373
|
+
else {
|
|
374
|
+
previousPosition = Position._createAfter(item.textNode);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
// Position is at the begining ot the text.
|
|
378
|
+
if (item.offsetInText === 0) {
|
|
379
|
+
if (this.direction == 'backward' && !(this.boundaries && this.boundaries.start.isEqual(this.position))) {
|
|
380
|
+
nextPosition = Position._createBefore(item.textNode);
|
|
381
|
+
// When we change nextPosition of returned value we need also update walker current position.
|
|
382
|
+
this.position = nextPosition;
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
previousPosition = Position._createBefore(item.textNode);
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
return {
|
|
390
|
+
done: false,
|
|
391
|
+
value: {
|
|
392
|
+
type,
|
|
393
|
+
item,
|
|
394
|
+
previousPosition,
|
|
395
|
+
nextPosition,
|
|
396
|
+
length
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
}
|
|
456
400
|
}
|
|
457
|
-
|
|
458
|
-
/**
|
|
459
|
-
* Type of the step made by {@link module:engine/view/treewalker~TreeWalker}.
|
|
460
|
-
* Possible values: `'elementStart'` if walker is at the beginning of a node, `'elementEnd'` if walker is at the end
|
|
461
|
-
* of node, or `'text'` if walker traversed over single and multiple characters.
|
|
462
|
-
* For {@link module:engine/view/text~Text} `elementStart` and `elementEnd` is not returned.
|
|
463
|
-
*
|
|
464
|
-
* @typedef {String} module:engine/view/treewalker~TreeWalkerValueType
|
|
465
|
-
*/
|
|
466
|
-
|
|
467
|
-
/**
|
|
468
|
-
* Object returned by {@link module:engine/view/treewalker~TreeWalker} when traversing tree view.
|
|
469
|
-
*
|
|
470
|
-
* @typedef {Object} module:engine/view/treewalker~TreeWalkerValue
|
|
471
|
-
* @property {module:engine/view/treewalker~TreeWalkerValueType} type
|
|
472
|
-
* @property {module:engine/view/item~Item} item Item between the old and the new positions
|
|
473
|
-
* of the tree walker.
|
|
474
|
-
* @property {module:engine/view/position~Position} previousPosition Previous position of the iterator.
|
|
475
|
-
* * Forward iteration: For `'elementEnd'` it is the last position inside the element. For all other types it is the
|
|
476
|
-
* position before the item.
|
|
477
|
-
* * Backward iteration: For `'elementStart'` it is the first position inside the element. For all other types it is
|
|
478
|
-
* the position after item.
|
|
479
|
-
* * If the position is at the beginning or at the end of the {@link module:engine/view/text~Text} it is always moved from the
|
|
480
|
-
* inside of the text to its parent just before or just after that text.
|
|
481
|
-
* @property {module:engine/view/position~Position} nextPosition Next position of the iterator.
|
|
482
|
-
* * Forward iteration: For `'elementStart'` it is the first position inside the element. For all other types it is
|
|
483
|
-
* the position after the item.
|
|
484
|
-
* * Backward iteration: For `'elementEnd'` it is last position inside element. For all other types it is the position
|
|
485
|
-
* before the item.
|
|
486
|
-
* * If the position is at the beginning or at the end of the {@link module:engine/view/text~Text} it is always moved from the
|
|
487
|
-
* inside of the text to its parent just before or just after that text.
|
|
488
|
-
* @property {Number} [length] Length of the item. For `'elementStart'` it is `1`. For `'text'` it is
|
|
489
|
-
* the length of that text. For `'elementEnd'` it is `undefined`.
|
|
490
|
-
*/
|
|
491
|
-
|
|
492
|
-
/**
|
|
493
|
-
* Tree walking directions.
|
|
494
|
-
*
|
|
495
|
-
* @typedef {'forward'|'backward'} module:engine/view/treewalker~TreeWalkerDirection
|
|
496
|
-
*/
|