@ckeditor/ckeditor5-engine 35.0.1 → 35.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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 +175 -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 +899 -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 +654 -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 +191 -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,20 +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
-
6
5
  /**
7
6
  * @module engine/view/position
8
7
  */
9
-
10
- import TreeWalker from './treewalker';
11
-
8
+ import TypeCheckable from './typecheckable';
12
9
  import compareArrays from '@ckeditor/ckeditor5-utils/src/comparearrays';
13
10
  import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
14
11
  import EditableElement from './editableelement';
15
-
16
12
  // To check if component is loaded more than once.
17
13
  import '@ckeditor/ckeditor5-utils/src/version';
18
-
14
+ import { default as TreeWalker } from './treewalker';
19
15
  /**
20
16
  * Position in the view tree. Position is represented by its parent node and an offset in this parent.
21
17
  *
@@ -25,402 +21,355 @@ import '@ckeditor/ckeditor5-utils/src/version';
25
21
  * * {@link module:engine/view/downcastwriter~DowncastWriter}
26
22
  * * {@link module:engine/view/upcastwriter~UpcastWriter}
27
23
  */
28
- export default class Position {
29
- /**
30
- * Creates a position.
31
- *
32
- * @param {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment} parent Position parent.
33
- * @param {Number} offset Position offset.
34
- */
35
- constructor( parent, offset ) {
36
- /**
37
- * Position parent.
38
- *
39
- * @readonly
40
- * @member {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
41
- * module:engine/view/position~Position#parent
42
- */
43
- this.parent = parent;
44
-
45
- /**
46
- * Position offset.
47
- *
48
- * @readonly
49
- * @member {Number} module:engine/view/position~Position#offset
50
- */
51
- this.offset = offset;
52
- }
53
-
54
- /**
55
- * Node directly after the position. Equals `null` when there is no node after position or position is located
56
- * inside text node.
57
- *
58
- * @readonly
59
- * @type {module:engine/view/node~Node|null}
60
- */
61
- get nodeAfter() {
62
- if ( this.parent.is( '$text' ) ) {
63
- return null;
64
- }
65
-
66
- return this.parent.getChild( this.offset ) || null;
67
- }
68
-
69
- /**
70
- * Node directly before the position. Equals `null` when there is no node before position or position is located
71
- * inside text node.
72
- *
73
- * @readonly
74
- * @type {module:engine/view/node~Node|null}
75
- */
76
- get nodeBefore() {
77
- if ( this.parent.is( '$text' ) ) {
78
- return null;
79
- }
80
-
81
- return this.parent.getChild( this.offset - 1 ) || null;
82
- }
83
-
84
- /**
85
- * Is `true` if position is at the beginning of its {@link module:engine/view/position~Position#parent parent}, `false` otherwise.
86
- *
87
- * @readonly
88
- * @type {Boolean}
89
- */
90
- get isAtStart() {
91
- return this.offset === 0;
92
- }
93
-
94
- /**
95
- * Is `true` if position is at the end of its {@link module:engine/view/position~Position#parent parent}, `false` otherwise.
96
- *
97
- * @readonly
98
- * @type {Boolean}
99
- */
100
- get isAtEnd() {
101
- const endOffset = this.parent.is( '$text' ) ? this.parent.data.length : this.parent.childCount;
102
-
103
- return this.offset === endOffset;
104
- }
105
-
106
- /**
107
- * Position's root, that is the root of the position's parent element.
108
- *
109
- * @readonly
110
- * @type {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
111
- */
112
- get root() {
113
- return this.parent.root;
114
- }
115
-
116
- /**
117
- * {@link module:engine/view/editableelement~EditableElement EditableElement} instance that contains this position, or `null` if
118
- * position is not inside an editable element.
119
- *
120
- * @type {module:engine/view/editableelement~EditableElement|null}
121
- */
122
- get editableElement() {
123
- let editable = this.parent;
124
-
125
- while ( !( editable instanceof EditableElement ) ) {
126
- if ( editable.parent ) {
127
- editable = editable.parent;
128
- } else {
129
- return null;
130
- }
131
- }
132
-
133
- return editable;
134
- }
135
-
136
- /**
137
- * Returns a new instance of Position with offset incremented by `shift` value.
138
- *
139
- * @param {Number} shift How position offset should get changed. Accepts negative values.
140
- * @returns {module:engine/view/position~Position} Shifted position.
141
- */
142
- getShiftedBy( shift ) {
143
- const shifted = Position._createAt( this );
144
-
145
- const offset = shifted.offset + shift;
146
- shifted.offset = offset < 0 ? 0 : offset;
147
-
148
- return shifted;
149
- }
150
-
151
- /**
152
- * Gets the farthest position which matches the callback using
153
- * {@link module:engine/view/treewalker~TreeWalker TreeWalker}.
154
- *
155
- * For example:
156
- *
157
- * getLastMatchingPosition( value => value.type == 'text' ); // <p>{}foo</p> -> <p>foo[]</p>
158
- * getLastMatchingPosition( value => value.type == 'text', { direction: 'backward' } ); // <p>foo[]</p> -> <p>{}foo</p>
159
- * getLastMatchingPosition( value => false ); // Do not move the position.
160
- *
161
- * @param {Function} skip Callback function. Gets {@link module:engine/view/treewalker~TreeWalkerValue} and should
162
- * return `true` if the value should be skipped or `false` if not.
163
- * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
164
- *
165
- * @returns {module:engine/view/position~Position} The position after the last item which matches the `skip` callback test.
166
- */
167
- getLastMatchingPosition( skip, options = {} ) {
168
- options.startPosition = this;
169
-
170
- const treeWalker = new TreeWalker( options );
171
- treeWalker.skip( skip );
172
-
173
- return treeWalker.position;
174
- }
175
-
176
- /**
177
- * Returns ancestors array of this position, that is this position's parent and it's ancestors.
178
- *
179
- * @returns {Array} Array with ancestors.
180
- */
181
- getAncestors() {
182
- if ( this.parent.is( 'documentFragment' ) ) {
183
- return [ this.parent ];
184
- } else {
185
- return this.parent.getAncestors( { includeSelf: true } );
186
- }
187
- }
188
-
189
- /**
190
- * Returns a {@link module:engine/view/node~Node} or {@link module:engine/view/documentfragment~DocumentFragment}
191
- * which is a common ancestor of both positions.
192
- *
193
- * @param {module:engine/view/position~Position} position
194
- * @returns {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment|null}
195
- */
196
- getCommonAncestor( position ) {
197
- const ancestorsA = this.getAncestors();
198
- const ancestorsB = position.getAncestors();
199
-
200
- let i = 0;
201
-
202
- while ( ancestorsA[ i ] == ancestorsB[ i ] && ancestorsA[ i ] ) {
203
- i++;
204
- }
205
-
206
- return i === 0 ? null : ancestorsA[ i - 1 ];
207
- }
208
-
209
- /**
210
- * Checks whether this object is of the given type.
211
- *
212
- * position.is( 'position' ); // -> true
213
- * position.is( 'view:position' ); // -> true
214
- *
215
- * position.is( 'model:position' ); // -> false
216
- * position.is( 'element' ); // -> false
217
- * position.is( 'range' ); // -> false
218
- *
219
- * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
220
- *
221
- * @param {String} type
222
- * @returns {Boolean}
223
- */
224
- is( type ) {
225
- return type === 'position' || type === 'view:position';
226
- }
227
-
228
- /**
229
- * Checks whether this position equals given position.
230
- *
231
- * @param {module:engine/view/position~Position} otherPosition Position to compare with.
232
- * @returns {Boolean} True if positions are same.
233
- */
234
- isEqual( otherPosition ) {
235
- return ( this.parent == otherPosition.parent && this.offset == otherPosition.offset );
236
- }
237
-
238
- /**
239
- * Checks whether this position is located before given position. When method returns `false` it does not mean that
240
- * this position is after give one. Two positions may be located inside separate roots and in that situation this
241
- * method will still return `false`.
242
- *
243
- * @see module:engine/view/position~Position#isAfter
244
- * @see module:engine/view/position~Position#compareWith
245
- * @param {module:engine/view/position~Position} otherPosition Position to compare with.
246
- * @returns {Boolean} Returns `true` if this position is before given position.
247
- */
248
- isBefore( otherPosition ) {
249
- return this.compareWith( otherPosition ) == 'before';
250
- }
251
-
252
- /**
253
- * Checks whether this position is located after given position. When method returns `false` it does not mean that
254
- * this position is before give one. Two positions may be located inside separate roots and in that situation this
255
- * method will still return `false`.
256
- *
257
- * @see module:engine/view/position~Position#isBefore
258
- * @see module:engine/view/position~Position#compareWith
259
- * @param {module:engine/view/position~Position} otherPosition Position to compare with.
260
- * @returns {Boolean} Returns `true` if this position is after given position.
261
- */
262
- isAfter( otherPosition ) {
263
- return this.compareWith( otherPosition ) == 'after';
264
- }
265
-
266
- /**
267
- * Checks whether this position is before, after or in same position that other position. Two positions may be also
268
- * different when they are located in separate roots.
269
- *
270
- * @param {module:engine/view/position~Position} otherPosition Position to compare with.
271
- * @returns {module:engine/view/position~PositionRelation}
272
- */
273
- compareWith( otherPosition ) {
274
- if ( this.root !== otherPosition.root ) {
275
- return 'different';
276
- }
277
-
278
- if ( this.isEqual( otherPosition ) ) {
279
- return 'same';
280
- }
281
-
282
- // Get path from root to position's parent element.
283
- const thisPath = this.parent.is( 'node' ) ? this.parent.getPath() : [];
284
- const otherPath = otherPosition.parent.is( 'node' ) ? otherPosition.parent.getPath() : [];
285
-
286
- // Add the positions' offsets to the parents offsets.
287
- thisPath.push( this.offset );
288
- otherPath.push( otherPosition.offset );
289
-
290
- // Compare both path arrays to find common ancestor.
291
- const result = compareArrays( thisPath, otherPath );
292
-
293
- switch ( result ) {
294
- case 'prefix':
295
- return 'before';
296
-
297
- case 'extension':
298
- return 'after';
299
-
300
- default:
301
- return thisPath[ result ] < otherPath[ result ] ? 'before' : 'after';
302
- }
303
- }
304
-
305
- /**
306
- * Creates a {@link module:engine/view/treewalker~TreeWalker TreeWalker} instance with this positions as a start position.
307
- *
308
- * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}
309
- * @param {module:engine/view/range~Range} [options.boundaries=null] Range to define boundaries of the iterator.
310
- * @param {Boolean} [options.singleCharacters=false]
311
- * @param {Boolean} [options.shallow=false]
312
- * @param {Boolean} [options.ignoreElementEnd=false]
313
- */
314
- getWalker( options = {} ) {
315
- options.startPosition = this;
316
-
317
- return new TreeWalker( options );
318
- }
319
-
320
- clone() {
321
- return new Position( this.parent, this.offset );
322
- }
323
-
324
- /**
325
- * Creates position at the given location. The location can be specified as:
326
- *
327
- * * a {@link module:engine/view/position~Position position},
328
- * * parent element and offset (offset defaults to `0`),
329
- * * parent element and `'end'` (sets position at the end of that element),
330
- * * {@link module:engine/view/item~Item view item} and `'before'` or `'after'` (sets position before or after given view item).
331
- *
332
- * This method is a shortcut to other constructors such as:
333
- *
334
- * * {@link module:engine/view/position~Position._createBefore},
335
- * * {@link module:engine/view/position~Position._createAfter}.
336
- *
337
- * @protected
338
- * @param {module:engine/view/item~Item|module:engine/view/position~Position} itemOrPosition
339
- * @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
340
- * first parameter is a {@link module:engine/view/item~Item view item}.
341
- */
342
- static _createAt( itemOrPosition, offset ) {
343
- if ( itemOrPosition instanceof Position ) {
344
- return new this( itemOrPosition.parent, itemOrPosition.offset );
345
- } else {
346
- const node = itemOrPosition;
347
-
348
- if ( offset == 'end' ) {
349
- offset = node.is( '$text' ) ? node.data.length : node.childCount;
350
- } else if ( offset == 'before' ) {
351
- return this._createBefore( node );
352
- } else if ( offset == 'after' ) {
353
- return this._createAfter( node );
354
- } else if ( offset !== 0 && !offset ) {
355
- /**
356
- * {@link module:engine/view/view~View#createPositionAt `View#createPositionAt()`}
357
- * requires the offset to be specified when the first parameter is a view item.
358
- *
359
- * @error view-createpositionat-offset-required
360
- */
361
- throw new CKEditorError( 'view-createpositionat-offset-required', node );
362
- }
363
-
364
- return new Position( node, offset );
365
- }
366
- }
367
-
368
- /**
369
- * Creates a new position after given view item.
370
- *
371
- * @protected
372
- * @param {module:engine/view/item~Item} item View item after which the position should be located.
373
- * @returns {module:engine/view/position~Position}
374
- */
375
- static _createAfter( item ) {
376
- // TextProxy is not a instance of Node so we need do handle it in specific way.
377
- if ( item.is( '$textProxy' ) ) {
378
- return new Position( item.textNode, item.offsetInText + item.data.length );
379
- }
380
-
381
- if ( !item.parent ) {
382
- /**
383
- * You can not make a position after a root.
384
- *
385
- * @error view-position-after-root
386
- * @param {module:engine/view/node~Node} root
387
- */
388
- throw new CKEditorError( 'view-position-after-root', item, { root: item } );
389
- }
390
-
391
- return new Position( item.parent, item.index + 1 );
392
- }
393
-
394
- /**
395
- * Creates a new position before given view item.
396
- *
397
- * @protected
398
- * @param {module:engine/view/item~Item} item View item before which the position should be located.
399
- * @returns {module:engine/view/position~Position}
400
- */
401
- static _createBefore( item ) {
402
- // TextProxy is not a instance of Node so we need do handle it in specific way.
403
- if ( item.is( '$textProxy' ) ) {
404
- return new Position( item.textNode, item.offsetInText );
405
- }
406
-
407
- if ( !item.parent ) {
408
- /**
409
- * You cannot make a position before a root.
410
- *
411
- * @error view-position-before-root
412
- * @param {module:engine/view/node~Node} root
413
- */
414
- throw new CKEditorError( 'view-position-before-root', item, { root: item } );
415
- }
416
-
417
- return new Position( item.parent, item.index );
418
- }
24
+ export default class Position extends TypeCheckable {
25
+ /**
26
+ * Creates a position.
27
+ *
28
+ * @param {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment} parent Position parent.
29
+ * @param {Number} offset Position offset.
30
+ */
31
+ constructor(parent, offset) {
32
+ super();
33
+ /**
34
+ * Position parent.
35
+ *
36
+ * @readonly
37
+ * @member {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
38
+ * module:engine/view/position~Position#parent
39
+ */
40
+ this.parent = parent;
41
+ /**
42
+ * Position offset.
43
+ *
44
+ * @readonly
45
+ * @member {Number} module:engine/view/position~Position#offset
46
+ */
47
+ this.offset = offset;
48
+ }
49
+ /**
50
+ * Node directly after the position. Equals `null` when there is no node after position or position is located
51
+ * inside text node.
52
+ *
53
+ * @readonly
54
+ * @type {module:engine/view/node~Node|null}
55
+ */
56
+ get nodeAfter() {
57
+ if (this.parent.is('$text')) {
58
+ return null;
59
+ }
60
+ return this.parent.getChild(this.offset) || null;
61
+ }
62
+ /**
63
+ * Node directly before the position. Equals `null` when there is no node before position or position is located
64
+ * inside text node.
65
+ *
66
+ * @readonly
67
+ * @type {module:engine/view/node~Node|null}
68
+ */
69
+ get nodeBefore() {
70
+ if (this.parent.is('$text')) {
71
+ return null;
72
+ }
73
+ return this.parent.getChild(this.offset - 1) || null;
74
+ }
75
+ /**
76
+ * Is `true` if position is at the beginning of its {@link module:engine/view/position~Position#parent parent}, `false` otherwise.
77
+ *
78
+ * @readonly
79
+ * @type {Boolean}
80
+ */
81
+ get isAtStart() {
82
+ return this.offset === 0;
83
+ }
84
+ /**
85
+ * Is `true` if position is at the end of its {@link module:engine/view/position~Position#parent parent}, `false` otherwise.
86
+ *
87
+ * @readonly
88
+ * @type {Boolean}
89
+ */
90
+ get isAtEnd() {
91
+ const endOffset = this.parent.is('$text') ? this.parent.data.length : this.parent.childCount;
92
+ return this.offset === endOffset;
93
+ }
94
+ /**
95
+ * Position's root, that is the root of the position's parent element.
96
+ *
97
+ * @readonly
98
+ * @type {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
99
+ */
100
+ get root() {
101
+ return this.parent.root;
102
+ }
103
+ /**
104
+ * {@link module:engine/view/editableelement~EditableElement EditableElement} instance that contains this position, or `null` if
105
+ * position is not inside an editable element.
106
+ *
107
+ * @type {module:engine/view/editableelement~EditableElement|null}
108
+ */
109
+ get editableElement() {
110
+ let editable = this.parent;
111
+ while (!(editable instanceof EditableElement)) {
112
+ if (editable.parent) {
113
+ editable = editable.parent;
114
+ }
115
+ else {
116
+ return null;
117
+ }
118
+ }
119
+ return editable;
120
+ }
121
+ /**
122
+ * Returns a new instance of Position with offset incremented by `shift` value.
123
+ *
124
+ * @param {Number} shift How position offset should get changed. Accepts negative values.
125
+ * @returns {module:engine/view/position~Position} Shifted position.
126
+ */
127
+ getShiftedBy(shift) {
128
+ const shifted = Position._createAt(this);
129
+ const offset = shifted.offset + shift;
130
+ shifted.offset = offset < 0 ? 0 : offset;
131
+ return shifted;
132
+ }
133
+ /**
134
+ * Gets the farthest position which matches the callback using
135
+ * {@link module:engine/view/treewalker~TreeWalker TreeWalker}.
136
+ *
137
+ * For example:
138
+ *
139
+ * getLastMatchingPosition( value => value.type == 'text' ); // <p>{}foo</p> -> <p>foo[]</p>
140
+ * getLastMatchingPosition( value => value.type == 'text', { direction: 'backward' } ); // <p>foo[]</p> -> <p>{}foo</p>
141
+ * getLastMatchingPosition( value => false ); // Do not move the position.
142
+ *
143
+ * @param {Function} skip Callback function. Gets {@link module:engine/view/treewalker~type TreeWalkerValue} and should
144
+ * return `true` if the value should be skipped or `false` if not.
145
+ * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
146
+ *
147
+ * @returns {module:engine/view/position~Position} The position after the last item which matches the `skip` callback test.
148
+ */
149
+ getLastMatchingPosition(skip, options = {}) {
150
+ options.startPosition = this;
151
+ const treeWalker = new TreeWalker(options);
152
+ treeWalker.skip(skip);
153
+ return treeWalker.position;
154
+ }
155
+ /**
156
+ * Returns ancestors array of this position, that is this position's parent and it's ancestors.
157
+ *
158
+ * @returns {Array} Array with ancestors.
159
+ */
160
+ getAncestors() {
161
+ if (this.parent.is('documentFragment')) {
162
+ return [this.parent];
163
+ }
164
+ else {
165
+ return this.parent.getAncestors({ includeSelf: true });
166
+ }
167
+ }
168
+ /**
169
+ * Returns a {@link module:engine/view/node~Node} or {@link module:engine/view/documentfragment~DocumentFragment}
170
+ * which is a common ancestor of both positions.
171
+ *
172
+ * @param {module:engine/view/position~Position} position
173
+ * @returns {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment|null}
174
+ */
175
+ getCommonAncestor(position) {
176
+ const ancestorsA = this.getAncestors();
177
+ const ancestorsB = position.getAncestors();
178
+ let i = 0;
179
+ while (ancestorsA[i] == ancestorsB[i] && ancestorsA[i]) {
180
+ i++;
181
+ }
182
+ return i === 0 ? null : ancestorsA[i - 1];
183
+ }
184
+ /**
185
+ * Checks whether this position equals given position.
186
+ *
187
+ * @param {module:engine/view/position~Position} otherPosition Position to compare with.
188
+ * @returns {Boolean} True if positions are same.
189
+ */
190
+ isEqual(otherPosition) {
191
+ return (this.parent == otherPosition.parent && this.offset == otherPosition.offset);
192
+ }
193
+ /**
194
+ * Checks whether this position is located before given position. When method returns `false` it does not mean that
195
+ * this position is after give one. Two positions may be located inside separate roots and in that situation this
196
+ * method will still return `false`.
197
+ *
198
+ * @see module:engine/view/position~Position#isAfter
199
+ * @see module:engine/view/position~Position#compareWith
200
+ * @param {module:engine/view/position~Position} otherPosition Position to compare with.
201
+ * @returns {Boolean} Returns `true` if this position is before given position.
202
+ */
203
+ isBefore(otherPosition) {
204
+ return this.compareWith(otherPosition) == 'before';
205
+ }
206
+ /**
207
+ * Checks whether this position is located after given position. When method returns `false` it does not mean that
208
+ * this position is before give one. Two positions may be located inside separate roots and in that situation this
209
+ * method will still return `false`.
210
+ *
211
+ * @see module:engine/view/position~Position#isBefore
212
+ * @see module:engine/view/position~Position#compareWith
213
+ * @param {module:engine/view/position~Position} otherPosition Position to compare with.
214
+ * @returns {Boolean} Returns `true` if this position is after given position.
215
+ */
216
+ isAfter(otherPosition) {
217
+ return this.compareWith(otherPosition) == 'after';
218
+ }
219
+ /**
220
+ * Checks whether this position is before, after or in same position that other position. Two positions may be also
221
+ * different when they are located in separate roots.
222
+ *
223
+ * @param {module:engine/view/position~Position} otherPosition Position to compare with.
224
+ * @returns {module:engine/view/position~PositionRelation}
225
+ */
226
+ compareWith(otherPosition) {
227
+ if (this.root !== otherPosition.root) {
228
+ return 'different';
229
+ }
230
+ if (this.isEqual(otherPosition)) {
231
+ return 'same';
232
+ }
233
+ // Get path from root to position's parent element.
234
+ const thisPath = this.parent.is('node') ? this.parent.getPath() : [];
235
+ const otherPath = otherPosition.parent.is('node') ? otherPosition.parent.getPath() : [];
236
+ // Add the positions' offsets to the parents offsets.
237
+ thisPath.push(this.offset);
238
+ otherPath.push(otherPosition.offset);
239
+ // Compare both path arrays to find common ancestor.
240
+ const result = compareArrays(thisPath, otherPath);
241
+ switch (result) {
242
+ case 'prefix':
243
+ return 'before';
244
+ case 'extension':
245
+ return 'after';
246
+ default:
247
+ return thisPath[result] < otherPath[result] ? 'before' : 'after';
248
+ }
249
+ }
250
+ /**
251
+ * Creates a {@link module:engine/view/treewalker~TreeWalker TreeWalker} instance with this positions as a start position.
252
+ *
253
+ * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}
254
+ * @param {module:engine/view/range~Range} [options.boundaries=null] Range to define boundaries of the iterator.
255
+ * @param {Boolean} [options.singleCharacters=false]
256
+ * @param {Boolean} [options.shallow=false]
257
+ * @param {Boolean} [options.ignoreElementEnd=false]
258
+ */
259
+ getWalker(options = {}) {
260
+ options.startPosition = this;
261
+ return new TreeWalker(options);
262
+ }
263
+ clone() {
264
+ return new Position(this.parent, this.offset);
265
+ }
266
+ /**
267
+ * Creates position at the given location. The location can be specified as:
268
+ *
269
+ * * a {@link module:engine/view/position~Position position},
270
+ * * parent element and offset (offset defaults to `0`),
271
+ * * parent element and `'end'` (sets position at the end of that element),
272
+ * * {@link module:engine/view/item~Item view item} and `'before'` or `'after'` (sets position before or after given view item).
273
+ *
274
+ * This method is a shortcut to other constructors such as:
275
+ *
276
+ * * {@link module:engine/view/position~Position._createBefore},
277
+ * * {@link module:engine/view/position~Position._createAfter}.
278
+ *
279
+ * @protected
280
+ * @param {module:engine/view/item~Item|module:engine/view/position~Position} itemOrPosition
281
+ * @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
282
+ * first parameter is a {@link module:engine/view/item~Item view item}.
283
+ */
284
+ static _createAt(itemOrPosition, offset) {
285
+ if (itemOrPosition instanceof Position) {
286
+ return new this(itemOrPosition.parent, itemOrPosition.offset);
287
+ }
288
+ else {
289
+ const node = itemOrPosition;
290
+ if (offset == 'end') {
291
+ offset = node.is('$text') ? node.data.length : node.childCount;
292
+ }
293
+ else if (offset == 'before') {
294
+ return this._createBefore(node);
295
+ }
296
+ else if (offset == 'after') {
297
+ return this._createAfter(node);
298
+ }
299
+ else if (offset !== 0 && !offset) {
300
+ /**
301
+ * {@link module:engine/view/view~View#createPositionAt `View#createPositionAt()`}
302
+ * requires the offset to be specified when the first parameter is a view item.
303
+ *
304
+ * @error view-createpositionat-offset-required
305
+ */
306
+ throw new CKEditorError('view-createpositionat-offset-required', node);
307
+ }
308
+ return new Position(node, offset);
309
+ }
310
+ }
311
+ /**
312
+ * Creates a new position after given view item.
313
+ *
314
+ * @protected
315
+ * @param {module:engine/view/item~Item} item View item after which the position should be located.
316
+ * @returns {module:engine/view/position~Position}
317
+ */
318
+ static _createAfter(item) {
319
+ // TextProxy is not a instance of Node so we need do handle it in specific way.
320
+ if (item.is('$textProxy')) {
321
+ return new Position(item.textNode, item.offsetInText + item.data.length);
322
+ }
323
+ if (!item.parent) {
324
+ /**
325
+ * You can not make a position after a root.
326
+ *
327
+ * @error view-position-after-root
328
+ * @param {module:engine/view/node~Node} root
329
+ */
330
+ throw new CKEditorError('view-position-after-root', item, { root: item });
331
+ }
332
+ return new Position(item.parent, item.index + 1);
333
+ }
334
+ /**
335
+ * Creates a new position before given view item.
336
+ *
337
+ * @protected
338
+ * @param {module:engine/view/item~Item} item View item before which the position should be located.
339
+ * @returns {module:engine/view/position~Position}
340
+ */
341
+ static _createBefore(item) {
342
+ // TextProxy is not a instance of Node so we need do handle it in specific way.
343
+ if (item.is('$textProxy')) {
344
+ return new Position(item.textNode, item.offsetInText);
345
+ }
346
+ if (!item.parent) {
347
+ /**
348
+ * You cannot make a position before a root.
349
+ *
350
+ * @error view-position-before-root
351
+ * @param {module:engine/view/node~Node} root
352
+ */
353
+ throw new CKEditorError('view-position-before-root', item, { root: item });
354
+ }
355
+ return new Position(item.parent, item.index);
356
+ }
419
357
  }
420
-
421
358
  /**
422
- * A flag indicating whether this position is `'before'` or `'after'` or `'same'` as given position.
423
- * If positions are in different roots `'different'` flag is returned.
359
+ * Checks whether this object is of the given type.
424
360
  *
425
- * @typedef {String} module:engine/view/position~PositionRelation
361
+ * position.is( 'position' ); // -> true
362
+ * position.is( 'view:position' ); // -> true
363
+ *
364
+ * position.is( 'model:position' ); // -> false
365
+ * position.is( 'element' ); // -> false
366
+ * position.is( 'range' ); // -> false
367
+ *
368
+ * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
369
+ *
370
+ * @param {String} type
371
+ * @returns {Boolean}
426
372
  */
373
+ Position.prototype.is = function (type) {
374
+ return type === 'position' || type === 'view:position';
375
+ };