@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,509 +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/utils/deletecontent
7
- */
8
- import { ModelDocumentSelection } from '../documentselection.js';
9
- import { ModelLivePosition } from '../liveposition.js';
10
- import { ModelRange } from '../range.js';
11
- /**
12
- * Deletes content of the selection and merge siblings. The resulting selection is always collapsed.
13
- *
14
- * **Note:** Use {@link module:engine/model/model~Model#deleteContent} instead of this function.
15
- * This function is only exposed to be reusable in algorithms
16
- * which change the {@link module:engine/model/model~Model#deleteContent}
17
- * method's behavior.
18
- *
19
- * @param model The model in context of which the insertion should be performed.
20
- * @param selection Selection of which the content should be deleted.
21
- * @param options.leaveUnmerged Whether to merge elements after removing the content of the selection.
22
- *
23
- * For example `<heading>x[x</heading><paragraph>y]y</paragraph>` will become:
24
- *
25
- * * `<heading>x^y</heading>` with the option disabled (`leaveUnmerged == false`)
26
- * * `<heading>x^</heading><paragraph>y</paragraph>` with enabled (`leaveUnmerged == true`).
27
- *
28
- * Note: {@link module:engine/model/schema~ModelSchema#isObject object} and {@link module:engine/model/schema~ModelSchema#isLimit limit}
29
- * elements will not be merged.
30
- *
31
- * @param options.doNotResetEntireContent Whether to skip replacing the entire content with a
32
- * paragraph when the entire content was selected.
33
- *
34
- * For example `<heading>[x</heading><paragraph>y]</paragraph>` will become:
35
- *
36
- * * `<paragraph>^</paragraph>` with the option disabled (`doNotResetEntireContent == false`)
37
- * * `<heading>^</heading>` with enabled (`doNotResetEntireContent == true`).
38
- *
39
- * @param options.doNotAutoparagraph Whether to create a paragraph if after content deletion selection is moved
40
- * to a place where text cannot be inserted.
41
- *
42
- * For example `<paragraph>x</paragraph>[<imageBlock src="foo.jpg"></imageBlock>]` will become:
43
- *
44
- * * `<paragraph>x</paragraph><paragraph>[]</paragraph>` with the option disabled (`doNotAutoparagraph == false`)
45
- * * `<paragraph>x</paragraph>[]` with the option enabled (`doNotAutoparagraph == true`).
46
- *
47
- * If you use this option you need to make sure to handle invalid selections yourself or leave
48
- * them to the selection post-fixer (may not always work).
49
- *
50
- * **Note:** If there is no valid position for the selection, the paragraph will always be created:
51
- *
52
- * `[<imageBlock src="foo.jpg"></imageBlock>]` -> `<paragraph>[]</paragraph>`.
53
- *
54
- * @param options.doNotFixSelection Whether given selection-to-remove should be fixed if it ends at the beginning of an element.
55
- *
56
- * By default, `deleteContent()` will fix selection before performing a deletion, so that the selection does not end at the beginning of
57
- * an element. For example, selection `<heading>[Heading</heading><paragraph>]Some text.</paragraph>` will be treated as it was
58
- * `<heading>[Heading]</heading><paragraph>Some text.</paragraph>`. As a result, the elements will not get merged.
59
- *
60
- * If selection is as in example, visually, the next element (paragraph) is not selected and it may be confusing for the user that
61
- * the elements got merged. Selection is set up like this by browsers when a user triple-clicks on some text.
62
- *
63
- * However, in some cases, it is expected to remove content exactly as selected in the selection, without any fixing. In these cases,
64
- * this flag can be set to `true`, which will prevent fixing the selection.
65
- *
66
- * @internal
67
- */
68
- export function deleteContent(model, selection, options = {}) {
69
- if (selection.isCollapsed) {
70
- return;
71
- }
72
- const selRange = selection.getFirstRange();
73
- // If the selection is already removed, don't do anything.
74
- if (selRange.root.rootName == '$graveyard') {
75
- return;
76
- }
77
- const schema = model.schema;
78
- model.change(writer => {
79
- // 1. Replace the entire content with paragraph.
80
- // See: https://github.com/ckeditor/ckeditor5-engine/issues/1012#issuecomment-315017594.
81
- if (!options.doNotResetEntireContent && shouldEntireContentBeReplacedWithParagraph(schema, selection)) {
82
- replaceEntireContentWithParagraph(writer, selection);
83
- return;
84
- }
85
- // Collect attributes to copy in case of autoparagraphing.
86
- const attributesForAutoparagraph = {};
87
- if (!options.doNotAutoparagraph) {
88
- const selectedElement = selection.getSelectedElement();
89
- if (selectedElement) {
90
- Object.assign(attributesForAutoparagraph, schema.getAttributesWithProperty(selectedElement, 'copyOnReplace', true));
91
- }
92
- }
93
- // Get the live positions for the range adjusted to span only blocks selected from the user perspective.
94
- let startPosition, endPosition;
95
- if (!options.doNotFixSelection) {
96
- [startPosition, endPosition] = getLivePositionsForSelectedBlocks(selRange);
97
- }
98
- else {
99
- startPosition = ModelLivePosition.fromPosition(selRange.start, 'toPrevious');
100
- endPosition = ModelLivePosition.fromPosition(selRange.end, 'toNext');
101
- }
102
- // 2. Remove the content if there is any.
103
- if (!startPosition.isTouching(endPosition)) {
104
- writer.remove(writer.createRange(startPosition, endPosition));
105
- }
106
- // 3. Merge elements in the right branch to the elements in the left branch.
107
- // The only reasonable (in terms of data and selection correctness) case in which we need to do that is:
108
- //
109
- // <heading type=1>Fo[</heading><paragraph>]ar</paragraph> => <heading type=1>Fo^ar</heading>
110
- //
111
- // However, the algorithm supports also merging deeper structures (up to the depth of the shallower branch),
112
- // as it's hard to imagine what should actually be the default behavior. Usually, specific features will
113
- // want to override that behavior anyway.
114
- if (!options.leaveUnmerged) {
115
- mergeBranches(writer, startPosition, endPosition);
116
- // TMP this will be replaced with a postfixer.
117
- // We need to check and strip disallowed attributes in all nested nodes because after merge
118
- // some attributes could end up in a path where are disallowed.
119
- //
120
- // e.g. bold is disallowed for <H1>
121
- // <h1>Fo{o</h1><p>b}a<b>r</b><p> -> <h1>Fo{}a<b>r</b><h1> -> <h1>Fo{}ar<h1>.
122
- schema.removeDisallowedAttributes(startPosition.parent.getChildren(), writer);
123
- }
124
- collapseSelectionAt(writer, selection, startPosition);
125
- // 4. Add a paragraph to set selection in it.
126
- // Check if a text is allowed in the new container. If not, try to create a new paragraph (if it's allowed here).
127
- // If autoparagraphing is off, we assume that you know what you do so we leave the selection wherever it was.
128
- if (!options.doNotAutoparagraph && shouldAutoparagraph(schema, startPosition)) {
129
- insertParagraph(writer, startPosition, selection, attributesForAutoparagraph);
130
- }
131
- startPosition.detach();
132
- endPosition.detach();
133
- });
134
- }
135
- /**
136
- * Returns the live positions for the range adjusted to span only blocks selected from the user perspective. Example:
137
- *
138
- * ```
139
- * <heading1>[foo</heading1>
140
- * <paragraph>bar</paragraph>
141
- * <heading1>]abc</heading1> <-- this block is not considered as selected
142
- * ```
143
- *
144
- * This is the same behavior as in Selection#getSelectedBlocks() "special case".
145
- */
146
- function getLivePositionsForSelectedBlocks(range) {
147
- const model = range.root.document.model;
148
- const startPosition = range.start;
149
- let endPosition = range.end;
150
- // If the end of selection is at the start position of last block in the selection, then
151
- // shrink it to not include that trailing block. Note that this should happen only for not empty selection.
152
- if (model.hasContent(range, { ignoreMarkers: true })) {
153
- const endBlock = getParentBlock(endPosition);
154
- if (endBlock && endPosition.isTouching(model.createPositionAt(endBlock, 0))) {
155
- // Create forward selection as a probe to find a valid position after excluding last block from the range.
156
- const selection = model.createSelection(range);
157
- // Modify the forward selection in backward direction to shrink it and remove first position of following block from it.
158
- // This is how modifySelection works and here we are making use of it.
159
- model.modifySelection(selection, { direction: 'backward' });
160
- const newEndPosition = selection.getLastPosition();
161
- // For such a model and selection:
162
- // <paragraph>A[</paragraph><imageBlock></imageBlock><paragraph>]B</paragraph>
163
- //
164
- // After modifySelection(), we would end up with this:
165
- // <paragraph>A[</paragraph>]<imageBlock></imageBlock><paragraph>B</paragraph>
166
- //
167
- // So we need to check if there is no content in the skipped range (because we want to include the <imageBlock>).
168
- const skippedRange = model.createRange(newEndPosition, endPosition);
169
- if (!model.hasContent(skippedRange, { ignoreMarkers: true })) {
170
- endPosition = newEndPosition;
171
- }
172
- }
173
- }
174
- return [
175
- ModelLivePosition.fromPosition(startPosition, 'toPrevious'),
176
- ModelLivePosition.fromPosition(endPosition, 'toNext')
177
- ];
178
- }
179
- /**
180
- * Finds the lowest element in position's ancestors which is a block.
181
- * Returns null if a limit element is encountered before reaching a block element.
182
- */
183
- function getParentBlock(position) {
184
- const element = position.parent;
185
- const schema = element.root.document.model.schema;
186
- const ancestors = element.getAncestors({ parentFirst: true, includeSelf: true });
187
- for (const element of ancestors) {
188
- if (schema.isLimit(element)) {
189
- return null;
190
- }
191
- if (schema.isBlock(element)) {
192
- return element;
193
- }
194
- }
195
- }
196
- /**
197
- * This function is a result of reaching the Ballmer's peak for just the right amount of time.
198
- * Even I had troubles documenting it after a while and after reading it again I couldn't believe that it really works.
199
- */
200
- function mergeBranches(writer, startPosition, endPosition) {
201
- const model = writer.model;
202
- // Verify if there is a need and possibility to merge.
203
- if (!checkShouldMerge(writer.model.schema, startPosition, endPosition)) {
204
- return;
205
- }
206
- // If the start element on the common ancestor level is empty, and the end element on the same level is not empty
207
- // then merge those to the right element so that it's properties are preserved (name, attributes).
208
- // Because of OT merging is used instead of removing elements.
209
- //
210
- // Merge left:
211
- // <heading1>foo[</heading1> -> <heading1>foo[]bar</heading1>
212
- // <paragraph>]bar</paragraph> -> --^
213
- //
214
- // Merge right:
215
- // <heading1>[</heading1> ->
216
- // <paragraph>]bar</paragraph> -> <paragraph>[]bar</paragraph>
217
- //
218
- // Merge left:
219
- // <blockQuote> -> <blockQuote>
220
- // <heading1>foo[</heading1> -> <heading1>foo[]bar</heading1>
221
- // <paragraph>]bar</paragraph> -> --^
222
- // </blockQuote> -> </blockQuote>
223
- //
224
- // Merge right:
225
- // <blockQuote> -> <blockQuote>
226
- // <heading1>[</heading1> ->
227
- // <paragraph>]bar</paragraph> -> <paragraph>[]bar</paragraph>
228
- // </blockQuote> -> </blockQuote>
229
- // Merging should not go deeper than common ancestor.
230
- const [startAncestor, endAncestor] = getAncestorsJustBelowCommonAncestor(startPosition, endPosition);
231
- // Branches can't be merged if one of the positions is directly inside a common ancestor.
232
- //
233
- // Example:
234
- // <blockQuote>
235
- // <paragraph>[foo</paragraph>]
236
- // <table> ... </table>
237
- // <blockQuote>
238
- //
239
- if (!startAncestor || !endAncestor) {
240
- return;
241
- }
242
- if (!model.hasContent(startAncestor, { ignoreMarkers: true }) && model.hasContent(endAncestor, { ignoreMarkers: true })) {
243
- mergeBranchesRight(writer, startPosition, endPosition, startAncestor.parent);
244
- }
245
- else {
246
- mergeBranchesLeft(writer, startPosition, endPosition, startAncestor.parent);
247
- }
248
- }
249
- /**
250
- * Merging blocks to the left (properties of the left block are preserved).
251
- * Simple example:
252
- *
253
- * ```
254
- * <heading1>foo[</heading1> -> <heading1>foo[bar</heading1>]
255
- * <paragraph>]bar</paragraph> -> --^
256
- * ```
257
- *
258
- * Nested example:
259
- *
260
- * ```
261
- * <blockQuote> -> <blockQuote>
262
- * <heading1>foo[</heading1> -> <heading1>foo[bar</heading1>
263
- * </blockQuote> -> </blockQuote>] ^
264
- * <blockBlock> -> |
265
- * <paragraph>]bar</paragraph> -> ---
266
- * </blockBlock> ->
267
- * ```
268
- */
269
- function mergeBranchesLeft(writer, startPosition, endPosition, commonAncestor) {
270
- const startElement = startPosition.parent;
271
- const endElement = endPosition.parent;
272
- // Merging reached the common ancestor element, stop here.
273
- if (startElement == commonAncestor || endElement == commonAncestor) {
274
- return;
275
- }
276
- // Remember next positions to merge in next recursive step (also used as modification points pointers).
277
- startPosition = writer.createPositionAfter(startElement);
278
- endPosition = writer.createPositionBefore(endElement);
279
- // Move endElement just after startElement if they aren't siblings.
280
- if (!endPosition.isEqual(startPosition)) {
281
- //
282
- // <blockQuote> -> <blockQuote>
283
- // <heading1>foo[</heading1> -> <heading1>foo</heading1>[<paragraph>bar</paragraph>
284
- // </blockQuote> -> </blockQuote> ^
285
- // <blockBlock> -> <blockBlock> |
286
- // <paragraph>]bar</paragraph> -> ] ---
287
- // </blockBlock> -> </blockBlock>
288
- //
289
- writer.insert(endElement, startPosition);
290
- }
291
- // Merge two siblings (nodes on sides of startPosition):
292
- //
293
- // <blockQuote> -> <blockQuote>
294
- // <heading1>foo</heading1>[<paragraph>bar</paragraph> -> <heading1>foo[bar</heading1>
295
- // </blockQuote> -> </blockQuote>
296
- // <blockBlock> -> <blockBlock>
297
- // ] -> ]
298
- // </blockBlock> -> </blockBlock>
299
- //
300
- // Or in simple case (without moving elements in above if):
301
- // <heading1>foo</heading1>[<paragraph>bar</paragraph>] -> <heading1>foo[bar</heading1>]
302
- //
303
- writer.merge(startPosition);
304
- // Remove empty end ancestors:
305
- //
306
- // <blockQuote> -> <blockQuote>
307
- // <heading1>foo[bar</heading1> -> <heading1>foo[bar</heading1>
308
- // </blockQuote> -> </blockQuote>
309
- // <blockBlock> ->
310
- // ] -> ]
311
- // </blockBlock> ->
312
- //
313
- while (endPosition.parent.isEmpty) {
314
- const parentToRemove = endPosition.parent;
315
- endPosition = writer.createPositionBefore(parentToRemove);
316
- writer.remove(parentToRemove);
317
- }
318
- // Verify if there is a need and possibility to merge next level.
319
- if (!checkShouldMerge(writer.model.schema, startPosition, endPosition)) {
320
- return;
321
- }
322
- // Continue merging next level (blockQuote with blockBlock in the examples above if it would not be empty and got removed).
323
- mergeBranchesLeft(writer, startPosition, endPosition, commonAncestor);
324
- }
325
- /**
326
- * Merging blocks to the right (properties of the right block are preserved).
327
- * Simple example:
328
- *
329
- * ```
330
- * <heading1>foo[</heading1> -> --v
331
- * <paragraph>]bar</paragraph> -> [<paragraph>foo]bar</paragraph>
332
- * ```
333
- *
334
- * Nested example:
335
- *
336
- * ```
337
- * <blockQuote> ->
338
- * <heading1>foo[</heading1> -> ---
339
- * </blockQuote> -> |
340
- * <blockBlock> -> [<blockBlock> v
341
- * <paragraph>]bar</paragraph> -> <paragraph>foo]bar</paragraph>
342
- * </blockBlock> -> </blockBlock>
343
- * ```
344
- */
345
- function mergeBranchesRight(writer, startPosition, endPosition, commonAncestor) {
346
- const startElement = startPosition.parent;
347
- const endElement = endPosition.parent;
348
- // Merging reached the common ancestor element, stop here.
349
- if (startElement == commonAncestor || endElement == commonAncestor) {
350
- return;
351
- }
352
- // Remember next positions to merge in next recursive step (also used as modification points pointers).
353
- startPosition = writer.createPositionAfter(startElement);
354
- endPosition = writer.createPositionBefore(endElement);
355
- // Move startElement just before endElement if they aren't siblings.
356
- if (!endPosition.isEqual(startPosition)) {
357
- //
358
- // <blockQuote> -> <blockQuote>
359
- // <heading1>foo[</heading1> -> [ ---
360
- // </blockQuote> -> </blockQuote> |
361
- // <blockBlock> -> <blockBlock> v
362
- // <paragraph>]bar</paragraph> -> <heading1>foo</heading1>]<paragraph>bar</paragraph>
363
- // </blockBlock> -> </blockBlock>
364
- //
365
- writer.insert(startElement, endPosition);
366
- }
367
- // Remove empty end ancestors:
368
- //
369
- // <blockQuote> ->
370
- // [ -> [
371
- // </blockQuote> ->
372
- // <blockBlock> -> <blockBlock>
373
- // <heading1>foo</heading1>]<paragraph>bar</paragraph> -> <heading1>foo</heading1>]<paragraph>bar</paragraph>
374
- // </blockBlock> -> </blockBlock>
375
- //
376
- while (startPosition.parent.isEmpty) {
377
- const parentToRemove = startPosition.parent;
378
- startPosition = writer.createPositionBefore(parentToRemove);
379
- writer.remove(parentToRemove);
380
- }
381
- // Update endPosition after inserting and removing elements.
382
- endPosition = writer.createPositionBefore(endElement);
383
- // Merge right two siblings (nodes on sides of endPosition):
384
- // ->
385
- // [ -> [
386
- // ->
387
- // <blockBlock> -> <blockBlock>
388
- // <heading1>foo</heading1>]<paragraph>bar</paragraph> -> <paragraph>foo]bar</paragraph>
389
- // </blockBlock> -> </blockBlock>
390
- //
391
- // Or in simple case (without moving elements in above if):
392
- // [<heading1>foo</heading1>]<paragraph>bar</paragraph> -> [<heading1>foo]bar</heading1>
393
- //
394
- mergeRight(writer, endPosition);
395
- // Verify if there is a need and possibility to merge next level.
396
- if (!checkShouldMerge(writer.model.schema, startPosition, endPosition)) {
397
- return;
398
- }
399
- // Continue merging next level (blockQuote with blockBlock in the examples above if it would not be empty and got removed).
400
- mergeBranchesRight(writer, startPosition, endPosition, commonAncestor);
401
- }
402
- /**
403
- * There is no right merge operation so we need to simulate it.
404
- */
405
- function mergeRight(writer, position) {
406
- const startElement = position.nodeBefore;
407
- const endElement = position.nodeAfter;
408
- if (startElement.name != endElement.name) {
409
- writer.rename(startElement, endElement.name);
410
- }
411
- writer.clearAttributes(startElement);
412
- writer.setAttributes(Object.fromEntries(endElement.getAttributes()), startElement);
413
- writer.merge(position);
414
- }
415
- /**
416
- * Verifies if merging is needed and possible. It's not needed if both positions are in the same element
417
- * and it's not possible if some element is a limit or the range crosses a limit element.
418
- */
419
- function checkShouldMerge(schema, startPosition, endPosition) {
420
- const startElement = startPosition.parent;
421
- const endElement = endPosition.parent;
422
- // If both positions ended up in the same parent, then there's nothing more to merge:
423
- // <$root><p>x[</p><p>]y</p></$root> => <$root><p>xy</p>[]</$root>
424
- if (startElement == endElement) {
425
- return false;
426
- }
427
- // If one of the positions is a limit element, then there's nothing to merge because we don't want to cross the limit boundaries.
428
- if (schema.isLimit(startElement) || schema.isLimit(endElement)) {
429
- return false;
430
- }
431
- // Check if operations we'll need to do won't need to cross object or limit boundaries.
432
- // E.g., we can't merge endElement into startElement in this case:
433
- // <limit><startElement>x[</startElement></limit><endElement>]</endElement>
434
- return isCrossingLimitElement(startPosition, endPosition, schema);
435
- }
436
- /**
437
- * Returns the elements that are the ancestors of the provided positions that are direct children of the common ancestor.
438
- */
439
- function getAncestorsJustBelowCommonAncestor(positionA, positionB) {
440
- const ancestorsA = positionA.getAncestors();
441
- const ancestorsB = positionB.getAncestors();
442
- let i = 0;
443
- while (ancestorsA[i] && ancestorsA[i] == ancestorsB[i]) {
444
- i++;
445
- }
446
- return [ancestorsA[i], ancestorsB[i]];
447
- }
448
- function shouldAutoparagraph(schema, position) {
449
- const isTextAllowed = schema.checkChild(position, '$text');
450
- const isParagraphAllowed = schema.checkChild(position, 'paragraph');
451
- return !isTextAllowed && isParagraphAllowed;
452
- }
453
- /**
454
- * Check if parents of two positions can be merged by checking if there are no limit/object
455
- * boundaries between those two positions.
456
- *
457
- * E.g. in <bQ><p>x[]</p></bQ><widget><caption>{}</caption></widget>
458
- * we'll check <p>, <bQ>, <widget> and <caption>.
459
- * Usually, widget and caption are marked as objects/limits in the schema, so in this case merging will be blocked.
460
- */
461
- function isCrossingLimitElement(leftPos, rightPos, schema) {
462
- const rangeToCheck = new ModelRange(leftPos, rightPos);
463
- for (const value of rangeToCheck.getWalker()) {
464
- if (schema.isLimit(value.item)) {
465
- return false;
466
- }
467
- }
468
- return true;
469
- }
470
- function insertParagraph(writer, position, selection, attributes = {}) {
471
- const paragraph = writer.createElement('paragraph');
472
- writer.model.schema.setAllowedAttributes(paragraph, attributes, writer);
473
- writer.insert(paragraph, position);
474
- collapseSelectionAt(writer, selection, writer.createPositionAt(paragraph, 0));
475
- }
476
- function replaceEntireContentWithParagraph(writer, selection) {
477
- const limitElement = writer.model.schema.getLimitElement(selection);
478
- writer.remove(writer.createRangeIn(limitElement));
479
- insertParagraph(writer, writer.createPositionAt(limitElement, 0), selection);
480
- }
481
- /**
482
- * We want to replace the entire content with a paragraph when:
483
- * * the entire content is selected,
484
- * * selection contains at least two elements,
485
- * * whether the paragraph is allowed in schema in the common ancestor.
486
- */
487
- function shouldEntireContentBeReplacedWithParagraph(schema, selection) {
488
- const limitElement = schema.getLimitElement(selection);
489
- if (!selection.containsEntireContent(limitElement)) {
490
- return false;
491
- }
492
- const range = selection.getFirstRange();
493
- if (range.start.parent == range.end.parent) {
494
- return false;
495
- }
496
- return schema.checkChild(limitElement, 'paragraph');
497
- }
498
- /**
499
- * Helper function that sets the selection. Depending whether given `selection` is a document selection or not,
500
- * uses a different method to set it.
501
- */
502
- function collapseSelectionAt(writer, selection, positionOrRange) {
503
- if (selection instanceof ModelDocumentSelection) {
504
- writer.setSelection(positionOrRange);
505
- }
506
- else {
507
- selection.setTo(positionOrRange);
508
- }
509
- }
@@ -1,126 +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/utils/getselectedcontent
7
- */
8
- /**
9
- * Gets a clone of the selected content.
10
- *
11
- * For example, for the following selection:
12
- *
13
- * ```html
14
- * <p>x</p><quote><p>y</p><h>fir[st</h></quote><p>se]cond</p><p>z</p>
15
- * ```
16
- *
17
- * It will return a document fragment with such a content:
18
- *
19
- * ```html
20
- * <quote><h>st</h></quote><p>se</p>
21
- * ```
22
- *
23
- * @param model The model in context of which the selection modification should be performed.
24
- * @param selection The selection of which content will be returned.
25
- * @internal
26
- */
27
- export function getSelectedContent(model, selection) {
28
- return model.change(writer => {
29
- const frag = writer.createDocumentFragment();
30
- const range = selection.getFirstRange();
31
- if (!range || range.isCollapsed) {
32
- return frag;
33
- }
34
- const root = range.start.root;
35
- const commonPath = range.start.getCommonPath(range.end);
36
- const commonParent = root.getNodeByPath(commonPath);
37
- // ## 1st step
38
- //
39
- // First, we'll clone a fragment represented by a minimal flat range
40
- // containing the original range to be cloned.
41
- // E.g. let's consider such a range:
42
- //
43
- // <p>x</p><quote><p>y</p><h>fir[st</h></quote><p>se]cond</p><p>z</p>
44
- //
45
- // A minimal flat range containing this one is:
46
- //
47
- // <p>x</p>[<quote><p>y</p><h>first</h></quote><p>second</p>]<p>z</p>
48
- //
49
- // We can easily clone this structure, preserving e.g. the <quote> element.
50
- let flatSubtreeRange;
51
- if (range.start.parent == range.end.parent) {
52
- // The original range is flat, so take it.
53
- flatSubtreeRange = range;
54
- }
55
- else {
56
- flatSubtreeRange = writer.createRange(writer.createPositionAt(commonParent, range.start.path[commonPath.length]), writer.createPositionAt(commonParent, range.end.path[commonPath.length] + 1));
57
- }
58
- const howMany = flatSubtreeRange.end.offset - flatSubtreeRange.start.offset;
59
- // Clone the whole contents.
60
- for (const item of flatSubtreeRange.getItems({ shallow: true })) {
61
- if (item.is('$textProxy')) {
62
- writer.appendText(item.data, item.getAttributes(), frag);
63
- }
64
- else {
65
- writer.append(writer.cloneElement(item, true), frag);
66
- }
67
- }
68
- // ## 2nd step
69
- //
70
- // If the original range wasn't flat, then we need to remove the excess nodes from the both ends of the cloned fragment.
71
- //
72
- // For example, for the range shown in the 1st step comment, we need to remove these pieces:
73
- //
74
- // <quote>[<p>y</p>]<h>[fir]st</h></quote><p>se[cond]</p>
75
- //
76
- // So this will be the final copied content:
77
- //
78
- // <quote><h>st</h></quote><p>se</p>
79
- //
80
- // In order to do that, we remove content from these two ranges:
81
- //
82
- // [<quote><p>y</p><h>fir]st</h></quote><p>se[cond</p>]
83
- if (flatSubtreeRange != range) {
84
- // Find the position of the original range in the cloned fragment.
85
- const newRange = range._getTransformedByMove(flatSubtreeRange.start, writer.createPositionAt(frag, 0), howMany)[0];
86
- const leftExcessRange = writer.createRange(writer.createPositionAt(frag, 0), newRange.start);
87
- const rightExcessRange = writer.createRange(newRange.end, writer.createPositionAt(frag, 'end'));
88
- removeRangeContent(rightExcessRange, writer);
89
- removeRangeContent(leftExcessRange, writer);
90
- }
91
- return frag;
92
- });
93
- }
94
- // After https://github.com/ckeditor/ckeditor5-engine/issues/690 is fixed,
95
- // this function will, most likely, be able to rewritten using getMinimalFlatRanges().
96
- function removeRangeContent(range, writer) {
97
- const parentsToCheck = [];
98
- Array.from(range.getItems({ direction: 'backward' }))
99
- // We should better store ranges because text proxies will lose integrity
100
- // with the text nodes when we'll start removing content.
101
- .map(item => writer.createRangeOn(item))
102
- // Filter only these items which are fully contained in the passed range.
103
- //
104
- // E.g. for the following range: [<quote><p>y</p><h>fir]st</h>
105
- // the walker will return the entire <h> element, when only the "fir" item inside it is fully contained.
106
- .filter(itemRange => {
107
- // We should be able to use Range.containsRange, but https://github.com/ckeditor/ckeditor5-engine/issues/691.
108
- const contained = (itemRange.start.isAfter(range.start) || itemRange.start.isEqual(range.start)) &&
109
- (itemRange.end.isBefore(range.end) || itemRange.end.isEqual(range.end));
110
- return contained;
111
- })
112
- .forEach(itemRange => {
113
- parentsToCheck.push(itemRange.start.parent);
114
- writer.remove(itemRange);
115
- });
116
- // Remove ancestors of the removed items if they turned to be empty now
117
- // (their whole content was contained in the range).
118
- parentsToCheck.forEach(parentToCheck => {
119
- let parent = parentToCheck;
120
- while (parent.parent && parent.isEmpty) {
121
- const removeRange = writer.createRangeOn(parent);
122
- parent = parent.parent;
123
- writer.remove(removeRange);
124
- }
125
- });
126
- }