@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
@@ -2,11 +2,9 @@
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 module:engine/view/upcastwriter
8
7
  */
9
-
10
8
  import DocumentFragment from './documentfragment';
11
9
  import Element from './element';
12
10
  import Text from './text';
@@ -14,7 +12,6 @@ import { isPlainObject } from 'lodash-es';
14
12
  import Position from './position';
15
13
  import Range from './range';
16
14
  import Selection from './selection';
17
-
18
15
  /**
19
16
  * View upcast writer. It provides a set of methods used to manipulate non-semantic view trees.
20
17
  *
@@ -35,450 +32,399 @@ import Selection from './selection';
35
32
  * writer.appendChild( text, someViewElement );
36
33
  */
37
34
  export default class UpcastWriter {
38
- /**
39
- * @param {module:engine/view/document~Document} document The view document instance in which this upcast writer operates.
40
- */
41
- constructor( document ) {
42
- /**
43
- * The view document instance in which this upcast writer operates.
44
- *
45
- * @readonly
46
- * @type {module:engine/view/document~Document}
47
- */
48
- this.document = document;
49
- }
50
-
51
- /**
52
- * Creates a new {@link module:engine/view/documentfragment~DocumentFragment} instance.
53
- *
54
- * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
55
- * A list of nodes to be inserted into the created document fragment.
56
- * @returns {module:engine/view/documentfragment~DocumentFragment} The created document fragment.
57
- */
58
- createDocumentFragment( children ) {
59
- return new DocumentFragment( this.document, children );
60
- }
61
-
62
- /**
63
- * Creates a new {@link module:engine/view/element~Element} instance.
64
- *
65
- * Attributes can be passed in various formats:
66
- *
67
- * upcastWriter.createElement( 'div', { class: 'editor', contentEditable: 'true' } ); // object
68
- * upcastWriter.createElement( 'div', [ [ 'class', 'editor' ], [ 'contentEditable', 'true' ] ] ); // map-like iterator
69
- * upcastWriter.createElement( 'div', mapOfAttributes ); // map
70
- *
71
- * @param {String} name Node name.
72
- * @param {Object|Iterable} [attrs] Collection of attributes.
73
- * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
74
- * A list of nodes to be inserted into created element.
75
- * @returns {module:engine/view/element~Element} Created element.
76
- */
77
- createElement( name, attrs, children ) {
78
- return new Element( this.document, name, attrs, children );
79
- }
80
-
81
- /**
82
- * Creates a new {@link module:engine/view/text~Text} instance.
83
- *
84
- * @param {String} data The text's data.
85
- * @returns {module:engine/view/text~Text} The created text node.
86
- */
87
- createText( data ) {
88
- return new Text( this.document, data );
89
- }
90
-
91
- /**
92
- * Clones the provided element.
93
- *
94
- * @see module:engine/view/element~Element#_clone
95
- * @param {module:engine/view/element~Element} element Element to be cloned.
96
- * @param {Boolean} [deep=false] If set to `true` clones element and all its children recursively. When set to `false`,
97
- * element will be cloned without any children.
98
- * @returns {module:engine/view/element~Element} Clone of this element.
99
- */
100
- clone( element, deep = false ) {
101
- return element._clone( deep );
102
- }
103
-
104
- /**
105
- * Appends a child node or a list of child nodes at the end of this node
106
- * and sets the parent of these nodes to this element.
107
- *
108
- * @see module:engine/view/element~Element#_appendChild
109
- * @param {module:engine/view/item~Item|Iterable.<module:engine/view/item~Item>} items Items to be inserted.
110
- * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} element Element
111
- * to which items will be appended.
112
- * @fires module:engine/view/node~Node#event:change
113
- * @returns {Number} Number of appended nodes.
114
- */
115
- appendChild( items, element ) {
116
- return element._appendChild( items );
117
- }
118
-
119
- /**
120
- * Inserts a child node or a list of child nodes on the given index and sets the parent of these nodes to
121
- * this element.
122
- *
123
- * @see module:engine/view/element~Element#_insertChild
124
- * @param {Number} index Offset at which nodes should be inserted.
125
- * @param {module:engine/view/item~Item|Iterable.<module:engine/view/item~Item>} items Items to be inserted.
126
- * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} element Element
127
- * to which items will be inserted.
128
- * @fires module:engine/view/node~Node#event:change
129
- * @returns {Number} Number of inserted nodes.
130
- */
131
- insertChild( index, items, element ) {
132
- return element._insertChild( index, items );
133
- }
134
-
135
- /**
136
- * Removes the given number of child nodes starting at the given index and set the parent of these nodes to `null`.
137
- *
138
- * @see module:engine/view/element~Element#_removeChildren
139
- * @param {Number} index Offset from which nodes will be removed.
140
- * @param {Number} howMany Number of nodes to remove.
141
- * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} element Element
142
- * which children will be removed.
143
- * @fires module:engine/view/node~Node#event:change
144
- * @returns {Array.<module:engine/view/node~Node>} The array containing removed nodes.
145
- */
146
- removeChildren( index, howMany, element ) {
147
- return element._removeChildren( index, howMany );
148
- }
149
-
150
- /**
151
- * Removes given element from the view structure. Will not have effect on detached elements.
152
- *
153
- * @param {module:engine/view/element~Element} element Element which will be removed.
154
- * @returns {Array.<module:engine/view/node~Node>} The array containing removed nodes.
155
- */
156
- remove( element ) {
157
- const parent = element.parent;
158
-
159
- if ( parent ) {
160
- return this.removeChildren( parent.getChildIndex( element ), 1, parent );
161
- }
162
-
163
- return [];
164
- }
165
-
166
- /**
167
- * Replaces given element with the new one in the view structure. Will not have effect on detached elements.
168
- *
169
- * @param {module:engine/view/element~Element} oldElement Element which will be replaced.
170
- * @param {module:engine/view/element~Element} newElement Element which will be inserted in the place of the old element.
171
- * @returns {Boolean} Whether old element was successfully replaced.
172
- */
173
- replace( oldElement, newElement ) {
174
- const parent = oldElement.parent;
175
-
176
- if ( parent ) {
177
- const index = parent.getChildIndex( oldElement );
178
-
179
- this.removeChildren( index, 1, parent );
180
- this.insertChild( index, newElement, parent );
181
-
182
- return true;
183
- }
184
-
185
- return false;
186
- }
187
-
188
- /**
189
- * Removes given element from view structure and places its children in its position.
190
- * It does nothing if element has no parent.
191
- *
192
- * @param {module:engine/view/element~Element} element Element to unwrap.
193
- */
194
- unwrapElement( element ) {
195
- const parent = element.parent;
196
-
197
- if ( parent ) {
198
- const index = parent.getChildIndex( element );
199
-
200
- this.remove( element );
201
- this.insertChild( index, element.getChildren(), parent );
202
- }
203
- }
204
-
205
- /**
206
- * Renames element by creating a copy of a given element but with its name changed and then moving contents of the
207
- * old element to the new one.
208
- *
209
- * Since this function creates a new element and removes the given one, the new element is returned to keep reference.
210
- *
211
- * @param {String} newName New element name.
212
- * @param {module:engine/view/element~Element} element Element to be renamed.
213
- * @returns {module:engine/view/element~Element|null} New element or null if the old element
214
- * was not replaced (happens for detached elements).
215
- */
216
- rename( newName, element ) {
217
- const newElement = new Element( this.document, newName, element.getAttributes(), element.getChildren() );
218
-
219
- return this.replace( element, newElement ) ? newElement : null;
220
- }
221
-
222
- /**
223
- * Adds or overwrites element's attribute with a specified key and value.
224
- *
225
- * writer.setAttribute( 'href', 'http://ckeditor.com', linkElement );
226
- *
227
- * @see module:engine/view/element~Element#_setAttribute
228
- * @param {String} key Attribute key.
229
- * @param {String} value Attribute value.
230
- * @param {module:engine/view/element~Element} element Element for which attribute will be set.
231
- */
232
- setAttribute( key, value, element ) {
233
- element._setAttribute( key, value );
234
- }
235
-
236
- /**
237
- * Removes attribute from the element.
238
- *
239
- * writer.removeAttribute( 'href', linkElement );
240
- *
241
- * @see module:engine/view/element~Element#_removeAttribute
242
- * @param {String} key Attribute key.
243
- * @param {module:engine/view/element~Element} element Element from which attribute will be removed.
244
- */
245
- removeAttribute( key, element ) {
246
- element._removeAttribute( key );
247
- }
248
-
249
- /**
250
- * Adds specified class to the element.
251
- *
252
- * writer.addClass( 'foo', linkElement );
253
- * writer.addClass( [ 'foo', 'bar' ], linkElement );
254
- *
255
- * @see module:engine/view/element~Element#_addClass
256
- * @param {Array.<String>|String} className Single class name or array of class names which will be added.
257
- * @param {module:engine/view/element~Element} element Element for which class will be added.
258
- */
259
- addClass( className, element ) {
260
- element._addClass( className );
261
- }
262
-
263
- /**
264
- * Removes specified class from the element.
265
- *
266
- * writer.removeClass( 'foo', linkElement );
267
- * writer.removeClass( [ 'foo', 'bar' ], linkElement );
268
- *
269
- * @see module:engine/view/element~Element#_removeClass
270
- * @param {Array.<String>|String} className Single class name or array of class names which will be removed.
271
- * @param {module:engine/view/element~Element} element Element from which class will be removed.
272
- */
273
- removeClass( className, element ) {
274
- element._removeClass( className );
275
- }
276
-
277
- /**
278
- * Adds style to the element.
279
- *
280
- * writer.setStyle( 'color', 'red', element );
281
- * writer.setStyle( {
282
- * color: 'red',
283
- * position: 'fixed'
284
- * }, element );
285
- *
286
- * **Note**: This method can work with normalized style names if
287
- * {@link module:engine/controller/datacontroller~DataController#addStyleProcessorRules a particular style processor rule is enabled}.
288
- * See {@link module:engine/view/stylesmap~StylesMap#set `StylesMap#set()`} for details.
289
- *
290
- * @see module:engine/view/element~Element#_setStyle
291
- * @param {String|Object} property Property name or object with key - value pairs.
292
- * @param {String} [value] Value to set. This parameter is ignored if object is provided as the first parameter.
293
- * @param {module:engine/view/element~Element} element Element for which style will be added.
294
- */
295
- setStyle( property, value, element ) {
296
- if ( isPlainObject( property ) && element === undefined ) {
297
- element = value;
298
- }
299
- element._setStyle( property, value );
300
- }
301
-
302
- /**
303
- * Removes specified style from the element.
304
- *
305
- * writer.removeStyle( 'color', element ); // Removes 'color' style.
306
- * writer.removeStyle( [ 'color', 'border-top' ], element ); // Removes both 'color' and 'border-top' styles.
307
- *
308
- * **Note**: This method can work with normalized style names if
309
- * {@link module:engine/controller/datacontroller~DataController#addStyleProcessorRules a particular style processor rule is enabled}.
310
- * See {@link module:engine/view/stylesmap~StylesMap#remove `StylesMap#remove()`} for details.
311
- *
312
- * @see module:engine/view/element~Element#_removeStyle
313
- * @param {Array.<String>|String} property Style property name or names to be removed.
314
- * @param {module:engine/view/element~Element} element Element from which style will be removed.
315
- */
316
- removeStyle( property, element ) {
317
- element._removeStyle( property );
318
- }
319
-
320
- /**
321
- * Sets a custom property on element. Unlike attributes, custom properties are not rendered to the DOM,
322
- * so they can be used to add special data to elements.
323
- *
324
- * @see module:engine/view/element~Element#_setCustomProperty
325
- * @param {String|Symbol} key Custom property name/key.
326
- * @param {*} value Custom property value to be stored.
327
- * @param {module:engine/view/element~Element} element Element for which custom property will be set.
328
- */
329
- setCustomProperty( key, value, element ) {
330
- element._setCustomProperty( key, value );
331
- }
332
-
333
- /**
334
- * Removes a custom property stored under the given key.
335
- *
336
- * @see module:engine/view/element~Element#_removeCustomProperty
337
- * @param {String|Symbol} key Name/key of the custom property to be removed.
338
- * @param {module:engine/view/element~Element} element Element from which the custom property will be removed.
339
- * @returns {Boolean} Returns true if property was removed.
340
- */
341
- removeCustomProperty( key, element ) {
342
- return element._removeCustomProperty( key );
343
- }
344
-
345
- /**
346
- * Creates position at the given location. The location can be specified as:
347
- *
348
- * * a {@link module:engine/view/position~Position position},
349
- * * parent element and offset (offset defaults to `0`),
350
- * * parent element and `'end'` (sets position at the end of that element),
351
- * * {@link module:engine/view/item~Item view item} and `'before'` or `'after'` (sets position before or after given view item).
352
- *
353
- * This method is a shortcut to other constructors such as:
354
- *
355
- * * {@link #createPositionBefore},
356
- * * {@link #createPositionAfter},
357
- *
358
- * @param {module:engine/view/item~Item|module:engine/model/position~Position} itemOrPosition
359
- * @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
360
- * first parameter is a {@link module:engine/view/item~Item view item}.
361
- * @returns {module:engine/view/position~Position}
362
- */
363
- createPositionAt( itemOrPosition, offset ) {
364
- return Position._createAt( itemOrPosition, offset );
365
- }
366
-
367
- /**
368
- * Creates a new position after given view item.
369
- *
370
- * @param {module:engine/view/item~Item} item View item after which the position should be located.
371
- * @returns {module:engine/view/position~Position}
372
- */
373
- createPositionAfter( item ) {
374
- return Position._createAfter( item );
375
- }
376
-
377
- /**
378
- * Creates a new position before given view item.
379
- *
380
- * @param {module:engine/view/item~Item} item View item before which the position should be located.
381
- * @returns {module:engine/view/position~Position}
382
- */
383
- createPositionBefore( item ) {
384
- return Position._createBefore( item );
385
- }
386
-
387
- /**
388
- * Creates a range spanning from `start` position to `end` position.
389
- *
390
- * **Note:** This factory method creates it's own {@link module:engine/view/position~Position} instances basing on passed values.
391
- *
392
- * @param {module:engine/view/position~Position} start Start position.
393
- * @param {module:engine/view/position~Position} [end] End position. If not set, range will be collapsed at `start` position.
394
- * @returns {module:engine/view/range~Range}
395
- */
396
- createRange( start, end ) {
397
- return new Range( start, end );
398
- }
399
-
400
- /**
401
- * Creates a range that starts before given {@link module:engine/view/item~Item view item} and ends after it.
402
- *
403
- * @param {module:engine/view/item~Item} item
404
- * @returns {module:engine/view/range~Range}
405
- */
406
- createRangeOn( item ) {
407
- return Range._createOn( item );
408
- }
409
-
410
- /**
411
- * Creates a range inside an {@link module:engine/view/element~Element element} which starts before the first child of
412
- * that element and ends after the last child of that element.
413
- *
414
- * @param {module:engine/view/element~Element} element Element which is a parent for the range.
415
- * @returns {module:engine/view/range~Range}
416
- */
417
- createRangeIn( element ) {
418
- return Range._createIn( element );
419
- }
420
-
421
- /**
422
- * Creates a new {@link module:engine/view/selection~Selection} instance.
423
- *
424
- * // Creates empty selection without ranges.
425
- * const selection = writer.createSelection();
426
- *
427
- * // Creates selection at the given range.
428
- * const range = writer.createRange( start, end );
429
- * const selection = writer.createSelection( range );
430
- *
431
- * // Creates selection at the given ranges
432
- * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ];
433
- * const selection = writer.createSelection( ranges );
434
- *
435
- * // Creates selection from the other selection.
436
- * const otherSelection = writer.createSelection();
437
- * const selection = writer.createSelection( otherSelection );
438
- *
439
- * // Creates selection from the document selection.
440
- * const selection = writer.createSelection( editor.editing.view.document.selection );
441
- *
442
- * // Creates selection at the given position.
443
- * const position = writer.createPositionFromPath( root, path );
444
- * const selection = writer.createSelection( position );
445
- *
446
- * // Creates collapsed selection at the position of given item and offset.
447
- * const paragraph = writer.createContainerElement( 'paragraph' );
448
- * const selection = writer.createSelection( paragraph, offset );
449
- *
450
- * // Creates a range inside an {@link module:engine/view/element~Element element} which starts before the
451
- * // first child of that element and ends after the last child of that element.
452
- * const selection = writer.createSelection( paragraph, 'in' );
453
- *
454
- * // Creates a range on an {@link module:engine/view/item~Item item} which starts before the item and ends
455
- * // just after the item.
456
- * const selection = writer.createSelection( paragraph, 'on' );
457
- *
458
- * `Selection`'s constructor allow passing additional options (`backward`, `fake` and `label`) as the last argument.
459
- *
460
- * // Creates backward selection.
461
- * const selection = writer.createSelection( range, { backward: true } );
462
- *
463
- * Fake selection does not render as browser native selection over selected elements and is hidden to the user.
464
- * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
465
- * represented in other way, for example by applying proper CSS class.
466
- *
467
- * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM
468
- * (and be properly handled by screen readers).
469
- *
470
- * // Creates fake selection with label.
471
- * const selection = writer.createSelection( range, { fake: true, label: 'foo' } );
472
- *
473
- * @param {module:engine/view/selection~Selectable} [selectable=null]
474
- * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Offset or place when selectable is an `Item`.
475
- * @param {Object} [options]
476
- * @param {Boolean} [options.backward] Sets this selection instance to be backward.
477
- * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
478
- * @param {String} [options.label] Label for the fake selection.
479
- * @returns {module:engine/view/selection~Selection}
480
- */
481
- createSelection( selectable, placeOrOffset, options ) {
482
- return new Selection( selectable, placeOrOffset, options );
483
- }
35
+ /**
36
+ * @param {module:engine/view/document~Document} document The view document instance in which this upcast writer operates.
37
+ */
38
+ constructor(document) {
39
+ /**
40
+ * The view document instance in which this upcast writer operates.
41
+ *
42
+ * @readonly
43
+ * @type {module:engine/view/document~Document}
44
+ */
45
+ this.document = document;
46
+ }
47
+ /**
48
+ * Creates a new {@link module:engine/view/documentfragment~DocumentFragment} instance.
49
+ *
50
+ * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
51
+ * A list of nodes to be inserted into the created document fragment.
52
+ * @returns {module:engine/view/documentfragment~DocumentFragment} The created document fragment.
53
+ */
54
+ createDocumentFragment(children) {
55
+ return new DocumentFragment(this.document, children);
56
+ }
57
+ /**
58
+ * Creates a new {@link module:engine/view/element~Element} instance.
59
+ *
60
+ * Attributes can be passed in various formats:
61
+ *
62
+ * upcastWriter.createElement( 'div', { class: 'editor', contentEditable: 'true' } ); // object
63
+ * upcastWriter.createElement( 'div', [ [ 'class', 'editor' ], [ 'contentEditable', 'true' ] ] ); // map-like iterator
64
+ * upcastWriter.createElement( 'div', mapOfAttributes ); // map
65
+ *
66
+ * @param {String} name Node name.
67
+ * @param {Object|Iterable} [attrs] Collection of attributes.
68
+ * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
69
+ * A list of nodes to be inserted into created element.
70
+ * @returns {module:engine/view/element~Element} Created element.
71
+ */
72
+ createElement(name, attrs, children) {
73
+ return new Element(this.document, name, attrs, children);
74
+ }
75
+ /**
76
+ * Creates a new {@link module:engine/view/text~Text} instance.
77
+ *
78
+ * @param {String} data The text's data.
79
+ * @returns {module:engine/view/text~Text} The created text node.
80
+ */
81
+ createText(data) {
82
+ return new Text(this.document, data);
83
+ }
84
+ /**
85
+ * Clones the provided element.
86
+ *
87
+ * @see module:engine/view/element~Element#_clone
88
+ * @param {module:engine/view/element~Element} element Element to be cloned.
89
+ * @param {Boolean} [deep=false] If set to `true` clones element and all its children recursively. When set to `false`,
90
+ * element will be cloned without any children.
91
+ * @returns {module:engine/view/element~Element} Clone of this element.
92
+ */
93
+ clone(element, deep = false) {
94
+ return element._clone(deep);
95
+ }
96
+ /**
97
+ * Appends a child node or a list of child nodes at the end of this node
98
+ * and sets the parent of these nodes to this element.
99
+ *
100
+ * @see module:engine/view/element~Element#_appendChild
101
+ * @param {module:engine/view/item~Item|Iterable.<module:engine/view/item~Item>} items Items to be inserted.
102
+ * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} element Element
103
+ * to which items will be appended.
104
+ * @fires module:engine/view/node~Node#event:change
105
+ * @returns {Number} Number of appended nodes.
106
+ */
107
+ appendChild(items, element) {
108
+ return element._appendChild(items);
109
+ }
110
+ /**
111
+ * Inserts a child node or a list of child nodes on the given index and sets the parent of these nodes to
112
+ * this element.
113
+ *
114
+ * @see module:engine/view/element~Element#_insertChild
115
+ * @param {Number} index Offset at which nodes should be inserted.
116
+ * @param {module:engine/view/item~Item|Iterable.<module:engine/view/item~Item>} items Items to be inserted.
117
+ * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} element Element
118
+ * to which items will be inserted.
119
+ * @fires module:engine/view/node~Node#event:change
120
+ * @returns {Number} Number of inserted nodes.
121
+ */
122
+ insertChild(index, items, element) {
123
+ return element._insertChild(index, items);
124
+ }
125
+ /**
126
+ * Removes the given number of child nodes starting at the given index and set the parent of these nodes to `null`.
127
+ *
128
+ * @see module:engine/view/element~Element#_removeChildren
129
+ * @param {Number} index Offset from which nodes will be removed.
130
+ * @param {Number} howMany Number of nodes to remove.
131
+ * @param {module:engine/view/element~Element|module:engine/view/documentfragment~DocumentFragment} element Element
132
+ * which children will be removed.
133
+ * @fires module:engine/view/node~Node#event:change
134
+ * @returns {Array.<module:engine/view/node~Node>} The array containing removed nodes.
135
+ */
136
+ removeChildren(index, howMany, element) {
137
+ return element._removeChildren(index, howMany);
138
+ }
139
+ /**
140
+ * Removes given element from the view structure. Will not have effect on detached elements.
141
+ *
142
+ * @param {module:engine/view/element~Element} element Element which will be removed.
143
+ * @returns {Array.<module:engine/view/node~Node>} The array containing removed nodes.
144
+ */
145
+ remove(element) {
146
+ const parent = element.parent;
147
+ if (parent) {
148
+ return this.removeChildren(parent.getChildIndex(element), 1, parent);
149
+ }
150
+ return [];
151
+ }
152
+ /**
153
+ * Replaces given element with the new one in the view structure. Will not have effect on detached elements.
154
+ *
155
+ * @param {module:engine/view/element~Element} oldElement Element which will be replaced.
156
+ * @param {module:engine/view/element~Element} newElement Element which will be inserted in the place of the old element.
157
+ * @returns {Boolean} Whether old element was successfully replaced.
158
+ */
159
+ replace(oldElement, newElement) {
160
+ const parent = oldElement.parent;
161
+ if (parent) {
162
+ const index = parent.getChildIndex(oldElement);
163
+ this.removeChildren(index, 1, parent);
164
+ this.insertChild(index, newElement, parent);
165
+ return true;
166
+ }
167
+ return false;
168
+ }
169
+ /**
170
+ * Removes given element from view structure and places its children in its position.
171
+ * It does nothing if element has no parent.
172
+ *
173
+ * @param {module:engine/view/element~Element} element Element to unwrap.
174
+ */
175
+ unwrapElement(element) {
176
+ const parent = element.parent;
177
+ if (parent) {
178
+ const index = parent.getChildIndex(element);
179
+ this.remove(element);
180
+ this.insertChild(index, element.getChildren(), parent);
181
+ }
182
+ }
183
+ /**
184
+ * Renames element by creating a copy of a given element but with its name changed and then moving contents of the
185
+ * old element to the new one.
186
+ *
187
+ * Since this function creates a new element and removes the given one, the new element is returned to keep reference.
188
+ *
189
+ * @param {String} newName New element name.
190
+ * @param {module:engine/view/element~Element} element Element to be renamed.
191
+ * @returns {module:engine/view/element~Element|null} New element or null if the old element
192
+ * was not replaced (happens for detached elements).
193
+ */
194
+ rename(newName, element) {
195
+ const newElement = new Element(this.document, newName, element.getAttributes(), element.getChildren());
196
+ return this.replace(element, newElement) ? newElement : null;
197
+ }
198
+ /**
199
+ * Adds or overwrites element's attribute with a specified key and value.
200
+ *
201
+ * writer.setAttribute( 'href', 'http://ckeditor.com', linkElement );
202
+ *
203
+ * @see module:engine/view/element~Element#_setAttribute
204
+ * @param {String} key Attribute key.
205
+ * @param {String} value Attribute value.
206
+ * @param {module:engine/view/element~Element} element Element for which attribute will be set.
207
+ */
208
+ setAttribute(key, value, element) {
209
+ element._setAttribute(key, value);
210
+ }
211
+ /**
212
+ * Removes attribute from the element.
213
+ *
214
+ * writer.removeAttribute( 'href', linkElement );
215
+ *
216
+ * @see module:engine/view/element~Element#_removeAttribute
217
+ * @param {String} key Attribute key.
218
+ * @param {module:engine/view/element~Element} element Element from which attribute will be removed.
219
+ */
220
+ removeAttribute(key, element) {
221
+ element._removeAttribute(key);
222
+ }
223
+ /**
224
+ * Adds specified class to the element.
225
+ *
226
+ * writer.addClass( 'foo', linkElement );
227
+ * writer.addClass( [ 'foo', 'bar' ], linkElement );
228
+ *
229
+ * @see module:engine/view/element~Element#_addClass
230
+ * @param {Array.<String>|String} className Single class name or array of class names which will be added.
231
+ * @param {module:engine/view/element~Element} element Element for which class will be added.
232
+ */
233
+ addClass(className, element) {
234
+ element._addClass(className);
235
+ }
236
+ /**
237
+ * Removes specified class from the element.
238
+ *
239
+ * writer.removeClass( 'foo', linkElement );
240
+ * writer.removeClass( [ 'foo', 'bar' ], linkElement );
241
+ *
242
+ * @see module:engine/view/element~Element#_removeClass
243
+ * @param {Array.<String>|String} className Single class name or array of class names which will be removed.
244
+ * @param {module:engine/view/element~Element} element Element from which class will be removed.
245
+ */
246
+ removeClass(className, element) {
247
+ element._removeClass(className);
248
+ }
249
+ setStyle(property, valueOrElement, element) {
250
+ if (isPlainObject(property) && element === undefined) {
251
+ valueOrElement._setStyle(property);
252
+ }
253
+ else {
254
+ element._setStyle(property, valueOrElement);
255
+ }
256
+ }
257
+ /**
258
+ * Removes specified style from the element.
259
+ *
260
+ * writer.removeStyle( 'color', element ); // Removes 'color' style.
261
+ * writer.removeStyle( [ 'color', 'border-top' ], element ); // Removes both 'color' and 'border-top' styles.
262
+ *
263
+ * **Note**: This method can work with normalized style names if
264
+ * {@link module:engine/controller/datacontroller~DataController#addStyleProcessorRules a particular style processor rule is enabled}.
265
+ * See {@link module:engine/view/stylesmap~StylesMap#remove `StylesMap#remove()`} for details.
266
+ *
267
+ * @see module:engine/view/element~Element#_removeStyle
268
+ * @param {Array.<String>|String} property Style property name or names to be removed.
269
+ * @param {module:engine/view/element~Element} element Element from which style will be removed.
270
+ */
271
+ removeStyle(property, element) {
272
+ element._removeStyle(property);
273
+ }
274
+ /**
275
+ * Sets a custom property on element. Unlike attributes, custom properties are not rendered to the DOM,
276
+ * so they can be used to add special data to elements.
277
+ *
278
+ * @see module:engine/view/element~Element#_setCustomProperty
279
+ * @param {String|Symbol} key Custom property name/key.
280
+ * @param {*} value Custom property value to be stored.
281
+ * @param {module:engine/view/element~Element} element Element for which custom property will be set.
282
+ */
283
+ setCustomProperty(key, value, element) {
284
+ element._setCustomProperty(key, value);
285
+ }
286
+ /**
287
+ * Removes a custom property stored under the given key.
288
+ *
289
+ * @see module:engine/view/element~Element#_removeCustomProperty
290
+ * @param {String|Symbol} key Name/key of the custom property to be removed.
291
+ * @param {module:engine/view/element~Element} element Element from which the custom property will be removed.
292
+ * @returns {Boolean} Returns true if property was removed.
293
+ */
294
+ removeCustomProperty(key, element) {
295
+ return element._removeCustomProperty(key);
296
+ }
297
+ /**
298
+ * Creates position at the given location. The location can be specified as:
299
+ *
300
+ * * a {@link module:engine/view/position~Position position},
301
+ * * parent element and offset (offset defaults to `0`),
302
+ * * parent element and `'end'` (sets position at the end of that element),
303
+ * * {@link module:engine/view/item~Item view item} and `'before'` or `'after'` (sets position before or after given view item).
304
+ *
305
+ * This method is a shortcut to other constructors such as:
306
+ *
307
+ * * {@link #createPositionBefore},
308
+ * * {@link #createPositionAfter},
309
+ *
310
+ * @param {module:engine/view/item~Item|module:engine/model/position~Position} itemOrPosition
311
+ * @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
312
+ * first parameter is a {@link module:engine/view/item~Item view item}.
313
+ * @returns {module:engine/view/position~Position}
314
+ */
315
+ createPositionAt(itemOrPosition, offset) {
316
+ return Position._createAt(itemOrPosition, offset);
317
+ }
318
+ /**
319
+ * Creates a new position after given view item.
320
+ *
321
+ * @param {module:engine/view/item~Item} item View item after which the position should be located.
322
+ * @returns {module:engine/view/position~Position}
323
+ */
324
+ createPositionAfter(item) {
325
+ return Position._createAfter(item);
326
+ }
327
+ /**
328
+ * Creates a new position before given view item.
329
+ *
330
+ * @param {module:engine/view/item~Item} item View item before which the position should be located.
331
+ * @returns {module:engine/view/position~Position}
332
+ */
333
+ createPositionBefore(item) {
334
+ return Position._createBefore(item);
335
+ }
336
+ /**
337
+ * Creates a range spanning from `start` position to `end` position.
338
+ *
339
+ * **Note:** This factory method creates it's own {@link module:engine/view/position~Position} instances basing on passed values.
340
+ *
341
+ * @param {module:engine/view/position~Position} start Start position.
342
+ * @param {module:engine/view/position~Position} [end] End position. If not set, range will be collapsed at `start` position.
343
+ * @returns {module:engine/view/range~Range}
344
+ */
345
+ createRange(start, end) {
346
+ return new Range(start, end);
347
+ }
348
+ /**
349
+ * Creates a range that starts before given {@link module:engine/view/item~Item view item} and ends after it.
350
+ *
351
+ * @param {module:engine/view/item~Item} item
352
+ * @returns {module:engine/view/range~Range}
353
+ */
354
+ createRangeOn(item) {
355
+ return Range._createOn(item);
356
+ }
357
+ /**
358
+ * Creates a range inside an {@link module:engine/view/element~Element element} which starts before the first child of
359
+ * that element and ends after the last child of that element.
360
+ *
361
+ * @param {module:engine/view/element~Element} element Element which is a parent for the range.
362
+ * @returns {module:engine/view/range~Range}
363
+ */
364
+ createRangeIn(element) {
365
+ return Range._createIn(element);
366
+ }
367
+ /**
368
+ * Creates a new {@link module:engine/view/selection~Selection} instance.
369
+ *
370
+ * // Creates empty selection without ranges.
371
+ * const selection = writer.createSelection();
372
+ *
373
+ * // Creates selection at the given range.
374
+ * const range = writer.createRange( start, end );
375
+ * const selection = writer.createSelection( range );
376
+ *
377
+ * // Creates selection at the given ranges
378
+ * const ranges = [ writer.createRange( start1, end2 ), writer.createRange( star2, end2 ) ];
379
+ * const selection = writer.createSelection( ranges );
380
+ *
381
+ * // Creates selection from the other selection.
382
+ * const otherSelection = writer.createSelection();
383
+ * const selection = writer.createSelection( otherSelection );
384
+ *
385
+ * // Creates selection from the document selection.
386
+ * const selection = writer.createSelection( editor.editing.view.document.selection );
387
+ *
388
+ * // Creates selection at the given position.
389
+ * const position = writer.createPositionFromPath( root, path );
390
+ * const selection = writer.createSelection( position );
391
+ *
392
+ * // Creates collapsed selection at the position of given item and offset.
393
+ * const paragraph = writer.createContainerElement( 'paragraph' );
394
+ * const selection = writer.createSelection( paragraph, offset );
395
+ *
396
+ * // Creates a range inside an {@link module:engine/view/element~Element element} which starts before the
397
+ * // first child of that element and ends after the last child of that element.
398
+ * const selection = writer.createSelection( paragraph, 'in' );
399
+ *
400
+ * // Creates a range on an {@link module:engine/view/item~Item item} which starts before the item and ends
401
+ * // just after the item.
402
+ * const selection = writer.createSelection( paragraph, 'on' );
403
+ *
404
+ * `Selection`'s constructor allow passing additional options (`backward`, `fake` and `label`) as the last argument.
405
+ *
406
+ * // Creates backward selection.
407
+ * const selection = writer.createSelection( range, { backward: true } );
408
+ *
409
+ * Fake selection does not render as browser native selection over selected elements and is hidden to the user.
410
+ * This way, no native selection UI artifacts are displayed to the user and selection over elements can be
411
+ * represented in other way, for example by applying proper CSS class.
412
+ *
413
+ * Additionally fake's selection label can be provided. It will be used to describe fake selection in DOM
414
+ * (and be properly handled by screen readers).
415
+ *
416
+ * // Creates fake selection with label.
417
+ * const selection = writer.createSelection( range, { fake: true, label: 'foo' } );
418
+ *
419
+ * @param {module:engine/view/selection~Selectable} [selectable=null]
420
+ * @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Offset or place when selectable is an `Item`.
421
+ * @param {Object} [options]
422
+ * @param {Boolean} [options.backward] Sets this selection instance to be backward.
423
+ * @param {Boolean} [options.fake] Sets this selection instance to be marked as `fake`.
424
+ * @param {String} [options.label] Label for the fake selection.
425
+ * @returns {module:engine/view/selection~Selection}
426
+ */
427
+ createSelection(...args) {
428
+ return new Selection(...args);
429
+ }
484
430
  }