@ckeditor/ckeditor5-engine 39.0.1 → 40.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 (244) hide show
  1. package/CHANGELOG.md +2 -2
  2. package/LICENSE.md +1 -1
  3. package/README.md +8 -8
  4. package/package.json +2 -6
  5. package/src/controller/datacontroller.d.ts +334 -334
  6. package/src/controller/datacontroller.js +481 -481
  7. package/src/controller/editingcontroller.d.ts +98 -98
  8. package/src/controller/editingcontroller.js +191 -191
  9. package/src/conversion/conversion.d.ts +478 -478
  10. package/src/conversion/conversion.js +601 -601
  11. package/src/conversion/conversionhelpers.d.ts +26 -26
  12. package/src/conversion/conversionhelpers.js +32 -32
  13. package/src/conversion/downcastdispatcher.d.ts +562 -562
  14. package/src/conversion/downcastdispatcher.js +547 -547
  15. package/src/conversion/downcasthelpers.d.ts +1226 -1226
  16. package/src/conversion/downcasthelpers.js +2183 -2183
  17. package/src/conversion/mapper.d.ts +503 -503
  18. package/src/conversion/mapper.js +536 -536
  19. package/src/conversion/modelconsumable.d.ts +201 -201
  20. package/src/conversion/modelconsumable.js +333 -333
  21. package/src/conversion/upcastdispatcher.d.ts +492 -492
  22. package/src/conversion/upcastdispatcher.js +460 -460
  23. package/src/conversion/upcasthelpers.d.ts +499 -499
  24. package/src/conversion/upcasthelpers.js +950 -950
  25. package/src/conversion/viewconsumable.d.ts +369 -369
  26. package/src/conversion/viewconsumable.js +532 -532
  27. package/src/dataprocessor/basichtmlwriter.d.ts +18 -18
  28. package/src/dataprocessor/basichtmlwriter.js +19 -19
  29. package/src/dataprocessor/dataprocessor.d.ts +61 -61
  30. package/src/dataprocessor/dataprocessor.js +5 -5
  31. package/src/dataprocessor/htmldataprocessor.d.ts +76 -76
  32. package/src/dataprocessor/htmldataprocessor.js +96 -96
  33. package/src/dataprocessor/htmlwriter.d.ts +16 -16
  34. package/src/dataprocessor/htmlwriter.js +5 -5
  35. package/src/dataprocessor/xmldataprocessor.d.ts +90 -90
  36. package/src/dataprocessor/xmldataprocessor.js +108 -108
  37. package/src/dev-utils/model.d.ts +124 -124
  38. package/src/dev-utils/model.js +395 -395
  39. package/src/dev-utils/operationreplayer.d.ts +51 -51
  40. package/src/dev-utils/operationreplayer.js +112 -112
  41. package/src/dev-utils/utils.d.ts +37 -37
  42. package/src/dev-utils/utils.js +73 -73
  43. package/src/dev-utils/view.d.ts +319 -319
  44. package/src/dev-utils/view.js +967 -967
  45. package/src/index.d.ts +114 -114
  46. package/src/index.js +78 -78
  47. package/src/model/batch.d.ts +106 -106
  48. package/src/model/batch.js +96 -96
  49. package/src/model/differ.d.ts +387 -387
  50. package/src/model/differ.js +1149 -1149
  51. package/src/model/document.d.ts +272 -272
  52. package/src/model/document.js +361 -361
  53. package/src/model/documentfragment.d.ts +200 -200
  54. package/src/model/documentfragment.js +306 -306
  55. package/src/model/documentselection.d.ts +420 -420
  56. package/src/model/documentselection.js +993 -993
  57. package/src/model/element.d.ts +165 -165
  58. package/src/model/element.js +281 -281
  59. package/src/model/history.d.ts +114 -114
  60. package/src/model/history.js +207 -207
  61. package/src/model/item.d.ts +14 -14
  62. package/src/model/item.js +5 -5
  63. package/src/model/liveposition.d.ts +77 -77
  64. package/src/model/liveposition.js +93 -93
  65. package/src/model/liverange.d.ts +102 -102
  66. package/src/model/liverange.js +120 -120
  67. package/src/model/markercollection.d.ts +335 -335
  68. package/src/model/markercollection.js +403 -403
  69. package/src/model/model.d.ts +919 -919
  70. package/src/model/model.js +842 -842
  71. package/src/model/node.d.ts +256 -256
  72. package/src/model/node.js +375 -375
  73. package/src/model/nodelist.d.ts +91 -91
  74. package/src/model/nodelist.js +163 -163
  75. package/src/model/operation/attributeoperation.d.ts +103 -103
  76. package/src/model/operation/attributeoperation.js +148 -148
  77. package/src/model/operation/detachoperation.d.ts +60 -60
  78. package/src/model/operation/detachoperation.js +77 -77
  79. package/src/model/operation/insertoperation.d.ts +90 -90
  80. package/src/model/operation/insertoperation.js +135 -135
  81. package/src/model/operation/markeroperation.d.ts +91 -91
  82. package/src/model/operation/markeroperation.js +107 -107
  83. package/src/model/operation/mergeoperation.d.ts +100 -100
  84. package/src/model/operation/mergeoperation.js +167 -167
  85. package/src/model/operation/moveoperation.d.ts +96 -96
  86. package/src/model/operation/moveoperation.js +164 -164
  87. package/src/model/operation/nooperation.d.ts +38 -38
  88. package/src/model/operation/nooperation.js +48 -48
  89. package/src/model/operation/operation.d.ts +96 -96
  90. package/src/model/operation/operation.js +62 -62
  91. package/src/model/operation/operationfactory.d.ts +18 -18
  92. package/src/model/operation/operationfactory.js +44 -44
  93. package/src/model/operation/renameoperation.d.ts +83 -83
  94. package/src/model/operation/renameoperation.js +115 -115
  95. package/src/model/operation/rootattributeoperation.d.ts +98 -98
  96. package/src/model/operation/rootattributeoperation.js +155 -155
  97. package/src/model/operation/rootoperation.d.ts +76 -76
  98. package/src/model/operation/rootoperation.js +90 -90
  99. package/src/model/operation/splitoperation.d.ts +109 -109
  100. package/src/model/operation/splitoperation.js +194 -194
  101. package/src/model/operation/transform.d.ts +100 -100
  102. package/src/model/operation/transform.js +1985 -1985
  103. package/src/model/operation/utils.d.ts +71 -71
  104. package/src/model/operation/utils.js +213 -213
  105. package/src/model/position.d.ts +539 -539
  106. package/src/model/position.js +979 -979
  107. package/src/model/range.d.ts +458 -458
  108. package/src/model/range.js +875 -875
  109. package/src/model/rootelement.d.ts +60 -60
  110. package/src/model/rootelement.js +74 -74
  111. package/src/model/schema.d.ts +1186 -1186
  112. package/src/model/schema.js +1242 -1242
  113. package/src/model/selection.d.ts +482 -482
  114. package/src/model/selection.js +789 -789
  115. package/src/model/text.d.ts +66 -66
  116. package/src/model/text.js +85 -85
  117. package/src/model/textproxy.d.ts +144 -144
  118. package/src/model/textproxy.js +189 -189
  119. package/src/model/treewalker.d.ts +186 -186
  120. package/src/model/treewalker.js +244 -244
  121. package/src/model/typecheckable.d.ts +285 -285
  122. package/src/model/typecheckable.js +16 -16
  123. package/src/model/utils/autoparagraphing.d.ts +37 -37
  124. package/src/model/utils/autoparagraphing.js +63 -63
  125. package/src/model/utils/deletecontent.d.ts +58 -58
  126. package/src/model/utils/deletecontent.js +488 -488
  127. package/src/model/utils/findoptimalinsertionrange.d.ts +32 -32
  128. package/src/model/utils/findoptimalinsertionrange.js +57 -57
  129. package/src/model/utils/getselectedcontent.d.ts +30 -30
  130. package/src/model/utils/getselectedcontent.js +125 -125
  131. package/src/model/utils/insertcontent.d.ts +46 -46
  132. package/src/model/utils/insertcontent.js +705 -705
  133. package/src/model/utils/insertobject.d.ts +44 -44
  134. package/src/model/utils/insertobject.js +139 -139
  135. package/src/model/utils/modifyselection.d.ts +48 -48
  136. package/src/model/utils/modifyselection.js +186 -186
  137. package/src/model/utils/selection-post-fixer.d.ts +74 -74
  138. package/src/model/utils/selection-post-fixer.js +260 -260
  139. package/src/model/writer.d.ts +851 -851
  140. package/src/model/writer.js +1306 -1306
  141. package/src/view/attributeelement.d.ts +108 -108
  142. package/src/view/attributeelement.js +184 -184
  143. package/src/view/containerelement.d.ts +49 -49
  144. package/src/view/containerelement.js +80 -80
  145. package/src/view/datatransfer.d.ts +79 -79
  146. package/src/view/datatransfer.js +98 -98
  147. package/src/view/document.d.ts +184 -184
  148. package/src/view/document.js +120 -120
  149. package/src/view/documentfragment.d.ts +149 -149
  150. package/src/view/documentfragment.js +228 -228
  151. package/src/view/documentselection.d.ts +306 -306
  152. package/src/view/documentselection.js +256 -256
  153. package/src/view/domconverter.d.ts +640 -640
  154. package/src/view/domconverter.js +1450 -1425
  155. package/src/view/downcastwriter.d.ts +996 -996
  156. package/src/view/downcastwriter.js +1696 -1696
  157. package/src/view/editableelement.d.ts +62 -62
  158. package/src/view/editableelement.js +62 -62
  159. package/src/view/element.d.ts +468 -468
  160. package/src/view/element.js +724 -724
  161. package/src/view/elementdefinition.d.ts +87 -87
  162. package/src/view/elementdefinition.js +5 -5
  163. package/src/view/emptyelement.d.ts +41 -41
  164. package/src/view/emptyelement.js +73 -73
  165. package/src/view/filler.d.ts +111 -111
  166. package/src/view/filler.js +150 -150
  167. package/src/view/item.d.ts +14 -14
  168. package/src/view/item.js +5 -5
  169. package/src/view/matcher.d.ts +486 -486
  170. package/src/view/matcher.js +507 -507
  171. package/src/view/node.d.ts +163 -163
  172. package/src/view/node.js +228 -228
  173. package/src/view/observer/arrowkeysobserver.d.ts +45 -45
  174. package/src/view/observer/arrowkeysobserver.js +40 -40
  175. package/src/view/observer/bubblingemittermixin.d.ts +166 -166
  176. package/src/view/observer/bubblingemittermixin.js +172 -172
  177. package/src/view/observer/bubblingeventinfo.d.ts +47 -47
  178. package/src/view/observer/bubblingeventinfo.js +37 -37
  179. package/src/view/observer/clickobserver.d.ts +43 -43
  180. package/src/view/observer/clickobserver.js +29 -29
  181. package/src/view/observer/compositionobserver.d.ts +82 -82
  182. package/src/view/observer/compositionobserver.js +60 -60
  183. package/src/view/observer/domeventdata.d.ts +50 -50
  184. package/src/view/observer/domeventdata.js +47 -47
  185. package/src/view/observer/domeventobserver.d.ts +73 -73
  186. package/src/view/observer/domeventobserver.js +79 -79
  187. package/src/view/observer/fakeselectionobserver.d.ts +47 -47
  188. package/src/view/observer/fakeselectionobserver.js +91 -91
  189. package/src/view/observer/focusobserver.d.ts +82 -82
  190. package/src/view/observer/focusobserver.js +86 -86
  191. package/src/view/observer/inputobserver.d.ts +86 -86
  192. package/src/view/observer/inputobserver.js +164 -164
  193. package/src/view/observer/keyobserver.d.ts +66 -66
  194. package/src/view/observer/keyobserver.js +39 -39
  195. package/src/view/observer/mouseobserver.d.ts +89 -89
  196. package/src/view/observer/mouseobserver.js +29 -29
  197. package/src/view/observer/mutationobserver.d.ts +86 -86
  198. package/src/view/observer/mutationobserver.js +206 -206
  199. package/src/view/observer/observer.d.ts +89 -89
  200. package/src/view/observer/observer.js +84 -84
  201. package/src/view/observer/selectionobserver.d.ts +148 -148
  202. package/src/view/observer/selectionobserver.js +202 -202
  203. package/src/view/observer/tabobserver.d.ts +46 -46
  204. package/src/view/observer/tabobserver.js +42 -42
  205. package/src/view/placeholder.d.ts +96 -96
  206. package/src/view/placeholder.js +267 -267
  207. package/src/view/position.d.ts +189 -189
  208. package/src/view/position.js +324 -324
  209. package/src/view/range.d.ts +279 -279
  210. package/src/view/range.js +430 -430
  211. package/src/view/rawelement.d.ts +73 -73
  212. package/src/view/rawelement.js +105 -105
  213. package/src/view/renderer.d.ts +265 -265
  214. package/src/view/renderer.js +999 -999
  215. package/src/view/rooteditableelement.d.ts +41 -41
  216. package/src/view/rooteditableelement.js +69 -69
  217. package/src/view/selection.d.ts +375 -375
  218. package/src/view/selection.js +559 -559
  219. package/src/view/styles/background.d.ts +33 -33
  220. package/src/view/styles/background.js +74 -74
  221. package/src/view/styles/border.d.ts +43 -43
  222. package/src/view/styles/border.js +316 -316
  223. package/src/view/styles/margin.d.ts +29 -29
  224. package/src/view/styles/margin.js +34 -34
  225. package/src/view/styles/padding.d.ts +29 -29
  226. package/src/view/styles/padding.js +34 -34
  227. package/src/view/styles/utils.d.ts +93 -93
  228. package/src/view/styles/utils.js +219 -219
  229. package/src/view/stylesmap.d.ts +675 -675
  230. package/src/view/stylesmap.js +766 -766
  231. package/src/view/text.d.ts +74 -74
  232. package/src/view/text.js +93 -93
  233. package/src/view/textproxy.d.ts +97 -97
  234. package/src/view/textproxy.js +124 -124
  235. package/src/view/treewalker.d.ts +195 -195
  236. package/src/view/treewalker.js +327 -327
  237. package/src/view/typecheckable.d.ts +448 -448
  238. package/src/view/typecheckable.js +19 -19
  239. package/src/view/uielement.d.ts +96 -96
  240. package/src/view/uielement.js +182 -182
  241. package/src/view/upcastwriter.d.ts +417 -417
  242. package/src/view/upcastwriter.js +359 -359
  243. package/src/view/view.d.ts +487 -487
  244. package/src/view/view.js +546 -546
@@ -1,789 +1,789 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module engine/model/selection
7
- */
8
- import TypeCheckable from './typecheckable';
9
- import Node from './node';
10
- import Position from './position';
11
- import Range from './range';
12
- import { CKEditorError, EmitterMixin, isIterable } from '@ckeditor/ckeditor5-utils';
13
- /**
14
- * Selection is a set of {@link module:engine/model/range~Range ranges}. It has a direction specified by its
15
- * {@link module:engine/model/selection~Selection#anchor anchor} and {@link module:engine/model/selection~Selection#focus focus}
16
- * (it can be {@link module:engine/model/selection~Selection#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 default class Selection extends EmitterMixin(TypeCheckable) {
21
- /**
22
- * Creates a new selection instance based on the given {@link module:engine/model/selection~Selectable selectable}
23
- * or creates an empty selection if no arguments were passed.
24
- *
25
- * ```ts
26
- * // Creates empty selection without ranges.
27
- * const selection = writer.createSelection();
28
- *
29
- * // Creates selection at the given range.
30
- * const range = writer.createRange( start, end );
31
- * const selection = writer.createSelection( range );
32
- *
33
- * // Creates selection at the given ranges
34
- * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ];
35
- * const selection = writer.createSelection( ranges );
36
- *
37
- * // Creates selection from the other selection.
38
- * // Note: It doesn't copy selection attributes.
39
- * const otherSelection = writer.createSelection();
40
- * const selection = writer.createSelection( otherSelection );
41
- *
42
- * // Creates selection from the given document selection.
43
- * // Note: It doesn't copy selection attributes.
44
- * const documentSelection = model.document.selection;
45
- * const selection = writer.createSelection( documentSelection );
46
- *
47
- * // Creates selection at the given position.
48
- * const position = writer.createPositionFromPath( root, path );
49
- * const selection = writer.createSelection( position );
50
- *
51
- * // Creates selection at the given offset in the given element.
52
- * const paragraph = writer.createElement( 'paragraph' );
53
- * const selection = writer.createSelection( paragraph, offset );
54
- *
55
- * // Creates a range inside an {@link module:engine/model/element~Element element} which starts before the
56
- * // first child of that element and ends after the last child of that element.
57
- * const selection = writer.createSelection( paragraph, 'in' );
58
- *
59
- * // Creates a range on an {@link module:engine/model/item~Item item} which starts before the item and ends
60
- * // just after the item.
61
- * const selection = writer.createSelection( paragraph, 'on' );
62
- * ```
63
- *
64
- * Selection's constructor allow passing additional options (`'backward'`) as the last argument.
65
- *
66
- * ```ts
67
- * // Creates backward selection.
68
- * const selection = writer.createSelection( range, { backward: true } );
69
- * ```
70
- *
71
- * @internal
72
- */
73
- constructor(...args) {
74
- super();
75
- /**
76
- * Specifies whether the last added range was added as a backward or forward range.
77
- */
78
- this._lastRangeBackward = false;
79
- /**
80
- * List of attributes set on current selection.
81
- */
82
- this._attrs = new Map();
83
- /** @internal */
84
- this._ranges = [];
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~Range#start start} or
97
- * {@link module:engine/model/range~Range#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 Range(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~Range#start start} position
193
- * {@link module:engine/model/position~Position#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 Range(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~Range#end end} position
210
- * {@link module:engine/model/position~Position#isAfter is after} end position of all other ranges (not to confuse with the range most
211
- * 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 Range(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~Position#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~Position#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~Selectable 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 DocumentSelection( 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~Element 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~Item 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 Selection) {
310
- this._setRanges(selectable.getRanges(), selectable.isBackward);
311
- }
312
- else if (selectable && typeof selectable.getRanges == 'function') {
313
- // We assume that the selectable is a DocumentSelection.
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 Range) {
318
- this._setRanges([selectable], !!options && !!options.backward);
319
- }
320
- else if (selectable instanceof Position) {
321
- this._setRanges([new Range(selectable)]);
322
- }
323
- else if (selectable instanceof Node) {
324
- const backward = !!options && !!options.backward;
325
- let range;
326
- if (placeOrOffset == 'in') {
327
- range = Range._createIn(selectable);
328
- }
329
- else if (placeOrOffset == 'on') {
330
- range = Range._createOn(selectable);
331
- }
332
- else if (placeOrOffset !== undefined) {
333
- range = new Range(Position._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~TextProxy} instance was passed instead of
356
- * a real {@link module:engine/model/text~Text}.
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~Selection#anchor} and
368
- * {@link module:engine/model/selection~Selection#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 Range)) {
380
- /**
381
- * Selection range set to an object that is not an instance of {@link module:engine/model/range~Range}.
382
- *
383
- * Only {@link module:engine/model/range~Range} 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~Selection#focus} to the specified location.
407
- *
408
- * The location can be specified in the same form as
409
- * {@link module:engine/model/writer~Writer#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~Item 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 = Position._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 Range(newFocus, anchor));
433
- this._lastRangeBackward = true;
434
- }
435
- else {
436
- this._pushRange(new Range(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~Element 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~Schema#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
- for (const value of range.getWalker()) {
588
- const block = value.item;
589
- if (value.type == 'elementEnd' && isUnvisitedTopBlock(block, visited, range)) {
590
- yield block;
591
- }
592
- }
593
- const endBlock = getParentBlock(range.end, visited);
594
- if (isEndBlockSelected(endBlock, range)) {
595
- yield endBlock;
596
- }
597
- }
598
- }
599
- /**
600
- * Checks whether the selection contains the entire content of the given element. This means that selection must start
601
- * at a position {@link module:engine/model/position~Position#isTouching touching} the element's start and ends at position
602
- * touching the element's end.
603
- *
604
- * By default, this method will check whether the entire content of the selection's current root is selected.
605
- * Useful to check if e.g. the user has just pressed <kbd>Ctrl</kbd> + <kbd>A</kbd>.
606
- */
607
- containsEntireContent(element = this.anchor.root) {
608
- const limitStartPosition = Position._createAt(element, 0);
609
- const limitEndPosition = Position._createAt(element, 'end');
610
- return limitStartPosition.isTouching(this.getFirstPosition()) &&
611
- limitEndPosition.isTouching(this.getLastPosition());
612
- }
613
- /**
614
- * Adds given range to internal {@link #_ranges ranges array}. Throws an error
615
- * if given range is intersecting with any range that is already stored in this selection.
616
- */
617
- _pushRange(range) {
618
- this._checkRange(range);
619
- this._ranges.push(new Range(range.start, range.end));
620
- }
621
- /**
622
- * Checks if given range intersects with ranges that are already in the selection. Throws an error if it does.
623
- */
624
- _checkRange(range) {
625
- for (let i = 0; i < this._ranges.length; i++) {
626
- if (range.isIntersecting(this._ranges[i])) {
627
- /**
628
- * Trying to add a range that intersects with another range in the selection.
629
- *
630
- * @error model-selection-range-intersects
631
- * @param addedRange Range that was added to the selection.
632
- * @param intersectingRange Range in the selection that intersects with `addedRange`.
633
- */
634
- throw new CKEditorError('model-selection-range-intersects', [this, range], { addedRange: range, intersectingRange: this._ranges[i] });
635
- }
636
- }
637
- }
638
- /**
639
- * Replaces all the ranges by the given ones.
640
- * Uses {@link #_popRange _popRange} and {@link #_pushRange _pushRange} to ensure proper ranges removal and addition.
641
- */
642
- _replaceAllRanges(ranges) {
643
- this._removeAllRanges();
644
- for (const range of ranges) {
645
- this._pushRange(range);
646
- }
647
- }
648
- /**
649
- * Deletes ranges from internal range array. Uses {@link #_popRange _popRange} to
650
- * ensure proper ranges removal.
651
- */
652
- _removeAllRanges() {
653
- while (this._ranges.length > 0) {
654
- this._popRange();
655
- }
656
- }
657
- /**
658
- * Removes most recently added range from the selection.
659
- */
660
- _popRange() {
661
- this._ranges.pop();
662
- }
663
- }
664
- // The magic of type inference using `is` method is centralized in `TypeCheckable` class.
665
- // Proper overload would interfere with that.
666
- Selection.prototype.is = function (type) {
667
- return type === 'selection' || type === 'model:selection';
668
- };
669
- /**
670
- * Checks whether the given element extends $block in the schema and has a parent (is not a root).
671
- * Marks it as already visited.
672
- */
673
- function isUnvisitedBlock(element, visited) {
674
- if (visited.has(element)) {
675
- return false;
676
- }
677
- visited.add(element);
678
- return element.root.document.model.schema.isBlock(element) && !!element.parent;
679
- }
680
- /**
681
- * Checks if the given element is a $block was not previously visited and is a top block in a range.
682
- */
683
- function isUnvisitedTopBlock(element, visited, range) {
684
- return isUnvisitedBlock(element, visited) && isTopBlockInRange(element, range);
685
- }
686
- /**
687
- * Finds the lowest element in position's ancestors which is a block.
688
- * It will search until first ancestor that is a limit element.
689
- * Marks all ancestors as already visited to not include any of them later on.
690
- */
691
- function getParentBlock(position, visited) {
692
- const element = position.parent;
693
- const schema = element.root.document.model.schema;
694
- const ancestors = position.parent.getAncestors({ parentFirst: true, includeSelf: true });
695
- let hasParentLimit = false;
696
- const block = ancestors.find((element) => {
697
- // Stop searching after first parent node that is limit element.
698
- if (hasParentLimit) {
699
- return false;
700
- }
701
- hasParentLimit = schema.isLimit(element);
702
- return !hasParentLimit && isUnvisitedBlock(element, visited);
703
- });
704
- // Mark all ancestors of this position's parent, because find() might've stopped early and
705
- // the found block may be a child of another block.
706
- ancestors.forEach(element => visited.add(element));
707
- return block;
708
- }
709
- /**
710
- * Checks if the blocks is not nested in other block inside a range.
711
- */
712
- function isTopBlockInRange(block, range) {
713
- const parentBlock = findAncestorBlock(block);
714
- if (!parentBlock) {
715
- return true;
716
- }
717
- // Add loose flag to check as parentRange can be equal to range.
718
- const isParentInRange = range.containsRange(Range._createOn(parentBlock), true);
719
- return !isParentInRange;
720
- }
721
- /**
722
- * 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.
723
- * See [#11585](https://github.com/ckeditor/ckeditor5/issues/11585) for more details.
724
- *
725
- * ```xml
726
- * <paragraph>a[</paragraph> // This block will not be returned
727
- * <paragraph>b</paragraph>
728
- * <paragraph>c]</paragraph>
729
- * ```
730
- *
731
- * Collapsed selection is not affected by it:
732
- *
733
- * ```xml
734
- * <paragraph>a[]</paragraph> // This block will be returned
735
- * ```
736
- */
737
- function isStartBlockSelected(startBlock, range) {
738
- if (!startBlock) {
739
- return false;
740
- }
741
- if (range.isCollapsed || startBlock.isEmpty) {
742
- return true;
743
- }
744
- if (range.start.isTouching(Position._createAt(startBlock, startBlock.maxOffset))) {
745
- return false;
746
- }
747
- return isTopBlockInRange(startBlock, range);
748
- }
749
- /**
750
- * 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.
751
- * See [#984](https://github.com/ckeditor/ckeditor5-engine/issues/984) for more details.
752
- *
753
- * ```xml
754
- * <paragraph>[a</paragraph>
755
- * <paragraph>b</paragraph>
756
- * <paragraph>]c</paragraph> // this block will not be returned
757
- * ```
758
- *
759
- * Collapsed selection is not affected by it:
760
- *
761
- * ```xml
762
- * <paragraph>[]a</paragraph> // this block will be returned
763
- * ```
764
- */
765
- function isEndBlockSelected(endBlock, range) {
766
- if (!endBlock) {
767
- return false;
768
- }
769
- if (range.isCollapsed || endBlock.isEmpty) {
770
- return true;
771
- }
772
- if (range.end.isTouching(Position._createAt(endBlock, 0))) {
773
- return false;
774
- }
775
- return isTopBlockInRange(endBlock, range);
776
- }
777
- /**
778
- * Returns first ancestor block of a node.
779
- */
780
- function findAncestorBlock(node) {
781
- const schema = node.root.document.model.schema;
782
- let parent = node.parent;
783
- while (parent) {
784
- if (schema.isBlock(parent)) {
785
- return parent;
786
- }
787
- parent = parent.parent;
788
- }
789
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module engine/model/selection
7
+ */
8
+ import TypeCheckable from './typecheckable';
9
+ import Node from './node';
10
+ import Position from './position';
11
+ import Range from './range';
12
+ import { CKEditorError, EmitterMixin, isIterable } from '@ckeditor/ckeditor5-utils';
13
+ /**
14
+ * Selection is a set of {@link module:engine/model/range~Range ranges}. It has a direction specified by its
15
+ * {@link module:engine/model/selection~Selection#anchor anchor} and {@link module:engine/model/selection~Selection#focus focus}
16
+ * (it can be {@link module:engine/model/selection~Selection#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 default class Selection extends EmitterMixin(TypeCheckable) {
21
+ /**
22
+ * Creates a new selection instance based on the given {@link module:engine/model/selection~Selectable selectable}
23
+ * or creates an empty selection if no arguments were passed.
24
+ *
25
+ * ```ts
26
+ * // Creates empty selection without ranges.
27
+ * const selection = writer.createSelection();
28
+ *
29
+ * // Creates selection at the given range.
30
+ * const range = writer.createRange( start, end );
31
+ * const selection = writer.createSelection( range );
32
+ *
33
+ * // Creates selection at the given ranges
34
+ * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ];
35
+ * const selection = writer.createSelection( ranges );
36
+ *
37
+ * // Creates selection from the other selection.
38
+ * // Note: It doesn't copy selection attributes.
39
+ * const otherSelection = writer.createSelection();
40
+ * const selection = writer.createSelection( otherSelection );
41
+ *
42
+ * // Creates selection from the given document selection.
43
+ * // Note: It doesn't copy selection attributes.
44
+ * const documentSelection = model.document.selection;
45
+ * const selection = writer.createSelection( documentSelection );
46
+ *
47
+ * // Creates selection at the given position.
48
+ * const position = writer.createPositionFromPath( root, path );
49
+ * const selection = writer.createSelection( position );
50
+ *
51
+ * // Creates selection at the given offset in the given element.
52
+ * const paragraph = writer.createElement( 'paragraph' );
53
+ * const selection = writer.createSelection( paragraph, offset );
54
+ *
55
+ * // Creates a range inside an {@link module:engine/model/element~Element element} which starts before the
56
+ * // first child of that element and ends after the last child of that element.
57
+ * const selection = writer.createSelection( paragraph, 'in' );
58
+ *
59
+ * // Creates a range on an {@link module:engine/model/item~Item item} which starts before the item and ends
60
+ * // just after the item.
61
+ * const selection = writer.createSelection( paragraph, 'on' );
62
+ * ```
63
+ *
64
+ * Selection's constructor allow passing additional options (`'backward'`) as the last argument.
65
+ *
66
+ * ```ts
67
+ * // Creates backward selection.
68
+ * const selection = writer.createSelection( range, { backward: true } );
69
+ * ```
70
+ *
71
+ * @internal
72
+ */
73
+ constructor(...args) {
74
+ super();
75
+ /**
76
+ * Specifies whether the last added range was added as a backward or forward range.
77
+ */
78
+ this._lastRangeBackward = false;
79
+ /**
80
+ * List of attributes set on current selection.
81
+ */
82
+ this._attrs = new Map();
83
+ /** @internal */
84
+ this._ranges = [];
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~Range#start start} or
97
+ * {@link module:engine/model/range~Range#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 Range(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~Range#start start} position
193
+ * {@link module:engine/model/position~Position#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 Range(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~Range#end end} position
210
+ * {@link module:engine/model/position~Position#isAfter is after} end position of all other ranges (not to confuse with the range most
211
+ * 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 Range(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~Position#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~Position#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~Selectable 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 DocumentSelection( 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~Element 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~Item 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 Selection) {
310
+ this._setRanges(selectable.getRanges(), selectable.isBackward);
311
+ }
312
+ else if (selectable && typeof selectable.getRanges == 'function') {
313
+ // We assume that the selectable is a DocumentSelection.
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 Range) {
318
+ this._setRanges([selectable], !!options && !!options.backward);
319
+ }
320
+ else if (selectable instanceof Position) {
321
+ this._setRanges([new Range(selectable)]);
322
+ }
323
+ else if (selectable instanceof Node) {
324
+ const backward = !!options && !!options.backward;
325
+ let range;
326
+ if (placeOrOffset == 'in') {
327
+ range = Range._createIn(selectable);
328
+ }
329
+ else if (placeOrOffset == 'on') {
330
+ range = Range._createOn(selectable);
331
+ }
332
+ else if (placeOrOffset !== undefined) {
333
+ range = new Range(Position._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~TextProxy} instance was passed instead of
356
+ * a real {@link module:engine/model/text~Text}.
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~Selection#anchor} and
368
+ * {@link module:engine/model/selection~Selection#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 Range)) {
380
+ /**
381
+ * Selection range set to an object that is not an instance of {@link module:engine/model/range~Range}.
382
+ *
383
+ * Only {@link module:engine/model/range~Range} 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~Selection#focus} to the specified location.
407
+ *
408
+ * The location can be specified in the same form as
409
+ * {@link module:engine/model/writer~Writer#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~Item 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 = Position._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 Range(newFocus, anchor));
433
+ this._lastRangeBackward = true;
434
+ }
435
+ else {
436
+ this._pushRange(new Range(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~Element 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~Schema#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
+ for (const value of range.getWalker()) {
588
+ const block = value.item;
589
+ if (value.type == 'elementEnd' && isUnvisitedTopBlock(block, visited, range)) {
590
+ yield block;
591
+ }
592
+ }
593
+ const endBlock = getParentBlock(range.end, visited);
594
+ if (isEndBlockSelected(endBlock, range)) {
595
+ yield endBlock;
596
+ }
597
+ }
598
+ }
599
+ /**
600
+ * Checks whether the selection contains the entire content of the given element. This means that selection must start
601
+ * at a position {@link module:engine/model/position~Position#isTouching touching} the element's start and ends at position
602
+ * touching the element's end.
603
+ *
604
+ * By default, this method will check whether the entire content of the selection's current root is selected.
605
+ * Useful to check if e.g. the user has just pressed <kbd>Ctrl</kbd> + <kbd>A</kbd>.
606
+ */
607
+ containsEntireContent(element = this.anchor.root) {
608
+ const limitStartPosition = Position._createAt(element, 0);
609
+ const limitEndPosition = Position._createAt(element, 'end');
610
+ return limitStartPosition.isTouching(this.getFirstPosition()) &&
611
+ limitEndPosition.isTouching(this.getLastPosition());
612
+ }
613
+ /**
614
+ * Adds given range to internal {@link #_ranges ranges array}. Throws an error
615
+ * if given range is intersecting with any range that is already stored in this selection.
616
+ */
617
+ _pushRange(range) {
618
+ this._checkRange(range);
619
+ this._ranges.push(new Range(range.start, range.end));
620
+ }
621
+ /**
622
+ * Checks if given range intersects with ranges that are already in the selection. Throws an error if it does.
623
+ */
624
+ _checkRange(range) {
625
+ for (let i = 0; i < this._ranges.length; i++) {
626
+ if (range.isIntersecting(this._ranges[i])) {
627
+ /**
628
+ * Trying to add a range that intersects with another range in the selection.
629
+ *
630
+ * @error model-selection-range-intersects
631
+ * @param addedRange Range that was added to the selection.
632
+ * @param intersectingRange Range in the selection that intersects with `addedRange`.
633
+ */
634
+ throw new CKEditorError('model-selection-range-intersects', [this, range], { addedRange: range, intersectingRange: this._ranges[i] });
635
+ }
636
+ }
637
+ }
638
+ /**
639
+ * Replaces all the ranges by the given ones.
640
+ * Uses {@link #_popRange _popRange} and {@link #_pushRange _pushRange} to ensure proper ranges removal and addition.
641
+ */
642
+ _replaceAllRanges(ranges) {
643
+ this._removeAllRanges();
644
+ for (const range of ranges) {
645
+ this._pushRange(range);
646
+ }
647
+ }
648
+ /**
649
+ * Deletes ranges from internal range array. Uses {@link #_popRange _popRange} to
650
+ * ensure proper ranges removal.
651
+ */
652
+ _removeAllRanges() {
653
+ while (this._ranges.length > 0) {
654
+ this._popRange();
655
+ }
656
+ }
657
+ /**
658
+ * Removes most recently added range from the selection.
659
+ */
660
+ _popRange() {
661
+ this._ranges.pop();
662
+ }
663
+ }
664
+ // The magic of type inference using `is` method is centralized in `TypeCheckable` class.
665
+ // Proper overload would interfere with that.
666
+ Selection.prototype.is = function (type) {
667
+ return type === 'selection' || type === 'model:selection';
668
+ };
669
+ /**
670
+ * Checks whether the given element extends $block in the schema and has a parent (is not a root).
671
+ * Marks it as already visited.
672
+ */
673
+ function isUnvisitedBlock(element, visited) {
674
+ if (visited.has(element)) {
675
+ return false;
676
+ }
677
+ visited.add(element);
678
+ return element.root.document.model.schema.isBlock(element) && !!element.parent;
679
+ }
680
+ /**
681
+ * Checks if the given element is a $block was not previously visited and is a top block in a range.
682
+ */
683
+ function isUnvisitedTopBlock(element, visited, range) {
684
+ return isUnvisitedBlock(element, visited) && isTopBlockInRange(element, range);
685
+ }
686
+ /**
687
+ * Finds the lowest element in position's ancestors which is a block.
688
+ * It will search until first ancestor that is a limit element.
689
+ * Marks all ancestors as already visited to not include any of them later on.
690
+ */
691
+ function getParentBlock(position, visited) {
692
+ const element = position.parent;
693
+ const schema = element.root.document.model.schema;
694
+ const ancestors = position.parent.getAncestors({ parentFirst: true, includeSelf: true });
695
+ let hasParentLimit = false;
696
+ const block = ancestors.find((element) => {
697
+ // Stop searching after first parent node that is limit element.
698
+ if (hasParentLimit) {
699
+ return false;
700
+ }
701
+ hasParentLimit = schema.isLimit(element);
702
+ return !hasParentLimit && isUnvisitedBlock(element, visited);
703
+ });
704
+ // Mark all ancestors of this position's parent, because find() might've stopped early and
705
+ // the found block may be a child of another block.
706
+ ancestors.forEach(element => visited.add(element));
707
+ return block;
708
+ }
709
+ /**
710
+ * Checks if the blocks is not nested in other block inside a range.
711
+ */
712
+ function isTopBlockInRange(block, range) {
713
+ const parentBlock = findAncestorBlock(block);
714
+ if (!parentBlock) {
715
+ return true;
716
+ }
717
+ // Add loose flag to check as parentRange can be equal to range.
718
+ const isParentInRange = range.containsRange(Range._createOn(parentBlock), true);
719
+ return !isParentInRange;
720
+ }
721
+ /**
722
+ * 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.
723
+ * See [#11585](https://github.com/ckeditor/ckeditor5/issues/11585) for more details.
724
+ *
725
+ * ```xml
726
+ * <paragraph>a[</paragraph> // This block will not be returned
727
+ * <paragraph>b</paragraph>
728
+ * <paragraph>c]</paragraph>
729
+ * ```
730
+ *
731
+ * Collapsed selection is not affected by it:
732
+ *
733
+ * ```xml
734
+ * <paragraph>a[]</paragraph> // This block will be returned
735
+ * ```
736
+ */
737
+ function isStartBlockSelected(startBlock, range) {
738
+ if (!startBlock) {
739
+ return false;
740
+ }
741
+ if (range.isCollapsed || startBlock.isEmpty) {
742
+ return true;
743
+ }
744
+ if (range.start.isTouching(Position._createAt(startBlock, startBlock.maxOffset))) {
745
+ return false;
746
+ }
747
+ return isTopBlockInRange(startBlock, range);
748
+ }
749
+ /**
750
+ * 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.
751
+ * See [#984](https://github.com/ckeditor/ckeditor5-engine/issues/984) for more details.
752
+ *
753
+ * ```xml
754
+ * <paragraph>[a</paragraph>
755
+ * <paragraph>b</paragraph>
756
+ * <paragraph>]c</paragraph> // this block will not be returned
757
+ * ```
758
+ *
759
+ * Collapsed selection is not affected by it:
760
+ *
761
+ * ```xml
762
+ * <paragraph>[]a</paragraph> // this block will be returned
763
+ * ```
764
+ */
765
+ function isEndBlockSelected(endBlock, range) {
766
+ if (!endBlock) {
767
+ return false;
768
+ }
769
+ if (range.isCollapsed || endBlock.isEmpty) {
770
+ return true;
771
+ }
772
+ if (range.end.isTouching(Position._createAt(endBlock, 0))) {
773
+ return false;
774
+ }
775
+ return isTopBlockInRange(endBlock, range);
776
+ }
777
+ /**
778
+ * Returns first ancestor block of a node.
779
+ */
780
+ function findAncestorBlock(node) {
781
+ const schema = node.root.document.model.schema;
782
+ let parent = node.parent;
783
+ while (parent) {
784
+ if (schema.isBlock(parent)) {
785
+ return parent;
786
+ }
787
+ parent = parent.parent;
788
+ }
789
+ }