@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,945 @@
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/element
8
+ */
9
+
10
+ import Node from './node';
11
+ import Text from './text';
12
+ import TextProxy from './textproxy';
13
+ import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
14
+ import toArray from '@ckeditor/ckeditor5-utils/src/toarray';
15
+ import isIterable from '@ckeditor/ckeditor5-utils/src/isiterable';
16
+ import Matcher from './matcher';
17
+ import StylesMap from './stylesmap';
18
+
19
+ // @if CK_DEBUG_ENGINE // const { convertMapToTags } = require( '../dev-utils/utils' );
20
+
21
+ /**
22
+ * View element.
23
+ *
24
+ * The editing engine does not define a fixed semantics of its elements (it is "DTD-free").
25
+ * This is why the type of the {@link module:engine/view/element~Element} need to
26
+ * be defined by the feature developer. When creating an element you should use one of the following methods:
27
+ *
28
+ * * {@link module:engine/view/downcastwriter~DowncastWriter#createContainerElement `downcastWriter#createContainerElement()`}
29
+ * in order to create a {@link module:engine/view/containerelement~ContainerElement},
30
+ * * {@link module:engine/view/downcastwriter~DowncastWriter#createAttributeElement `downcastWriter#createAttributeElement()`}
31
+ * in order to create a {@link module:engine/view/attributeelement~AttributeElement},
32
+ * * {@link module:engine/view/downcastwriter~DowncastWriter#createEmptyElement `downcastWriter#createEmptyElement()`}
33
+ * in order to create a {@link module:engine/view/emptyelement~EmptyElement}.
34
+ * * {@link module:engine/view/downcastwriter~DowncastWriter#createUIElement `downcastWriter#createUIElement()`}
35
+ * in order to create a {@link module:engine/view/uielement~UIElement}.
36
+ * * {@link module:engine/view/downcastwriter~DowncastWriter#createEditableElement `downcastWriter#createEditableElement()`}
37
+ * in order to create a {@link module:engine/view/editableelement~EditableElement}.
38
+ *
39
+ * Note that for view elements which are not created from the model, like elements from mutations, paste or
40
+ * {@link module:engine/controller/datacontroller~DataController#set data.set} it is not possible to define the type of the element.
41
+ * In such cases the {@link module:engine/view/upcastwriter~UpcastWriter#createElement `UpcastWriter#createElement()`} method
42
+ * should be used to create generic view elements.
43
+ *
44
+ * @extends module:engine/view/node~Node
45
+ */
46
+ export default class Element extends Node {
47
+ /**
48
+ * Creates a view element.
49
+ *
50
+ * Attributes can be passed in various formats:
51
+ *
52
+ * new Element( viewDocument, 'div', { class: 'editor', contentEditable: 'true' } ); // object
53
+ * new Element( viewDocument, 'div', [ [ 'class', 'editor' ], [ 'contentEditable', 'true' ] ] ); // map-like iterator
54
+ * new Element( viewDocument, 'div', mapOfAttributes ); // map
55
+ *
56
+ * @protected
57
+ * @param {module:engine/view/document~Document} document The document instance to which this element belongs.
58
+ * @param {String} name Node name.
59
+ * @param {Object|Iterable} [attrs] Collection of attributes.
60
+ * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
61
+ * A list of nodes to be inserted into created element.
62
+ */
63
+ constructor( document, name, attrs, children ) {
64
+ super( document );
65
+
66
+ /**
67
+ * Name of the element.
68
+ *
69
+ * @readonly
70
+ * @member {String}
71
+ */
72
+ this.name = name;
73
+
74
+ /**
75
+ * Map of attributes, where attributes names are keys and attributes values are values.
76
+ *
77
+ * @protected
78
+ * @member {Map} #_attrs
79
+ */
80
+ this._attrs = parseAttributes( attrs );
81
+
82
+ /**
83
+ * Array of child nodes.
84
+ *
85
+ * @protected
86
+ * @member {Array.<module:engine/view/node~Node>}
87
+ */
88
+ this._children = [];
89
+
90
+ if ( children ) {
91
+ this._insertChild( 0, children );
92
+ }
93
+
94
+ /**
95
+ * Set of classes associated with element instance.
96
+ *
97
+ * @protected
98
+ * @member {Set}
99
+ */
100
+ this._classes = new Set();
101
+
102
+ if ( this._attrs.has( 'class' ) ) {
103
+ // Remove class attribute and handle it by class set.
104
+ const classString = this._attrs.get( 'class' );
105
+ parseClasses( this._classes, classString );
106
+ this._attrs.delete( 'class' );
107
+ }
108
+
109
+ /**
110
+ * Normalized styles.
111
+ *
112
+ * @protected
113
+ * @member {module:engine/view/stylesmap~StylesMap} module:engine/view/element~Element#_styles
114
+ */
115
+ this._styles = new StylesMap( this.document.stylesProcessor );
116
+
117
+ if ( this._attrs.has( 'style' ) ) {
118
+ // Remove style attribute and handle it by styles map.
119
+ this._styles.setTo( this._attrs.get( 'style' ) );
120
+
121
+ this._attrs.delete( 'style' );
122
+ }
123
+
124
+ /**
125
+ * Map of custom properties.
126
+ * Custom properties can be added to element instance, will be cloned but not rendered into DOM.
127
+ *
128
+ * @protected
129
+ * @member {Map}
130
+ */
131
+ this._customProperties = new Map();
132
+
133
+ /**
134
+ * Whether an element is allowed inside an AttributeElement and can be wrapped with
135
+ * {@link module:engine/view/attributeelement~AttributeElement} by {@link module:engine/view/downcastwriter~DowncastWriter}.
136
+ *
137
+ * @protected
138
+ * @member {Boolean}
139
+ */
140
+ this._isAllowedInsideAttributeElement = false;
141
+ }
142
+
143
+ /**
144
+ * Number of element's children.
145
+ *
146
+ * @readonly
147
+ * @type {Number}
148
+ */
149
+ get childCount() {
150
+ return this._children.length;
151
+ }
152
+
153
+ /**
154
+ * Is `true` if there are no nodes inside this element, `false` otherwise.
155
+ *
156
+ * @readonly
157
+ * @type {Boolean}
158
+ */
159
+ get isEmpty() {
160
+ return this._children.length === 0;
161
+ }
162
+
163
+ /**
164
+ * Whether the element is allowed inside an AttributeElement and can be wrapped with
165
+ * {@link module:engine/view/attributeelement~AttributeElement} by {@link module:engine/view/downcastwriter~DowncastWriter}.
166
+ *
167
+ * @readonly
168
+ * @type {Boolean}
169
+ */
170
+ get isAllowedInsideAttributeElement() {
171
+ return this._isAllowedInsideAttributeElement;
172
+ }
173
+
174
+ /**
175
+ * Checks whether this object is of the given.
176
+ *
177
+ * element.is( 'element' ); // -> true
178
+ * element.is( 'node' ); // -> true
179
+ * element.is( 'view:element' ); // -> true
180
+ * element.is( 'view:node' ); // -> true
181
+ *
182
+ * element.is( 'model:element' ); // -> false
183
+ * element.is( 'documentSelection' ); // -> false
184
+ *
185
+ * Assuming that the object being checked is an element, you can also check its
186
+ * {@link module:engine/view/element~Element#name name}:
187
+ *
188
+ * element.is( 'element', 'img' ); // -> true if this is an <img> element
189
+ * text.is( 'element', 'img' ); -> false
190
+ *
191
+ * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
192
+ *
193
+ * @param {String} type Type to check.
194
+ * @param {String} [name] Element name.
195
+ * @returns {Boolean}
196
+ */
197
+ is( type, name = null ) {
198
+ if ( !name ) {
199
+ return type === 'element' || type === 'view:element' ||
200
+ // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
201
+ type === 'node' || type === 'view:node';
202
+ } else {
203
+ return name === this.name && ( type === 'element' || type === 'view:element' );
204
+ }
205
+ }
206
+
207
+ /**
208
+ * Gets child at the given index.
209
+ *
210
+ * @param {Number} index Index of child.
211
+ * @returns {module:engine/view/node~Node} Child node.
212
+ */
213
+ getChild( index ) {
214
+ return this._children[ index ];
215
+ }
216
+
217
+ /**
218
+ * Gets index of the given child node. Returns `-1` if child node is not found.
219
+ *
220
+ * @param {module:engine/view/node~Node} node Child node.
221
+ * @returns {Number} Index of the child node.
222
+ */
223
+ getChildIndex( node ) {
224
+ return this._children.indexOf( node );
225
+ }
226
+
227
+ /**
228
+ * Gets child nodes iterator.
229
+ *
230
+ * @returns {Iterable.<module:engine/view/node~Node>} Child nodes iterator.
231
+ */
232
+ getChildren() {
233
+ return this._children[ Symbol.iterator ]();
234
+ }
235
+
236
+ /**
237
+ * Returns an iterator that contains the keys for attributes. Order of inserting attributes is not preserved.
238
+ *
239
+ * @returns {Iterable.<String>} Keys for attributes.
240
+ */
241
+ * getAttributeKeys() {
242
+ if ( this._classes.size > 0 ) {
243
+ yield 'class';
244
+ }
245
+
246
+ if ( !this._styles.isEmpty ) {
247
+ yield 'style';
248
+ }
249
+
250
+ yield* this._attrs.keys();
251
+ }
252
+
253
+ /**
254
+ * Returns iterator that iterates over this element's attributes.
255
+ *
256
+ * Attributes are returned as arrays containing two items. First one is attribute key and second is attribute value.
257
+ * This format is accepted by native `Map` object and also can be passed in `Node` constructor.
258
+ *
259
+ * @returns {Iterable.<*>}
260
+ */
261
+ * getAttributes() {
262
+ yield* this._attrs.entries();
263
+
264
+ if ( this._classes.size > 0 ) {
265
+ yield [ 'class', this.getAttribute( 'class' ) ];
266
+ }
267
+
268
+ if ( !this._styles.isEmpty ) {
269
+ yield [ 'style', this.getAttribute( 'style' ) ];
270
+ }
271
+ }
272
+
273
+ /**
274
+ * Gets attribute by key. If attribute is not present - returns undefined.
275
+ *
276
+ * @param {String} key Attribute key.
277
+ * @returns {String|undefined} Attribute value.
278
+ */
279
+ getAttribute( key ) {
280
+ if ( key == 'class' ) {
281
+ if ( this._classes.size > 0 ) {
282
+ return [ ...this._classes ].join( ' ' );
283
+ }
284
+
285
+ return undefined;
286
+ }
287
+
288
+ if ( key == 'style' ) {
289
+ const inlineStyle = this._styles.toString();
290
+
291
+ return inlineStyle == '' ? undefined : inlineStyle;
292
+ }
293
+
294
+ return this._attrs.get( key );
295
+ }
296
+
297
+ /**
298
+ * Returns a boolean indicating whether an attribute with the specified key exists in the element.
299
+ *
300
+ * @param {String} key Attribute key.
301
+ * @returns {Boolean} `true` if attribute with the specified key exists in the element, false otherwise.
302
+ */
303
+ hasAttribute( key ) {
304
+ if ( key == 'class' ) {
305
+ return this._classes.size > 0;
306
+ }
307
+
308
+ if ( key == 'style' ) {
309
+ return !this._styles.isEmpty;
310
+ }
311
+
312
+ return this._attrs.has( key );
313
+ }
314
+
315
+ /**
316
+ * Checks if this element is similar to other element.
317
+ * Both elements should have the same name and attributes to be considered as similar. Two similar elements
318
+ * can contain different set of children nodes.
319
+ *
320
+ * @param {module:engine/view/element~Element} otherElement
321
+ * @returns {Boolean}
322
+ */
323
+ isSimilar( otherElement ) {
324
+ if ( !( otherElement instanceof Element ) ) {
325
+ return false;
326
+ }
327
+
328
+ // If exactly the same Element is provided - return true immediately.
329
+ if ( this === otherElement ) {
330
+ return true;
331
+ }
332
+
333
+ // Check element name.
334
+ if ( this.name != otherElement.name ) {
335
+ return false;
336
+ }
337
+
338
+ // Check isAllowedInsideAttributeElement property.
339
+ if ( this.isAllowedInsideAttributeElement != otherElement.isAllowedInsideAttributeElement ) {
340
+ return false;
341
+ }
342
+
343
+ // Check number of attributes, classes and styles.
344
+ if ( this._attrs.size !== otherElement._attrs.size || this._classes.size !== otherElement._classes.size ||
345
+ this._styles.size !== otherElement._styles.size ) {
346
+ return false;
347
+ }
348
+
349
+ // Check if attributes are the same.
350
+ for ( const [ key, value ] of this._attrs ) {
351
+ if ( !otherElement._attrs.has( key ) || otherElement._attrs.get( key ) !== value ) {
352
+ return false;
353
+ }
354
+ }
355
+
356
+ // Check if classes are the same.
357
+ for ( const className of this._classes ) {
358
+ if ( !otherElement._classes.has( className ) ) {
359
+ return false;
360
+ }
361
+ }
362
+
363
+ // Check if styles are the same.
364
+ for ( const property of this._styles.getStyleNames() ) {
365
+ if (
366
+ !otherElement._styles.has( property ) ||
367
+ otherElement._styles.getAsString( property ) !== this._styles.getAsString( property )
368
+ ) {
369
+ return false;
370
+ }
371
+ }
372
+
373
+ return true;
374
+ }
375
+
376
+ /**
377
+ * Returns true if class is present.
378
+ * If more then one class is provided - returns true only when all classes are present.
379
+ *
380
+ * element.hasClass( 'foo' ); // Returns true if 'foo' class is present.
381
+ * element.hasClass( 'foo', 'bar' ); // Returns true if 'foo' and 'bar' classes are both present.
382
+ *
383
+ * @param {...String} className
384
+ */
385
+ hasClass( ...className ) {
386
+ for ( const name of className ) {
387
+ if ( !this._classes.has( name ) ) {
388
+ return false;
389
+ }
390
+ }
391
+
392
+ return true;
393
+ }
394
+
395
+ /**
396
+ * Returns iterator that contains all class names.
397
+ *
398
+ * @returns {Iterable.<String>}
399
+ */
400
+ getClassNames() {
401
+ return this._classes.keys();
402
+ }
403
+
404
+ /**
405
+ * Returns style value for the given property mae.
406
+ * If the style does not exist `undefined` is returned.
407
+ *
408
+ * **Note**: This method can work with normalized style names if
409
+ * {@link module:engine/controller/datacontroller~DataController#addStyleProcessorRules a particular style processor rule is enabled}.
410
+ * See {@link module:engine/view/stylesmap~StylesMap#getAsString `StylesMap#getAsString()`} for details.
411
+ *
412
+ * For an element with style set to `'margin:1px'`:
413
+ *
414
+ * // Enable 'margin' shorthand processing:
415
+ * editor.data.addStyleProcessorRules( addMarginRules );
416
+ *
417
+ * const element = view.change( writer => {
418
+ * const element = writer.createElement();
419
+ * writer.setStyle( 'margin', '1px' );
420
+ * writer.setStyle( 'margin-bottom', '3em' );
421
+ *
422
+ * return element;
423
+ * } );
424
+ *
425
+ * element.getStyle( 'margin' ); // -> 'margin: 1px 1px 3em;'
426
+ *
427
+ * @param {String} property
428
+ * @returns {String|undefined}
429
+ */
430
+ getStyle( property ) {
431
+ return this._styles.getAsString( property );
432
+ }
433
+
434
+ /**
435
+ * Returns a normalized style object or single style value.
436
+ *
437
+ * For an element with style set to: margin:1px 2px 3em;
438
+ *
439
+ * element.getNormalizedStyle( 'margin' ) );
440
+ *
441
+ * will return:
442
+ *
443
+ * {
444
+ * top: '1px',
445
+ * right: '2px',
446
+ * bottom: '3em',
447
+ * left: '2px' // a normalized value from margin shorthand
448
+ * }
449
+ *
450
+ * and reading for single style value:
451
+ *
452
+ * styles.getNormalizedStyle( 'margin-left' );
453
+ *
454
+ * Will return a `2px` string.
455
+ *
456
+ * **Note**: This method will return normalized values only if
457
+ * {@link module:engine/controller/datacontroller~DataController#addStyleProcessorRules a particular style processor rule is enabled}.
458
+ * See {@link module:engine/view/stylesmap~StylesMap#getNormalized `StylesMap#getNormalized()`} for details.
459
+ *
460
+ *
461
+ * @param {String} property Name of CSS property
462
+ * @returns {Object|String|undefined}
463
+ */
464
+ getNormalizedStyle( property ) {
465
+ return this._styles.getNormalized( property );
466
+ }
467
+
468
+ /**
469
+ * Returns iterator that contains all style names.
470
+ *
471
+ * @param {Boolean} [expand=false] Expand shorthand style properties and return all equivalent style representations.
472
+ * @returns {Iterable.<String>}
473
+ */
474
+ getStyleNames( expand = false ) {
475
+ return this._styles.getStyleNames( expand );
476
+ }
477
+
478
+ /**
479
+ * Returns true if style keys are present.
480
+ * If more then one style property is provided - returns true only when all properties are present.
481
+ *
482
+ * element.hasStyle( 'color' ); // Returns true if 'border-top' style is present.
483
+ * element.hasStyle( 'color', 'border-top' ); // Returns true if 'color' and 'border-top' styles are both present.
484
+ *
485
+ * @param {...String} property
486
+ */
487
+ hasStyle( ...property ) {
488
+ for ( const name of property ) {
489
+ if ( !this._styles.has( name ) ) {
490
+ return false;
491
+ }
492
+ }
493
+
494
+ return true;
495
+ }
496
+
497
+ /**
498
+ * Returns ancestor element that match specified pattern.
499
+ * Provided patterns should be compatible with {@link module:engine/view/matcher~Matcher Matcher} as it is used internally.
500
+ *
501
+ * @see module:engine/view/matcher~Matcher
502
+ * @param {Object|String|RegExp|Function} patterns Patterns used to match correct ancestor.
503
+ * See {@link module:engine/view/matcher~Matcher}.
504
+ * @returns {module:engine/view/element~Element|null} Found element or `null` if no matching ancestor was found.
505
+ */
506
+ findAncestor( ...patterns ) {
507
+ const matcher = new Matcher( ...patterns );
508
+ let parent = this.parent;
509
+
510
+ while ( parent ) {
511
+ if ( matcher.match( parent ) ) {
512
+ return parent;
513
+ }
514
+
515
+ parent = parent.parent;
516
+ }
517
+
518
+ return null;
519
+ }
520
+
521
+ /**
522
+ * Returns the custom property value for the given key.
523
+ *
524
+ * @param {String|Symbol} key
525
+ * @returns {*}
526
+ */
527
+ getCustomProperty( key ) {
528
+ return this._customProperties.get( key );
529
+ }
530
+
531
+ /**
532
+ * Returns an iterator which iterates over this element's custom properties.
533
+ * Iterator provides `[ key, value ]` pairs for each stored property.
534
+ *
535
+ * @returns {Iterable.<*>}
536
+ */
537
+ * getCustomProperties() {
538
+ yield* this._customProperties.entries();
539
+ }
540
+
541
+ /**
542
+ * Returns identity string based on element's name, styles, classes and other attributes.
543
+ * Two elements that {@link #isSimilar are similar} will have same identity string.
544
+ * It has the following format:
545
+ *
546
+ * 'name class="class1,class2" style="style1:value1;style2:value2" attr1="val1" attr2="val2"'
547
+ *
548
+ * For example:
549
+ *
550
+ * const element = writer.createContainerElement( 'foo', {
551
+ * banana: '10',
552
+ * apple: '20',
553
+ * style: 'color: red; border-color: white;',
554
+ * class: 'baz'
555
+ * } );
556
+ *
557
+ * // returns 'foo class="baz" style="border-color:white;color:red" apple="20" banana="10"'
558
+ * element.getIdentity();
559
+ *
560
+ * **Note**: Classes, styles and other attributes are sorted alphabetically.
561
+ *
562
+ * @returns {String}
563
+ */
564
+ getIdentity() {
565
+ const classes = Array.from( this._classes ).sort().join( ',' );
566
+ const styles = this._styles.toString();
567
+ const attributes = Array.from( this._attrs ).map( i => `${ i[ 0 ] }="${ i[ 1 ] }"` ).sort().join( ' ' );
568
+
569
+ return this.name +
570
+ ( classes == '' ? '' : ` class="${ classes }"` ) +
571
+ ( !styles ? '' : ` style="${ styles }"` ) +
572
+ ( attributes == '' ? '' : ` ${ attributes }` );
573
+ }
574
+
575
+ /**
576
+ * Clones provided element.
577
+ *
578
+ * @protected
579
+ * @param {Boolean} [deep=false] If set to `true` clones element and all its children recursively. When set to `false`,
580
+ * element will be cloned without any children.
581
+ * @returns {module:engine/view/element~Element} Clone of this element.
582
+ */
583
+ _clone( deep = false ) {
584
+ const childrenClone = [];
585
+
586
+ if ( deep ) {
587
+ for ( const child of this.getChildren() ) {
588
+ childrenClone.push( child._clone( deep ) );
589
+ }
590
+ }
591
+
592
+ // ContainerElement and AttributeElement should be also cloned properly.
593
+ const cloned = new this.constructor( this.document, this.name, this._attrs, childrenClone );
594
+
595
+ // Classes and styles are cloned separately - this solution is faster than adding them back to attributes and
596
+ // parse once again in constructor.
597
+ cloned._classes = new Set( this._classes );
598
+ cloned._styles.set( this._styles.getNormalized() );
599
+
600
+ // Clone custom properties.
601
+ cloned._customProperties = new Map( this._customProperties );
602
+
603
+ // Clone filler offset method.
604
+ // We can't define this method in a prototype because it's behavior which
605
+ // is changed by e.g. toWidget() function from ckeditor5-widget. Perhaps this should be one of custom props.
606
+ cloned.getFillerOffset = this.getFillerOffset;
607
+
608
+ cloned._isAllowedInsideAttributeElement = this.isAllowedInsideAttributeElement;
609
+
610
+ return cloned;
611
+ }
612
+
613
+ /**
614
+ * {@link module:engine/view/element~Element#_insertChild Insert} a child node or a list of child nodes at the end of this node
615
+ * and sets the parent of these nodes to this element.
616
+ *
617
+ * @see module:engine/view/downcastwriter~DowncastWriter#insert
618
+ * @protected
619
+ * @param {module:engine/view/item~Item|Iterable.<module:engine/view/item~Item>} items Items to be inserted.
620
+ * @fires module:engine/view/node~Node#change
621
+ * @returns {Number} Number of appended nodes.
622
+ */
623
+ _appendChild( items ) {
624
+ return this._insertChild( this.childCount, items );
625
+ }
626
+
627
+ /**
628
+ * Inserts a child node or a list of child nodes on the given index and sets the parent of these nodes to
629
+ * this element.
630
+ *
631
+ * @see module:engine/view/downcastwriter~DowncastWriter#insert
632
+ * @protected
633
+ * @param {Number} index Position where nodes should be inserted.
634
+ * @param {module:engine/view/item~Item|Iterable.<module:engine/view/item~Item>} items Items to be inserted.
635
+ * @fires module:engine/view/node~Node#change
636
+ * @returns {Number} Number of inserted nodes.
637
+ */
638
+ _insertChild( index, items ) {
639
+ this._fireChange( 'children', this );
640
+ let count = 0;
641
+
642
+ const nodes = normalize( this.document, items );
643
+
644
+ for ( const node of nodes ) {
645
+ // If node that is being added to this element is already inside another element, first remove it from the old parent.
646
+ if ( node.parent !== null ) {
647
+ node._remove();
648
+ }
649
+
650
+ node.parent = this;
651
+ node.document = this.document;
652
+
653
+ this._children.splice( index, 0, node );
654
+ index++;
655
+ count++;
656
+ }
657
+
658
+ return count;
659
+ }
660
+
661
+ /**
662
+ * Removes number of child nodes starting at the given index and set the parent of these nodes to `null`.
663
+ *
664
+ * @see module:engine/view/downcastwriter~DowncastWriter#remove
665
+ * @protected
666
+ * @param {Number} index Number of the first node to remove.
667
+ * @param {Number} [howMany=1] Number of nodes to remove.
668
+ * @fires module:engine/view/node~Node#change
669
+ * @returns {Array.<module:engine/view/node~Node>} The array of removed nodes.
670
+ */
671
+ _removeChildren( index, howMany = 1 ) {
672
+ this._fireChange( 'children', this );
673
+
674
+ for ( let i = index; i < index + howMany; i++ ) {
675
+ this._children[ i ].parent = null;
676
+ }
677
+
678
+ return this._children.splice( index, howMany );
679
+ }
680
+
681
+ /**
682
+ * Adds or overwrite attribute with a specified key and value.
683
+ *
684
+ * @see module:engine/view/downcastwriter~DowncastWriter#setAttribute
685
+ * @protected
686
+ * @param {String} key Attribute key.
687
+ * @param {String} value Attribute value.
688
+ * @fires module:engine/view/node~Node#change
689
+ */
690
+ _setAttribute( key, value ) {
691
+ value = String( value );
692
+
693
+ this._fireChange( 'attributes', this );
694
+
695
+ if ( key == 'class' ) {
696
+ parseClasses( this._classes, value );
697
+ } else if ( key == 'style' ) {
698
+ this._styles.setTo( value );
699
+ } else {
700
+ this._attrs.set( key, value );
701
+ }
702
+ }
703
+
704
+ /**
705
+ * Removes attribute from the element.
706
+ *
707
+ * @see module:engine/view/downcastwriter~DowncastWriter#removeAttribute
708
+ * @protected
709
+ * @param {String} key Attribute key.
710
+ * @returns {Boolean} Returns true if an attribute existed and has been removed.
711
+ * @fires module:engine/view/node~Node#change
712
+ */
713
+ _removeAttribute( key ) {
714
+ this._fireChange( 'attributes', this );
715
+
716
+ // Remove class attribute.
717
+ if ( key == 'class' ) {
718
+ if ( this._classes.size > 0 ) {
719
+ this._classes.clear();
720
+
721
+ return true;
722
+ }
723
+
724
+ return false;
725
+ }
726
+
727
+ // Remove style attribute.
728
+ if ( key == 'style' ) {
729
+ if ( !this._styles.isEmpty ) {
730
+ this._styles.clear();
731
+
732
+ return true;
733
+ }
734
+
735
+ return false;
736
+ }
737
+
738
+ // Remove other attributes.
739
+ return this._attrs.delete( key );
740
+ }
741
+
742
+ /**
743
+ * Adds specified class.
744
+ *
745
+ * element._addClass( 'foo' ); // Adds 'foo' class.
746
+ * element._addClass( [ 'foo', 'bar' ] ); // Adds 'foo' and 'bar' classes.
747
+ *
748
+ * @see module:engine/view/downcastwriter~DowncastWriter#addClass
749
+ * @protected
750
+ * @param {Array.<String>|String} className
751
+ * @fires module:engine/view/node~Node#change
752
+ */
753
+ _addClass( className ) {
754
+ this._fireChange( 'attributes', this );
755
+
756
+ for ( const name of toArray( className ) ) {
757
+ this._classes.add( name );
758
+ }
759
+ }
760
+
761
+ /**
762
+ * Removes specified class.
763
+ *
764
+ * element._removeClass( 'foo' ); // Removes 'foo' class.
765
+ * element._removeClass( [ 'foo', 'bar' ] ); // Removes both 'foo' and 'bar' classes.
766
+ *
767
+ * @see module:engine/view/downcastwriter~DowncastWriter#removeClass
768
+ * @protected
769
+ * @param {Array.<String>|String} className
770
+ * @fires module:engine/view/node~Node#change
771
+ */
772
+ _removeClass( className ) {
773
+ this._fireChange( 'attributes', this );
774
+
775
+ for ( const name of toArray( className ) ) {
776
+ this._classes.delete( name );
777
+ }
778
+ }
779
+
780
+ /**
781
+ * Adds style to the element.
782
+ *
783
+ * element._setStyle( 'color', 'red' );
784
+ * element._setStyle( {
785
+ * color: 'red',
786
+ * position: 'fixed'
787
+ * } );
788
+ *
789
+ * **Note**: This method can work with normalized style names if
790
+ * {@link module:engine/controller/datacontroller~DataController#addStyleProcessorRules a particular style processor rule is enabled}.
791
+ * See {@link module:engine/view/stylesmap~StylesMap#set `StylesMap#set()`} for details.
792
+ *
793
+ * @see module:engine/view/downcastwriter~DowncastWriter#setStyle
794
+ * @protected
795
+ * @param {String|Object} property Property name or object with key - value pairs.
796
+ * @param {String} [value] Value to set. This parameter is ignored if object is provided as the first parameter.
797
+ * @fires module:engine/view/node~Node#change
798
+ */
799
+ _setStyle( property, value ) {
800
+ this._fireChange( 'attributes', this );
801
+
802
+ this._styles.set( property, value );
803
+ }
804
+
805
+ /**
806
+ * Removes specified style.
807
+ *
808
+ * element._removeStyle( 'color' ); // Removes 'color' style.
809
+ * element._removeStyle( [ 'color', 'border-top' ] ); // Removes both 'color' and 'border-top' styles.
810
+ *
811
+ * **Note**: This method can work with normalized style names if
812
+ * {@link module:engine/controller/datacontroller~DataController#addStyleProcessorRules a particular style processor rule is enabled}.
813
+ * See {@link module:engine/view/stylesmap~StylesMap#remove `StylesMap#remove()`} for details.
814
+ *
815
+ * @see module:engine/view/downcastwriter~DowncastWriter#removeStyle
816
+ * @protected
817
+ * @param {Array.<String>|String} property
818
+ * @fires module:engine/view/node~Node#change
819
+ */
820
+ _removeStyle( property ) {
821
+ this._fireChange( 'attributes', this );
822
+
823
+ for ( const name of toArray( property ) ) {
824
+ this._styles.remove( name );
825
+ }
826
+ }
827
+
828
+ /**
829
+ * Sets a custom property. Unlike attributes, custom properties are not rendered to the DOM,
830
+ * so they can be used to add special data to elements.
831
+ *
832
+ * @see module:engine/view/downcastwriter~DowncastWriter#setCustomProperty
833
+ * @protected
834
+ * @param {String|Symbol} key
835
+ * @param {*} value
836
+ */
837
+ _setCustomProperty( key, value ) {
838
+ this._customProperties.set( key, value );
839
+ }
840
+
841
+ /**
842
+ * Removes the custom property stored under the given key.
843
+ *
844
+ * @see module:engine/view/downcastwriter~DowncastWriter#removeCustomProperty
845
+ * @protected
846
+ * @param {String|Symbol} key
847
+ * @returns {Boolean} Returns true if property was removed.
848
+ */
849
+ _removeCustomProperty( key ) {
850
+ return this._customProperties.delete( key );
851
+ }
852
+
853
+ /**
854
+ * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
855
+ *
856
+ * @abstract
857
+ * @method module:engine/view/element~Element#getFillerOffset
858
+ */
859
+
860
+ // @if CK_DEBUG_ENGINE // printTree( level = 0) {
861
+ // @if CK_DEBUG_ENGINE // let string = '';
862
+
863
+ // @if CK_DEBUG_ENGINE // string += '\t'.repeat( level ) + `<${ this.name }${ convertMapToTags( this.getAttributes() ) }>`;
864
+
865
+ // @if CK_DEBUG_ENGINE // for ( const child of this.getChildren() ) {
866
+ // @if CK_DEBUG_ENGINE // if ( child.is( '$text' ) ) {
867
+ // @if CK_DEBUG_ENGINE // string += '\n' + '\t'.repeat( level + 1 ) + child.data;
868
+ // @if CK_DEBUG_ENGINE // } else {
869
+ // @if CK_DEBUG_ENGINE // string += '\n' + child.printTree( level + 1 );
870
+ // @if CK_DEBUG_ENGINE // }
871
+ // @if CK_DEBUG_ENGINE // }
872
+
873
+ // @if CK_DEBUG_ENGINE // if ( this.childCount ) {
874
+ // @if CK_DEBUG_ENGINE // string += '\n' + '\t'.repeat( level );
875
+ // @if CK_DEBUG_ENGINE // }
876
+
877
+ // @if CK_DEBUG_ENGINE // string += `</${ this.name }>`;
878
+
879
+ // @if CK_DEBUG_ENGINE // return string;
880
+ // @if CK_DEBUG_ENGINE // }
881
+
882
+ // @if CK_DEBUG_ENGINE // logTree() {
883
+ // @if CK_DEBUG_ENGINE // console.log( this.printTree() );
884
+ // @if CK_DEBUG_ENGINE // }
885
+ }
886
+
887
+ // Parses attributes provided to the element constructor before they are applied to an element. If attributes are passed
888
+ // as an object (instead of `Iterable`), the object is transformed to the map. Attributes with `null` value are removed.
889
+ // Attributes with non-`String` value are converted to `String`.
890
+ //
891
+ // @param {Object|Iterable} attrs Attributes to parse.
892
+ // @returns {Map} Parsed attributes.
893
+ function parseAttributes( attrs ) {
894
+ attrs = toMap( attrs );
895
+
896
+ for ( const [ key, value ] of attrs ) {
897
+ if ( value === null ) {
898
+ attrs.delete( key );
899
+ } else if ( typeof value != 'string' ) {
900
+ attrs.set( key, String( value ) );
901
+ }
902
+ }
903
+
904
+ return attrs;
905
+ }
906
+
907
+ // Parses class attribute and puts all classes into classes set.
908
+ // Classes set s cleared before insertion.
909
+ //
910
+ // @param {Set.<String>} classesSet Set to insert parsed classes.
911
+ // @param {String} classesString String with classes to parse.
912
+ function parseClasses( classesSet, classesString ) {
913
+ const classArray = classesString.split( /\s+/ );
914
+ classesSet.clear();
915
+ classArray.forEach( name => classesSet.add( name ) );
916
+ }
917
+
918
+ // Converts strings to Text and non-iterables to arrays.
919
+ //
920
+ // @param {String|module:engine/view/item~Item|Iterable.<String|module:engine/view/item~Item>}
921
+ // @returns {Iterable.<module:engine/view/node~Node>}
922
+ function normalize( document, nodes ) {
923
+ // Separate condition because string is iterable.
924
+ if ( typeof nodes == 'string' ) {
925
+ return [ new Text( document, nodes ) ];
926
+ }
927
+
928
+ if ( !isIterable( nodes ) ) {
929
+ nodes = [ nodes ];
930
+ }
931
+
932
+ // Array.from to enable .map() on non-arrays.
933
+ return Array.from( nodes )
934
+ .map( node => {
935
+ if ( typeof node == 'string' ) {
936
+ return new Text( document, node );
937
+ }
938
+
939
+ if ( node instanceof TextProxy ) {
940
+ return new Text( document, node.data );
941
+ }
942
+
943
+ return node;
944
+ } );
945
+ }