@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,1041 +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/position
7
- */
8
- import { ModelTypeCheckable } from './typecheckable.js';
9
- import { ModelTreeWalker } from './treewalker.js';
10
- import { CKEditorError, compareArrays } from '@ckeditor/ckeditor5-utils';
11
- /**
12
- * Represents a position in the model tree.
13
- *
14
- * A position is represented by its {@link module:engine/model/position~ModelPosition#root} and
15
- * a {@link module:engine/model/position~ModelPosition#path} in that root.
16
- *
17
- * You can create position instances via its constructor or the `createPosition*()` factory methods of
18
- * {@link module:engine/model/model~Model} and {@link module:engine/model/writer~ModelWriter}.
19
- *
20
- * **Note:** Position is based on offsets, not indexes. This means that a position between two text nodes
21
- * `foo` and `bar` has offset `3`, not `1`. See {@link module:engine/model/position~ModelPosition#path} for more information.
22
- *
23
- * Since a position in the model is represented by a {@link module:engine/model/position~ModelPosition#root position root} and
24
- * {@link module:engine/model/position~ModelPosition#path position path} it is possible to create positions placed in non-existing places.
25
- * This requirement is important for operational transformation algorithms.
26
- *
27
- * Also, {@link module:engine/model/operation/operation~Operation operations}
28
- * kept in the {@link module:engine/model/document~ModelDocument#history document history}
29
- * are storing positions (and ranges) which were correct when those operations were applied, but may not be correct
30
- * after the document has changed.
31
- *
32
- * When changes are applied to the model, it may also happen that {@link module:engine/model/position~ModelPosition#parent position parent}
33
- * will change even if position path has not changed. Keep in mind, that if a position leads to non-existing element,
34
- * {@link module:engine/model/position~ModelPosition#parent} and some other properties and methods will throw errors.
35
- *
36
- * In most cases, position with wrong path is caused by an error in code, but it is sometimes needed, as described above.
37
- */
38
- export class ModelPosition extends ModelTypeCheckable {
39
- /**
40
- * Root of the position path.
41
- */
42
- root;
43
- /**
44
- * Position of the node in the tree. **Path contains offsets, not indexes.**
45
- *
46
- * Position can be placed before, after or in a {@link module:engine/model/node~ModelNode node} if that node has
47
- * {@link module:engine/model/node~ModelNode#offsetSize} greater than `1`. Items in position path are
48
- * {@link module:engine/model/node~ModelNode#startOffset starting offsets} of position ancestors, starting from direct root children,
49
- * down to the position offset in it's parent.
50
- *
51
- * ```
52
- * ROOT
53
- * |- P before: [ 0 ] after: [ 1 ]
54
- * |- UL before: [ 1 ] after: [ 2 ]
55
- * |- LI before: [ 1, 0 ] after: [ 1, 1 ]
56
- * | |- foo before: [ 1, 0, 0 ] after: [ 1, 0, 3 ]
57
- * |- LI before: [ 1, 1 ] after: [ 1, 2 ]
58
- * |- bar before: [ 1, 1, 0 ] after: [ 1, 1, 3 ]
59
- * ```
60
- *
61
- * `foo` and `bar` are representing {@link module:engine/model/text~ModelText text nodes}. Since text nodes has offset size
62
- * greater than `1` you can place position offset between their start and end:
63
- *
64
- * ```
65
- * ROOT
66
- * |- P
67
- * |- UL
68
- * |- LI
69
- * | |- f^o|o ^ has path: [ 1, 0, 1 ] | has path: [ 1, 0, 2 ]
70
- * |- LI
71
- * |- b^a|r ^ has path: [ 1, 1, 1 ] | has path: [ 1, 1, 2 ]
72
- * ```
73
- */
74
- path;
75
- /**
76
- * Position stickiness. See {@link module:engine/model/position~ModelPositionStickiness}.
77
- */
78
- stickiness;
79
- /**
80
- * Creates a position.
81
- *
82
- * @param root Root of the position.
83
- * @param path Position path. See {@link module:engine/model/position~ModelPosition#path}.
84
- * @param stickiness Position stickiness. See {@link module:engine/model/position~ModelPositionStickiness}.
85
- */
86
- constructor(root, path, stickiness = 'toNone') {
87
- super();
88
- if (!root.is('element') && !root.is('documentFragment')) {
89
- /**
90
- * Position root is invalid.
91
- *
92
- * Positions can only be anchored in elements or document fragments.
93
- *
94
- * @error model-position-root-invalid
95
- */
96
- throw new CKEditorError('model-position-root-invalid', root);
97
- }
98
- if (!Array.isArray(path) || path.length === 0) {
99
- /**
100
- * Position path must be an array with at least one item.
101
- *
102
- * @error model-position-path-incorrect-format
103
- * @param {Array.<number>} path A path to the position.
104
- */
105
- throw new CKEditorError('model-position-path-incorrect-format', root, { path });
106
- }
107
- // Normalize the root and path when element (not root) is passed.
108
- if (root.is('rootElement')) {
109
- path = path.slice();
110
- }
111
- else {
112
- path = [...root.getPath(), ...path];
113
- root = root.root;
114
- }
115
- this.root = root;
116
- this.path = path;
117
- this.stickiness = stickiness;
118
- }
119
- /**
120
- * Offset at which this position is located in its {@link module:engine/model/position~ModelPosition#parent parent}. It is equal
121
- * to the last item in position {@link module:engine/model/position~ModelPosition#path path}.
122
- *
123
- * @type {Number}
124
- */
125
- get offset() {
126
- return this.path[this.path.length - 1];
127
- }
128
- set offset(newOffset) {
129
- this.path[this.path.length - 1] = newOffset;
130
- }
131
- /**
132
- * Parent element of this position.
133
- *
134
- * Keep in mind that `parent` value is calculated when the property is accessed.
135
- * If {@link module:engine/model/position~ModelPosition#path position path}
136
- * leads to a non-existing element, `parent` property will throw error.
137
- *
138
- * Also it is a good idea to cache `parent` property if it is used frequently in an algorithm (i.e. in a long loop).
139
- */
140
- get parent() {
141
- let parent = this.root;
142
- for (let i = 0; i < this.path.length - 1; i++) {
143
- parent = parent.getChildAtOffset(this.path[i]);
144
- if (!parent) {
145
- /**
146
- * The position's path is incorrect. This means that a position does not point to
147
- * a correct place in the tree and hence, some of its methods and getters cannot work correctly.
148
- *
149
- * **Note**: Unlike DOM and view positions, in the model, the
150
- * {@link module:engine/model/position~ModelPosition#parent position's parent} is always an element or a document fragment.
151
- * The last offset in the
152
- * {@link module:engine/model/position~ModelPosition#path position's path} is the point in this element
153
- * where this position points.
154
- *
155
- * Read more about model positions and offsets in
156
- * the {@glink framework/architecture/editing-engine#indexes-and-offsets Editing engine architecture} guide.
157
- *
158
- * @error model-position-path-incorrect
159
- * @param {module:engine/model/position~ModelPosition} position The incorrect position.
160
- */
161
- throw new CKEditorError('model-position-path-incorrect', this, { position: this });
162
- }
163
- }
164
- if (parent.is('$text')) {
165
- throw new CKEditorError('model-position-path-incorrect', this, { position: this });
166
- }
167
- return parent;
168
- }
169
- /**
170
- * Position {@link module:engine/model/position~ModelPosition#offset offset} converted to an index in position's parent node. It is
171
- * equal to the {@link module:engine/model/node~ModelNode#index index} of a node after this position. If position is placed
172
- * in text node, position index is equal to the index of that text node.
173
- */
174
- get index() {
175
- return this.parent.offsetToIndex(this.offset);
176
- }
177
- /**
178
- * Returns {@link module:engine/model/text~ModelText text node} instance in which this position is placed or `null` if this
179
- * position is not in a text node.
180
- */
181
- get textNode() {
182
- return getTextNodeAtPosition(this, this.parent);
183
- }
184
- /**
185
- * Node directly after this position. Returns `null` if this position is at the end of its parent, or if it is in a text node.
186
- */
187
- get nodeAfter() {
188
- // Cache the parent and reuse for performance reasons. See #6579 and #6582.
189
- const parent = this.parent;
190
- return getNodeAfterPosition(this, parent, getTextNodeAtPosition(this, parent));
191
- }
192
- /**
193
- * Node directly before this position. Returns `null` if this position is at the start of its parent, or if it is in a text node.
194
- */
195
- get nodeBefore() {
196
- // Cache the parent and reuse for performance reasons. See #6579 and #6582.
197
- const parent = this.parent;
198
- return getNodeBeforePosition(this, parent, getTextNodeAtPosition(this, parent));
199
- }
200
- /**
201
- * Is `true` if position is at the beginning of its {@link module:engine/model/position~ModelPosition#parent parent}, `false` otherwise.
202
- */
203
- get isAtStart() {
204
- return this.offset === 0;
205
- }
206
- /**
207
- * Is `true` if position is at the end of its {@link module:engine/model/position~ModelPosition#parent parent}, `false` otherwise.
208
- */
209
- get isAtEnd() {
210
- return this.offset == this.parent.maxOffset;
211
- }
212
- /**
213
- * Checks whether the position is valid in current model tree, that is whether it points to an existing place in the model.
214
- */
215
- isValid() {
216
- if (this.offset < 0) {
217
- return false;
218
- }
219
- let parent = this.root;
220
- for (let i = 0; i < this.path.length - 1; i++) {
221
- parent = parent.getChildAtOffset(this.path[i]);
222
- if (!parent) {
223
- return false;
224
- }
225
- }
226
- return this.offset <= parent.maxOffset;
227
- }
228
- /**
229
- * Checks whether this position is before or after given position.
230
- *
231
- * This method is safe to use it on non-existing positions (for example during operational transformation).
232
- */
233
- compareWith(otherPosition) {
234
- if (this.root != otherPosition.root) {
235
- return 'different';
236
- }
237
- const result = compareArrays(this.path, otherPosition.path);
238
- switch (result) {
239
- case 'same':
240
- return 'same';
241
- case 'prefix':
242
- return 'before';
243
- case 'extension':
244
- return 'after';
245
- default:
246
- return this.path[result] < otherPosition.path[result] ? 'before' : 'after';
247
- }
248
- }
249
- /**
250
- * Gets the farthest position which matches the callback using
251
- * {@link module:engine/model/treewalker~ModelTreeWalker TreeWalker}.
252
- *
253
- * For example:
254
- *
255
- * ```ts
256
- * getLastMatchingPosition( value => value.type == 'text' );
257
- * // <paragraph>[]foo</paragraph> -> <paragraph>foo[]</paragraph>
258
- *
259
- * getLastMatchingPosition( value => value.type == 'text', { direction: 'backward' } );
260
- * // <paragraph>foo[]</paragraph> -> <paragraph>[]foo</paragraph>
261
- *
262
- * getLastMatchingPosition( value => false );
263
- * // Do not move the position.
264
- * ```
265
- *
266
- * @param skip Callback function. Gets {@link module:engine/model/treewalker~ModelTreeWalkerValue} and should
267
- * return `true` if the value should be skipped or `false` if not.
268
- * @param options Object with configuration options. See {@link module:engine/model/treewalker~ModelTreeWalker}.
269
- *
270
- * @returns The position after the last item which matches the `skip` callback test.
271
- */
272
- getLastMatchingPosition(skip, options = {}) {
273
- options.startPosition = this;
274
- const treeWalker = new ModelTreeWalker(options);
275
- treeWalker.skip(skip);
276
- return treeWalker.position;
277
- }
278
- /**
279
- * Returns a path to this position's parent. Parent path is equal to position
280
- * {@link module:engine/model/position~ModelPosition#path path} but without the last item.
281
- *
282
- * This method is safe to use it on non-existing positions (for example during operational transformation).
283
- *
284
- * @returns Path to the parent.
285
- */
286
- getParentPath() {
287
- return this.path.slice(0, -1);
288
- }
289
- /**
290
- * Returns ancestors array of this position, that is this position's parent and its ancestors.
291
- *
292
- * @returns Array with ancestors.
293
- */
294
- getAncestors() {
295
- const parent = this.parent;
296
- if (parent.is('documentFragment')) {
297
- return [parent];
298
- }
299
- else {
300
- return parent.getAncestors({ includeSelf: true });
301
- }
302
- }
303
- /**
304
- * Returns the parent element of the given name. Returns null if the position is not inside the desired parent.
305
- *
306
- * @param parentName The name of the parent element to find.
307
- */
308
- findAncestor(parentName) {
309
- const parent = this.parent;
310
- if (parent.is('element')) {
311
- return parent.findAncestor(parentName, { includeSelf: true });
312
- }
313
- return null;
314
- }
315
- /**
316
- * Returns the slice of two position {@link #path paths} which is identical. The {@link #root roots}
317
- * of these two paths must be identical.
318
- *
319
- * This method is safe to use it on non-existing positions (for example during operational transformation).
320
- *
321
- * @param position The second position.
322
- * @returns The common path.
323
- */
324
- getCommonPath(position) {
325
- if (this.root != position.root) {
326
- return [];
327
- }
328
- // We find on which tree-level start and end have the lowest common ancestor
329
- const cmp = compareArrays(this.path, position.path);
330
- // If comparison returned string it means that arrays are same.
331
- const diffAt = (typeof cmp == 'string') ? Math.min(this.path.length, position.path.length) : cmp;
332
- return this.path.slice(0, diffAt);
333
- }
334
- /**
335
- * Returns an {@link module:engine/model/element~ModelElement} or {@link module:engine/model/documentfragment~ModelDocumentFragment}
336
- * which is a common ancestor of both positions. The {@link #root roots} of these two positions must be identical.
337
- *
338
- * @param position The second position.
339
- */
340
- getCommonAncestor(position) {
341
- const ancestorsA = this.getAncestors();
342
- const ancestorsB = position.getAncestors();
343
- let i = 0;
344
- while (ancestorsA[i] == ancestorsB[i] && ancestorsA[i]) {
345
- i++;
346
- }
347
- return i === 0 ? null : ancestorsA[i - 1];
348
- }
349
- /**
350
- * Returns a new instance of `Position`, that has same {@link #parent parent} but it's offset
351
- * is shifted by `shift` value (can be a negative value).
352
- *
353
- * This method is safe to use it on non-existing positions (for example during operational transformation).
354
- *
355
- * @param shift Offset shift. Can be a negative value.
356
- * @returns Shifted position.
357
- */
358
- getShiftedBy(shift) {
359
- const shifted = this.clone();
360
- const offset = shifted.offset + shift;
361
- shifted.offset = offset < 0 ? 0 : offset;
362
- return shifted;
363
- }
364
- /**
365
- * Checks whether this position is after given position.
366
- *
367
- * This method is safe to use it on non-existing positions (for example during operational transformation).
368
- *
369
- * @see module:engine/model/position~ModelPosition#isBefore
370
- * @param otherPosition Position to compare with.
371
- * @returns True if this position is after given position.
372
- */
373
- isAfter(otherPosition) {
374
- return this.compareWith(otherPosition) == 'after';
375
- }
376
- /**
377
- * Checks whether this position is before given position.
378
- *
379
- * **Note:** watch out when using negation of the value returned by this method, because the negation will also
380
- * be `true` if positions are in different roots and you might not expect this. You should probably use
381
- * `a.isAfter( b ) || a.isEqual( b )` or `!a.isBefore( p ) && a.root == b.root` in most scenarios. If your
382
- * condition uses multiple `isAfter` and `isBefore` checks, build them so they do not use negated values, i.e.:
383
- *
384
- * ```ts
385
- * if ( a.isBefore( b ) && c.isAfter( d ) ) {
386
- * // do A.
387
- * } else {
388
- * // do B.
389
- * }
390
- * ```
391
- *
392
- * or, if you have only one if-branch:
393
- *
394
- * ```ts
395
- * if ( !( a.isBefore( b ) && c.isAfter( d ) ) {
396
- * // do B.
397
- * }
398
- * ```
399
- *
400
- * rather than:
401
- *
402
- * ```ts
403
- * if ( !a.isBefore( b ) || && !c.isAfter( d ) ) {
404
- * // do B.
405
- * } else {
406
- * // do A.
407
- * }
408
- * ```
409
- *
410
- * This method is safe to use it on non-existing positions (for example during operational transformation).
411
- *
412
- * @param otherPosition Position to compare with.
413
- * @returns True if this position is before given position.
414
- */
415
- isBefore(otherPosition) {
416
- return this.compareWith(otherPosition) == 'before';
417
- }
418
- /**
419
- * Checks whether this position is equal to given position.
420
- *
421
- * This method is safe to use it on non-existing positions (for example during operational transformation).
422
- *
423
- * @param otherPosition Position to compare with.
424
- * @returns True if positions are same.
425
- */
426
- isEqual(otherPosition) {
427
- return this.compareWith(otherPosition) == 'same';
428
- }
429
- /**
430
- * Checks whether this position is touching given position. Positions touch when there are no text nodes
431
- * or empty nodes in a range between them. Technically, those positions are not equal but in many cases
432
- * they are very similar or even indistinguishable.
433
- *
434
- * @param otherPosition Position to compare with.
435
- * @returns True if positions touch.
436
- */
437
- isTouching(otherPosition) {
438
- if (this.root !== otherPosition.root) {
439
- return false;
440
- }
441
- const commonLevel = Math.min(this.path.length, otherPosition.path.length);
442
- for (let level = 0; level < commonLevel; level++) {
443
- const diff = this.path[level] - otherPosition.path[level];
444
- // Positions are spread by a node, so they are not touching.
445
- if (diff < -1 || diff > 1) {
446
- return false;
447
- }
448
- else if (diff === 1) {
449
- // `otherPosition` is on the left.
450
- // `this` is on the right.
451
- return checkTouchingBranch(otherPosition, this, level);
452
- }
453
- else if (diff === -1) {
454
- // `this` is on the left.
455
- // `otherPosition` is on the right.
456
- return checkTouchingBranch(this, otherPosition, level);
457
- }
458
- // `diff === 0`.
459
- // Positions are inside the same element on this level, compare deeper.
460
- }
461
- // If we ended up here, it means that positions paths have the same beginning.
462
- // If the paths have the same length, then it means that they are identical, so the positions are same.
463
- if (this.path.length === otherPosition.path.length) {
464
- return true;
465
- }
466
- // If positions have different length of paths, then the common part is the same.
467
- // In this case, the "shorter" position is on the left, the "longer" position is on the right.
468
- //
469
- // If the positions are touching, the "longer" position must have only zeroes. For example:
470
- // [ 1, 2 ] vs [ 1, 2, 0 ]
471
- // [ 1, 2 ] vs [ 1, 2, 0, 0, 0 ]
472
- else if (this.path.length > otherPosition.path.length) {
473
- return checkOnlyZeroes(this.path, commonLevel);
474
- }
475
- else {
476
- return checkOnlyZeroes(otherPosition.path, commonLevel);
477
- }
478
- }
479
- /**
480
- * Checks if two positions are in the same parent.
481
- *
482
- * This method is safe to use it on non-existing positions (for example during operational transformation).
483
- *
484
- * @param position Position to compare with.
485
- * @returns `true` if positions have the same parent, `false` otherwise.
486
- */
487
- hasSameParentAs(position) {
488
- if (this.root !== position.root) {
489
- return false;
490
- }
491
- const thisParentPath = this.getParentPath();
492
- const posParentPath = position.getParentPath();
493
- return compareArrays(thisParentPath, posParentPath) == 'same';
494
- }
495
- /**
496
- * Returns a copy of this position that is transformed by given `operation`.
497
- *
498
- * The new position's parameters are updated accordingly to the effect of the `operation`.
499
- *
500
- * For example, if `n` nodes are inserted before the position, the returned position {@link ~ModelPosition#offset} will be
501
- * increased by `n`. If the position was in a merged element, it will be accordingly moved to the new element, etc.
502
- *
503
- * This method is safe to use it on non-existing positions (for example during operational transformation).
504
- *
505
- * @param operation Operation to transform by.
506
- * @returns Transformed position.
507
- */
508
- getTransformedByOperation(operation) {
509
- let result;
510
- switch (operation.type) {
511
- case 'insert':
512
- result = this._getTransformedByInsertOperation(operation);
513
- break;
514
- case 'move':
515
- case 'remove':
516
- case 'reinsert':
517
- result = this._getTransformedByMoveOperation(operation);
518
- break;
519
- case 'split':
520
- result = this._getTransformedBySplitOperation(operation);
521
- break;
522
- case 'merge':
523
- result = this._getTransformedByMergeOperation(operation);
524
- break;
525
- default:
526
- result = ModelPosition._createAt(this);
527
- break;
528
- }
529
- return result;
530
- }
531
- /**
532
- * Returns a copy of this position transformed by an insert operation.
533
- *
534
- * @internal
535
- */
536
- _getTransformedByInsertOperation(operation) {
537
- return this._getTransformedByInsertion(operation.position, operation.howMany);
538
- }
539
- /**
540
- * Returns a copy of this position transformed by a move operation.
541
- *
542
- * @internal
543
- */
544
- _getTransformedByMoveOperation(operation) {
545
- return this._getTransformedByMove(operation.sourcePosition, operation.targetPosition, operation.howMany);
546
- }
547
- /**
548
- * Returns a copy of this position transformed by a split operation.
549
- *
550
- * @internal
551
- */
552
- _getTransformedBySplitOperation(operation) {
553
- const movedRange = operation.movedRange;
554
- const isContained = movedRange.containsPosition(this) ||
555
- (movedRange.start.isEqual(this) && this.stickiness == 'toNext');
556
- if (isContained) {
557
- return this._getCombined(operation.splitPosition, operation.moveTargetPosition);
558
- }
559
- else {
560
- if (operation.graveyardPosition) {
561
- return this._getTransformedByMove(operation.graveyardPosition, operation.insertionPosition, 1);
562
- }
563
- else {
564
- return this._getTransformedByInsertion(operation.insertionPosition, 1);
565
- }
566
- }
567
- }
568
- /**
569
- * Returns a copy of this position transformed by merge operation.
570
- *
571
- * @internal
572
- */
573
- _getTransformedByMergeOperation(operation) {
574
- const movedRange = operation.movedRange;
575
- const isContained = movedRange.containsPosition(this) || movedRange.start.isEqual(this);
576
- let pos;
577
- if (isContained) {
578
- pos = this._getCombined(operation.sourcePosition, operation.targetPosition);
579
- if (operation.sourcePosition.isBefore(operation.targetPosition)) {
580
- // Above happens during OT when the merged element is moved before the merged-to element.
581
- pos = pos._getTransformedByDeletion(operation.deletionPosition, 1);
582
- }
583
- }
584
- else if (this.isEqual(operation.deletionPosition)) {
585
- pos = ModelPosition._createAt(operation.deletionPosition);
586
- }
587
- else {
588
- pos = this._getTransformedByMove(operation.deletionPosition, operation.graveyardPosition, 1);
589
- }
590
- return pos;
591
- }
592
- /**
593
- * Returns a copy of this position that is updated by removing `howMany` nodes starting from `deletePosition`.
594
- * It may happen that this position is in a removed node. If that is the case, `null` is returned instead.
595
- *
596
- * @internal
597
- * @param deletePosition Position before the first removed node.
598
- * @param howMany How many nodes are removed.
599
- * @returns Transformed position or `null`.
600
- */
601
- _getTransformedByDeletion(deletePosition, howMany) {
602
- const transformed = ModelPosition._createAt(this);
603
- // This position can't be affected if deletion was in a different root.
604
- if (this.root != deletePosition.root) {
605
- return transformed;
606
- }
607
- if (compareArrays(deletePosition.getParentPath(), this.getParentPath()) == 'same') {
608
- // If nodes are removed from the node that is pointed by this position...
609
- if (deletePosition.offset < this.offset) {
610
- // And are removed from before an offset of that position...
611
- if (deletePosition.offset + howMany > this.offset) {
612
- // Position is in removed range, it's no longer in the tree.
613
- return null;
614
- }
615
- else {
616
- // Decrement the offset accordingly.
617
- transformed.offset -= howMany;
618
- }
619
- }
620
- }
621
- else if (compareArrays(deletePosition.getParentPath(), this.getParentPath()) == 'prefix') {
622
- // If nodes are removed from a node that is on a path to this position...
623
- const i = deletePosition.path.length - 1;
624
- if (deletePosition.offset <= this.path[i]) {
625
- // And are removed from before next node of that path...
626
- if (deletePosition.offset + howMany > this.path[i]) {
627
- // If the next node of that path is removed return null
628
- // because the node containing this position got removed.
629
- return null;
630
- }
631
- else {
632
- // Otherwise, decrement index on that path.
633
- transformed.path[i] -= howMany;
634
- }
635
- }
636
- }
637
- return transformed;
638
- }
639
- /**
640
- * Returns a copy of this position that is updated by inserting `howMany` nodes at `insertPosition`.
641
- *
642
- * @internal
643
- * @param insertPosition Position where nodes are inserted.
644
- * @param howMany How many nodes are inserted.
645
- * @returns Transformed position.
646
- */
647
- _getTransformedByInsertion(insertPosition, howMany) {
648
- const transformed = ModelPosition._createAt(this);
649
- // This position can't be affected if insertion was in a different root.
650
- if (this.root != insertPosition.root) {
651
- return transformed;
652
- }
653
- if (compareArrays(insertPosition.getParentPath(), this.getParentPath()) == 'same') {
654
- // If nodes are inserted in the node that is pointed by this position...
655
- if (insertPosition.offset < this.offset || (insertPosition.offset == this.offset && this.stickiness != 'toPrevious')) {
656
- // And are inserted before an offset of that position...
657
- // "Push" this positions offset.
658
- transformed.offset += howMany;
659
- }
660
- }
661
- else if (compareArrays(insertPosition.getParentPath(), this.getParentPath()) == 'prefix') {
662
- // If nodes are inserted in a node that is on a path to this position...
663
- const i = insertPosition.path.length - 1;
664
- if (insertPosition.offset <= this.path[i]) {
665
- // And are inserted before next node of that path...
666
- // "Push" the index on that path.
667
- transformed.path[i] += howMany;
668
- }
669
- }
670
- return transformed;
671
- }
672
- /**
673
- * Returns a copy of this position that is updated by moving `howMany` nodes from `sourcePosition` to `targetPosition`.
674
- *
675
- * @internal
676
- * @param sourcePosition Position before the first element to move.
677
- * @param targetPosition Position where moved elements will be inserted.
678
- * @param howMany How many consecutive nodes to move, starting from `sourcePosition`.
679
- * @returns Transformed position.
680
- */
681
- _getTransformedByMove(sourcePosition, targetPosition, howMany) {
682
- // Update target position, as it could be affected by nodes removal.
683
- targetPosition = targetPosition._getTransformedByDeletion(sourcePosition, howMany);
684
- if (sourcePosition.isEqual(targetPosition)) {
685
- // If `targetPosition` is equal to `sourcePosition` this isn't really any move. Just return position as it is.
686
- return ModelPosition._createAt(this);
687
- }
688
- // Moving a range removes nodes from their original position. We acknowledge this by proper transformation.
689
- const transformed = this._getTransformedByDeletion(sourcePosition, howMany);
690
- const isMoved = transformed === null ||
691
- (sourcePosition.isEqual(this) && this.stickiness == 'toNext') ||
692
- (sourcePosition.getShiftedBy(howMany).isEqual(this) && this.stickiness == 'toPrevious');
693
- if (isMoved) {
694
- // This position is inside moved range (or sticks to it).
695
- // In this case, we calculate a combination of this position, move source position and target position.
696
- return this._getCombined(sourcePosition, targetPosition);
697
- }
698
- else {
699
- // This position is not inside a removed range.
700
- //
701
- // In next step, we simply reflect inserting `howMany` nodes, which might further affect the position.
702
- return transformed._getTransformedByInsertion(targetPosition, howMany);
703
- }
704
- }
705
- /**
706
- * Returns a new position that is a combination of this position and given positions.
707
- *
708
- * The combined position is a copy of this position transformed by moving a range starting at `source` position
709
- * to the `target` position. It is expected that this position is inside the moved range.
710
- *
711
- * Example:
712
- *
713
- * ```ts
714
- * let original = model.createPositionFromPath( root, [ 2, 3, 1 ] );
715
- * let source = model.createPositionFromPath( root, [ 2, 2 ] );
716
- * let target = model.createPositionFromPath( otherRoot, [ 1, 1, 3 ] );
717
- * original._getCombined( source, target ); // path is [ 1, 1, 4, 1 ], root is `otherRoot`
718
- * ```
719
- *
720
- * Explanation:
721
- *
722
- * We have a position `[ 2, 3, 1 ]` and move some nodes from `[ 2, 2 ]` to `[ 1, 1, 3 ]`. The original position
723
- * was inside moved nodes and now should point to the new place. The moved nodes will be after
724
- * positions `[ 1, 1, 3 ]`, `[ 1, 1, 4 ]`, `[ 1, 1, 5 ]`. Since our position was in the second moved node,
725
- * the transformed position will be in a sub-tree of a node at `[ 1, 1, 4 ]`. Looking at original path, we
726
- * took care of `[ 2, 3 ]` part of it. Now we have to add the rest of the original path to the transformed path.
727
- * Finally, the transformed position will point to `[ 1, 1, 4, 1 ]`.
728
- *
729
- * @internal
730
- * @param source Beginning of the moved range.
731
- * @param target Position where the range is moved.
732
- * @returns Combined position.
733
- */
734
- _getCombined(source, target) {
735
- const i = source.path.length - 1;
736
- // The first part of a path to combined position is a path to the place where nodes were moved.
737
- const combined = ModelPosition._createAt(target);
738
- combined.stickiness = this.stickiness;
739
- // Then we have to update the rest of the path.
740
- // Fix the offset because this position might be after `from` position and we have to reflect that.
741
- combined.offset = combined.offset + this.path[i] - source.offset;
742
- // Then, add the rest of the path.
743
- // If this position is at the same level as `from` position nothing will get added.
744
- combined.path = [...combined.path, ...this.path.slice(i + 1)];
745
- return combined;
746
- }
747
- /**
748
- * @inheritDoc
749
- */
750
- toJSON() {
751
- return {
752
- root: this.root.toJSON(),
753
- path: Array.from(this.path),
754
- stickiness: this.stickiness
755
- };
756
- }
757
- /**
758
- * Returns a new position that is equal to current position.
759
- */
760
- clone() {
761
- return new this.constructor(this.root, this.path, this.stickiness);
762
- }
763
- /**
764
- * Creates position at the given location. The location can be specified as:
765
- *
766
- * * a {@link module:engine/model/position~ModelPosition position},
767
- * * parent element and offset (offset defaults to `0`),
768
- * * parent element and `'end'` (sets position at the end of that element),
769
- * * {@link module:engine/model/item~ModelItem model item} and `'before'` or `'after'` (sets position before or after given model item).
770
- *
771
- * This method is a shortcut to other factory methods such as:
772
- *
773
- * * {@link module:engine/model/position~ModelPosition._createBefore},
774
- * * {@link module:engine/model/position~ModelPosition._createAfter}.
775
- *
776
- * @internal
777
- * @param offset Offset or one of the flags. Used only when the first parameter
778
- * is a {@link module:engine/model/item~ModelItem model item}.
779
- * @param stickiness Position stickiness. Used only when the first parameter is a {@link module:engine/model/item~ModelItem model item}.
780
- */
781
- static _createAt(itemOrPosition, offset, stickiness = 'toNone') {
782
- if (itemOrPosition.is('model:position')) {
783
- return new ModelPosition(itemOrPosition.root, itemOrPosition.path, itemOrPosition.stickiness);
784
- }
785
- else {
786
- const node = itemOrPosition;
787
- if (offset == 'end') {
788
- offset = node.maxOffset;
789
- }
790
- else if (offset == 'before') {
791
- return this._createBefore(node, stickiness);
792
- }
793
- else if (offset == 'after') {
794
- return this._createAfter(node, stickiness);
795
- }
796
- else if (offset !== 0 && !offset) {
797
- /**
798
- * {@link module:engine/model/model~Model#createPositionAt `Model#createPositionAt()`}
799
- * requires the offset to be specified when the first parameter is a model item.
800
- *
801
- * @error model-createpositionat-offset-required
802
- */
803
- throw new CKEditorError('model-createpositionat-offset-required', [this, itemOrPosition]);
804
- }
805
- if (!node.is('element') && !node.is('documentFragment')) {
806
- /**
807
- * Position parent have to be a model element or model document fragment.
808
- *
809
- * @error model-position-parent-incorrect
810
- */
811
- throw new CKEditorError('model-position-parent-incorrect', [this, itemOrPosition]);
812
- }
813
- const path = node.getPath();
814
- path.push(offset);
815
- return new this(node.root, path, stickiness);
816
- }
817
- }
818
- /**
819
- * Creates a new position, after given {@link module:engine/model/item~ModelItem model item}.
820
- *
821
- * @internal
822
- * @param item Item after which the position should be placed.
823
- * @param stickiness Position stickiness.
824
- */
825
- static _createAfter(item, stickiness) {
826
- if (!item.parent) {
827
- /**
828
- * You cannot make a position after a root element.
829
- *
830
- * @error model-position-after-root
831
- * @param {module:engine/model/rootelement~ModelRootElement} root The root element..
832
- */
833
- throw new CKEditorError('model-position-after-root', [this, item], { root: item });
834
- }
835
- return this._createAt(item.parent, item.endOffset, stickiness);
836
- }
837
- /**
838
- * Creates a new position, before the given {@link module:engine/model/item~ModelItem model item}.
839
- *
840
- * @internal
841
- * @param item Item before which the position should be placed.
842
- * @param stickiness Position stickiness.
843
- */
844
- static _createBefore(item, stickiness) {
845
- if (!item.parent) {
846
- /**
847
- * You cannot make a position before a root element.
848
- *
849
- * @error model-position-before-root
850
- * @param {module:engine/model/rootelement~ModelRootElement} root The root element..
851
- */
852
- throw new CKEditorError('model-position-before-root', item, { root: item });
853
- }
854
- return this._createAt(item.parent, item.startOffset, stickiness);
855
- }
856
- /**
857
- * Creates a `Position` instance from given plain object (i.e. parsed JSON string).
858
- *
859
- * @param json Plain object to be converted to `Position`.
860
- * @param doc Document object that will be position owner.
861
- * @returns `Position` instance created using given plain object.
862
- */
863
- static fromJSON(json, doc) {
864
- if (json.root === '$graveyard') {
865
- const pos = new ModelPosition(doc.graveyard, json.path);
866
- pos.stickiness = json.stickiness;
867
- return pos;
868
- }
869
- if (!doc.getRoot(json.root)) {
870
- /**
871
- * Cannot create position for document. Root with specified name does not exist.
872
- *
873
- * @error model-position-fromjson-no-root
874
- * @param {string} rootName The root name.
875
- */
876
- throw new CKEditorError('model-position-fromjson-no-root', doc, { rootName: json.root });
877
- }
878
- return new ModelPosition(doc.getRoot(json.root), json.path, json.stickiness);
879
- }
880
- }
881
- // The magic of type inference using `is` method is centralized in `TypeCheckable` class.
882
- // Proper overload would interfere with that.
883
- ModelPosition.prototype.is = function (type) {
884
- return type === 'position' || type === 'model:position';
885
- };
886
- /**
887
- * Returns a text node at the given position.
888
- *
889
- * This is a helper function optimized to reuse the position parent instance for performance reasons.
890
- *
891
- * Normally, you should use {@link module:engine/model/position~ModelPosition#textNode `Position#textNode`}.
892
- * If you start hitting performance issues with {@link module:engine/model/position~ModelPosition#parent `Position#parent`}
893
- * check if your algorithm does not access it multiple times (which can happen directly or indirectly via other position properties).
894
- *
895
- * See https://github.com/ckeditor/ckeditor5/issues/6579.
896
- *
897
- * See also:
898
- *
899
- * * {@link module:engine/model/position~getNodeAfterPosition}
900
- * * {@link module:engine/model/position~getNodeBeforePosition}
901
- *
902
- * @param position
903
- * @param positionParent The parent of the given position.
904
- * @internal
905
- */
906
- export function getTextNodeAtPosition(position, positionParent) {
907
- const node = positionParent.getChildAtOffset(position.offset);
908
- if (node && node.is('$text') && node.startOffset < position.offset) {
909
- return node;
910
- }
911
- return null;
912
- }
913
- /**
914
- * Returns the node after the given position.
915
- *
916
- * This is a helper function optimized to reuse the position parent instance and the calculation of the text node at the
917
- * specific position for performance reasons.
918
- *
919
- * Normally, you should use {@link module:engine/model/position~ModelPosition#nodeAfter `Position#nodeAfter`}.
920
- * If you start hitting performance issues with {@link module:engine/model/position~ModelPosition#parent `Position#parent`} and/or
921
- * {@link module:engine/model/position~ModelPosition#textNode `Position#textNode`}
922
- * check if your algorithm does not access those properties multiple times
923
- * (which can happen directly or indirectly via other position properties).
924
- *
925
- * See https://github.com/ckeditor/ckeditor5/issues/6579 and https://github.com/ckeditor/ckeditor5/issues/6582.
926
- *
927
- * See also:
928
- *
929
- * * {@link module:engine/model/position~getTextNodeAtPosition}
930
- * * {@link module:engine/model/position~getNodeBeforePosition}
931
- *
932
- * @param position Position to check.
933
- * @param positionParent The parent of the given position.
934
- * @param textNode Text node at the given position.
935
- * @internal
936
- */
937
- export function getNodeAfterPosition(position, positionParent, textNode) {
938
- if (textNode !== null) {
939
- return null;
940
- }
941
- return positionParent.getChildAtOffset(position.offset);
942
- }
943
- /**
944
- * Returns the node before the given position.
945
- *
946
- * Refer to {@link module:engine/model/position~getNodeBeforePosition} for documentation on when to use this util method.
947
- *
948
- * See also:
949
- *
950
- * * {@link module:engine/model/position~getTextNodeAtPosition}
951
- * * {@link module:engine/model/position~getNodeAfterPosition}
952
- *
953
- * @param position Position to check.
954
- * @param positionParent The parent of the given position.
955
- * @param textNode Text node at the given position.
956
- * @internal
957
- */
958
- export function getNodeBeforePosition(position, positionParent, textNode) {
959
- if (textNode !== null) {
960
- return null;
961
- }
962
- return positionParent.getChild(positionParent.offsetToIndex(position.offset) - 1);
963
- }
964
- /**
965
- * This is a helper function for `Position#isTouching()`.
966
- *
967
- * It checks whether to given positions are touching, considering that they have the same root and paths
968
- * until given level, and at given level they differ by 1 (so they are branching at `level` point).
969
- *
970
- * The exact requirements for touching positions are described in `Position#isTouching()` and also
971
- * in the body of this function.
972
- *
973
- * @param left Position "on the left" (it is before `right`).
974
- * @param right Position "on the right" (it is after `left`).
975
- * @param level Level on which the positions are different.
976
- */
977
- function checkTouchingBranch(left, right, level) {
978
- if (level + 1 === left.path.length) {
979
- // Left position does not have any more entries after the point where the positions differ.
980
- // [ 2 ] vs [ 3 ]
981
- // [ 2 ] vs [ 3, 0, 0 ]
982
- // The positions are spread by node at [ 2 ].
983
- return false;
984
- }
985
- if (!checkOnlyZeroes(right.path, level + 1)) {
986
- // Right position does not have only zeroes, so we have situation like:
987
- // [ 2, maxOffset ] vs [ 3, 1 ]
988
- // [ 2, maxOffset ] vs [ 3, 1, 0, 0 ]
989
- // The positions are spread by node at [ 3, 0 ].
990
- return false;
991
- }
992
- if (!checkOnlyMaxOffset(left, level + 1)) {
993
- // Left position does not have only max offsets, so we have situation like:
994
- // [ 2, 4 ] vs [ 3 ]
995
- // [ 2, 4 ] vs [ 3, 0, 0 ]
996
- // The positions are spread by node at [ 2, 5 ].
997
- return false;
998
- }
999
- // Left position has only max offsets and right position has only zeroes or nothing.
1000
- // [ 2, maxOffset ] vs [ 3 ]
1001
- // [ 2, maxOffset, maxOffset ] vs [ 3, 0 ]
1002
- // There are not elements between positions. The positions are touching.
1003
- return true;
1004
- }
1005
- /**
1006
- * Checks whether for given array, starting from given index until the end of the array, all items are `0`s.
1007
- *
1008
- * This is a helper function for `Position#isTouching()`.
1009
- */
1010
- function checkOnlyZeroes(arr, idx) {
1011
- while (idx < arr.length) {
1012
- if (arr[idx] !== 0) {
1013
- return false;
1014
- }
1015
- idx++;
1016
- }
1017
- return true;
1018
- }
1019
- /**
1020
- * Checks whether for given position, starting from given path level, whether the position is at the end of
1021
- * its parent and whether each element on the path to the position is also at at the end of its parent.
1022
- *
1023
- * This is a helper function for `Position#isTouching()`.
1024
- */
1025
- function checkOnlyMaxOffset(pos, level) {
1026
- let parent = pos.parent;
1027
- let idx = pos.path.length - 1;
1028
- let add = 0;
1029
- while (idx >= level) {
1030
- if (pos.path[idx] + add !== parent.maxOffset) {
1031
- return false;
1032
- }
1033
- // After the first check, we "go up", and check whether the position's parent-parent is the last element.
1034
- // However, we need to add 1 to the value in the path to "simulate" moving the path after the parent.
1035
- // It happens just once.
1036
- add = 1;
1037
- idx--;
1038
- parent = parent.parent;
1039
- }
1040
- return true;
1041
- }