@ckeditor/ckeditor5-engine 36.0.1 → 37.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 (200) hide show
  1. package/README.md +1 -1
  2. package/package.json +24 -23
  3. package/src/controller/datacontroller.d.ts +331 -0
  4. package/src/controller/datacontroller.js +62 -109
  5. package/src/controller/editingcontroller.d.ts +98 -0
  6. package/src/controller/editingcontroller.js +22 -46
  7. package/src/conversion/conversion.d.ts +476 -0
  8. package/src/conversion/conversion.js +328 -347
  9. package/src/conversion/conversionhelpers.d.ts +26 -0
  10. package/src/conversion/conversionhelpers.js +1 -5
  11. package/src/conversion/downcastdispatcher.d.ts +547 -0
  12. package/src/conversion/downcastdispatcher.js +74 -152
  13. package/src/conversion/downcasthelpers.d.ts +1226 -0
  14. package/src/conversion/downcasthelpers.js +843 -762
  15. package/src/conversion/mapper.d.ts +499 -0
  16. package/src/conversion/mapper.js +84 -99
  17. package/src/conversion/modelconsumable.d.ts +201 -0
  18. package/src/conversion/modelconsumable.js +96 -99
  19. package/src/conversion/upcastdispatcher.d.ts +492 -0
  20. package/src/conversion/upcastdispatcher.js +73 -100
  21. package/src/conversion/upcasthelpers.d.ts +499 -0
  22. package/src/conversion/upcasthelpers.js +406 -373
  23. package/src/conversion/viewconsumable.d.ts +177 -0
  24. package/src/conversion/viewconsumable.js +157 -162
  25. package/src/dataprocessor/basichtmlwriter.d.ts +18 -0
  26. package/src/dataprocessor/basichtmlwriter.js +0 -9
  27. package/src/dataprocessor/dataprocessor.d.ts +61 -0
  28. package/src/dataprocessor/htmldataprocessor.d.ts +76 -0
  29. package/src/dataprocessor/htmldataprocessor.js +6 -28
  30. package/src/dataprocessor/htmlwriter.d.ts +16 -0
  31. package/src/dataprocessor/xmldataprocessor.d.ts +90 -0
  32. package/src/dataprocessor/xmldataprocessor.js +8 -40
  33. package/src/dev-utils/model.d.ts +124 -0
  34. package/src/dev-utils/model.js +41 -38
  35. package/src/dev-utils/operationreplayer.d.ts +51 -0
  36. package/src/dev-utils/operationreplayer.js +6 -14
  37. package/src/dev-utils/utils.d.ts +37 -0
  38. package/src/dev-utils/utils.js +5 -18
  39. package/src/dev-utils/view.d.ts +319 -0
  40. package/src/dev-utils/view.js +205 -226
  41. package/src/index.d.ts +105 -0
  42. package/src/index.js +1 -0
  43. package/src/model/batch.d.ts +106 -0
  44. package/src/model/differ.d.ts +329 -0
  45. package/src/model/document.d.ts +245 -0
  46. package/src/model/document.js +1 -1
  47. package/src/model/documentfragment.d.ts +196 -0
  48. package/src/model/documentfragment.js +2 -2
  49. package/src/model/documentselection.d.ts +420 -0
  50. package/src/model/element.d.ts +165 -0
  51. package/src/model/history.d.ts +114 -0
  52. package/src/model/item.d.ts +14 -0
  53. package/src/model/liveposition.d.ts +77 -0
  54. package/src/model/liverange.d.ts +102 -0
  55. package/src/model/markercollection.d.ts +335 -0
  56. package/src/model/model.d.ts +812 -0
  57. package/src/model/model.js +59 -30
  58. package/src/model/node.d.ts +256 -0
  59. package/src/model/nodelist.d.ts +91 -0
  60. package/src/model/operation/attributeoperation.d.ts +98 -0
  61. package/src/model/operation/detachoperation.d.ts +55 -0
  62. package/src/model/operation/insertoperation.d.ts +85 -0
  63. package/src/model/operation/markeroperation.d.ts +86 -0
  64. package/src/model/operation/mergeoperation.d.ts +95 -0
  65. package/src/model/operation/moveoperation.d.ts +91 -0
  66. package/src/model/operation/nooperation.d.ts +33 -0
  67. package/src/model/operation/operation.d.ts +89 -0
  68. package/src/model/operation/operationfactory.d.ts +18 -0
  69. package/src/model/operation/renameoperation.d.ts +78 -0
  70. package/src/model/operation/rootattributeoperation.d.ts +97 -0
  71. package/src/model/operation/rootattributeoperation.js +1 -1
  72. package/src/model/operation/splitoperation.d.ts +104 -0
  73. package/src/model/operation/transform.d.ts +100 -0
  74. package/src/model/operation/utils.d.ts +71 -0
  75. package/src/model/position.d.ts +539 -0
  76. package/src/model/position.js +1 -1
  77. package/src/model/range.d.ts +458 -0
  78. package/src/model/range.js +1 -1
  79. package/src/model/rootelement.d.ts +40 -0
  80. package/src/model/schema.d.ts +1176 -0
  81. package/src/model/schema.js +15 -15
  82. package/src/model/selection.d.ts +472 -0
  83. package/src/model/text.d.ts +66 -0
  84. package/src/model/text.js +0 -2
  85. package/src/model/textproxy.d.ts +144 -0
  86. package/src/model/treewalker.d.ts +186 -0
  87. package/src/model/treewalker.js +19 -10
  88. package/src/model/typecheckable.d.ts +255 -0
  89. package/src/model/utils/autoparagraphing.d.ts +37 -0
  90. package/src/model/utils/deletecontent.d.ts +58 -0
  91. package/src/model/utils/findoptimalinsertionrange.d.ts +32 -0
  92. package/src/model/utils/getselectedcontent.d.ts +30 -0
  93. package/src/model/utils/insertcontent.d.ts +46 -0
  94. package/src/model/utils/insertcontent.js +2 -12
  95. package/src/model/utils/insertobject.d.ts +44 -0
  96. package/src/model/utils/insertobject.js +3 -14
  97. package/src/model/utils/modifyselection.d.ts +48 -0
  98. package/src/model/utils/selection-post-fixer.d.ts +65 -0
  99. package/src/model/writer.d.ts +726 -0
  100. package/src/model/writer.js +6 -4
  101. package/src/view/attributeelement.d.ts +108 -0
  102. package/src/view/attributeelement.js +25 -69
  103. package/src/view/containerelement.d.ts +49 -0
  104. package/src/view/containerelement.js +10 -43
  105. package/src/view/datatransfer.d.ts +75 -0
  106. package/src/view/document.d.ts +184 -0
  107. package/src/view/document.js +15 -84
  108. package/src/view/documentfragment.d.ts +150 -0
  109. package/src/view/documentfragment.js +40 -81
  110. package/src/view/documentselection.d.ts +219 -0
  111. package/src/view/documentselection.js +75 -121
  112. package/src/view/domconverter.d.ts +620 -0
  113. package/src/view/domconverter.js +159 -276
  114. package/src/view/downcastwriter.d.ts +804 -0
  115. package/src/view/downcastwriter.js +336 -380
  116. package/src/view/editableelement.d.ts +52 -0
  117. package/src/view/editableelement.js +9 -49
  118. package/src/view/element.d.ts +466 -0
  119. package/src/view/element.js +150 -222
  120. package/src/view/elementdefinition.d.ts +87 -0
  121. package/src/view/emptyelement.d.ts +41 -0
  122. package/src/view/emptyelement.js +11 -44
  123. package/src/view/filler.d.ts +111 -0
  124. package/src/view/filler.js +24 -21
  125. package/src/view/item.d.ts +14 -0
  126. package/src/view/matcher.d.ts +486 -0
  127. package/src/view/matcher.js +247 -218
  128. package/src/view/node.d.ts +160 -0
  129. package/src/view/node.js +26 -100
  130. package/src/view/observer/arrowkeysobserver.d.ts +41 -0
  131. package/src/view/observer/arrowkeysobserver.js +0 -13
  132. package/src/view/observer/bubblingemittermixin.d.ts +166 -0
  133. package/src/view/observer/bubblingemittermixin.js +36 -25
  134. package/src/view/observer/bubblingeventinfo.d.ts +47 -0
  135. package/src/view/observer/bubblingeventinfo.js +3 -29
  136. package/src/view/observer/clickobserver.d.ts +43 -0
  137. package/src/view/observer/clickobserver.js +9 -19
  138. package/src/view/observer/compositionobserver.d.ts +82 -0
  139. package/src/view/observer/compositionobserver.js +13 -42
  140. package/src/view/observer/domeventdata.d.ts +50 -0
  141. package/src/view/observer/domeventdata.js +5 -30
  142. package/src/view/observer/domeventobserver.d.ts +69 -0
  143. package/src/view/observer/domeventobserver.js +19 -21
  144. package/src/view/observer/fakeselectionobserver.d.ts +46 -0
  145. package/src/view/observer/fakeselectionobserver.js +2 -15
  146. package/src/view/observer/focusobserver.d.ts +82 -0
  147. package/src/view/observer/focusobserver.js +14 -40
  148. package/src/view/observer/inputobserver.d.ts +86 -0
  149. package/src/view/observer/inputobserver.js +18 -64
  150. package/src/view/observer/keyobserver.d.ts +67 -0
  151. package/src/view/observer/keyobserver.js +8 -42
  152. package/src/view/observer/mouseobserver.d.ts +89 -0
  153. package/src/view/observer/mouseobserver.js +8 -28
  154. package/src/view/observer/mutationobserver.d.ts +82 -0
  155. package/src/view/observer/mutationobserver.js +7 -37
  156. package/src/view/observer/observer.d.ts +84 -0
  157. package/src/view/observer/observer.js +12 -25
  158. package/src/view/observer/selectionobserver.d.ts +143 -0
  159. package/src/view/observer/selectionobserver.js +9 -99
  160. package/src/view/observer/tabobserver.d.ts +42 -0
  161. package/src/view/observer/tabobserver.js +0 -14
  162. package/src/view/placeholder.d.ts +85 -0
  163. package/src/view/placeholder.js +26 -43
  164. package/src/view/position.d.ts +189 -0
  165. package/src/view/position.js +36 -83
  166. package/src/view/range.d.ts +279 -0
  167. package/src/view/range.js +79 -122
  168. package/src/view/rawelement.d.ts +73 -0
  169. package/src/view/rawelement.js +34 -48
  170. package/src/view/renderer.d.ts +265 -0
  171. package/src/view/renderer.js +105 -193
  172. package/src/view/rooteditableelement.d.ts +41 -0
  173. package/src/view/rooteditableelement.js +12 -40
  174. package/src/view/selection.d.ts +375 -0
  175. package/src/view/selection.js +79 -153
  176. package/src/view/styles/background.d.ts +33 -0
  177. package/src/view/styles/background.js +14 -12
  178. package/src/view/styles/border.d.ts +43 -0
  179. package/src/view/styles/border.js +58 -48
  180. package/src/view/styles/margin.d.ts +29 -0
  181. package/src/view/styles/margin.js +13 -11
  182. package/src/view/styles/padding.d.ts +29 -0
  183. package/src/view/styles/padding.js +13 -11
  184. package/src/view/styles/utils.d.ts +93 -0
  185. package/src/view/styles/utils.js +22 -48
  186. package/src/view/stylesmap.d.ts +675 -0
  187. package/src/view/stylesmap.js +249 -244
  188. package/src/view/text.d.ts +74 -0
  189. package/src/view/text.js +16 -46
  190. package/src/view/textproxy.d.ts +97 -0
  191. package/src/view/textproxy.js +10 -59
  192. package/src/view/treewalker.d.ts +195 -0
  193. package/src/view/treewalker.js +43 -106
  194. package/src/view/typecheckable.d.ts +401 -0
  195. package/src/view/uielement.d.ts +96 -0
  196. package/src/view/uielement.js +28 -62
  197. package/src/view/upcastwriter.d.ts +328 -0
  198. package/src/view/upcastwriter.js +124 -134
  199. package/src/view/view.d.ts +327 -0
  200. package/src/view/view.js +79 -150
@@ -24,33 +24,17 @@ export default class Position extends TypeCheckable {
24
24
  /**
25
25
  * Creates a position.
26
26
  *
27
- * @param {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment} parent Position parent.
28
- * @param {Number} offset Position offset.
27
+ * @param parent Position parent.
28
+ * @param offset Position offset.
29
29
  */
30
30
  constructor(parent, offset) {
31
31
  super();
32
- /**
33
- * Position parent.
34
- *
35
- * @readonly
36
- * @member {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
37
- * module:engine/view/position~Position#parent
38
- */
39
32
  this.parent = parent;
40
- /**
41
- * Position offset.
42
- *
43
- * @readonly
44
- * @member {Number} module:engine/view/position~Position#offset
45
- */
46
33
  this.offset = offset;
47
34
  }
48
35
  /**
49
36
  * Node directly after the position. Equals `null` when there is no node after position or position is located
50
37
  * inside text node.
51
- *
52
- * @readonly
53
- * @type {module:engine/view/node~Node|null}
54
38
  */
55
39
  get nodeAfter() {
56
40
  if (this.parent.is('$text')) {
@@ -61,9 +45,6 @@ export default class Position extends TypeCheckable {
61
45
  /**
62
46
  * Node directly before the position. Equals `null` when there is no node before position or position is located
63
47
  * inside text node.
64
- *
65
- * @readonly
66
- * @type {module:engine/view/node~Node|null}
67
48
  */
68
49
  get nodeBefore() {
69
50
  if (this.parent.is('$text')) {
@@ -73,18 +54,12 @@ export default class Position extends TypeCheckable {
73
54
  }
74
55
  /**
75
56
  * Is `true` if position is at the beginning of its {@link module:engine/view/position~Position#parent parent}, `false` otherwise.
76
- *
77
- * @readonly
78
- * @type {Boolean}
79
57
  */
80
58
  get isAtStart() {
81
59
  return this.offset === 0;
82
60
  }
83
61
  /**
84
62
  * Is `true` if position is at the end of its {@link module:engine/view/position~Position#parent parent}, `false` otherwise.
85
- *
86
- * @readonly
87
- * @type {Boolean}
88
63
  */
89
64
  get isAtEnd() {
90
65
  const endOffset = this.parent.is('$text') ? this.parent.data.length : this.parent.childCount;
@@ -92,9 +67,6 @@ export default class Position extends TypeCheckable {
92
67
  }
93
68
  /**
94
69
  * Position's root, that is the root of the position's parent element.
95
- *
96
- * @readonly
97
- * @type {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment}
98
70
  */
99
71
  get root() {
100
72
  return this.parent.root;
@@ -102,8 +74,6 @@ export default class Position extends TypeCheckable {
102
74
  /**
103
75
  * {@link module:engine/view/editableelement~EditableElement EditableElement} instance that contains this position, or `null` if
104
76
  * position is not inside an editable element.
105
- *
106
- * @type {module:engine/view/editableelement~EditableElement|null}
107
77
  */
108
78
  get editableElement() {
109
79
  let editable = this.parent;
@@ -120,8 +90,8 @@ export default class Position extends TypeCheckable {
120
90
  /**
121
91
  * Returns a new instance of Position with offset incremented by `shift` value.
122
92
  *
123
- * @param {Number} shift How position offset should get changed. Accepts negative values.
124
- * @returns {module:engine/view/position~Position} Shifted position.
93
+ * @param shift How position offset should get changed. Accepts negative values.
94
+ * @returns Shifted position.
125
95
  */
126
96
  getShiftedBy(shift) {
127
97
  const shifted = Position._createAt(this);
@@ -135,15 +105,16 @@ export default class Position extends TypeCheckable {
135
105
  *
136
106
  * For example:
137
107
  *
138
- * getLastMatchingPosition( value => value.type == 'text' ); // <p>{}foo</p> -> <p>foo[]</p>
139
- * getLastMatchingPosition( value => value.type == 'text', { direction: 'backward' } ); // <p>foo[]</p> -> <p>{}foo</p>
140
- * getLastMatchingPosition( value => false ); // Do not move the position.
108
+ * ```ts
109
+ * getLastMatchingPosition( value => value.type == 'text' ); // <p>{}foo</p> -> <p>foo[]</p>
110
+ * getLastMatchingPosition( value => value.type == 'text', { direction: 'backward' } ); // <p>foo[]</p> -> <p>{}foo</p>
111
+ * getLastMatchingPosition( value => false ); // Do not move the position.
112
+ * ```
141
113
  *
142
- * @param {Function} skip Callback function. Gets {@link module:engine/view/treewalker~type TreeWalkerValue} and should
114
+ * @param skip Callback function. Gets {@link module:engine/view/treewalker~type TreeWalkerValue} and should
143
115
  * return `true` if the value should be skipped or `false` if not.
144
- * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
145
- *
146
- * @returns {module:engine/view/position~Position} The position after the last item which matches the `skip` callback test.
116
+ * @param options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
117
+ * @returns The position after the last item which matches the `skip` callback test.
147
118
  */
148
119
  getLastMatchingPosition(skip, options = {}) {
149
120
  options.startPosition = this;
@@ -154,7 +125,7 @@ export default class Position extends TypeCheckable {
154
125
  /**
155
126
  * Returns ancestors array of this position, that is this position's parent and it's ancestors.
156
127
  *
157
- * @returns {Array} Array with ancestors.
128
+ * @returns Array with ancestors.
158
129
  */
159
130
  getAncestors() {
160
131
  if (this.parent.is('documentFragment')) {
@@ -167,9 +138,6 @@ export default class Position extends TypeCheckable {
167
138
  /**
168
139
  * Returns a {@link module:engine/view/node~Node} or {@link module:engine/view/documentfragment~DocumentFragment}
169
140
  * which is a common ancestor of both positions.
170
- *
171
- * @param {module:engine/view/position~Position} position
172
- * @returns {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment|null}
173
141
  */
174
142
  getCommonAncestor(position) {
175
143
  const ancestorsA = this.getAncestors();
@@ -183,8 +151,8 @@ export default class Position extends TypeCheckable {
183
151
  /**
184
152
  * Checks whether this position equals given position.
185
153
  *
186
- * @param {module:engine/view/position~Position} otherPosition Position to compare with.
187
- * @returns {Boolean} True if positions are same.
154
+ * @param otherPosition Position to compare with.
155
+ * @returns True if positions are same.
188
156
  */
189
157
  isEqual(otherPosition) {
190
158
  return (this.parent == otherPosition.parent && this.offset == otherPosition.offset);
@@ -196,8 +164,8 @@ export default class Position extends TypeCheckable {
196
164
  *
197
165
  * @see module:engine/view/position~Position#isAfter
198
166
  * @see module:engine/view/position~Position#compareWith
199
- * @param {module:engine/view/position~Position} otherPosition Position to compare with.
200
- * @returns {Boolean} Returns `true` if this position is before given position.
167
+ * @param otherPosition Position to compare with.
168
+ * @returns Returns `true` if this position is before given position.
201
169
  */
202
170
  isBefore(otherPosition) {
203
171
  return this.compareWith(otherPosition) == 'before';
@@ -209,8 +177,8 @@ export default class Position extends TypeCheckable {
209
177
  *
210
178
  * @see module:engine/view/position~Position#isBefore
211
179
  * @see module:engine/view/position~Position#compareWith
212
- * @param {module:engine/view/position~Position} otherPosition Position to compare with.
213
- * @returns {Boolean} Returns `true` if this position is after given position.
180
+ * @param otherPosition Position to compare with.
181
+ * @returns Returns `true` if this position is after given position.
214
182
  */
215
183
  isAfter(otherPosition) {
216
184
  return this.compareWith(otherPosition) == 'after';
@@ -219,8 +187,7 @@ export default class Position extends TypeCheckable {
219
187
  * Checks whether this position is before, after or in same position that other position. Two positions may be also
220
188
  * different when they are located in separate roots.
221
189
  *
222
- * @param {module:engine/view/position~Position} otherPosition Position to compare with.
223
- * @returns {module:engine/view/position~PositionRelation}
190
+ * @param otherPosition Position to compare with.
224
191
  */
225
192
  compareWith(otherPosition) {
226
193
  if (this.root !== otherPosition.root) {
@@ -242,6 +209,10 @@ export default class Position extends TypeCheckable {
242
209
  return 'before';
243
210
  case 'extension':
244
211
  return 'after';
212
+ /* istanbul ignore next */
213
+ case 'same':
214
+ // Already covered by `this.isEqual` above. Added so TypeScript can infer `result` as number in `default` case.
215
+ return 'same';
245
216
  default:
246
217
  return thisPath[result] < otherPath[result] ? 'before' : 'after';
247
218
  }
@@ -249,16 +220,15 @@ export default class Position extends TypeCheckable {
249
220
  /**
250
221
  * Creates a {@link module:engine/view/treewalker~TreeWalker TreeWalker} instance with this positions as a start position.
251
222
  *
252
- * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}
253
- * @param {module:engine/view/range~Range} [options.boundaries=null] Range to define boundaries of the iterator.
254
- * @param {Boolean} [options.singleCharacters=false]
255
- * @param {Boolean} [options.shallow=false]
256
- * @param {Boolean} [options.ignoreElementEnd=false]
223
+ * @param options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}
257
224
  */
258
225
  getWalker(options = {}) {
259
226
  options.startPosition = this;
260
227
  return new TreeWalker(options);
261
228
  }
229
+ /**
230
+ * Clones this position.
231
+ */
262
232
  clone() {
263
233
  return new Position(this.parent, this.offset);
264
234
  }
@@ -275,10 +245,8 @@ export default class Position extends TypeCheckable {
275
245
  * * {@link module:engine/view/position~Position._createBefore},
276
246
  * * {@link module:engine/view/position~Position._createAfter}.
277
247
  *
278
- * @protected
279
- * @param {module:engine/view/item~Item|module:engine/view/position~Position} itemOrPosition
280
- * @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
281
- * first parameter is a {@link module:engine/view/item~Item view item}.
248
+ * @internal
249
+ * @param offset Offset or one of the flags. Used only when first parameter is a {@link module:engine/view/item~Item view item}.
282
250
  */
283
251
  static _createAt(itemOrPosition, offset) {
284
252
  if (itemOrPosition instanceof Position) {
@@ -310,9 +278,8 @@ export default class Position extends TypeCheckable {
310
278
  /**
311
279
  * Creates a new position after given view item.
312
280
  *
313
- * @protected
314
- * @param {module:engine/view/item~Item} item View item after which the position should be located.
315
- * @returns {module:engine/view/position~Position}
281
+ * @internal
282
+ * @param item View item after which the position should be located.
316
283
  */
317
284
  static _createAfter(item) {
318
285
  // TextProxy is not a instance of Node so we need do handle it in specific way.
@@ -333,9 +300,8 @@ export default class Position extends TypeCheckable {
333
300
  /**
334
301
  * Creates a new position before given view item.
335
302
  *
336
- * @protected
337
- * @param {module:engine/view/item~Item} item View item before which the position should be located.
338
- * @returns {module:engine/view/position~Position}
303
+ * @internal
304
+ * @param item View item before which the position should be located.
339
305
  */
340
306
  static _createBefore(item) {
341
307
  // TextProxy is not a instance of Node so we need do handle it in specific way.
@@ -354,21 +320,8 @@ export default class Position extends TypeCheckable {
354
320
  return new Position(item.parent, item.index);
355
321
  }
356
322
  }
357
- /**
358
- * Checks whether this object is of the given type.
359
- *
360
- * position.is( 'position' ); // -> true
361
- * position.is( 'view:position' ); // -> true
362
- *
363
- * position.is( 'model:position' ); // -> false
364
- * position.is( 'element' ); // -> false
365
- * position.is( 'range' ); // -> false
366
- *
367
- * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
368
- *
369
- * @param {String} type
370
- * @returns {Boolean}
371
- */
323
+ // The magic of type inference using `is` method is centralized in `TypeCheckable` class.
324
+ // Proper overload would interfere with that.
372
325
  Position.prototype.is = function (type) {
373
326
  return type === 'position' || type === 'view:position';
374
327
  };
@@ -0,0 +1,279 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module engine/view/range
7
+ */
8
+ import TypeCheckable from './typecheckable';
9
+ import Position from './position';
10
+ import type DocumentFragment from './documentfragment';
11
+ import type Element from './element';
12
+ import type Item from './item';
13
+ import type Node from './node';
14
+ import { default as TreeWalker, type TreeWalkerValue, type TreeWalkerOptions } from './treewalker';
15
+ /**
16
+ * Range in the view tree. A range is represented by its start and end {@link module:engine/view/position~Position positions}.
17
+ *
18
+ * In order to create a new position instance use the `createPosition*()` factory methods available in:
19
+ *
20
+ * * {@link module:engine/view/view~View}
21
+ * * {@link module:engine/view/downcastwriter~DowncastWriter}
22
+ * * {@link module:engine/view/upcastwriter~UpcastWriter}
23
+ */
24
+ export default class Range extends TypeCheckable implements Iterable<TreeWalkerValue> {
25
+ /**
26
+ * Start position.
27
+ */
28
+ readonly start: Position;
29
+ /**
30
+ * End position.
31
+ */
32
+ readonly end: Position;
33
+ /**
34
+ * Creates a range spanning from `start` position to `end` position.
35
+ *
36
+ * **Note:** Constructor creates it's own {@link module:engine/view/position~Position} instances basing on passed values.
37
+ *
38
+ * @param start Start position.
39
+ * @param end End position. If not set, range will be collapsed at the `start` position.
40
+ */
41
+ constructor(start: Position, end?: Position | null);
42
+ /**
43
+ * Iterable interface.
44
+ *
45
+ * Iterates over all {@link module:engine/view/item~Item view items} that are in this range and returns
46
+ * them together with additional information like length or {@link module:engine/view/position~Position positions},
47
+ * grouped as {@link module:engine/view/treewalker~TreeWalkerValue}.
48
+ *
49
+ * This iterator uses {@link module:engine/view/treewalker~TreeWalker TreeWalker} with `boundaries` set to this range and
50
+ * `ignoreElementEnd` option
51
+ * set to `true`.
52
+ */
53
+ [Symbol.iterator](): IterableIterator<TreeWalkerValue>;
54
+ /**
55
+ * Returns whether the range is collapsed, that is it start and end positions are equal.
56
+ */
57
+ get isCollapsed(): boolean;
58
+ /**
59
+ * Returns whether this range is flat, that is if {@link module:engine/view/range~Range#start start} position and
60
+ * {@link module:engine/view/range~Range#end end} position are in the same {@link module:engine/view/position~Position#parent parent}.
61
+ */
62
+ get isFlat(): boolean;
63
+ /**
64
+ * Range root element.
65
+ */
66
+ get root(): Node | DocumentFragment;
67
+ /**
68
+ * Creates a maximal range that has the same content as this range but is expanded in both ways (at the beginning
69
+ * and at the end).
70
+ *
71
+ * For example:
72
+ *
73
+ * ```html
74
+ * <p>Foo</p><p><b>{Bar}</b></p> -> <p>Foo</p>[<p><b>Bar</b>]</p>
75
+ * <p><b>foo</b>{bar}<span></span></p> -> <p><b>foo[</b>bar<span></span>]</p>
76
+ * ```
77
+ *
78
+ * Note that in the sample above:
79
+ *
80
+ * - `<p>` have type of {@link module:engine/view/containerelement~ContainerElement},
81
+ * - `<b>` have type of {@link module:engine/view/attributeelement~AttributeElement},
82
+ * - `<span>` have type of {@link module:engine/view/uielement~UIElement}.
83
+ *
84
+ * @returns Enlarged range.
85
+ */
86
+ getEnlarged(): Range;
87
+ /**
88
+ * Creates a minimum range that has the same content as this range but is trimmed in both ways (at the beginning
89
+ * and at the end).
90
+ *
91
+ * For example:
92
+ *
93
+ * ```html
94
+ * <p>Foo</p>[<p><b>Bar</b>]</p> -> <p>Foo</p><p><b>{Bar}</b></p>
95
+ * <p><b>foo[</b>bar<span></span>]</p> -> <p><b>foo</b>{bar}<span></span></p>
96
+ * ```
97
+ *
98
+ * Note that in the sample above:
99
+ *
100
+ * - `<p>` have type of {@link module:engine/view/containerelement~ContainerElement},
101
+ * - `<b>` have type of {@link module:engine/view/attributeelement~AttributeElement},
102
+ * - `<span>` have type of {@link module:engine/view/uielement~UIElement}.
103
+ *
104
+ * @returns Shrunk range.
105
+ */
106
+ getTrimmed(): Range;
107
+ /**
108
+ * Two ranges are equal if their start and end positions are equal.
109
+ *
110
+ * @param otherRange Range to compare with.
111
+ * @returns `true` if ranges are equal, `false` otherwise
112
+ */
113
+ isEqual(otherRange: Range): boolean;
114
+ /**
115
+ * Checks whether this range contains given {@link module:engine/view/position~Position position}.
116
+ *
117
+ * @param position Position to check.
118
+ * @returns `true` if given {@link module:engine/view/position~Position position} is contained in this range, `false` otherwise.
119
+ */
120
+ containsPosition(position: Position): boolean;
121
+ /**
122
+ * Checks whether this range contains given {@link module:engine/view/range~Range range}.
123
+ *
124
+ * @param otherRange Range to check.
125
+ * @param loose Whether the check is loose or strict. If the check is strict (`false`), compared range cannot
126
+ * start or end at the same position as this range boundaries. If the check is loose (`true`), compared range can start, end or
127
+ * even be equal to this range. Note that collapsed ranges are always compared in strict mode.
128
+ * @returns `true` if given {@link module:engine/view/range~Range range} boundaries are contained by this range, `false`
129
+ * otherwise.
130
+ */
131
+ containsRange(otherRange: Range, loose?: boolean): boolean;
132
+ /**
133
+ * Computes which part(s) of this {@link module:engine/view/range~Range range} is not a part of given
134
+ * {@link module:engine/view/range~Range range}.
135
+ * Returned array contains zero, one or two {@link module:engine/view/range~Range ranges}.
136
+ *
137
+ * Examples:
138
+ *
139
+ * ```ts
140
+ * let foo = downcastWriter.createText( 'foo' );
141
+ * let img = downcastWriter.createContainerElement( 'img' );
142
+ * let bar = downcastWriter.createText( 'bar' );
143
+ * let p = downcastWriter.createContainerElement( 'p', null, [ foo, img, bar ] );
144
+ *
145
+ * let range = view.createRange( view.createPositionAt( foo, 2 ), view.createPositionAt( bar, 1 ); // "o", img, "b" are in range.
146
+ * let otherRange = view.createRange( // "oo", img, "ba" are in range.
147
+ * view.createPositionAt( foo, 1 ),
148
+ * view.createPositionAt( bar, 2 )
149
+ * );
150
+ * let transformed = range.getDifference( otherRange );
151
+ * // transformed array has no ranges because `otherRange` contains `range`
152
+ *
153
+ * otherRange = view.createRange( view.createPositionAt( foo, 1 ), view.createPositionAt( p, 2 ); // "oo", img are in range.
154
+ * transformed = range.getDifference( otherRange );
155
+ * // transformed array has one range: from ( p, 2 ) to ( bar, 1 )
156
+ *
157
+ * otherRange = view.createRange( view.createPositionAt( p, 1 ), view.createPositionAt( p, 2 ) ); // img is in range.
158
+ * transformed = range.getDifference( otherRange );
159
+ * // transformed array has two ranges: from ( foo, 1 ) to ( p, 1 ) and from ( p, 2 ) to ( bar, 1 )
160
+ * ```
161
+ *
162
+ * @param otherRange Range to differentiate against.
163
+ * @returns The difference between ranges.
164
+ */
165
+ getDifference(otherRange: Range): Array<Range>;
166
+ /**
167
+ * Returns an intersection of this {@link module:engine/view/range~Range range} and given {@link module:engine/view/range~Range range}.
168
+ * Intersection is a common part of both of those ranges. If ranges has no common part, returns `null`.
169
+ *
170
+ * Examples:
171
+ *
172
+ * ```ts
173
+ * let foo = downcastWriter.createText( 'foo' );
174
+ * let img = downcastWriter.createContainerElement( 'img' );
175
+ * let bar = downcastWriter.createText( 'bar' );
176
+ * let p = downcastWriter.createContainerElement( 'p', null, [ foo, img, bar ] );
177
+ *
178
+ * let range = view.createRange( view.createPositionAt( foo, 2 ), view.createPositionAt( bar, 1 ); // "o", img, "b" are in range.
179
+ * let otherRange = view.createRange( view.createPositionAt( foo, 1 ), view.createPositionAt( p, 2 ); // "oo", img are in range.
180
+ * let transformed = range.getIntersection( otherRange ); // range from ( foo, 1 ) to ( p, 2 ).
181
+ *
182
+ * otherRange = view.createRange( view.createPositionAt( bar, 1 ), view.createPositionAt( bar, 3 ); "ar" is in range.
183
+ * transformed = range.getIntersection( otherRange ); // null - no common part.
184
+ * ```
185
+ *
186
+ * @param otherRange Range to check for intersection.
187
+ * @returns A common part of given ranges or `null` if ranges have no common part.
188
+ */
189
+ getIntersection(otherRange: Range): Range | null;
190
+ /**
191
+ * Creates a {@link module:engine/view/treewalker~TreeWalker TreeWalker} instance with this range as a boundary.
192
+ *
193
+ * @param options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
194
+ */
195
+ getWalker(options?: TreeWalkerOptions): TreeWalker;
196
+ /**
197
+ * Returns a {@link module:engine/view/node~Node} or {@link module:engine/view/documentfragment~DocumentFragment}
198
+ * which is a common ancestor of range's both ends (in which the entire range is contained).
199
+ */
200
+ getCommonAncestor(): Node | DocumentFragment | null;
201
+ /**
202
+ * Returns an {@link module:engine/view/element~Element Element} contained by the range.
203
+ * The element will be returned when it is the **only** node within the range and **fully–contained**
204
+ * at the same time.
205
+ */
206
+ getContainedElement(): Element | null;
207
+ /**
208
+ * Clones this range.
209
+ */
210
+ clone(): Range;
211
+ /**
212
+ * Returns an iterator that iterates over all {@link module:engine/view/item~Item view items} that are in this range and returns
213
+ * them.
214
+ *
215
+ * This method uses {@link module:engine/view/treewalker~TreeWalker} with `boundaries` set to this range and `ignoreElementEnd` option
216
+ * set to `true`. However it returns only {@link module:engine/view/item~Item items},
217
+ * not {@link module:engine/view/treewalker~TreeWalkerValue}.
218
+ *
219
+ * You may specify additional options for the tree walker. See {@link module:engine/view/treewalker~TreeWalker} for
220
+ * a full list of available options.
221
+ *
222
+ * @param options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
223
+ */
224
+ getItems(options?: TreeWalkerOptions): IterableIterator<Item>;
225
+ /**
226
+ * Returns an iterator that iterates over all {@link module:engine/view/position~Position positions} that are boundaries or
227
+ * contained in this range.
228
+ *
229
+ * This method uses {@link module:engine/view/treewalker~TreeWalker} with `boundaries` set to this range. However it returns only
230
+ * {@link module:engine/view/position~Position positions}, not {@link module:engine/view/treewalker~TreeWalkerValue}.
231
+ *
232
+ * You may specify additional options for the tree walker. See {@link module:engine/view/treewalker~TreeWalker} for
233
+ * a full list of available options.
234
+ *
235
+ * @param options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
236
+ */
237
+ getPositions(options?: TreeWalkerOptions): IterableIterator<Position>;
238
+ /**
239
+ * Checks and returns whether this range intersects with the given range.
240
+ *
241
+ * @param otherRange Range to compare with.
242
+ * @returns True if ranges intersect.
243
+ */
244
+ isIntersecting(otherRange: Range): boolean;
245
+ /**
246
+ * Creates a range from the given parents and offsets.
247
+ *
248
+ * @internal
249
+ * @param startElement Start position parent element.
250
+ * @param startOffset Start position offset.
251
+ * @param endElement End position parent element.
252
+ * @param endOffset End position offset.
253
+ * @returns Created range.
254
+ */
255
+ static _createFromParentsAndOffsets(startElement: Element | DocumentFragment, startOffset: number, endElement: Element | DocumentFragment, endOffset: number): Range;
256
+ /**
257
+ * Creates a new range, spreading from specified {@link module:engine/view/position~Position position} to a position moved by
258
+ * given `shift`. If `shift` is a negative value, shifted position is treated as the beginning of the range.
259
+ *
260
+ * @internal
261
+ * @param position Beginning of the range.
262
+ * @param shift How long the range should be.
263
+ */
264
+ static _createFromPositionAndShift(position: Position, shift: number): Range;
265
+ /**
266
+ * Creates a range inside an {@link module:engine/view/element~Element element} which starts before the first child of
267
+ * that element and ends after the last child of that element.
268
+ *
269
+ * @internal
270
+ * @param element Element which is a parent for the range.
271
+ */
272
+ static _createIn(element: Element | DocumentFragment): Range;
273
+ /**
274
+ * Creates a range that starts before given {@link module:engine/view/item~Item view item} and ends after it.
275
+ *
276
+ * @internal
277
+ */
278
+ static _createOn(item: Item): Range;
279
+ }