@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.
- package/LICENSE.md +1 -1
- package/{src → dist}/engineconfig.d.ts +6 -15
- package/dist/index-editor.css +38 -15
- package/dist/index.css +37 -37
- package/dist/index.css.map +1 -1
- package/{src → dist}/index.d.ts +0 -1
- package/dist/index.js +588 -94
- package/dist/index.js.map +1 -1
- package/{src → dist}/model/model.d.ts +10 -4
- package/{src → dist}/model/selection.d.ts +1 -1
- package/{src → dist}/view/downcastwriter.d.ts +3 -2
- package/{src → dist}/view/element.d.ts +2 -2
- package/{src → dist}/view/matcher.d.ts +4 -2
- package/dist/view/styles/background.d.ts +18 -0
- package/{src → dist}/view/styles/border.d.ts +0 -12
- package/{src → dist}/view/styles/margin.d.ts +0 -13
- package/{src → dist}/view/styles/padding.d.ts +0 -13
- package/{src → dist}/view/styles/utils.d.ts +12 -0
- package/package.json +20 -39
- package/src/controller/datacontroller.js +0 -522
- package/src/controller/editingcontroller.js +0 -181
- package/src/conversion/conversion.js +0 -606
- package/src/conversion/conversionhelpers.js +0 -33
- package/src/conversion/downcastdispatcher.js +0 -563
- package/src/conversion/downcasthelpers.js +0 -2160
- package/src/conversion/mapper.js +0 -1050
- package/src/conversion/modelconsumable.js +0 -331
- package/src/conversion/upcastdispatcher.js +0 -470
- package/src/conversion/upcasthelpers.js +0 -952
- package/src/conversion/viewconsumable.js +0 -541
- package/src/dataprocessor/basichtmlwriter.js +0 -22
- package/src/dataprocessor/dataprocessor.js +0 -5
- package/src/dataprocessor/htmldataprocessor.js +0 -107
- package/src/dataprocessor/htmlwriter.js +0 -5
- package/src/dataprocessor/xmldataprocessor.js +0 -127
- package/src/dev-utils/model.js +0 -396
- package/src/dev-utils/operationreplayer.js +0 -116
- package/src/dev-utils/utils.js +0 -122
- package/src/dev-utils/view.js +0 -990
- package/src/engineconfig.js +0 -5
- package/src/index.js +0 -134
- package/src/legacyerrors.js +0 -17
- package/src/model/batch.js +0 -98
- package/src/model/differ.js +0 -1288
- package/src/model/document.js +0 -398
- package/src/model/documentfragment.js +0 -332
- package/src/model/documentselection.js +0 -1026
- package/src/model/element.js +0 -323
- package/src/model/history.js +0 -206
- package/src/model/item.js +0 -5
- package/src/model/liveposition.js +0 -93
- package/src/model/liverange.js +0 -121
- package/src/model/markercollection.js +0 -436
- package/src/model/model.js +0 -866
- package/src/model/node.js +0 -371
- package/src/model/nodelist.js +0 -244
- package/src/model/operation/attributeoperation.js +0 -172
- package/src/model/operation/detachoperation.js +0 -87
- package/src/model/operation/insertoperation.js +0 -153
- package/src/model/operation/markeroperation.js +0 -136
- package/src/model/operation/mergeoperation.js +0 -184
- package/src/model/operation/moveoperation.js +0 -179
- package/src/model/operation/nooperation.js +0 -48
- package/src/model/operation/operation.js +0 -78
- package/src/model/operation/operationfactory.js +0 -44
- package/src/model/operation/renameoperation.js +0 -128
- package/src/model/operation/rootattributeoperation.js +0 -173
- package/src/model/operation/rootoperation.js +0 -106
- package/src/model/operation/splitoperation.js +0 -214
- package/src/model/operation/transform.js +0 -2211
- package/src/model/operation/utils.js +0 -217
- package/src/model/position.js +0 -1041
- package/src/model/range.js +0 -880
- package/src/model/rootelement.js +0 -82
- package/src/model/schema.js +0 -1542
- package/src/model/selection.js +0 -814
- package/src/model/text.js +0 -92
- package/src/model/textproxy.js +0 -202
- package/src/model/treewalker.js +0 -313
- package/src/model/typecheckable.js +0 -16
- package/src/model/utils/autoparagraphing.js +0 -63
- package/src/model/utils/deletecontent.js +0 -509
- package/src/model/utils/getselectedcontent.js +0 -126
- package/src/model/utils/insertcontent.js +0 -750
- package/src/model/utils/insertobject.js +0 -135
- package/src/model/utils/modifyselection.js +0 -187
- package/src/model/utils/selection-post-fixer.js +0 -264
- package/src/model/writer.js +0 -1318
- package/src/view/attributeelement.js +0 -220
- package/src/view/containerelement.js +0 -91
- package/src/view/datatransfer.js +0 -106
- package/src/view/document.js +0 -139
- package/src/view/documentfragment.js +0 -251
- package/src/view/documentselection.js +0 -270
- package/src/view/domconverter.js +0 -1661
- package/src/view/downcastwriter.js +0 -1589
- package/src/view/editableelement.js +0 -74
- package/src/view/element.js +0 -1053
- package/src/view/elementdefinition.js +0 -5
- package/src/view/emptyelement.js +0 -83
- package/src/view/filler.js +0 -161
- package/src/view/item.js +0 -5
- package/src/view/matcher.js +0 -437
- package/src/view/node.js +0 -238
- package/src/view/observer/arrowkeysobserver.js +0 -40
- package/src/view/observer/bubblingemittermixin.js +0 -215
- package/src/view/observer/bubblingeventinfo.js +0 -49
- package/src/view/observer/clickobserver.js +0 -26
- package/src/view/observer/compositionobserver.js +0 -64
- package/src/view/observer/domeventdata.js +0 -63
- package/src/view/observer/domeventobserver.js +0 -81
- package/src/view/observer/fakeselectionobserver.js +0 -95
- package/src/view/observer/focusobserver.js +0 -166
- package/src/view/observer/inputobserver.js +0 -236
- package/src/view/observer/keyobserver.js +0 -36
- package/src/view/observer/mouseobserver.js +0 -26
- package/src/view/observer/mutationobserver.js +0 -219
- package/src/view/observer/observer.js +0 -92
- package/src/view/observer/pointerobserver.js +0 -26
- package/src/view/observer/selectionobserver.js +0 -318
- package/src/view/observer/tabobserver.js +0 -42
- package/src/view/observer/touchobserver.js +0 -26
- package/src/view/placeholder.js +0 -285
- package/src/view/position.js +0 -341
- package/src/view/range.js +0 -451
- package/src/view/rawelement.js +0 -115
- package/src/view/renderer.js +0 -1148
- package/src/view/rooteditableelement.js +0 -78
- package/src/view/selection.js +0 -594
- package/src/view/styles/background.d.ts +0 -33
- package/src/view/styles/background.js +0 -74
- package/src/view/styles/border.js +0 -316
- package/src/view/styles/margin.js +0 -34
- package/src/view/styles/padding.js +0 -34
- package/src/view/styles/utils.js +0 -219
- package/src/view/stylesmap.js +0 -941
- package/src/view/text.js +0 -110
- package/src/view/textproxy.js +0 -136
- package/src/view/tokenlist.js +0 -194
- package/src/view/treewalker.js +0 -389
- package/src/view/typecheckable.js +0 -19
- package/src/view/uielement.js +0 -194
- package/src/view/upcastwriter.js +0 -363
- package/src/view/view.js +0 -579
- package/theme/placeholder.css +0 -36
- package/theme/renderer.css +0 -9
- /package/{src → dist}/controller/datacontroller.d.ts +0 -0
- /package/{src → dist}/controller/editingcontroller.d.ts +0 -0
- /package/{src → dist}/conversion/conversion.d.ts +0 -0
- /package/{src → dist}/conversion/conversionhelpers.d.ts +0 -0
- /package/{src → dist}/conversion/downcastdispatcher.d.ts +0 -0
- /package/{src → dist}/conversion/downcasthelpers.d.ts +0 -0
- /package/{src → dist}/conversion/mapper.d.ts +0 -0
- /package/{src → dist}/conversion/modelconsumable.d.ts +0 -0
- /package/{src → dist}/conversion/upcastdispatcher.d.ts +0 -0
- /package/{src → dist}/conversion/upcasthelpers.d.ts +0 -0
- /package/{src → dist}/conversion/viewconsumable.d.ts +0 -0
- /package/{src → dist}/dataprocessor/basichtmlwriter.d.ts +0 -0
- /package/{src → dist}/dataprocessor/dataprocessor.d.ts +0 -0
- /package/{src → dist}/dataprocessor/htmldataprocessor.d.ts +0 -0
- /package/{src → dist}/dataprocessor/htmlwriter.d.ts +0 -0
- /package/{src → dist}/dataprocessor/xmldataprocessor.d.ts +0 -0
- /package/{src → dist}/dev-utils/model.d.ts +0 -0
- /package/{src → dist}/dev-utils/operationreplayer.d.ts +0 -0
- /package/{src → dist}/dev-utils/utils.d.ts +0 -0
- /package/{src → dist}/dev-utils/view.d.ts +0 -0
- /package/{src → dist}/legacyerrors.d.ts +0 -0
- /package/{src → dist}/model/batch.d.ts +0 -0
- /package/{src → dist}/model/differ.d.ts +0 -0
- /package/{src → dist}/model/document.d.ts +0 -0
- /package/{src → dist}/model/documentfragment.d.ts +0 -0
- /package/{src → dist}/model/documentselection.d.ts +0 -0
- /package/{src → dist}/model/element.d.ts +0 -0
- /package/{src → dist}/model/history.d.ts +0 -0
- /package/{src → dist}/model/item.d.ts +0 -0
- /package/{src → dist}/model/liveposition.d.ts +0 -0
- /package/{src → dist}/model/liverange.d.ts +0 -0
- /package/{src → dist}/model/markercollection.d.ts +0 -0
- /package/{src → dist}/model/node.d.ts +0 -0
- /package/{src → dist}/model/nodelist.d.ts +0 -0
- /package/{src → dist}/model/operation/attributeoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/detachoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/insertoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/markeroperation.d.ts +0 -0
- /package/{src → dist}/model/operation/mergeoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/moveoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/nooperation.d.ts +0 -0
- /package/{src → dist}/model/operation/operation.d.ts +0 -0
- /package/{src → dist}/model/operation/operationfactory.d.ts +0 -0
- /package/{src → dist}/model/operation/renameoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/rootattributeoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/rootoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/splitoperation.d.ts +0 -0
- /package/{src → dist}/model/operation/transform.d.ts +0 -0
- /package/{src → dist}/model/operation/utils.d.ts +0 -0
- /package/{src → dist}/model/position.d.ts +0 -0
- /package/{src → dist}/model/range.d.ts +0 -0
- /package/{src → dist}/model/rootelement.d.ts +0 -0
- /package/{src → dist}/model/schema.d.ts +0 -0
- /package/{src → dist}/model/text.d.ts +0 -0
- /package/{src → dist}/model/textproxy.d.ts +0 -0
- /package/{src → dist}/model/treewalker.d.ts +0 -0
- /package/{src → dist}/model/typecheckable.d.ts +0 -0
- /package/{src → dist}/model/utils/autoparagraphing.d.ts +0 -0
- /package/{src → dist}/model/utils/deletecontent.d.ts +0 -0
- /package/{src → dist}/model/utils/getselectedcontent.d.ts +0 -0
- /package/{src → dist}/model/utils/insertcontent.d.ts +0 -0
- /package/{src → dist}/model/utils/insertobject.d.ts +0 -0
- /package/{src → dist}/model/utils/modifyselection.d.ts +0 -0
- /package/{src → dist}/model/utils/selection-post-fixer.d.ts +0 -0
- /package/{src → dist}/model/writer.d.ts +0 -0
- /package/{src → dist}/view/attributeelement.d.ts +0 -0
- /package/{src → dist}/view/containerelement.d.ts +0 -0
- /package/{src → dist}/view/datatransfer.d.ts +0 -0
- /package/{src → dist}/view/document.d.ts +0 -0
- /package/{src → dist}/view/documentfragment.d.ts +0 -0
- /package/{src → dist}/view/documentselection.d.ts +0 -0
- /package/{src → dist}/view/domconverter.d.ts +0 -0
- /package/{src → dist}/view/editableelement.d.ts +0 -0
- /package/{src → dist}/view/elementdefinition.d.ts +0 -0
- /package/{src → dist}/view/emptyelement.d.ts +0 -0
- /package/{src → dist}/view/filler.d.ts +0 -0
- /package/{src → dist}/view/item.d.ts +0 -0
- /package/{src → dist}/view/node.d.ts +0 -0
- /package/{src → dist}/view/observer/arrowkeysobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/bubblingemittermixin.d.ts +0 -0
- /package/{src → dist}/view/observer/bubblingeventinfo.d.ts +0 -0
- /package/{src → dist}/view/observer/clickobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/compositionobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/domeventdata.d.ts +0 -0
- /package/{src → dist}/view/observer/domeventobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/fakeselectionobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/focusobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/inputobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/keyobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/mouseobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/mutationobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/observer.d.ts +0 -0
- /package/{src → dist}/view/observer/pointerobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/selectionobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/tabobserver.d.ts +0 -0
- /package/{src → dist}/view/observer/touchobserver.d.ts +0 -0
- /package/{src → dist}/view/placeholder.d.ts +0 -0
- /package/{src → dist}/view/position.d.ts +0 -0
- /package/{src → dist}/view/range.d.ts +0 -0
- /package/{src → dist}/view/rawelement.d.ts +0 -0
- /package/{src → dist}/view/renderer.d.ts +0 -0
- /package/{src → dist}/view/rooteditableelement.d.ts +0 -0
- /package/{src → dist}/view/selection.d.ts +0 -0
- /package/{src → dist}/view/stylesmap.d.ts +0 -0
- /package/{src → dist}/view/text.d.ts +0 -0
- /package/{src → dist}/view/textproxy.d.ts +0 -0
- /package/{src → dist}/view/tokenlist.d.ts +0 -0
- /package/{src → dist}/view/treewalker.d.ts +0 -0
- /package/{src → dist}/view/typecheckable.d.ts +0 -0
- /package/{src → dist}/view/uielement.d.ts +0 -0
- /package/{src → dist}/view/upcastwriter.d.ts +0 -0
- /package/{src → dist}/view/view.d.ts +0 -0
package/src/view/stylesmap.js
DELETED
|
@@ -1,941 +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/view/stylesmap
|
|
7
|
-
*/
|
|
8
|
-
import { get, isObject, merge, set } from 'es-toolkit/compat';
|
|
9
|
-
import { toArray } from '@ckeditor/ckeditor5-utils';
|
|
10
|
-
import { isPatternMatched } from './matcher.js';
|
|
11
|
-
/**
|
|
12
|
-
* Styles map. Allows handling (adding, removing, retrieving) a set of style rules (usually, of an element).
|
|
13
|
-
*/
|
|
14
|
-
export class StylesMap {
|
|
15
|
-
/**
|
|
16
|
-
* Keeps an internal representation of styles map. Normalized styles are kept as object tree to allow unified modification and
|
|
17
|
-
* value access model using lodash's get, set, unset, etc methods.
|
|
18
|
-
*
|
|
19
|
-
* When no style processor rules are defined it acts as simple key-value storage.
|
|
20
|
-
*/
|
|
21
|
-
_styles;
|
|
22
|
-
/**
|
|
23
|
-
* Cached list of style names for faster access.
|
|
24
|
-
*/
|
|
25
|
-
_cachedStyleNames = null;
|
|
26
|
-
/**
|
|
27
|
-
* Cached list of expanded style names for faster access.
|
|
28
|
-
*/
|
|
29
|
-
_cachedExpandedStyleNames = null;
|
|
30
|
-
/**
|
|
31
|
-
* An instance of the {@link module:engine/view/stylesmap~StylesProcessor}.
|
|
32
|
-
*/
|
|
33
|
-
_styleProcessor;
|
|
34
|
-
/**
|
|
35
|
-
* Creates Styles instance.
|
|
36
|
-
*/
|
|
37
|
-
constructor(styleProcessor) {
|
|
38
|
-
this._styles = {};
|
|
39
|
-
this._styleProcessor = styleProcessor;
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* Returns true if style map has no styles set.
|
|
43
|
-
*/
|
|
44
|
-
get isEmpty() {
|
|
45
|
-
const entries = Object.entries(this._styles);
|
|
46
|
-
return !entries.length;
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Number of styles defined.
|
|
50
|
-
*/
|
|
51
|
-
get size() {
|
|
52
|
-
if (this.isEmpty) {
|
|
53
|
-
return 0;
|
|
54
|
-
}
|
|
55
|
-
return this.getStyleNames().length;
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Set styles map to a new value.
|
|
59
|
-
*
|
|
60
|
-
* ```ts
|
|
61
|
-
* styles.setTo( 'border:1px solid blue;margin-top:1px;' );
|
|
62
|
-
* ```
|
|
63
|
-
*/
|
|
64
|
-
setTo(inlineStyle) {
|
|
65
|
-
this.clear();
|
|
66
|
-
const parsedStyles = parseInlineStyles(inlineStyle);
|
|
67
|
-
for (const [key, value] of parsedStyles) {
|
|
68
|
-
this._styleProcessor.toNormalizedForm(key, value, this._styles);
|
|
69
|
-
}
|
|
70
|
-
return this;
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* Checks if a given style is set.
|
|
74
|
-
*
|
|
75
|
-
* ```ts
|
|
76
|
-
* styles.setTo( 'margin-left:1px;' );
|
|
77
|
-
*
|
|
78
|
-
* styles.has( 'margin-left' ); // -> true
|
|
79
|
-
* styles.has( 'padding' ); // -> false
|
|
80
|
-
* ```
|
|
81
|
-
*
|
|
82
|
-
* **Note**: This check supports normalized style names.
|
|
83
|
-
*
|
|
84
|
-
* ```ts
|
|
85
|
-
* // Enable 'margin' shorthand processing:
|
|
86
|
-
* editor.data.addStyleProcessorRules( addMarginStylesRules );
|
|
87
|
-
*
|
|
88
|
-
* styles.setTo( 'margin:2px;' );
|
|
89
|
-
*
|
|
90
|
-
* styles.has( 'margin' ); // -> true
|
|
91
|
-
* styles.has( 'margin-top' ); // -> true
|
|
92
|
-
* styles.has( 'margin-left' ); // -> true
|
|
93
|
-
*
|
|
94
|
-
* styles.remove( 'margin-top' );
|
|
95
|
-
*
|
|
96
|
-
* styles.has( 'margin' ); // -> false
|
|
97
|
-
* styles.has( 'margin-top' ); // -> false
|
|
98
|
-
* styles.has( 'margin-left' ); // -> true
|
|
99
|
-
* ```
|
|
100
|
-
*
|
|
101
|
-
* @param name Style name.
|
|
102
|
-
*/
|
|
103
|
-
has(name) {
|
|
104
|
-
if (this.isEmpty) {
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
const styles = this._styleProcessor.getReducedForm(name, this._styles);
|
|
108
|
-
const propertyDescriptor = styles.find(([property]) => property === name);
|
|
109
|
-
// Only return a value if it is set;
|
|
110
|
-
return Array.isArray(propertyDescriptor);
|
|
111
|
-
}
|
|
112
|
-
set(nameOrObject, valueOrObject) {
|
|
113
|
-
this._cachedStyleNames = null;
|
|
114
|
-
this._cachedExpandedStyleNames = null;
|
|
115
|
-
if (isObject(nameOrObject)) {
|
|
116
|
-
for (const [key, value] of Object.entries(nameOrObject)) {
|
|
117
|
-
this._styleProcessor.toNormalizedForm(key, value, this._styles);
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
this._styleProcessor.toNormalizedForm(nameOrObject, valueOrObject, this._styles);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
/**
|
|
125
|
-
* Removes given style.
|
|
126
|
-
*
|
|
127
|
-
* ```ts
|
|
128
|
-
* styles.setTo( 'background:#f00;margin-right:2px;' );
|
|
129
|
-
*
|
|
130
|
-
* styles.remove( 'background' );
|
|
131
|
-
*
|
|
132
|
-
* styles.toString(); // -> 'margin-right:2px;'
|
|
133
|
-
* ```
|
|
134
|
-
*
|
|
135
|
-
* ***Note**:* This method uses {@link module:engine/controller/datacontroller~DataController#addStyleProcessorRules
|
|
136
|
-
* enabled style processor rules} to normalize passed values.
|
|
137
|
-
*
|
|
138
|
-
* ```ts
|
|
139
|
-
* // Enable 'margin' shorthand processing:
|
|
140
|
-
* editor.data.addStyleProcessorRules( addMarginStylesRules );
|
|
141
|
-
*
|
|
142
|
-
* styles.setTo( 'margin:1px' );
|
|
143
|
-
*
|
|
144
|
-
* styles.remove( 'margin-top' );
|
|
145
|
-
* styles.remove( 'margin-right' );
|
|
146
|
-
*
|
|
147
|
-
* styles.toString(); // -> 'margin-bottom:1px;margin-left:1px;'
|
|
148
|
-
* ```
|
|
149
|
-
*
|
|
150
|
-
* @param names Style name or an array of names.
|
|
151
|
-
*/
|
|
152
|
-
remove(names) {
|
|
153
|
-
const normalizedStylesToRemove = {};
|
|
154
|
-
for (const name of toArray(names)) {
|
|
155
|
-
// First, try the easy path, when the path reflects normalized styles structure.
|
|
156
|
-
const path = toPath(name);
|
|
157
|
-
const pathValue = get(this._styles, path);
|
|
158
|
-
if (pathValue) {
|
|
159
|
-
appendStyleValue(normalizedStylesToRemove, path, pathValue);
|
|
160
|
-
}
|
|
161
|
-
else {
|
|
162
|
-
// Easy path did not work, so try to get the value from the styles map.
|
|
163
|
-
const value = this.getAsString(name);
|
|
164
|
-
if (value !== undefined) {
|
|
165
|
-
this._styleProcessor.toNormalizedForm(name, value, normalizedStylesToRemove);
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
if (Object.keys(normalizedStylesToRemove).length) {
|
|
170
|
-
removeStyles(this._styles, normalizedStylesToRemove);
|
|
171
|
-
this._cachedStyleNames = null;
|
|
172
|
-
this._cachedExpandedStyleNames = null;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* Returns a normalized style object or a single value.
|
|
177
|
-
*
|
|
178
|
-
* ```ts
|
|
179
|
-
* // Enable 'margin' shorthand processing:
|
|
180
|
-
* editor.data.addStyleProcessorRules( addMarginStylesRules );
|
|
181
|
-
*
|
|
182
|
-
* const styles = new Styles();
|
|
183
|
-
* styles.setTo( 'margin:1px 2px 3em;' );
|
|
184
|
-
*
|
|
185
|
-
* styles.getNormalized( 'margin' );
|
|
186
|
-
* // will log:
|
|
187
|
-
* // {
|
|
188
|
-
* // top: '1px',
|
|
189
|
-
* // right: '2px',
|
|
190
|
-
* // bottom: '3em',
|
|
191
|
-
* // left: '2px' // normalized value from margin shorthand
|
|
192
|
-
* // }
|
|
193
|
-
*
|
|
194
|
-
* styles.getNormalized( 'margin-left' ); // -> '2px'
|
|
195
|
-
* ```
|
|
196
|
-
*
|
|
197
|
-
* **Note**: This method will only return normalized styles if a style processor was defined.
|
|
198
|
-
*
|
|
199
|
-
* @param name Style name.
|
|
200
|
-
*/
|
|
201
|
-
getNormalized(name) {
|
|
202
|
-
return this._styleProcessor.getNormalized(name, this._styles);
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Returns a normalized style string. Styles are sorted by name.
|
|
206
|
-
*
|
|
207
|
-
* ```ts
|
|
208
|
-
* styles.set( 'margin' , '1px' );
|
|
209
|
-
* styles.set( 'background', '#f00' );
|
|
210
|
-
*
|
|
211
|
-
* styles.toString(); // -> 'background:#f00;margin:1px;'
|
|
212
|
-
* ```
|
|
213
|
-
*
|
|
214
|
-
* **Note**: This method supports normalized styles if defined.
|
|
215
|
-
*
|
|
216
|
-
* ```ts
|
|
217
|
-
* // Enable 'margin' shorthand processing:
|
|
218
|
-
* editor.data.addStyleProcessorRules( addMarginStylesRules );
|
|
219
|
-
*
|
|
220
|
-
* styles.set( 'margin' , '1px' );
|
|
221
|
-
* styles.set( 'background', '#f00' );
|
|
222
|
-
* styles.remove( 'margin-top' );
|
|
223
|
-
* styles.remove( 'margin-right' );
|
|
224
|
-
*
|
|
225
|
-
* styles.toString(); // -> 'background:#f00;margin-bottom:1px;margin-left:1px;'
|
|
226
|
-
* ```
|
|
227
|
-
*/
|
|
228
|
-
toString() {
|
|
229
|
-
if (this.isEmpty) {
|
|
230
|
-
return '';
|
|
231
|
-
}
|
|
232
|
-
return this.getStylesEntries()
|
|
233
|
-
.map(arr => arr.join(':'))
|
|
234
|
-
.sort()
|
|
235
|
-
.join(';') + ';';
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Returns property as a value string or undefined if property is not set.
|
|
239
|
-
*
|
|
240
|
-
* ```ts
|
|
241
|
-
* // Enable 'margin' shorthand processing:
|
|
242
|
-
* editor.data.addStyleProcessorRules( addMarginStylesRules );
|
|
243
|
-
*
|
|
244
|
-
* const styles = new Styles();
|
|
245
|
-
* styles.setTo( 'margin:1px;' );
|
|
246
|
-
* styles.set( 'margin-bottom', '3em' );
|
|
247
|
-
*
|
|
248
|
-
* styles.getAsString( 'margin' ); // -> 'margin: 1px 1px 3em;'
|
|
249
|
-
* ```
|
|
250
|
-
*
|
|
251
|
-
* Note, however, that all sub-values must be set for the longhand property name to return a value:
|
|
252
|
-
*
|
|
253
|
-
* ```ts
|
|
254
|
-
* const styles = new Styles();
|
|
255
|
-
* styles.setTo( 'margin:1px;' );
|
|
256
|
-
* styles.remove( 'margin-bottom' );
|
|
257
|
-
*
|
|
258
|
-
* styles.getAsString( 'margin' ); // -> undefined
|
|
259
|
-
* ```
|
|
260
|
-
*
|
|
261
|
-
* In the above scenario, it is not possible to return a `margin` value, so `undefined` is returned.
|
|
262
|
-
* Instead, you should use:
|
|
263
|
-
*
|
|
264
|
-
* ```ts
|
|
265
|
-
* const styles = new Styles();
|
|
266
|
-
* styles.setTo( 'margin:1px;' );
|
|
267
|
-
* styles.remove( 'margin-bottom' );
|
|
268
|
-
*
|
|
269
|
-
* for ( const styleName of styles.getStyleNames() ) {
|
|
270
|
-
* console.log( styleName, styles.getAsString( styleName ) );
|
|
271
|
-
* }
|
|
272
|
-
* // 'margin-top', '1px'
|
|
273
|
-
* // 'margin-right', '1px'
|
|
274
|
-
* // 'margin-left', '1px'
|
|
275
|
-
* ```
|
|
276
|
-
*
|
|
277
|
-
* In general, it is recommend to iterate over style names like in the example above. This way, you will always get all
|
|
278
|
-
* the currently set style values. So, if all the 4 margin values would be set
|
|
279
|
-
* the for-of loop above would yield only `'margin'`, `'1px'`:
|
|
280
|
-
*
|
|
281
|
-
* ```ts
|
|
282
|
-
* const styles = new Styles();
|
|
283
|
-
* styles.setTo( 'margin:1px;' );
|
|
284
|
-
*
|
|
285
|
-
* for ( const styleName of styles.getStyleNames() ) {
|
|
286
|
-
* console.log( styleName, styles.getAsString( styleName ) );
|
|
287
|
-
* }
|
|
288
|
-
* // 'margin', '1px'
|
|
289
|
-
* ```
|
|
290
|
-
*
|
|
291
|
-
* **Note**: To get a normalized version of a longhand property use the {@link #getNormalized `#getNormalized()`} method.
|
|
292
|
-
*/
|
|
293
|
-
getAsString(propertyName) {
|
|
294
|
-
if (this.isEmpty) {
|
|
295
|
-
return;
|
|
296
|
-
}
|
|
297
|
-
if (this._styles[propertyName] && !isObject(this._styles[propertyName])) {
|
|
298
|
-
// Try return styles set directly - values that are not parsed.
|
|
299
|
-
return this._styles[propertyName];
|
|
300
|
-
}
|
|
301
|
-
const styles = this._styleProcessor.getReducedForm(propertyName, this._styles);
|
|
302
|
-
const propertyDescriptor = styles.find(([property]) => property === propertyName);
|
|
303
|
-
// Only return a value if it is set;
|
|
304
|
-
if (Array.isArray(propertyDescriptor)) {
|
|
305
|
-
return propertyDescriptor[1];
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Returns all style properties names as they would appear when using {@link #toString `#toString()`}.
|
|
310
|
-
*
|
|
311
|
-
* When `expand` is set to true and there's a shorthand style property set, it will also return all equivalent styles:
|
|
312
|
-
*
|
|
313
|
-
* ```ts
|
|
314
|
-
* stylesMap.setTo( 'margin: 1em' )
|
|
315
|
-
* ```
|
|
316
|
-
*
|
|
317
|
-
* will be expanded to:
|
|
318
|
-
*
|
|
319
|
-
* ```ts
|
|
320
|
-
* [ 'margin', 'margin-top', 'margin-right', 'margin-bottom', 'margin-left' ]
|
|
321
|
-
* ```
|
|
322
|
-
*
|
|
323
|
-
* @param expand Expand shorthand style properties and all return equivalent style representations.
|
|
324
|
-
*/
|
|
325
|
-
getStyleNames(expand = false) {
|
|
326
|
-
if (this.isEmpty) {
|
|
327
|
-
return [];
|
|
328
|
-
}
|
|
329
|
-
if (expand) {
|
|
330
|
-
this._cachedExpandedStyleNames ||= this._styleProcessor.getStyleNames(this._styles);
|
|
331
|
-
return this._cachedExpandedStyleNames;
|
|
332
|
-
}
|
|
333
|
-
this._cachedStyleNames ||= this.getStylesEntries().map(([key]) => key);
|
|
334
|
-
return this._cachedStyleNames;
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* Alias for {@link #getStyleNames}.
|
|
338
|
-
*/
|
|
339
|
-
keys() {
|
|
340
|
-
return this.getStyleNames();
|
|
341
|
-
}
|
|
342
|
-
/**
|
|
343
|
-
* Removes all styles.
|
|
344
|
-
*/
|
|
345
|
-
clear() {
|
|
346
|
-
this._styles = {};
|
|
347
|
-
this._cachedStyleNames = null;
|
|
348
|
-
this._cachedExpandedStyleNames = null;
|
|
349
|
-
}
|
|
350
|
-
/**
|
|
351
|
-
* Returns `true` if both attributes have the same styles.
|
|
352
|
-
*/
|
|
353
|
-
isSimilar(other) {
|
|
354
|
-
if (this.size !== other.size) {
|
|
355
|
-
return false;
|
|
356
|
-
}
|
|
357
|
-
for (const property of this.getStyleNames()) {
|
|
358
|
-
if (!other.has(property) || other.getAsString(property) !== this.getAsString(property)) {
|
|
359
|
-
return false;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
return true;
|
|
363
|
-
}
|
|
364
|
-
/**
|
|
365
|
-
* Returns normalized styles entries for further processing.
|
|
366
|
-
*/
|
|
367
|
-
getStylesEntries() {
|
|
368
|
-
const parsed = [];
|
|
369
|
-
const keys = Object.keys(this._styles);
|
|
370
|
-
for (const key of keys) {
|
|
371
|
-
parsed.push(...this._styleProcessor.getReducedForm(key, this._styles));
|
|
372
|
-
}
|
|
373
|
-
return parsed;
|
|
374
|
-
}
|
|
375
|
-
/**
|
|
376
|
-
* Clones the attribute value.
|
|
377
|
-
*
|
|
378
|
-
* @internal
|
|
379
|
-
*/
|
|
380
|
-
_clone() {
|
|
381
|
-
const clone = new this.constructor(this._styleProcessor);
|
|
382
|
-
clone.set(this.getNormalized());
|
|
383
|
-
return clone;
|
|
384
|
-
}
|
|
385
|
-
/**
|
|
386
|
-
* Used by the {@link module:engine/view/matcher~Matcher Matcher} to collect matching styles.
|
|
387
|
-
*
|
|
388
|
-
* @internal
|
|
389
|
-
* @param tokenPattern The matched style name pattern.
|
|
390
|
-
* @param valuePattern The matched style value pattern.
|
|
391
|
-
* @returns An array of matching tokens (style names).
|
|
392
|
-
*/
|
|
393
|
-
_getTokensMatch(tokenPattern, valuePattern) {
|
|
394
|
-
const match = [];
|
|
395
|
-
for (const styleName of this.getStyleNames(true)) {
|
|
396
|
-
if (isPatternMatched(tokenPattern, styleName)) {
|
|
397
|
-
if (valuePattern === true) {
|
|
398
|
-
match.push(styleName);
|
|
399
|
-
continue;
|
|
400
|
-
}
|
|
401
|
-
// For now, the reducers are not returning the full tree of properties.
|
|
402
|
-
// Casting to string preserves the old behavior until the root cause is fixed.
|
|
403
|
-
// More can be found in https://github.com/ckeditor/ckeditor5/issues/10399.
|
|
404
|
-
const value = this.getAsString(styleName);
|
|
405
|
-
if (isPatternMatched(valuePattern, value)) {
|
|
406
|
-
match.push(styleName);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
return match.length ? match : undefined;
|
|
411
|
-
}
|
|
412
|
-
/**
|
|
413
|
-
* Returns a list of consumables for the attribute. This includes related styles.
|
|
414
|
-
*
|
|
415
|
-
* Could be filtered by the given style name.
|
|
416
|
-
*
|
|
417
|
-
* @internal
|
|
418
|
-
*/
|
|
419
|
-
_getConsumables(name) {
|
|
420
|
-
const result = [];
|
|
421
|
-
if (name) {
|
|
422
|
-
result.push(name);
|
|
423
|
-
for (const relatedName of this._styleProcessor.getRelatedStyles(name)) {
|
|
424
|
-
result.push(relatedName);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
else {
|
|
428
|
-
for (const name of this.getStyleNames()) {
|
|
429
|
-
for (const relatedName of this._styleProcessor.getRelatedStyles(name)) {
|
|
430
|
-
result.push(relatedName);
|
|
431
|
-
}
|
|
432
|
-
result.push(name);
|
|
433
|
-
}
|
|
434
|
-
}
|
|
435
|
-
return result;
|
|
436
|
-
}
|
|
437
|
-
/**
|
|
438
|
-
* Used by {@link module:engine/view/element~ViewElement#_canMergeAttributesFrom} to verify if the given attribute can be merged without
|
|
439
|
-
* conflicts into the attribute.
|
|
440
|
-
*
|
|
441
|
-
* This method is indirectly used by the {@link module:engine/view/downcastwriter~ViewDowncastWriter} while down-casting
|
|
442
|
-
* an {@link module:engine/view/attributeelement~ViewAttributeElement} to merge it with other ViewAttributeElement.
|
|
443
|
-
*
|
|
444
|
-
* @internal
|
|
445
|
-
*/
|
|
446
|
-
_canMergeFrom(other) {
|
|
447
|
-
for (const key of other.getStyleNames()) {
|
|
448
|
-
if (this.has(key) && this.getAsString(key) !== other.getAsString(key)) {
|
|
449
|
-
return false;
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
return true;
|
|
453
|
-
}
|
|
454
|
-
/**
|
|
455
|
-
* Used by {@link module:engine/view/element~ViewElement#_mergeAttributesFrom} to merge a given attribute into the attribute.
|
|
456
|
-
*
|
|
457
|
-
* This method is indirectly used by the {@link module:engine/view/downcastwriter~ViewDowncastWriter} while down-casting
|
|
458
|
-
* an {@link module:engine/view/attributeelement~ViewAttributeElement} to merge it with other ViewAttributeElement.
|
|
459
|
-
*
|
|
460
|
-
* @internal
|
|
461
|
-
*/
|
|
462
|
-
_mergeFrom(other) {
|
|
463
|
-
for (const prop of other.getStyleNames()) {
|
|
464
|
-
if (!this.has(prop)) {
|
|
465
|
-
this.set(prop, other.getAsString(prop));
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
/**
|
|
470
|
-
* Used by {@link module:engine/view/element~ViewElement#_canSubtractAttributesOf} to verify if the given attribute can be fully
|
|
471
|
-
* subtracted from the attribute.
|
|
472
|
-
*
|
|
473
|
-
* This method is indirectly used by the {@link module:engine/view/downcastwriter~ViewDowncastWriter} while down-casting
|
|
474
|
-
* an {@link module:engine/view/attributeelement~ViewAttributeElement} to unwrap the ViewAttributeElement.
|
|
475
|
-
*
|
|
476
|
-
* @internal
|
|
477
|
-
*/
|
|
478
|
-
_isMatching(other) {
|
|
479
|
-
for (const key of other.getStyleNames()) {
|
|
480
|
-
if (!this.has(key) || this.getAsString(key) !== other.getAsString(key)) {
|
|
481
|
-
return false;
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
return true;
|
|
485
|
-
}
|
|
486
|
-
}
|
|
487
|
-
/**
|
|
488
|
-
* Style processor is responsible for writing and reading a normalized styles object.
|
|
489
|
-
*/
|
|
490
|
-
export class StylesProcessor {
|
|
491
|
-
_normalizers;
|
|
492
|
-
_extractors;
|
|
493
|
-
_reducers;
|
|
494
|
-
_consumables;
|
|
495
|
-
/**
|
|
496
|
-
* Creates StylesProcessor instance.
|
|
497
|
-
*
|
|
498
|
-
* @internal
|
|
499
|
-
*/
|
|
500
|
-
constructor() {
|
|
501
|
-
this._normalizers = new Map();
|
|
502
|
-
this._extractors = new Map();
|
|
503
|
-
this._reducers = new Map();
|
|
504
|
-
this._consumables = new Map();
|
|
505
|
-
}
|
|
506
|
-
/**
|
|
507
|
-
* Parse style string value to a normalized object and appends it to styles object.
|
|
508
|
-
*
|
|
509
|
-
* ```ts
|
|
510
|
-
* const styles = {};
|
|
511
|
-
*
|
|
512
|
-
* stylesProcessor.toNormalizedForm( 'margin', '1px', styles );
|
|
513
|
-
*
|
|
514
|
-
* // styles will consist: { margin: { top: '1px', right: '1px', bottom: '1px', left: '1px; } }
|
|
515
|
-
* ```
|
|
516
|
-
*
|
|
517
|
-
* **Note**: To define normalizer callbacks use {@link #setNormalizer}.
|
|
518
|
-
*
|
|
519
|
-
* @param name Name of style property.
|
|
520
|
-
* @param propertyValue Value of style property.
|
|
521
|
-
* @param styles Object holding normalized styles.
|
|
522
|
-
*/
|
|
523
|
-
toNormalizedForm(name, propertyValue, styles) {
|
|
524
|
-
if (isObject(propertyValue)) {
|
|
525
|
-
appendStyleValue(styles, toPath(name), propertyValue);
|
|
526
|
-
return;
|
|
527
|
-
}
|
|
528
|
-
if (this._normalizers.has(name)) {
|
|
529
|
-
const normalizer = this._normalizers.get(name);
|
|
530
|
-
const { path, value } = normalizer(propertyValue);
|
|
531
|
-
appendStyleValue(styles, path, value);
|
|
532
|
-
}
|
|
533
|
-
else {
|
|
534
|
-
appendStyleValue(styles, name, propertyValue);
|
|
535
|
-
}
|
|
536
|
-
}
|
|
537
|
-
/**
|
|
538
|
-
* Returns a normalized version of a style property.
|
|
539
|
-
*
|
|
540
|
-
* ```ts
|
|
541
|
-
* const styles = {
|
|
542
|
-
* margin: { top: '1px', right: '1px', bottom: '1px', left: '1px; },
|
|
543
|
-
* background: { color: '#f00' }
|
|
544
|
-
* };
|
|
545
|
-
*
|
|
546
|
-
* stylesProcessor.getNormalized( 'background' );
|
|
547
|
-
* // will return: { color: '#f00' }
|
|
548
|
-
*
|
|
549
|
-
* stylesProcessor.getNormalized( 'margin-top' );
|
|
550
|
-
* // will return: '1px'
|
|
551
|
-
* ```
|
|
552
|
-
*
|
|
553
|
-
* **Note**: In some cases extracting single value requires defining an extractor callback {@link #setExtractor}.
|
|
554
|
-
*
|
|
555
|
-
* @param name Name of style property.
|
|
556
|
-
* @param styles Object holding normalized styles.
|
|
557
|
-
*/
|
|
558
|
-
getNormalized(name, styles) {
|
|
559
|
-
if (!name) {
|
|
560
|
-
return merge({}, styles);
|
|
561
|
-
}
|
|
562
|
-
// Might be empty string.
|
|
563
|
-
if (styles[name] !== undefined) {
|
|
564
|
-
return styles[name];
|
|
565
|
-
}
|
|
566
|
-
if (this._extractors.has(name)) {
|
|
567
|
-
const extractor = this._extractors.get(name);
|
|
568
|
-
if (typeof extractor === 'string') {
|
|
569
|
-
return get(styles, extractor);
|
|
570
|
-
}
|
|
571
|
-
const value = extractor(name, styles);
|
|
572
|
-
if (value) {
|
|
573
|
-
return value;
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
return get(styles, toPath(name));
|
|
577
|
-
}
|
|
578
|
-
/**
|
|
579
|
-
* Returns a reduced form of style property form normalized object.
|
|
580
|
-
*
|
|
581
|
-
* For default margin reducer, the below code:
|
|
582
|
-
*
|
|
583
|
-
* ```ts
|
|
584
|
-
* stylesProcessor.getReducedForm( 'margin', {
|
|
585
|
-
* margin: { top: '1px', right: '1px', bottom: '2px', left: '1px; }
|
|
586
|
-
* } );
|
|
587
|
-
* ```
|
|
588
|
-
*
|
|
589
|
-
* will return:
|
|
590
|
-
*
|
|
591
|
-
* ```ts
|
|
592
|
-
* [
|
|
593
|
-
* [ 'margin', '1px 1px 2px' ]
|
|
594
|
-
* ]
|
|
595
|
-
* ```
|
|
596
|
-
*
|
|
597
|
-
* because it might be represented as a shorthand 'margin' value. However if one of margin long hand values is missing it should return:
|
|
598
|
-
*
|
|
599
|
-
* ```ts
|
|
600
|
-
* [
|
|
601
|
-
* [ 'margin-top', '1px' ],
|
|
602
|
-
* [ 'margin-right', '1px' ],
|
|
603
|
-
* [ 'margin-bottom', '2px' ]
|
|
604
|
-
* // the 'left' value is missing - cannot use 'margin' shorthand.
|
|
605
|
-
* ]
|
|
606
|
-
* ```
|
|
607
|
-
*
|
|
608
|
-
* **Note**: To define reducer callbacks use {@link #setReducer}.
|
|
609
|
-
*
|
|
610
|
-
* @param name Name of style property.
|
|
611
|
-
*/
|
|
612
|
-
getReducedForm(name, styles) {
|
|
613
|
-
const normalizedValue = this.getNormalized(name, styles);
|
|
614
|
-
// Might be empty string.
|
|
615
|
-
if (normalizedValue === undefined) {
|
|
616
|
-
return [];
|
|
617
|
-
}
|
|
618
|
-
if (this._reducers.has(name)) {
|
|
619
|
-
const reducer = this._reducers.get(name);
|
|
620
|
-
return reducer(normalizedValue);
|
|
621
|
-
}
|
|
622
|
-
return [[name, normalizedValue]];
|
|
623
|
-
}
|
|
624
|
-
/**
|
|
625
|
-
* Return all style properties. Also expand shorthand properties (e.g. `margin`, `background`) if respective extractor is available.
|
|
626
|
-
*
|
|
627
|
-
* @param styles Object holding normalized styles.
|
|
628
|
-
*/
|
|
629
|
-
getStyleNames(styles) {
|
|
630
|
-
const styleNamesKeysSet = new Set();
|
|
631
|
-
// Find all extractable styles that have a value.
|
|
632
|
-
for (const name of this._consumables.keys()) {
|
|
633
|
-
const style = this.getNormalized(name, styles);
|
|
634
|
-
if (style && (typeof style != 'object' || Object.keys(style).length)) {
|
|
635
|
-
styleNamesKeysSet.add(name);
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
// For simple styles (for example `color`) we don't have a map of those styles
|
|
639
|
-
// but they are 1 to 1 with normalized object keys.
|
|
640
|
-
for (const name of Object.keys(styles)) {
|
|
641
|
-
styleNamesKeysSet.add(name);
|
|
642
|
-
}
|
|
643
|
-
return Array.from(styleNamesKeysSet);
|
|
644
|
-
}
|
|
645
|
-
/**
|
|
646
|
-
* Returns related style names.
|
|
647
|
-
*
|
|
648
|
-
* ```ts
|
|
649
|
-
* stylesProcessor.getRelatedStyles( 'margin' );
|
|
650
|
-
* // will return: [ 'margin-top', 'margin-right', 'margin-bottom', 'margin-left' ];
|
|
651
|
-
*
|
|
652
|
-
* stylesProcessor.getRelatedStyles( 'margin-top' );
|
|
653
|
-
* // will return: [ 'margin' ];
|
|
654
|
-
* ```
|
|
655
|
-
*
|
|
656
|
-
* **Note**: To define new style relations load an existing style processor or use
|
|
657
|
-
* {@link module:engine/view/stylesmap~StylesProcessor#setStyleRelation `StylesProcessor.setStyleRelation()`}.
|
|
658
|
-
*/
|
|
659
|
-
getRelatedStyles(name) {
|
|
660
|
-
return this._consumables.get(name) || [];
|
|
661
|
-
}
|
|
662
|
-
/**
|
|
663
|
-
* Adds a normalizer method for a style property.
|
|
664
|
-
*
|
|
665
|
-
* A normalizer returns describing how the value should be normalized.
|
|
666
|
-
*
|
|
667
|
-
* For instance 'margin' style is a shorthand for four margin values:
|
|
668
|
-
*
|
|
669
|
-
* - 'margin-top'
|
|
670
|
-
* - 'margin-right'
|
|
671
|
-
* - 'margin-bottom'
|
|
672
|
-
* - 'margin-left'
|
|
673
|
-
*
|
|
674
|
-
* and can be written in various ways if some values are equal to others. For instance `'margin: 1px 2em;'` is a shorthand for
|
|
675
|
-
* `'margin-top: 1px;margin-right: 2em;margin-bottom: 1px;margin-left: 2em'`.
|
|
676
|
-
*
|
|
677
|
-
* A normalizer should parse various margin notations as a single object:
|
|
678
|
-
*
|
|
679
|
-
* ```ts
|
|
680
|
-
* const styles = {
|
|
681
|
-
* margin: {
|
|
682
|
-
* top: '1px',
|
|
683
|
-
* right: '2em',
|
|
684
|
-
* bottom: '1px',
|
|
685
|
-
* left: '2em'
|
|
686
|
-
* }
|
|
687
|
-
* };
|
|
688
|
-
* ```
|
|
689
|
-
*
|
|
690
|
-
* Thus a normalizer for 'margin' style should return an object defining style path and value to store:
|
|
691
|
-
*
|
|
692
|
-
* ```ts
|
|
693
|
-
* const returnValue = {
|
|
694
|
-
* path: 'margin',
|
|
695
|
-
* value: {
|
|
696
|
-
* top: '1px',
|
|
697
|
-
* right: '2em',
|
|
698
|
-
* bottom: '1px',
|
|
699
|
-
* left: '2em'
|
|
700
|
-
* }
|
|
701
|
-
* };
|
|
702
|
-
* ```
|
|
703
|
-
*
|
|
704
|
-
* Additionally to fully support all margin notations there should be also defined 4 normalizers for longhand margin notations. Below
|
|
705
|
-
* is an example for 'margin-top' style property normalizer:
|
|
706
|
-
*
|
|
707
|
-
* ```ts
|
|
708
|
-
* stylesProcessor.setNormalizer( 'margin-top', valueString => {
|
|
709
|
-
* return {
|
|
710
|
-
* path: 'margin.top',
|
|
711
|
-
* value: valueString
|
|
712
|
-
* }
|
|
713
|
-
* } );
|
|
714
|
-
* ```
|
|
715
|
-
*/
|
|
716
|
-
setNormalizer(name, callback) {
|
|
717
|
-
this._normalizers.set(name, callback);
|
|
718
|
-
}
|
|
719
|
-
/**
|
|
720
|
-
* Adds a extractor callback for a style property.
|
|
721
|
-
*
|
|
722
|
-
* Most normalized style values are stored as one level objects. It is assumed that `'margin-top'` style will be stored as:
|
|
723
|
-
*
|
|
724
|
-
* ```ts
|
|
725
|
-
* const styles = {
|
|
726
|
-
* margin: {
|
|
727
|
-
* top: 'value'
|
|
728
|
-
* }
|
|
729
|
-
* }
|
|
730
|
-
* ```
|
|
731
|
-
*
|
|
732
|
-
* However, some styles can have conflicting notations and thus it might be harder to extract a style value from shorthand. For instance
|
|
733
|
-
* the 'border-top-style' can be defined using `'border-top:solid'`, `'border-style:solid none none none'` or by `'border:solid'`
|
|
734
|
-
* shorthands. The default border styles processors stores styles as:
|
|
735
|
-
*
|
|
736
|
-
* ```ts
|
|
737
|
-
* const styles = {
|
|
738
|
-
* border: {
|
|
739
|
-
* style: {
|
|
740
|
-
* top: 'solid'
|
|
741
|
-
* }
|
|
742
|
-
* }
|
|
743
|
-
* }
|
|
744
|
-
* ```
|
|
745
|
-
*
|
|
746
|
-
* as it is better to modify border style independently from other values. On the other part the output of the border might be
|
|
747
|
-
* desired as `border-top`, `border-left`, etc notation.
|
|
748
|
-
*
|
|
749
|
-
* In the above example an extractor should return a side border value that combines style, color and width:
|
|
750
|
-
*
|
|
751
|
-
* ```ts
|
|
752
|
-
* styleProcessor.setExtractor( 'border-top', styles => {
|
|
753
|
-
* return {
|
|
754
|
-
* color: styles.border.color.top,
|
|
755
|
-
* style: styles.border.style.top,
|
|
756
|
-
* width: styles.border.width.top
|
|
757
|
-
* }
|
|
758
|
-
* } );
|
|
759
|
-
* ```
|
|
760
|
-
*
|
|
761
|
-
* @param callbackOrPath Callback that return a requested value or path string for single values.
|
|
762
|
-
*/
|
|
763
|
-
setExtractor(name, callbackOrPath) {
|
|
764
|
-
this._extractors.set(name, callbackOrPath);
|
|
765
|
-
}
|
|
766
|
-
/**
|
|
767
|
-
* Adds a reducer callback for a style property.
|
|
768
|
-
*
|
|
769
|
-
* Reducer returns a minimal notation for given style name. For longhand properties it is not required to write a reducer as
|
|
770
|
-
* by default the direct value from style path is taken.
|
|
771
|
-
*
|
|
772
|
-
* For shorthand styles a reducer should return minimal style notation either by returning single name-value tuple or multiple tuples
|
|
773
|
-
* if a shorthand cannot be used. For instance for a margin shorthand a reducer might return:
|
|
774
|
-
*
|
|
775
|
-
* ```ts
|
|
776
|
-
* const marginShortHandTuple = [
|
|
777
|
-
* [ 'margin', '1px 1px 2px' ]
|
|
778
|
-
* ];
|
|
779
|
-
* ```
|
|
780
|
-
*
|
|
781
|
-
* or a longhand tuples for defined values:
|
|
782
|
-
*
|
|
783
|
-
* ```ts
|
|
784
|
-
* // Considering margin.bottom and margin.left are undefined.
|
|
785
|
-
* const marginLonghandsTuples = [
|
|
786
|
-
* [ 'margin-top', '1px' ],
|
|
787
|
-
* [ 'margin-right', '1px' ]
|
|
788
|
-
* ];
|
|
789
|
-
* ```
|
|
790
|
-
*
|
|
791
|
-
* A reducer obtains a normalized style value:
|
|
792
|
-
*
|
|
793
|
-
* ```ts
|
|
794
|
-
* // Simplified reducer that always outputs 4 values which are always present:
|
|
795
|
-
* stylesProcessor.setReducer( 'margin', margin => {
|
|
796
|
-
* return [
|
|
797
|
-
* [ 'margin', `${ margin.top } ${ margin.right } ${ margin.bottom } ${ margin.left }` ]
|
|
798
|
-
* ]
|
|
799
|
-
* } );
|
|
800
|
-
* ```
|
|
801
|
-
*/
|
|
802
|
-
setReducer(name, callback) {
|
|
803
|
-
this._reducers.set(name, callback);
|
|
804
|
-
}
|
|
805
|
-
/**
|
|
806
|
-
* Defines a style shorthand relation to other style notations.
|
|
807
|
-
*
|
|
808
|
-
* ```ts
|
|
809
|
-
* stylesProcessor.setStyleRelation( 'margin', [
|
|
810
|
-
* 'margin-top',
|
|
811
|
-
* 'margin-right',
|
|
812
|
-
* 'margin-bottom',
|
|
813
|
-
* 'margin-left'
|
|
814
|
-
* ] );
|
|
815
|
-
* ```
|
|
816
|
-
*
|
|
817
|
-
* This enables expanding of style names for shorthands. For instance, if defined,
|
|
818
|
-
* {@link module:engine/conversion/viewconsumable~ViewConsumable view consumable} items are automatically created
|
|
819
|
-
* for long-hand margin style notation alongside the `'margin'` item.
|
|
820
|
-
*
|
|
821
|
-
* This means that when an element being converted has a style `margin`, a converter for `margin-left` will work just
|
|
822
|
-
* fine since the view consumable will contain a consumable `margin-left` item (thanks to the relation) and
|
|
823
|
-
* `element.getStyle( 'margin-left' )` will work as well assuming that the style processor was correctly configured.
|
|
824
|
-
* However, once `margin-left` is consumed, `margin` will not be consumable anymore.
|
|
825
|
-
*/
|
|
826
|
-
setStyleRelation(shorthandName, styleNames) {
|
|
827
|
-
this._mapStyleNames(shorthandName, styleNames);
|
|
828
|
-
for (const alsoName of styleNames) {
|
|
829
|
-
this._mapStyleNames(alsoName, [shorthandName]);
|
|
830
|
-
}
|
|
831
|
-
}
|
|
832
|
-
/**
|
|
833
|
-
* Set two-way binding of style names.
|
|
834
|
-
*/
|
|
835
|
-
_mapStyleNames(name, styleNames) {
|
|
836
|
-
if (!this._consumables.has(name)) {
|
|
837
|
-
this._consumables.set(name, []);
|
|
838
|
-
}
|
|
839
|
-
this._consumables.get(name).push(...styleNames);
|
|
840
|
-
}
|
|
841
|
-
}
|
|
842
|
-
/**
|
|
843
|
-
* Parses inline styles and puts property - value pairs into styles map.
|
|
844
|
-
*
|
|
845
|
-
* @param stylesString Styles to parse.
|
|
846
|
-
* @returns Map of parsed properties and values.
|
|
847
|
-
*/
|
|
848
|
-
function parseInlineStyles(stylesString) {
|
|
849
|
-
// `null` if no quote was found in input string or last found quote was a closing quote. See below.
|
|
850
|
-
let quoteType = null;
|
|
851
|
-
let propertyNameStart = 0;
|
|
852
|
-
let propertyValueStart = 0;
|
|
853
|
-
let propertyName = null;
|
|
854
|
-
const stylesMap = new Map();
|
|
855
|
-
// Do not set anything if input string is empty.
|
|
856
|
-
if (stylesString === '') {
|
|
857
|
-
return stylesMap;
|
|
858
|
-
}
|
|
859
|
-
// Fix inline styles that do not end with `;` so they are compatible with algorithm below.
|
|
860
|
-
if (stylesString.charAt(stylesString.length - 1) != ';') {
|
|
861
|
-
stylesString = stylesString + ';';
|
|
862
|
-
}
|
|
863
|
-
// Seek the whole string for "special characters".
|
|
864
|
-
for (let i = 0; i < stylesString.length; i++) {
|
|
865
|
-
const char = stylesString.charAt(i);
|
|
866
|
-
if (quoteType === null) {
|
|
867
|
-
// No quote found yet or last found quote was a closing quote.
|
|
868
|
-
switch (char) {
|
|
869
|
-
case ':':
|
|
870
|
-
// Most of time colon means that property name just ended.
|
|
871
|
-
// Sometimes however `:` is found inside property value (for example in background image url).
|
|
872
|
-
if (!propertyName) {
|
|
873
|
-
// Treat this as end of property only if property name is not already saved.
|
|
874
|
-
// Save property name.
|
|
875
|
-
propertyName = stylesString.substr(propertyNameStart, i - propertyNameStart);
|
|
876
|
-
// Save this point as the start of property value.
|
|
877
|
-
propertyValueStart = i + 1;
|
|
878
|
-
}
|
|
879
|
-
break;
|
|
880
|
-
case '"':
|
|
881
|
-
case '\'':
|
|
882
|
-
// Opening quote found (this is an opening quote, because `quoteType` is `null`).
|
|
883
|
-
quoteType = char;
|
|
884
|
-
break;
|
|
885
|
-
case ';': {
|
|
886
|
-
// Property value just ended.
|
|
887
|
-
// Use previously stored property value start to obtain property value.
|
|
888
|
-
const propertyValue = stylesString.substr(propertyValueStart, i - propertyValueStart);
|
|
889
|
-
if (propertyName) {
|
|
890
|
-
// Save parsed part.
|
|
891
|
-
stylesMap.set(propertyName.trim(), propertyValue.trim());
|
|
892
|
-
}
|
|
893
|
-
propertyName = null;
|
|
894
|
-
// Save this point as property name start. Property name starts immediately after previous property value ends.
|
|
895
|
-
propertyNameStart = i + 1;
|
|
896
|
-
break;
|
|
897
|
-
}
|
|
898
|
-
}
|
|
899
|
-
}
|
|
900
|
-
else if (char === quoteType) {
|
|
901
|
-
// If a quote char is found and it is a closing quote, mark this fact by `null`-ing `quoteType`.
|
|
902
|
-
quoteType = null;
|
|
903
|
-
}
|
|
904
|
-
}
|
|
905
|
-
return stylesMap;
|
|
906
|
-
}
|
|
907
|
-
/**
|
|
908
|
-
* Return lodash compatible path from style name.
|
|
909
|
-
*/
|
|
910
|
-
function toPath(name) {
|
|
911
|
-
return name.replace('-', '.');
|
|
912
|
-
}
|
|
913
|
-
/**
|
|
914
|
-
* Appends style definition to the styles object.
|
|
915
|
-
*/
|
|
916
|
-
function appendStyleValue(stylesObject, nameOrPath, valueOrObject) {
|
|
917
|
-
let valueToSet = valueOrObject;
|
|
918
|
-
if (isObject(valueOrObject)) {
|
|
919
|
-
valueToSet = merge({}, get(stylesObject, nameOrPath), valueOrObject);
|
|
920
|
-
}
|
|
921
|
-
set(stylesObject, nameOrPath, valueToSet);
|
|
922
|
-
}
|
|
923
|
-
/**
|
|
924
|
-
* Modifies the `styles` deeply nested object by removing properties defined in `toRemove`.
|
|
925
|
-
*/
|
|
926
|
-
function removeStyles(styles, toRemove) {
|
|
927
|
-
for (const key of Object.keys(toRemove)) {
|
|
928
|
-
if (styles[key] !== null &&
|
|
929
|
-
!Array.isArray(styles[key]) &&
|
|
930
|
-
typeof styles[key] == 'object' &&
|
|
931
|
-
typeof toRemove[key] == 'object') {
|
|
932
|
-
removeStyles(styles[key], toRemove[key]);
|
|
933
|
-
if (!Object.keys(styles[key]).length) {
|
|
934
|
-
delete styles[key];
|
|
935
|
-
}
|
|
936
|
-
}
|
|
937
|
-
else {
|
|
938
|
-
delete styles[key];
|
|
939
|
-
}
|
|
940
|
-
}
|
|
941
|
-
}
|