@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,274 @@
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/attributeelement
8
+ */
9
+
10
+ import Element from './element';
11
+ import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
12
+
13
+ // Default attribute priority.
14
+ const DEFAULT_PRIORITY = 10;
15
+
16
+ /**
17
+ * Attribute elements are used to represent formatting elements in the view (think – `<b>`, `<span style="font-size: 2em">`, etc.).
18
+ * Most often they are created when downcasting model text attributes.
19
+ *
20
+ * Editing engine does not define a fixed HTML DTD. This is why a feature developer needs to choose between various
21
+ * types (container element, {@link module:engine/view/attributeelement~AttributeElement attribute element},
22
+ * {@link module:engine/view/emptyelement~EmptyElement empty element}, etc) when developing a feature.
23
+ *
24
+ * To create a new attribute element instance use the
25
+ * {@link module:engine/view/downcastwriter~DowncastWriter#createAttributeElement `DowncastWriter#createAttributeElement()`} method.
26
+ *
27
+ * **Note:** Attribute elements by default can wrap {@link module:engine/view/text~Text},
28
+ * {@link module:engine/view/emptyelement~EmptyElement}, {@link module:engine/view/uielement~UIElement},
29
+ * {@link module:engine/view/rawelement~RawElement} and other attribute elements with higher priority. Other elements while placed inside
30
+ * an attribute element will split it (or nest in case of an `AttributeElement`). This behavior can be modified by changing
31
+ * the `isAllowedInsideAttributeElement` option while creating
32
+ * {@link module:engine/view/downcastwriter~DowncastWriter#createContainerElement},
33
+ * {@link module:engine/view/downcastwriter~DowncastWriter#createEmptyElement},
34
+ * {@link module:engine/view/downcastwriter~DowncastWriter#createUIElement} or
35
+ * {@link module:engine/view/downcastwriter~DowncastWriter#createRawElement}.
36
+ *
37
+ * @extends module:engine/view/element~Element
38
+ */
39
+ export default class AttributeElement extends Element {
40
+ /**
41
+ * Creates an attribute element.
42
+ *
43
+ * @see module:engine/view/downcastwriter~DowncastWriter#createAttributeElement
44
+ * @see module:engine/view/element~Element
45
+ * @protected
46
+ * @param {module:engine/view/document~Document} document The document instance to which this element belongs.
47
+ * @param {String} name Node name.
48
+ * @param {Object|Iterable} [attrs] Collection of attributes.
49
+ * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
50
+ * A list of nodes to be inserted into created element.
51
+ */
52
+ constructor( document, name, attrs, children ) {
53
+ super( document, name, attrs, children );
54
+
55
+ /**
56
+ * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
57
+ *
58
+ * @method #getFillerOffset
59
+ * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
60
+ */
61
+ this.getFillerOffset = getFillerOffset;
62
+
63
+ /**
64
+ * Element priority. Decides in what order elements are wrapped by {@link module:engine/view/downcastwriter~DowncastWriter}.
65
+ *
66
+ * @protected
67
+ * @member {Number}
68
+ */
69
+ this._priority = DEFAULT_PRIORITY;
70
+
71
+ /**
72
+ * Element identifier. If set, it is used by {@link module:engine/view/element~Element#isSimilar},
73
+ * and then two elements are considered similar if, and only if they have the same `_id`.
74
+ *
75
+ * @protected
76
+ * @member {String|Number}
77
+ */
78
+ this._id = null;
79
+
80
+ /**
81
+ * Keeps all the attribute elements that have the same {@link module:engine/view/attributeelement~AttributeElement#id ids}
82
+ * and still exist in the view tree.
83
+ *
84
+ * This property is managed by {@link module:engine/view/downcastwriter~DowncastWriter}.
85
+ *
86
+ * @protected
87
+ * @member {Set.<module:engine/view/attributeelement~AttributeElement>|null}
88
+ */
89
+ this._clonesGroup = null;
90
+ }
91
+
92
+ /**
93
+ * Element priority. Decides in what order elements are wrapped by {@link module:engine/view/downcastwriter~DowncastWriter}.
94
+ *
95
+ * @readonly
96
+ * @type {Number}
97
+ */
98
+ get priority() {
99
+ return this._priority;
100
+ }
101
+
102
+ /**
103
+ * Element identifier. If set, it is used by {@link module:engine/view/element~Element#isSimilar},
104
+ * and then two elements are considered similar if, and only if they have the same `id`.
105
+ *
106
+ * @readonly
107
+ * @type {String|Number}
108
+ */
109
+ get id() {
110
+ return this._id;
111
+ }
112
+
113
+ /**
114
+ * Returns all {@link module:engine/view/attributeelement~AttributeElement attribute elements} that has the
115
+ * same {@link module:engine/view/attributeelement~AttributeElement#id id} and are in the view tree (were not removed).
116
+ *
117
+ * Note: If this element has been removed from the tree, returned set will not include it.
118
+ *
119
+ * Throws {@link module:utils/ckeditorerror~CKEditorError attribute-element-get-elements-with-same-id-no-id}
120
+ * if this element has no `id`.
121
+ *
122
+ * @returns {Set.<module:engine/view/attributeelement~AttributeElement>} Set containing all the attribute elements
123
+ * with the same `id` that were added and not removed from the view tree.
124
+ */
125
+ getElementsWithSameId() {
126
+ if ( this.id === null ) {
127
+ /**
128
+ * Cannot get elements with the same id for an attribute element without id.
129
+ *
130
+ * @error attribute-element-get-elements-with-same-id-no-id
131
+ */
132
+ throw new CKEditorError(
133
+ 'attribute-element-get-elements-with-same-id-no-id',
134
+ this
135
+ );
136
+ }
137
+
138
+ return new Set( this._clonesGroup );
139
+ }
140
+
141
+ /**
142
+ * Checks whether this object is of the given.
143
+ *
144
+ * attributeElement.is( 'attributeElement' ); // -> true
145
+ * attributeElement.is( 'element' ); // -> true
146
+ * attributeElement.is( 'node' ); // -> true
147
+ * attributeElement.is( 'view:attributeElement' ); // -> true
148
+ * attributeElement.is( 'view:element' ); // -> true
149
+ * attributeElement.is( 'view:node' ); // -> true
150
+ *
151
+ * attributeElement.is( 'model:element' ); // -> false
152
+ * attributeElement.is( 'documentFragment' ); // -> false
153
+ *
154
+ * Assuming that the object being checked is an attribute element, you can also check its
155
+ * {@link module:engine/view/attributeelement~AttributeElement#name name}:
156
+ *
157
+ * attributeElement.is( 'element', 'b' ); // -> true if this is a bold element
158
+ * attributeElement.is( 'attributeElement', 'b' ); // -> same as above
159
+ * text.is( 'element', 'b' ); -> false
160
+ *
161
+ * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
162
+ *
163
+ * @param {String} type Type to check.
164
+ * @param {String} [name] Element name.
165
+ * @returns {Boolean}
166
+ */
167
+ is( type, name = null ) {
168
+ if ( !name ) {
169
+ return type === 'attributeElement' || type === 'view:attributeElement' ||
170
+ // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
171
+ type === 'element' || type === 'view:element' ||
172
+ type === 'node' || type === 'view:node';
173
+ } else {
174
+ return name === this.name && (
175
+ type === 'attributeElement' || type === 'view:attributeElement' ||
176
+ // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
177
+ type === 'element' || type === 'view:element'
178
+ );
179
+ }
180
+ }
181
+
182
+ /**
183
+ * Checks if this element is similar to other element.
184
+ *
185
+ * If none of elements has set {@link module:engine/view/attributeelement~AttributeElement#id}, then both elements
186
+ * should have the same name, attributes and priority to be considered as similar. Two similar elements can contain
187
+ * different set of children nodes.
188
+ *
189
+ * If at least one element has {@link module:engine/view/attributeelement~AttributeElement#id} set, then both
190
+ * elements have to have the same {@link module:engine/view/attributeelement~AttributeElement#id} value to be
191
+ * considered similar.
192
+ *
193
+ * Similarity is important for {@link module:engine/view/downcastwriter~DowncastWriter}. For example:
194
+ *
195
+ * * two following similar elements can be merged together into one, longer element,
196
+ * * {@link module:engine/view/downcastwriter~DowncastWriter#unwrap} checks similarity of passed element and processed element to
197
+ * decide whether processed element should be unwrapped,
198
+ * * etc.
199
+ *
200
+ * @param {module:engine/view/element~Element} otherElement
201
+ * @returns {Boolean}
202
+ */
203
+ isSimilar( otherElement ) {
204
+ // If any element has an `id` set, just compare the ids.
205
+ if ( this.id !== null || otherElement.id !== null ) {
206
+ return this.id === otherElement.id;
207
+ }
208
+
209
+ return super.isSimilar( otherElement ) && this.priority == otherElement.priority;
210
+ }
211
+
212
+ /**
213
+ * Clones provided element with priority.
214
+ *
215
+ * @protected
216
+ * @param {Boolean} deep If set to `true` clones element and all its children recursively. When set to `false`,
217
+ * element will be cloned without any children.
218
+ * @returns {module:engine/view/attributeelement~AttributeElement} Clone of this element.
219
+ */
220
+ _clone( deep ) {
221
+ const cloned = super._clone( deep );
222
+
223
+ // Clone priority too.
224
+ cloned._priority = this._priority;
225
+
226
+ // And id too.
227
+ cloned._id = this._id;
228
+
229
+ return cloned;
230
+ }
231
+ }
232
+
233
+ /**
234
+ * Default attribute priority.
235
+ *
236
+ * @member {Number} module:engine/view/attributeelement~AttributeElement.DEFAULT_PRIORITY
237
+ */
238
+ AttributeElement.DEFAULT_PRIORITY = DEFAULT_PRIORITY;
239
+
240
+ // Returns block {@link module:engine/view/filler~Filler filler} offset or `null` if block filler is not needed.
241
+ //
242
+ // @returns {Number|null} Block filler offset or `null` if block filler is not needed.
243
+ function getFillerOffset() {
244
+ // <b>foo</b> does not need filler.
245
+ if ( nonUiChildrenCount( this ) ) {
246
+ return null;
247
+ }
248
+
249
+ let element = this.parent;
250
+
251
+ // <p><b></b></p> needs filler -> <p><b><br></b></p>
252
+ while ( element && element.is( 'attributeElement' ) ) {
253
+ if ( nonUiChildrenCount( element ) > 1 ) {
254
+ return null;
255
+ }
256
+
257
+ element = element.parent;
258
+ }
259
+
260
+ if ( !element || nonUiChildrenCount( element ) > 1 ) {
261
+ return null;
262
+ }
263
+
264
+ // Render block filler at the end of element (after all ui elements).
265
+ return this.childCount;
266
+ }
267
+
268
+ // Returns total count of children that are not {@link module:engine/view/uielement~UIElement UIElements}.
269
+ //
270
+ // @param {module:engine/view/element~Element} element
271
+ // @returns {Number}
272
+ function nonUiChildrenCount( element ) {
273
+ return Array.from( element.getChildren() ).filter( element => !element.is( 'uiElement' ) ).length;
274
+ }
@@ -0,0 +1,123 @@
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/containerelement
8
+ */
9
+
10
+ import Element from './element';
11
+
12
+ /**
13
+ * Containers are elements which define document structure. They define boundaries for
14
+ * {@link module:engine/view/attributeelement~AttributeElement attributes}. They are mostly used for block elements like `<p>` or `<div>`.
15
+ *
16
+ * Editing engine does not define a fixed HTML DTD. This is why a feature developer needs to choose between various
17
+ * types (container element, {@link module:engine/view/attributeelement~AttributeElement attribute element},
18
+ * {@link module:engine/view/emptyelement~EmptyElement empty element}, etc) when developing a feature.
19
+ *
20
+ * The container element should be your default choice when writing a converter, unless:
21
+ *
22
+ * * this element represents a model text attribute (then use {@link module:engine/view/attributeelement~AttributeElement}),
23
+ * * this is an empty element like `<img>` (then use {@link module:engine/view/emptyelement~EmptyElement}),
24
+ * * this is a root element,
25
+ * * this is a nested editable element (then use {@link module:engine/view/editableelement~EditableElement}).
26
+ *
27
+ * To create a new container element instance use the
28
+ * {@link module:engine/view/downcastwriter~DowncastWriter#createContainerElement `DowncastWriter#createContainerElement()`}
29
+ * method.
30
+ *
31
+ * @extends module:engine/view/element~Element
32
+ */
33
+ export default class ContainerElement extends Element {
34
+ /**
35
+ * Creates a container element.
36
+ *
37
+ * @see module:engine/view/downcastwriter~DowncastWriter#createContainerElement
38
+ * @see module:engine/view/element~Element
39
+ * @protected
40
+ * @param {module:engine/view/document~Document} document The document instance to which this element belongs.
41
+ * @param {String} name Node name.
42
+ * @param {Object|Iterable} [attrs] Collection of attributes.
43
+ * @param {module:engine/view/node~Node|Iterable.<module:engine/view/node~Node>} [children]
44
+ * A list of nodes to be inserted into created element.
45
+ */
46
+ constructor( document, name, attrs, children ) {
47
+ super( document, name, attrs, children );
48
+
49
+ /**
50
+ * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
51
+ *
52
+ * @method #getFillerOffset
53
+ * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
54
+ */
55
+ this.getFillerOffset = getFillerOffset;
56
+ }
57
+
58
+ /**
59
+ * Checks whether this object is of the given.
60
+ *
61
+ * containerElement.is( 'containerElement' ); // -> true
62
+ * containerElement.is( 'element' ); // -> true
63
+ * containerElement.is( 'node' ); // -> true
64
+ * containerElement.is( 'view:containerElement' ); // -> true
65
+ * containerElement.is( 'view:element' ); // -> true
66
+ * containerElement.is( 'view:node' ); // -> true
67
+ *
68
+ * containerElement.is( 'model:element' ); // -> false
69
+ * containerElement.is( 'documentFragment' ); // -> false
70
+ *
71
+ * Assuming that the object being checked is a container element, you can also check its
72
+ * {@link module:engine/view/containerelement~ContainerElement#name name}:
73
+ *
74
+ * containerElement.is( 'element', 'div' ); // -> true if this is a div container element
75
+ * containerElement.is( 'contaienrElement', 'div' ); // -> same as above
76
+ * text.is( 'element', 'div' ); -> false
77
+ *
78
+ * {@link module:engine/view/node~Node#is Check the entire list of view objects} which implement the `is()` method.
79
+ *
80
+ * @param {String} type Type to check.
81
+ * @param {String} [name] Element name.
82
+ * @returns {Boolean}
83
+ */
84
+ is( type, name = null ) {
85
+ if ( !name ) {
86
+ return type === 'containerElement' || type === 'view:containerElement' ||
87
+ // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
88
+ type === 'element' || type === 'view:element' ||
89
+ type === 'node' || type === 'view:node';
90
+ } else {
91
+ return name === this.name && (
92
+ type === 'containerElement' || type === 'view:containerElement' ||
93
+ // From super.is(). This is highly utilised method and cannot call super. See ckeditor/ckeditor5#6529.
94
+ type === 'element' || type === 'view:element'
95
+ );
96
+ }
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Returns block {@link module:engine/view/filler filler} offset or `null` if block filler is not needed.
102
+ *
103
+ * @returns {Number|null} Block filler offset or `null` if block filler is not needed.
104
+ */
105
+ export function getFillerOffset() {
106
+ const children = [ ...this.getChildren() ];
107
+ const lastChild = children[ this.childCount - 1 ];
108
+
109
+ // Block filler is required after a `<br>` if it's the last element in its container. See #1422.
110
+ if ( lastChild && lastChild.is( 'element', 'br' ) ) {
111
+ return this.childCount;
112
+ }
113
+
114
+ for ( const child of children ) {
115
+ // If there's any non-UI element – don't render the bogus.
116
+ if ( !child.is( 'uiElement' ) ) {
117
+ return null;
118
+ }
119
+ }
120
+
121
+ // If there are only UI elements – render the bogus at the end of the element.
122
+ return this.childCount;
123
+ }
@@ -0,0 +1,221 @@
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/document
8
+ */
9
+
10
+ import DocumentSelection from './documentselection';
11
+ import Collection from '@ckeditor/ckeditor5-utils/src/collection';
12
+ import mix from '@ckeditor/ckeditor5-utils/src/mix';
13
+ import BubblingEmitterMixin from './observer/bubblingemittermixin';
14
+ import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
15
+
16
+ // @if CK_DEBUG_ENGINE // const { logDocument } = require( '../dev-utils/utils' );
17
+
18
+ /**
19
+ * Document class creates an abstract layer over the content editable area, contains a tree of view elements and
20
+ * {@link module:engine/view/documentselection~DocumentSelection view selection} associated with this document.
21
+ *
22
+ * @mixes module:engine/view/observer/bubblingemittermixin~BubblingEmitterMixin
23
+ * @mixes module:utils/observablemixin~ObservableMixin
24
+ */
25
+ export default class Document {
26
+ /**
27
+ * Creates a Document instance.
28
+ *
29
+ * @param {module:engine/view/stylesmap~StylesProcessor} stylesProcessor The styles processor instance.
30
+ */
31
+ constructor( stylesProcessor ) {
32
+ /**
33
+ * Selection done on this document.
34
+ *
35
+ * @readonly
36
+ * @member {module:engine/view/documentselection~DocumentSelection} module:engine/view/document~Document#selection
37
+ */
38
+ this.selection = new DocumentSelection();
39
+
40
+ /**
41
+ * Roots of the view tree. Collection of the {@link module:engine/view/element~Element view elements}.
42
+ *
43
+ * View roots are created as a result of binding between {@link module:engine/view/document~Document#roots} and
44
+ * {@link module:engine/model/document~Document#roots} and this is handled by
45
+ * {@link module:engine/controller/editingcontroller~EditingController}, so to create view root we need to create
46
+ * model root using {@link module:engine/model/document~Document#createRoot}.
47
+ *
48
+ * @readonly
49
+ * @member {module:utils/collection~Collection} module:engine/view/document~Document#roots
50
+ */
51
+ this.roots = new Collection( { idProperty: 'rootName' } );
52
+
53
+ /**
54
+ * The styles processor instance used by this document when normalizing styles.
55
+ *
56
+ * @readonly
57
+ * @member {module:engine/view/stylesmap~StylesProcessor}
58
+ */
59
+ this.stylesProcessor = stylesProcessor;
60
+
61
+ /**
62
+ * Defines whether document is in read-only mode.
63
+ *
64
+ * When document is read-ony then all roots are read-only as well and caret placed inside this root is hidden.
65
+ *
66
+ * @observable
67
+ * @member {Boolean} #isReadOnly
68
+ */
69
+ this.set( 'isReadOnly', false );
70
+
71
+ /**
72
+ * True if document is focused.
73
+ *
74
+ * This property is updated by the {@link module:engine/view/observer/focusobserver~FocusObserver}.
75
+ * If the {@link module:engine/view/observer/focusobserver~FocusObserver} is disabled this property will not change.
76
+ *
77
+ * @readonly
78
+ * @observable
79
+ * @member {Boolean} module:engine/view/document~Document#isFocused
80
+ */
81
+ this.set( 'isFocused', false );
82
+
83
+ /**
84
+ * True if composition is in progress inside the document.
85
+ *
86
+ * This property is updated by the {@link module:engine/view/observer/compositionobserver~CompositionObserver}.
87
+ * If the {@link module:engine/view/observer/compositionobserver~CompositionObserver} is disabled this property will not change.
88
+ *
89
+ * @readonly
90
+ * @observable
91
+ * @member {Boolean} module:engine/view/document~Document#isComposing
92
+ */
93
+ this.set( 'isComposing', false );
94
+
95
+ /**
96
+ * Post-fixer callbacks registered to the view document.
97
+ *
98
+ * @private
99
+ * @member {Set}
100
+ */
101
+ this._postFixers = new Set();
102
+ }
103
+
104
+ /**
105
+ * Gets a {@link module:engine/view/document~Document#roots view root element} with the specified name. If the name is not
106
+ * specific "main" root is returned.
107
+ *
108
+ * @param {String} [name='main'] Name of the root.
109
+ * @returns {module:engine/view/rooteditableelement~RootEditableElement|null} The view root element with the specified name
110
+ * or null when there is no root of given name.
111
+ */
112
+ getRoot( name = 'main' ) {
113
+ return this.roots.get( name );
114
+ }
115
+
116
+ /**
117
+ * Allows registering post-fixer callbacks. A post-fixers mechanism allows to update the view tree just before it is rendered
118
+ * to the DOM.
119
+ *
120
+ * Post-fixers are executed right after all changes from the outermost change block were applied but
121
+ * before the {@link module:engine/view/view~View#event:render render event} is fired. If a post-fixer callback made
122
+ * a change, it should return `true`. When this happens, all post-fixers are fired again to check if something else should
123
+ * not be fixed in the new document tree state.
124
+ *
125
+ * View post-fixers are useful when you want to apply some fixes whenever the view structure changes. Keep in mind that
126
+ * changes executed in a view post-fixer should not break model-view mapping.
127
+ *
128
+ * The types of changes which should be safe:
129
+ *
130
+ * * adding or removing attribute from elements,
131
+ * * changes inside of {@link module:engine/view/uielement~UIElement UI elements},
132
+ * * {@link module:engine/model/differ~Differ#refreshItem marking some of the model elements to be re-converted}.
133
+ *
134
+ * Try to avoid changes which touch view structure:
135
+ *
136
+ * * you should not add or remove nor wrap or unwrap any view elements,
137
+ * * you should not change the editor data model in a view post-fixer.
138
+ *
139
+ * As a parameter, a post-fixer callback receives a {@link module:engine/view/downcastwriter~DowncastWriter downcast writer}.
140
+ *
141
+ * Typically, a post-fixer will look like this:
142
+ *
143
+ * editor.editing.view.document.registerPostFixer( writer => {
144
+ * if ( checkSomeCondition() ) {
145
+ * writer.doSomething();
146
+ *
147
+ * // Let other post-fixers know that something changed.
148
+ * return true;
149
+ * }
150
+ * } );
151
+ *
152
+ * Note that nothing happens right after you register a post-fixer (e.g. execute such a code in the console).
153
+ * That is because adding a post-fixer does not execute it.
154
+ * The post-fixer will be executed as soon as any change in the document needs to cause its rendering.
155
+ * If you want to re-render the editor's view after registering the post-fixer then you should do it manually by calling
156
+ * {@link module:engine/view/view~View#forceRender `view.forceRender()`}.
157
+ *
158
+ * If you need to register a callback which is executed when DOM elements are already updated,
159
+ * use {@link module:engine/view/view~View#event:render render event}.
160
+ *
161
+ * @param {Function} postFixer
162
+ */
163
+ registerPostFixer( postFixer ) {
164
+ this._postFixers.add( postFixer );
165
+ }
166
+
167
+ /**
168
+ * Destroys this instance. Makes sure that all observers are destroyed and listeners removed.
169
+ */
170
+ destroy() {
171
+ this.roots.map( root => root.destroy() );
172
+ this.stopListening();
173
+ }
174
+
175
+ /**
176
+ * Performs post-fixer loops. Executes post-fixer callbacks as long as none of them has done any changes to the model.
177
+ *
178
+ * @protected
179
+ * @param {module:engine/view/downcastwriter~DowncastWriter} writer
180
+ */
181
+ _callPostFixers( writer ) {
182
+ let wasFixed = false;
183
+
184
+ do {
185
+ for ( const callback of this._postFixers ) {
186
+ wasFixed = callback( writer );
187
+
188
+ if ( wasFixed ) {
189
+ break;
190
+ }
191
+ }
192
+ } while ( wasFixed );
193
+ }
194
+
195
+ /**
196
+ * Event fired whenever document content layout changes. It is fired whenever content is
197
+ * {@link module:engine/view/view~View#event:render rendered}, but should be also fired by observers in case of
198
+ * other actions which may change layout, for instance when image loads.
199
+ *
200
+ * @event layoutChanged
201
+ */
202
+
203
+ // @if CK_DEBUG_ENGINE // log( version ) {
204
+ // @if CK_DEBUG_ENGINE // logDocument( this, version );
205
+ // @if CK_DEBUG_ENGINE // }
206
+ }
207
+
208
+ mix( Document, BubblingEmitterMixin );
209
+ mix( Document, ObservableMixin );
210
+
211
+ /**
212
+ * Enum representing type of the change.
213
+ *
214
+ * Possible values:
215
+ *
216
+ * * `children` - for child list changes,
217
+ * * `attributes` - for element attributes changes,
218
+ * * `text` - for text nodes changes.
219
+ *
220
+ * @typedef {String} module:engine/view/document~ChangeType
221
+ */