@ckeditor/ckeditor5-engine 30.0.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 (117) hide show
  1. package/LICENSE.md +17 -0
  2. package/README.md +30 -0
  3. package/package.json +70 -0
  4. package/src/controller/datacontroller.js +563 -0
  5. package/src/controller/editingcontroller.js +149 -0
  6. package/src/conversion/conversion.js +644 -0
  7. package/src/conversion/conversionhelpers.js +40 -0
  8. package/src/conversion/downcastdispatcher.js +914 -0
  9. package/src/conversion/downcasthelpers.js +1706 -0
  10. package/src/conversion/mapper.js +696 -0
  11. package/src/conversion/modelconsumable.js +329 -0
  12. package/src/conversion/upcastdispatcher.js +807 -0
  13. package/src/conversion/upcasthelpers.js +997 -0
  14. package/src/conversion/viewconsumable.js +623 -0
  15. package/src/dataprocessor/basichtmlwriter.js +32 -0
  16. package/src/dataprocessor/dataprocessor.jsdoc +64 -0
  17. package/src/dataprocessor/htmldataprocessor.js +159 -0
  18. package/src/dataprocessor/htmlwriter.js +22 -0
  19. package/src/dataprocessor/xmldataprocessor.js +161 -0
  20. package/src/dev-utils/model.js +482 -0
  21. package/src/dev-utils/operationreplayer.js +140 -0
  22. package/src/dev-utils/utils.js +103 -0
  23. package/src/dev-utils/view.js +1091 -0
  24. package/src/index.js +52 -0
  25. package/src/model/batch.js +82 -0
  26. package/src/model/differ.js +1282 -0
  27. package/src/model/document.js +483 -0
  28. package/src/model/documentfragment.js +390 -0
  29. package/src/model/documentselection.js +1261 -0
  30. package/src/model/element.js +438 -0
  31. package/src/model/history.js +138 -0
  32. package/src/model/item.jsdoc +14 -0
  33. package/src/model/liveposition.js +182 -0
  34. package/src/model/liverange.js +221 -0
  35. package/src/model/markercollection.js +553 -0
  36. package/src/model/model.js +934 -0
  37. package/src/model/node.js +507 -0
  38. package/src/model/nodelist.js +217 -0
  39. package/src/model/operation/attributeoperation.js +202 -0
  40. package/src/model/operation/detachoperation.js +103 -0
  41. package/src/model/operation/insertoperation.js +188 -0
  42. package/src/model/operation/markeroperation.js +154 -0
  43. package/src/model/operation/mergeoperation.js +216 -0
  44. package/src/model/operation/moveoperation.js +209 -0
  45. package/src/model/operation/nooperation.js +58 -0
  46. package/src/model/operation/operation.js +139 -0
  47. package/src/model/operation/operationfactory.js +49 -0
  48. package/src/model/operation/renameoperation.js +155 -0
  49. package/src/model/operation/rootattributeoperation.js +211 -0
  50. package/src/model/operation/splitoperation.js +254 -0
  51. package/src/model/operation/transform.js +2389 -0
  52. package/src/model/operation/utils.js +292 -0
  53. package/src/model/position.js +1164 -0
  54. package/src/model/range.js +1049 -0
  55. package/src/model/rootelement.js +111 -0
  56. package/src/model/schema.js +1851 -0
  57. package/src/model/selection.js +902 -0
  58. package/src/model/text.js +138 -0
  59. package/src/model/textproxy.js +279 -0
  60. package/src/model/treewalker.js +414 -0
  61. package/src/model/utils/autoparagraphing.js +77 -0
  62. package/src/model/utils/deletecontent.js +528 -0
  63. package/src/model/utils/getselectedcontent.js +150 -0
  64. package/src/model/utils/insertcontent.js +824 -0
  65. package/src/model/utils/modifyselection.js +229 -0
  66. package/src/model/utils/selection-post-fixer.js +297 -0
  67. package/src/model/writer.js +1574 -0
  68. package/src/view/attributeelement.js +274 -0
  69. package/src/view/containerelement.js +123 -0
  70. package/src/view/document.js +221 -0
  71. package/src/view/documentfragment.js +273 -0
  72. package/src/view/documentselection.js +387 -0
  73. package/src/view/domconverter.js +1437 -0
  74. package/src/view/downcastwriter.js +2121 -0
  75. package/src/view/editableelement.js +118 -0
  76. package/src/view/element.js +945 -0
  77. package/src/view/elementdefinition.jsdoc +59 -0
  78. package/src/view/emptyelement.js +119 -0
  79. package/src/view/filler.js +161 -0
  80. package/src/view/item.jsdoc +14 -0
  81. package/src/view/matcher.js +776 -0
  82. package/src/view/node.js +391 -0
  83. package/src/view/observer/arrowkeysobserver.js +58 -0
  84. package/src/view/observer/bubblingemittermixin.js +307 -0
  85. package/src/view/observer/bubblingeventinfo.js +71 -0
  86. package/src/view/observer/clickobserver.js +46 -0
  87. package/src/view/observer/compositionobserver.js +79 -0
  88. package/src/view/observer/domeventdata.js +82 -0
  89. package/src/view/observer/domeventobserver.js +99 -0
  90. package/src/view/observer/fakeselectionobserver.js +118 -0
  91. package/src/view/observer/focusobserver.js +106 -0
  92. package/src/view/observer/inputobserver.js +44 -0
  93. package/src/view/observer/keyobserver.js +83 -0
  94. package/src/view/observer/mouseobserver.js +56 -0
  95. package/src/view/observer/mutationobserver.js +345 -0
  96. package/src/view/observer/observer.js +118 -0
  97. package/src/view/observer/selectionobserver.js +242 -0
  98. package/src/view/placeholder.js +285 -0
  99. package/src/view/position.js +426 -0
  100. package/src/view/range.js +533 -0
  101. package/src/view/rawelement.js +148 -0
  102. package/src/view/renderer.js +1037 -0
  103. package/src/view/rooteditableelement.js +107 -0
  104. package/src/view/selection.js +718 -0
  105. package/src/view/styles/background.js +73 -0
  106. package/src/view/styles/border.js +362 -0
  107. package/src/view/styles/margin.js +41 -0
  108. package/src/view/styles/padding.js +40 -0
  109. package/src/view/styles/utils.js +277 -0
  110. package/src/view/stylesmap.js +938 -0
  111. package/src/view/text.js +147 -0
  112. package/src/view/textproxy.js +199 -0
  113. package/src/view/treewalker.js +496 -0
  114. package/src/view/uielement.js +238 -0
  115. package/src/view/upcastwriter.js +484 -0
  116. package/src/view/view.js +721 -0
  117. package/theme/placeholder.css +27 -0
@@ -0,0 +1,273 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module engine/view/documentfragment
8
+ */
9
+
10
+ import Text from './text';
11
+ import TextProxy from './textproxy';
12
+ import mix from '@ckeditor/ckeditor5-utils/src/mix';
13
+ import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
14
+ import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
15
+
16
+ /**
17
+ * Document fragment.
18
+ *
19
+ * To create a new document fragment instance use the
20
+ * {@link module:engine/view/upcastwriter~UpcastWriter#createDocumentFragment `UpcastWriter#createDocumentFragment()`}
21
+ * method.
22
+ */
23
+ export default class DocumentFragment {
24
+ /**
25
+ * Creates new DocumentFragment instance.
26
+ *
27
+ * @protected
28
+ * @param {module:engine/view/document~Document} document The document to which this document fragment belongs.
29
+ * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
30
+ * A list of nodes to be inserted into the created document fragment.
31
+ */
32
+ constructor( document, children ) {
33
+ /**
34
+ * The document to which this document fragment belongs.
35
+ *
36
+ * @readonly
37
+ * @member {module:engine/view/document~Document}
38
+ */
39
+ this.document = document;
40
+
41
+ /**
42
+ * Array of child nodes.
43
+ *
44
+ * @protected
45
+ * @member {Array.<module:engine/view/element~Element>} module:engine/view/documentfragment~DocumentFragment#_children
46
+ */
47
+ this._children = [];
48
+
49
+ if ( children ) {
50
+ this._insertChild( 0, children );
51
+ }
52
+ }
53
+
54
+ /**
55
+ * Iterable interface.
56
+ *
57
+ * Iterates over nodes added to this document fragment.
58
+ *
59
+ * @returns {Iterable.<module:engine/view/node~Node>}
60
+ */
61
+ [ Symbol.iterator ]() {
62
+ return this._children[ Symbol.iterator ]();
63
+ }
64
+
65
+ /**
66
+ * Number of child nodes in this document fragment.
67
+ *
68
+ * @readonly
69
+ * @type {Number}
70
+ */
71
+ get childCount() {
72
+ return this._children.length;
73
+ }
74
+
75
+ /**
76
+ * Is `true` if there are no nodes inside this document fragment, `false` otherwise.
77
+ *
78
+ * @readonly
79
+ * @type {Boolean}
80
+ */
81
+ get isEmpty() {
82
+ return this.childCount === 0;
83
+ }
84
+
85
+ /**
86
+ * Artificial root of `DocumentFragment`. Returns itself. Added for compatibility reasons.
87
+ *
88
+ * @readonly
89
+ * @type {module:engine/model/documentfragment~DocumentFragment}
90
+ */
91
+ get root() {
92
+ return this;
93
+ }
94
+
95
+ /**
96
+ * Artificial parent of `DocumentFragment`. Returns `null`. Added for compatibility reasons.
97
+ *
98
+ * @readonly
99
+ * @type {null}
100
+ */
101
+ get parent() {
102
+ return null;
103
+ }
104
+
105
+ /**
106
+ * Checks whether this object is of the given type.
107
+ *
108
+ * docFrag.is( 'documentFragment' ); // -> true
109
+ * docFrag.is( 'view:documentFragment' ); // -> true
110
+ *
111
+ * docFrag.is( 'model:documentFragment' ); // -> false
112
+ * docFrag.is( 'element' ); // -> false
113
+ * docFrag.is( 'node' ); // -> false
114
+ *
115
+ * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
116
+ *
117
+ * @param {String} type
118
+ * @returns {Boolean}
119
+ */
120
+ is( type ) {
121
+ return type === 'documentFragment' || type === 'view:documentFragment';
122
+ }
123
+
124
+ /**
125
+ * {@link module:engine/view/documentfragment~DocumentFragment#_insertChild Insert} a child node or a list of child nodes at the end
126
+ * and sets the parent of these nodes to this fragment.
127
+ *
128
+ * @param {module:engine/view/item~Item|Iterable.<module:engine/view/item~Item>} items Items to be inserted.
129
+ * @returns {Number} Number of appended nodes.
130
+ */
131
+ _appendChild( items ) {
132
+ return this._insertChild( this.childCount, items );
133
+ }
134
+
135
+ /**
136
+ * Gets child at the given index.
137
+ *
138
+ * @param {Number} index Index of child.
139
+ * @returns {module:engine/view/node~Node} Child node.
140
+ */
141
+ getChild( index ) {
142
+ return this._children[ index ];
143
+ }
144
+
145
+ /**
146
+ * Gets index of the given child node. Returns `-1` if child node is not found.
147
+ *
148
+ * @param {module:engine/view/node~Node} node Child node.
149
+ * @returns {Number} Index of the child node.
150
+ */
151
+ getChildIndex( node ) {
152
+ return this._children.indexOf( node );
153
+ }
154
+
155
+ /**
156
+ * Gets child nodes iterator.
157
+ *
158
+ * @returns {Iterable.<module:engine/view/node~Node>} Child nodes iterator.
159
+ */
160
+ getChildren() {
161
+ return this._children[ Symbol.iterator ]();
162
+ }
163
+
164
+ /**
165
+ * Inserts a child node or a list of child nodes on the given index and sets the parent of these nodes to
166
+ * this fragment.
167
+ *
168
+ * @param {Number} index Position where nodes should be inserted.
169
+ * @param {module:engine/view/item~Item|Iterable.<module:engine/view/item~Item>} items Items to be inserted.
170
+ * @returns {Number} Number of inserted nodes.
171
+ */
172
+ _insertChild( index, items ) {
173
+ this._fireChange( 'children', this );
174
+ let count = 0;
175
+
176
+ const nodes = normalize( this.document, items );
177
+
178
+ for ( const node of nodes ) {
179
+ // If node that is being added to this element is already inside another element, first remove it from the old parent.
180
+ if ( node.parent !== null ) {
181
+ node._remove();
182
+ }
183
+
184
+ node.parent = this;
185
+
186
+ this._children.splice( index, 0, node );
187
+ index++;
188
+ count++;
189
+ }
190
+
191
+ return count;
192
+ }
193
+
194
+ /**
195
+ * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
196
+ *
197
+ * @param {Number} index Number of the first node to remove.
198
+ * @param {Number} [howMany=1] Number of nodes to remove.
199
+ * @returns {Array.<module:engine/view/node~Node>} The array of removed nodes.
200
+ */
201
+ _removeChildren( index, howMany = 1 ) {
202
+ this._fireChange( 'children', this );
203
+
204
+ for ( let i = index; i < index + howMany; i++ ) {
205
+ this._children[ i ].parent = null;
206
+ }
207
+
208
+ return this._children.splice( index, howMany );
209
+ }
210
+
211
+ /**
212
+ * Fires `change` event with given type of the change.
213
+ *
214
+ * @private
215
+ * @param {module:engine/view/document~ChangeType} type Type of the change.
216
+ * @param {module:engine/view/node~Node} node Changed node.
217
+ * @fires module:engine/view/node~Node#change
218
+ */
219
+ _fireChange( type, node ) {
220
+ this.fire( 'change:' + type, node );
221
+ }
222
+
223
+ // @if CK_DEBUG_ENGINE // printTree() {
224
+ // @if CK_DEBUG_ENGINE // let string = 'ViewDocumentFragment: [';
225
+
226
+ // @if CK_DEBUG_ENGINE // for ( const child of this.getChildren() ) {
227
+ // @if CK_DEBUG_ENGINE // if ( child.is( '$text' ) ) {
228
+ // @if CK_DEBUG_ENGINE // string += '\n' + '\t'.repeat( 1 ) + child.data;
229
+ // @if CK_DEBUG_ENGINE // } else {
230
+ // @if CK_DEBUG_ENGINE // string += '\n' + child.printTree( 1 );
231
+ // @if CK_DEBUG_ENGINE // }
232
+ // @if CK_DEBUG_ENGINE // }
233
+
234
+ // @if CK_DEBUG_ENGINE // string += '\n]';
235
+
236
+ // @if CK_DEBUG_ENGINE // return string;
237
+ // @if CK_DEBUG_ENGINE // }
238
+
239
+ // @if CK_DEBUG_ENGINE // logTree() {
240
+ // @if CK_DEBUG_ENGINE // console.log( this.printTree() );
241
+ // @if CK_DEBUG_ENGINE // }
242
+ }
243
+
244
+ mix( DocumentFragment, EmitterMixin );
245
+
246
+ // Converts strings to Text and non-iterables to arrays.
247
+ //
248
+ // @param {String|module:engine/view/item~Item|Iterable.<String|module:engine/view/item~Item>}
249
+ // @returns {Iterable.<module:engine/view/node~Node>}
250
+ function normalize( document, nodes ) {
251
+ // Separate condition because string is iterable.
252
+ if ( typeof nodes == 'string' ) {
253
+ return [ new Text( document, nodes ) ];
254
+ }
255
+
256
+ if ( !isIterable( nodes ) ) {
257
+ nodes = [ nodes ];
258
+ }
259
+
260
+ // Array.from to enable .map() on non-arrays.
261
+ return Array.from( nodes )
262
+ .map( node => {
263
+ if ( typeof node == 'string' ) {
264
+ return new Text( document, node );
265
+ }
266
+
267
+ if ( node instanceof TextProxy ) {
268
+ return new Text( document, node.data );
269
+ }
270
+
271
+ return node;
272
+ } );
273
+ }
@@ -0,0 +1,387 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module engine/view/documentselection
8
+ */
9
+
10
+ import Selection from './selection';
11
+ import mix from '@ckeditor/ckeditor5-utils/src/mix';
12
+ import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
13
+
14
+ /**
15
+ * Class representing the document selection in the view.
16
+ *
17
+ * Its instance is available in {@link module:engine/view/document~Document#selection `Document#selection`}.
18
+ *
19
+ * It is similar to {@link module:engine/view/selection~Selection} but
20
+ * it has a read-only API and can be modified only by the writer available in
21
+ * the {@link module:engine/view/view~View#change `View#change()`} block
22
+ * (so via {@link module:engine/view/downcastwriter~DowncastWriter#setSelection `DowncastWriter#setSelection()`}).
23
+ */
24
+ export default class DocumentSelection {
25
+ /**
26
+ * Creates new DocumentSelection instance.
27
+ *
28
+ * // Creates empty selection without ranges.
29
+ * const selection = new DocumentSelection();
30
+ *
31
+ * // Creates selection at the given range.
32
+ * const range = writer.createRange( start, end );
33
+ * const selection = new DocumentSelection( range );
34
+ *
35
+ * // Creates selection at the given ranges
36
+ * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( start2, end2 ) ];
37
+ * const selection = new DocumentSelection( ranges );
38
+ *
39
+ * // Creates selection from the other selection.
40
+ * const otherSelection = writer.createSelection();
41
+ * const selection = new DocumentSelection( otherSelection );
42
+ *
43
+ * // Creates selection at the given position.
44
+ * const position = writer.createPositionAt( root, offset );
45
+ * const selection = new DocumentSelection( position );
46
+ *
47
+ * // Creates collapsed selection at the position of given item and offset.
48
+ * const paragraph = writer.createContainerElement( 'paragraph' );
49
+ * const selection = new DocumentSelection( paragraph, offset );
50
+ *
51
+ * // Creates a range inside an {@link module:engine/view/element~Element element} which starts before the
52
+ * // first child of that element and ends after the last child of that element.
53
+ * const selection = new DocumentSelection( paragraph, 'in' );
54
+ *
55
+ * // Creates a range on an {@link module:engine/view/item~Item item} which starts before the item and ends
56
+ * // just after the item.
57
+ * const selection = new DocumentSelection( paragraph, 'on' );
58
+ *
59
+ * `Selection`'s constructor allow passing additional options (`backward`, `fake` and `label`) as the last argument.
60
+ *
61
+ * // Creates backward selection.
62
+ * const selection = new DocumentSelection( range, { backward: true } );
63
+ *
64
+ * Fake selection does not render as browser native selection over selected elements and is hidden to the user.
65
+ * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
66
+ * represented in other way, for example by applying proper CSS class.
67
+ *
68
+ * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM
69
+ * (and be properly handled by screen readers).
70
+ *
71
+ * // Creates fake selection with label.
72
+ * const selection = new DocumentSelection( range, { fake: true, label: 'foo' } );
73
+ *
74
+ * @param {module:engine/view/selection~Selectable} [selectable=null]
75
+ * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Offset or place when selectable is an `Item`.
76
+ * @param {Object} [options]
77
+ * @param {Boolean} [options.backward] Sets this selection instance to be backward.
78
+ * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
79
+ * @param {String} [options.label] Label for the fake selection.
80
+ */
81
+ constructor( selectable = null, placeOrOffset, options ) {
82
+ /**
83
+ * Selection is used internally (`DocumentSelection` is a proxy to that selection).
84
+ *
85
+ * @private
86
+ * @member {module:engine/view/selection~Selection}
87
+ */
88
+ this._selection = new Selection();
89
+
90
+ // Delegate change event to be fired on DocumentSelection instance.
91
+ this._selection.delegate( 'change' ).to( this );
92
+
93
+ // Set selection data.
94
+ this._selection.setTo( selectable, placeOrOffset, options );
95
+ }
96
+
97
+ /**
98
+ * Returns true if selection instance is marked as `fake`.
99
+ *
100
+ * @see #_setTo
101
+ * @type {Boolean}
102
+ */
103
+ get isFake() {
104
+ return this._selection.isFake;
105
+ }
106
+
107
+ /**
108
+ * Returns fake selection label.
109
+ *
110
+ * @see #_setTo
111
+ * @type {String}
112
+ */
113
+ get fakeSelectionLabel() {
114
+ return this._selection.fakeSelectionLabel;
115
+ }
116
+
117
+ /**
118
+ * Selection anchor. Anchor may be described as a position where the selection starts. Together with
119
+ * {@link #focus focus} they define the direction of selection, which is important
120
+ * when expanding/shrinking selection. Anchor is always the start or end of the most recent added range.
121
+ * It may be a bit unintuitive when there are multiple ranges in selection.
122
+ *
123
+ * @see #focus
124
+ * @type {module:engine/view/position~Position}
125
+ */
126
+ get anchor() {
127
+ return this._selection.anchor;
128
+ }
129
+
130
+ /**
131
+ * Selection focus. Focus is a position where the selection ends.
132
+ *
133
+ * @see #anchor
134
+ * @type {module:engine/view/position~Position}
135
+ */
136
+ get focus() {
137
+ return this._selection.focus;
138
+ }
139
+
140
+ /**
141
+ * Returns whether the selection is collapsed. Selection is collapsed when there is exactly one range which is
142
+ * collapsed.
143
+ *
144
+ * @type {Boolean}
145
+ */
146
+ get isCollapsed() {
147
+ return this._selection.isCollapsed;
148
+ }
149
+
150
+ /**
151
+ * Returns number of ranges in selection.
152
+ *
153
+ * @type {Number}
154
+ */
155
+ get rangeCount() {
156
+ return this._selection.rangeCount;
157
+ }
158
+
159
+ /**
160
+ * Specifies whether the {@link #focus} precedes {@link #anchor}.
161
+ *
162
+ * @type {Boolean}
163
+ */
164
+ get isBackward() {
165
+ return this._selection.isBackward;
166
+ }
167
+
168
+ /**
169
+ * {@link module:engine/view/editableelement~EditableElement EditableElement} instance that contains this selection, or `null`
170
+ * if the selection is not inside an editable element.
171
+ *
172
+ * @type {module:engine/view/editableelement~EditableElement|null}
173
+ */
174
+ get editableElement() {
175
+ return this._selection.editableElement;
176
+ }
177
+
178
+ /**
179
+ * Used for the compatibility with the {@link module:engine/view/selection~Selection#isEqual} method.
180
+ *
181
+ * @protected
182
+ */
183
+ get _ranges() {
184
+ return this._selection._ranges;
185
+ }
186
+
187
+ /**
188
+ * Returns an iterable that contains copies of all ranges added to the selection.
189
+ *
190
+ * @returns {Iterable.<module:engine/view/range~Range>}
191
+ */
192
+ * getRanges() {
193
+ yield* this._selection.getRanges();
194
+ }
195
+
196
+ /**
197
+ * Returns copy of the first range in the selection. First range is the one which
198
+ * {@link module:engine/view/range~Range#start start} position {@link module:engine/view/position~Position#isBefore is before} start
199
+ * position of all other ranges (not to confuse with the first range added to the selection).
200
+ * Returns `null` if no ranges are added to selection.
201
+ *
202
+ * @returns {module:engine/view/range~Range|null}
203
+ */
204
+ getFirstRange() {
205
+ return this._selection.getFirstRange();
206
+ }
207
+
208
+ /**
209
+ * Returns copy of the last range in the selection. Last range is the one which {@link module:engine/view/range~Range#end end}
210
+ * position {@link module:engine/view/position~Position#isAfter is after} end position of all other ranges (not to confuse
211
+ * with the last range added to the selection). Returns `null` if no ranges are added to selection.
212
+ *
213
+ * @returns {module:engine/view/range~Range|null}
214
+ */
215
+ getLastRange() {
216
+ return this._selection.getLastRange();
217
+ }
218
+
219
+ /**
220
+ * Returns copy of the first position in the selection. First position is the position that
221
+ * {@link module:engine/view/position~Position#isBefore is before} any other position in the selection ranges.
222
+ * Returns `null` if no ranges are added to selection.
223
+ *
224
+ * @returns {module:engine/view/position~Position|null}
225
+ */
226
+ getFirstPosition() {
227
+ return this._selection.getFirstPosition();
228
+ }
229
+
230
+ /**
231
+ * Returns copy of the last position in the selection. Last position is the position that
232
+ * {@link module:engine/view/position~Position#isAfter is after} any other position in the selection ranges.
233
+ * Returns `null` if no ranges are added to selection.
234
+ *
235
+ * @returns {module:engine/view/position~Position|null}
236
+ */
237
+ getLastPosition() {
238
+ return this._selection.getLastPosition();
239
+ }
240
+
241
+ /**
242
+ * Returns the selected element. {@link module:engine/view/element~Element Element} is considered as selected if there is only
243
+ * one range in the selection, and that range contains exactly one element.
244
+ * Returns `null` if there is no selected element.
245
+ *
246
+ * @returns {module:engine/view/element~Element|null}
247
+ */
248
+ getSelectedElement() {
249
+ return this._selection.getSelectedElement();
250
+ }
251
+
252
+ /**
253
+ * Checks whether, this selection is equal to given selection. Selections are equal if they have same directions,
254
+ * same number of ranges and all ranges from one selection equal to a range from other selection.
255
+ *
256
+ * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} otherSelection
257
+ * Selection to compare with.
258
+ * @returns {Boolean} `true` if selections are equal, `false` otherwise.
259
+ */
260
+ isEqual( otherSelection ) {
261
+ return this._selection.isEqual( otherSelection );
262
+ }
263
+
264
+ /**
265
+ * Checks whether this selection is similar to given selection. Selections are similar if they have same directions, same
266
+ * number of ranges, and all {@link module:engine/view/range~Range#getTrimmed trimmed} ranges from one selection are
267
+ * equal to any trimmed range from other selection.
268
+ *
269
+ * @param {module:engine/view/selection~Selection|module:engine/view/documentselection~DocumentSelection} otherSelection
270
+ * Selection to compare with.
271
+ * @returns {Boolean} `true` if selections are similar, `false` otherwise.
272
+ */
273
+ isSimilar( otherSelection ) {
274
+ return this._selection.isSimilar( otherSelection );
275
+ }
276
+
277
+ /**
278
+ * Checks whether this object is of the given type.
279
+ *
280
+ * docSelection.is( 'selection' ); // -> true
281
+ * docSelection.is( 'documentSelection' ); // -> true
282
+ * docSelection.is( 'view:selection' ); // -> true
283
+ * docSelection.is( 'view:documentSelection' ); // -> true
284
+ *
285
+ * docSelection.is( 'model:documentSelection' ); // -> false
286
+ * docSelection.is( 'element' ); // -> false
287
+ * docSelection.is( 'node' ); // -> false
288
+ *
289
+ * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
290
+ *
291
+ * @param {String} type
292
+ * @returns {Boolean}
293
+ */
294
+ is( type ) {
295
+ return type === 'selection' ||
296
+ type == 'documentSelection' ||
297
+ type == 'view:selection' ||
298
+ type == 'view:documentSelection';
299
+ }
300
+
301
+ /**
302
+ * Sets this selection's ranges and direction to the specified location based on the given
303
+ * {@link module:engine/view/selection~Selectable selectable}.
304
+ *
305
+ * // Sets selection to the given range.
306
+ * const range = writer.createRange( start, end );
307
+ * documentSelection._setTo( range );
308
+ *
309
+ * // Sets selection to given ranges.
310
+ * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( start2, end2 ) ];
311
+ * documentSelection._setTo( range );
312
+ *
313
+ * // Sets selection to the other selection.
314
+ * const otherSelection = writer.createSelection();
315
+ * documentSelection._setTo( otherSelection );
316
+ *
317
+ * // Sets collapsed selection at the given position.
318
+ * const position = writer.createPositionAt( root, offset );
319
+ * documentSelection._setTo( position );
320
+ *
321
+ * // Sets collapsed selection at the position of given item and offset.
322
+ * documentSelection._setTo( paragraph, offset );
323
+ *
324
+ * Creates a range inside an {@link module:engine/view/element~Element element} which starts before the first child of
325
+ * that element and ends after the last child of that element.
326
+ *
327
+ * documentSelection._setTo( paragraph, 'in' );
328
+ *
329
+ * Creates a range on an {@link module:engine/view/item~Item item} which starts before the item and ends just after the item.
330
+ *
331
+ * documentSelection._setTo( paragraph, 'on' );
332
+ *
333
+ * // Clears selection. Removes all ranges.
334
+ * documentSelection._setTo( null );
335
+ *
336
+ * `Selection#_setTo()` method allow passing additional options (`backward`, `fake` and `label`) as the last argument.
337
+ *
338
+ * // Sets selection as backward.
339
+ * documentSelection._setTo( range, { backward: true } );
340
+ *
341
+ * Fake selection does not render as browser native selection over selected elements and is hidden to the user.
342
+ * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
343
+ * represented in other way, for example by applying proper CSS class.
344
+ *
345
+ * Additionally fake's selection label can be provided. It will be used to des cribe fake selection in DOM
346
+ * (and be properly handled by screen readers).
347
+ *
348
+ * // Creates fake selection with label.
349
+ * documentSelection._setTo( range, { fake: true, label: 'foo' } );
350
+ *
351
+ * @protected
352
+ * @fires change
353
+ * @param {module:engine/view/selection~Selectable} selectable
354
+ * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Sets place or offset of the selection.
355
+ * @param {Object} [options]
356
+ * @param {Boolean} [options.backward] Sets this selection instance to be backward.
357
+ * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
358
+ * @param {String} [options.label] Label for the fake selection.
359
+ */
360
+ _setTo( selectable, placeOrOffset, options ) {
361
+ this._selection.setTo( selectable, placeOrOffset, options );
362
+ }
363
+
364
+ /**
365
+ * Moves {@link #focus} to the specified location.
366
+ *
367
+ * The location can be specified in the same form as {@link module:engine/view/view~View#createPositionAt view.createPositionAt()}
368
+ * parameters.
369
+ *
370
+ * @protected
371
+ * @fires change
372
+ * @param {module:engine/view/item~Item|module:engine/view/position~Position} itemOrPosition
373
+ * @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
374
+ * first parameter is a {@link module:engine/view/item~Item view item}.
375
+ */
376
+ _setFocus( itemOrPosition, offset ) {
377
+ this._selection.setFocus( itemOrPosition, offset );
378
+ }
379
+
380
+ /**
381
+ * Fired whenever selection ranges are changed through {@link ~DocumentSelection Selection API}.
382
+ *
383
+ * @event change
384
+ */
385
+ }
386
+
387
+ mix( DocumentSelection, EmitterMixin );