@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
package/src/model/node.js DELETED
@@ -1,371 +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
- /* eslint-disable @typescript-eslint/no-unused-vars */
6
- /**
7
- * @module engine/model/node
8
- */
9
- import { ModelTypeCheckable } from './typecheckable.js';
10
- import { compareArrays, toMap } from '@ckeditor/ckeditor5-utils';
11
- /**
12
- * Model node. Most basic structure of model tree.
13
- *
14
- * This is an abstract class that is a base for other classes representing different nodes in model.
15
- *
16
- * **Note:** If a node is detached from the model tree, you can manipulate it using it's API.
17
- * However, it is **very important** that nodes already attached to model tree should be only changed through
18
- * {@link module:engine/model/writer~ModelWriter Writer API}.
19
- *
20
- * Changes done by `Node` methods, like {@link module:engine/model/element~ModelElement#_insertChild _insertChild} or
21
- * {@link module:engine/model/node~ModelNode#_setAttribute _setAttribute}
22
- * do not generate {@link module:engine/model/operation/operation~Operation operations}
23
- * which are essential for correct editor work if you modify nodes in {@link module:engine/model/document~ModelDocument document} root.
24
- *
25
- * The flow of working on `Node` (and classes that inherits from it) is as such:
26
- * 1. You can create a `Node` instance, modify it using it's API.
27
- * 2. Add `Node` to the model using `Batch` API.
28
- * 3. Change `Node` that was already added to the model using `Batch` API.
29
- *
30
- * Similarly, you cannot use `Batch` API on a node that has not been added to the model tree, with the exception
31
- * of {@link module:engine/model/writer~ModelWriter#insert inserting} that node to the model tree.
32
- *
33
- * Be aware that using {@link module:engine/model/writer~ModelWriter#remove remove from Batch API} does not allow to use `Node` API because
34
- * the information about `Node` is still kept in model document.
35
- *
36
- * In case of {@link module:engine/model/element~ModelElement element node}, adding and removing children also counts as changing a node and
37
- * follows same rules.
38
- */
39
- export class ModelNode extends ModelTypeCheckable {
40
- /**
41
- * Parent of this node. It could be {@link module:engine/model/element~ModelElement}
42
- * or {@link module:engine/model/documentfragment~ModelDocumentFragment}.
43
- * Equals to `null` if the node has no parent.
44
- */
45
- parent = null;
46
- /**
47
- * Attributes set on this node.
48
- */
49
- _attrs;
50
- /**
51
- * Index of this node in its parent or `null` if the node has no parent.
52
- *
53
- * @internal
54
- */
55
- _index = null;
56
- /**
57
- * Offset at which this node starts in its parent or `null` if the node has no parent.
58
- *
59
- * @internal
60
- */
61
- _startOffset = null;
62
- /**
63
- * Creates a model node.
64
- *
65
- * This is an abstract class, so this constructor should not be used directly.
66
- *
67
- * @param attrs Node's attributes. See {@link module:utils/tomap~toMap} for a list of accepted values.
68
- */
69
- constructor(attrs) {
70
- super();
71
- this._attrs = toMap(attrs);
72
- }
73
- /**
74
- * {@link module:engine/model/document~ModelDocument Document} that owns this root element.
75
- */
76
- get document() {
77
- return null;
78
- }
79
- /**
80
- * Index of this node in its parent or `null` if the node has no parent.
81
- */
82
- get index() {
83
- return this._index;
84
- }
85
- /**
86
- * Offset at which this node starts in its parent. It is equal to the sum of {@link #offsetSize offsetSize}
87
- * of all its previous siblings. Equals to `null` if node has no parent.
88
- */
89
- get startOffset() {
90
- return this._startOffset;
91
- }
92
- /**
93
- * Offset size of this node.
94
- *
95
- * Represents how much "offset space" is occupied by the node in its parent. It is important for
96
- * {@link module:engine/model/position~ModelPosition position}. When node has `offsetSize` greater
97
- * than `1`, position can be placed between that node start and end. `offsetSize` greater than `1` is for
98
- * nodes that represents more than one entity, i.e. a {@link module:engine/model/text~ModelText text node}.
99
- */
100
- get offsetSize() {
101
- return 1;
102
- }
103
- /**
104
- * Offset at which this node ends in its parent. It is equal to the sum of this node's
105
- * {@link module:engine/model/node~ModelNode#startOffset start offset} and {@link #offsetSize offset size}.
106
- * Equals to `null` if the node has no parent.
107
- */
108
- get endOffset() {
109
- if (this.startOffset === null) {
110
- return null;
111
- }
112
- return this.startOffset + this.offsetSize;
113
- }
114
- /**
115
- * Node's next sibling or `null` if the node is a last child of it's parent or if the node has no parent.
116
- */
117
- get nextSibling() {
118
- const index = this.index;
119
- return (index !== null && this.parent.getChild(index + 1)) || null;
120
- }
121
- /**
122
- * Node's previous sibling or `null` if the node is a first child of it's parent or if the node has no parent.
123
- */
124
- get previousSibling() {
125
- const index = this.index;
126
- return (index !== null && this.parent.getChild(index - 1)) || null;
127
- }
128
- /**
129
- * The top-most ancestor of the node. If node has no parent it is the root itself. If the node is a part
130
- * of {@link module:engine/model/documentfragment~ModelDocumentFragment}, it's `root` is equal to that `DocumentFragment`.
131
- */
132
- get root() {
133
- // eslint-disable-next-line @typescript-eslint/no-this-alias, consistent-this
134
- let root = this;
135
- while (root.parent) {
136
- root = root.parent;
137
- }
138
- return root;
139
- }
140
- /**
141
- * Returns `true` if the node is inside a document root that is attached to the document.
142
- */
143
- isAttached() {
144
- // If the node has no parent it means that it is a root.
145
- // But this is not a `RootElement`, so it means that it is not attached.
146
- //
147
- // If this is not the root, check if this element's root is attached.
148
- return this.parent === null ? false : this.root.isAttached();
149
- }
150
- /**
151
- * Gets path to the node. The path is an array containing starting offsets of consecutive ancestors of this node,
152
- * beginning from {@link module:engine/model/node~ModelNode#root root}, down to this node's starting offset. The path can be used to
153
- * create {@link module:engine/model/position~ModelPosition Position} instance.
154
- *
155
- * ```ts
156
- * const abc = new Text( 'abc' );
157
- * const foo = new Text( 'foo' );
158
- * const h1 = new ModelElement( 'h1', null, new Text( 'header' ) );
159
- * const p = new ModelElement( 'p', null, [ abc, foo ] );
160
- * const div = new ModelElement( 'div', null, [ h1, p ] );
161
- * foo.getPath(); // Returns [ 1, 3 ]. `foo` is in `p` which is in `div`. `p` starts at offset 1, while `foo` at 3.
162
- * h1.getPath(); // Returns [ 0 ].
163
- * div.getPath(); // Returns [].
164
- * ```
165
- */
166
- getPath() {
167
- const path = [];
168
- // eslint-disable-next-line @typescript-eslint/no-this-alias, consistent-this
169
- let node = this;
170
- while (node.parent) {
171
- path.unshift(node.startOffset);
172
- node = node.parent;
173
- }
174
- return path;
175
- }
176
- /**
177
- * Returns ancestors array of this node.
178
- *
179
- * @param options Options object.
180
- * @param options.includeSelf When set to `true` this node will be also included in parent's array.
181
- * @param options.parentFirst When set to `true`, array will be sorted from node's parent to root element,
182
- * otherwise root element will be the first item in the array.
183
- * @returns Array with ancestors.
184
- */
185
- getAncestors(options = {}) {
186
- const ancestors = [];
187
- let parent = options.includeSelf ? this : this.parent;
188
- while (parent) {
189
- ancestors[options.parentFirst ? 'push' : 'unshift'](parent);
190
- parent = parent.parent;
191
- }
192
- return ancestors;
193
- }
194
- /**
195
- * Returns a {@link module:engine/model/element~ModelElement} or {@link module:engine/model/documentfragment~ModelDocumentFragment}
196
- * which is a common ancestor of both nodes.
197
- *
198
- * @param node The second node.
199
- * @param options Options object.
200
- * @param options.includeSelf When set to `true` both nodes will be considered "ancestors" too.
201
- * Which means that if e.g. node A is inside B, then their common ancestor will be B.
202
- */
203
- getCommonAncestor(node, options = {}) {
204
- const ancestorsA = this.getAncestors(options);
205
- const ancestorsB = node.getAncestors(options);
206
- let i = 0;
207
- while (ancestorsA[i] == ancestorsB[i] && ancestorsA[i]) {
208
- i++;
209
- }
210
- return i === 0 ? null : ancestorsA[i - 1];
211
- }
212
- /**
213
- * Returns whether this node is before given node. `false` is returned if nodes are in different trees (for example,
214
- * in different {@link module:engine/model/documentfragment~ModelDocumentFragment}s).
215
- *
216
- * @param node Node to compare with.
217
- */
218
- isBefore(node) {
219
- // Given node is not before this node if they are same.
220
- if (this == node) {
221
- return false;
222
- }
223
- // Return `false` if it is impossible to compare nodes.
224
- if (this.root !== node.root) {
225
- return false;
226
- }
227
- const thisPath = this.getPath();
228
- const nodePath = node.getPath();
229
- const result = compareArrays(thisPath, nodePath);
230
- switch (result) {
231
- case 'prefix':
232
- return true;
233
- case 'extension':
234
- return false;
235
- default:
236
- return thisPath[result] < nodePath[result];
237
- }
238
- }
239
- /**
240
- * Returns whether this node is after given node. `false` is returned if nodes are in different trees (for example,
241
- * in different {@link module:engine/model/documentfragment~ModelDocumentFragment}s).
242
- *
243
- * @param node Node to compare with.
244
- */
245
- isAfter(node) {
246
- // Given node is not before this node if they are same.
247
- if (this == node) {
248
- return false;
249
- }
250
- // Return `false` if it is impossible to compare nodes.
251
- if (this.root !== node.root) {
252
- return false;
253
- }
254
- // In other cases, just check if the `node` is before, and return the opposite.
255
- return !this.isBefore(node);
256
- }
257
- /**
258
- * Checks if the node has an attribute with given key.
259
- *
260
- * @param key Key of attribute to check.
261
- * @returns `true` if attribute with given key is set on node, `false` otherwise.
262
- */
263
- hasAttribute(key) {
264
- return this._attrs.has(key);
265
- }
266
- /**
267
- * Gets an attribute value for given key or `undefined` if that attribute is not set on node.
268
- *
269
- * @param key Key of attribute to look for.
270
- * @returns Attribute value or `undefined`.
271
- */
272
- getAttribute(key) {
273
- return this._attrs.get(key);
274
- }
275
- /**
276
- * Returns iterator that iterates over this node's attributes.
277
- *
278
- * Attributes are returned as arrays containing two items. First one is attribute key and second is attribute value.
279
- * This format is accepted by native `Map` object and also can be passed in `Node` constructor.
280
- */
281
- getAttributes() {
282
- return this._attrs.entries();
283
- }
284
- /**
285
- * Returns iterator that iterates over this node's attribute keys.
286
- */
287
- getAttributeKeys() {
288
- return this._attrs.keys();
289
- }
290
- /**
291
- * Converts `Node` to plain object and returns it.
292
- *
293
- * @returns `Node` converted to plain object.
294
- */
295
- toJSON() {
296
- const json = {};
297
- // Serializes attributes to the object.
298
- // attributes = { a: 'foo', b: 1, c: true }.
299
- if (this._attrs.size) {
300
- json.attributes = Array.from(this._attrs).reduce((result, attr) => {
301
- result[attr[0]] = attr[1];
302
- return result;
303
- }, {});
304
- }
305
- return json;
306
- }
307
- /**
308
- * Creates a copy of this node, that is a node with exactly same attributes, and returns it.
309
- *
310
- * @internal
311
- * @returns Node with same attributes as this node.
312
- */
313
- _clone(_deep) {
314
- return new this.constructor(this._attrs);
315
- }
316
- /**
317
- * Removes this node from its parent.
318
- *
319
- * @internal
320
- * @see module:engine/model/writer~ModelWriter#remove
321
- */
322
- _remove() {
323
- this.parent._removeChildren(this.index);
324
- }
325
- /**
326
- * Sets attribute on the node. If attribute with the same key already is set, it's value is overwritten.
327
- *
328
- * @see module:engine/model/writer~ModelWriter#setAttribute
329
- * @internal
330
- * @param key Key of attribute to set.
331
- * @param value Attribute value.
332
- */
333
- _setAttribute(key, value) {
334
- this._attrs.set(key, value);
335
- }
336
- /**
337
- * Removes all attributes from the node and sets given attributes.
338
- *
339
- * @see module:engine/model/writer~ModelWriter#setAttributes
340
- * @internal
341
- * @param attrs Attributes to set. See {@link module:utils/tomap~toMap} for a list of accepted values.
342
- */
343
- _setAttributesTo(attrs) {
344
- this._attrs = toMap(attrs);
345
- }
346
- /**
347
- * Removes an attribute with given key from the node.
348
- *
349
- * @see module:engine/model/writer~ModelWriter#removeAttribute
350
- * @internal
351
- * @param key Key of attribute to remove.
352
- * @returns `true` if the attribute was set on the element, `false` otherwise.
353
- */
354
- _removeAttribute(key) {
355
- return this._attrs.delete(key);
356
- }
357
- /**
358
- * Removes all attributes from the node.
359
- *
360
- * @see module:engine/model/writer~ModelWriter#clearAttributes
361
- * @internal
362
- */
363
- _clearAttributes() {
364
- this._attrs.clear();
365
- }
366
- }
367
- // The magic of type inference using `is` method is centralized in `TypeCheckable` class.
368
- // Proper overload would interfere with that.
369
- ModelNode.prototype.is = function (type) {
370
- return type === 'node' || type === 'model:node';
371
- };
@@ -1,244 +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/nodelist
7
- */
8
- import { ModelNode } from './node.js';
9
- import { CKEditorError, spliceArray } from '@ckeditor/ckeditor5-utils';
10
- /**
11
- * Provides an interface to operate on a list of {@link module:engine/model/node~ModelNode nodes}. `NodeList` is used internally
12
- * in classes like {@link module:engine/model/element~ModelElement Element}
13
- * or {@link module:engine/model/documentfragment~ModelDocumentFragment ModelDocumentFragment}.
14
- */
15
- export class ModelNodeList {
16
- /**
17
- * Nodes contained in this node list.
18
- */
19
- _nodes = [];
20
- /**
21
- * This array maps numbers (offsets) to node that is placed at that offset.
22
- *
23
- * This array is similar to `_nodes` with the difference that one node may occupy multiple consecutive items in the array.
24
- *
25
- * This array is needed to quickly retrieve a node that is placed at given offset.
26
- */
27
- _offsetToNode = [];
28
- /**
29
- * Creates a node list.
30
- *
31
- * @internal
32
- * @param nodes Nodes contained in this node list.
33
- */
34
- constructor(nodes) {
35
- if (nodes) {
36
- this._insertNodes(0, nodes);
37
- }
38
- }
39
- /**
40
- * Iterable interface.
41
- *
42
- * Iterates over all nodes contained inside this node list.
43
- */
44
- [Symbol.iterator]() {
45
- return this._nodes[Symbol.iterator]();
46
- }
47
- /**
48
- * Number of nodes contained inside this node list.
49
- */
50
- get length() {
51
- return this._nodes.length;
52
- }
53
- /**
54
- * Sum of {@link module:engine/model/node~ModelNode#offsetSize offset sizes} of all nodes contained inside this node list.
55
- */
56
- get maxOffset() {
57
- return this._offsetToNode.length;
58
- }
59
- /**
60
- * Gets the node at the given index. Returns `null` if incorrect index was passed.
61
- */
62
- getNode(index) {
63
- return this._nodes[index] || null;
64
- }
65
- /**
66
- * Gets the node at the given offset. Returns `null` if incorrect offset was passed.
67
- */
68
- getNodeAtOffset(offset) {
69
- return this._offsetToNode[offset] || null;
70
- }
71
- /**
72
- * Returns an index of the given node or `null` if given node does not have a parent.
73
- *
74
- * This is an alias to {@link module:engine/model/node~ModelNode#index}.
75
- */
76
- getNodeIndex(node) {
77
- return node.index;
78
- }
79
- /**
80
- * Returns the offset at which given node is placed in its parent or `null` if given node does not have a parent.
81
- *
82
- * This is an alias to {@link module:engine/model/node~ModelNode#startOffset}.
83
- */
84
- getNodeStartOffset(node) {
85
- return node.startOffset;
86
- }
87
- /**
88
- * Converts index to offset in node list.
89
- *
90
- * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `model-nodelist-index-out-of-bounds` if given index is less
91
- * than `0` or more than {@link #length}.
92
- */
93
- indexToOffset(index) {
94
- if (index == this._nodes.length) {
95
- return this.maxOffset;
96
- }
97
- const node = this._nodes[index];
98
- if (!node) {
99
- /**
100
- * Given index cannot be found in the node list.
101
- *
102
- * @error model-nodelist-index-out-of-bounds
103
- */
104
- throw new CKEditorError('model-nodelist-index-out-of-bounds', this);
105
- }
106
- return this.getNodeStartOffset(node);
107
- }
108
- /**
109
- * Converts offset in node list to index.
110
- *
111
- * Throws {@link module:utils/ckeditorerror~CKEditorError CKEditorError} `model-nodelist-offset-out-of-bounds` if given offset is less
112
- * than `0` or more than {@link #maxOffset}.
113
- */
114
- offsetToIndex(offset) {
115
- if (offset == this._offsetToNode.length) {
116
- return this._nodes.length;
117
- }
118
- const node = this._offsetToNode[offset];
119
- if (!node) {
120
- /**
121
- * Given offset cannot be found in the node list.
122
- *
123
- * @error model-nodelist-offset-out-of-bounds
124
- * @param {number} offset The offset value.
125
- * @param {module:engine/model/nodelist~ModelNodeList} nodeList Stringified node list.
126
- */
127
- throw new CKEditorError('model-nodelist-offset-out-of-bounds', this, {
128
- offset,
129
- nodeList: this
130
- });
131
- }
132
- return this.getNodeIndex(node);
133
- }
134
- /**
135
- * Inserts given nodes at given index.
136
- *
137
- * @internal
138
- * @param index Index at which nodes should be inserted.
139
- * @param nodes Nodes to be inserted.
140
- */
141
- _insertNodes(index, nodes) {
142
- const nodesArray = [];
143
- // Validation.
144
- for (const node of nodes) {
145
- if (!(node instanceof ModelNode)) {
146
- /**
147
- * Trying to insert an object which is not a Node instance.
148
- *
149
- * @error model-nodelist-insertnodes-not-node
150
- */
151
- throw new CKEditorError('model-nodelist-insertnodes-not-node', this);
152
- }
153
- nodesArray.push(node);
154
- }
155
- let offset = this.indexToOffset(index);
156
- // Splice nodes array and offsets array into the nodelist.
157
- spliceArray(this._nodes, nodesArray, index);
158
- spliceArray(this._offsetToNode, makeOffsetsArray(nodesArray), offset);
159
- // Refresh indexes and offsets for nodes inside this node list. We need to do this for all inserted nodes and all nodes after them.
160
- for (let i = index; i < this._nodes.length; i++) {
161
- this._nodes[i]._index = i;
162
- this._nodes[i]._startOffset = offset;
163
- offset += this._nodes[i].offsetSize;
164
- }
165
- }
166
- /**
167
- * Removes one or more nodes starting at the given index.
168
- *
169
- * @internal
170
- * @param indexStart Index of the first node to remove.
171
- * @param howMany Number of nodes to remove.
172
- * @returns Array containing removed nodes.
173
- */
174
- _removeNodes(indexStart, howMany = 1) {
175
- if (howMany == 0) {
176
- return [];
177
- }
178
- // Remove nodes from this nodelist.
179
- let offset = this.indexToOffset(indexStart);
180
- const nodes = this._nodes.splice(indexStart, howMany);
181
- const lastNode = nodes[nodes.length - 1];
182
- const removedOffsetSum = lastNode.startOffset + lastNode.offsetSize - offset;
183
- this._offsetToNode.splice(offset, removedOffsetSum);
184
- // Reset index and start offset properties for the removed nodes -- they do not have a parent anymore.
185
- for (const node of nodes) {
186
- node._index = null;
187
- node._startOffset = null;
188
- }
189
- for (let i = indexStart; i < this._nodes.length; i++) {
190
- this._nodes[i]._index = i;
191
- this._nodes[i]._startOffset = offset;
192
- offset += this._nodes[i].offsetSize;
193
- }
194
- return nodes;
195
- }
196
- /**
197
- * Removes children nodes provided as an array. These nodes do not need to be direct siblings.
198
- *
199
- * This method is faster than removing nodes one by one, as it recalculates offsets only once.
200
- *
201
- * @internal
202
- * @param nodes Array of nodes.
203
- */
204
- _removeNodesArray(nodes) {
205
- if (nodes.length == 0) {
206
- return;
207
- }
208
- for (const node of nodes) {
209
- node._index = null;
210
- node._startOffset = null;
211
- }
212
- this._nodes = this._nodes.filter(node => node.index !== null);
213
- this._offsetToNode = this._offsetToNode.filter(node => node.index !== null);
214
- let offset = 0;
215
- for (let i = 0; i < this._nodes.length; i++) {
216
- this._nodes[i]._index = i;
217
- this._nodes[i]._startOffset = offset;
218
- offset += this._nodes[i].offsetSize;
219
- }
220
- }
221
- /**
222
- * Converts `NodeList` instance to an array containing nodes that were inserted in the node list. Nodes
223
- * are also converted to their plain object representation.
224
- *
225
- * @returns `NodeList` instance converted to `Array`.
226
- */
227
- toJSON() {
228
- return this._nodes.map(node => node.toJSON());
229
- }
230
- }
231
- /**
232
- * Creates an array of nodes in the format as in {@link module:engine/model/nodelist~ModelNodeList#_offsetToNode}, i.e. one node will
233
- * occupy multiple items if its offset size is greater than one.
234
- */
235
- function makeOffsetsArray(nodes) {
236
- const offsets = [];
237
- let index = 0;
238
- for (const node of nodes) {
239
- for (let i = 0; i < node.offsetSize; i++) {
240
- offsets[index++] = node;
241
- }
242
- }
243
- return offsets;
244
- }