@ckeditor/ckeditor5-engine 47.6.1 → 48.0.0-alpha.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 (258) hide show
  1. package/LICENSE.md +1 -1
  2. package/{src → dist}/engineconfig.d.ts +6 -15
  3. package/dist/index-editor.css +38 -15
  4. package/dist/index.css +37 -37
  5. package/dist/index.css.map +1 -1
  6. package/{src → dist}/index.d.ts +0 -1
  7. package/dist/index.js +588 -94
  8. package/dist/index.js.map +1 -1
  9. package/{src → dist}/model/model.d.ts +10 -4
  10. package/{src → dist}/model/selection.d.ts +1 -1
  11. package/{src → dist}/view/downcastwriter.d.ts +3 -2
  12. package/{src → dist}/view/element.d.ts +2 -2
  13. package/{src → dist}/view/matcher.d.ts +4 -2
  14. package/dist/view/styles/background.d.ts +18 -0
  15. package/{src → dist}/view/styles/border.d.ts +0 -12
  16. package/{src → dist}/view/styles/margin.d.ts +0 -13
  17. package/{src → dist}/view/styles/padding.d.ts +0 -13
  18. package/{src → dist}/view/styles/utils.d.ts +12 -0
  19. package/package.json +20 -39
  20. package/src/controller/datacontroller.js +0 -522
  21. package/src/controller/editingcontroller.js +0 -181
  22. package/src/conversion/conversion.js +0 -606
  23. package/src/conversion/conversionhelpers.js +0 -33
  24. package/src/conversion/downcastdispatcher.js +0 -563
  25. package/src/conversion/downcasthelpers.js +0 -2160
  26. package/src/conversion/mapper.js +0 -1050
  27. package/src/conversion/modelconsumable.js +0 -331
  28. package/src/conversion/upcastdispatcher.js +0 -470
  29. package/src/conversion/upcasthelpers.js +0 -952
  30. package/src/conversion/viewconsumable.js +0 -541
  31. package/src/dataprocessor/basichtmlwriter.js +0 -22
  32. package/src/dataprocessor/dataprocessor.js +0 -5
  33. package/src/dataprocessor/htmldataprocessor.js +0 -107
  34. package/src/dataprocessor/htmlwriter.js +0 -5
  35. package/src/dataprocessor/xmldataprocessor.js +0 -127
  36. package/src/dev-utils/model.js +0 -396
  37. package/src/dev-utils/operationreplayer.js +0 -116
  38. package/src/dev-utils/utils.js +0 -122
  39. package/src/dev-utils/view.js +0 -990
  40. package/src/engineconfig.js +0 -5
  41. package/src/index.js +0 -134
  42. package/src/legacyerrors.js +0 -17
  43. package/src/model/batch.js +0 -98
  44. package/src/model/differ.js +0 -1288
  45. package/src/model/document.js +0 -398
  46. package/src/model/documentfragment.js +0 -332
  47. package/src/model/documentselection.js +0 -1026
  48. package/src/model/element.js +0 -323
  49. package/src/model/history.js +0 -206
  50. package/src/model/item.js +0 -5
  51. package/src/model/liveposition.js +0 -93
  52. package/src/model/liverange.js +0 -121
  53. package/src/model/markercollection.js +0 -436
  54. package/src/model/model.js +0 -866
  55. package/src/model/node.js +0 -371
  56. package/src/model/nodelist.js +0 -244
  57. package/src/model/operation/attributeoperation.js +0 -172
  58. package/src/model/operation/detachoperation.js +0 -87
  59. package/src/model/operation/insertoperation.js +0 -153
  60. package/src/model/operation/markeroperation.js +0 -136
  61. package/src/model/operation/mergeoperation.js +0 -184
  62. package/src/model/operation/moveoperation.js +0 -179
  63. package/src/model/operation/nooperation.js +0 -48
  64. package/src/model/operation/operation.js +0 -78
  65. package/src/model/operation/operationfactory.js +0 -44
  66. package/src/model/operation/renameoperation.js +0 -128
  67. package/src/model/operation/rootattributeoperation.js +0 -173
  68. package/src/model/operation/rootoperation.js +0 -106
  69. package/src/model/operation/splitoperation.js +0 -214
  70. package/src/model/operation/transform.js +0 -2211
  71. package/src/model/operation/utils.js +0 -217
  72. package/src/model/position.js +0 -1041
  73. package/src/model/range.js +0 -880
  74. package/src/model/rootelement.js +0 -82
  75. package/src/model/schema.js +0 -1542
  76. package/src/model/selection.js +0 -814
  77. package/src/model/text.js +0 -92
  78. package/src/model/textproxy.js +0 -202
  79. package/src/model/treewalker.js +0 -313
  80. package/src/model/typecheckable.js +0 -16
  81. package/src/model/utils/autoparagraphing.js +0 -63
  82. package/src/model/utils/deletecontent.js +0 -509
  83. package/src/model/utils/getselectedcontent.js +0 -126
  84. package/src/model/utils/insertcontent.js +0 -750
  85. package/src/model/utils/insertobject.js +0 -135
  86. package/src/model/utils/modifyselection.js +0 -187
  87. package/src/model/utils/selection-post-fixer.js +0 -264
  88. package/src/model/writer.js +0 -1318
  89. package/src/view/attributeelement.js +0 -220
  90. package/src/view/containerelement.js +0 -91
  91. package/src/view/datatransfer.js +0 -106
  92. package/src/view/document.js +0 -139
  93. package/src/view/documentfragment.js +0 -251
  94. package/src/view/documentselection.js +0 -270
  95. package/src/view/domconverter.js +0 -1661
  96. package/src/view/downcastwriter.js +0 -1589
  97. package/src/view/editableelement.js +0 -74
  98. package/src/view/element.js +0 -1053
  99. package/src/view/elementdefinition.js +0 -5
  100. package/src/view/emptyelement.js +0 -83
  101. package/src/view/filler.js +0 -161
  102. package/src/view/item.js +0 -5
  103. package/src/view/matcher.js +0 -437
  104. package/src/view/node.js +0 -238
  105. package/src/view/observer/arrowkeysobserver.js +0 -40
  106. package/src/view/observer/bubblingemittermixin.js +0 -215
  107. package/src/view/observer/bubblingeventinfo.js +0 -49
  108. package/src/view/observer/clickobserver.js +0 -26
  109. package/src/view/observer/compositionobserver.js +0 -64
  110. package/src/view/observer/domeventdata.js +0 -63
  111. package/src/view/observer/domeventobserver.js +0 -81
  112. package/src/view/observer/fakeselectionobserver.js +0 -95
  113. package/src/view/observer/focusobserver.js +0 -166
  114. package/src/view/observer/inputobserver.js +0 -236
  115. package/src/view/observer/keyobserver.js +0 -36
  116. package/src/view/observer/mouseobserver.js +0 -26
  117. package/src/view/observer/mutationobserver.js +0 -219
  118. package/src/view/observer/observer.js +0 -92
  119. package/src/view/observer/pointerobserver.js +0 -26
  120. package/src/view/observer/selectionobserver.js +0 -318
  121. package/src/view/observer/tabobserver.js +0 -42
  122. package/src/view/observer/touchobserver.js +0 -26
  123. package/src/view/placeholder.js +0 -285
  124. package/src/view/position.js +0 -341
  125. package/src/view/range.js +0 -451
  126. package/src/view/rawelement.js +0 -115
  127. package/src/view/renderer.js +0 -1148
  128. package/src/view/rooteditableelement.js +0 -78
  129. package/src/view/selection.js +0 -594
  130. package/src/view/styles/background.d.ts +0 -33
  131. package/src/view/styles/background.js +0 -74
  132. package/src/view/styles/border.js +0 -316
  133. package/src/view/styles/margin.js +0 -34
  134. package/src/view/styles/padding.js +0 -34
  135. package/src/view/styles/utils.js +0 -219
  136. package/src/view/stylesmap.js +0 -941
  137. package/src/view/text.js +0 -110
  138. package/src/view/textproxy.js +0 -136
  139. package/src/view/tokenlist.js +0 -194
  140. package/src/view/treewalker.js +0 -389
  141. package/src/view/typecheckable.js +0 -19
  142. package/src/view/uielement.js +0 -194
  143. package/src/view/upcastwriter.js +0 -363
  144. package/src/view/view.js +0 -579
  145. package/theme/placeholder.css +0 -36
  146. package/theme/renderer.css +0 -9
  147. /package/{src → dist}/controller/datacontroller.d.ts +0 -0
  148. /package/{src → dist}/controller/editingcontroller.d.ts +0 -0
  149. /package/{src → dist}/conversion/conversion.d.ts +0 -0
  150. /package/{src → dist}/conversion/conversionhelpers.d.ts +0 -0
  151. /package/{src → dist}/conversion/downcastdispatcher.d.ts +0 -0
  152. /package/{src → dist}/conversion/downcasthelpers.d.ts +0 -0
  153. /package/{src → dist}/conversion/mapper.d.ts +0 -0
  154. /package/{src → dist}/conversion/modelconsumable.d.ts +0 -0
  155. /package/{src → dist}/conversion/upcastdispatcher.d.ts +0 -0
  156. /package/{src → dist}/conversion/upcasthelpers.d.ts +0 -0
  157. /package/{src → dist}/conversion/viewconsumable.d.ts +0 -0
  158. /package/{src → dist}/dataprocessor/basichtmlwriter.d.ts +0 -0
  159. /package/{src → dist}/dataprocessor/dataprocessor.d.ts +0 -0
  160. /package/{src → dist}/dataprocessor/htmldataprocessor.d.ts +0 -0
  161. /package/{src → dist}/dataprocessor/htmlwriter.d.ts +0 -0
  162. /package/{src → dist}/dataprocessor/xmldataprocessor.d.ts +0 -0
  163. /package/{src → dist}/dev-utils/model.d.ts +0 -0
  164. /package/{src → dist}/dev-utils/operationreplayer.d.ts +0 -0
  165. /package/{src → dist}/dev-utils/utils.d.ts +0 -0
  166. /package/{src → dist}/dev-utils/view.d.ts +0 -0
  167. /package/{src → dist}/legacyerrors.d.ts +0 -0
  168. /package/{src → dist}/model/batch.d.ts +0 -0
  169. /package/{src → dist}/model/differ.d.ts +0 -0
  170. /package/{src → dist}/model/document.d.ts +0 -0
  171. /package/{src → dist}/model/documentfragment.d.ts +0 -0
  172. /package/{src → dist}/model/documentselection.d.ts +0 -0
  173. /package/{src → dist}/model/element.d.ts +0 -0
  174. /package/{src → dist}/model/history.d.ts +0 -0
  175. /package/{src → dist}/model/item.d.ts +0 -0
  176. /package/{src → dist}/model/liveposition.d.ts +0 -0
  177. /package/{src → dist}/model/liverange.d.ts +0 -0
  178. /package/{src → dist}/model/markercollection.d.ts +0 -0
  179. /package/{src → dist}/model/node.d.ts +0 -0
  180. /package/{src → dist}/model/nodelist.d.ts +0 -0
  181. /package/{src → dist}/model/operation/attributeoperation.d.ts +0 -0
  182. /package/{src → dist}/model/operation/detachoperation.d.ts +0 -0
  183. /package/{src → dist}/model/operation/insertoperation.d.ts +0 -0
  184. /package/{src → dist}/model/operation/markeroperation.d.ts +0 -0
  185. /package/{src → dist}/model/operation/mergeoperation.d.ts +0 -0
  186. /package/{src → dist}/model/operation/moveoperation.d.ts +0 -0
  187. /package/{src → dist}/model/operation/nooperation.d.ts +0 -0
  188. /package/{src → dist}/model/operation/operation.d.ts +0 -0
  189. /package/{src → dist}/model/operation/operationfactory.d.ts +0 -0
  190. /package/{src → dist}/model/operation/renameoperation.d.ts +0 -0
  191. /package/{src → dist}/model/operation/rootattributeoperation.d.ts +0 -0
  192. /package/{src → dist}/model/operation/rootoperation.d.ts +0 -0
  193. /package/{src → dist}/model/operation/splitoperation.d.ts +0 -0
  194. /package/{src → dist}/model/operation/transform.d.ts +0 -0
  195. /package/{src → dist}/model/operation/utils.d.ts +0 -0
  196. /package/{src → dist}/model/position.d.ts +0 -0
  197. /package/{src → dist}/model/range.d.ts +0 -0
  198. /package/{src → dist}/model/rootelement.d.ts +0 -0
  199. /package/{src → dist}/model/schema.d.ts +0 -0
  200. /package/{src → dist}/model/text.d.ts +0 -0
  201. /package/{src → dist}/model/textproxy.d.ts +0 -0
  202. /package/{src → dist}/model/treewalker.d.ts +0 -0
  203. /package/{src → dist}/model/typecheckable.d.ts +0 -0
  204. /package/{src → dist}/model/utils/autoparagraphing.d.ts +0 -0
  205. /package/{src → dist}/model/utils/deletecontent.d.ts +0 -0
  206. /package/{src → dist}/model/utils/getselectedcontent.d.ts +0 -0
  207. /package/{src → dist}/model/utils/insertcontent.d.ts +0 -0
  208. /package/{src → dist}/model/utils/insertobject.d.ts +0 -0
  209. /package/{src → dist}/model/utils/modifyselection.d.ts +0 -0
  210. /package/{src → dist}/model/utils/selection-post-fixer.d.ts +0 -0
  211. /package/{src → dist}/model/writer.d.ts +0 -0
  212. /package/{src → dist}/view/attributeelement.d.ts +0 -0
  213. /package/{src → dist}/view/containerelement.d.ts +0 -0
  214. /package/{src → dist}/view/datatransfer.d.ts +0 -0
  215. /package/{src → dist}/view/document.d.ts +0 -0
  216. /package/{src → dist}/view/documentfragment.d.ts +0 -0
  217. /package/{src → dist}/view/documentselection.d.ts +0 -0
  218. /package/{src → dist}/view/domconverter.d.ts +0 -0
  219. /package/{src → dist}/view/editableelement.d.ts +0 -0
  220. /package/{src → dist}/view/elementdefinition.d.ts +0 -0
  221. /package/{src → dist}/view/emptyelement.d.ts +0 -0
  222. /package/{src → dist}/view/filler.d.ts +0 -0
  223. /package/{src → dist}/view/item.d.ts +0 -0
  224. /package/{src → dist}/view/node.d.ts +0 -0
  225. /package/{src → dist}/view/observer/arrowkeysobserver.d.ts +0 -0
  226. /package/{src → dist}/view/observer/bubblingemittermixin.d.ts +0 -0
  227. /package/{src → dist}/view/observer/bubblingeventinfo.d.ts +0 -0
  228. /package/{src → dist}/view/observer/clickobserver.d.ts +0 -0
  229. /package/{src → dist}/view/observer/compositionobserver.d.ts +0 -0
  230. /package/{src → dist}/view/observer/domeventdata.d.ts +0 -0
  231. /package/{src → dist}/view/observer/domeventobserver.d.ts +0 -0
  232. /package/{src → dist}/view/observer/fakeselectionobserver.d.ts +0 -0
  233. /package/{src → dist}/view/observer/focusobserver.d.ts +0 -0
  234. /package/{src → dist}/view/observer/inputobserver.d.ts +0 -0
  235. /package/{src → dist}/view/observer/keyobserver.d.ts +0 -0
  236. /package/{src → dist}/view/observer/mouseobserver.d.ts +0 -0
  237. /package/{src → dist}/view/observer/mutationobserver.d.ts +0 -0
  238. /package/{src → dist}/view/observer/observer.d.ts +0 -0
  239. /package/{src → dist}/view/observer/pointerobserver.d.ts +0 -0
  240. /package/{src → dist}/view/observer/selectionobserver.d.ts +0 -0
  241. /package/{src → dist}/view/observer/tabobserver.d.ts +0 -0
  242. /package/{src → dist}/view/observer/touchobserver.d.ts +0 -0
  243. /package/{src → dist}/view/placeholder.d.ts +0 -0
  244. /package/{src → dist}/view/position.d.ts +0 -0
  245. /package/{src → dist}/view/range.d.ts +0 -0
  246. /package/{src → dist}/view/rawelement.d.ts +0 -0
  247. /package/{src → dist}/view/renderer.d.ts +0 -0
  248. /package/{src → dist}/view/rooteditableelement.d.ts +0 -0
  249. /package/{src → dist}/view/selection.d.ts +0 -0
  250. /package/{src → dist}/view/stylesmap.d.ts +0 -0
  251. /package/{src → dist}/view/text.d.ts +0 -0
  252. /package/{src → dist}/view/textproxy.d.ts +0 -0
  253. /package/{src → dist}/view/tokenlist.d.ts +0 -0
  254. /package/{src → dist}/view/treewalker.d.ts +0 -0
  255. /package/{src → dist}/view/typecheckable.d.ts +0 -0
  256. /package/{src → dist}/view/uielement.d.ts +0 -0
  257. /package/{src → dist}/view/upcastwriter.d.ts +0 -0
  258. /package/{src → dist}/view/view.d.ts +0 -0
@@ -1,814 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module engine/model/selection
7
- */
8
- import { ModelTypeCheckable } from './typecheckable.js';
9
- import { ModelNode } from './node.js';
10
- import { ModelPosition } from './position.js';
11
- import { ModelRange } from './range.js';
12
- import { CKEditorError, EmitterMixin, isIterable } from '@ckeditor/ckeditor5-utils';
13
- /**
14
- * Selection is a set of {@link module:engine/model/range~ModelRange ranges}. It has a direction specified by its
15
- * {@link module:engine/model/selection~ModelSelection#anchor anchor} and {@link module:engine/model/selection~ModelSelection#focus focus}
16
- * (it can be {@link module:engine/model/selection~ModelSelection#isBackward forward or backward}).
17
- * Additionally, selection may have its own attributes (think – whether text typed in in this selection
18
- * should have those attributes – e.g. whether you type a bolded text).
19
- */
20
- export class ModelSelection extends /* #__PURE__ */ EmitterMixin(ModelTypeCheckable) {
21
- /**
22
- * Specifies whether the last added range was added as a backward or forward range.
23
- */
24
- _lastRangeBackward = false;
25
- /**
26
- * List of attributes set on current selection.
27
- */
28
- _attrs = new Map();
29
- /** @internal */
30
- _ranges = [];
31
- /**
32
- * Creates a new selection instance based on the given {@link module:engine/model/selection~ModelSelectable selectable}
33
- * or creates an empty selection if no arguments were passed.
34
- *
35
- * ```ts
36
- * // Creates empty selection without ranges.
37
- * const selection = writer.createSelection();
38
- *
39
- * // Creates selection at the given range.
40
- * const range = writer.createRange( start, end );
41
- * const selection = writer.createSelection( range );
42
- *
43
- * // Creates selection at the given ranges
44
- * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ];
45
- * const selection = writer.createSelection( ranges );
46
- *
47
- * // Creates selection from the other selection.
48
- * // Note: It doesn't copy selection attributes.
49
- * const otherSelection = writer.createSelection();
50
- * const selection = writer.createSelection( otherSelection );
51
- *
52
- * // Creates selection from the given document selection.
53
- * // Note: It doesn't copy selection attributes.
54
- * const documentSelection = model.document.selection;
55
- * const selection = writer.createSelection( documentSelection );
56
- *
57
- * // Creates selection at the given position.
58
- * const position = writer.createPositionFromPath( root, path );
59
- * const selection = writer.createSelection( position );
60
- *
61
- * // Creates selection at the given offset in the given element.
62
- * const paragraph = writer.createElement( 'paragraph' );
63
- * const selection = writer.createSelection( paragraph, offset );
64
- *
65
- * // Creates a range inside an {@link module:engine/model/element~ModelElement 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/model/item~ModelItem item} which starts before the item and ends
70
- * // just after the item.
71
- * const selection = writer.createSelection( paragraph, 'on' );
72
- * ```
73
- *
74
- * Selection's constructor allow passing additional options (`'backward'`) as the last argument.
75
- *
76
- * ```ts
77
- * // Creates backward selection.
78
- * const selection = writer.createSelection( range, { backward: true } );
79
- * ```
80
- *
81
- * @internal
82
- */
83
- constructor(...args) {
84
- super();
85
- if (args.length) {
86
- this.setTo(...args);
87
- }
88
- }
89
- /**
90
- * Selection anchor. Anchor is the position from which the selection was started. If a user is making a selection
91
- * by dragging the mouse, the anchor is where the user pressed the mouse button (the beginning of the selection).
92
- *
93
- * Anchor and {@link #focus} define the direction of the selection, which is important
94
- * when expanding/shrinking selection. The focus moves, while the anchor should remain in the same place.
95
- *
96
- * Anchor is always set to the {@link module:engine/model/range~ModelRange#start start} or
97
- * {@link module:engine/model/range~ModelRange#end end} position of the last of selection's ranges. Whether it is
98
- * the `start` or `end` depends on the specified `options.backward`. See the {@link #setTo `setTo()`} method.
99
- *
100
- * May be set to `null` if there are no ranges in the selection.
101
- *
102
- * @see #focus
103
- */
104
- get anchor() {
105
- if (this._ranges.length > 0) {
106
- const range = this._ranges[this._ranges.length - 1];
107
- return this._lastRangeBackward ? range.end : range.start;
108
- }
109
- return null;
110
- }
111
- /**
112
- * Selection focus. Focus is the position where the selection ends. If a user is making a selection
113
- * by dragging the mouse, the focus is where the mouse cursor is.
114
- *
115
- * May be set to `null` if there are no ranges in the selection.
116
- *
117
- * @see #anchor
118
- */
119
- get focus() {
120
- if (this._ranges.length > 0) {
121
- const range = this._ranges[this._ranges.length - 1];
122
- return this._lastRangeBackward ? range.start : range.end;
123
- }
124
- return null;
125
- }
126
- /**
127
- * Whether the selection is collapsed. Selection is collapsed when there is exactly one range in it
128
- * and it is collapsed.
129
- */
130
- get isCollapsed() {
131
- const length = this._ranges.length;
132
- if (length === 1) {
133
- return this._ranges[0].isCollapsed;
134
- }
135
- else {
136
- return false;
137
- }
138
- }
139
- /**
140
- * Returns the number of ranges in the selection.
141
- */
142
- get rangeCount() {
143
- return this._ranges.length;
144
- }
145
- /**
146
- * Specifies whether the selection's {@link #focus} precedes the selection's {@link #anchor}.
147
- */
148
- get isBackward() {
149
- return !this.isCollapsed && this._lastRangeBackward;
150
- }
151
- /**
152
- * Checks whether this selection is equal to the given selection. Selections are equal if they have the same directions,
153
- * the same number of ranges and all ranges from one selection equal to ranges from the another selection.
154
- *
155
- * @param otherSelection Selection to compare with.
156
- * @returns `true` if selections are equal, `false` otherwise.
157
- */
158
- isEqual(otherSelection) {
159
- if (this.rangeCount != otherSelection.rangeCount) {
160
- return false;
161
- }
162
- else if (this.rangeCount === 0) {
163
- return true;
164
- }
165
- if (!this.anchor.isEqual(otherSelection.anchor) || !this.focus.isEqual(otherSelection.focus)) {
166
- return false;
167
- }
168
- for (const thisRange of this._ranges) {
169
- let found = false;
170
- for (const otherRange of otherSelection._ranges) {
171
- if (thisRange.isEqual(otherRange)) {
172
- found = true;
173
- break;
174
- }
175
- }
176
- if (!found) {
177
- return false;
178
- }
179
- }
180
- return true;
181
- }
182
- /**
183
- * Returns an iterable object that iterates over copies of selection ranges.
184
- */
185
- *getRanges() {
186
- for (const range of this._ranges) {
187
- yield new ModelRange(range.start, range.end);
188
- }
189
- }
190
- /**
191
- * Returns a copy of the first range in the selection.
192
- * First range is the one which {@link module:engine/model/range~ModelRange#start start} position
193
- * {@link module:engine/model/position~ModelPosition#isBefore is before} start position of all other ranges
194
- * (not to confuse with the first range added to the selection).
195
- *
196
- * Returns `null` if there are no ranges in selection.
197
- */
198
- getFirstRange() {
199
- let first = null;
200
- for (const range of this._ranges) {
201
- if (!first || range.start.isBefore(first.start)) {
202
- first = range;
203
- }
204
- }
205
- return first ? new ModelRange(first.start, first.end) : null;
206
- }
207
- /**
208
- * Returns a copy of the last range in the selection.
209
- * Last range is the one which {@link module:engine/model/range~ModelRange#end end} position
210
- * {@link module:engine/model/position~ModelPosition#isAfter is after} end position of all other
211
- * ranges (not to confuse with the range most recently added to the selection).
212
- *
213
- * Returns `null` if there are no ranges in selection.
214
- */
215
- getLastRange() {
216
- let last = null;
217
- for (const range of this._ranges) {
218
- if (!last || range.end.isAfter(last.end)) {
219
- last = range;
220
- }
221
- }
222
- return last ? new ModelRange(last.start, last.end) : null;
223
- }
224
- /**
225
- * Returns the first position in the selection.
226
- * First position is the position that {@link module:engine/model/position~ModelPosition#isBefore is before}
227
- * any other position in the selection.
228
- *
229
- * Returns `null` if there are no ranges in selection.
230
- */
231
- getFirstPosition() {
232
- const first = this.getFirstRange();
233
- return first ? first.start.clone() : null;
234
- }
235
- /**
236
- * Returns the last position in the selection.
237
- * Last position is the position that {@link module:engine/model/position~ModelPosition#isAfter is after}
238
- * any other position in the selection.
239
- *
240
- * Returns `null` if there are no ranges in selection.
241
- */
242
- getLastPosition() {
243
- const lastRange = this.getLastRange();
244
- return lastRange ? lastRange.end.clone() : null;
245
- }
246
- /**
247
- * Sets this selection's ranges and direction to the specified location based on the given
248
- * {@link module:engine/model/selection~ModelSelectable selectable}.
249
- *
250
- * ```ts
251
- * // Removes all selection's ranges.
252
- * selection.setTo( null );
253
- *
254
- * // Sets selection to the given range.
255
- * const range = writer.createRange( start, end );
256
- * selection.setTo( range );
257
- *
258
- * // Sets selection to given ranges.
259
- * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ];
260
- * selection.setTo( ranges );
261
- *
262
- * // Sets selection to other selection.
263
- * // Note: It doesn't copy selection attributes.
264
- * const otherSelection = writer.createSelection();
265
- * selection.setTo( otherSelection );
266
- *
267
- * // Sets selection to the given document selection.
268
- * // Note: It doesn't copy selection attributes.
269
- * const documentSelection = new ModelDocumentSelection( doc );
270
- * selection.setTo( documentSelection );
271
- *
272
- * // Sets collapsed selection at the given position.
273
- * const position = writer.createPositionFromPath( root, path );
274
- * selection.setTo( position );
275
- *
276
- * // Sets collapsed selection at the position of the given node and an offset.
277
- * selection.setTo( paragraph, offset );
278
- * ```
279
- *
280
- * Creates a range inside an {@link module:engine/model/element~ModelElement element} which starts before the first child of
281
- * that element and ends after the last child of that element.
282
- *
283
- * ```ts
284
- * selection.setTo( paragraph, 'in' );
285
- * ```
286
- *
287
- * Creates a range on an {@link module:engine/model/item~ModelItem item} which starts before the item and ends just after the item.
288
- *
289
- * ```ts
290
- * selection.setTo( paragraph, 'on' );
291
- * ```
292
- *
293
- * `Selection#setTo()`' method allow passing additional options (`backward`) as the last argument.
294
- *
295
- * ```ts
296
- * // Sets backward selection.
297
- * const selection = writer.createSelection( range, { backward: true } );
298
- * ```
299
- */
300
- setTo(...args) {
301
- let [selectable, placeOrOffset, options] = args;
302
- if (typeof placeOrOffset == 'object') {
303
- options = placeOrOffset;
304
- placeOrOffset = undefined;
305
- }
306
- if (selectable === null) {
307
- this._setRanges([]);
308
- }
309
- else if (selectable instanceof ModelSelection) {
310
- this._setRanges(selectable.getRanges(), selectable.isBackward);
311
- }
312
- else if (selectable && typeof selectable.getRanges == 'function') {
313
- // We assume that the selectable is a ModelDocumentSelection.
314
- // It can't be imported here, because it would lead to circular imports.
315
- this._setRanges(selectable.getRanges(), selectable.isBackward);
316
- }
317
- else if (selectable instanceof ModelRange) {
318
- this._setRanges([selectable], !!options && !!options.backward);
319
- }
320
- else if (selectable instanceof ModelPosition) {
321
- this._setRanges([new ModelRange(selectable)]);
322
- }
323
- else if (selectable instanceof ModelNode) {
324
- const backward = !!options && !!options.backward;
325
- let range;
326
- if (placeOrOffset == 'in') {
327
- range = ModelRange._createIn(selectable);
328
- }
329
- else if (placeOrOffset == 'on') {
330
- range = ModelRange._createOn(selectable);
331
- }
332
- else if (placeOrOffset !== undefined) {
333
- range = new ModelRange(ModelPosition._createAt(selectable, placeOrOffset));
334
- }
335
- else {
336
- /**
337
- * selection.setTo requires the second parameter when the first parameter is a node.
338
- *
339
- * @error model-selection-setto-required-second-parameter
340
- */
341
- throw new CKEditorError('model-selection-setto-required-second-parameter', [this, selectable]);
342
- }
343
- this._setRanges([range], backward);
344
- }
345
- else if (isIterable(selectable)) {
346
- // We assume that the selectable is an iterable of ranges.
347
- this._setRanges(selectable, options && !!options.backward);
348
- }
349
- else {
350
- /**
351
- * Cannot set the selection to the given place.
352
- *
353
- * Invalid parameters were specified when setting the selection. Common issues:
354
- *
355
- * * A {@link module:engine/model/textproxy~ModelTextProxy} instance was passed instead of
356
- * a real {@link module:engine/model/text~ModelText}.
357
- * * View nodes were passed instead of model nodes.
358
- * * `null`/`undefined` was passed.
359
- *
360
- * @error model-selection-setto-not-selectable
361
- */
362
- throw new CKEditorError('model-selection-setto-not-selectable', [this, selectable]);
363
- }
364
- }
365
- /**
366
- * Replaces all ranges that were added to the selection with given array of ranges. Last range of the array
367
- * is treated like the last added range and is used to set {@link module:engine/model/selection~ModelSelection#anchor} and
368
- * {@link module:engine/model/selection~ModelSelection#focus}. Accepts a flag describing in which direction the selection is made.
369
- *
370
- * @fires change:range
371
- * @param newRanges Ranges to set.
372
- * @param isLastBackward Flag describing if last added range was selected forward - from start to end (`false`)
373
- * or backward - from end to start (`true`).
374
- */
375
- _setRanges(newRanges, isLastBackward = false) {
376
- const ranges = Array.from(newRanges);
377
- // Check whether there is any range in new ranges set that is different than all already added ranges.
378
- const anyNewRange = ranges.some(newRange => {
379
- if (!(newRange instanceof ModelRange)) {
380
- /**
381
- * Selection range set to an object that is not an instance of {@link module:engine/model/range~ModelRange}.
382
- *
383
- * Only {@link module:engine/model/range~ModelRange} instances can be used to set a selection.
384
- * Common mistakes leading to this error are:
385
- *
386
- * * using DOM `Range` object,
387
- * * incorrect CKEditor 5 installation with multiple `ckeditor5-engine` packages having different versions.
388
- *
389
- * @error model-selection-set-ranges-not-range
390
- */
391
- throw new CKEditorError('model-selection-set-ranges-not-range', [this, newRanges]);
392
- }
393
- return this._ranges.every(oldRange => {
394
- return !oldRange.isEqual(newRange);
395
- });
396
- });
397
- // Don't do anything if nothing changed.
398
- if (ranges.length === this._ranges.length && !anyNewRange) {
399
- return;
400
- }
401
- this._replaceAllRanges(ranges);
402
- this._lastRangeBackward = !!isLastBackward;
403
- this.fire('change:range', { directChange: true });
404
- }
405
- /**
406
- * Moves {@link module:engine/model/selection~ModelSelection#focus} to the specified location.
407
- *
408
- * The location can be specified in the same form as
409
- * {@link module:engine/model/writer~ModelWriter#createPositionAt writer.createPositionAt()} parameters.
410
- *
411
- * @fires change:range
412
- * @param offset Offset or one of the flags. Used only when first parameter is a {@link module:engine/model/item~ModelItem model item}.
413
- */
414
- setFocus(itemOrPosition, offset) {
415
- if (this.anchor === null) {
416
- /**
417
- * Cannot set selection focus if there are no ranges in selection.
418
- *
419
- * @error model-selection-setfocus-no-ranges
420
- */
421
- throw new CKEditorError('model-selection-setfocus-no-ranges', [this, itemOrPosition]);
422
- }
423
- const newFocus = ModelPosition._createAt(itemOrPosition, offset);
424
- if (newFocus.compareWith(this.focus) == 'same') {
425
- return;
426
- }
427
- const anchor = this.anchor;
428
- if (this._ranges.length) {
429
- this._popRange();
430
- }
431
- if (newFocus.compareWith(anchor) == 'before') {
432
- this._pushRange(new ModelRange(newFocus, anchor));
433
- this._lastRangeBackward = true;
434
- }
435
- else {
436
- this._pushRange(new ModelRange(anchor, newFocus));
437
- this._lastRangeBackward = false;
438
- }
439
- this.fire('change:range', { directChange: true });
440
- }
441
- /**
442
- * Gets an attribute value for given key or `undefined` if that attribute is not set on the selection.
443
- *
444
- * @param key Key of attribute to look for.
445
- * @returns Attribute value or `undefined`.
446
- */
447
- getAttribute(key) {
448
- return this._attrs.get(key);
449
- }
450
- /**
451
- * Returns iterable that iterates over this selection's attributes.
452
- *
453
- * Attributes are returned as arrays containing two items. First one is attribute key and second is attribute value.
454
- * This format is accepted by native `Map` object and also can be passed in `Node` constructor.
455
- */
456
- getAttributes() {
457
- return this._attrs.entries();
458
- }
459
- /**
460
- * Returns iterable that iterates over this selection's attribute keys.
461
- */
462
- getAttributeKeys() {
463
- return this._attrs.keys();
464
- }
465
- /**
466
- * Checks if the selection has an attribute for given key.
467
- *
468
- * @param key Key of attribute to check.
469
- * @returns `true` if attribute with given key is set on selection, `false` otherwise.
470
- */
471
- hasAttribute(key) {
472
- return this._attrs.has(key);
473
- }
474
- /**
475
- * Removes an attribute with given key from the selection.
476
- *
477
- * If given attribute was set on the selection, fires the {@link #event:change:range} event with
478
- * removed attribute key.
479
- *
480
- * @fires change:attribute
481
- * @param key Key of attribute to remove.
482
- */
483
- removeAttribute(key) {
484
- if (this.hasAttribute(key)) {
485
- this._attrs.delete(key);
486
- this.fire('change:attribute', { attributeKeys: [key], directChange: true });
487
- }
488
- }
489
- /**
490
- * Sets attribute on the selection. If attribute with the same key already is set, it's value is overwritten.
491
- *
492
- * If the attribute value has changed, fires the {@link #event:change:range} event with
493
- * the attribute key.
494
- *
495
- * @fires change:attribute
496
- * @param key Key of attribute to set.
497
- * @param value Attribute value.
498
- */
499
- setAttribute(key, value) {
500
- if (this.getAttribute(key) !== value) {
501
- this._attrs.set(key, value);
502
- this.fire('change:attribute', { attributeKeys: [key], directChange: true });
503
- }
504
- }
505
- /**
506
- * Returns the selected element. {@link module:engine/model/element~ModelElement Element} is considered as selected if there is only
507
- * one range in the selection, and that range contains exactly one element.
508
- * Returns `null` if there is no selected element.
509
- */
510
- getSelectedElement() {
511
- if (this.rangeCount !== 1) {
512
- return null;
513
- }
514
- return this.getFirstRange().getContainedElement();
515
- }
516
- /**
517
- * Gets elements of type {@link module:engine/model/schema~ModelSchema#isBlock "block"} touched by the selection.
518
- *
519
- * This method's result can be used for example to apply block styling to all blocks covered by this selection.
520
- *
521
- * **Note:** `getSelectedBlocks()` returns blocks that are nested in other non-block elements
522
- * but will not return blocks nested in other blocks.
523
- *
524
- * In this case the function will return exactly all 3 paragraphs (note: `<blockQuote>` is not a block itself):
525
- *
526
- * ```xml
527
- * <paragraph>[a</paragraph>
528
- * <blockQuote>
529
- * <paragraph>b</paragraph>
530
- * </blockQuote>
531
- * <paragraph>c]d</paragraph>
532
- * ```
533
- *
534
- * In this case the paragraph will also be returned, despite the collapsed selection:
535
- *
536
- * ```xml
537
- * <paragraph>[]a</paragraph>
538
- * ```
539
- *
540
- * In such a scenario, however, only blocks A, B & E will be returned as blocks C & D are nested in block B:
541
- *
542
- * ```xml
543
- * [<blockA></blockA>
544
- * <blockB>
545
- * <blockC></blockC>
546
- * <blockD></blockD>
547
- * </blockB>
548
- * <blockE></blockE>]
549
- * ```
550
- *
551
- * If the selection is inside a block all the inner blocks (A & B) are returned:
552
- *
553
- * ```xml
554
- * <block>
555
- * <blockA>[a</blockA>
556
- * <blockB>b]</blockB>
557
- * </block>
558
- * ```
559
- *
560
- * **Special case**: Selection ignores first and/or last blocks if nothing (from user perspective) is selected in them.
561
- *
562
- * ```xml
563
- * // Selection ends and the beginning of the last block.
564
- * <paragraph>[a</paragraph>
565
- * <paragraph>b</paragraph>
566
- * <paragraph>]c</paragraph> // This block will not be returned
567
- *
568
- * // Selection begins at the end of the first block.
569
- * <paragraph>a[</paragraph> // This block will not be returned
570
- * <paragraph>b</paragraph>
571
- * <paragraph>c]</paragraph>
572
- *
573
- * // Selection begings at the end of the first block and ends at the beginning of the last block.
574
- * <paragraph>a[</paragraph> // This block will not be returned
575
- * <paragraph>b</paragraph>
576
- * <paragraph>]c</paragraph> // This block will not be returned
577
- * ```
578
- */
579
- *getSelectedBlocks() {
580
- const visited = new WeakSet();
581
- for (const range of this.getRanges()) {
582
- // Get start block of range in case of a collapsed range.
583
- const startBlock = getParentBlock(range.start, visited);
584
- if (isStartBlockSelected(startBlock, range)) {
585
- yield startBlock;
586
- }
587
- const treewalker = range.getWalker();
588
- for (const value of treewalker) {
589
- const block = value.item;
590
- if (value.type == 'elementEnd' && isUnvisitedTopBlock(block, visited, range)) {
591
- yield block;
592
- }
593
- // If element is block, we can skip its children and jump to the end of it.
594
- else if (value.type == 'elementStart' &&
595
- block.is('model:element') &&
596
- block.root.document.model.schema.isBlock(block)) {
597
- treewalker.jumpTo(ModelPosition._createAt(block, 'end'));
598
- }
599
- }
600
- const endBlock = getParentBlock(range.end, visited);
601
- if (isEndBlockSelected(endBlock, range)) {
602
- yield endBlock;
603
- }
604
- }
605
- }
606
- /**
607
- * Checks whether the selection contains the entire content of the given element. This means that selection must start
608
- * at a position {@link module:engine/model/position~ModelPosition#isTouching touching} the element's start and ends at position
609
- * touching the element's end.
610
- *
611
- * By default, this method will check whether the entire content of the selection's current root is selected.
612
- * Useful to check if e.g. the user has just pressed <kbd>Ctrl</kbd> + <kbd>A</kbd>.
613
- */
614
- containsEntireContent(element = this.anchor.root) {
615
- const limitStartPosition = ModelPosition._createAt(element, 0);
616
- const limitEndPosition = ModelPosition._createAt(element, 'end');
617
- return limitStartPosition.isTouching(this.getFirstPosition()) &&
618
- limitEndPosition.isTouching(this.getLastPosition());
619
- }
620
- /**
621
- * Converts `Selection` to plain object and returns it.
622
- *
623
- * @returns `Selection` converted to plain object.
624
- */
625
- toJSON() {
626
- const json = {
627
- ranges: Array.from(this.getRanges()).map(range => range.toJSON())
628
- };
629
- const attributes = Object.fromEntries(this.getAttributes());
630
- if (Object.keys(attributes).length) {
631
- json.attributes = attributes;
632
- }
633
- if (this.isBackward) {
634
- json.isBackward = true;
635
- }
636
- return json;
637
- }
638
- /**
639
- * Adds given range to internal {@link #_ranges ranges array}. Throws an error
640
- * if given range is intersecting with any range that is already stored in this selection.
641
- */
642
- _pushRange(range) {
643
- this._checkRange(range);
644
- this._ranges.push(new ModelRange(range.start, range.end));
645
- }
646
- /**
647
- * Checks if given range intersects with ranges that are already in the selection. Throws an error if it does.
648
- */
649
- _checkRange(range) {
650
- for (let i = 0; i < this._ranges.length; i++) {
651
- if (range.isIntersecting(this._ranges[i])) {
652
- /**
653
- * Trying to add a range that intersects with another range in the selection.
654
- *
655
- * @error model-selection-range-intersects
656
- * @param {module:engine/model/range~ModelRange} addedRange Range that was added to the selection.
657
- * @param {module:engine/model/range~ModelRange} intersectingRange Range in the selection that intersects with `addedRange`.
658
- */
659
- throw new CKEditorError('model-selection-range-intersects', [this, range], { addedRange: range, intersectingRange: this._ranges[i] });
660
- }
661
- }
662
- }
663
- /**
664
- * Replaces all the ranges by the given ones.
665
- * Uses {@link #_popRange _popRange} and {@link #_pushRange _pushRange} to ensure proper ranges removal and addition.
666
- */
667
- _replaceAllRanges(ranges) {
668
- this._removeAllRanges();
669
- for (const range of ranges) {
670
- this._pushRange(range);
671
- }
672
- }
673
- /**
674
- * Deletes ranges from internal range array. Uses {@link #_popRange _popRange} to
675
- * ensure proper ranges removal.
676
- */
677
- _removeAllRanges() {
678
- while (this._ranges.length > 0) {
679
- this._popRange();
680
- }
681
- }
682
- /**
683
- * Removes most recently added range from the selection.
684
- */
685
- _popRange() {
686
- this._ranges.pop();
687
- }
688
- }
689
- // The magic of type inference using `is` method is centralized in `TypeCheckable` class.
690
- // Proper overload would interfere with that.
691
- ModelSelection.prototype.is = function (type) {
692
- return type === 'selection' || type === 'model:selection';
693
- };
694
- /**
695
- * Checks whether the given element extends $block in the schema and has a parent (is not a root).
696
- * Marks it as already visited.
697
- */
698
- function isUnvisitedBlock(element, visited) {
699
- if (visited.has(element)) {
700
- return false;
701
- }
702
- visited.add(element);
703
- return element.root.document.model.schema.isBlock(element) && !!element.parent;
704
- }
705
- /**
706
- * Checks if the given element is a $block was not previously visited and is a top block in a range.
707
- */
708
- function isUnvisitedTopBlock(element, visited, range) {
709
- return isUnvisitedBlock(element, visited) && isTopBlockInRange(element, range);
710
- }
711
- /**
712
- * Finds the lowest element in position's ancestors which is a block.
713
- * It will search until first ancestor that is a limit element.
714
- * Marks all ancestors as already visited to not include any of them later on.
715
- */
716
- function getParentBlock(position, visited) {
717
- const element = position.parent;
718
- const schema = element.root.document.model.schema;
719
- const ancestors = position.parent.getAncestors({ parentFirst: true, includeSelf: true });
720
- let hasParentLimit = false;
721
- const block = ancestors.find((element) => {
722
- // Stop searching after first parent node that is limit element.
723
- if (hasParentLimit) {
724
- return false;
725
- }
726
- hasParentLimit = schema.isLimit(element);
727
- return !hasParentLimit && isUnvisitedBlock(element, visited);
728
- });
729
- // Mark all ancestors of this position's parent, because find() might've stopped early and
730
- // the found block may be a child of another block.
731
- ancestors.forEach(element => visited.add(element));
732
- return block;
733
- }
734
- /**
735
- * Checks if the blocks is not nested in other block inside a range.
736
- */
737
- function isTopBlockInRange(block, range) {
738
- const parentBlock = findAncestorBlock(block);
739
- if (!parentBlock) {
740
- return true;
741
- }
742
- // Add loose flag to check as parentRange can be equal to range.
743
- const isParentInRange = range.containsRange(ModelRange._createOn(parentBlock), true);
744
- return !isParentInRange;
745
- }
746
- /**
747
- * If a selection starts at the end of a block, that block is not returned as from the user's perspective this block wasn't selected.
748
- * See [#11585](https://github.com/ckeditor/ckeditor5/issues/11585) for more details.
749
- *
750
- * ```xml
751
- * <paragraph>a[</paragraph> // This block will not be returned
752
- * <paragraph>b</paragraph>
753
- * <paragraph>c]</paragraph>
754
- * ```
755
- *
756
- * Collapsed selection is not affected by it:
757
- *
758
- * ```xml
759
- * <paragraph>a[]</paragraph> // This block will be returned
760
- * ```
761
- */
762
- function isStartBlockSelected(startBlock, range) {
763
- if (!startBlock) {
764
- return false;
765
- }
766
- if (range.isCollapsed || startBlock.isEmpty) {
767
- return true;
768
- }
769
- if (range.start.isTouching(ModelPosition._createAt(startBlock, startBlock.maxOffset))) {
770
- return false;
771
- }
772
- return isTopBlockInRange(startBlock, range);
773
- }
774
- /**
775
- * If a selection ends at the beginning of a block, that block is not returned as from the user's perspective this block wasn't selected.
776
- * See [#984](https://github.com/ckeditor/ckeditor5-engine/issues/984) for more details.
777
- *
778
- * ```xml
779
- * <paragraph>[a</paragraph>
780
- * <paragraph>b</paragraph>
781
- * <paragraph>]c</paragraph> // this block will not be returned
782
- * ```
783
- *
784
- * Collapsed selection is not affected by it:
785
- *
786
- * ```xml
787
- * <paragraph>[]a</paragraph> // this block will be returned
788
- * ```
789
- */
790
- function isEndBlockSelected(endBlock, range) {
791
- if (!endBlock) {
792
- return false;
793
- }
794
- if (range.isCollapsed || endBlock.isEmpty) {
795
- return true;
796
- }
797
- if (range.end.isTouching(ModelPosition._createAt(endBlock, 0))) {
798
- return false;
799
- }
800
- return isTopBlockInRange(endBlock, range);
801
- }
802
- /**
803
- * Returns first ancestor block of a node.
804
- */
805
- function findAncestorBlock(node) {
806
- const schema = node.root.document.model.schema;
807
- let parent = node.parent;
808
- while (parent) {
809
- if (schema.isBlock(parent)) {
810
- return parent;
811
- }
812
- parent = parent.parent;
813
- }
814
- }