@ckeditor/ckeditor5-engine 47.6.1 → 48.0.0-alpha.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 (258) hide show
  1. package/LICENSE.md +1 -1
  2. package/{src → dist}/engineconfig.d.ts +6 -15
  3. package/dist/index-editor.css +38 -15
  4. package/dist/index.css +37 -37
  5. package/dist/index.css.map +1 -1
  6. package/{src → dist}/index.d.ts +0 -1
  7. package/dist/index.js +588 -94
  8. package/dist/index.js.map +1 -1
  9. package/{src → dist}/model/model.d.ts +10 -4
  10. package/{src → dist}/model/selection.d.ts +1 -1
  11. package/{src → dist}/view/downcastwriter.d.ts +3 -2
  12. package/{src → dist}/view/element.d.ts +2 -2
  13. package/{src → dist}/view/matcher.d.ts +4 -2
  14. package/dist/view/styles/background.d.ts +18 -0
  15. package/{src → dist}/view/styles/border.d.ts +0 -12
  16. package/{src → dist}/view/styles/margin.d.ts +0 -13
  17. package/{src → dist}/view/styles/padding.d.ts +0 -13
  18. package/{src → dist}/view/styles/utils.d.ts +12 -0
  19. package/package.json +20 -39
  20. package/src/controller/datacontroller.js +0 -522
  21. package/src/controller/editingcontroller.js +0 -181
  22. package/src/conversion/conversion.js +0 -606
  23. package/src/conversion/conversionhelpers.js +0 -33
  24. package/src/conversion/downcastdispatcher.js +0 -563
  25. package/src/conversion/downcasthelpers.js +0 -2160
  26. package/src/conversion/mapper.js +0 -1050
  27. package/src/conversion/modelconsumable.js +0 -331
  28. package/src/conversion/upcastdispatcher.js +0 -470
  29. package/src/conversion/upcasthelpers.js +0 -952
  30. package/src/conversion/viewconsumable.js +0 -541
  31. package/src/dataprocessor/basichtmlwriter.js +0 -22
  32. package/src/dataprocessor/dataprocessor.js +0 -5
  33. package/src/dataprocessor/htmldataprocessor.js +0 -107
  34. package/src/dataprocessor/htmlwriter.js +0 -5
  35. package/src/dataprocessor/xmldataprocessor.js +0 -127
  36. package/src/dev-utils/model.js +0 -396
  37. package/src/dev-utils/operationreplayer.js +0 -116
  38. package/src/dev-utils/utils.js +0 -122
  39. package/src/dev-utils/view.js +0 -990
  40. package/src/engineconfig.js +0 -5
  41. package/src/index.js +0 -134
  42. package/src/legacyerrors.js +0 -17
  43. package/src/model/batch.js +0 -98
  44. package/src/model/differ.js +0 -1288
  45. package/src/model/document.js +0 -398
  46. package/src/model/documentfragment.js +0 -332
  47. package/src/model/documentselection.js +0 -1026
  48. package/src/model/element.js +0 -323
  49. package/src/model/history.js +0 -206
  50. package/src/model/item.js +0 -5
  51. package/src/model/liveposition.js +0 -93
  52. package/src/model/liverange.js +0 -121
  53. package/src/model/markercollection.js +0 -436
  54. package/src/model/model.js +0 -866
  55. package/src/model/node.js +0 -371
  56. package/src/model/nodelist.js +0 -244
  57. package/src/model/operation/attributeoperation.js +0 -172
  58. package/src/model/operation/detachoperation.js +0 -87
  59. package/src/model/operation/insertoperation.js +0 -153
  60. package/src/model/operation/markeroperation.js +0 -136
  61. package/src/model/operation/mergeoperation.js +0 -184
  62. package/src/model/operation/moveoperation.js +0 -179
  63. package/src/model/operation/nooperation.js +0 -48
  64. package/src/model/operation/operation.js +0 -78
  65. package/src/model/operation/operationfactory.js +0 -44
  66. package/src/model/operation/renameoperation.js +0 -128
  67. package/src/model/operation/rootattributeoperation.js +0 -173
  68. package/src/model/operation/rootoperation.js +0 -106
  69. package/src/model/operation/splitoperation.js +0 -214
  70. package/src/model/operation/transform.js +0 -2211
  71. package/src/model/operation/utils.js +0 -217
  72. package/src/model/position.js +0 -1041
  73. package/src/model/range.js +0 -880
  74. package/src/model/rootelement.js +0 -82
  75. package/src/model/schema.js +0 -1542
  76. package/src/model/selection.js +0 -814
  77. package/src/model/text.js +0 -92
  78. package/src/model/textproxy.js +0 -202
  79. package/src/model/treewalker.js +0 -313
  80. package/src/model/typecheckable.js +0 -16
  81. package/src/model/utils/autoparagraphing.js +0 -63
  82. package/src/model/utils/deletecontent.js +0 -509
  83. package/src/model/utils/getselectedcontent.js +0 -126
  84. package/src/model/utils/insertcontent.js +0 -750
  85. package/src/model/utils/insertobject.js +0 -135
  86. package/src/model/utils/modifyselection.js +0 -187
  87. package/src/model/utils/selection-post-fixer.js +0 -264
  88. package/src/model/writer.js +0 -1318
  89. package/src/view/attributeelement.js +0 -220
  90. package/src/view/containerelement.js +0 -91
  91. package/src/view/datatransfer.js +0 -106
  92. package/src/view/document.js +0 -139
  93. package/src/view/documentfragment.js +0 -251
  94. package/src/view/documentselection.js +0 -270
  95. package/src/view/domconverter.js +0 -1661
  96. package/src/view/downcastwriter.js +0 -1589
  97. package/src/view/editableelement.js +0 -74
  98. package/src/view/element.js +0 -1053
  99. package/src/view/elementdefinition.js +0 -5
  100. package/src/view/emptyelement.js +0 -83
  101. package/src/view/filler.js +0 -161
  102. package/src/view/item.js +0 -5
  103. package/src/view/matcher.js +0 -437
  104. package/src/view/node.js +0 -238
  105. package/src/view/observer/arrowkeysobserver.js +0 -40
  106. package/src/view/observer/bubblingemittermixin.js +0 -215
  107. package/src/view/observer/bubblingeventinfo.js +0 -49
  108. package/src/view/observer/clickobserver.js +0 -26
  109. package/src/view/observer/compositionobserver.js +0 -64
  110. package/src/view/observer/domeventdata.js +0 -63
  111. package/src/view/observer/domeventobserver.js +0 -81
  112. package/src/view/observer/fakeselectionobserver.js +0 -95
  113. package/src/view/observer/focusobserver.js +0 -166
  114. package/src/view/observer/inputobserver.js +0 -236
  115. package/src/view/observer/keyobserver.js +0 -36
  116. package/src/view/observer/mouseobserver.js +0 -26
  117. package/src/view/observer/mutationobserver.js +0 -219
  118. package/src/view/observer/observer.js +0 -92
  119. package/src/view/observer/pointerobserver.js +0 -26
  120. package/src/view/observer/selectionobserver.js +0 -318
  121. package/src/view/observer/tabobserver.js +0 -42
  122. package/src/view/observer/touchobserver.js +0 -26
  123. package/src/view/placeholder.js +0 -285
  124. package/src/view/position.js +0 -341
  125. package/src/view/range.js +0 -451
  126. package/src/view/rawelement.js +0 -115
  127. package/src/view/renderer.js +0 -1148
  128. package/src/view/rooteditableelement.js +0 -78
  129. package/src/view/selection.js +0 -594
  130. package/src/view/styles/background.d.ts +0 -33
  131. package/src/view/styles/background.js +0 -74
  132. package/src/view/styles/border.js +0 -316
  133. package/src/view/styles/margin.js +0 -34
  134. package/src/view/styles/padding.js +0 -34
  135. package/src/view/styles/utils.js +0 -219
  136. package/src/view/stylesmap.js +0 -941
  137. package/src/view/text.js +0 -110
  138. package/src/view/textproxy.js +0 -136
  139. package/src/view/tokenlist.js +0 -194
  140. package/src/view/treewalker.js +0 -389
  141. package/src/view/typecheckable.js +0 -19
  142. package/src/view/uielement.js +0 -194
  143. package/src/view/upcastwriter.js +0 -363
  144. package/src/view/view.js +0 -579
  145. package/theme/placeholder.css +0 -36
  146. package/theme/renderer.css +0 -9
  147. /package/{src → dist}/controller/datacontroller.d.ts +0 -0
  148. /package/{src → dist}/controller/editingcontroller.d.ts +0 -0
  149. /package/{src → dist}/conversion/conversion.d.ts +0 -0
  150. /package/{src → dist}/conversion/conversionhelpers.d.ts +0 -0
  151. /package/{src → dist}/conversion/downcastdispatcher.d.ts +0 -0
  152. /package/{src → dist}/conversion/downcasthelpers.d.ts +0 -0
  153. /package/{src → dist}/conversion/mapper.d.ts +0 -0
  154. /package/{src → dist}/conversion/modelconsumable.d.ts +0 -0
  155. /package/{src → dist}/conversion/upcastdispatcher.d.ts +0 -0
  156. /package/{src → dist}/conversion/upcasthelpers.d.ts +0 -0
  157. /package/{src → dist}/conversion/viewconsumable.d.ts +0 -0
  158. /package/{src → dist}/dataprocessor/basichtmlwriter.d.ts +0 -0
  159. /package/{src → dist}/dataprocessor/dataprocessor.d.ts +0 -0
  160. /package/{src → dist}/dataprocessor/htmldataprocessor.d.ts +0 -0
  161. /package/{src → dist}/dataprocessor/htmlwriter.d.ts +0 -0
  162. /package/{src → dist}/dataprocessor/xmldataprocessor.d.ts +0 -0
  163. /package/{src → dist}/dev-utils/model.d.ts +0 -0
  164. /package/{src → dist}/dev-utils/operationreplayer.d.ts +0 -0
  165. /package/{src → dist}/dev-utils/utils.d.ts +0 -0
  166. /package/{src → dist}/dev-utils/view.d.ts +0 -0
  167. /package/{src → dist}/legacyerrors.d.ts +0 -0
  168. /package/{src → dist}/model/batch.d.ts +0 -0
  169. /package/{src → dist}/model/differ.d.ts +0 -0
  170. /package/{src → dist}/model/document.d.ts +0 -0
  171. /package/{src → dist}/model/documentfragment.d.ts +0 -0
  172. /package/{src → dist}/model/documentselection.d.ts +0 -0
  173. /package/{src → dist}/model/element.d.ts +0 -0
  174. /package/{src → dist}/model/history.d.ts +0 -0
  175. /package/{src → dist}/model/item.d.ts +0 -0
  176. /package/{src → dist}/model/liveposition.d.ts +0 -0
  177. /package/{src → dist}/model/liverange.d.ts +0 -0
  178. /package/{src → dist}/model/markercollection.d.ts +0 -0
  179. /package/{src → dist}/model/node.d.ts +0 -0
  180. /package/{src → dist}/model/nodelist.d.ts +0 -0
  181. /package/{src → dist}/model/operation/attributeoperation.d.ts +0 -0
  182. /package/{src → dist}/model/operation/detachoperation.d.ts +0 -0
  183. /package/{src → dist}/model/operation/insertoperation.d.ts +0 -0
  184. /package/{src → dist}/model/operation/markeroperation.d.ts +0 -0
  185. /package/{src → dist}/model/operation/mergeoperation.d.ts +0 -0
  186. /package/{src → dist}/model/operation/moveoperation.d.ts +0 -0
  187. /package/{src → dist}/model/operation/nooperation.d.ts +0 -0
  188. /package/{src → dist}/model/operation/operation.d.ts +0 -0
  189. /package/{src → dist}/model/operation/operationfactory.d.ts +0 -0
  190. /package/{src → dist}/model/operation/renameoperation.d.ts +0 -0
  191. /package/{src → dist}/model/operation/rootattributeoperation.d.ts +0 -0
  192. /package/{src → dist}/model/operation/rootoperation.d.ts +0 -0
  193. /package/{src → dist}/model/operation/splitoperation.d.ts +0 -0
  194. /package/{src → dist}/model/operation/transform.d.ts +0 -0
  195. /package/{src → dist}/model/operation/utils.d.ts +0 -0
  196. /package/{src → dist}/model/position.d.ts +0 -0
  197. /package/{src → dist}/model/range.d.ts +0 -0
  198. /package/{src → dist}/model/rootelement.d.ts +0 -0
  199. /package/{src → dist}/model/schema.d.ts +0 -0
  200. /package/{src → dist}/model/text.d.ts +0 -0
  201. /package/{src → dist}/model/textproxy.d.ts +0 -0
  202. /package/{src → dist}/model/treewalker.d.ts +0 -0
  203. /package/{src → dist}/model/typecheckable.d.ts +0 -0
  204. /package/{src → dist}/model/utils/autoparagraphing.d.ts +0 -0
  205. /package/{src → dist}/model/utils/deletecontent.d.ts +0 -0
  206. /package/{src → dist}/model/utils/getselectedcontent.d.ts +0 -0
  207. /package/{src → dist}/model/utils/insertcontent.d.ts +0 -0
  208. /package/{src → dist}/model/utils/insertobject.d.ts +0 -0
  209. /package/{src → dist}/model/utils/modifyselection.d.ts +0 -0
  210. /package/{src → dist}/model/utils/selection-post-fixer.d.ts +0 -0
  211. /package/{src → dist}/model/writer.d.ts +0 -0
  212. /package/{src → dist}/view/attributeelement.d.ts +0 -0
  213. /package/{src → dist}/view/containerelement.d.ts +0 -0
  214. /package/{src → dist}/view/datatransfer.d.ts +0 -0
  215. /package/{src → dist}/view/document.d.ts +0 -0
  216. /package/{src → dist}/view/documentfragment.d.ts +0 -0
  217. /package/{src → dist}/view/documentselection.d.ts +0 -0
  218. /package/{src → dist}/view/domconverter.d.ts +0 -0
  219. /package/{src → dist}/view/editableelement.d.ts +0 -0
  220. /package/{src → dist}/view/elementdefinition.d.ts +0 -0
  221. /package/{src → dist}/view/emptyelement.d.ts +0 -0
  222. /package/{src → dist}/view/filler.d.ts +0 -0
  223. /package/{src → dist}/view/item.d.ts +0 -0
  224. /package/{src → dist}/view/node.d.ts +0 -0
  225. /package/{src → dist}/view/observer/arrowkeysobserver.d.ts +0 -0
  226. /package/{src → dist}/view/observer/bubblingemittermixin.d.ts +0 -0
  227. /package/{src → dist}/view/observer/bubblingeventinfo.d.ts +0 -0
  228. /package/{src → dist}/view/observer/clickobserver.d.ts +0 -0
  229. /package/{src → dist}/view/observer/compositionobserver.d.ts +0 -0
  230. /package/{src → dist}/view/observer/domeventdata.d.ts +0 -0
  231. /package/{src → dist}/view/observer/domeventobserver.d.ts +0 -0
  232. /package/{src → dist}/view/observer/fakeselectionobserver.d.ts +0 -0
  233. /package/{src → dist}/view/observer/focusobserver.d.ts +0 -0
  234. /package/{src → dist}/view/observer/inputobserver.d.ts +0 -0
  235. /package/{src → dist}/view/observer/keyobserver.d.ts +0 -0
  236. /package/{src → dist}/view/observer/mouseobserver.d.ts +0 -0
  237. /package/{src → dist}/view/observer/mutationobserver.d.ts +0 -0
  238. /package/{src → dist}/view/observer/observer.d.ts +0 -0
  239. /package/{src → dist}/view/observer/pointerobserver.d.ts +0 -0
  240. /package/{src → dist}/view/observer/selectionobserver.d.ts +0 -0
  241. /package/{src → dist}/view/observer/tabobserver.d.ts +0 -0
  242. /package/{src → dist}/view/observer/touchobserver.d.ts +0 -0
  243. /package/{src → dist}/view/placeholder.d.ts +0 -0
  244. /package/{src → dist}/view/position.d.ts +0 -0
  245. /package/{src → dist}/view/range.d.ts +0 -0
  246. /package/{src → dist}/view/rawelement.d.ts +0 -0
  247. /package/{src → dist}/view/renderer.d.ts +0 -0
  248. /package/{src → dist}/view/rooteditableelement.d.ts +0 -0
  249. /package/{src → dist}/view/selection.d.ts +0 -0
  250. /package/{src → dist}/view/stylesmap.d.ts +0 -0
  251. /package/{src → dist}/view/text.d.ts +0 -0
  252. /package/{src → dist}/view/textproxy.d.ts +0 -0
  253. /package/{src → dist}/view/tokenlist.d.ts +0 -0
  254. /package/{src → dist}/view/treewalker.d.ts +0 -0
  255. /package/{src → dist}/view/typecheckable.d.ts +0 -0
  256. /package/{src → dist}/view/uielement.d.ts +0 -0
  257. /package/{src → dist}/view/upcastwriter.d.ts +0 -0
  258. /package/{src → dist}/view/view.d.ts +0 -0
@@ -1,331 +0,0 @@
1
- /**
2
- * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options
4
- */
5
- /**
6
- * @module engine/conversion/modelconsumable
7
- */
8
- import { ModelTextProxy } from '../model/textproxy.js';
9
- import { CKEditorError } from '@ckeditor/ckeditor5-utils';
10
- /**
11
- * Manages a list of consumable values for the {@link module:engine/model/item~ModelItem model items}.
12
- *
13
- * Consumables are various aspects of the model. A model item can be broken down into separate, single properties that might be
14
- * taken into consideration when converting that item.
15
- *
16
- * `ModelConsumable` is used by {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher} while analyzing the changed
17
- * parts of {@link module:engine/model/document~ModelDocument the document}. The added / changed / removed model items are broken down
18
- * into singular properties (the item itself and its attributes). All those parts are saved in `ModelConsumable`. Then,
19
- * during conversion, when the given part of a model item is converted (i.e. the view element has been inserted into the view,
20
- * but without attributes), the consumable value is removed from `ModelConsumable`.
21
- *
22
- * For model items, `ModelConsumable` stores consumable values of one of following types: `insert`, `addattribute:<attributeKey>`,
23
- * `changeattributes:<attributeKey>`, `removeattributes:<attributeKey>`.
24
- *
25
- * In most cases, it is enough to let th {@link module:engine/conversion/downcastdispatcher~DowncastDispatcher}
26
- * gather consumable values, so there is no need to use
27
- * the {@link module:engine/conversion/modelconsumable~ModelConsumable#add add method} directly.
28
- * However, it is important to understand how consumable values can be
29
- * {@link module:engine/conversion/modelconsumable~ModelConsumable#consume consumed}.
30
- * See {@link module:engine/conversion/downcasthelpers default downcast converters} for more information.
31
- *
32
- * Keep in mind that one conversion event may have multiple callbacks (converters) attached to it. Each of those is
33
- * able to convert one or more parts of the model. However, when one of those callbacks actually converts
34
- * something, the others should not, because they would duplicate the results. Using `ModelConsumable` helps to avoid
35
- * this situation, because callbacks should only convert these values that were not yet consumed from `ModelConsumable`.
36
- *
37
- * Consuming multiple values in a single callback:
38
- *
39
- * ```ts
40
- * // Converter for custom `imageBlock` element that might have a `caption` element inside which changes
41
- * // how the image is displayed in the view:
42
- * //
43
- * // Model:
44
- * //
45
- * // [imageBlock]
46
- * // └─ [caption]
47
- * // └─ foo
48
- * //
49
- * // View:
50
- * //
51
- * // <figure>
52
- * // ├─ <img />
53
- * // └─ <caption>
54
- * // └─ foo
55
- * modelConversionDispatcher.on( 'insert:imageBlock', ( evt, data, conversionApi ) => {
56
- * // First, consume the `imageBlock` element.
57
- * conversionApi.consumable.consume( data.item, 'insert' );
58
- *
59
- * // Just create normal image element for the view.
60
- * // Maybe it will be "decorated" later.
61
- * const viewImage = new ViewElement( 'img' );
62
- * const insertPosition = conversionApi.mapper.toViewPosition( data.range.start );
63
- * const viewWriter = conversionApi.writer;
64
- *
65
- * // Check if the `imageBlock` element has children.
66
- * if ( data.item.childCount > 0 ) {
67
- * const modelCaption = data.item.getChild( 0 );
68
- *
69
- * // `modelCaption` insertion change is consumed from consumable values.
70
- * // It will not be converted by other converters, but it's children (probably some text) will be.
71
- * // Through mapping, converters for text will know where to insert contents of `modelCaption`.
72
- * if ( conversionApi.consumable.consume( modelCaption, 'insert' ) ) {
73
- * const viewCaption = new ViewElement( 'figcaption' );
74
- *
75
- * const viewImageHolder = new ViewElement( 'figure', null, [ viewImage, viewCaption ] );
76
- *
77
- * conversionApi.mapper.bindElements( modelCaption, viewCaption );
78
- * conversionApi.mapper.bindElements( data.item, viewImageHolder );
79
- * viewWriter.insert( insertPosition, viewImageHolder );
80
- * }
81
- * } else {
82
- * conversionApi.mapper.bindElements( data.item, viewImage );
83
- * viewWriter.insert( insertPosition, viewImage );
84
- * }
85
- *
86
- * evt.stop();
87
- * } );
88
- * ```
89
- */
90
- export class ModelConsumable {
91
- /**
92
- * Contains list of consumable values.
93
- */
94
- _consumable = new Map();
95
- /**
96
- * For each {@link module:engine/model/textproxy~ModelTextProxy} added to `ModelConsumable`, this registry holds a parent
97
- * of that `ModelTextProxy` and the start and end indices of that `ModelTextProxy`. This allows identification of the `ModelTextProxy`
98
- * instances that point to the same part of the model but are different instances. Each distinct `ModelTextProxy`
99
- * is given a unique `Symbol` which is then registered as consumable. This process is transparent for the `ModelConsumable`
100
- * API user because whenever `ModelTextProxy` is added, tested, consumed or reverted, the internal mechanisms of
101
- * `ModelConsumable` translate `ModelTextProxy` to that unique `Symbol`.
102
- */
103
- _textProxyRegistry = new Map();
104
- /**
105
- * Adds a consumable value to the consumables list and links it with a given model item.
106
- *
107
- * ```ts
108
- * modelConsumable.add( modelElement, 'insert' ); // Add `modelElement` insertion change to consumable values.
109
- * modelConsumable.add( modelElement, 'addAttribute:bold' ); // Add `bold` attribute insertion on `modelElement` change.
110
- * modelConsumable.add( modelElement, 'removeAttribute:bold' ); // Add `bold` attribute removal on `modelElement` change.
111
- * modelConsumable.add( modelSelection, 'selection' ); // Add `modelSelection` to consumable values.
112
- * modelConsumable.add( modelRange, 'range' ); // Add `modelRange` to consumable values.
113
- * ```
114
- *
115
- * @param item Model item, range or selection that has the consumable.
116
- * @param type Consumable type. Will be normalized to a proper form, that is either `<word>` or `<part>:<part>`.
117
- * Second colon and everything after will be cut. Passing event name is a safe and good practice.
118
- */
119
- add(item, type) {
120
- type = _normalizeConsumableType(type);
121
- if (item instanceof ModelTextProxy) {
122
- item = this._getSymbolForTextProxy(item);
123
- }
124
- if (!this._consumable.has(item)) {
125
- this._consumable.set(item, new Map());
126
- }
127
- this._consumable.get(item).set(type, true);
128
- }
129
- /**
130
- * Removes a given consumable value from a given model item.
131
- *
132
- * ```ts
133
- * modelConsumable.consume( modelElement, 'insert' ); // Remove `modelElement` insertion change from consumable values.
134
- * modelConsumable.consume( modelElement, 'addAttribute:bold' ); // Remove `bold` attribute insertion on `modelElement` change.
135
- * modelConsumable.consume( modelElement, 'removeAttribute:bold' ); // Remove `bold` attribute removal on `modelElement` change.
136
- * modelConsumable.consume( modelSelection, 'selection' ); // Remove `modelSelection` from consumable values.
137
- * modelConsumable.consume( modelRange, 'range' ); // Remove 'modelRange' from consumable values.
138
- * ```
139
- *
140
- * @param item Model item, range or selection from which consumable will be consumed.
141
- * @param type Consumable type. Will be normalized to a proper form, that is either `<word>` or `<part>:<part>`.
142
- * Second colon and everything after will be cut. Passing event name is a safe and good practice.
143
- * @returns `true` if consumable value was available and was consumed, `false` otherwise.
144
- */
145
- consume(item, type) {
146
- type = _normalizeConsumableType(type);
147
- if (item instanceof ModelTextProxy) {
148
- item = this._getSymbolForTextProxy(item);
149
- }
150
- if (this.test(item, type)) {
151
- this._consumable.get(item).set(type, false);
152
- return true;
153
- }
154
- else {
155
- return false;
156
- }
157
- }
158
- /**
159
- * Tests whether there is a consumable value of a given type connected with a given model item.
160
- *
161
- * ```ts
162
- * modelConsumable.test( modelElement, 'insert' ); // Check for `modelElement` insertion change.
163
- * modelConsumable.test( modelElement, 'addAttribute:bold' ); // Check for `bold` attribute insertion on `modelElement` change.
164
- * modelConsumable.test( modelElement, 'removeAttribute:bold' ); // Check for `bold` attribute removal on `modelElement` change.
165
- * modelConsumable.test( modelSelection, 'selection' ); // Check if `modelSelection` is consumable.
166
- * modelConsumable.test( modelRange, 'range' ); // Check if `modelRange` is consumable.
167
- * ```
168
- *
169
- * @param item Model item, range or selection to be tested.
170
- * @param type Consumable type. Will be normalized to a proper form, that is either `<word>` or `<part>:<part>`.
171
- * Second colon and everything after will be cut. Passing event name is a safe and good practice.
172
- * @returns `null` if such consumable was never added, `false` if the consumable values was
173
- * already consumed or `true` if it was added and not consumed yet.
174
- */
175
- test(item, type) {
176
- type = _normalizeConsumableType(type);
177
- if (item instanceof ModelTextProxy) {
178
- item = this._getSymbolForTextProxy(item);
179
- }
180
- const itemConsumables = this._consumable.get(item);
181
- if (itemConsumables === undefined) {
182
- return null;
183
- }
184
- const value = itemConsumables.get(type);
185
- if (value === undefined) {
186
- return null;
187
- }
188
- return value;
189
- }
190
- /**
191
- * Reverts consuming of a consumable value.
192
- *
193
- * ```ts
194
- * modelConsumable.revert( modelElement, 'insert' ); // Revert consuming `modelElement` insertion change.
195
- * modelConsumable.revert( modelElement, 'addAttribute:bold' ); // Revert consuming `bold` attribute insert from `modelElement`.
196
- * modelConsumable.revert( modelElement, 'removeAttribute:bold' ); // Revert consuming `bold` attribute remove from `modelElement`.
197
- * modelConsumable.revert( modelSelection, 'selection' ); // Revert consuming `modelSelection`.
198
- * modelConsumable.revert( modelRange, 'range' ); // Revert consuming `modelRange`.
199
- * ```
200
- *
201
- * @param item Model item, range or selection to be reverted.
202
- * @param type Consumable type.
203
- * @returns `true` if consumable has been reversed, `false` otherwise. `null` if the consumable has
204
- * never been added.
205
- */
206
- revert(item, type) {
207
- type = _normalizeConsumableType(type);
208
- if (item instanceof ModelTextProxy) {
209
- item = this._getSymbolForTextProxy(item);
210
- }
211
- const test = this.test(item, type);
212
- if (test === false) {
213
- this._consumable.get(item).set(type, true);
214
- return true;
215
- }
216
- else if (test === true) {
217
- return false;
218
- }
219
- return null;
220
- }
221
- /**
222
- * Verifies if all events from the specified group were consumed.
223
- *
224
- * @param eventGroup The events group to verify.
225
- */
226
- verifyAllConsumed(eventGroup) {
227
- const items = [];
228
- for (const [item, consumables] of this._consumable) {
229
- for (const [event, canConsume] of consumables) {
230
- const eventPrefix = event.split(':')[0];
231
- if (canConsume && eventGroup == eventPrefix) {
232
- items.push({
233
- event,
234
- item: item.name || item.description
235
- });
236
- }
237
- }
238
- }
239
- if (items.length) {
240
- /**
241
- * Some of the {@link module:engine/model/item~ModelItem model items} were not consumed while downcasting the model to view.
242
- *
243
- * This might be the effect of:
244
- *
245
- * * A missing converter for some model elements. Make sure that you registered downcast converters for all model elements.
246
- * * A custom converter that does not consume converted items. Make sure that you
247
- * {@link module:engine/conversion/modelconsumable~ModelConsumable#consume consumed} all model elements that you converted
248
- * from the model to the view.
249
- * * A custom converter that called `event.stop()`. When providing a custom converter, keep in mind that you should not stop
250
- * the event. If you stop it then the default converter at the `lowest` priority will not trigger the conversion of this node's
251
- * attributes and child nodes.
252
- *
253
- * @error conversion-model-consumable-not-consumed
254
- * @param {Array.<module:engine/model/item~ModelItem>} items Items that were not consumed.
255
- */
256
- throw new CKEditorError('conversion-model-consumable-not-consumed', null, { items });
257
- }
258
- }
259
- /**
260
- * Gets a unique symbol for the passed {@link module:engine/model/textproxy~ModelTextProxy} instance.
261
- * All `ModelTextProxy` instances that have same parent, same start index and same end index will get the same symbol.
262
- *
263
- * Used internally to correctly consume `ModelTextProxy` instances.
264
- *
265
- * @internal
266
- * @param textProxy `ModelTextProxy` instance to get a symbol for.
267
- * @returns Symbol representing all equal instances of `ModelTextProxy`.
268
- */
269
- _getSymbolForTextProxy(textProxy) {
270
- let symbol = null;
271
- const startMap = this._textProxyRegistry.get(textProxy.startOffset);
272
- if (startMap) {
273
- const endMap = startMap.get(textProxy.endOffset);
274
- if (endMap) {
275
- symbol = endMap.get(textProxy.parent);
276
- }
277
- }
278
- if (!symbol) {
279
- symbol = this._addSymbolForTextProxy(textProxy);
280
- }
281
- return symbol;
282
- }
283
- /**
284
- * Adds a symbol for the given {@link module:engine/model/textproxy~ModelTextProxy} instance.
285
- *
286
- * Used internally to correctly consume `ModelTextProxy` instances.
287
- *
288
- * @param textProxy Text proxy instance.
289
- * @returns Symbol generated for given `ModelTextProxy`.
290
- */
291
- _addSymbolForTextProxy(textProxy) {
292
- const start = textProxy.startOffset;
293
- const end = textProxy.endOffset;
294
- const parent = textProxy.parent;
295
- const symbol = Symbol('$textProxy:' + textProxy.data);
296
- let startMap;
297
- let endMap;
298
- startMap = this._textProxyRegistry.get(start);
299
- if (!startMap) {
300
- startMap = new Map();
301
- this._textProxyRegistry.set(start, startMap);
302
- }
303
- endMap = startMap.get(end);
304
- if (!endMap) {
305
- endMap = new Map();
306
- startMap.set(end, endMap);
307
- }
308
- endMap.set(parent, symbol);
309
- return symbol;
310
- }
311
- }
312
- /**
313
- * Returns a normalized consumable type name from the given string. A normalized consumable type name is a string that has
314
- * at most one colon, for example: `insert` or `addMarker:highlight`. If a string to normalize has more "parts" (more colons),
315
- * the further parts are dropped, for example: `addattribute:bold:$text` -> `addattributes:bold`.
316
- *
317
- * @param type Consumable type.
318
- * @returns Normalized consumable type.
319
- */
320
- function _normalizeConsumableType(type) {
321
- const parts = type.split(':');
322
- // For inserts allow passing event name, it's stored in the context of a specified element so the element name is not needed.
323
- if (parts[0] == 'insert') {
324
- return parts[0];
325
- }
326
- // Markers are identified by the whole name (otherwise we would consume the whole markers group).
327
- if (parts[0] == 'addMarker' || parts[0] == 'removeMarker') {
328
- return type;
329
- }
330
- return parts.length > 1 ? parts[0] + ':' + parts[1] : parts[0];
331
- }