@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,21 +2,19 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
5
+ /* eslint-disable new-cap */
6
6
  /**
7
7
  * @module engine/view/selection
8
8
  */
9
-
10
- import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
9
+ import TypeCheckable from './typecheckable';
11
10
  import Range from './range';
12
11
  import Position from './position';
13
- import mix from '@ckeditor/ckeditor5-utils/src/mix';
14
- import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
15
12
  import Node from './node';
13
+ import DocumentSelection from './documentselection';
14
+ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
16
15
  import count from '@ckeditor/ckeditor5-utils/src/count';
17
16
  import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
18
- import DocumentSelection from './documentselection';
19
-
17
+ import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
20
18
  /**
21
19
  * Class representing an arbirtary selection in the view.
22
20
  * See also {@link module:engine/view/documentselection~DocumentSelection}.
@@ -29,690 +27,611 @@ import DocumentSelection from './documentselection';
29
27
  * A selection can consist of {@link module:engine/view/range~Range ranges} that can be set by using
30
28
  * the {@link module:engine/view/selection~Selection#setTo `Selection#setTo()`} method.
31
29
  */
32
- export default class Selection {
33
- /**
34
- * Creates new selection instance.
35
- *
36
- * **Note**: The selection constructor is available as a factory method:
37
- *
38
- * * {@link module:engine/view/view~View#createSelection `View#createSelection()`},
39
- * * {@link module:engine/view/upcastwriter~UpcastWriter#createSelection `UpcastWriter#createSelection()`}.
40
- *
41
- * // Creates empty selection without ranges.
42
- * const selection = writer.createSelection();
43
- *
44
- * // Creates selection at the given range.
45
- * const range = writer.createRange( start, end );
46
- * const selection = writer.createSelection( range );
47
- *
48
- * // Creates selection at the given ranges
49
- * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ];
50
- * const selection = writer.createSelection( ranges );
51
- *
52
- * // Creates selection from the other selection.
53
- * const otherSelection = writer.createSelection();
54
- * const selection = writer.createSelection( otherSelection );
55
- *
56
- * // Creates selection from the document selection.
57
- * const selection = writer.createSelection( editor.editing.view.document.selection );
58
- *
59
- * // Creates selection at the given position.
60
- * const position = writer.createPositionFromPath( root, path );
61
- * const selection = writer.createSelection( position );
62
- *
63
- * // Creates collapsed selection at the position of given item and offset.
64
- * const paragraph = writer.createContainerElement( 'paragraph' );
65
- * const selection = writer.createSelection( paragraph, offset );
66
- *
67
- * // Creates a range inside an {@link module:engine/view/element~Element element} which starts before the
68
- * // first child of that element and ends after the last child of that element.
69
- * const selection = writer.createSelection( paragraph, 'in' );
70
- *
71
- * // Creates a range on an {@link module:engine/view/item~Item item} which starts before the item and ends
72
- * // just after the item.
73
- * const selection = writer.createSelection( paragraph, 'on' );
74
- *
75
- * `Selection`'s constructor allow passing additional options (`backward`, `fake` and `label`) as the last argument.
76
- *
77
- * // Creates backward selection.
78
- * const selection = writer.createSelection( range, { backward: true } );
79
- *
80
- * Fake selection does not render as browser native selection over selected elements and is hidden to the user.
81
- * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
82
- * represented in other way, for example by applying proper CSS class.
83
- *
84
- * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM
85
- * (and be properly handled by screen readers).
86
- *
87
- * // Creates fake selection with label.
88
- * const selection = writer.createSelection( range, { fake: true, label: 'foo' } );
89
- *
90
- * @param {module:engine/view/selection~Selectable} [selectable=null]
91
- * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Offset or place when selectable is an `Item`.
92
- * @param {Object} [options]
93
- * @param {Boolean} [options.backward] Sets this selection instance to be backward.
94
- * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
95
- * @param {String} [options.label] Label for the fake selection.
96
- */
97
- constructor( selectable = null, placeOrOffset, options ) {
98
- /**
99
- * Stores all ranges that are selected.
100
- *
101
- * @protected
102
- * @member {Array.<module:engine/view/range~Range>}
103
- */
104
- this._ranges = [];
105
-
106
- /**
107
- * Specifies whether the last added range was added as a backward or forward range.
108
- *
109
- * @protected
110
- * @member {Boolean}
111
- */
112
- this._lastRangeBackward = false;
113
-
114
- /**
115
- * Specifies whether selection instance is fake.
116
- *
117
- * @private
118
- * @member {Boolean}
119
- */
120
- this._isFake = false;
121
-
122
- /**
123
- * Fake selection's label.
124
- *
125
- * @private
126
- * @member {String}
127
- */
128
- this._fakeSelectionLabel = '';
129
-
130
- this.setTo( selectable, placeOrOffset, options );
131
- }
132
-
133
- /**
134
- * Returns true if selection instance is marked as `fake`.
135
- *
136
- * @see #setTo
137
- * @type {Boolean}
138
- */
139
- get isFake() {
140
- return this._isFake;
141
- }
142
-
143
- /**
144
- * Returns fake selection label.
145
- *
146
- * @see #setTo
147
- * @type {String}
148
- */
149
- get fakeSelectionLabel() {
150
- return this._fakeSelectionLabel;
151
- }
152
-
153
- /**
154
- * Selection anchor. Anchor may be described as a position where the selection starts. Together with
155
- * {@link #focus focus} they define the direction of selection, which is important
156
- * when expanding/shrinking selection. Anchor is always the start or end of the most recent added range.
157
- * It may be a bit unintuitive when there are multiple ranges in selection.
158
- *
159
- * @see #focus
160
- * @type {module:engine/view/position~Position}
161
- */
162
- get anchor() {
163
- if ( !this._ranges.length ) {
164
- return null;
165
- }
166
- const range = this._ranges[ this._ranges.length - 1 ];
167
- const anchor = this._lastRangeBackward ? range.end : range.start;
168
-
169
- return anchor.clone();
170
- }
171
-
172
- /**
173
- * Selection focus. Focus is a position where the selection ends.
174
- *
175
- * @see #anchor
176
- * @type {module:engine/view/position~Position}
177
- */
178
- get focus() {
179
- if ( !this._ranges.length ) {
180
- return null;
181
- }
182
- const range = this._ranges[ this._ranges.length - 1 ];
183
- const focus = this._lastRangeBackward ? range.start : range.end;
184
-
185
- return focus.clone();
186
- }
187
-
188
- /**
189
- * Returns whether the selection is collapsed. Selection is collapsed when there is exactly one range which is
190
- * collapsed.
191
- *
192
- * @type {Boolean}
193
- */
194
- get isCollapsed() {
195
- return this.rangeCount === 1 && this._ranges[ 0 ].isCollapsed;
196
- }
197
-
198
- /**
199
- * Returns number of ranges in selection.
200
- *
201
- * @type {Number}
202
- */
203
- get rangeCount() {
204
- return this._ranges.length;
205
- }
206
-
207
- /**
208
- * Specifies whether the {@link #focus} precedes {@link #anchor}.
209
- *
210
- * @type {Boolean}
211
- */
212
- get isBackward() {
213
- return !this.isCollapsed && this._lastRangeBackward;
214
- }
215
-
216
- /**
217
- * {@link module:engine/view/editableelement~EditableElement EditableElement} instance that contains this selection, or `null`
218
- * if the selection is not inside an editable element.
219
- *
220
- * @type {module:engine/view/editableelement~EditableElement|null}
221
- */
222
- get editableElement() {
223
- if ( this.anchor ) {
224
- return this.anchor.editableElement;
225
- }
226
-
227
- return null;
228
- }
229
-
230
- /**
231
- * Returns an iterable that contains copies of all ranges added to the selection.
232
- *
233
- * @returns {Iterable.<module:engine/view/range~Range>}
234
- */
235
- * getRanges() {
236
- for ( const range of this._ranges ) {
237
- yield range.clone();
238
- }
239
- }
240
-
241
- /**
242
- * Returns copy of the first range in the selection. First range is the one which
243
- * {@link module:engine/view/range~Range#start start} position {@link module:engine/view/position~Position#isBefore is before} start
244
- * position of all other ranges (not to confuse with the first range added to the selection).
245
- * Returns `null` if no ranges are added to selection.
246
- *
247
- * @returns {module:engine/view/range~Range|null}
248
- */
249
- getFirstRange() {
250
- let first = null;
251
-
252
- for ( const range of this._ranges ) {
253
- if ( !first || range.start.isBefore( first.start ) ) {
254
- first = range;
255
- }
256
- }
257
-
258
- return first ? first.clone() : null;
259
- }
260
-
261
- /**
262
- * Returns copy of the last range in the selection. Last range is the one which {@link module:engine/view/range~Range#end end}
263
- * position {@link module:engine/view/position~Position#isAfter is after} end position of all other ranges (not to confuse
264
- * with the last range added to the selection). Returns `null` if no ranges are added to selection.
265
- *
266
- * @returns {module:engine/view/range~Range|null}
267
- */
268
- getLastRange() {
269
- let last = null;
270
-
271
- for ( const range of this._ranges ) {
272
- if ( !last || range.end.isAfter( last.end ) ) {
273
- last = range;
274
- }
275
- }
276
-
277
- return last ? last.clone() : null;
278
- }
279
-
280
- /**
281
- * Returns copy of the first position in the selection. First position is the position that
282
- * {@link module:engine/view/position~Position#isBefore is before} any other position in the selection ranges.
283
- * Returns `null` if no ranges are added to selection.
284
- *
285
- * @returns {module:engine/view/position~Position|null}
286
- */
287
- getFirstPosition() {
288
- const firstRange = this.getFirstRange();
289
-
290
- return firstRange ? firstRange.start.clone() : null;
291
- }
292
-
293
- /**
294
- * Returns copy of the last position in the selection. Last position is the position that
295
- * {@link module:engine/view/position~Position#isAfter is after} any other position in the selection ranges.
296
- * Returns `null` if no ranges are added to selection.
297
- *
298
- * @returns {module:engine/view/position~Position|null}
299
- */
300
- getLastPosition() {
301
- const lastRange = this.getLastRange();
302
-
303
- return lastRange ? lastRange.end.clone() : null;
304
- }
305
-
306
- /**
307
- * Checks whether, this selection is equal to given selection. Selections are equal if they have same directions,
308
- * same number of ranges and all ranges from one selection equal to a range from other selection.
309
- *
310
- * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} otherSelection
311
- * Selection to compare with.
312
- * @returns {Boolean} `true` if selections are equal, `false` otherwise.
313
- */
314
- isEqual( otherSelection ) {
315
- if ( this.isFake != otherSelection.isFake ) {
316
- return false;
317
- }
318
-
319
- if ( this.isFake && this.fakeSelectionLabel != otherSelection.fakeSelectionLabel ) {
320
- return false;
321
- }
322
-
323
- if ( this.rangeCount != otherSelection.rangeCount ) {
324
- return false;
325
- } else if ( this.rangeCount === 0 ) {
326
- return true;
327
- }
328
-
329
- if ( !this.anchor.isEqual( otherSelection.anchor ) || !this.focus.isEqual( otherSelection.focus ) ) {
330
- return false;
331
- }
332
-
333
- for ( const thisRange of this._ranges ) {
334
- let found = false;
335
-
336
- for ( const otherRange of otherSelection._ranges ) {
337
- if ( thisRange.isEqual( otherRange ) ) {
338
- found = true;
339
- break;
340
- }
341
- }
342
-
343
- if ( !found ) {
344
- return false;
345
- }
346
- }
347
-
348
- return true;
349
- }
350
-
351
- /**
352
- * Checks whether this selection is similar to given selection. Selections are similar if they have same directions, same
353
- * number of ranges, and all {@link module:engine/view/range~Range#getTrimmed trimmed} ranges from one selection are
354
- * equal to any trimmed range from other selection.
355
- *
356
- * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} otherSelection
357
- * Selection to compare with.
358
- * @returns {Boolean} `true` if selections are similar, `false` otherwise.
359
- */
360
- isSimilar( otherSelection ) {
361
- if ( this.isBackward != otherSelection.isBackward ) {
362
- return false;
363
- }
364
-
365
- const numOfRangesA = count( this.getRanges() );
366
- const numOfRangesB = count( otherSelection.getRanges() );
367
-
368
- // If selections have different number of ranges, they cannot be similar.
369
- if ( numOfRangesA != numOfRangesB ) {
370
- return false;
371
- }
372
-
373
- // If both selections have no ranges, they are similar.
374
- if ( numOfRangesA == 0 ) {
375
- return true;
376
- }
377
-
378
- // Check if each range in one selection has a similar range in other selection.
379
- for ( let rangeA of this.getRanges() ) {
380
- rangeA = rangeA.getTrimmed();
381
-
382
- let found = false;
383
-
384
- for ( let rangeB of otherSelection.getRanges() ) {
385
- rangeB = rangeB.getTrimmed();
386
-
387
- if ( rangeA.start.isEqual( rangeB.start ) && rangeA.end.isEqual( rangeB.end ) ) {
388
- found = true;
389
- break;
390
- }
391
- }
392
-
393
- // For `rangeA`, neither range in `otherSelection` was similar. So selections are not similar.
394
- if ( !found ) {
395
- return false;
396
- }
397
- }
398
-
399
- // There were no ranges that weren't matched. Selections are similar.
400
- return true;
401
- }
402
-
403
- /**
404
- * Returns the selected element. {@link module:engine/view/element~Element Element} is considered as selected if there is only
405
- * one range in the selection, and that range contains exactly one element.
406
- * Returns `null` if there is no selected element.
407
- *
408
- * @returns {module:engine/view/element~Element|null}
409
- */
410
- getSelectedElement() {
411
- if ( this.rangeCount !== 1 ) {
412
- return null;
413
- }
414
-
415
- return this.getFirstRange().getContainedElement();
416
- }
417
-
418
- /**
419
- * Sets this selection's ranges and direction to the specified location based on the given
420
- * {@link module:engine/view/selection~Selectable selectable}.
421
- *
422
- * // Sets selection to the given range.
423
- * const range = writer.createRange( start, end );
424
- * selection.setTo( range );
425
- *
426
- * // Sets selection to given ranges.
427
- * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ];
428
- * selection.setTo( range );
429
- *
430
- * // Sets selection to the other selection.
431
- * const otherSelection = writer.createSelection();
432
- * selection.setTo( otherSelection );
433
- *
434
- * // Sets selection to contents of DocumentSelection.
435
- * selection.setTo( editor.editing.view.document.selection );
436
- *
437
- * // Sets collapsed selection at the given position.
438
- * const position = writer.createPositionAt( root, path );
439
- * selection.setTo( position );
440
- *
441
- * // Sets collapsed selection at the position of given item and offset.
442
- * selection.setTo( paragraph, offset );
443
- *
444
- * Creates a range inside an {@link module:engine/view/element~Element element} which starts before the first child of
445
- * that element and ends after the last child of that element.
446
- *
447
- * selection.setTo( paragraph, 'in' );
448
- *
449
- * Creates a range on an {@link module:engine/view/item~Item item} which starts before the item and ends just after the item.
450
- *
451
- * selection.setTo( paragraph, 'on' );
452
- *
453
- * // Clears selection. Removes all ranges.
454
- * selection.setTo( null );
455
- *
456
- * `Selection#setTo()` method allow passing additional options (`backward`, `fake` and `label`) as the last argument.
457
- *
458
- * // Sets selection as backward.
459
- * selection.setTo( range, { backward: true } );
460
- *
461
- * Fake selection does not render as browser native selection over selected elements and is hidden to the user.
462
- * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
463
- * represented in other way, for example by applying proper CSS class.
464
- *
465
- * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM
466
- * (and be properly handled by screen readers).
467
- *
468
- * // Creates fake selection with label.
469
- * selection.setTo( range, { fake: true, label: 'foo' } );
470
- *
471
- * @fires change
472
- * @param {module:engine/view/selection~Selectable} selectable
473
- * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Sets place or offset of the selection.
474
- * @param {Object} [options]
475
- * @param {Boolean} [options.backward] Sets this selection instance to be backward.
476
- * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
477
- * @param {String} [options.label] Label for the fake selection.
478
- */
479
- setTo( selectable, placeOrOffset, options ) {
480
- if ( selectable === null ) {
481
- this._setRanges( [] );
482
- this._setFakeOptions( placeOrOffset );
483
- } else if ( selectable instanceof Selection || selectable instanceof DocumentSelection ) {
484
- this._setRanges( selectable.getRanges(), selectable.isBackward );
485
- this._setFakeOptions( { fake: selectable.isFake, label: selectable.fakeSelectionLabel } );
486
- } else if ( selectable instanceof Range ) {
487
- this._setRanges( [ selectable ], placeOrOffset && placeOrOffset.backward );
488
- this._setFakeOptions( placeOrOffset );
489
- } else if ( selectable instanceof Position ) {
490
- this._setRanges( [ new Range( selectable ) ] );
491
- this._setFakeOptions( placeOrOffset );
492
- } else if ( selectable instanceof Node ) {
493
- const backward = !!options && !!options.backward;
494
- let range;
495
-
496
- if ( placeOrOffset === undefined ) {
497
- /**
498
- * selection.setTo requires the second parameter when the first parameter is a node.
499
- *
500
- * @error view-selection-setto-required-second-parameter
501
- */
502
- throw new CKEditorError( 'view-selection-setto-required-second-parameter', this );
503
- } else if ( placeOrOffset == 'in' ) {
504
- range = Range._createIn( selectable );
505
- } else if ( placeOrOffset == 'on' ) {
506
- range = Range._createOn( selectable );
507
- } else {
508
- range = new Range( Position._createAt( selectable, placeOrOffset ) );
509
- }
510
-
511
- this._setRanges( [ range ], backward );
512
- this._setFakeOptions( options );
513
- } else if ( isIterable( selectable ) ) {
514
- // We assume that the selectable is an iterable of ranges.
515
- // Array.from() is used to prevent setting ranges to the old iterable
516
- this._setRanges( selectable, placeOrOffset && placeOrOffset.backward );
517
- this._setFakeOptions( placeOrOffset );
518
- } else {
519
- /**
520
- * Cannot set selection to given place.
521
- *
522
- * @error view-selection-setto-not-selectable
523
- */
524
- throw new CKEditorError( 'view-selection-setto-not-selectable', this );
525
- }
526
-
527
- this.fire( 'change' );
528
- }
529
-
530
- /**
531
- * Moves {@link #focus} to the specified location.
532
- *
533
- * The location can be specified in the same form as {@link module:engine/view/view~View#createPositionAt view.createPositionAt()}
534
- * parameters.
535
- *
536
- * @fires change
537
- * @param {module:engine/view/item~Item|module:engine/view/position~Position} itemOrPosition
538
- * @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
539
- * first parameter is a {@link module:engine/view/item~Item view item}.
540
- */
541
- setFocus( itemOrPosition, offset ) {
542
- if ( this.anchor === null ) {
543
- /**
544
- * Cannot set selection focus if there are no ranges in selection.
545
- *
546
- * @error view-selection-setfocus-no-ranges
547
- */
548
- throw new CKEditorError( 'view-selection-setfocus-no-ranges', this );
549
- }
550
-
551
- const newFocus = Position._createAt( itemOrPosition, offset );
552
-
553
- if ( newFocus.compareWith( this.focus ) == 'same' ) {
554
- return;
555
- }
556
-
557
- const anchor = this.anchor;
558
-
559
- this._ranges.pop();
560
-
561
- if ( newFocus.compareWith( anchor ) == 'before' ) {
562
- this._addRange( new Range( newFocus, anchor ), true );
563
- } else {
564
- this._addRange( new Range( anchor, newFocus ) );
565
- }
566
-
567
- this.fire( 'change' );
568
- }
569
-
570
- /**
571
- * Checks whether this object is of the given type.
572
- *
573
- * selection.is( 'selection' ); // -> true
574
- * selection.is( 'view:selection' ); // -> true
575
- *
576
- * selection.is( 'model:selection' ); // -> false
577
- * selection.is( 'element' ); // -> false
578
- * selection.is( 'range' ); // -> false
579
- *
580
- * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
581
- *
582
- * @param {String} type
583
- * @returns {Boolean}
584
- */
585
- is( type ) {
586
- return type === 'selection' || type === 'view:selection';
587
- }
588
-
589
- /**
590
- * Replaces all ranges that were added to the selection with given array of ranges. Last range of the array
591
- * is treated like the last added range and is used to set {@link #anchor anchor} and {@link #focus focus}.
592
- * Accepts a flag describing in which way the selection is made.
593
- *
594
- * @private
595
- * @param {Iterable.<module:engine/view/range~Range>} newRanges Iterable object of ranges to set.
596
- * @param {Boolean} [isLastBackward=false] Flag describing if last added range was selected forward - from start to end
597
- * (`false`) or backward - from end to start (`true`). Defaults to `false`.
598
- */
599
- _setRanges( newRanges, isLastBackward = false ) {
600
- // New ranges should be copied to prevent removing them by setting them to `[]` first.
601
- // Only applies to situations when selection is set to the same selection or same selection's ranges.
602
- newRanges = Array.from( newRanges );
603
-
604
- this._ranges = [];
605
-
606
- for ( const range of newRanges ) {
607
- this._addRange( range );
608
- }
609
-
610
- this._lastRangeBackward = !!isLastBackward;
611
- }
612
-
613
- /**
614
- * Sets this selection instance to be marked as `fake`. A fake selection does not render as browser native selection
615
- * over selected elements and is hidden to the user. This way, no native selection UI artifacts are displayed to
616
- * the user and selection over elements can be represented in other way, for example by applying proper CSS class.
617
- *
618
- * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM (and be
619
- * properly handled by screen readers).
620
- *
621
- * @private
622
- * @param {Object} [options] Options.
623
- * @param {Boolean} [options.fake] If set to true selection will be marked as `fake`.
624
- * @param {String} [options.label=''] Fake selection label.
625
- */
626
- _setFakeOptions( options = {} ) {
627
- this._isFake = !!options.fake;
628
- this._fakeSelectionLabel = options.fake ? options.label || '' : '';
629
- }
630
-
631
- /**
632
- * Adds a range to the selection. Added range is copied. This means that passed range is not saved in the
633
- * selection instance and you can safely operate on it.
634
- *
635
- * Accepts a flag describing in which way the selection is made - passed range might be selected from
636
- * {@link module:engine/view/range~Range#start start} to {@link module:engine/view/range~Range#end end}
637
- * or from {@link module:engine/view/range~Range#end end} to {@link module:engine/view/range~Range#start start}.
638
- * The flag is used to set {@link #anchor anchor} and {@link #focus focus} properties.
639
- *
640
- * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-selection-range-intersects` if added range intersects
641
- * with ranges already stored in Selection instance.
642
- *
643
- * @private
644
- * @fires change
645
- * @param {module:engine/view/range~Range} range
646
- * @param {Boolean} [isBackward]
647
- */
648
- _addRange( range, isBackward = false ) {
649
- if ( !( range instanceof Range ) ) {
650
- /**
651
- * Selection range set to an object that is not an instance of {@link module:engine/view/range~Range}.
652
- *
653
- * @error view-selection-add-range-not-range
654
- */
655
- throw new CKEditorError(
656
- 'view-selection-add-range-not-range',
657
- this
658
- );
659
- }
660
-
661
- this._pushRange( range );
662
- this._lastRangeBackward = !!isBackward;
663
- }
664
-
665
- /**
666
- * Adds range to selection - creates copy of given range so it can be safely used and modified.
667
- *
668
- * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-selection-range-intersects` if added range intersects
669
- * with ranges already stored in selection instance.
670
- *
671
- * @private
672
- * @param {module:engine/view/range~Range} range
673
- */
674
- _pushRange( range ) {
675
- for ( const storedRange of this._ranges ) {
676
- if ( range.isIntersecting( storedRange ) ) {
677
- /**
678
- * Trying to add a range that intersects with another range from selection.
679
- *
680
- * @error view-selection-range-intersects
681
- * @param {module:engine/view/range~Range} addedRange Range that was added to the selection.
682
- * @param {module:engine/view/range~Range} intersectingRange Range from selection that intersects with `addedRange`.
683
- */
684
- throw new CKEditorError(
685
- 'view-selection-range-intersects',
686
- this,
687
- { addedRange: range, intersectingRange: storedRange }
688
- );
689
- }
690
- }
691
-
692
- this._ranges.push( new Range( range.start, range.end ) );
693
- }
694
-
695
- /**
696
- * Fired whenever selection ranges are changed through {@link ~Selection Selection API}.
697
- *
698
- * @event change
699
- */
30
+ export default class Selection extends EmitterMixin(TypeCheckable) {
31
+ /**
32
+ * Creates new selection instance.
33
+ *
34
+ * **Note**: The selection constructor is available as a factory method:
35
+ *
36
+ * * {@link module:engine/view/view~View#createSelection `View#createSelection()`},
37
+ * * {@link module:engine/view/upcastwriter~UpcastWriter#createSelection `UpcastWriter#createSelection()`}.
38
+ *
39
+ * // Creates empty selection without ranges.
40
+ * const selection = writer.createSelection();
41
+ *
42
+ * // Creates selection at the given range.
43
+ * const range = writer.createRange( start, end );
44
+ * const selection = writer.createSelection( range );
45
+ *
46
+ * // Creates selection at the given ranges
47
+ * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ];
48
+ * const selection = writer.createSelection( ranges );
49
+ *
50
+ * // Creates selection from the other selection.
51
+ * const otherSelection = writer.createSelection();
52
+ * const selection = writer.createSelection( otherSelection );
53
+ *
54
+ * // Creates selection from the document selection.
55
+ * const selection = writer.createSelection( editor.editing.view.document.selection );
56
+ *
57
+ * // Creates selection at the given position.
58
+ * const position = writer.createPositionFromPath( root, path );
59
+ * const selection = writer.createSelection( position );
60
+ *
61
+ * // Creates collapsed selection at the position of given item and offset.
62
+ * const paragraph = writer.createContainerElement( 'paragraph' );
63
+ * const selection = writer.createSelection( paragraph, offset );
64
+ *
65
+ * // Creates a range inside an {@link module:engine/view/element~Element element} which starts before the
66
+ * // first child of that element and ends after the last child of that element.
67
+ * const selection = writer.createSelection( paragraph, 'in' );
68
+ *
69
+ * // Creates a range on an {@link module:engine/view/item~Item item} which starts before the item and ends
70
+ * // just after the item.
71
+ * const selection = writer.createSelection( paragraph, 'on' );
72
+ *
73
+ * `Selection`'s constructor allow passing additional options (`backward`, `fake` and `label`) as the last argument.
74
+ *
75
+ * // Creates backward selection.
76
+ * const selection = writer.createSelection( range, { backward: true } );
77
+ *
78
+ * Fake selection does not render as browser native selection over selected elements and is hidden to the user.
79
+ * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
80
+ * represented in other way, for example by applying proper CSS class.
81
+ *
82
+ * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM
83
+ * (and be properly handled by screen readers).
84
+ *
85
+ * // Creates fake selection with label.
86
+ * const selection = writer.createSelection( range, { fake: true, label: 'foo' } );
87
+ *
88
+ * @param {module:engine/view/selection~Selectable} [selectable=null]
89
+ * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Offset or place when selectable is an `Item`.
90
+ * @param {Object} [options]
91
+ * @param {Boolean} [options.backward] Sets this selection instance to be backward.
92
+ * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
93
+ * @param {String} [options.label] Label for the fake selection.
94
+ */
95
+ constructor(...args) {
96
+ super();
97
+ /**
98
+ * Stores all ranges that are selected.
99
+ *
100
+ * @protected
101
+ * @member {Array.<module:engine/view/range~Range>}
102
+ */
103
+ this._ranges = [];
104
+ /**
105
+ * Specifies whether the last added range was added as a backward or forward range.
106
+ *
107
+ * @protected
108
+ * @member {Boolean}
109
+ */
110
+ this._lastRangeBackward = false;
111
+ /**
112
+ * Specifies whether selection instance is fake.
113
+ *
114
+ * @private
115
+ * @member {Boolean}
116
+ */
117
+ this._isFake = false;
118
+ /**
119
+ * Fake selection's label.
120
+ *
121
+ * @private
122
+ * @member {String}
123
+ */
124
+ this._fakeSelectionLabel = '';
125
+ if (args.length) {
126
+ this.setTo(...args);
127
+ }
128
+ }
129
+ /**
130
+ * Returns true if selection instance is marked as `fake`.
131
+ *
132
+ * @see #setTo
133
+ * @type {Boolean}
134
+ */
135
+ get isFake() {
136
+ return this._isFake;
137
+ }
138
+ /**
139
+ * Returns fake selection label.
140
+ *
141
+ * @see #setTo
142
+ * @type {String}
143
+ */
144
+ get fakeSelectionLabel() {
145
+ return this._fakeSelectionLabel;
146
+ }
147
+ /**
148
+ * Selection anchor. Anchor may be described as a position where the selection starts. Together with
149
+ * {@link #focus focus} they define the direction of selection, which is important
150
+ * when expanding/shrinking selection. Anchor is always the start or end of the most recent added range.
151
+ * It may be a bit unintuitive when there are multiple ranges in selection.
152
+ *
153
+ * @see #focus
154
+ * @type {module:engine/view/position~Position}
155
+ */
156
+ get anchor() {
157
+ if (!this._ranges.length) {
158
+ return null;
159
+ }
160
+ const range = this._ranges[this._ranges.length - 1];
161
+ const anchor = this._lastRangeBackward ? range.end : range.start;
162
+ return anchor.clone();
163
+ }
164
+ /**
165
+ * Selection focus. Focus is a position where the selection ends.
166
+ *
167
+ * @see #anchor
168
+ * @type {module:engine/view/position~Position}
169
+ */
170
+ get focus() {
171
+ if (!this._ranges.length) {
172
+ return null;
173
+ }
174
+ const range = this._ranges[this._ranges.length - 1];
175
+ const focus = this._lastRangeBackward ? range.start : range.end;
176
+ return focus.clone();
177
+ }
178
+ /**
179
+ * Returns whether the selection is collapsed. Selection is collapsed when there is exactly one range which is
180
+ * collapsed.
181
+ *
182
+ * @type {Boolean}
183
+ */
184
+ get isCollapsed() {
185
+ return this.rangeCount === 1 && this._ranges[0].isCollapsed;
186
+ }
187
+ /**
188
+ * Returns number of ranges in selection.
189
+ *
190
+ * @type {Number}
191
+ */
192
+ get rangeCount() {
193
+ return this._ranges.length;
194
+ }
195
+ /**
196
+ * Specifies whether the {@link #focus} precedes {@link #anchor}.
197
+ *
198
+ * @type {Boolean}
199
+ */
200
+ get isBackward() {
201
+ return !this.isCollapsed && this._lastRangeBackward;
202
+ }
203
+ /**
204
+ * {@link module:engine/view/editableelement~EditableElement EditableElement} instance that contains this selection, or `null`
205
+ * if the selection is not inside an editable element.
206
+ *
207
+ * @type {module:engine/view/editableelement~EditableElement|null}
208
+ */
209
+ get editableElement() {
210
+ if (this.anchor) {
211
+ return this.anchor.editableElement;
212
+ }
213
+ return null;
214
+ }
215
+ /**
216
+ * Returns an iterable that contains copies of all ranges added to the selection.
217
+ *
218
+ * @returns {Iterable.<module:engine/view/range~Range>}
219
+ */
220
+ *getRanges() {
221
+ for (const range of this._ranges) {
222
+ yield range.clone();
223
+ }
224
+ }
225
+ /**
226
+ * Returns copy of the first range in the selection. First range is the one which
227
+ * {@link module:engine/view/range~Range#start start} position {@link module:engine/view/position~Position#isBefore is before} start
228
+ * position of all other ranges (not to confuse with the first range added to the selection).
229
+ * Returns `null` if no ranges are added to selection.
230
+ *
231
+ * @returns {module:engine/view/range~Range|null}
232
+ */
233
+ getFirstRange() {
234
+ let first = null;
235
+ for (const range of this._ranges) {
236
+ if (!first || range.start.isBefore(first.start)) {
237
+ first = range;
238
+ }
239
+ }
240
+ return first ? first.clone() : null;
241
+ }
242
+ /**
243
+ * Returns copy of the last range in the selection. Last range is the one which {@link module:engine/view/range~Range#end end}
244
+ * position {@link module:engine/view/position~Position#isAfter is after} end position of all other ranges (not to confuse
245
+ * with the last range added to the selection). Returns `null` if no ranges are added to selection.
246
+ *
247
+ * @returns {module:engine/view/range~Range|null}
248
+ */
249
+ getLastRange() {
250
+ let last = null;
251
+ for (const range of this._ranges) {
252
+ if (!last || range.end.isAfter(last.end)) {
253
+ last = range;
254
+ }
255
+ }
256
+ return last ? last.clone() : null;
257
+ }
258
+ /**
259
+ * Returns copy of the first position in the selection. First position is the position that
260
+ * {@link module:engine/view/position~Position#isBefore is before} any other position in the selection ranges.
261
+ * Returns `null` if no ranges are added to selection.
262
+ *
263
+ * @returns {module:engine/view/position~Position|null}
264
+ */
265
+ getFirstPosition() {
266
+ const firstRange = this.getFirstRange();
267
+ return firstRange ? firstRange.start.clone() : null;
268
+ }
269
+ /**
270
+ * Returns copy of the last position in the selection. Last position is the position that
271
+ * {@link module:engine/view/position~Position#isAfter is after} any other position in the selection ranges.
272
+ * Returns `null` if no ranges are added to selection.
273
+ *
274
+ * @returns {module:engine/view/position~Position|null}
275
+ */
276
+ getLastPosition() {
277
+ const lastRange = this.getLastRange();
278
+ return lastRange ? lastRange.end.clone() : null;
279
+ }
280
+ /**
281
+ * Checks whether, this selection is equal to given selection. Selections are equal if they have same directions,
282
+ * same number of ranges and all ranges from one selection equal to a range from other selection.
283
+ *
284
+ * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} otherSelection
285
+ * Selection to compare with.
286
+ * @returns {Boolean} `true` if selections are equal, `false` otherwise.
287
+ */
288
+ isEqual(otherSelection) {
289
+ if (this.isFake != otherSelection.isFake) {
290
+ return false;
291
+ }
292
+ if (this.isFake && this.fakeSelectionLabel != otherSelection.fakeSelectionLabel) {
293
+ return false;
294
+ }
295
+ if (this.rangeCount != otherSelection.rangeCount) {
296
+ return false;
297
+ }
298
+ else if (this.rangeCount === 0) {
299
+ return true;
300
+ }
301
+ if (!this.anchor.isEqual(otherSelection.anchor) || !this.focus.isEqual(otherSelection.focus)) {
302
+ return false;
303
+ }
304
+ for (const thisRange of this._ranges) {
305
+ let found = false;
306
+ for (const otherRange of otherSelection._ranges) {
307
+ if (thisRange.isEqual(otherRange)) {
308
+ found = true;
309
+ break;
310
+ }
311
+ }
312
+ if (!found) {
313
+ return false;
314
+ }
315
+ }
316
+ return true;
317
+ }
318
+ /**
319
+ * Checks whether this selection is similar to given selection. Selections are similar if they have same directions, same
320
+ * number of ranges, and all {@link module:engine/view/range~Range#getTrimmed trimmed} ranges from one selection are
321
+ * equal to any trimmed range from other selection.
322
+ *
323
+ * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} otherSelection
324
+ * Selection to compare with.
325
+ * @returns {Boolean} `true` if selections are similar, `false` otherwise.
326
+ */
327
+ isSimilar(otherSelection) {
328
+ if (this.isBackward != otherSelection.isBackward) {
329
+ return false;
330
+ }
331
+ const numOfRangesA = count(this.getRanges());
332
+ const numOfRangesB = count(otherSelection.getRanges());
333
+ // If selections have different number of ranges, they cannot be similar.
334
+ if (numOfRangesA != numOfRangesB) {
335
+ return false;
336
+ }
337
+ // If both selections have no ranges, they are similar.
338
+ if (numOfRangesA == 0) {
339
+ return true;
340
+ }
341
+ // Check if each range in one selection has a similar range in other selection.
342
+ for (let rangeA of this.getRanges()) {
343
+ rangeA = rangeA.getTrimmed();
344
+ let found = false;
345
+ for (let rangeB of otherSelection.getRanges()) {
346
+ rangeB = rangeB.getTrimmed();
347
+ if (rangeA.start.isEqual(rangeB.start) && rangeA.end.isEqual(rangeB.end)) {
348
+ found = true;
349
+ break;
350
+ }
351
+ }
352
+ // For `rangeA`, neither range in `otherSelection` was similar. So selections are not similar.
353
+ if (!found) {
354
+ return false;
355
+ }
356
+ }
357
+ // There were no ranges that weren't matched. Selections are similar.
358
+ return true;
359
+ }
360
+ /**
361
+ * Returns the selected element. {@link module:engine/view/element~Element Element} is considered as selected if there is only
362
+ * one range in the selection, and that range contains exactly one element.
363
+ * Returns `null` if there is no selected element.
364
+ *
365
+ * @returns {module:engine/view/element~Element|null}
366
+ */
367
+ getSelectedElement() {
368
+ if (this.rangeCount !== 1) {
369
+ return null;
370
+ }
371
+ return this.getFirstRange().getContainedElement();
372
+ }
373
+ /**
374
+ * Sets this selection's ranges and direction to the specified location based on the given
375
+ * {@link module:engine/view/selection~Selectable selectable}.
376
+ *
377
+ * // Sets selection to the given range.
378
+ * const range = writer.createRange( start, end );
379
+ * selection.setTo( range );
380
+ *
381
+ * // Sets selection to given ranges.
382
+ * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ];
383
+ * selection.setTo( range );
384
+ *
385
+ * // Sets selection to the other selection.
386
+ * const otherSelection = writer.createSelection();
387
+ * selection.setTo( otherSelection );
388
+ *
389
+ * // Sets selection to contents of DocumentSelection.
390
+ * selection.setTo( editor.editing.view.document.selection );
391
+ *
392
+ * // Sets collapsed selection at the given position.
393
+ * const position = writer.createPositionAt( root, path );
394
+ * selection.setTo( position );
395
+ *
396
+ * // Sets collapsed selection at the position of given item and offset.
397
+ * selection.setTo( paragraph, offset );
398
+ *
399
+ * Creates a range inside an {@link module:engine/view/element~Element element} which starts before the first child of
400
+ * that element and ends after the last child of that element.
401
+ *
402
+ * selection.setTo( paragraph, 'in' );
403
+ *
404
+ * Creates a range on an {@link module:engine/view/item~Item item} which starts before the item and ends just after the item.
405
+ *
406
+ * selection.setTo( paragraph, 'on' );
407
+ *
408
+ * // Clears selection. Removes all ranges.
409
+ * selection.setTo( null );
410
+ *
411
+ * `Selection#setTo()` method allow passing additional options (`backward`, `fake` and `label`) as the last argument.
412
+ *
413
+ * // Sets selection as backward.
414
+ * selection.setTo( range, { backward: true } );
415
+ *
416
+ * Fake selection does not render as browser native selection over selected elements and is hidden to the user.
417
+ * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
418
+ * represented in other way, for example by applying proper CSS class.
419
+ *
420
+ * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM
421
+ * (and be properly handled by screen readers).
422
+ *
423
+ * // Creates fake selection with label.
424
+ * selection.setTo( range, { fake: true, label: 'foo' } );
425
+ *
426
+ * @fires change
427
+ * @param {module:engine/view/selection~Selectable} selectable
428
+ * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Sets place or offset of the selection.
429
+ * @param {Object} [options]
430
+ * @param {Boolean} [options.backward] Sets this selection instance to be backward.
431
+ * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
432
+ * @param {String} [options.label] Label for the fake selection.
433
+ */
434
+ setTo(...args) {
435
+ let [selectable, placeOrOffset, options] = args;
436
+ if (typeof placeOrOffset == 'object') {
437
+ options = placeOrOffset;
438
+ placeOrOffset = undefined;
439
+ }
440
+ if (selectable === null) {
441
+ this._setRanges([]);
442
+ this._setFakeOptions(options);
443
+ }
444
+ else if (selectable instanceof Selection || selectable instanceof DocumentSelection) {
445
+ this._setRanges(selectable.getRanges(), selectable.isBackward);
446
+ this._setFakeOptions({ fake: selectable.isFake, label: selectable.fakeSelectionLabel });
447
+ }
448
+ else if (selectable instanceof Range) {
449
+ this._setRanges([selectable], options && options.backward);
450
+ this._setFakeOptions(options);
451
+ }
452
+ else if (selectable instanceof Position) {
453
+ this._setRanges([new Range(selectable)]);
454
+ this._setFakeOptions(options);
455
+ }
456
+ else if (selectable instanceof Node) {
457
+ const backward = !!options && !!options.backward;
458
+ let range;
459
+ if (placeOrOffset === undefined) {
460
+ /**
461
+ * selection.setTo requires the second parameter when the first parameter is a node.
462
+ *
463
+ * @error view-selection-setto-required-second-parameter
464
+ */
465
+ throw new CKEditorError('view-selection-setto-required-second-parameter', this);
466
+ }
467
+ else if (placeOrOffset == 'in') {
468
+ range = Range._createIn(selectable);
469
+ }
470
+ else if (placeOrOffset == 'on') {
471
+ range = Range._createOn(selectable);
472
+ }
473
+ else {
474
+ range = new Range(Position._createAt(selectable, placeOrOffset));
475
+ }
476
+ this._setRanges([range], backward);
477
+ this._setFakeOptions(options);
478
+ }
479
+ else if (isIterable(selectable)) {
480
+ // We assume that the selectable is an iterable of ranges.
481
+ // Array.from() is used to prevent setting ranges to the old iterable
482
+ this._setRanges(selectable, options && options.backward);
483
+ this._setFakeOptions(options);
484
+ }
485
+ else {
486
+ /**
487
+ * Cannot set selection to given place.
488
+ *
489
+ * @error view-selection-setto-not-selectable
490
+ */
491
+ throw new CKEditorError('view-selection-setto-not-selectable', this);
492
+ }
493
+ this.fire('change');
494
+ }
495
+ /**
496
+ * Moves {@link #focus} to the specified location.
497
+ *
498
+ * The location can be specified in the same form as {@link module:engine/view/view~View#createPositionAt view.createPositionAt()}
499
+ * parameters.
500
+ *
501
+ * @fires change
502
+ * @param {module:engine/view/item~Item|module:engine/view/position~Position} itemOrPosition
503
+ * @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
504
+ * first parameter is a {@link module:engine/view/item~Item view item}.
505
+ */
506
+ setFocus(itemOrPosition, offset) {
507
+ if (this.anchor === null) {
508
+ /**
509
+ * Cannot set selection focus if there are no ranges in selection.
510
+ *
511
+ * @error view-selection-setfocus-no-ranges
512
+ */
513
+ throw new CKEditorError('view-selection-setfocus-no-ranges', this);
514
+ }
515
+ const newFocus = Position._createAt(itemOrPosition, offset);
516
+ if (newFocus.compareWith(this.focus) == 'same') {
517
+ return;
518
+ }
519
+ const anchor = this.anchor;
520
+ this._ranges.pop();
521
+ if (newFocus.compareWith(anchor) == 'before') {
522
+ this._addRange(new Range(newFocus, anchor), true);
523
+ }
524
+ else {
525
+ this._addRange(new Range(anchor, newFocus));
526
+ }
527
+ this.fire('change');
528
+ }
529
+ /**
530
+ * Replaces all ranges that were added to the selection with given array of ranges. Last range of the array
531
+ * is treated like the last added range and is used to set {@link #anchor anchor} and {@link #focus focus}.
532
+ * Accepts a flag describing in which way the selection is made.
533
+ *
534
+ * @private
535
+ * @param {Iterable.<module:engine/view/range~Range>} newRanges Iterable object of ranges to set.
536
+ * @param {Boolean} [isLastBackward=false] Flag describing if last added range was selected forward - from start to end
537
+ * (`false`) or backward - from end to start (`true`). Defaults to `false`.
538
+ */
539
+ _setRanges(newRanges, isLastBackward = false) {
540
+ // New ranges should be copied to prevent removing them by setting them to `[]` first.
541
+ // Only applies to situations when selection is set to the same selection or same selection's ranges.
542
+ newRanges = Array.from(newRanges);
543
+ this._ranges = [];
544
+ for (const range of newRanges) {
545
+ this._addRange(range);
546
+ }
547
+ this._lastRangeBackward = !!isLastBackward;
548
+ }
549
+ /**
550
+ * Sets this selection instance to be marked as `fake`. A fake selection does not render as browser native selection
551
+ * over selected elements and is hidden to the user. This way, no native selection UI artifacts are displayed to
552
+ * the user and selection over elements can be represented in other way, for example by applying proper CSS class.
553
+ *
554
+ * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM (and be
555
+ * properly handled by screen readers).
556
+ *
557
+ * @private
558
+ * @param {Object} [options] Options.
559
+ * @param {Boolean} [options.fake] If set to true selection will be marked as `fake`.
560
+ * @param {String} [options.label=''] Fake selection label.
561
+ */
562
+ _setFakeOptions(options = {}) {
563
+ this._isFake = !!options.fake;
564
+ this._fakeSelectionLabel = options.fake ? options.label || '' : '';
565
+ }
566
+ /**
567
+ * Adds a range to the selection. Added range is copied. This means that passed range is not saved in the
568
+ * selection instance and you can safely operate on it.
569
+ *
570
+ * Accepts a flag describing in which way the selection is made - passed range might be selected from
571
+ * {@link module:engine/view/range~Range#start start} to {@link module:engine/view/range~Range#end end}
572
+ * or from {@link module:engine/view/range~Range#end end} to {@link module:engine/view/range~Range#start start}.
573
+ * The flag is used to set {@link #anchor anchor} and {@link #focus focus} properties.
574
+ *
575
+ * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-selection-range-intersects` if added range intersects
576
+ * with ranges already stored in Selection instance.
577
+ *
578
+ * @private
579
+ * @fires change
580
+ * @param {module:engine/view/range~Range} range
581
+ * @param {Boolean} [isBackward]
582
+ */
583
+ _addRange(range, isBackward = false) {
584
+ if (!(range instanceof Range)) {
585
+ /**
586
+ * Selection range set to an object that is not an instance of {@link module:engine/view/range~Range}.
587
+ *
588
+ * @error view-selection-add-range-not-range
589
+ */
590
+ throw new CKEditorError('view-selection-add-range-not-range', this);
591
+ }
592
+ this._pushRange(range);
593
+ this._lastRangeBackward = !!isBackward;
594
+ }
595
+ /**
596
+ * Adds range to selection - creates copy of given range so it can be safely used and modified.
597
+ *
598
+ * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `view-selection-range-intersects` if added range intersects
599
+ * with ranges already stored in selection instance.
600
+ *
601
+ * @private
602
+ * @param {module:engine/view/range~Range} range
603
+ */
604
+ _pushRange(range) {
605
+ for (const storedRange of this._ranges) {
606
+ if (range.isIntersecting(storedRange)) {
607
+ /**
608
+ * Trying to add a range that intersects with another range from selection.
609
+ *
610
+ * @error view-selection-range-intersects
611
+ * @param {module:engine/view/range~Range} addedRange Range that was added to the selection.
612
+ * @param {module:engine/view/range~Range} intersectingRange Range from selection that intersects with `addedRange`.
613
+ */
614
+ throw new CKEditorError('view-selection-range-intersects', this, { addedRange: range, intersectingRange: storedRange });
615
+ }
616
+ }
617
+ this._ranges.push(new Range(range.start, range.end));
618
+ }
700
619
  }
701
-
702
- mix( Selection, EmitterMixin );
703
-
704
620
  /**
705
- * An entity that is used to set selection.
621
+ * Checks whether this object is of the given type.
622
+ *
623
+ * selection.is( 'selection' ); // -> true
624
+ * selection.is( 'view:selection' ); // -> true
625
+ *
626
+ * selection.is( 'model:selection' ); // -> false
627
+ * selection.is( 'element' ); // -> false
628
+ * selection.is( 'range' ); // -> false
706
629
  *
707
- * See also {@link module:engine/view/selection~Selection#setTo}
630
+ * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
708
631
  *
709
- * @typedef {
710
- * module:engine/view/selection~Selection|
711
- * module:engine/view/documentselection~DocumentSelection|
712
- * module:engine/view/position~Position|
713
- * Iterable.<module:engine/view/range~Range>|
714
- * module:engine/view/range~Range|
715
- * module:engine/view/item~Item|
716
- * null
717
- * } module:engine/view/selection~Selectable
632
+ * @param {String} type
633
+ * @returns {Boolean}
718
634
  */
635
+ Selection.prototype.is = function (type) {
636
+ return type === 'selection' || type === 'view:selection';
637
+ };