@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,135 +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
- import { CKEditorError, first } from '@ckeditor/ckeditor5-utils';
6
- /**
7
- * Inserts an {@glink framework/deep-dive/schema#object-elements object element} at a specific position in the editor content.
8
- *
9
- * **Note:** Use {@link module:engine/model/model~Model#insertObject} instead of this function.
10
- * This function is only exposed to be reusable in algorithms which change the {@link module:engine/model/model~Model#insertObject}
11
- * method's behavior.
12
- *
13
- * **Note**: For more documentation and examples, see {@link module:engine/model/model~Model#insertObject}.
14
- *
15
- * @param model The model in context of which the insertion should be performed.
16
- * @param object An object to be inserted into the model document.
17
- * @param selectable A selectable where the content should be inserted. If not specified, the current
18
- * {@link module:engine/model/document~ModelDocument#selection document selection} will be used instead.
19
- * @param options Additional options.
20
- * @param options.findOptimalPosition An option that, when set, adjusts the insertion position (relative to
21
- * `selectable` and `placeOrOffset`) so that the content of `selectable` is not split upon insertion (a.k.a. non-destructive insertion).
22
- * * When `'auto'`, the algorithm will decide whether to insert the object before or after `selectable` to avoid content splitting.
23
- * * When `'before'`, the closest position before `selectable` will be used that will not result in content splitting.
24
- * * When `'after'`, the closest position after `selectable` will be used that will not result in content splitting.
25
- *
26
- * Note that this option works only for block objects. Inline objects are inserted into text and do not split blocks.
27
- * @param options.setSelection An option that, when set, moves the
28
- * {@link module:engine/model/document~ModelDocument#selection document selection} after inserting the object.
29
- * * When `'on'`, the document selection will be set on the inserted object.
30
- * * When `'after'`, the document selection will move to the closest text node after the inserted object. If there is no
31
- * such text node, a paragraph will be created and the document selection will be moved inside it.
32
- * @returns A range which contains all the performed changes. This is a range that, if removed,
33
- * would return the model to the state before the insertion. If no changes were preformed by `insertObject()`, returns a range collapsed
34
- * at the insertion position.
35
- */
36
- export function insertObject(model, object, selectable, options = {}) {
37
- if (!model.schema.isObject(object)) {
38
- /**
39
- * Tried to insert an element with {@link module:engine/model/utils/insertobject insertObject()} function
40
- * that is not defined as an object in schema.
41
- * See {@link module:engine/model/schema~ModelSchemaItemDefinition#isObject `SchemaItemDefinition`}.
42
- * If you want to insert content that is not an object you might want to use
43
- * {@link module:engine/model/utils/insertcontent insertContent()} function.
44
- * @error insertobject-element-not-an-object
45
- */
46
- throw new CKEditorError('insertobject-element-not-an-object', model, { object });
47
- }
48
- // Normalize selectable to a selection instance.
49
- const originalSelection = selectable ? selectable : model.document.selection;
50
- // Adjust the insertion selection.
51
- let insertionSelection = originalSelection;
52
- if (options.findOptimalPosition && model.schema.isBlock(object)) {
53
- insertionSelection = model.createSelection(model.schema.findOptimalInsertionRange(originalSelection, options.findOptimalPosition));
54
- }
55
- // Collect attributes to be copied on the inserted object.
56
- const firstSelectedBlock = first(originalSelection.getSelectedBlocks());
57
- const attributesToCopy = {};
58
- if (firstSelectedBlock) {
59
- Object.assign(attributesToCopy, model.schema.getAttributesWithProperty(firstSelectedBlock, 'copyOnReplace', true));
60
- }
61
- return model.change(writer => {
62
- // Remove the selected content to find out what the parent of the inserted object would be.
63
- // It would be removed inside model.insertContent() anyway.
64
- if (!insertionSelection.isCollapsed) {
65
- model.deleteContent(insertionSelection, { doNotAutoparagraph: true });
66
- }
67
- let elementToInsert = object;
68
- const insertionPositionParent = insertionSelection.anchor.parent;
69
- const context = model.schema.createContext(insertionPositionParent);
70
- // Auto-paragraphing of an inline objects.
71
- if (!model.schema.checkChild(context, object) &&
72
- model.schema.checkChild(context, 'paragraph') &&
73
- model.schema.checkChild(context.push('paragraph'), object)) {
74
- elementToInsert = writer.createElement('paragraph');
75
- writer.insert(object, elementToInsert);
76
- }
77
- // Apply attributes that are allowed on the inserted object (or paragraph if autoparagraphed).
78
- model.schema.setAllowedAttributes(elementToInsert, attributesToCopy, writer);
79
- // Insert the prepared content at the optionally adjusted selection.
80
- const affectedRange = model.insertContent(elementToInsert, insertionSelection);
81
- // Nothing got inserted.
82
- if (affectedRange.isCollapsed) {
83
- return affectedRange;
84
- }
85
- if (options.setSelection) {
86
- updateSelection(writer, object, options.setSelection, attributesToCopy);
87
- }
88
- return affectedRange;
89
- });
90
- }
91
- /**
92
- * Updates document selection based on given `place` parameter in relation to `contextElement` element.
93
- *
94
- * @param writer An instance of the model writer.
95
- * @param contextElement An element to set the attributes on.
96
- * @param place The place where selection should be set in relation to the `contextElement` element.
97
- * Value `on` will set selection on the passed `contextElement`. Value `after` will set selection after `contextElement`.
98
- * @param attributes Attributes keys and values to set on a paragraph that this function can create when
99
- * `place` parameter is equal to `after` but there is no element with `$text` node to set selection in.
100
- */
101
- function updateSelection(writer, contextElement, place, paragraphAttributes) {
102
- const model = writer.model;
103
- if (place == 'on') {
104
- writer.setSelection(contextElement, 'on');
105
- return;
106
- }
107
- if (place != 'after') {
108
- /**
109
- * The unsupported `options.setSelection` parameter was passed
110
- * to the {@link module:engine/model/utils/insertobject insertObject()} function.
111
- * Check the {@link module:engine/model/utils/insertobject insertObject()} API documentation for allowed
112
- * `options.setSelection` parameter values.
113
- *
114
- * @error insertobject-invalid-place-parameter-value
115
- */
116
- throw new CKEditorError('insertobject-invalid-place-parameter-value', model);
117
- }
118
- let nextElement = contextElement.nextSibling;
119
- if (model.schema.isInline(contextElement)) {
120
- writer.setSelection(contextElement, 'after');
121
- return;
122
- }
123
- // Check whether an element next to the inserted element is defined and can contain a text.
124
- const canSetSelection = nextElement && model.schema.checkChild(nextElement, '$text');
125
- // If the element is missing, but a paragraph could be inserted next to the element, let's add it.
126
- if (!canSetSelection && model.schema.checkChild(contextElement.parent, 'paragraph')) {
127
- nextElement = writer.createElement('paragraph');
128
- model.schema.setAllowedAttributes(nextElement, paragraphAttributes, writer);
129
- model.insertContent(nextElement, writer.createPositionAfter(contextElement));
130
- }
131
- // Put the selection inside the element, at the beginning.
132
- if (nextElement) {
133
- writer.setSelection(nextElement, 0);
134
- }
135
- }
@@ -1,187 +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/modifyselection
7
- */
8
- import { ModelDocumentSelection } from '../documentselection.js';
9
- import { ModelPosition } from '../position.js';
10
- import { ModelRange } from '../range.js';
11
- import { ModelTreeWalker } from '../treewalker.js';
12
- import { isInsideSurrogatePair, isInsideCombinedSymbol, isInsideEmojiSequence } from '@ckeditor/ckeditor5-utils';
13
- const wordBoundaryCharacters = ' ,.?!:;"-()';
14
- /**
15
- * Modifies the selection. Currently, the supported modifications are:
16
- *
17
- * * Extending. The selection focus is moved in the specified `options.direction` with a step specified in `options.unit`.
18
- * Possible values for `unit` are:
19
- * * `'character'` (default) - moves selection by one user-perceived character. In most cases this means moving by one
20
- * character in `String` sense. However, unicode also defines "combing marks". These are special symbols, that combines
21
- * with a symbol before it ("base character") to create one user-perceived character. For example, `q̣̇` is a normal
22
- * letter `q` with two "combining marks": upper dot (`Ux0307`) and lower dot (`Ux0323`). For most actions, i.e. extending
23
- * selection by one position, it is correct to include both "base character" and all of it's "combining marks". That is
24
- * why `'character'` value is most natural and common method of modifying selection.
25
- * * `'codePoint'` - moves selection by one unicode code point. In contrary to, `'character'` unit, this will insert
26
- * selection between "base character" and "combining mark", because "combining marks" have their own unicode code points.
27
- * However, for technical reasons, unicode code points with values above `UxFFFF` are represented in native `String` by
28
- * two characters, called "surrogate pairs". Halves of "surrogate pairs" have a meaning only when placed next to each other.
29
- * For example `𨭎` is represented in `String` by `\uD862\uDF4E`. Both `\uD862` and `\uDF4E` do not have any meaning
30
- * outside the pair (are rendered as ? when alone). Position between them would be incorrect. In this case, selection
31
- * extension will include whole "surrogate pair".
32
- * * `'word'` - moves selection by a whole word.
33
- *
34
- * **Note:** if you extend a forward selection in a backward direction you will in fact shrink it.
35
- *
36
- * **Note:** Use {@link module:engine/model/model~Model#modifySelection} instead of this function.
37
- * This function is only exposed to be reusable in algorithms
38
- * which change the {@link module:engine/model/model~Model#modifySelection}
39
- * method's behavior.
40
- *
41
- * @param model The model in context of which the selection modification should be performed.
42
- * @param selection The selection to modify.
43
- * @param options.direction The direction in which the selection should be modified. Default 'forward'.
44
- * @param options.unit The unit by which selection should be modified. Default 'character'.
45
- * @param options.treatEmojiAsSingleUnit Whether multi-characer emoji sequences should be handled as single unit.
46
- * @internal
47
- */
48
- export function modifySelection(model, selection, options = {}) {
49
- const schema = model.schema;
50
- const isForward = options.direction != 'backward';
51
- const unit = options.unit ? options.unit : 'character';
52
- const treatEmojiAsSingleUnit = !!options.treatEmojiAsSingleUnit;
53
- const focus = selection.focus;
54
- const walker = new ModelTreeWalker({
55
- boundaries: getSearchRange(focus, isForward),
56
- singleCharacters: true,
57
- direction: isForward ? 'forward' : 'backward'
58
- });
59
- const data = { walker, schema, isForward, unit, treatEmojiAsSingleUnit };
60
- let next;
61
- while ((next = walker.next())) {
62
- if (next.done) {
63
- return;
64
- }
65
- const position = tryExtendingTo(data, next.value);
66
- if (position) {
67
- if (selection instanceof ModelDocumentSelection) {
68
- model.change(writer => {
69
- writer.setSelectionFocus(position);
70
- });
71
- }
72
- else {
73
- selection.setFocus(position);
74
- }
75
- return;
76
- }
77
- }
78
- }
79
- /**
80
- * Checks whether the selection can be extended to the the walker's next value (next position).
81
- */
82
- function tryExtendingTo(data, value) {
83
- const { isForward, walker, unit, schema, treatEmojiAsSingleUnit } = data;
84
- const { type, item, nextPosition } = value;
85
- // If found text, we can certainly put the focus in it. Let's just find a correct position
86
- // based on the unit.
87
- if (type == 'text') {
88
- if (data.unit === 'word') {
89
- return getCorrectWordBreakPosition(walker, isForward);
90
- }
91
- return getCorrectPosition(walker, unit, treatEmojiAsSingleUnit);
92
- }
93
- // Entering an element.
94
- if (type == (isForward ? 'elementStart' : 'elementEnd')) {
95
- // If it's a selectable, we can select it now.
96
- if (schema.isSelectable(item)) {
97
- return ModelPosition._createAt(item, isForward ? 'after' : 'before');
98
- }
99
- // If text allowed on this position, extend to this place.
100
- if (schema.checkChild(nextPosition, '$text')) {
101
- return nextPosition;
102
- }
103
- }
104
- // Leaving an element.
105
- else {
106
- // If leaving a limit element, stop.
107
- if (schema.isLimit(item)) {
108
- // NOTE: Fast-forward the walker until the end.
109
- walker.skip(() => true);
110
- return;
111
- }
112
- // If text allowed on this position, extend to this place.
113
- if (schema.checkChild(nextPosition, '$text')) {
114
- return nextPosition;
115
- }
116
- }
117
- }
118
- /**
119
- * Finds a correct position by walking in a text node and checking whether selection can be extended to given position
120
- * or should be extended further.
121
- */
122
- function getCorrectPosition(walker, unit, treatEmojiAsSingleUnit) {
123
- const textNode = walker.position.textNode;
124
- if (textNode) {
125
- const data = textNode.data;
126
- let offset = walker.position.offset - textNode.startOffset;
127
- while (isInsideSurrogatePair(data, offset) ||
128
- (unit == 'character' && isInsideCombinedSymbol(data, offset)) ||
129
- (treatEmojiAsSingleUnit && isInsideEmojiSequence(data, offset))) {
130
- walker.next();
131
- offset = walker.position.offset - textNode.startOffset;
132
- }
133
- }
134
- return walker.position;
135
- }
136
- /**
137
- * Finds a correct position of a word break by walking in a text node and checking whether selection can be extended to given position
138
- * or should be extended further.
139
- */
140
- function getCorrectWordBreakPosition(walker, isForward) {
141
- let textNode = walker.position.textNode;
142
- if (!textNode) {
143
- textNode = isForward ? walker.position.nodeAfter : walker.position.nodeBefore;
144
- }
145
- while (textNode && textNode.is('$text')) {
146
- const offset = walker.position.offset - textNode.startOffset;
147
- // Check of adjacent text nodes with different attributes (like BOLD).
148
- // Example : 'foofoo []bar<$text bold="true">bar</$text> bazbaz'
149
- // should expand to : 'foofoo [bar<$text bold="true">bar</$text>] bazbaz'.
150
- if (isAtNodeBoundary(textNode, offset, isForward)) {
151
- textNode = isForward ? walker.position.nodeAfter : walker.position.nodeBefore;
152
- }
153
- // Check if this is a word boundary.
154
- else if (isAtWordBoundary(textNode.data, offset, isForward)) {
155
- break;
156
- }
157
- // Maybe one more character.
158
- else {
159
- walker.next();
160
- }
161
- }
162
- return walker.position;
163
- }
164
- function getSearchRange(start, isForward) {
165
- const root = start.root;
166
- const searchEnd = ModelPosition._createAt(root, isForward ? 'end' : 0);
167
- if (isForward) {
168
- return new ModelRange(start, searchEnd);
169
- }
170
- else {
171
- return new ModelRange(searchEnd, start);
172
- }
173
- }
174
- /**
175
- * Checks if selection is on word boundary.
176
- */
177
- function isAtWordBoundary(data, offset, isForward) {
178
- // The offset to check depends on direction.
179
- const offsetToCheck = offset + (isForward ? 0 : -1);
180
- return wordBoundaryCharacters.includes(data.charAt(offsetToCheck));
181
- }
182
- /**
183
- * Checks if selection is on node boundary.
184
- */
185
- function isAtNodeBoundary(textNode, offset, isForward) {
186
- return offset === (isForward ? textNode.offsetSize : 0);
187
- }
@@ -1,264 +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/selection-post-fixer
7
- */
8
- import { ModelPosition } from '../position.js';
9
- import { ModelRange } from '../range.js';
10
- /**
11
- * Injects selection post-fixer to the model.
12
- *
13
- * The role of the selection post-fixer is to ensure that the selection is in a correct place
14
- * after a {@link module:engine/model/model~Model#change `change()`} block was executed.
15
- *
16
- * The correct position means that:
17
- *
18
- * * All collapsed selection ranges are in a place where the {@link module:engine/model/schema~ModelSchema}
19
- * allows a `$text`.
20
- * * None of the selection's non-collapsed ranges crosses a {@link module:engine/model/schema~ModelSchema#isLimit limit element}
21
- * boundary (a range must be rooted within one limit element).
22
- * * Only {@link module:engine/model/schema~ModelSchema#isSelectable selectable elements} can be selected from the outside
23
- * (e.g. `[<paragraph>foo</paragraph>]` is invalid). This rule applies independently to both selection ends, so this
24
- * selection is correct: `<paragraph>f[oo</paragraph><imageBlock></imageBlock>]`.
25
- *
26
- * If the position is not correct, the post-fixer will automatically correct it.
27
- *
28
- * ## Fixing a non-collapsed selection
29
- *
30
- * See as an example a selection that starts in a P1 element and ends inside the text of a TD element
31
- * (`[` and `]` are range boundaries and `(l)` denotes an element defined as `isLimit=true`):
32
- *
33
- * ```
34
- * root
35
- * |- element P1
36
- * | |- "foo" root
37
- * |- element TABLE (l) P1 TABLE P2
38
- * | |- element TR (l) f o[o TR TR b a r
39
- * | | |- element TD (l) TD TD
40
- * | | |- "aaa" a]a a b b b
41
- * | |- element TR (l)
42
- * | | |- element TD (l) ||
43
- * | | |- "bbb" ||
44
- * |- element P2 VV
45
- * | |- "bar"
46
- * root
47
- * P1 TABLE] P2
48
- * f o[o TR TR b a r
49
- * TD TD
50
- * a a a b b b
51
- * ```
52
- *
53
- * In the example above, the TABLE, TR and TD are defined as `isLimit=true` in the schema. The range which is not contained within
54
- * a single limit element must be expanded to select the outermost limit element. The range end is inside the text node of the TD element.
55
- * As the TD element is a child of the TR and TABLE elements, where both are defined as `isLimit=true` in the schema, the range must be
56
- * expanded to select the whole TABLE element.
57
- *
58
- * **Note** If the selection contains multiple ranges, the method returns a minimal set of ranges that are not intersecting after expanding
59
- * them to select `isLimit=true` elements.
60
- *
61
- * @internal
62
- */
63
- export function injectSelectionPostFixer(model) {
64
- model.document.registerPostFixer(writer => selectionPostFixer(writer, model));
65
- }
66
- /**
67
- * The selection post-fixer.
68
- */
69
- function selectionPostFixer(writer, model) {
70
- const selection = model.document.selection;
71
- const schema = model.schema;
72
- const ranges = [];
73
- let wasFixed = false;
74
- for (const modelRange of selection.getRanges()) {
75
- // Go through all ranges in selection and try fixing each of them.
76
- // Those ranges might overlap but will be corrected later.
77
- const correctedRange = tryFixingRange(modelRange, schema);
78
- // "Selection fixing" algorithms sometimes get lost. In consequence, it may happen
79
- // that a new range is returned but, in fact, it has the same positions as the original
80
- // range anyway. If this range is not discarded, a new selection will be set and that,
81
- // for instance, would destroy the selection attributes. Let's make sure that the post-fixer
82
- // actually worked first before setting a new selection.
83
- //
84
- // https://github.com/ckeditor/ckeditor5/issues/6693
85
- if (correctedRange && !correctedRange.isEqual(modelRange)) {
86
- ranges.push(correctedRange);
87
- wasFixed = true;
88
- }
89
- else {
90
- ranges.push(modelRange);
91
- }
92
- }
93
- // If any of ranges were corrected update the selection.
94
- if (wasFixed) {
95
- writer.setSelection(mergeIntersectingRanges(ranges), { backward: selection.isBackward });
96
- }
97
- return false;
98
- }
99
- /**
100
- * Tries fixing a range if it's incorrect.
101
- *
102
- * **Note:** This helper is used by the selection post-fixer and to fix the `beforeinput` target ranges.
103
- *
104
- * @returns Returns fixed range or null if range is valid.
105
- * @internal
106
- */
107
- export function tryFixingRange(range, schema) {
108
- if (range.isCollapsed) {
109
- return tryFixingCollapsedRange(range, schema);
110
- }
111
- return tryFixingNonCollapsedRage(range, schema);
112
- }
113
- /**
114
- * Tries to fix collapsed ranges.
115
- *
116
- * * Fixes situation when a range is in a place where $text is not allowed
117
- *
118
- * @param range Collapsed range to fix.
119
- * @returns Returns fixed range or null if range is valid.
120
- */
121
- function tryFixingCollapsedRange(range, schema) {
122
- const originalPosition = range.start;
123
- const nearestSelectionRange = schema.getNearestSelectionRange(originalPosition);
124
- // This might be null, i.e. when the editor data is empty or the selection is inside a limit element
125
- // that doesn't allow text inside.
126
- // In the first case, there is no need to fix the selection range.
127
- // In the second, let's go up to the outer selectable element
128
- if (!nearestSelectionRange) {
129
- const ancestorObject = originalPosition.getAncestors().reverse().find((item) => schema.isObject(item));
130
- if (ancestorObject) {
131
- return ModelRange._createOn(ancestorObject);
132
- }
133
- return null;
134
- }
135
- if (!nearestSelectionRange.isCollapsed) {
136
- return nearestSelectionRange;
137
- }
138
- const fixedPosition = nearestSelectionRange.start;
139
- // Fixed position is the same as original - no need to return corrected range.
140
- if (originalPosition.isEqual(fixedPosition)) {
141
- return null;
142
- }
143
- return new ModelRange(fixedPosition);
144
- }
145
- /**
146
- * Tries to fix an expanded range.
147
- *
148
- * @param range Expanded range to fix.
149
- * @returns Returns fixed range or null if range is valid.
150
- */
151
- function tryFixingNonCollapsedRage(range, schema) {
152
- const { start, end } = range;
153
- const isTextAllowedOnStart = schema.checkChild(start, '$text');
154
- const isTextAllowedOnEnd = schema.checkChild(end, '$text');
155
- const startLimitElement = schema.getLimitElement(start);
156
- const endLimitElement = schema.getLimitElement(end);
157
- // Ranges which both end are inside the same limit element (or root) might needs only minor fix.
158
- if (startLimitElement === endLimitElement) {
159
- // Range is valid when both position allows to place a text:
160
- // - <block>f[oobarba]z</block>
161
- // This would be "fixed" by a next check but as it will be the same it's better to return null so the selection stays the same.
162
- if (isTextAllowedOnStart && isTextAllowedOnEnd) {
163
- return null;
164
- }
165
- // Range that is on non-limit element (or is partially) must be fixed so it is placed inside the block around $text:
166
- // - [<block>foo</block>] -> <block>[foo]</block>
167
- // - [<block>foo]</block> -> <block>[foo]</block>
168
- // - <block>f[oo</block>] -> <block>f[oo]</block>
169
- // - [<block>foo</block><selectable></selectable>] -> <block>[foo</block><selectable></selectable>]
170
- if (checkSelectionOnNonLimitElements(start, end, schema)) {
171
- const isStartBeforeSelectable = start.nodeAfter && schema.isSelectable(start.nodeAfter);
172
- const fixedStart = isStartBeforeSelectable ? null : schema.getNearestSelectionRange(start, 'forward');
173
- const isEndAfterSelectable = end.nodeBefore && schema.isSelectable(end.nodeBefore);
174
- const fixedEnd = isEndAfterSelectable ? null : schema.getNearestSelectionRange(end, 'backward');
175
- // The schema.getNearestSelectionRange might return null - if that happens use original position.
176
- const rangeStart = fixedStart ? fixedStart.start : start;
177
- const rangeEnd = fixedEnd ? fixedEnd.end : end;
178
- return new ModelRange(rangeStart, rangeEnd);
179
- }
180
- }
181
- const isStartInLimit = startLimitElement && !startLimitElement.is('rootElement');
182
- const isEndInLimit = endLimitElement && !endLimitElement.is('rootElement');
183
- // At this point we eliminated valid positions on text nodes so if one of range positions is placed inside a limit element
184
- // then the range crossed limit element boundaries and needs to be fixed.
185
- if (isStartInLimit || isEndInLimit) {
186
- const bothInSameParent = (start.nodeAfter && end.nodeBefore) && start.nodeAfter.parent === end.nodeBefore.parent;
187
- const expandStart = isStartInLimit && (!bothInSameParent || !isSelectable(start.nodeAfter, schema));
188
- const expandEnd = isEndInLimit && (!bothInSameParent || !isSelectable(end.nodeBefore, schema));
189
- // Although we've already found limit element on start/end positions we must find the outer-most limit element.
190
- // as limit elements might be nested directly inside (ie table > tableRow > tableCell).
191
- let fixedStart = start;
192
- let fixedEnd = end;
193
- if (expandStart) {
194
- fixedStart = ModelPosition._createBefore(findOutermostLimitAncestor(startLimitElement, schema));
195
- }
196
- if (expandEnd) {
197
- fixedEnd = ModelPosition._createAfter(findOutermostLimitAncestor(endLimitElement, schema));
198
- }
199
- return new ModelRange(fixedStart, fixedEnd);
200
- }
201
- // Range was not fixed at this point so it is valid - ie it was placed around limit element already.
202
- return null;
203
- }
204
- /**
205
- * Finds the outer-most ancestor.
206
- */
207
- function findOutermostLimitAncestor(startingNode, schema) {
208
- let isLimitNode = startingNode;
209
- let parent = isLimitNode;
210
- // Find outer most isLimit block as such blocks might be nested (ie. in tables).
211
- while (schema.isLimit(parent) && parent.parent) {
212
- isLimitNode = parent;
213
- parent = parent.parent;
214
- }
215
- return isLimitNode;
216
- }
217
- /**
218
- * Checks whether any of range boundaries is placed around non-limit elements.
219
- */
220
- function checkSelectionOnNonLimitElements(start, end, schema) {
221
- const startIsOnBlock = (start.nodeAfter && !schema.isLimit(start.nodeAfter)) || schema.checkChild(start, '$text');
222
- const endIsOnBlock = (end.nodeBefore && !schema.isLimit(end.nodeBefore)) || schema.checkChild(end, '$text');
223
- // We should fix such selection when one of those nodes needs fixing.
224
- return startIsOnBlock || endIsOnBlock;
225
- }
226
- /**
227
- * Returns a minimal non-intersecting array of ranges without duplicates.
228
- *
229
- * @param ranges Ranges to merge.
230
- * @returns Array of unique and non-intersecting ranges.
231
- * @internal
232
- */
233
- export function mergeIntersectingRanges(ranges) {
234
- const rangesToMerge = [...ranges];
235
- const rangeIndexesToRemove = new Set();
236
- let currentRangeIndex = 1;
237
- while (currentRangeIndex < rangesToMerge.length) {
238
- const currentRange = rangesToMerge[currentRangeIndex];
239
- const previousRanges = rangesToMerge.slice(0, currentRangeIndex);
240
- for (const [previousRangeIndex, previousRange] of previousRanges.entries()) {
241
- if (rangeIndexesToRemove.has(previousRangeIndex)) {
242
- continue;
243
- }
244
- if (currentRange.isEqual(previousRange)) {
245
- rangeIndexesToRemove.add(previousRangeIndex);
246
- }
247
- else if (currentRange.isIntersecting(previousRange)) {
248
- rangeIndexesToRemove.add(previousRangeIndex);
249
- rangeIndexesToRemove.add(currentRangeIndex);
250
- const mergedRange = currentRange.getJoined(previousRange);
251
- rangesToMerge.push(mergedRange);
252
- }
253
- }
254
- currentRangeIndex++;
255
- }
256
- const nonIntersectingRanges = rangesToMerge.filter((_, index) => !rangeIndexesToRemove.has(index));
257
- return nonIntersectingRanges;
258
- }
259
- /**
260
- * Checks if node exists and if it's a selectable.
261
- */
262
- function isSelectable(node, schema) {
263
- return node && schema.isSelectable(node);
264
- }