@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.
- package/CHANGELOG.md +4 -4
- package/package.json +30 -24
- 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 +100 -116
- package/src/dataprocessor/htmlwriter.js +1 -18
- package/src/dataprocessor/xmldataprocessor.js +116 -137
- 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 +1370 -1617
- 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 -1018
- 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/position.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
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
|
-
*
|
|
423
|
-
* If positions are in different roots `'different'` flag is returned.
|
|
359
|
+
* Checks whether this object is of the given type.
|
|
424
360
|
*
|
|
425
|
-
*
|
|
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
|
+
};
|