@ckeditor/ckeditor5-engine 38.1.1 → 38.2.0-alpha.1

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 (241) hide show
  1. package/package.json +3 -2
  2. package/src/controller/datacontroller.d.ts +334 -334
  3. package/src/controller/datacontroller.js +481 -481
  4. package/src/controller/editingcontroller.d.ts +98 -98
  5. package/src/controller/editingcontroller.js +191 -191
  6. package/src/conversion/conversion.d.ts +478 -478
  7. package/src/conversion/conversion.js +601 -601
  8. package/src/conversion/conversionhelpers.d.ts +26 -26
  9. package/src/conversion/conversionhelpers.js +32 -32
  10. package/src/conversion/downcastdispatcher.d.ts +547 -547
  11. package/src/conversion/downcastdispatcher.js +538 -538
  12. package/src/conversion/downcasthelpers.d.ts +1226 -1226
  13. package/src/conversion/downcasthelpers.js +2183 -2183
  14. package/src/conversion/mapper.d.ts +503 -503
  15. package/src/conversion/mapper.js +536 -536
  16. package/src/conversion/modelconsumable.d.ts +201 -201
  17. package/src/conversion/modelconsumable.js +333 -333
  18. package/src/conversion/upcastdispatcher.d.ts +492 -492
  19. package/src/conversion/upcastdispatcher.js +460 -460
  20. package/src/conversion/upcasthelpers.d.ts +499 -499
  21. package/src/conversion/upcasthelpers.js +950 -950
  22. package/src/conversion/viewconsumable.d.ts +369 -369
  23. package/src/conversion/viewconsumable.js +532 -532
  24. package/src/dataprocessor/basichtmlwriter.d.ts +18 -18
  25. package/src/dataprocessor/basichtmlwriter.js +19 -19
  26. package/src/dataprocessor/dataprocessor.d.ts +61 -61
  27. package/src/dataprocessor/dataprocessor.js +5 -5
  28. package/src/dataprocessor/htmldataprocessor.d.ts +76 -76
  29. package/src/dataprocessor/htmldataprocessor.js +96 -96
  30. package/src/dataprocessor/htmlwriter.d.ts +16 -16
  31. package/src/dataprocessor/htmlwriter.js +5 -5
  32. package/src/dataprocessor/xmldataprocessor.d.ts +90 -90
  33. package/src/dataprocessor/xmldataprocessor.js +108 -108
  34. package/src/dev-utils/model.d.ts +124 -124
  35. package/src/dev-utils/model.js +395 -395
  36. package/src/dev-utils/operationreplayer.d.ts +51 -51
  37. package/src/dev-utils/operationreplayer.js +112 -112
  38. package/src/dev-utils/utils.d.ts +37 -37
  39. package/src/dev-utils/utils.js +73 -73
  40. package/src/dev-utils/view.d.ts +319 -319
  41. package/src/dev-utils/view.js +967 -967
  42. package/src/index.d.ts +113 -113
  43. package/src/index.js +77 -77
  44. package/src/model/batch.d.ts +106 -106
  45. package/src/model/batch.js +96 -96
  46. package/src/model/differ.d.ts +373 -373
  47. package/src/model/differ.js +1090 -1090
  48. package/src/model/document.d.ts +266 -264
  49. package/src/model/document.js +356 -356
  50. package/src/model/documentfragment.d.ts +200 -200
  51. package/src/model/documentfragment.js +306 -306
  52. package/src/model/documentselection.d.ts +420 -420
  53. package/src/model/documentselection.js +990 -987
  54. package/src/model/element.d.ts +165 -165
  55. package/src/model/element.js +281 -281
  56. package/src/model/history.d.ts +114 -114
  57. package/src/model/history.js +207 -207
  58. package/src/model/item.d.ts +14 -14
  59. package/src/model/item.js +5 -5
  60. package/src/model/liveposition.d.ts +77 -77
  61. package/src/model/liveposition.js +93 -93
  62. package/src/model/liverange.d.ts +102 -102
  63. package/src/model/liverange.js +120 -120
  64. package/src/model/markercollection.d.ts +335 -335
  65. package/src/model/markercollection.js +403 -403
  66. package/src/model/model.d.ts +920 -920
  67. package/src/model/model.js +843 -843
  68. package/src/model/node.d.ts +256 -256
  69. package/src/model/node.js +375 -375
  70. package/src/model/nodelist.d.ts +91 -91
  71. package/src/model/nodelist.js +163 -163
  72. package/src/model/operation/attributeoperation.d.ts +103 -103
  73. package/src/model/operation/attributeoperation.js +148 -148
  74. package/src/model/operation/detachoperation.d.ts +60 -60
  75. package/src/model/operation/detachoperation.js +77 -77
  76. package/src/model/operation/insertoperation.d.ts +90 -90
  77. package/src/model/operation/insertoperation.js +135 -135
  78. package/src/model/operation/markeroperation.d.ts +91 -91
  79. package/src/model/operation/markeroperation.js +107 -107
  80. package/src/model/operation/mergeoperation.d.ts +100 -100
  81. package/src/model/operation/mergeoperation.js +167 -167
  82. package/src/model/operation/moveoperation.d.ts +96 -96
  83. package/src/model/operation/moveoperation.js +164 -164
  84. package/src/model/operation/nooperation.d.ts +38 -38
  85. package/src/model/operation/nooperation.js +48 -48
  86. package/src/model/operation/operation.d.ts +96 -96
  87. package/src/model/operation/operation.js +62 -62
  88. package/src/model/operation/operationfactory.d.ts +18 -18
  89. package/src/model/operation/operationfactory.js +44 -44
  90. package/src/model/operation/renameoperation.d.ts +83 -83
  91. package/src/model/operation/renameoperation.js +115 -115
  92. package/src/model/operation/rootattributeoperation.d.ts +98 -98
  93. package/src/model/operation/rootattributeoperation.js +155 -155
  94. package/src/model/operation/rootoperation.d.ts +80 -80
  95. package/src/model/operation/rootoperation.js +114 -114
  96. package/src/model/operation/splitoperation.d.ts +109 -109
  97. package/src/model/operation/splitoperation.js +194 -194
  98. package/src/model/operation/transform.d.ts +100 -100
  99. package/src/model/operation/transform.js +1985 -1985
  100. package/src/model/operation/utils.d.ts +71 -71
  101. package/src/model/operation/utils.js +213 -213
  102. package/src/model/position.d.ts +539 -539
  103. package/src/model/position.js +979 -979
  104. package/src/model/range.d.ts +458 -458
  105. package/src/model/range.js +875 -875
  106. package/src/model/rootelement.d.ts +54 -54
  107. package/src/model/rootelement.js +68 -68
  108. package/src/model/schema.d.ts +1186 -1176
  109. package/src/model/schema.js +1237 -1237
  110. package/src/model/selection.d.ts +482 -482
  111. package/src/model/selection.js +789 -789
  112. package/src/model/text.d.ts +66 -66
  113. package/src/model/text.js +85 -85
  114. package/src/model/textproxy.d.ts +144 -144
  115. package/src/model/textproxy.js +189 -189
  116. package/src/model/treewalker.d.ts +186 -186
  117. package/src/model/treewalker.js +244 -244
  118. package/src/model/typecheckable.d.ts +285 -285
  119. package/src/model/typecheckable.js +16 -16
  120. package/src/model/utils/autoparagraphing.d.ts +37 -37
  121. package/src/model/utils/autoparagraphing.js +64 -64
  122. package/src/model/utils/deletecontent.d.ts +58 -58
  123. package/src/model/utils/deletecontent.js +488 -488
  124. package/src/model/utils/findoptimalinsertionrange.d.ts +32 -32
  125. package/src/model/utils/findoptimalinsertionrange.js +57 -57
  126. package/src/model/utils/getselectedcontent.d.ts +30 -30
  127. package/src/model/utils/getselectedcontent.js +125 -125
  128. package/src/model/utils/insertcontent.d.ts +46 -46
  129. package/src/model/utils/insertcontent.js +705 -705
  130. package/src/model/utils/insertobject.d.ts +44 -44
  131. package/src/model/utils/insertobject.js +139 -139
  132. package/src/model/utils/modifyselection.d.ts +48 -48
  133. package/src/model/utils/modifyselection.js +186 -186
  134. package/src/model/utils/selection-post-fixer.d.ts +74 -74
  135. package/src/model/utils/selection-post-fixer.js +260 -260
  136. package/src/model/writer.d.ts +851 -851
  137. package/src/model/writer.js +1306 -1306
  138. package/src/view/attributeelement.d.ts +108 -108
  139. package/src/view/attributeelement.js +184 -184
  140. package/src/view/containerelement.d.ts +49 -49
  141. package/src/view/containerelement.js +80 -80
  142. package/src/view/datatransfer.d.ts +79 -79
  143. package/src/view/datatransfer.js +98 -98
  144. package/src/view/document.d.ts +184 -184
  145. package/src/view/document.js +120 -120
  146. package/src/view/documentfragment.d.ts +149 -149
  147. package/src/view/documentfragment.js +228 -228
  148. package/src/view/documentselection.d.ts +306 -306
  149. package/src/view/documentselection.js +256 -256
  150. package/src/view/domconverter.d.ts +634 -650
  151. package/src/view/domconverter.js +1407 -1373
  152. package/src/view/downcastwriter.d.ts +996 -996
  153. package/src/view/downcastwriter.js +1696 -1696
  154. package/src/view/editableelement.d.ts +52 -52
  155. package/src/view/editableelement.js +61 -61
  156. package/src/view/element.d.ts +468 -468
  157. package/src/view/element.js +724 -724
  158. package/src/view/elementdefinition.d.ts +87 -87
  159. package/src/view/elementdefinition.js +5 -5
  160. package/src/view/emptyelement.d.ts +41 -41
  161. package/src/view/emptyelement.js +73 -73
  162. package/src/view/filler.d.ts +111 -111
  163. package/src/view/filler.js +150 -148
  164. package/src/view/item.d.ts +14 -14
  165. package/src/view/item.js +5 -5
  166. package/src/view/matcher.d.ts +486 -486
  167. package/src/view/matcher.js +507 -507
  168. package/src/view/node.d.ts +163 -163
  169. package/src/view/node.js +228 -228
  170. package/src/view/observer/arrowkeysobserver.d.ts +45 -45
  171. package/src/view/observer/arrowkeysobserver.js +40 -40
  172. package/src/view/observer/bubblingemittermixin.d.ts +166 -166
  173. package/src/view/observer/bubblingemittermixin.js +172 -172
  174. package/src/view/observer/bubblingeventinfo.d.ts +47 -47
  175. package/src/view/observer/bubblingeventinfo.js +37 -37
  176. package/src/view/observer/clickobserver.d.ts +43 -43
  177. package/src/view/observer/clickobserver.js +29 -29
  178. package/src/view/observer/compositionobserver.d.ts +82 -82
  179. package/src/view/observer/compositionobserver.js +60 -60
  180. package/src/view/observer/domeventdata.d.ts +50 -50
  181. package/src/view/observer/domeventdata.js +47 -47
  182. package/src/view/observer/domeventobserver.d.ts +73 -73
  183. package/src/view/observer/domeventobserver.js +79 -79
  184. package/src/view/observer/fakeselectionobserver.d.ts +47 -47
  185. package/src/view/observer/fakeselectionobserver.js +91 -91
  186. package/src/view/observer/focusobserver.d.ts +82 -82
  187. package/src/view/observer/focusobserver.js +86 -86
  188. package/src/view/observer/inputobserver.d.ts +86 -86
  189. package/src/view/observer/inputobserver.js +164 -164
  190. package/src/view/observer/keyobserver.d.ts +66 -66
  191. package/src/view/observer/keyobserver.js +39 -39
  192. package/src/view/observer/mouseobserver.d.ts +89 -89
  193. package/src/view/observer/mouseobserver.js +29 -29
  194. package/src/view/observer/mutationobserver.d.ts +86 -86
  195. package/src/view/observer/mutationobserver.js +206 -206
  196. package/src/view/observer/observer.d.ts +89 -89
  197. package/src/view/observer/observer.js +84 -84
  198. package/src/view/observer/selectionobserver.d.ts +148 -148
  199. package/src/view/observer/selectionobserver.js +202 -202
  200. package/src/view/observer/tabobserver.d.ts +46 -46
  201. package/src/view/observer/tabobserver.js +42 -42
  202. package/src/view/placeholder.d.ts +85 -85
  203. package/src/view/placeholder.js +230 -230
  204. package/src/view/position.d.ts +189 -189
  205. package/src/view/position.js +324 -324
  206. package/src/view/range.d.ts +279 -279
  207. package/src/view/range.js +430 -430
  208. package/src/view/rawelement.d.ts +73 -73
  209. package/src/view/rawelement.js +105 -105
  210. package/src/view/renderer.d.ts +265 -265
  211. package/src/view/renderer.js +1000 -1000
  212. package/src/view/rooteditableelement.d.ts +41 -41
  213. package/src/view/rooteditableelement.js +69 -69
  214. package/src/view/selection.d.ts +375 -375
  215. package/src/view/selection.js +559 -559
  216. package/src/view/styles/background.d.ts +33 -33
  217. package/src/view/styles/background.js +74 -74
  218. package/src/view/styles/border.d.ts +43 -43
  219. package/src/view/styles/border.js +316 -316
  220. package/src/view/styles/margin.d.ts +29 -29
  221. package/src/view/styles/margin.js +34 -34
  222. package/src/view/styles/padding.d.ts +29 -29
  223. package/src/view/styles/padding.js +34 -34
  224. package/src/view/styles/utils.d.ts +93 -93
  225. package/src/view/styles/utils.js +219 -219
  226. package/src/view/stylesmap.d.ts +675 -675
  227. package/src/view/stylesmap.js +766 -766
  228. package/src/view/text.d.ts +74 -74
  229. package/src/view/text.js +93 -93
  230. package/src/view/textproxy.d.ts +97 -97
  231. package/src/view/textproxy.js +124 -124
  232. package/src/view/treewalker.d.ts +195 -195
  233. package/src/view/treewalker.js +327 -327
  234. package/src/view/typecheckable.d.ts +448 -448
  235. package/src/view/typecheckable.js +19 -19
  236. package/src/view/uielement.d.ts +96 -96
  237. package/src/view/uielement.js +182 -182
  238. package/src/view/upcastwriter.d.ts +417 -417
  239. package/src/view/upcastwriter.js +359 -359
  240. package/src/view/view.d.ts +480 -480
  241. package/src/view/view.js +534 -534
@@ -1,766 +1,766 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
- */
5
- /**
6
- * @module engine/view/stylesmap
7
- */
8
- import { get, isObject, merge, set, unset } from 'lodash-es';
9
- /**
10
- * Styles map. Allows handling (adding, removing, retrieving) a set of style rules (usually, of an element).
11
- *
12
- * The styles map is capable of normalizing style names so e.g. the following operations are possible:
13
- */
14
- export default class StylesMap {
15
- /**
16
- * Creates Styles instance.
17
- */
18
- constructor(styleProcessor) {
19
- this._styles = {};
20
- this._styleProcessor = styleProcessor;
21
- }
22
- /**
23
- * Returns true if style map has no styles set.
24
- */
25
- get isEmpty() {
26
- const entries = Object.entries(this._styles);
27
- const from = Array.from(entries);
28
- return !from.length;
29
- }
30
- /**
31
- * Number of styles defined.
32
- */
33
- get size() {
34
- if (this.isEmpty) {
35
- return 0;
36
- }
37
- return this.getStyleNames().length;
38
- }
39
- /**
40
- * Set styles map to a new value.
41
- *
42
- * ```ts
43
- * styles.setTo( 'border:1px solid blue;margin-top:1px;' );
44
- * ```
45
- */
46
- setTo(inlineStyle) {
47
- this.clear();
48
- const parsedStyles = Array.from(parseInlineStyles(inlineStyle).entries());
49
- for (const [key, value] of parsedStyles) {
50
- this._styleProcessor.toNormalizedForm(key, value, this._styles);
51
- }
52
- }
53
- /**
54
- * Checks if a given style is set.
55
- *
56
- * ```ts
57
- * styles.setTo( 'margin-left:1px;' );
58
- *
59
- * styles.has( 'margin-left' ); // -> true
60
- * styles.has( 'padding' ); // -> false
61
- * ```
62
- *
63
- * **Note**: This check supports normalized style names.
64
- *
65
- * ```ts
66
- * // Enable 'margin' shorthand processing:
67
- * editor.data.addStyleProcessorRules( addMarginRules );
68
- *
69
- * styles.setTo( 'margin:2px;' );
70
- *
71
- * styles.has( 'margin' ); // -> true
72
- * styles.has( 'margin-top' ); // -> true
73
- * styles.has( 'margin-left' ); // -> true
74
- *
75
- * styles.remove( 'margin-top' );
76
- *
77
- * styles.has( 'margin' ); // -> false
78
- * styles.has( 'margin-top' ); // -> false
79
- * styles.has( 'margin-left' ); // -> true
80
- * ```
81
- *
82
- * @param name Style name.
83
- */
84
- has(name) {
85
- if (this.isEmpty) {
86
- return false;
87
- }
88
- const styles = this._styleProcessor.getReducedForm(name, this._styles);
89
- const propertyDescriptor = styles.find(([property]) => property === name);
90
- // Only return a value if it is set;
91
- return Array.isArray(propertyDescriptor);
92
- }
93
- set(nameOrObject, valueOrObject) {
94
- if (isObject(nameOrObject)) {
95
- for (const [key, value] of Object.entries(nameOrObject)) {
96
- this._styleProcessor.toNormalizedForm(key, value, this._styles);
97
- }
98
- }
99
- else {
100
- this._styleProcessor.toNormalizedForm(nameOrObject, valueOrObject, this._styles);
101
- }
102
- }
103
- /**
104
- * Removes given style.
105
- *
106
- * ```ts
107
- * styles.setTo( 'background:#f00;margin-right:2px;' );
108
- *
109
- * styles.remove( 'background' );
110
- *
111
- * styles.toString(); // -> 'margin-right:2px;'
112
- * ```
113
- *
114
- * ***Note**:* This method uses {@link module:engine/controller/datacontroller~DataController#addStyleProcessorRules
115
- * enabled style processor rules} to normalize passed values.
116
- *
117
- * ```ts
118
- * // Enable 'margin' shorthand processing:
119
- * editor.data.addStyleProcessorRules( addMarginRules );
120
- *
121
- * styles.setTo( 'margin:1px' );
122
- *
123
- * styles.remove( 'margin-top' );
124
- * styles.remove( 'margin-right' );
125
- *
126
- * styles.toString(); // -> 'margin-bottom:1px;margin-left:1px;'
127
- * ```
128
- *
129
- * @param name Style name.
130
- */
131
- remove(name) {
132
- const path = toPath(name);
133
- unset(this._styles, path);
134
- delete this._styles[name];
135
- this._cleanEmptyObjectsOnPath(path);
136
- }
137
- /**
138
- * Returns a normalized style object or a single value.
139
- *
140
- * ```ts
141
- * // Enable 'margin' shorthand processing:
142
- * editor.data.addStyleProcessorRules( addMarginRules );
143
- *
144
- * const styles = new Styles();
145
- * styles.setTo( 'margin:1px 2px 3em;' );
146
- *
147
- * styles.getNormalized( 'margin' );
148
- * // will log:
149
- * // {
150
- * // top: '1px',
151
- * // right: '2px',
152
- * // bottom: '3em',
153
- * // left: '2px' // normalized value from margin shorthand
154
- * // }
155
- *
156
- * styles.getNormalized( 'margin-left' ); // -> '2px'
157
- * ```
158
- *
159
- * **Note**: This method will only return normalized styles if a style processor was defined.
160
- *
161
- * @param name Style name.
162
- */
163
- getNormalized(name) {
164
- return this._styleProcessor.getNormalized(name, this._styles);
165
- }
166
- /**
167
- * Returns a normalized style string. Styles are sorted by name.
168
- *
169
- * ```ts
170
- * styles.set( 'margin' , '1px' );
171
- * styles.set( 'background', '#f00' );
172
- *
173
- * styles.toString(); // -> 'background:#f00;margin:1px;'
174
- * ```
175
- *
176
- * **Note**: This method supports normalized styles if defined.
177
- *
178
- * ```ts
179
- * // Enable 'margin' shorthand processing:
180
- * editor.data.addStyleProcessorRules( addMarginRules );
181
- *
182
- * styles.set( 'margin' , '1px' );
183
- * styles.set( 'background', '#f00' );
184
- * styles.remove( 'margin-top' );
185
- * styles.remove( 'margin-right' );
186
- *
187
- * styles.toString(); // -> 'background:#f00;margin-bottom:1px;margin-left:1px;'
188
- * ```
189
- */
190
- toString() {
191
- if (this.isEmpty) {
192
- return '';
193
- }
194
- return this._getStylesEntries()
195
- .map(arr => arr.join(':'))
196
- .sort()
197
- .join(';') + ';';
198
- }
199
- /**
200
- * Returns property as a value string or undefined if property is not set.
201
- *
202
- * ```ts
203
- * // Enable 'margin' shorthand processing:
204
- * editor.data.addStyleProcessorRules( addMarginRules );
205
- *
206
- * const styles = new Styles();
207
- * styles.setTo( 'margin:1px;' );
208
- * styles.set( 'margin-bottom', '3em' );
209
- *
210
- * styles.getAsString( 'margin' ); // -> 'margin: 1px 1px 3em;'
211
- * ```
212
- *
213
- * Note, however, that all sub-values must be set for the longhand property name to return a value:
214
- *
215
- * ```ts
216
- * const styles = new Styles();
217
- * styles.setTo( 'margin:1px;' );
218
- * styles.remove( 'margin-bottom' );
219
- *
220
- * styles.getAsString( 'margin' ); // -> undefined
221
- * ```
222
- *
223
- * In the above scenario, it is not possible to return a `margin` value, so `undefined` is returned.
224
- * Instead, you should use:
225
- *
226
- * ```ts
227
- * const styles = new Styles();
228
- * styles.setTo( 'margin:1px;' );
229
- * styles.remove( 'margin-bottom' );
230
- *
231
- * for ( const styleName of styles.getStyleNames() ) {
232
- * console.log( styleName, styles.getAsString( styleName ) );
233
- * }
234
- * // 'margin-top', '1px'
235
- * // 'margin-right', '1px'
236
- * // 'margin-left', '1px'
237
- * ```
238
- *
239
- * In general, it is recommend to iterate over style names like in the example above. This way, you will always get all
240
- * the currently set style values. So, if all the 4 margin values would be set
241
- * the for-of loop above would yield only `'margin'`, `'1px'`:
242
- *
243
- * ```ts
244
- * const styles = new Styles();
245
- * styles.setTo( 'margin:1px;' );
246
- *
247
- * for ( const styleName of styles.getStyleNames() ) {
248
- * console.log( styleName, styles.getAsString( styleName ) );
249
- * }
250
- * // 'margin', '1px'
251
- * ```
252
- *
253
- * **Note**: To get a normalized version of a longhand property use the {@link #getNormalized `#getNormalized()`} method.
254
- */
255
- getAsString(propertyName) {
256
- if (this.isEmpty) {
257
- return;
258
- }
259
- if (this._styles[propertyName] && !isObject(this._styles[propertyName])) {
260
- // Try return styles set directly - values that are not parsed.
261
- return this._styles[propertyName];
262
- }
263
- const styles = this._styleProcessor.getReducedForm(propertyName, this._styles);
264
- const propertyDescriptor = styles.find(([property]) => property === propertyName);
265
- // Only return a value if it is set;
266
- if (Array.isArray(propertyDescriptor)) {
267
- return propertyDescriptor[1];
268
- }
269
- }
270
- /**
271
- * Returns all style properties names as they would appear when using {@link #toString `#toString()`}.
272
- *
273
- * When `expand` is set to true and there's a shorthand style property set, it will also return all equivalent styles:
274
- *
275
- * ```ts
276
- * stylesMap.setTo( 'margin: 1em' )
277
- * ```
278
- *
279
- * will be expanded to:
280
- *
281
- * ```ts
282
- * [ 'margin', 'margin-top', 'margin-right', 'margin-bottom', 'margin-left' ]
283
- * ```
284
- *
285
- * @param expand Expand shorthand style properties and all return equivalent style representations.
286
- */
287
- getStyleNames(expand = false) {
288
- if (this.isEmpty) {
289
- return [];
290
- }
291
- if (expand) {
292
- return this._styleProcessor.getStyleNames(this._styles);
293
- }
294
- const entries = this._getStylesEntries();
295
- return entries.map(([key]) => key);
296
- }
297
- /**
298
- * Removes all styles.
299
- */
300
- clear() {
301
- this._styles = {};
302
- }
303
- /**
304
- * Returns normalized styles entries for further processing.
305
- */
306
- _getStylesEntries() {
307
- const parsed = [];
308
- const keys = Object.keys(this._styles);
309
- for (const key of keys) {
310
- parsed.push(...this._styleProcessor.getReducedForm(key, this._styles));
311
- }
312
- return parsed;
313
- }
314
- /**
315
- * Removes empty objects upon removing an entry from internal object.
316
- */
317
- _cleanEmptyObjectsOnPath(path) {
318
- const pathParts = path.split('.');
319
- const isChildPath = pathParts.length > 1;
320
- if (!isChildPath) {
321
- return;
322
- }
323
- const parentPath = pathParts.splice(0, pathParts.length - 1).join('.');
324
- const parentObject = get(this._styles, parentPath);
325
- if (!parentObject) {
326
- return;
327
- }
328
- const isParentEmpty = !Array.from(Object.keys(parentObject)).length;
329
- if (isParentEmpty) {
330
- this.remove(parentPath);
331
- }
332
- }
333
- }
334
- /**
335
- * Style processor is responsible for writing and reading a normalized styles object.
336
- */
337
- export class StylesProcessor {
338
- /**
339
- * Creates StylesProcessor instance.
340
- *
341
- * @internal
342
- */
343
- constructor() {
344
- this._normalizers = new Map();
345
- this._extractors = new Map();
346
- this._reducers = new Map();
347
- this._consumables = new Map();
348
- }
349
- /**
350
- * Parse style string value to a normalized object and appends it to styles object.
351
- *
352
- * ```ts
353
- * const styles = {};
354
- *
355
- * stylesProcessor.toNormalizedForm( 'margin', '1px', styles );
356
- *
357
- * // styles will consist: { margin: { top: '1px', right: '1px', bottom: '1px', left: '1px; } }
358
- * ```
359
- *
360
- * **Note**: To define normalizer callbacks use {@link #setNormalizer}.
361
- *
362
- * @param name Name of style property.
363
- * @param propertyValue Value of style property.
364
- * @param styles Object holding normalized styles.
365
- */
366
- toNormalizedForm(name, propertyValue, styles) {
367
- if (isObject(propertyValue)) {
368
- appendStyleValue(styles, toPath(name), propertyValue);
369
- return;
370
- }
371
- if (this._normalizers.has(name)) {
372
- const normalizer = this._normalizers.get(name);
373
- const { path, value } = normalizer(propertyValue);
374
- appendStyleValue(styles, path, value);
375
- }
376
- else {
377
- appendStyleValue(styles, name, propertyValue);
378
- }
379
- }
380
- /**
381
- * Returns a normalized version of a style property.
382
- *
383
- * ```ts
384
- * const styles = {
385
- * margin: { top: '1px', right: '1px', bottom: '1px', left: '1px; },
386
- * background: { color: '#f00' }
387
- * };
388
- *
389
- * stylesProcessor.getNormalized( 'background' );
390
- * // will return: { color: '#f00' }
391
- *
392
- * stylesProcessor.getNormalized( 'margin-top' );
393
- * // will return: '1px'
394
- * ```
395
- *
396
- * **Note**: In some cases extracting single value requires defining an extractor callback {@link #setExtractor}.
397
- *
398
- * @param name Name of style property.
399
- * @param styles Object holding normalized styles.
400
- */
401
- getNormalized(name, styles) {
402
- if (!name) {
403
- return merge({}, styles);
404
- }
405
- // Might be empty string.
406
- if (styles[name] !== undefined) {
407
- return styles[name];
408
- }
409
- if (this._extractors.has(name)) {
410
- const extractor = this._extractors.get(name);
411
- if (typeof extractor === 'string') {
412
- return get(styles, extractor);
413
- }
414
- const value = extractor(name, styles);
415
- if (value) {
416
- return value;
417
- }
418
- }
419
- return get(styles, toPath(name));
420
- }
421
- /**
422
- * Returns a reduced form of style property form normalized object.
423
- *
424
- * For default margin reducer, the below code:
425
- *
426
- * ```ts
427
- * stylesProcessor.getReducedForm( 'margin', {
428
- * margin: { top: '1px', right: '1px', bottom: '2px', left: '1px; }
429
- * } );
430
- * ```
431
- *
432
- * will return:
433
- *
434
- * ```ts
435
- * [
436
- * [ 'margin', '1px 1px 2px' ]
437
- * ]
438
- * ```
439
- *
440
- * because it might be represented as a shorthand 'margin' value. However if one of margin long hand values is missing it should return:
441
- *
442
- * ```ts
443
- * [
444
- * [ 'margin-top', '1px' ],
445
- * [ 'margin-right', '1px' ],
446
- * [ 'margin-bottom', '2px' ]
447
- * // the 'left' value is missing - cannot use 'margin' shorthand.
448
- * ]
449
- * ```
450
- *
451
- * **Note**: To define reducer callbacks use {@link #setReducer}.
452
- *
453
- * @param name Name of style property.
454
- */
455
- getReducedForm(name, styles) {
456
- const normalizedValue = this.getNormalized(name, styles);
457
- // Might be empty string.
458
- if (normalizedValue === undefined) {
459
- return [];
460
- }
461
- if (this._reducers.has(name)) {
462
- const reducer = this._reducers.get(name);
463
- return reducer(normalizedValue);
464
- }
465
- return [[name, normalizedValue]];
466
- }
467
- /**
468
- * Return all style properties. Also expand shorthand properties (e.g. `margin`, `background`) if respective extractor is available.
469
- *
470
- * @param styles Object holding normalized styles.
471
- */
472
- getStyleNames(styles) {
473
- // Find all extractable styles that have a value.
474
- const expandedStyleNames = Array.from(this._consumables.keys()).filter(name => {
475
- const style = this.getNormalized(name, styles);
476
- if (style && typeof style == 'object') {
477
- return Object.keys(style).length;
478
- }
479
- return style;
480
- });
481
- // For simple styles (for example `color`) we don't have a map of those styles
482
- // but they are 1 to 1 with normalized object keys.
483
- const styleNamesKeysSet = new Set([
484
- ...expandedStyleNames,
485
- ...Object.keys(styles)
486
- ]);
487
- return Array.from(styleNamesKeysSet.values());
488
- }
489
- /**
490
- * Returns related style names.
491
- *
492
- * ```ts
493
- * stylesProcessor.getRelatedStyles( 'margin' );
494
- * // will return: [ 'margin-top', 'margin-right', 'margin-bottom', 'margin-left' ];
495
- *
496
- * stylesProcessor.getRelatedStyles( 'margin-top' );
497
- * // will return: [ 'margin' ];
498
- * ```
499
- *
500
- * **Note**: To define new style relations load an existing style processor or use
501
- * {@link module:engine/view/stylesmap~StylesProcessor#setStyleRelation `StylesProcessor.setStyleRelation()`}.
502
- */
503
- getRelatedStyles(name) {
504
- return this._consumables.get(name) || [];
505
- }
506
- /**
507
- * Adds a normalizer method for a style property.
508
- *
509
- * A normalizer returns describing how the value should be normalized.
510
- *
511
- * For instance 'margin' style is a shorthand for four margin values:
512
- *
513
- * - 'margin-top'
514
- * - 'margin-right'
515
- * - 'margin-bottom'
516
- * - 'margin-left'
517
- *
518
- * and can be written in various ways if some values are equal to others. For instance `'margin: 1px 2em;'` is a shorthand for
519
- * `'margin-top: 1px;margin-right: 2em;margin-bottom: 1px;margin-left: 2em'`.
520
- *
521
- * A normalizer should parse various margin notations as a single object:
522
- *
523
- * ```ts
524
- * const styles = {
525
- * margin: {
526
- * top: '1px',
527
- * right: '2em',
528
- * bottom: '1px',
529
- * left: '2em'
530
- * }
531
- * };
532
- * ```
533
- *
534
- * Thus a normalizer for 'margin' style should return an object defining style path and value to store:
535
- *
536
- * ```ts
537
- * const returnValue = {
538
- * path: 'margin',
539
- * value: {
540
- * top: '1px',
541
- * right: '2em',
542
- * bottom: '1px',
543
- * left: '2em'
544
- * }
545
- * };
546
- * ```
547
- *
548
- * Additionally to fully support all margin notations there should be also defined 4 normalizers for longhand margin notations. Below
549
- * is an example for 'margin-top' style property normalizer:
550
- *
551
- * ```ts
552
- * stylesProcessor.setNormalizer( 'margin-top', valueString => {
553
- * return {
554
- * path: 'margin.top',
555
- * value: valueString
556
- * }
557
- * } );
558
- * ```
559
- */
560
- setNormalizer(name, callback) {
561
- this._normalizers.set(name, callback);
562
- }
563
- /**
564
- * Adds a extractor callback for a style property.
565
- *
566
- * Most normalized style values are stored as one level objects. It is assumed that `'margin-top'` style will be stored as:
567
- *
568
- * ```ts
569
- * const styles = {
570
- * margin: {
571
- * top: 'value'
572
- * }
573
- * }
574
- * ```
575
- *
576
- * However, some styles can have conflicting notations and thus it might be harder to extract a style value from shorthand. For instance
577
- * the 'border-top-style' can be defined using `'border-top:solid'`, `'border-style:solid none none none'` or by `'border:solid'`
578
- * shorthands. The default border styles processors stores styles as:
579
- *
580
- * ```ts
581
- * const styles = {
582
- * border: {
583
- * style: {
584
- * top: 'solid'
585
- * }
586
- * }
587
- * }
588
- * ```
589
- *
590
- * as it is better to modify border style independently from other values. On the other part the output of the border might be
591
- * desired as `border-top`, `border-left`, etc notation.
592
- *
593
- * In the above example an extractor should return a side border value that combines style, color and width:
594
- *
595
- * ```ts
596
- * styleProcessor.setExtractor( 'border-top', styles => {
597
- * return {
598
- * color: styles.border.color.top,
599
- * style: styles.border.style.top,
600
- * width: styles.border.width.top
601
- * }
602
- * } );
603
- * ```
604
- *
605
- * @param callbackOrPath Callback that return a requested value or path string for single values.
606
- */
607
- setExtractor(name, callbackOrPath) {
608
- this._extractors.set(name, callbackOrPath);
609
- }
610
- /**
611
- * Adds a reducer callback for a style property.
612
- *
613
- * Reducer returns a minimal notation for given style name. For longhand properties it is not required to write a reducer as
614
- * by default the direct value from style path is taken.
615
- *
616
- * For shorthand styles a reducer should return minimal style notation either by returning single name-value tuple or multiple tuples
617
- * if a shorthand cannot be used. For instance for a margin shorthand a reducer might return:
618
- *
619
- * ```ts
620
- * const marginShortHandTuple = [
621
- * [ 'margin', '1px 1px 2px' ]
622
- * ];
623
- * ```
624
- *
625
- * or a longhand tuples for defined values:
626
- *
627
- * ```ts
628
- * // Considering margin.bottom and margin.left are undefined.
629
- * const marginLonghandsTuples = [
630
- * [ 'margin-top', '1px' ],
631
- * [ 'margin-right', '1px' ]
632
- * ];
633
- * ```
634
- *
635
- * A reducer obtains a normalized style value:
636
- *
637
- * ```ts
638
- * // Simplified reducer that always outputs 4 values which are always present:
639
- * stylesProcessor.setReducer( 'margin', margin => {
640
- * return [
641
- * [ 'margin', `${ margin.top } ${ margin.right } ${ margin.bottom } ${ margin.left }` ]
642
- * ]
643
- * } );
644
- * ```
645
- */
646
- setReducer(name, callback) {
647
- this._reducers.set(name, callback);
648
- }
649
- /**
650
- * Defines a style shorthand relation to other style notations.
651
- *
652
- * ```ts
653
- * stylesProcessor.setStyleRelation( 'margin', [
654
- * 'margin-top',
655
- * 'margin-right',
656
- * 'margin-bottom',
657
- * 'margin-left'
658
- * ] );
659
- * ```
660
- *
661
- * This enables expanding of style names for shorthands. For instance, if defined,
662
- * {@link module:engine/conversion/viewconsumable~ViewConsumable view consumable} items are automatically created
663
- * for long-hand margin style notation alongside the `'margin'` item.
664
- *
665
- * This means that when an element being converted has a style `margin`, a converter for `margin-left` will work just
666
- * fine since the view consumable will contain a consumable `margin-left` item (thanks to the relation) and
667
- * `element.getStyle( 'margin-left' )` will work as well assuming that the style processor was correctly configured.
668
- * However, once `margin-left` is consumed, `margin` will not be consumable anymore.
669
- */
670
- setStyleRelation(shorthandName, styleNames) {
671
- this._mapStyleNames(shorthandName, styleNames);
672
- for (const alsoName of styleNames) {
673
- this._mapStyleNames(alsoName, [shorthandName]);
674
- }
675
- }
676
- /**
677
- * Set two-way binding of style names.
678
- */
679
- _mapStyleNames(name, styleNames) {
680
- if (!this._consumables.has(name)) {
681
- this._consumables.set(name, []);
682
- }
683
- this._consumables.get(name).push(...styleNames);
684
- }
685
- }
686
- /**
687
- * Parses inline styles and puts property - value pairs into styles map.
688
- *
689
- * @param stylesString Styles to parse.
690
- * @returns Map of parsed properties and values.
691
- */
692
- function parseInlineStyles(stylesString) {
693
- // `null` if no quote was found in input string or last found quote was a closing quote. See below.
694
- let quoteType = null;
695
- let propertyNameStart = 0;
696
- let propertyValueStart = 0;
697
- let propertyName = null;
698
- const stylesMap = new Map();
699
- // Do not set anything if input string is empty.
700
- if (stylesString === '') {
701
- return stylesMap;
702
- }
703
- // Fix inline styles that do not end with `;` so they are compatible with algorithm below.
704
- if (stylesString.charAt(stylesString.length - 1) != ';') {
705
- stylesString = stylesString + ';';
706
- }
707
- // Seek the whole string for "special characters".
708
- for (let i = 0; i < stylesString.length; i++) {
709
- const char = stylesString.charAt(i);
710
- if (quoteType === null) {
711
- // No quote found yet or last found quote was a closing quote.
712
- switch (char) {
713
- case ':':
714
- // Most of time colon means that property name just ended.
715
- // Sometimes however `:` is found inside property value (for example in background image url).
716
- if (!propertyName) {
717
- // Treat this as end of property only if property name is not already saved.
718
- // Save property name.
719
- propertyName = stylesString.substr(propertyNameStart, i - propertyNameStart);
720
- // Save this point as the start of property value.
721
- propertyValueStart = i + 1;
722
- }
723
- break;
724
- case '"':
725
- case '\'':
726
- // Opening quote found (this is an opening quote, because `quoteType` is `null`).
727
- quoteType = char;
728
- break;
729
- case ';': {
730
- // Property value just ended.
731
- // Use previously stored property value start to obtain property value.
732
- const propertyValue = stylesString.substr(propertyValueStart, i - propertyValueStart);
733
- if (propertyName) {
734
- // Save parsed part.
735
- stylesMap.set(propertyName.trim(), propertyValue.trim());
736
- }
737
- propertyName = null;
738
- // Save this point as property name start. Property name starts immediately after previous property value ends.
739
- propertyNameStart = i + 1;
740
- break;
741
- }
742
- }
743
- }
744
- else if (char === quoteType) {
745
- // If a quote char is found and it is a closing quote, mark this fact by `null`-ing `quoteType`.
746
- quoteType = null;
747
- }
748
- }
749
- return stylesMap;
750
- }
751
- /**
752
- * Return lodash compatible path from style name.
753
- */
754
- function toPath(name) {
755
- return name.replace('-', '.');
756
- }
757
- /**
758
- * Appends style definition to the styles object.
759
- */
760
- function appendStyleValue(stylesObject, nameOrPath, valueOrObject) {
761
- let valueToSet = valueOrObject;
762
- if (isObject(valueOrObject)) {
763
- valueToSet = merge({}, get(stylesObject, nameOrPath), valueOrObject);
764
- }
765
- set(stylesObject, nameOrPath, valueToSet);
766
- }
1
+ /**
2
+ * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
+ * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
+ */
5
+ /**
6
+ * @module engine/view/stylesmap
7
+ */
8
+ import { get, isObject, merge, set, unset } from 'lodash-es';
9
+ /**
10
+ * Styles map. Allows handling (adding, removing, retrieving) a set of style rules (usually, of an element).
11
+ *
12
+ * The styles map is capable of normalizing style names so e.g. the following operations are possible:
13
+ */
14
+ export default class StylesMap {
15
+ /**
16
+ * Creates Styles instance.
17
+ */
18
+ constructor(styleProcessor) {
19
+ this._styles = {};
20
+ this._styleProcessor = styleProcessor;
21
+ }
22
+ /**
23
+ * Returns true if style map has no styles set.
24
+ */
25
+ get isEmpty() {
26
+ const entries = Object.entries(this._styles);
27
+ const from = Array.from(entries);
28
+ return !from.length;
29
+ }
30
+ /**
31
+ * Number of styles defined.
32
+ */
33
+ get size() {
34
+ if (this.isEmpty) {
35
+ return 0;
36
+ }
37
+ return this.getStyleNames().length;
38
+ }
39
+ /**
40
+ * Set styles map to a new value.
41
+ *
42
+ * ```ts
43
+ * styles.setTo( 'border:1px solid blue;margin-top:1px;' );
44
+ * ```
45
+ */
46
+ setTo(inlineStyle) {
47
+ this.clear();
48
+ const parsedStyles = Array.from(parseInlineStyles(inlineStyle).entries());
49
+ for (const [key, value] of parsedStyles) {
50
+ this._styleProcessor.toNormalizedForm(key, value, this._styles);
51
+ }
52
+ }
53
+ /**
54
+ * Checks if a given style is set.
55
+ *
56
+ * ```ts
57
+ * styles.setTo( 'margin-left:1px;' );
58
+ *
59
+ * styles.has( 'margin-left' ); // -> true
60
+ * styles.has( 'padding' ); // -> false
61
+ * ```
62
+ *
63
+ * **Note**: This check supports normalized style names.
64
+ *
65
+ * ```ts
66
+ * // Enable 'margin' shorthand processing:
67
+ * editor.data.addStyleProcessorRules( addMarginRules );
68
+ *
69
+ * styles.setTo( 'margin:2px;' );
70
+ *
71
+ * styles.has( 'margin' ); // -> true
72
+ * styles.has( 'margin-top' ); // -> true
73
+ * styles.has( 'margin-left' ); // -> true
74
+ *
75
+ * styles.remove( 'margin-top' );
76
+ *
77
+ * styles.has( 'margin' ); // -> false
78
+ * styles.has( 'margin-top' ); // -> false
79
+ * styles.has( 'margin-left' ); // -> true
80
+ * ```
81
+ *
82
+ * @param name Style name.
83
+ */
84
+ has(name) {
85
+ if (this.isEmpty) {
86
+ return false;
87
+ }
88
+ const styles = this._styleProcessor.getReducedForm(name, this._styles);
89
+ const propertyDescriptor = styles.find(([property]) => property === name);
90
+ // Only return a value if it is set;
91
+ return Array.isArray(propertyDescriptor);
92
+ }
93
+ set(nameOrObject, valueOrObject) {
94
+ if (isObject(nameOrObject)) {
95
+ for (const [key, value] of Object.entries(nameOrObject)) {
96
+ this._styleProcessor.toNormalizedForm(key, value, this._styles);
97
+ }
98
+ }
99
+ else {
100
+ this._styleProcessor.toNormalizedForm(nameOrObject, valueOrObject, this._styles);
101
+ }
102
+ }
103
+ /**
104
+ * Removes given style.
105
+ *
106
+ * ```ts
107
+ * styles.setTo( 'background:#f00;margin-right:2px;' );
108
+ *
109
+ * styles.remove( 'background' );
110
+ *
111
+ * styles.toString(); // -> 'margin-right:2px;'
112
+ * ```
113
+ *
114
+ * ***Note**:* This method uses {@link module:engine/controller/datacontroller~DataController#addStyleProcessorRules
115
+ * enabled style processor rules} to normalize passed values.
116
+ *
117
+ * ```ts
118
+ * // Enable 'margin' shorthand processing:
119
+ * editor.data.addStyleProcessorRules( addMarginRules );
120
+ *
121
+ * styles.setTo( 'margin:1px' );
122
+ *
123
+ * styles.remove( 'margin-top' );
124
+ * styles.remove( 'margin-right' );
125
+ *
126
+ * styles.toString(); // -> 'margin-bottom:1px;margin-left:1px;'
127
+ * ```
128
+ *
129
+ * @param name Style name.
130
+ */
131
+ remove(name) {
132
+ const path = toPath(name);
133
+ unset(this._styles, path);
134
+ delete this._styles[name];
135
+ this._cleanEmptyObjectsOnPath(path);
136
+ }
137
+ /**
138
+ * Returns a normalized style object or a single value.
139
+ *
140
+ * ```ts
141
+ * // Enable 'margin' shorthand processing:
142
+ * editor.data.addStyleProcessorRules( addMarginRules );
143
+ *
144
+ * const styles = new Styles();
145
+ * styles.setTo( 'margin:1px 2px 3em;' );
146
+ *
147
+ * styles.getNormalized( 'margin' );
148
+ * // will log:
149
+ * // {
150
+ * // top: '1px',
151
+ * // right: '2px',
152
+ * // bottom: '3em',
153
+ * // left: '2px' // normalized value from margin shorthand
154
+ * // }
155
+ *
156
+ * styles.getNormalized( 'margin-left' ); // -> '2px'
157
+ * ```
158
+ *
159
+ * **Note**: This method will only return normalized styles if a style processor was defined.
160
+ *
161
+ * @param name Style name.
162
+ */
163
+ getNormalized(name) {
164
+ return this._styleProcessor.getNormalized(name, this._styles);
165
+ }
166
+ /**
167
+ * Returns a normalized style string. Styles are sorted by name.
168
+ *
169
+ * ```ts
170
+ * styles.set( 'margin' , '1px' );
171
+ * styles.set( 'background', '#f00' );
172
+ *
173
+ * styles.toString(); // -> 'background:#f00;margin:1px;'
174
+ * ```
175
+ *
176
+ * **Note**: This method supports normalized styles if defined.
177
+ *
178
+ * ```ts
179
+ * // Enable 'margin' shorthand processing:
180
+ * editor.data.addStyleProcessorRules( addMarginRules );
181
+ *
182
+ * styles.set( 'margin' , '1px' );
183
+ * styles.set( 'background', '#f00' );
184
+ * styles.remove( 'margin-top' );
185
+ * styles.remove( 'margin-right' );
186
+ *
187
+ * styles.toString(); // -> 'background:#f00;margin-bottom:1px;margin-left:1px;'
188
+ * ```
189
+ */
190
+ toString() {
191
+ if (this.isEmpty) {
192
+ return '';
193
+ }
194
+ return this._getStylesEntries()
195
+ .map(arr => arr.join(':'))
196
+ .sort()
197
+ .join(';') + ';';
198
+ }
199
+ /**
200
+ * Returns property as a value string or undefined if property is not set.
201
+ *
202
+ * ```ts
203
+ * // Enable 'margin' shorthand processing:
204
+ * editor.data.addStyleProcessorRules( addMarginRules );
205
+ *
206
+ * const styles = new Styles();
207
+ * styles.setTo( 'margin:1px;' );
208
+ * styles.set( 'margin-bottom', '3em' );
209
+ *
210
+ * styles.getAsString( 'margin' ); // -> 'margin: 1px 1px 3em;'
211
+ * ```
212
+ *
213
+ * Note, however, that all sub-values must be set for the longhand property name to return a value:
214
+ *
215
+ * ```ts
216
+ * const styles = new Styles();
217
+ * styles.setTo( 'margin:1px;' );
218
+ * styles.remove( 'margin-bottom' );
219
+ *
220
+ * styles.getAsString( 'margin' ); // -> undefined
221
+ * ```
222
+ *
223
+ * In the above scenario, it is not possible to return a `margin` value, so `undefined` is returned.
224
+ * Instead, you should use:
225
+ *
226
+ * ```ts
227
+ * const styles = new Styles();
228
+ * styles.setTo( 'margin:1px;' );
229
+ * styles.remove( 'margin-bottom' );
230
+ *
231
+ * for ( const styleName of styles.getStyleNames() ) {
232
+ * console.log( styleName, styles.getAsString( styleName ) );
233
+ * }
234
+ * // 'margin-top', '1px'
235
+ * // 'margin-right', '1px'
236
+ * // 'margin-left', '1px'
237
+ * ```
238
+ *
239
+ * In general, it is recommend to iterate over style names like in the example above. This way, you will always get all
240
+ * the currently set style values. So, if all the 4 margin values would be set
241
+ * the for-of loop above would yield only `'margin'`, `'1px'`:
242
+ *
243
+ * ```ts
244
+ * const styles = new Styles();
245
+ * styles.setTo( 'margin:1px;' );
246
+ *
247
+ * for ( const styleName of styles.getStyleNames() ) {
248
+ * console.log( styleName, styles.getAsString( styleName ) );
249
+ * }
250
+ * // 'margin', '1px'
251
+ * ```
252
+ *
253
+ * **Note**: To get a normalized version of a longhand property use the {@link #getNormalized `#getNormalized()`} method.
254
+ */
255
+ getAsString(propertyName) {
256
+ if (this.isEmpty) {
257
+ return;
258
+ }
259
+ if (this._styles[propertyName] && !isObject(this._styles[propertyName])) {
260
+ // Try return styles set directly - values that are not parsed.
261
+ return this._styles[propertyName];
262
+ }
263
+ const styles = this._styleProcessor.getReducedForm(propertyName, this._styles);
264
+ const propertyDescriptor = styles.find(([property]) => property === propertyName);
265
+ // Only return a value if it is set;
266
+ if (Array.isArray(propertyDescriptor)) {
267
+ return propertyDescriptor[1];
268
+ }
269
+ }
270
+ /**
271
+ * Returns all style properties names as they would appear when using {@link #toString `#toString()`}.
272
+ *
273
+ * When `expand` is set to true and there's a shorthand style property set, it will also return all equivalent styles:
274
+ *
275
+ * ```ts
276
+ * stylesMap.setTo( 'margin: 1em' )
277
+ * ```
278
+ *
279
+ * will be expanded to:
280
+ *
281
+ * ```ts
282
+ * [ 'margin', 'margin-top', 'margin-right', 'margin-bottom', 'margin-left' ]
283
+ * ```
284
+ *
285
+ * @param expand Expand shorthand style properties and all return equivalent style representations.
286
+ */
287
+ getStyleNames(expand = false) {
288
+ if (this.isEmpty) {
289
+ return [];
290
+ }
291
+ if (expand) {
292
+ return this._styleProcessor.getStyleNames(this._styles);
293
+ }
294
+ const entries = this._getStylesEntries();
295
+ return entries.map(([key]) => key);
296
+ }
297
+ /**
298
+ * Removes all styles.
299
+ */
300
+ clear() {
301
+ this._styles = {};
302
+ }
303
+ /**
304
+ * Returns normalized styles entries for further processing.
305
+ */
306
+ _getStylesEntries() {
307
+ const parsed = [];
308
+ const keys = Object.keys(this._styles);
309
+ for (const key of keys) {
310
+ parsed.push(...this._styleProcessor.getReducedForm(key, this._styles));
311
+ }
312
+ return parsed;
313
+ }
314
+ /**
315
+ * Removes empty objects upon removing an entry from internal object.
316
+ */
317
+ _cleanEmptyObjectsOnPath(path) {
318
+ const pathParts = path.split('.');
319
+ const isChildPath = pathParts.length > 1;
320
+ if (!isChildPath) {
321
+ return;
322
+ }
323
+ const parentPath = pathParts.splice(0, pathParts.length - 1).join('.');
324
+ const parentObject = get(this._styles, parentPath);
325
+ if (!parentObject) {
326
+ return;
327
+ }
328
+ const isParentEmpty = !Array.from(Object.keys(parentObject)).length;
329
+ if (isParentEmpty) {
330
+ this.remove(parentPath);
331
+ }
332
+ }
333
+ }
334
+ /**
335
+ * Style processor is responsible for writing and reading a normalized styles object.
336
+ */
337
+ export class StylesProcessor {
338
+ /**
339
+ * Creates StylesProcessor instance.
340
+ *
341
+ * @internal
342
+ */
343
+ constructor() {
344
+ this._normalizers = new Map();
345
+ this._extractors = new Map();
346
+ this._reducers = new Map();
347
+ this._consumables = new Map();
348
+ }
349
+ /**
350
+ * Parse style string value to a normalized object and appends it to styles object.
351
+ *
352
+ * ```ts
353
+ * const styles = {};
354
+ *
355
+ * stylesProcessor.toNormalizedForm( 'margin', '1px', styles );
356
+ *
357
+ * // styles will consist: { margin: { top: '1px', right: '1px', bottom: '1px', left: '1px; } }
358
+ * ```
359
+ *
360
+ * **Note**: To define normalizer callbacks use {@link #setNormalizer}.
361
+ *
362
+ * @param name Name of style property.
363
+ * @param propertyValue Value of style property.
364
+ * @param styles Object holding normalized styles.
365
+ */
366
+ toNormalizedForm(name, propertyValue, styles) {
367
+ if (isObject(propertyValue)) {
368
+ appendStyleValue(styles, toPath(name), propertyValue);
369
+ return;
370
+ }
371
+ if (this._normalizers.has(name)) {
372
+ const normalizer = this._normalizers.get(name);
373
+ const { path, value } = normalizer(propertyValue);
374
+ appendStyleValue(styles, path, value);
375
+ }
376
+ else {
377
+ appendStyleValue(styles, name, propertyValue);
378
+ }
379
+ }
380
+ /**
381
+ * Returns a normalized version of a style property.
382
+ *
383
+ * ```ts
384
+ * const styles = {
385
+ * margin: { top: '1px', right: '1px', bottom: '1px', left: '1px; },
386
+ * background: { color: '#f00' }
387
+ * };
388
+ *
389
+ * stylesProcessor.getNormalized( 'background' );
390
+ * // will return: { color: '#f00' }
391
+ *
392
+ * stylesProcessor.getNormalized( 'margin-top' );
393
+ * // will return: '1px'
394
+ * ```
395
+ *
396
+ * **Note**: In some cases extracting single value requires defining an extractor callback {@link #setExtractor}.
397
+ *
398
+ * @param name Name of style property.
399
+ * @param styles Object holding normalized styles.
400
+ */
401
+ getNormalized(name, styles) {
402
+ if (!name) {
403
+ return merge({}, styles);
404
+ }
405
+ // Might be empty string.
406
+ if (styles[name] !== undefined) {
407
+ return styles[name];
408
+ }
409
+ if (this._extractors.has(name)) {
410
+ const extractor = this._extractors.get(name);
411
+ if (typeof extractor === 'string') {
412
+ return get(styles, extractor);
413
+ }
414
+ const value = extractor(name, styles);
415
+ if (value) {
416
+ return value;
417
+ }
418
+ }
419
+ return get(styles, toPath(name));
420
+ }
421
+ /**
422
+ * Returns a reduced form of style property form normalized object.
423
+ *
424
+ * For default margin reducer, the below code:
425
+ *
426
+ * ```ts
427
+ * stylesProcessor.getReducedForm( 'margin', {
428
+ * margin: { top: '1px', right: '1px', bottom: '2px', left: '1px; }
429
+ * } );
430
+ * ```
431
+ *
432
+ * will return:
433
+ *
434
+ * ```ts
435
+ * [
436
+ * [ 'margin', '1px 1px 2px' ]
437
+ * ]
438
+ * ```
439
+ *
440
+ * because it might be represented as a shorthand 'margin' value. However if one of margin long hand values is missing it should return:
441
+ *
442
+ * ```ts
443
+ * [
444
+ * [ 'margin-top', '1px' ],
445
+ * [ 'margin-right', '1px' ],
446
+ * [ 'margin-bottom', '2px' ]
447
+ * // the 'left' value is missing - cannot use 'margin' shorthand.
448
+ * ]
449
+ * ```
450
+ *
451
+ * **Note**: To define reducer callbacks use {@link #setReducer}.
452
+ *
453
+ * @param name Name of style property.
454
+ */
455
+ getReducedForm(name, styles) {
456
+ const normalizedValue = this.getNormalized(name, styles);
457
+ // Might be empty string.
458
+ if (normalizedValue === undefined) {
459
+ return [];
460
+ }
461
+ if (this._reducers.has(name)) {
462
+ const reducer = this._reducers.get(name);
463
+ return reducer(normalizedValue);
464
+ }
465
+ return [[name, normalizedValue]];
466
+ }
467
+ /**
468
+ * Return all style properties. Also expand shorthand properties (e.g. `margin`, `background`) if respective extractor is available.
469
+ *
470
+ * @param styles Object holding normalized styles.
471
+ */
472
+ getStyleNames(styles) {
473
+ // Find all extractable styles that have a value.
474
+ const expandedStyleNames = Array.from(this._consumables.keys()).filter(name => {
475
+ const style = this.getNormalized(name, styles);
476
+ if (style && typeof style == 'object') {
477
+ return Object.keys(style).length;
478
+ }
479
+ return style;
480
+ });
481
+ // For simple styles (for example `color`) we don't have a map of those styles
482
+ // but they are 1 to 1 with normalized object keys.
483
+ const styleNamesKeysSet = new Set([
484
+ ...expandedStyleNames,
485
+ ...Object.keys(styles)
486
+ ]);
487
+ return Array.from(styleNamesKeysSet.values());
488
+ }
489
+ /**
490
+ * Returns related style names.
491
+ *
492
+ * ```ts
493
+ * stylesProcessor.getRelatedStyles( 'margin' );
494
+ * // will return: [ 'margin-top', 'margin-right', 'margin-bottom', 'margin-left' ];
495
+ *
496
+ * stylesProcessor.getRelatedStyles( 'margin-top' );
497
+ * // will return: [ 'margin' ];
498
+ * ```
499
+ *
500
+ * **Note**: To define new style relations load an existing style processor or use
501
+ * {@link module:engine/view/stylesmap~StylesProcessor#setStyleRelation `StylesProcessor.setStyleRelation()`}.
502
+ */
503
+ getRelatedStyles(name) {
504
+ return this._consumables.get(name) || [];
505
+ }
506
+ /**
507
+ * Adds a normalizer method for a style property.
508
+ *
509
+ * A normalizer returns describing how the value should be normalized.
510
+ *
511
+ * For instance 'margin' style is a shorthand for four margin values:
512
+ *
513
+ * - 'margin-top'
514
+ * - 'margin-right'
515
+ * - 'margin-bottom'
516
+ * - 'margin-left'
517
+ *
518
+ * and can be written in various ways if some values are equal to others. For instance `'margin: 1px 2em;'` is a shorthand for
519
+ * `'margin-top: 1px;margin-right: 2em;margin-bottom: 1px;margin-left: 2em'`.
520
+ *
521
+ * A normalizer should parse various margin notations as a single object:
522
+ *
523
+ * ```ts
524
+ * const styles = {
525
+ * margin: {
526
+ * top: '1px',
527
+ * right: '2em',
528
+ * bottom: '1px',
529
+ * left: '2em'
530
+ * }
531
+ * };
532
+ * ```
533
+ *
534
+ * Thus a normalizer for 'margin' style should return an object defining style path and value to store:
535
+ *
536
+ * ```ts
537
+ * const returnValue = {
538
+ * path: 'margin',
539
+ * value: {
540
+ * top: '1px',
541
+ * right: '2em',
542
+ * bottom: '1px',
543
+ * left: '2em'
544
+ * }
545
+ * };
546
+ * ```
547
+ *
548
+ * Additionally to fully support all margin notations there should be also defined 4 normalizers for longhand margin notations. Below
549
+ * is an example for 'margin-top' style property normalizer:
550
+ *
551
+ * ```ts
552
+ * stylesProcessor.setNormalizer( 'margin-top', valueString => {
553
+ * return {
554
+ * path: 'margin.top',
555
+ * value: valueString
556
+ * }
557
+ * } );
558
+ * ```
559
+ */
560
+ setNormalizer(name, callback) {
561
+ this._normalizers.set(name, callback);
562
+ }
563
+ /**
564
+ * Adds a extractor callback for a style property.
565
+ *
566
+ * Most normalized style values are stored as one level objects. It is assumed that `'margin-top'` style will be stored as:
567
+ *
568
+ * ```ts
569
+ * const styles = {
570
+ * margin: {
571
+ * top: 'value'
572
+ * }
573
+ * }
574
+ * ```
575
+ *
576
+ * However, some styles can have conflicting notations and thus it might be harder to extract a style value from shorthand. For instance
577
+ * the 'border-top-style' can be defined using `'border-top:solid'`, `'border-style:solid none none none'` or by `'border:solid'`
578
+ * shorthands. The default border styles processors stores styles as:
579
+ *
580
+ * ```ts
581
+ * const styles = {
582
+ * border: {
583
+ * style: {
584
+ * top: 'solid'
585
+ * }
586
+ * }
587
+ * }
588
+ * ```
589
+ *
590
+ * as it is better to modify border style independently from other values. On the other part the output of the border might be
591
+ * desired as `border-top`, `border-left`, etc notation.
592
+ *
593
+ * In the above example an extractor should return a side border value that combines style, color and width:
594
+ *
595
+ * ```ts
596
+ * styleProcessor.setExtractor( 'border-top', styles => {
597
+ * return {
598
+ * color: styles.border.color.top,
599
+ * style: styles.border.style.top,
600
+ * width: styles.border.width.top
601
+ * }
602
+ * } );
603
+ * ```
604
+ *
605
+ * @param callbackOrPath Callback that return a requested value or path string for single values.
606
+ */
607
+ setExtractor(name, callbackOrPath) {
608
+ this._extractors.set(name, callbackOrPath);
609
+ }
610
+ /**
611
+ * Adds a reducer callback for a style property.
612
+ *
613
+ * Reducer returns a minimal notation for given style name. For longhand properties it is not required to write a reducer as
614
+ * by default the direct value from style path is taken.
615
+ *
616
+ * For shorthand styles a reducer should return minimal style notation either by returning single name-value tuple or multiple tuples
617
+ * if a shorthand cannot be used. For instance for a margin shorthand a reducer might return:
618
+ *
619
+ * ```ts
620
+ * const marginShortHandTuple = [
621
+ * [ 'margin', '1px 1px 2px' ]
622
+ * ];
623
+ * ```
624
+ *
625
+ * or a longhand tuples for defined values:
626
+ *
627
+ * ```ts
628
+ * // Considering margin.bottom and margin.left are undefined.
629
+ * const marginLonghandsTuples = [
630
+ * [ 'margin-top', '1px' ],
631
+ * [ 'margin-right', '1px' ]
632
+ * ];
633
+ * ```
634
+ *
635
+ * A reducer obtains a normalized style value:
636
+ *
637
+ * ```ts
638
+ * // Simplified reducer that always outputs 4 values which are always present:
639
+ * stylesProcessor.setReducer( 'margin', margin => {
640
+ * return [
641
+ * [ 'margin', `${ margin.top } ${ margin.right } ${ margin.bottom } ${ margin.left }` ]
642
+ * ]
643
+ * } );
644
+ * ```
645
+ */
646
+ setReducer(name, callback) {
647
+ this._reducers.set(name, callback);
648
+ }
649
+ /**
650
+ * Defines a style shorthand relation to other style notations.
651
+ *
652
+ * ```ts
653
+ * stylesProcessor.setStyleRelation( 'margin', [
654
+ * 'margin-top',
655
+ * 'margin-right',
656
+ * 'margin-bottom',
657
+ * 'margin-left'
658
+ * ] );
659
+ * ```
660
+ *
661
+ * This enables expanding of style names for shorthands. For instance, if defined,
662
+ * {@link module:engine/conversion/viewconsumable~ViewConsumable view consumable} items are automatically created
663
+ * for long-hand margin style notation alongside the `'margin'` item.
664
+ *
665
+ * This means that when an element being converted has a style `margin`, a converter for `margin-left` will work just
666
+ * fine since the view consumable will contain a consumable `margin-left` item (thanks to the relation) and
667
+ * `element.getStyle( 'margin-left' )` will work as well assuming that the style processor was correctly configured.
668
+ * However, once `margin-left` is consumed, `margin` will not be consumable anymore.
669
+ */
670
+ setStyleRelation(shorthandName, styleNames) {
671
+ this._mapStyleNames(shorthandName, styleNames);
672
+ for (const alsoName of styleNames) {
673
+ this._mapStyleNames(alsoName, [shorthandName]);
674
+ }
675
+ }
676
+ /**
677
+ * Set two-way binding of style names.
678
+ */
679
+ _mapStyleNames(name, styleNames) {
680
+ if (!this._consumables.has(name)) {
681
+ this._consumables.set(name, []);
682
+ }
683
+ this._consumables.get(name).push(...styleNames);
684
+ }
685
+ }
686
+ /**
687
+ * Parses inline styles and puts property - value pairs into styles map.
688
+ *
689
+ * @param stylesString Styles to parse.
690
+ * @returns Map of parsed properties and values.
691
+ */
692
+ function parseInlineStyles(stylesString) {
693
+ // `null` if no quote was found in input string or last found quote was a closing quote. See below.
694
+ let quoteType = null;
695
+ let propertyNameStart = 0;
696
+ let propertyValueStart = 0;
697
+ let propertyName = null;
698
+ const stylesMap = new Map();
699
+ // Do not set anything if input string is empty.
700
+ if (stylesString === '') {
701
+ return stylesMap;
702
+ }
703
+ // Fix inline styles that do not end with `;` so they are compatible with algorithm below.
704
+ if (stylesString.charAt(stylesString.length - 1) != ';') {
705
+ stylesString = stylesString + ';';
706
+ }
707
+ // Seek the whole string for "special characters".
708
+ for (let i = 0; i < stylesString.length; i++) {
709
+ const char = stylesString.charAt(i);
710
+ if (quoteType === null) {
711
+ // No quote found yet or last found quote was a closing quote.
712
+ switch (char) {
713
+ case ':':
714
+ // Most of time colon means that property name just ended.
715
+ // Sometimes however `:` is found inside property value (for example in background image url).
716
+ if (!propertyName) {
717
+ // Treat this as end of property only if property name is not already saved.
718
+ // Save property name.
719
+ propertyName = stylesString.substr(propertyNameStart, i - propertyNameStart);
720
+ // Save this point as the start of property value.
721
+ propertyValueStart = i + 1;
722
+ }
723
+ break;
724
+ case '"':
725
+ case '\'':
726
+ // Opening quote found (this is an opening quote, because `quoteType` is `null`).
727
+ quoteType = char;
728
+ break;
729
+ case ';': {
730
+ // Property value just ended.
731
+ // Use previously stored property value start to obtain property value.
732
+ const propertyValue = stylesString.substr(propertyValueStart, i - propertyValueStart);
733
+ if (propertyName) {
734
+ // Save parsed part.
735
+ stylesMap.set(propertyName.trim(), propertyValue.trim());
736
+ }
737
+ propertyName = null;
738
+ // Save this point as property name start. Property name starts immediately after previous property value ends.
739
+ propertyNameStart = i + 1;
740
+ break;
741
+ }
742
+ }
743
+ }
744
+ else if (char === quoteType) {
745
+ // If a quote char is found and it is a closing quote, mark this fact by `null`-ing `quoteType`.
746
+ quoteType = null;
747
+ }
748
+ }
749
+ return stylesMap;
750
+ }
751
+ /**
752
+ * Return lodash compatible path from style name.
753
+ */
754
+ function toPath(name) {
755
+ return name.replace('-', '.');
756
+ }
757
+ /**
758
+ * Appends style definition to the styles object.
759
+ */
760
+ function appendStyleValue(stylesObject, nameOrPath, valueOrObject) {
761
+ let valueToSet = valueOrObject;
762
+ if (isObject(valueOrObject)) {
763
+ valueToSet = merge({}, get(stylesObject, nameOrPath), valueOrObject);
764
+ }
765
+ set(stylesObject, nameOrPath, valueToSet);
766
+ }