@ckeditor/ckeditor5-engine 30.0.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 (117) hide show
  1. package/LICENSE.md +17 -0
  2. package/README.md +30 -0
  3. package/package.json +70 -0
  4. package/src/controller/datacontroller.js +563 -0
  5. package/src/controller/editingcontroller.js +149 -0
  6. package/src/conversion/conversion.js +644 -0
  7. package/src/conversion/conversionhelpers.js +40 -0
  8. package/src/conversion/downcastdispatcher.js +914 -0
  9. package/src/conversion/downcasthelpers.js +1706 -0
  10. package/src/conversion/mapper.js +696 -0
  11. package/src/conversion/modelconsumable.js +329 -0
  12. package/src/conversion/upcastdispatcher.js +807 -0
  13. package/src/conversion/upcasthelpers.js +997 -0
  14. package/src/conversion/viewconsumable.js +623 -0
  15. package/src/dataprocessor/basichtmlwriter.js +32 -0
  16. package/src/dataprocessor/dataprocessor.jsdoc +64 -0
  17. package/src/dataprocessor/htmldataprocessor.js +159 -0
  18. package/src/dataprocessor/htmlwriter.js +22 -0
  19. package/src/dataprocessor/xmldataprocessor.js +161 -0
  20. package/src/dev-utils/model.js +482 -0
  21. package/src/dev-utils/operationreplayer.js +140 -0
  22. package/src/dev-utils/utils.js +103 -0
  23. package/src/dev-utils/view.js +1091 -0
  24. package/src/index.js +52 -0
  25. package/src/model/batch.js +82 -0
  26. package/src/model/differ.js +1282 -0
  27. package/src/model/document.js +483 -0
  28. package/src/model/documentfragment.js +390 -0
  29. package/src/model/documentselection.js +1261 -0
  30. package/src/model/element.js +438 -0
  31. package/src/model/history.js +138 -0
  32. package/src/model/item.jsdoc +14 -0
  33. package/src/model/liveposition.js +182 -0
  34. package/src/model/liverange.js +221 -0
  35. package/src/model/markercollection.js +553 -0
  36. package/src/model/model.js +934 -0
  37. package/src/model/node.js +507 -0
  38. package/src/model/nodelist.js +217 -0
  39. package/src/model/operation/attributeoperation.js +202 -0
  40. package/src/model/operation/detachoperation.js +103 -0
  41. package/src/model/operation/insertoperation.js +188 -0
  42. package/src/model/operation/markeroperation.js +154 -0
  43. package/src/model/operation/mergeoperation.js +216 -0
  44. package/src/model/operation/moveoperation.js +209 -0
  45. package/src/model/operation/nooperation.js +58 -0
  46. package/src/model/operation/operation.js +139 -0
  47. package/src/model/operation/operationfactory.js +49 -0
  48. package/src/model/operation/renameoperation.js +155 -0
  49. package/src/model/operation/rootattributeoperation.js +211 -0
  50. package/src/model/operation/splitoperation.js +254 -0
  51. package/src/model/operation/transform.js +2389 -0
  52. package/src/model/operation/utils.js +292 -0
  53. package/src/model/position.js +1164 -0
  54. package/src/model/range.js +1049 -0
  55. package/src/model/rootelement.js +111 -0
  56. package/src/model/schema.js +1851 -0
  57. package/src/model/selection.js +902 -0
  58. package/src/model/text.js +138 -0
  59. package/src/model/textproxy.js +279 -0
  60. package/src/model/treewalker.js +414 -0
  61. package/src/model/utils/autoparagraphing.js +77 -0
  62. package/src/model/utils/deletecontent.js +528 -0
  63. package/src/model/utils/getselectedcontent.js +150 -0
  64. package/src/model/utils/insertcontent.js +824 -0
  65. package/src/model/utils/modifyselection.js +229 -0
  66. package/src/model/utils/selection-post-fixer.js +297 -0
  67. package/src/model/writer.js +1574 -0
  68. package/src/view/attributeelement.js +274 -0
  69. package/src/view/containerelement.js +123 -0
  70. package/src/view/document.js +221 -0
  71. package/src/view/documentfragment.js +273 -0
  72. package/src/view/documentselection.js +387 -0
  73. package/src/view/domconverter.js +1437 -0
  74. package/src/view/downcastwriter.js +2121 -0
  75. package/src/view/editableelement.js +118 -0
  76. package/src/view/element.js +945 -0
  77. package/src/view/elementdefinition.jsdoc +59 -0
  78. package/src/view/emptyelement.js +119 -0
  79. package/src/view/filler.js +161 -0
  80. package/src/view/item.jsdoc +14 -0
  81. package/src/view/matcher.js +776 -0
  82. package/src/view/node.js +391 -0
  83. package/src/view/observer/arrowkeysobserver.js +58 -0
  84. package/src/view/observer/bubblingemittermixin.js +307 -0
  85. package/src/view/observer/bubblingeventinfo.js +71 -0
  86. package/src/view/observer/clickobserver.js +46 -0
  87. package/src/view/observer/compositionobserver.js +79 -0
  88. package/src/view/observer/domeventdata.js +82 -0
  89. package/src/view/observer/domeventobserver.js +99 -0
  90. package/src/view/observer/fakeselectionobserver.js +118 -0
  91. package/src/view/observer/focusobserver.js +106 -0
  92. package/src/view/observer/inputobserver.js +44 -0
  93. package/src/view/observer/keyobserver.js +83 -0
  94. package/src/view/observer/mouseobserver.js +56 -0
  95. package/src/view/observer/mutationobserver.js +345 -0
  96. package/src/view/observer/observer.js +118 -0
  97. package/src/view/observer/selectionobserver.js +242 -0
  98. package/src/view/placeholder.js +285 -0
  99. package/src/view/position.js +426 -0
  100. package/src/view/range.js +533 -0
  101. package/src/view/rawelement.js +148 -0
  102. package/src/view/renderer.js +1037 -0
  103. package/src/view/rooteditableelement.js +107 -0
  104. package/src/view/selection.js +718 -0
  105. package/src/view/styles/background.js +73 -0
  106. package/src/view/styles/border.js +362 -0
  107. package/src/view/styles/margin.js +41 -0
  108. package/src/view/styles/padding.js +40 -0
  109. package/src/view/styles/utils.js +277 -0
  110. package/src/view/stylesmap.js +938 -0
  111. package/src/view/text.js +147 -0
  112. package/src/view/textproxy.js +199 -0
  113. package/src/view/treewalker.js +496 -0
  114. package/src/view/uielement.js +238 -0
  115. package/src/view/upcastwriter.js +484 -0
  116. package/src/view/view.js +721 -0
  117. package/theme/placeholder.css +27 -0
@@ -0,0 +1,533 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module engine/view/range
8
+ */
9
+
10
+ import Position from './position';
11
+ import TreeWalker from './treewalker';
12
+
13
+ /**
14
+ * Range in the view tree. A range is represented by its start and end {@link module:engine/view/position~Position positions}.
15
+ *
16
+ * In order to create a new position instance use the `createPosition*()` factory methods available in:
17
+ *
18
+ * * {@link module:engine/view/view~View}
19
+ * * {@link module:engine/view/downcastwriter~DowncastWriter}
20
+ * * {@link module:engine/view/upcastwriter~UpcastWriter}
21
+ */
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
+ }
524
+ }
525
+
526
+ // 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;
533
+ }
@@ -0,0 +1,148 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2021, CKSource - Frederico Knabben. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+
6
+ /**
7
+ * @module engine/view/rawelement
8
+ */
9
+
10
+ import Element from './element';
11
+ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
12
+ import Node from './node';
13
+
14
+ /**
15
+ * The raw element class.
16
+ *
17
+ * The raw elements work as data containers ("wrappers", "sandboxes") but their children are not managed or
18
+ * even recognized by the editor. This encapsulation allows integrations to maintain custom DOM structures
19
+ * in the editor content without, for instance, worrying about compatibility with other editor features.
20
+ * Raw elements are a perfect tool for integration with external frameworks and data sources.
21
+ *
22
+ * Unlike {@link module:engine/view/uielement~UIElement UI elements}, raw elements act like real editor
23
+ * content (similar to {@link module:engine/view/containerelement~ContainerElement} or
24
+ * {@link module:engine/view/emptyelement~EmptyElement}), they are considered by the editor selection and
25
+ * {@link module:widget/utils~toWidget they can work as widgets}.
26
+ *
27
+ * To create a new raw element, use the
28
+ * {@link module:engine/view/downcastwriter~DowncastWriter#createRawElement `downcastWriter#createRawElement()`} method.
29
+ *
30
+ * @extends module:engine/view/element~Element
31
+ */
32
+ export default class RawElement extends Element {
33
+ /**
34
+ * Creates a new instance of a raw element.
35
+ *
36
+ * Throws the `view-rawelement-cannot-add` {@link module:utils/ckeditorerror~CKEditorError CKEditorError} when the `children`
37
+ * parameter is passed to inform that the usage of `RawElement` is incorrect (adding child nodes to `RawElement` is forbidden).
38
+ *
39
+ * @see module:engine/view/downcastwriter~DowncastWriter#createRawElement
40
+ * @protected
41
+ * @param {module:engine/view/document~Document} document The document instance to which this element belongs.
42
+ * @param {String} name A node name.
43
+ * @param {Object|Iterable} [attrs] The collection of attributes.
44
+ * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
45
+ * A list of nodes to be inserted into the created element.
46
+ */
47
+ constructor( document, name, attrs, children ) {
48
+ super( document, name, attrs, children );
49
+
50
+ // Override the default of the base class.
51
+ this._isAllowedInsideAttributeElement = true;
52
+
53
+ /**
54
+ * Returns `null` because filler is not needed for raw elements.
55
+ *
56
+ * @method #getFillerOffset
57
+ * @returns {null} Always returns null.
58
+ */
59
+ this.getFillerOffset = getFillerOffset;
60
+ }
61
+
62
+ /**
63
+ * Checks whether this object is of the given type or name.
64
+ *
65
+ * rawElement.is( 'rawElement' ); // -> true
66
+ * rawElement.is( 'element' ); // -> true
67
+ * rawElement.is( 'node' ); // -> true
68
+ * rawElement.is( 'view:rawElement' ); // -> true
69
+ * rawElement.is( 'view:element' ); // -> true
70
+ * rawElement.is( 'view:node' ); // -> true
71
+ *
72
+ * rawElement.is( 'model:element' ); // -> false
73
+ * rawElement.is( 'documentFragment' ); // -> false
74
+ *
75
+ * Assuming that the object being checked is a raw element, you can also check its
76
+ * {@link module:engine/view/rawelement~RawElement#name name}:
77
+ *
78
+ * rawElement.is( 'img' ); // -> true if this is an img element
79
+ * rawElement.is( 'rawElement', 'img' ); // -> same as above
80
+ * text.is( 'img' ); -> false
81
+ *
82
+ * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
83
+ *
84
+ * @param {String} type The type to check when the `name` parameter is present.
85
+ * Otherwise, it acts like the `name` parameter.
86
+ * @param {String} [name] The element name.
87
+ * @returns {Boolean}
88
+ */
89
+ is( type, name = null ) {
90
+ if ( !name ) {
91
+ return type === 'rawElement' || type === 'view:rawElement' ||
92
+ // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
93
+ type === this.name || type === 'view:' + this.name ||
94
+ type === 'element' || type === 'view:element' ||
95
+ type === 'node' || type === 'view:node';
96
+ } else {
97
+ return name === this.name && (
98
+ type === 'rawElement' || type === 'view:rawElement' ||
99
+ type === 'element' || type === 'view:element'
100
+ );
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Overrides the {@link module:engine/view/element~Element#_insertChild} method.
106
+ * Throws the `view-rawelement-cannot-add` {@link module:utils/ckeditorerror~CKEditorError CKEditorError} to prevent
107
+ * adding any child nodes to a raw element.
108
+ *
109
+ * @protected
110
+ */
111
+ _insertChild( index, nodes ) {
112
+ if ( nodes && ( nodes instanceof Node || Array.from( nodes ).length > 0 ) ) {
113
+ /**
114
+ * Cannot add children to a {@link module:engine/view/rawelement~RawElement} instance.
115
+ *
116
+ * @error view-rawelement-cannot-add
117
+ */
118
+ throw new CKEditorError(
119
+ 'view-rawelement-cannot-add',
120
+ [ this, nodes ]
121
+ );
122
+ }
123
+ }
124
+
125
+ /**
126
+ * This allows rendering the children of a {@link module:engine/view/rawelement~RawElement} on the DOM level.
127
+ * This method is called by the {@link module:engine/view/domconverter~DomConverter} with the raw DOM element
128
+ * passed as an argument, leaving the number and shape of the children up to the integrator.
129
+ *
130
+ * This method **must be defined** for the raw element to work:
131
+ *
132
+ * const myRawElement = downcastWriter.createRawElement( 'div' );
133
+ *
134
+ * myRawElement.render = function( domElement ) {
135
+ * domElement.innerHTML = '<b>This is the raw content of myRawElement.</b>';
136
+ * };
137
+ *
138
+ * @method #render
139
+ * @param {HTMLElement} domElement The native DOM element representing the raw view element.
140
+ */
141
+ }
142
+
143
+ // Returns `null` because block filler is not needed for raw elements.
144
+ //
145
+ // @returns {null}
146
+ function getFillerOffset() {
147
+ return null;
148
+ }