@ckeditor/ckeditor5-engine 35.0.1 → 35.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (124) hide show
  1. package/CHANGELOG.md +4 -4
  2. package/package.json +30 -24
  3. package/src/controller/datacontroller.js +467 -561
  4. package/src/controller/editingcontroller.js +168 -204
  5. package/src/conversion/conversion.js +541 -565
  6. package/src/conversion/conversionhelpers.js +24 -28
  7. package/src/conversion/downcastdispatcher.js +457 -686
  8. package/src/conversion/downcasthelpers.js +1583 -1965
  9. package/src/conversion/mapper.js +518 -707
  10. package/src/conversion/modelconsumable.js +240 -283
  11. package/src/conversion/upcastdispatcher.js +372 -718
  12. package/src/conversion/upcasthelpers.js +707 -818
  13. package/src/conversion/viewconsumable.js +524 -581
  14. package/src/dataprocessor/basichtmlwriter.js +12 -16
  15. package/src/dataprocessor/dataprocessor.js +5 -0
  16. package/src/dataprocessor/htmldataprocessor.js +100 -116
  17. package/src/dataprocessor/htmlwriter.js +1 -18
  18. package/src/dataprocessor/xmldataprocessor.js +116 -137
  19. package/src/dev-utils/model.js +260 -352
  20. package/src/dev-utils/operationreplayer.js +106 -126
  21. package/src/dev-utils/utils.js +34 -51
  22. package/src/dev-utils/view.js +632 -753
  23. package/src/index.js +0 -11
  24. package/src/model/batch.js +111 -127
  25. package/src/model/differ.js +988 -1233
  26. package/src/model/document.js +340 -449
  27. package/src/model/documentfragment.js +327 -364
  28. package/src/model/documentselection.js +996 -1189
  29. package/src/model/element.js +306 -410
  30. package/src/model/history.js +224 -262
  31. package/src/model/item.js +5 -0
  32. package/src/model/liveposition.js +84 -145
  33. package/src/model/liverange.js +108 -185
  34. package/src/model/markercollection.js +379 -480
  35. package/src/model/model.js +883 -1034
  36. package/src/model/node.js +419 -463
  37. package/src/model/nodelist.js +176 -201
  38. package/src/model/operation/attributeoperation.js +153 -182
  39. package/src/model/operation/detachoperation.js +64 -83
  40. package/src/model/operation/insertoperation.js +135 -166
  41. package/src/model/operation/markeroperation.js +114 -140
  42. package/src/model/operation/mergeoperation.js +163 -191
  43. package/src/model/operation/moveoperation.js +157 -187
  44. package/src/model/operation/nooperation.js +28 -38
  45. package/src/model/operation/operation.js +106 -125
  46. package/src/model/operation/operationfactory.js +30 -34
  47. package/src/model/operation/renameoperation.js +109 -135
  48. package/src/model/operation/rootattributeoperation.js +155 -188
  49. package/src/model/operation/splitoperation.js +196 -232
  50. package/src/model/operation/transform.js +1833 -2204
  51. package/src/model/operation/utils.js +140 -204
  52. package/src/model/position.js +980 -1053
  53. package/src/model/range.js +910 -1028
  54. package/src/model/rootelement.js +77 -97
  55. package/src/model/schema.js +1189 -1835
  56. package/src/model/selection.js +745 -862
  57. package/src/model/text.js +90 -114
  58. package/src/model/textproxy.js +204 -240
  59. package/src/model/treewalker.js +316 -397
  60. package/src/model/typecheckable.js +16 -0
  61. package/src/model/utils/autoparagraphing.js +32 -44
  62. package/src/model/utils/deletecontent.js +334 -418
  63. package/src/model/utils/findoptimalinsertionrange.js +25 -36
  64. package/src/model/utils/getselectedcontent.js +96 -118
  65. package/src/model/utils/insertcontent.js +757 -773
  66. package/src/model/utils/insertobject.js +96 -119
  67. package/src/model/utils/modifyselection.js +120 -158
  68. package/src/model/utils/selection-post-fixer.js +153 -201
  69. package/src/model/writer.js +1305 -1474
  70. package/src/view/attributeelement.js +189 -225
  71. package/src/view/containerelement.js +75 -85
  72. package/src/view/document.js +172 -215
  73. package/src/view/documentfragment.js +200 -249
  74. package/src/view/documentselection.js +338 -367
  75. package/src/view/domconverter.js +1370 -1617
  76. package/src/view/downcastwriter.js +1747 -2076
  77. package/src/view/editableelement.js +81 -97
  78. package/src/view/element.js +739 -890
  79. package/src/view/elementdefinition.js +5 -0
  80. package/src/view/emptyelement.js +82 -92
  81. package/src/view/filler.js +35 -50
  82. package/src/view/item.js +5 -0
  83. package/src/view/matcher.js +260 -559
  84. package/src/view/node.js +274 -360
  85. package/src/view/observer/arrowkeysobserver.js +19 -28
  86. package/src/view/observer/bubblingemittermixin.js +120 -263
  87. package/src/view/observer/bubblingeventinfo.js +47 -55
  88. package/src/view/observer/clickobserver.js +7 -13
  89. package/src/view/observer/compositionobserver.js +14 -24
  90. package/src/view/observer/domeventdata.js +57 -67
  91. package/src/view/observer/domeventobserver.js +40 -64
  92. package/src/view/observer/fakeselectionobserver.js +81 -96
  93. package/src/view/observer/focusobserver.js +45 -61
  94. package/src/view/observer/inputobserver.js +7 -13
  95. package/src/view/observer/keyobserver.js +17 -27
  96. package/src/view/observer/mouseobserver.js +7 -14
  97. package/src/view/observer/mutationobserver.js +220 -315
  98. package/src/view/observer/observer.js +81 -102
  99. package/src/view/observer/selectionobserver.js +199 -246
  100. package/src/view/observer/tabobserver.js +23 -36
  101. package/src/view/placeholder.js +128 -173
  102. package/src/view/position.js +350 -401
  103. package/src/view/range.js +453 -513
  104. package/src/view/rawelement.js +85 -112
  105. package/src/view/renderer.js +874 -1018
  106. package/src/view/rooteditableelement.js +80 -90
  107. package/src/view/selection.js +608 -689
  108. package/src/view/styles/background.js +43 -44
  109. package/src/view/styles/border.js +220 -276
  110. package/src/view/styles/margin.js +8 -17
  111. package/src/view/styles/padding.js +8 -16
  112. package/src/view/styles/utils.js +127 -160
  113. package/src/view/stylesmap.js +728 -905
  114. package/src/view/text.js +102 -126
  115. package/src/view/textproxy.js +144 -170
  116. package/src/view/treewalker.js +383 -479
  117. package/src/view/typecheckable.js +19 -0
  118. package/src/view/uielement.js +166 -187
  119. package/src/view/upcastwriter.js +395 -449
  120. package/src/view/view.js +569 -664
  121. package/src/dataprocessor/dataprocessor.jsdoc +0 -64
  122. package/src/model/item.jsdoc +0 -14
  123. package/src/view/elementdefinition.jsdoc +0 -59
  124. package/src/view/item.jsdoc +0 -14
@@ -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
- * Creates a range iterator. All parameters are optional, but you have to specify either `boundaries` or `startPosition`.
22
- *
23
- * @constructor
24
- * @param {Object} options Object with configuration.
25
- * @param {module:engine/view/range~Range} [options.boundaries=null] Range to define boundaries of the iterator.
26
- * @param {module:engine/view/position~Position} [options.startPosition] Starting position.
27
- * @param {'forward'|'backward'} [options.direction='forward'] Walking direction.
28
- * @param {Boolean} [options.singleCharacters=false] Flag indicating whether all characters from
29
- * {@link module:engine/view/text~Text} should be returned as one {@link module:engine/view/text~Text} (`false`) ore one by one as
30
- * {@link module:engine/view/textproxy~TextProxy} (`true`).
31
- * @param {Boolean} [options.shallow=false] Flag indicating whether iterator should enter elements or not. If the
32
- * iterator is shallow child nodes of any iterated node will not be returned along with `elementEnd` tag.
33
- * @param {Boolean} [options.ignoreElementEnd=false] Flag indicating whether iterator should ignore `elementEnd`
34
- * tags. If the option is true walker will not return a parent node of start position. If this option is `true`
35
- * each {@link module:engine/view/element~Element} will be returned once, while if the option is `false` they might be returned
36
- * twice: for `'elementStart'` and `'elementEnd'`.
37
- */
38
- constructor( options = {} ) {
39
- if ( !options.boundaries && !options.startPosition ) {
40
- /**
41
- * Neither boundaries nor starting position have been defined.
42
- *
43
- * @error view-tree-walker-no-start-position
44
- */
45
- throw new CKEditorError(
46
- 'view-tree-walker-no-start-position',
47
- null
48
- );
49
- }
50
-
51
- if ( options.direction && options.direction != 'forward' && options.direction != 'backward' ) {
52
- /**
53
- * Only `backward` and `forward` direction allowed.
54
- *
55
- * @error view-tree-walker-unknown-direction
56
- */
57
- throw new CKEditorError( 'view-tree-walker-unknown-direction', options.startPosition, { direction: options.direction } );
58
- }
59
-
60
- /**
61
- * Iterator boundaries.
62
- *
63
- * When the iterator is walking `'forward'` on the end of boundary or is walking `'backward'`
64
- * on the start of boundary, then `{ done: true }` is returned.
65
- *
66
- * If boundaries are not defined they are set before first and after last child of the root node.
67
- *
68
- * @readonly
69
- * @member {module:engine/view/range~Range} module:engine/view/treewalker~TreeWalker#boundaries
70
- */
71
- this.boundaries = options.boundaries || null;
72
-
73
- /**
74
- * Iterator position. If start position is not defined then position depends on {@link #direction}. If direction is
75
- * `'forward'` position starts form the beginning, when direction is `'backward'` position starts from the end.
76
- *
77
- * @readonly
78
- * @member {module:engine/view/position~Position} module:engine/view/treewalker~TreeWalker#position
79
- */
80
- if ( options.startPosition ) {
81
- this.position = Position._createAt( options.startPosition );
82
- } else {
83
- this.position = Position._createAt( options.boundaries[ options.direction == 'backward' ? 'end' : 'start' ] );
84
- }
85
-
86
- /**
87
- * Walking direction. Defaults `'forward'`.
88
- *
89
- * @readonly
90
- * @member {'backward'|'forward'} module:engine/view/treewalker~TreeWalker#direction
91
- */
92
- this.direction = options.direction || 'forward';
93
-
94
- /**
95
- * Flag indicating whether all characters from {@link module:engine/view/text~Text} should be returned as one
96
- * {@link module:engine/view/text~Text} or one by one as {@link module:engine/view/textproxy~TextProxy}.
97
- *
98
- * @readonly
99
- * @member {Boolean} module:engine/view/treewalker~TreeWalker#singleCharacters
100
- */
101
- this.singleCharacters = !!options.singleCharacters;
102
-
103
- /**
104
- * Flag indicating whether iterator should enter elements or not. If the iterator is shallow child nodes of any
105
- * iterated node will not be returned along with `elementEnd` tag.
106
- *
107
- * @readonly
108
- * @member {Boolean} module:engine/view/treewalker~TreeWalker#shallow
109
- */
110
- this.shallow = !!options.shallow;
111
-
112
- /**
113
- * Flag indicating whether iterator should ignore `elementEnd` tags. If set to `true`, walker will not
114
- * return a parent node of the start position. Each {@link module:engine/view/element~Element} will be returned once.
115
- * When set to `false` each element might be returned twice: for `'elementStart'` and `'elementEnd'`.
116
- *
117
- * @readonly
118
- * @member {Boolean} module:engine/view/treewalker~TreeWalker#ignoreElementEnd
119
- */
120
- this.ignoreElementEnd = !!options.ignoreElementEnd;
121
-
122
- /**
123
- * Start boundary parent.
124
- *
125
- * @private
126
- * @member {module:engine/view/node~Node} module:engine/view/treewalker~TreeWalker#_boundaryStartParent
127
- */
128
- this._boundaryStartParent = this.boundaries ? this.boundaries.start.parent : null;
129
-
130
- /**
131
- * End boundary parent.
132
- *
133
- * @private
134
- * @member {module:engine/view/node~Node} module:engine/view/treewalker~TreeWalker#_boundaryEndParent
135
- */
136
- this._boundaryEndParent = this.boundaries ? this.boundaries.end.parent : null;
137
- }
138
-
139
- /**
140
- * Iterable interface.
141
- *
142
- * @returns {Iterable.<module:engine/view/treewalker~TreeWalkerValue>}
143
- */
144
- [ Symbol.iterator ]() {
145
- return this;
146
- }
147
-
148
- /**
149
- * Moves {@link #position} in the {@link #direction} skipping values as long as the callback function returns `true`.
150
- *
151
- * For example:
152
- *
153
- * walker.skip( value => value.type == 'text' ); // <p>{}foo</p> -> <p>foo[]</p>
154
- * walker.skip( value => true ); // Move the position to the end: <p>{}foo</p> -> <p>foo</p>[]
155
- * walker.skip( value => false ); // Do not move the position.
156
- *
157
- * @param {Function} skip Callback function. Gets {@link module:engine/view/treewalker~TreeWalkerValue} and should
158
- * return `true` if the value should be skipped or `false` if not.
159
- */
160
- skip( skip ) {
161
- let done, value, prevPosition;
162
-
163
- do {
164
- prevPosition = this.position;
165
-
166
- ( { done, value } = this.next() );
167
- } while ( !done && skip( value ) );
168
-
169
- if ( !done ) {
170
- this.position = prevPosition;
171
- }
172
- }
173
-
174
- /**
175
- * Gets the next tree walker's value.
176
- *
177
- * @returns {module:engine/view/treewalker~TreeWalkerValue} Object implementing iterator interface, returning
178
- * information about taken step.
179
- */
180
- next() {
181
- if ( this.direction == 'forward' ) {
182
- return this._next();
183
- } else {
184
- return this._previous();
185
- }
186
- }
187
-
188
- /**
189
- * Makes a step forward in view. Moves the {@link #position} to the next position and returns the encountered value.
190
- *
191
- * @private
192
- * @returns {Object}
193
- * @returns {Boolean} return.done `true` if iterator is done, `false` otherwise.
194
- * @returns {module:engine/view/treewalker~TreeWalkerValue} return.value Information about taken step.
195
- */
196
- _next() {
197
- let position = this.position.clone();
198
- const previousPosition = this.position;
199
- const parent = position.parent;
200
-
201
- // We are at the end of the root.
202
- if ( parent.parent === null && position.offset === parent.childCount ) {
203
- return { done: true };
204
- }
205
-
206
- // We reached the walker boundary.
207
- if ( parent === this._boundaryEndParent && position.offset == this.boundaries.end.offset ) {
208
- return { done: true };
209
- }
210
-
211
- // Get node just after current position.
212
- let node;
213
-
214
- // Text is a specific parent because it contains string instead of child nodes.
215
- if ( parent instanceof Text ) {
216
- if ( position.isAtEnd ) {
217
- // Prevent returning "elementEnd" for Text node. Skip that value and return the next walker step.
218
- this.position = Position._createAfter( parent );
219
-
220
- return this._next();
221
- }
222
-
223
- node = parent.data[ position.offset ];
224
- } else {
225
- node = parent.getChild( position.offset );
226
- }
227
-
228
- if ( node instanceof Element ) {
229
- if ( !this.shallow ) {
230
- position = new Position( node, 0 );
231
- } else {
232
- position.offset++;
233
- }
234
-
235
- this.position = position;
236
-
237
- return this._formatReturnValue( 'elementStart', node, previousPosition, position, 1 );
238
- } else if ( node instanceof Text ) {
239
- if ( this.singleCharacters ) {
240
- position = new Position( node, 0 );
241
- this.position = position;
242
-
243
- return this._next();
244
- } else {
245
- let charactersCount = node.data.length;
246
- let item;
247
-
248
- // If text stick out of walker range, we need to cut it and wrap in TextProxy.
249
- if ( node == this._boundaryEndParent ) {
250
- charactersCount = this.boundaries.end.offset;
251
- item = new TextProxy( node, 0, charactersCount );
252
- position = Position._createAfter( item );
253
- } else {
254
- item = new TextProxy( node, 0, node.data.length );
255
- // If not just keep moving forward.
256
- position.offset++;
257
- }
258
-
259
- this.position = position;
260
-
261
- return this._formatReturnValue( 'text', item, previousPosition, position, charactersCount );
262
- }
263
- } else if ( typeof node == 'string' ) {
264
- let textLength;
265
-
266
- if ( this.singleCharacters ) {
267
- textLength = 1;
268
- } else {
269
- // Check if text stick out of walker range.
270
- const endOffset = parent === this._boundaryEndParent ? this.boundaries.end.offset : parent.data.length;
271
-
272
- textLength = endOffset - position.offset;
273
- }
274
-
275
- const textProxy = new TextProxy( parent, position.offset, textLength );
276
-
277
- position.offset += textLength;
278
- this.position = position;
279
-
280
- return this._formatReturnValue( 'text', textProxy, previousPosition, position, textLength );
281
- } else {
282
- // `node` is not set, we reached the end of current `parent`.
283
- position = Position._createAfter( parent );
284
- this.position = position;
285
-
286
- if ( this.ignoreElementEnd ) {
287
- return this._next();
288
- } else {
289
- return this._formatReturnValue( 'elementEnd', parent, previousPosition, position );
290
- }
291
- }
292
- }
293
-
294
- /**
295
- * Makes a step backward in view. Moves the {@link #position} to the previous position and returns the encountered value.
296
- *
297
- * @private
298
- * @returns {Object}
299
- * @returns {Boolean} return.done True if iterator is done.
300
- * @returns {module:engine/view/treewalker~TreeWalkerValue} return.value Information about taken step.
301
- */
302
- _previous() {
303
- let position = this.position.clone();
304
- const previousPosition = this.position;
305
- const parent = position.parent;
306
-
307
- // We are at the beginning of the root.
308
- if ( parent.parent === null && position.offset === 0 ) {
309
- return { done: true };
310
- }
311
-
312
- // We reached the walker boundary.
313
- if ( parent == this._boundaryStartParent && position.offset == this.boundaries.start.offset ) {
314
- return { done: true };
315
- }
316
-
317
- // Get node just before current position.
318
- let node;
319
-
320
- // Text {@link module:engine/view/text~Text} element is a specific parent because contains string instead of child nodes.
321
- if ( parent instanceof Text ) {
322
- if ( position.isAtStart ) {
323
- // Prevent returning "elementStart" for Text node. Skip that value and return the next walker step.
324
- this.position = Position._createBefore( parent );
325
-
326
- return this._previous();
327
- }
328
-
329
- node = parent.data[ position.offset - 1 ];
330
- } else {
331
- node = parent.getChild( position.offset - 1 );
332
- }
333
-
334
- if ( node instanceof Element ) {
335
- if ( !this.shallow ) {
336
- position = new Position( node, node.childCount );
337
- this.position = position;
338
-
339
- if ( this.ignoreElementEnd ) {
340
- return this._previous();
341
- } else {
342
- return this._formatReturnValue( 'elementEnd', node, previousPosition, position );
343
- }
344
- } else {
345
- position.offset--;
346
- this.position = position;
347
-
348
- return this._formatReturnValue( 'elementStart', node, previousPosition, position, 1 );
349
- }
350
- } else if ( node instanceof Text ) {
351
- if ( this.singleCharacters ) {
352
- position = new Position( node, node.data.length );
353
- this.position = position;
354
-
355
- return this._previous();
356
- } else {
357
- let charactersCount = node.data.length;
358
- let item;
359
-
360
- // If text stick out of walker range, we need to cut it and wrap in TextProxy.
361
- if ( node == this._boundaryStartParent ) {
362
- const offset = this.boundaries.start.offset;
363
-
364
- item = new TextProxy( node, offset, node.data.length - offset );
365
- charactersCount = item.data.length;
366
- position = Position._createBefore( item );
367
- } else {
368
- item = new TextProxy( node, 0, node.data.length );
369
- // If not just keep moving backward.
370
- position.offset--;
371
- }
372
-
373
- this.position = position;
374
-
375
- return this._formatReturnValue( 'text', item, previousPosition, position, charactersCount );
376
- }
377
- } else if ( typeof node == 'string' ) {
378
- let textLength;
379
-
380
- if ( !this.singleCharacters ) {
381
- // Check if text stick out of walker range.
382
- const startOffset = parent === this._boundaryStartParent ? this.boundaries.start.offset : 0;
383
-
384
- textLength = position.offset - startOffset;
385
- } else {
386
- textLength = 1;
387
- }
388
-
389
- position.offset -= textLength;
390
-
391
- const textProxy = new TextProxy( parent, position.offset, textLength );
392
-
393
- this.position = position;
394
-
395
- return this._formatReturnValue( 'text', textProxy, previousPosition, position, textLength );
396
- } else {
397
- // `node` is not set, we reached the beginning of current `parent`.
398
- position = Position._createBefore( parent );
399
- this.position = position;
400
-
401
- return this._formatReturnValue( 'elementStart', parent, previousPosition, position, 1 );
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
- */