@ckeditor/ckeditor5-engine 34.2.0 → 35.1.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 (125) hide show
  1. package/CHANGELOG.md +823 -0
  2. package/LICENSE.md +4 -0
  3. package/package.json +32 -25
  4. package/src/controller/datacontroller.js +467 -561
  5. package/src/controller/editingcontroller.js +168 -204
  6. package/src/conversion/conversion.js +541 -565
  7. package/src/conversion/conversionhelpers.js +24 -28
  8. package/src/conversion/downcastdispatcher.js +457 -686
  9. package/src/conversion/downcasthelpers.js +1583 -1965
  10. package/src/conversion/mapper.js +518 -707
  11. package/src/conversion/modelconsumable.js +240 -283
  12. package/src/conversion/upcastdispatcher.js +372 -718
  13. package/src/conversion/upcasthelpers.js +707 -818
  14. package/src/conversion/viewconsumable.js +524 -581
  15. package/src/dataprocessor/basichtmlwriter.js +12 -16
  16. package/src/dataprocessor/dataprocessor.js +5 -0
  17. package/src/dataprocessor/htmldataprocessor.js +101 -117
  18. package/src/dataprocessor/htmlwriter.js +1 -18
  19. package/src/dataprocessor/xmldataprocessor.js +117 -138
  20. package/src/dev-utils/model.js +260 -352
  21. package/src/dev-utils/operationreplayer.js +106 -126
  22. package/src/dev-utils/utils.js +34 -51
  23. package/src/dev-utils/view.js +632 -753
  24. package/src/index.js +0 -11
  25. package/src/model/batch.js +111 -127
  26. package/src/model/differ.js +988 -1233
  27. package/src/model/document.js +340 -449
  28. package/src/model/documentfragment.js +327 -364
  29. package/src/model/documentselection.js +996 -1189
  30. package/src/model/element.js +306 -410
  31. package/src/model/history.js +224 -262
  32. package/src/model/item.js +5 -0
  33. package/src/model/liveposition.js +84 -145
  34. package/src/model/liverange.js +108 -185
  35. package/src/model/markercollection.js +379 -480
  36. package/src/model/model.js +883 -1034
  37. package/src/model/node.js +419 -463
  38. package/src/model/nodelist.js +175 -201
  39. package/src/model/operation/attributeoperation.js +153 -182
  40. package/src/model/operation/detachoperation.js +64 -83
  41. package/src/model/operation/insertoperation.js +135 -166
  42. package/src/model/operation/markeroperation.js +114 -140
  43. package/src/model/operation/mergeoperation.js +163 -191
  44. package/src/model/operation/moveoperation.js +157 -187
  45. package/src/model/operation/nooperation.js +28 -38
  46. package/src/model/operation/operation.js +106 -125
  47. package/src/model/operation/operationfactory.js +30 -34
  48. package/src/model/operation/renameoperation.js +109 -135
  49. package/src/model/operation/rootattributeoperation.js +155 -188
  50. package/src/model/operation/splitoperation.js +196 -232
  51. package/src/model/operation/transform.js +1833 -2204
  52. package/src/model/operation/utils.js +140 -204
  53. package/src/model/position.js +899 -1053
  54. package/src/model/range.js +910 -1028
  55. package/src/model/rootelement.js +77 -97
  56. package/src/model/schema.js +1189 -1835
  57. package/src/model/selection.js +745 -862
  58. package/src/model/text.js +90 -114
  59. package/src/model/textproxy.js +204 -240
  60. package/src/model/treewalker.js +316 -397
  61. package/src/model/typecheckable.js +16 -0
  62. package/src/model/utils/autoparagraphing.js +32 -44
  63. package/src/model/utils/deletecontent.js +334 -418
  64. package/src/model/utils/findoptimalinsertionrange.js +25 -36
  65. package/src/model/utils/getselectedcontent.js +96 -118
  66. package/src/model/utils/insertcontent.js +654 -773
  67. package/src/model/utils/insertobject.js +96 -119
  68. package/src/model/utils/modifyselection.js +120 -158
  69. package/src/model/utils/selection-post-fixer.js +153 -201
  70. package/src/model/writer.js +1305 -1474
  71. package/src/view/attributeelement.js +189 -225
  72. package/src/view/containerelement.js +75 -85
  73. package/src/view/document.js +172 -215
  74. package/src/view/documentfragment.js +200 -249
  75. package/src/view/documentselection.js +338 -367
  76. package/src/view/domconverter.js +1371 -1613
  77. package/src/view/downcastwriter.js +1747 -2076
  78. package/src/view/editableelement.js +81 -97
  79. package/src/view/element.js +739 -890
  80. package/src/view/elementdefinition.js +5 -0
  81. package/src/view/emptyelement.js +82 -92
  82. package/src/view/filler.js +35 -50
  83. package/src/view/item.js +5 -0
  84. package/src/view/matcher.js +260 -559
  85. package/src/view/node.js +274 -360
  86. package/src/view/observer/arrowkeysobserver.js +19 -28
  87. package/src/view/observer/bubblingemittermixin.js +120 -263
  88. package/src/view/observer/bubblingeventinfo.js +47 -55
  89. package/src/view/observer/clickobserver.js +7 -13
  90. package/src/view/observer/compositionobserver.js +14 -24
  91. package/src/view/observer/domeventdata.js +57 -67
  92. package/src/view/observer/domeventobserver.js +40 -64
  93. package/src/view/observer/fakeselectionobserver.js +81 -96
  94. package/src/view/observer/focusobserver.js +45 -61
  95. package/src/view/observer/inputobserver.js +7 -13
  96. package/src/view/observer/keyobserver.js +17 -27
  97. package/src/view/observer/mouseobserver.js +7 -14
  98. package/src/view/observer/mutationobserver.js +220 -315
  99. package/src/view/observer/observer.js +81 -102
  100. package/src/view/observer/selectionobserver.js +191 -246
  101. package/src/view/observer/tabobserver.js +23 -36
  102. package/src/view/placeholder.js +128 -173
  103. package/src/view/position.js +350 -401
  104. package/src/view/range.js +453 -513
  105. package/src/view/rawelement.js +85 -112
  106. package/src/view/renderer.js +874 -1014
  107. package/src/view/rooteditableelement.js +80 -90
  108. package/src/view/selection.js +608 -689
  109. package/src/view/styles/background.js +43 -44
  110. package/src/view/styles/border.js +220 -276
  111. package/src/view/styles/margin.js +8 -17
  112. package/src/view/styles/padding.js +8 -16
  113. package/src/view/styles/utils.js +127 -160
  114. package/src/view/stylesmap.js +728 -905
  115. package/src/view/text.js +102 -126
  116. package/src/view/textproxy.js +144 -170
  117. package/src/view/treewalker.js +383 -479
  118. package/src/view/typecheckable.js +19 -0
  119. package/src/view/uielement.js +166 -187
  120. package/src/view/upcastwriter.js +395 -449
  121. package/src/view/view.js +569 -664
  122. package/src/dataprocessor/dataprocessor.jsdoc +0 -64
  123. package/src/model/item.jsdoc +0 -14
  124. package/src/view/elementdefinition.jsdoc +0 -59
  125. package/src/view/item.jsdoc +0 -14
package/src/view/range.js CHANGED
@@ -2,14 +2,12 @@
2
2
  * @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
-
6
5
  /**
7
6
  * @module engine/view/range
8
7
  */
9
-
8
+ import TypeCheckable from './typecheckable';
10
9
  import Position from './position';
11
- import TreeWalker from './treewalker';
12
-
10
+ import { default as TreeWalker } from './treewalker';
13
11
  /**
14
12
  * Range in the view tree. A range is represented by its start and end {@link module:engine/view/position~Position positions}.
15
13
  *
@@ -19,515 +17,457 @@ import TreeWalker from './treewalker';
19
17
  * * {@link module:engine/view/downcastwriter~DowncastWriter}
20
18
  * * {@link module:engine/view/upcastwriter~UpcastWriter}
21
19
  */
22
- export default class Range {
23
- /**
24
- * Creates a range spanning from `start` position to `end` position.
25
- *
26
- * **Note:** Constructor creates it's own {@link module:engine/view/position~Position} instances basing on passed values.
27
- *
28
- * @param {module:engine/view/position~Position} start Start position.
29
- * @param {module:engine/view/position~Position} [end] End position. If not set, range will be collapsed at the `start` position.
30
- */
31
- constructor( start, end = null ) {
32
- /**
33
- * Start position.
34
- *
35
- * @readonly
36
- * @member {module:engine/view/position~Position}
37
- */
38
- this.start = start.clone();
39
-
40
- /**
41
- * End position.
42
- *
43
- * @readonly
44
- * @member {module:engine/view/position~Position}
45
- */
46
- this.end = end ? end.clone() : start.clone();
47
- }
48
-
49
- /**
50
- * Iterable interface.
51
- *
52
- * Iterates over all {@link module:engine/view/item~Item view items} that are in this range and returns
53
- * them together with additional information like length or {@link module:engine/view/position~Position positions},
54
- * grouped as {@link module:engine/view/treewalker~TreeWalkerValue}.
55
- *
56
- * This iterator uses {@link module:engine/view/treewalker~TreeWalker TreeWalker} with `boundaries` set to this range and
57
- * `ignoreElementEnd` option
58
- * set to `true`.
59
- *
60
- * @returns {Iterable.<module:engine/view/treewalker~TreeWalkerValue>}
61
- */
62
- * [ Symbol.iterator ]() {
63
- yield* new TreeWalker( { boundaries: this, ignoreElementEnd: true } );
64
- }
65
-
66
- /**
67
- * Returns whether the range is collapsed, that is it start and end positions are equal.
68
- *
69
- * @type {Boolean}
70
- */
71
- get isCollapsed() {
72
- return this.start.isEqual( this.end );
73
- }
74
-
75
- /**
76
- * Returns whether this range is flat, that is if {@link module:engine/view/range~Range#start start} position and
77
- * {@link module:engine/view/range~Range#end end} position are in the same {@link module:engine/view/position~Position#parent parent}.
78
- *
79
- * @type {Boolean}
80
- */
81
- get isFlat() {
82
- return this.start.parent === this.end.parent;
83
- }
84
-
85
- /**
86
- * Range root element.
87
- *
88
- * @type {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment}
89
- */
90
- get root() {
91
- return this.start.root;
92
- }
93
-
94
- /**
95
- * Creates a maximal range that has the same content as this range but is expanded in both ways (at the beginning
96
- * and at the end).
97
- *
98
- * For example:
99
- *
100
- * <p>Foo</p><p><b>{Bar}</b></p> -> <p>Foo</p>[<p><b>Bar</b>]</p>
101
- * <p><b>foo</b>{bar}<span></span></p> -> <p><b>foo[</b>bar<span></span>]</p>
102
- *
103
- * Note that in the sample above:
104
- *
105
- * - `<p>` have type of {@link module:engine/view/containerelement~ContainerElement},
106
- * - `<b>` have type of {@link module:engine/view/attributeelement~AttributeElement},
107
- * - `<span>` have type of {@link module:engine/view/uielement~UIElement}.
108
- *
109
- * @returns {module:engine/view/range~Range} Enlarged range.
110
- */
111
- getEnlarged() {
112
- let start = this.start.getLastMatchingPosition( enlargeTrimSkip, { direction: 'backward' } );
113
- let end = this.end.getLastMatchingPosition( enlargeTrimSkip );
114
-
115
- // Fix positions, in case if they are in Text node.
116
- if ( start.parent.is( '$text' ) && start.isAtStart ) {
117
- start = Position._createBefore( start.parent );
118
- }
119
-
120
- if ( end.parent.is( '$text' ) && end.isAtEnd ) {
121
- end = Position._createAfter( end.parent );
122
- }
123
-
124
- return new Range( start, end );
125
- }
126
-
127
- /**
128
- * Creates a minimum range that has the same content as this range but is trimmed in both ways (at the beginning
129
- * and at the end).
130
- *
131
- * For example:
132
- *
133
- * <p>Foo</p>[<p><b>Bar</b>]</p> -> <p>Foo</p><p><b>{Bar}</b></p>
134
- * <p><b>foo[</b>bar<span></span>]</p> -> <p><b>foo</b>{bar}<span></span></p>
135
- *
136
- * Note that in the sample above:
137
- *
138
- * - `<p>` have type of {@link module:engine/view/containerelement~ContainerElement},
139
- * - `<b>` have type of {@link module:engine/view/attributeelement~AttributeElement},
140
- * - `<span>` have type of {@link module:engine/view/uielement~UIElement}.
141
- *
142
- * @returns {module:engine/view/range~Range} Shrink range.
143
- */
144
- getTrimmed() {
145
- let start = this.start.getLastMatchingPosition( enlargeTrimSkip );
146
-
147
- if ( start.isAfter( this.end ) || start.isEqual( this.end ) ) {
148
- return new Range( start, start );
149
- }
150
-
151
- let end = this.end.getLastMatchingPosition( enlargeTrimSkip, { direction: 'backward' } );
152
- const nodeAfterStart = start.nodeAfter;
153
- const nodeBeforeEnd = end.nodeBefore;
154
-
155
- // Because TreeWalker prefers positions next to text node, we need to move them manually into these text nodes.
156
- if ( nodeAfterStart && nodeAfterStart.is( '$text' ) ) {
157
- start = new Position( nodeAfterStart, 0 );
158
- }
159
-
160
- if ( nodeBeforeEnd && nodeBeforeEnd.is( '$text' ) ) {
161
- end = new Position( nodeBeforeEnd, nodeBeforeEnd.data.length );
162
- }
163
-
164
- return new Range( start, end );
165
- }
166
-
167
- /**
168
- * Two ranges are equal if their start and end positions are equal.
169
- *
170
- * @param {module:engine/view/range~Range} otherRange Range to compare with.
171
- * @returns {Boolean} `true` if ranges are equal, `false` otherwise
172
- */
173
- isEqual( otherRange ) {
174
- return this == otherRange || ( this.start.isEqual( otherRange.start ) && this.end.isEqual( otherRange.end ) );
175
- }
176
-
177
- /**
178
- * Checks whether this range contains given {@link module:engine/view/position~Position position}.
179
- *
180
- * @param {module:engine/view/position~Position} position Position to check.
181
- * @returns {Boolean} `true` if given {@link module:engine/view/position~Position position} is contained in this range,
182
- * `false` otherwise.
183
- */
184
- containsPosition( position ) {
185
- return position.isAfter( this.start ) && position.isBefore( this.end );
186
- }
187
-
188
- /**
189
- * Checks whether this range contains given {@link module:engine/view/range~Range range}.
190
- *
191
- * @param {module:engine/view/range~Range} otherRange Range to check.
192
- * @param {Boolean} [loose=false] Whether the check is loose or strict. If the check is strict (`false`), compared range cannot
193
- * start or end at the same position as this range boundaries. If the check is loose (`true`), compared range can start, end or
194
- * even be equal to this range. Note that collapsed ranges are always compared in strict mode.
195
- * @returns {Boolean} `true` if given {@link module:engine/view/range~Range range} boundaries are contained by this range, `false`
196
- * otherwise.
197
- */
198
- containsRange( otherRange, loose = false ) {
199
- if ( otherRange.isCollapsed ) {
200
- loose = false;
201
- }
202
-
203
- const containsStart = this.containsPosition( otherRange.start ) || ( loose && this.start.isEqual( otherRange.start ) );
204
- const containsEnd = this.containsPosition( otherRange.end ) || ( loose && this.end.isEqual( otherRange.end ) );
205
-
206
- return containsStart && containsEnd;
207
- }
208
-
209
- /**
210
- * Computes which part(s) of this {@link module:engine/view/range~Range range} is not a part of given
211
- * {@link module:engine/view/range~Range range}.
212
- * Returned array contains zero, one or two {@link module:engine/view/range~Range ranges}.
213
- *
214
- * Examples:
215
- *
216
- * let foo = downcastWriter.createText( 'foo' );
217
- * let img = downcastWriter.createContainerElement( 'img' );
218
- * let bar = downcastWriter.createText( 'bar' );
219
- * let p = downcastWriter.createContainerElement( 'p', null, [ foo, img, bar ] );
220
- *
221
- * let range = view.createRange( view.createPositionAt( foo, 2 ), view.createPositionAt( bar, 1 ); // "o", img, "b" are in range.
222
- * let otherRange = view.createRange( // "oo", img, "ba" are in range.
223
- * view.createPositionAt( foo, 1 ),
224
- * view.createPositionAt( bar, 2 )
225
- * );
226
- * let transformed = range.getDifference( otherRange );
227
- * // transformed array has no ranges because `otherRange` contains `range`
228
- *
229
- * otherRange = view.createRange( view.createPositionAt( foo, 1 ), view.createPositionAt( p, 2 ); // "oo", img are in range.
230
- * transformed = range.getDifference( otherRange );
231
- * // transformed array has one range: from ( p, 2 ) to ( bar, 1 )
232
- *
233
- * otherRange = view.createRange( view.createPositionAt( p, 1 ), view.createPositionAt( p, 2 ) ); // img is in range.
234
- * transformed = range.getDifference( otherRange );
235
- * // transformed array has two ranges: from ( foo, 1 ) to ( p, 1 ) and from ( p, 2 ) to ( bar, 1 )
236
- *
237
- * @param {module:engine/view/range~Range} otherRange Range to differentiate against.
238
- * @returns {Array.<module:engine/view/range~Range>} The difference between ranges.
239
- */
240
- getDifference( otherRange ) {
241
- const ranges = [];
242
-
243
- if ( this.isIntersecting( otherRange ) ) {
244
- // Ranges intersect.
245
-
246
- if ( this.containsPosition( otherRange.start ) ) {
247
- // Given range start is inside this range. This means that we have to
248
- // add shrunken range - from the start to the middle of this range.
249
- ranges.push( new Range( this.start, otherRange.start ) );
250
- }
251
-
252
- if ( this.containsPosition( otherRange.end ) ) {
253
- // Given range end is inside this range. This means that we have to
254
- // add shrunken range - from the middle of this range to the end.
255
- ranges.push( new Range( otherRange.end, this.end ) );
256
- }
257
- } else {
258
- // Ranges do not intersect, return the original range.
259
- ranges.push( this.clone() );
260
- }
261
-
262
- return ranges;
263
- }
264
-
265
- /**
266
- * Returns an intersection of this {@link module:engine/view/range~Range range} and given {@link module:engine/view/range~Range range}.
267
- * Intersection is a common part of both of those ranges. If ranges has no common part, returns `null`.
268
- *
269
- * Examples:
270
- *
271
- * let foo = downcastWriter.createText( 'foo' );
272
- * let img = downcastWriter.createContainerElement( 'img' );
273
- * let bar = downcastWriter.createText( 'bar' );
274
- * let p = downcastWriter.createContainerElement( 'p', null, [ foo, img, bar ] );
275
- *
276
- * let range = view.createRange( view.createPositionAt( foo, 2 ), view.createPositionAt( bar, 1 ); // "o", img, "b" are in range.
277
- * let otherRange = view.createRange( view.createPositionAt( foo, 1 ), view.createPositionAt( p, 2 ); // "oo", img are in range.
278
- * let transformed = range.getIntersection( otherRange ); // range from ( foo, 1 ) to ( p, 2 ).
279
- *
280
- * otherRange = view.createRange( view.createPositionAt( bar, 1 ), view.createPositionAt( bar, 3 ); "ar" is in range.
281
- * transformed = range.getIntersection( otherRange ); // null - no common part.
282
- *
283
- * @param {module:engine/view/range~Range} otherRange Range to check for intersection.
284
- * @returns {module:engine/view/range~Range|null} A common part of given ranges or `null` if ranges have no common part.
285
- */
286
- getIntersection( otherRange ) {
287
- if ( this.isIntersecting( otherRange ) ) {
288
- // Ranges intersect, so a common range will be returned.
289
- // At most, it will be same as this range.
290
- let commonRangeStart = this.start;
291
- let commonRangeEnd = this.end;
292
-
293
- if ( this.containsPosition( otherRange.start ) ) {
294
- // Given range start is inside this range. This means thaNt we have to
295
- // shrink common range to the given range start.
296
- commonRangeStart = otherRange.start;
297
- }
298
-
299
- if ( this.containsPosition( otherRange.end ) ) {
300
- // Given range end is inside this range. This means that we have to
301
- // shrink common range to the given range end.
302
- commonRangeEnd = otherRange.end;
303
- }
304
-
305
- return new Range( commonRangeStart, commonRangeEnd );
306
- }
307
-
308
- // Ranges do not intersect, so they do not have common part.
309
- return null;
310
- }
311
-
312
- /**
313
- * Creates a {@link module:engine/view/treewalker~TreeWalker TreeWalker} instance with this range as a boundary.
314
- *
315
- * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
316
- * @param {module:engine/view/position~Position} [options.startPosition]
317
- * @param {Boolean} [options.singleCharacters=false]
318
- * @param {Boolean} [options.shallow=false]
319
- * @param {Boolean} [options.ignoreElementEnd=false]
320
- * @returns {module:engine/view/treewalker~TreeWalker}
321
- */
322
- getWalker( options = {} ) {
323
- options.boundaries = this;
324
-
325
- return new TreeWalker( options );
326
- }
327
-
328
- /**
329
- * Returns a {@link module:engine/view/node~Node} or {@link module:engine/view/documentfragment~DocumentFragment}
330
- * which is a common ancestor of range's both ends (in which the entire range is contained).
331
- *
332
- * @returns {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment|null}
333
- */
334
- getCommonAncestor() {
335
- return this.start.getCommonAncestor( this.end );
336
- }
337
-
338
- /**
339
- * Returns an {@link module:engine/view/element~Element Element} contained by the range.
340
- * The element will be returned when it is the **only** node within the range and **fully–contained**
341
- * at the same time.
342
- *
343
- * @returns {module:engine/view/element~Element|null}
344
- */
345
- getContainedElement() {
346
- if ( this.isCollapsed ) {
347
- return null;
348
- }
349
-
350
- let nodeAfterStart = this.start.nodeAfter;
351
- let nodeBeforeEnd = this.end.nodeBefore;
352
-
353
- // Handle the situation when the range position is at the beginning / at the end of a text node.
354
- // In such situation `.nodeAfter` and `.nodeBefore` are `null` but the range still might be spanning
355
- // over one element.
356
- //
357
- // <p>Foo{<span class="widget"></span>}bar</p> vs <p>Foo[<span class="widget"></span>]bar</p>
358
- //
359
- // These are basically the same range, only the difference is if the range position is at
360
- // at the end/at the beginning of a text node or just before/just after the text node.
361
- //
362
- if ( this.start.parent.is( '$text' ) && this.start.isAtEnd && this.start.parent.nextSibling ) {
363
- nodeAfterStart = this.start.parent.nextSibling;
364
- }
365
-
366
- if ( this.end.parent.is( '$text' ) && this.end.isAtStart && this.end.parent.previousSibling ) {
367
- nodeBeforeEnd = this.end.parent.previousSibling;
368
- }
369
-
370
- if ( nodeAfterStart && nodeAfterStart.is( 'element' ) && nodeAfterStart === nodeBeforeEnd ) {
371
- return nodeAfterStart;
372
- }
373
-
374
- return null;
375
- }
376
-
377
- /**
378
- * Clones this range.
379
- *
380
- * @returns {module:engine/view/range~Range}
381
- */
382
- clone() {
383
- return new Range( this.start, this.end );
384
- }
385
-
386
- /**
387
- * Returns an iterator that iterates over all {@link module:engine/view/item~Item view items} that are in this range and returns
388
- * them.
389
- *
390
- * This method uses {@link module:engine/view/treewalker~TreeWalker} with `boundaries` set to this range and `ignoreElementEnd` option
391
- * set to `true`. However it returns only {@link module:engine/view/item~Item items},
392
- * not {@link module:engine/view/treewalker~TreeWalkerValue}.
393
- *
394
- * You may specify additional options for the tree walker. See {@link module:engine/view/treewalker~TreeWalker} for
395
- * a full list of available options.
396
- *
397
- * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
398
- * @returns {Iterable.<module:engine/view/item~Item>}
399
- */
400
- * getItems( options = {} ) {
401
- options.boundaries = this;
402
- options.ignoreElementEnd = true;
403
-
404
- const treeWalker = new TreeWalker( options );
405
-
406
- for ( const value of treeWalker ) {
407
- yield value.item;
408
- }
409
- }
410
-
411
- /**
412
- * Returns an iterator that iterates over all {@link module:engine/view/position~Position positions} that are boundaries or
413
- * contained in this range.
414
- *
415
- * This method uses {@link module:engine/view/treewalker~TreeWalker} with `boundaries` set to this range. However it returns only
416
- * {@link module:engine/view/position~Position positions}, not {@link module:engine/view/treewalker~TreeWalkerValue}.
417
- *
418
- * You may specify additional options for the tree walker. See {@link module:engine/view/treewalker~TreeWalker} for
419
- * a full list of available options.
420
- *
421
- * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
422
- * @returns {Iterable.<module:engine/view/position~Position>}
423
- */
424
- * getPositions( options = {} ) {
425
- options.boundaries = this;
426
-
427
- const treeWalker = new TreeWalker( options );
428
-
429
- yield treeWalker.position;
430
-
431
- for ( const value of treeWalker ) {
432
- yield value.nextPosition;
433
- }
434
- }
435
-
436
- /**
437
- * Checks whether this object is of the given type.
438
- *
439
- * range.is( 'range' ); // -> true
440
- * range.is( 'view:range' ); // -> true
441
- *
442
- * range.is( 'model:range' ); // -> false
443
- * range.is( 'element' ); // -> false
444
- * range.is( 'selection' ); // -> false
445
- *
446
- * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
447
- *
448
- * @param {String} type
449
- * @returns {Boolean}
450
- */
451
- is( type ) {
452
- return type === 'range' || type === 'view:range';
453
- }
454
-
455
- /**
456
- * Checks and returns whether this range intersects with the given range.
457
- *
458
- * @param {module:engine/view/range~Range} otherRange Range to compare with.
459
- * @returns {Boolean} True if ranges intersect.
460
- */
461
- isIntersecting( otherRange ) {
462
- return this.start.isBefore( otherRange.end ) && this.end.isAfter( otherRange.start );
463
- }
464
-
465
- /**
466
- * Creates a range from the given parents and offsets.
467
- *
468
- * @protected
469
- * @param {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment} startElement Start position
470
- * parent element.
471
- * @param {Number} startOffset Start position offset.
472
- * @param {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment} endElement End position
473
- * parent element.
474
- * @param {Number} endOffset End position offset.
475
- * @returns {module:engine/view/range~Range} Created range.
476
- */
477
- static _createFromParentsAndOffsets( startElement, startOffset, endElement, endOffset ) {
478
- return new this(
479
- new Position( startElement, startOffset ),
480
- new Position( endElement, endOffset )
481
- );
482
- }
483
-
484
- /**
485
- * Creates a new range, spreading from specified {@link module:engine/view/position~Position position} to a position moved by
486
- * given `shift`. If `shift` is a negative value, shifted position is treated as the beginning of the range.
487
- *
488
- * @protected
489
- * @param {module:engine/view/position~Position} position Beginning of the range.
490
- * @param {Number} shift How long the range should be.
491
- * @returns {module:engine/view/range~Range}
492
- */
493
- static _createFromPositionAndShift( position, shift ) {
494
- const start = position;
495
- const end = position.getShiftedBy( shift );
496
-
497
- return shift > 0 ? new this( start, end ) : new this( end, start );
498
- }
499
-
500
- /**
501
- * Creates a range inside an {@link module:engine/view/element~Element element} which starts before the first child of
502
- * that element and ends after the last child of that element.
503
- *
504
- * @protected
505
- * @param {module:engine/view/element~Element} element Element which is a parent for the range.
506
- * @returns {module:engine/view/range~Range}
507
- */
508
- static _createIn( element ) {
509
- return this._createFromParentsAndOffsets( element, 0, element, element.childCount );
510
- }
511
-
512
- /**
513
- * Creates a range that starts before given {@link module:engine/view/item~Item view item} and ends after it.
514
- *
515
- * @protected
516
- * @param {module:engine/view/item~Item} item
517
- * @returns {module:engine/view/range~Range}
518
- */
519
- static _createOn( item ) {
520
- const size = item.is( '$textProxy' ) ? item.offsetSize : 1;
521
-
522
- return this._createFromPositionAndShift( Position._createBefore( item ), size );
523
- }
20
+ export default class Range extends TypeCheckable {
21
+ /**
22
+ * Creates a range spanning from `start` position to `end` position.
23
+ *
24
+ * **Note:** Constructor creates it's own {@link module:engine/view/position~Position} instances basing on passed values.
25
+ *
26
+ * @param {module:engine/view/position~Position} start Start position.
27
+ * @param {module:engine/view/position~Position} [end] End position. If not set, range will be collapsed at the `start` position.
28
+ */
29
+ constructor(start, end = null) {
30
+ super();
31
+ /**
32
+ * Start position.
33
+ *
34
+ * @readonly
35
+ * @member {module:engine/view/position~Position}
36
+ */
37
+ this.start = start.clone();
38
+ /**
39
+ * End position.
40
+ *
41
+ * @readonly
42
+ * @member {module:engine/view/position~Position}
43
+ */
44
+ this.end = end ? end.clone() : start.clone();
45
+ }
46
+ /**
47
+ * Iterable interface.
48
+ *
49
+ * Iterates over all {@link module:engine/view/item~Item view items} that are in this range and returns
50
+ * them together with additional information like length or {@link module:engine/view/position~Position positions},
51
+ * grouped as {@link module:engine/view/treewalker~TreeWalkerValue}.
52
+ *
53
+ * This iterator uses {@link module:engine/view/treewalker~TreeWalker TreeWalker} with `boundaries` set to this range and
54
+ * `ignoreElementEnd` option
55
+ * set to `true`.
56
+ *
57
+ * @returns {Iterable.<module:engine/view/treewalker~TreeWalkerValue>}
58
+ */
59
+ *[Symbol.iterator]() {
60
+ yield* new TreeWalker({ boundaries: this, ignoreElementEnd: true });
61
+ }
62
+ /**
63
+ * Returns whether the range is collapsed, that is it start and end positions are equal.
64
+ *
65
+ * @type {Boolean}
66
+ */
67
+ get isCollapsed() {
68
+ return this.start.isEqual(this.end);
69
+ }
70
+ /**
71
+ * Returns whether this range is flat, that is if {@link module:engine/view/range~Range#start start} position and
72
+ * {@link module:engine/view/range~Range#end end} position are in the same {@link module:engine/view/position~Position#parent parent}.
73
+ *
74
+ * @type {Boolean}
75
+ */
76
+ get isFlat() {
77
+ return this.start.parent === this.end.parent;
78
+ }
79
+ /**
80
+ * Range root element.
81
+ *
82
+ * @type {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment}
83
+ */
84
+ get root() {
85
+ return this.start.root;
86
+ }
87
+ /**
88
+ * Creates a maximal range that has the same content as this range but is expanded in both ways (at the beginning
89
+ * and at the end).
90
+ *
91
+ * For example:
92
+ *
93
+ * <p>Foo</p><p><b>{Bar}</b></p> -> <p>Foo</p>[<p><b>Bar</b>]</p>
94
+ * <p><b>foo</b>{bar}<span></span></p> -> <p><b>foo[</b>bar<span></span>]</p>
95
+ *
96
+ * Note that in the sample above:
97
+ *
98
+ * - `<p>` have type of {@link module:engine/view/containerelement~ContainerElement},
99
+ * - `<b>` have type of {@link module:engine/view/attributeelement~AttributeElement},
100
+ * - `<span>` have type of {@link module:engine/view/uielement~UIElement}.
101
+ *
102
+ * @returns {module:engine/view/range~Range} Enlarged range.
103
+ */
104
+ getEnlarged() {
105
+ let start = this.start.getLastMatchingPosition(enlargeTrimSkip, { direction: 'backward' });
106
+ let end = this.end.getLastMatchingPosition(enlargeTrimSkip);
107
+ // Fix positions, in case if they are in Text node.
108
+ if (start.parent.is('$text') && start.isAtStart) {
109
+ start = Position._createBefore(start.parent);
110
+ }
111
+ if (end.parent.is('$text') && end.isAtEnd) {
112
+ end = Position._createAfter(end.parent);
113
+ }
114
+ return new Range(start, end);
115
+ }
116
+ /**
117
+ * Creates a minimum range that has the same content as this range but is trimmed in both ways (at the beginning
118
+ * and at the end).
119
+ *
120
+ * For example:
121
+ *
122
+ * <p>Foo</p>[<p><b>Bar</b>]</p> -> <p>Foo</p><p><b>{Bar}</b></p>
123
+ * <p><b>foo[</b>bar<span></span>]</p> -> <p><b>foo</b>{bar}<span></span></p>
124
+ *
125
+ * Note that in the sample above:
126
+ *
127
+ * - `<p>` have type of {@link module:engine/view/containerelement~ContainerElement},
128
+ * - `<b>` have type of {@link module:engine/view/attributeelement~AttributeElement},
129
+ * - `<span>` have type of {@link module:engine/view/uielement~UIElement}.
130
+ *
131
+ * @returns {module:engine/view/range~Range} Shrink range.
132
+ */
133
+ getTrimmed() {
134
+ let start = this.start.getLastMatchingPosition(enlargeTrimSkip);
135
+ if (start.isAfter(this.end) || start.isEqual(this.end)) {
136
+ return new Range(start, start);
137
+ }
138
+ let end = this.end.getLastMatchingPosition(enlargeTrimSkip, { direction: 'backward' });
139
+ const nodeAfterStart = start.nodeAfter;
140
+ const nodeBeforeEnd = end.nodeBefore;
141
+ // Because TreeWalker prefers positions next to text node, we need to move them manually into these text nodes.
142
+ if (nodeAfterStart && nodeAfterStart.is('$text')) {
143
+ start = new Position(nodeAfterStart, 0);
144
+ }
145
+ if (nodeBeforeEnd && nodeBeforeEnd.is('$text')) {
146
+ end = new Position(nodeBeforeEnd, nodeBeforeEnd.data.length);
147
+ }
148
+ return new Range(start, end);
149
+ }
150
+ /**
151
+ * Two ranges are equal if their start and end positions are equal.
152
+ *
153
+ * @param {module:engine/view/range~Range} otherRange Range to compare with.
154
+ * @returns {Boolean} `true` if ranges are equal, `false` otherwise
155
+ */
156
+ isEqual(otherRange) {
157
+ return this == otherRange || (this.start.isEqual(otherRange.start) && this.end.isEqual(otherRange.end));
158
+ }
159
+ /**
160
+ * Checks whether this range contains given {@link module:engine/view/position~Position position}.
161
+ *
162
+ * @param {module:engine/view/position~Position} position Position to check.
163
+ * @returns {Boolean} `true` if given {@link module:engine/view/position~Position position} is contained in this range,
164
+ * `false` otherwise.
165
+ */
166
+ containsPosition(position) {
167
+ return position.isAfter(this.start) && position.isBefore(this.end);
168
+ }
169
+ /**
170
+ * Checks whether this range contains given {@link module:engine/view/range~Range range}.
171
+ *
172
+ * @param {module:engine/view/range~Range} otherRange Range to check.
173
+ * @param {Boolean} [loose=false] Whether the check is loose or strict. If the check is strict (`false`), compared range cannot
174
+ * start or end at the same position as this range boundaries. If the check is loose (`true`), compared range can start, end or
175
+ * even be equal to this range. Note that collapsed ranges are always compared in strict mode.
176
+ * @returns {Boolean} `true` if given {@link module:engine/view/range~Range range} boundaries are contained by this range, `false`
177
+ * otherwise.
178
+ */
179
+ containsRange(otherRange, loose = false) {
180
+ if (otherRange.isCollapsed) {
181
+ loose = false;
182
+ }
183
+ const containsStart = this.containsPosition(otherRange.start) || (loose && this.start.isEqual(otherRange.start));
184
+ const containsEnd = this.containsPosition(otherRange.end) || (loose && this.end.isEqual(otherRange.end));
185
+ return containsStart && containsEnd;
186
+ }
187
+ /**
188
+ * Computes which part(s) of this {@link module:engine/view/range~Range range} is not a part of given
189
+ * {@link module:engine/view/range~Range range}.
190
+ * Returned array contains zero, one or two {@link module:engine/view/range~Range ranges}.
191
+ *
192
+ * Examples:
193
+ *
194
+ * let foo = downcastWriter.createText( 'foo' );
195
+ * let img = downcastWriter.createContainerElement( 'img' );
196
+ * let bar = downcastWriter.createText( 'bar' );
197
+ * let p = downcastWriter.createContainerElement( 'p', null, [ foo, img, bar ] );
198
+ *
199
+ * let range = view.createRange( view.createPositionAt( foo, 2 ), view.createPositionAt( bar, 1 ); // "o", img, "b" are in range.
200
+ * let otherRange = view.createRange( // "oo", img, "ba" are in range.
201
+ * view.createPositionAt( foo, 1 ),
202
+ * view.createPositionAt( bar, 2 )
203
+ * );
204
+ * let transformed = range.getDifference( otherRange );
205
+ * // transformed array has no ranges because `otherRange` contains `range`
206
+ *
207
+ * otherRange = view.createRange( view.createPositionAt( foo, 1 ), view.createPositionAt( p, 2 ); // "oo", img are in range.
208
+ * transformed = range.getDifference( otherRange );
209
+ * // transformed array has one range: from ( p, 2 ) to ( bar, 1 )
210
+ *
211
+ * otherRange = view.createRange( view.createPositionAt( p, 1 ), view.createPositionAt( p, 2 ) ); // img is in range.
212
+ * transformed = range.getDifference( otherRange );
213
+ * // transformed array has two ranges: from ( foo, 1 ) to ( p, 1 ) and from ( p, 2 ) to ( bar, 1 )
214
+ *
215
+ * @param {module:engine/view/range~Range} otherRange Range to differentiate against.
216
+ * @returns {Array.<module:engine/view/range~Range>} The difference between ranges.
217
+ */
218
+ getDifference(otherRange) {
219
+ const ranges = [];
220
+ if (this.isIntersecting(otherRange)) {
221
+ // Ranges intersect.
222
+ if (this.containsPosition(otherRange.start)) {
223
+ // Given range start is inside this range. This means that we have to
224
+ // add shrunken range - from the start to the middle of this range.
225
+ ranges.push(new Range(this.start, otherRange.start));
226
+ }
227
+ if (this.containsPosition(otherRange.end)) {
228
+ // Given range end is inside this range. This means that we have to
229
+ // add shrunken range - from the middle of this range to the end.
230
+ ranges.push(new Range(otherRange.end, this.end));
231
+ }
232
+ }
233
+ else {
234
+ // Ranges do not intersect, return the original range.
235
+ ranges.push(this.clone());
236
+ }
237
+ return ranges;
238
+ }
239
+ /**
240
+ * Returns an intersection of this {@link module:engine/view/range~Range range} and given {@link module:engine/view/range~Range range}.
241
+ * Intersection is a common part of both of those ranges. If ranges has no common part, returns `null`.
242
+ *
243
+ * Examples:
244
+ *
245
+ * let foo = downcastWriter.createText( 'foo' );
246
+ * let img = downcastWriter.createContainerElement( 'img' );
247
+ * let bar = downcastWriter.createText( 'bar' );
248
+ * let p = downcastWriter.createContainerElement( 'p', null, [ foo, img, bar ] );
249
+ *
250
+ * let range = view.createRange( view.createPositionAt( foo, 2 ), view.createPositionAt( bar, 1 ); // "o", img, "b" are in range.
251
+ * let otherRange = view.createRange( view.createPositionAt( foo, 1 ), view.createPositionAt( p, 2 ); // "oo", img are in range.
252
+ * let transformed = range.getIntersection( otherRange ); // range from ( foo, 1 ) to ( p, 2 ).
253
+ *
254
+ * otherRange = view.createRange( view.createPositionAt( bar, 1 ), view.createPositionAt( bar, 3 ); "ar" is in range.
255
+ * transformed = range.getIntersection( otherRange ); // null - no common part.
256
+ *
257
+ * @param {module:engine/view/range~Range} otherRange Range to check for intersection.
258
+ * @returns {module:engine/view/range~Range|null} A common part of given ranges or `null` if ranges have no common part.
259
+ */
260
+ getIntersection(otherRange) {
261
+ if (this.isIntersecting(otherRange)) {
262
+ // Ranges intersect, so a common range will be returned.
263
+ // At most, it will be same as this range.
264
+ let commonRangeStart = this.start;
265
+ let commonRangeEnd = this.end;
266
+ if (this.containsPosition(otherRange.start)) {
267
+ // Given range start is inside this range. This means thaNt we have to
268
+ // shrink common range to the given range start.
269
+ commonRangeStart = otherRange.start;
270
+ }
271
+ if (this.containsPosition(otherRange.end)) {
272
+ // Given range end is inside this range. This means that we have to
273
+ // shrink common range to the given range end.
274
+ commonRangeEnd = otherRange.end;
275
+ }
276
+ return new Range(commonRangeStart, commonRangeEnd);
277
+ }
278
+ // Ranges do not intersect, so they do not have common part.
279
+ return null;
280
+ }
281
+ /**
282
+ * Creates a {@link module:engine/view/treewalker~TreeWalker TreeWalker} instance with this range as a boundary.
283
+ *
284
+ * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
285
+ * @param {module:engine/view/position~Position} [options.startPosition]
286
+ * @param {Boolean} [options.singleCharacters=false]
287
+ * @param {Boolean} [options.shallow=false]
288
+ * @param {Boolean} [options.ignoreElementEnd=false]
289
+ * @returns {module:engine/view/treewalker~TreeWalker}
290
+ */
291
+ getWalker(options = {}) {
292
+ options.boundaries = this;
293
+ return new TreeWalker(options);
294
+ }
295
+ /**
296
+ * Returns a {@link module:engine/view/node~Node} or {@link module:engine/view/documentfragment~DocumentFragment}
297
+ * which is a common ancestor of range's both ends (in which the entire range is contained).
298
+ *
299
+ * @returns {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment|null}
300
+ */
301
+ getCommonAncestor() {
302
+ return this.start.getCommonAncestor(this.end);
303
+ }
304
+ /**
305
+ * Returns an {@link module:engine/view/element~Element Element} contained by the range.
306
+ * The element will be returned when it is the **only** node within the range and **fully–contained**
307
+ * at the same time.
308
+ *
309
+ * @returns {module:engine/view/element~Element|null}
310
+ */
311
+ getContainedElement() {
312
+ if (this.isCollapsed) {
313
+ return null;
314
+ }
315
+ let nodeAfterStart = this.start.nodeAfter;
316
+ let nodeBeforeEnd = this.end.nodeBefore;
317
+ // Handle the situation when the range position is at the beginning / at the end of a text node.
318
+ // In such situation `.nodeAfter` and `.nodeBefore` are `null` but the range still might be spanning
319
+ // over one element.
320
+ //
321
+ // <p>Foo{<span class="widget"></span>}bar</p> vs <p>Foo[<span class="widget"></span>]bar</p>
322
+ //
323
+ // These are basically the same range, only the difference is if the range position is at
324
+ // at the end/at the beginning of a text node or just before/just after the text node.
325
+ //
326
+ if (this.start.parent.is('$text') && this.start.isAtEnd && this.start.parent.nextSibling) {
327
+ nodeAfterStart = this.start.parent.nextSibling;
328
+ }
329
+ if (this.end.parent.is('$text') && this.end.isAtStart && this.end.parent.previousSibling) {
330
+ nodeBeforeEnd = this.end.parent.previousSibling;
331
+ }
332
+ if (nodeAfterStart && nodeAfterStart.is('element') && nodeAfterStart === nodeBeforeEnd) {
333
+ return nodeAfterStart;
334
+ }
335
+ return null;
336
+ }
337
+ /**
338
+ * Clones this range.
339
+ *
340
+ * @returns {module:engine/view/range~Range}
341
+ */
342
+ clone() {
343
+ return new Range(this.start, this.end);
344
+ }
345
+ /**
346
+ * Returns an iterator that iterates over all {@link module:engine/view/item~Item view items} that are in this range and returns
347
+ * them.
348
+ *
349
+ * This method uses {@link module:engine/view/treewalker~TreeWalker} with `boundaries` set to this range and `ignoreElementEnd` option
350
+ * set to `true`. However it returns only {@link module:engine/view/item~Item items},
351
+ * not {@link module:engine/view/treewalker~TreeWalkerValue}.
352
+ *
353
+ * You may specify additional options for the tree walker. See {@link module:engine/view/treewalker~TreeWalker} for
354
+ * a full list of available options.
355
+ *
356
+ * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
357
+ * @returns {Iterable.<module:engine/view/item~Item>}
358
+ */
359
+ *getItems(options = {}) {
360
+ options.boundaries = this;
361
+ options.ignoreElementEnd = true;
362
+ const treeWalker = new TreeWalker(options);
363
+ for (const value of treeWalker) {
364
+ yield value.item;
365
+ }
366
+ }
367
+ /**
368
+ * Returns an iterator that iterates over all {@link module:engine/view/position~Position positions} that are boundaries or
369
+ * contained in this range.
370
+ *
371
+ * This method uses {@link module:engine/view/treewalker~TreeWalker} with `boundaries` set to this range. However it returns only
372
+ * {@link module:engine/view/position~Position positions}, not {@link module:engine/view/treewalker~TreeWalkerValue}.
373
+ *
374
+ * You may specify additional options for the tree walker. See {@link module:engine/view/treewalker~TreeWalker} for
375
+ * a full list of available options.
376
+ *
377
+ * @param {Object} options Object with configuration options. See {@link module:engine/view/treewalker~TreeWalker}.
378
+ * @returns {Iterable.<module:engine/view/position~Position>}
379
+ */
380
+ *getPositions(options = {}) {
381
+ options.boundaries = this;
382
+ const treeWalker = new TreeWalker(options);
383
+ yield treeWalker.position;
384
+ for (const value of treeWalker) {
385
+ yield value.nextPosition;
386
+ }
387
+ }
388
+ /**
389
+ * Checks and returns whether this range intersects with the given range.
390
+ *
391
+ * @param {module:engine/view/range~Range} otherRange Range to compare with.
392
+ * @returns {Boolean} True if ranges intersect.
393
+ */
394
+ isIntersecting(otherRange) {
395
+ return this.start.isBefore(otherRange.end) && this.end.isAfter(otherRange.start);
396
+ }
397
+ /**
398
+ * Creates a range from the given parents and offsets.
399
+ *
400
+ * @protected
401
+ * @param {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment} startElement Start position
402
+ * parent element.
403
+ * @param {Number} startOffset Start position offset.
404
+ * @param {module:engine/view/node~Node|module:engine/view/documentfragment~DocumentFragment} endElement End position
405
+ * parent element.
406
+ * @param {Number} endOffset End position offset.
407
+ * @returns {module:engine/view/range~Range} Created range.
408
+ */
409
+ static _createFromParentsAndOffsets(startElement, startOffset, endElement, endOffset) {
410
+ return new this(new Position(startElement, startOffset), new Position(endElement, endOffset));
411
+ }
412
+ /**
413
+ * Creates a new range, spreading from specified {@link module:engine/view/position~Position position} to a position moved by
414
+ * given `shift`. If `shift` is a negative value, shifted position is treated as the beginning of the range.
415
+ *
416
+ * @protected
417
+ * @param {module:engine/view/position~Position} position Beginning of the range.
418
+ * @param {Number} shift How long the range should be.
419
+ * @returns {module:engine/view/range~Range}
420
+ */
421
+ static _createFromPositionAndShift(position, shift) {
422
+ const start = position;
423
+ const end = position.getShiftedBy(shift);
424
+ return shift > 0 ? new this(start, end) : new this(end, start);
425
+ }
426
+ /**
427
+ * Creates a range inside an {@link module:engine/view/element~Element element} which starts before the first child of
428
+ * that element and ends after the last child of that element.
429
+ *
430
+ * @protected
431
+ * @param {module:engine/view/element~Element} element Element which is a parent for the range.
432
+ * @returns {module:engine/view/range~Range}
433
+ */
434
+ static _createIn(element) {
435
+ return this._createFromParentsAndOffsets(element, 0, element, element.childCount);
436
+ }
437
+ /**
438
+ * Creates a range that starts before given {@link module:engine/view/item~Item view item} and ends after it.
439
+ *
440
+ * @protected
441
+ * @param {module:engine/view/item~Item} item
442
+ * @returns {module:engine/view/range~Range}
443
+ */
444
+ static _createOn(item) {
445
+ const size = item.is('$textProxy') ? item.offsetSize : 1;
446
+ return this._createFromPositionAndShift(Position._createBefore(item), size);
447
+ }
524
448
  }
525
-
449
+ /**
450
+ * Checks whether this object is of the given type.
451
+ *
452
+ * range.is( 'range' ); // -> true
453
+ * range.is( 'view:range' ); // -> true
454
+ *
455
+ * range.is( 'model:range' ); // -> false
456
+ * range.is( 'element' ); // -> false
457
+ * range.is( 'selection' ); // -> false
458
+ *
459
+ * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
460
+ *
461
+ * @param {String} type
462
+ * @returns {Boolean}
463
+ */
464
+ Range.prototype.is = function (type) {
465
+ return type === 'range' || type === 'view:range';
466
+ };
526
467
  // Function used by getEnlarged and getTrimmed methods.
527
- function enlargeTrimSkip( value ) {
528
- if ( value.item.is( 'attributeElement' ) || value.item.is( 'uiElement' ) ) {
529
- return true;
530
- }
531
-
532
- return false;
468
+ function enlargeTrimSkip(value) {
469
+ if (value.item.is('attributeElement') || value.item.is('uiElement')) {
470
+ return true;
471
+ }
472
+ return false;
533
473
  }