@master/css 2.0.0-rc.5 → 2.0.0-rc.51
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/README.md +4 -43
- package/dist/_virtual/_commonjsHelpers.cjs +10 -0
- package/dist/_virtual/_commonjsHelpers.mjs +7 -0
- package/dist/_virtual/css-escape.cjs +5 -0
- package/dist/_virtual/css-escape.mjs +3 -0
- package/dist/bin/index.cjs +11 -0
- package/dist/bin/index.d.ts +0 -1
- package/dist/bin/index.mjs +9 -0
- package/dist/common.cjs +159 -0
- package/dist/common.d.ts +26 -0
- package/dist/common.mjs +137 -0
- package/dist/config/animations.cjs +113 -0
- package/dist/config/animations.mjs +111 -0
- package/dist/config/at.cjs +14 -0
- package/dist/config/at.d.ts +11 -0
- package/dist/config/at.mjs +12 -0
- package/dist/config/functions.cjs +375 -0
- package/dist/config/functions.d.ts +1 -1
- package/dist/config/functions.mjs +373 -0
- package/dist/config/index.cjs +37 -0
- package/dist/config/index.d.ts +5 -62
- package/dist/config/index.mjs +27 -0
- package/dist/config/modes.cjs +8 -0
- package/dist/config/modes.d.ts +3 -0
- package/dist/config/modes.mjs +6 -0
- package/dist/config/rules.cjs +2327 -0
- package/dist/config/rules.d.ts +277 -274
- package/dist/config/rules.mjs +2325 -0
- package/dist/config/selectors.cjs +28 -0
- package/dist/config/selectors.d.ts +0 -1
- package/dist/config/selectors.mjs +26 -0
- package/dist/config/styles.cjs +2 -0
- package/dist/config/styles.d.ts +2 -2
- package/dist/config/styles.mjs +1 -0
- package/dist/config/utilities.cjs +204 -0
- package/dist/config/{semantics.d.ts → utilities.d.ts} +15 -5
- package/dist/config/utilities.mjs +202 -0
- package/dist/config/variables.cjs +484 -0
- package/dist/config/variables.d.ts +208 -312
- package/dist/config/variables.mjs +482 -0
- package/dist/core.cjs +664 -0
- package/dist/core.d.ts +44 -73
- package/dist/core.mjs +659 -0
- package/dist/index.cjs +81 -0
- package/dist/index.d.ts +20 -9
- package/dist/index.mjs +26 -0
- package/dist/layer.cjs +116 -0
- package/dist/layer.d.ts +13 -10
- package/dist/layer.mjs +114 -0
- package/dist/non-layer.cjs +65 -0
- package/dist/non-layer.d.ts +12 -0
- package/dist/non-layer.mjs +63 -0
- package/dist/polyfills/css-escape.cjs +9 -0
- package/dist/polyfills/css-escape.mjs +7 -0
- package/dist/polyfills/css-escape2.cjs +76 -0
- package/dist/polyfills/css-escape2.mjs +74 -0
- package/dist/rule.cjs +18 -0
- package/dist/rule.d.ts +6 -111
- package/dist/rule.mjs +16 -0
- package/dist/shared/utils/css-escape.cjs +56 -0
- package/dist/shared/utils/css-escape.mjs +54 -0
- package/dist/shared/utils/find-native-css-rule-index.cjs +12 -0
- package/dist/shared/utils/find-native-css-rule-index.mjs +10 -0
- package/dist/syntax-layer.cjs +291 -0
- package/dist/syntax-layer.d.ts +17 -0
- package/dist/syntax-layer.mjs +289 -0
- package/dist/syntax-rule-type.cjs +27 -0
- package/dist/syntax-rule-type.d.ts +28 -0
- package/dist/syntax-rule-type.mjs +25 -0
- package/dist/syntax-rule.cjs +717 -0
- package/dist/syntax-rule.d.ts +47 -0
- package/dist/syntax-rule.mjs +715 -0
- package/dist/types/config.cjs +2 -0
- package/dist/types/config.d.ts +70 -0
- package/dist/types/config.mjs +1 -0
- package/dist/types/global.cjs +2 -0
- package/dist/types/global.d.ts +3 -3
- package/dist/types/global.mjs +1 -0
- package/dist/types/syntax.cjs +2 -0
- package/dist/types/syntax.d.ts +106 -0
- package/dist/types/syntax.mjs +1 -0
- package/dist/utils/are-rule-at-equal.cjs +22 -0
- package/dist/utils/are-rule-at-equal.d.ts +2 -0
- package/dist/utils/are-rule-at-equal.mjs +20 -0
- package/dist/utils/are-rule-modes-equal.cjs +7 -0
- package/dist/utils/are-rule-modes-equal.d.ts +2 -0
- package/dist/utils/are-rule-modes-equal.mjs +5 -0
- package/dist/utils/are-rule-selectors-equal.cjs +29 -0
- package/dist/utils/are-rule-selectors-equal.d.ts +2 -0
- package/dist/utils/are-rule-selectors-equal.mjs +27 -0
- package/dist/utils/are-rule-states-equal.cjs +11 -0
- package/dist/utils/are-rule-states-equal.d.ts +2 -0
- package/dist/utils/are-rule-states-equal.mjs +9 -0
- package/dist/utils/are-rules-duplicated.cjs +17 -0
- package/dist/utils/are-rules-duplicated.d.ts +2 -0
- package/dist/utils/are-rules-duplicated.mjs +15 -0
- package/dist/utils/autofill-solid-style.cjs +31 -0
- package/dist/utils/autofill-solid-style.d.ts +3 -0
- package/dist/utils/autofill-solid-style.mjs +29 -0
- package/dist/utils/extend-config.cjs +73 -0
- package/dist/utils/extend-config.d.ts +2 -2
- package/dist/utils/extend-config.mjs +71 -0
- package/dist/utils/flatten-object.cjs +15 -0
- package/dist/utils/flatten-object.mjs +13 -0
- package/dist/utils/generate-css.cjs +13 -0
- package/dist/utils/generate-css.d.ts +2 -0
- package/dist/utils/generate-css.mjs +11 -0
- package/dist/utils/hex-to-rgb.cjs +30 -0
- package/dist/utils/hex-to-rgb.d.ts +1 -1
- package/dist/utils/hex-to-rgb.mjs +28 -0
- package/dist/utils/is-core-rule.cjs +9 -0
- package/dist/utils/is-core-rule.d.ts +1 -0
- package/dist/utils/is-core-rule.mjs +7 -0
- package/dist/utils/reorder-for-readable-classes.cjs +62 -0
- package/dist/{functions → utils}/reorder-for-readable-classes.d.ts +2 -6
- package/dist/utils/reorder-for-readable-classes.mjs +60 -0
- package/package.json +1 -1
- package/dist/bin/index.bundle.js +0 -36
- package/dist/config/media-queries.d.ts +0 -1
- package/dist/constants/base-unit-regex.d.ts +0 -1
- package/dist/constants/config-esm-text.d.ts +0 -1
- package/dist/constants/config-text.d.ts +0 -1
- package/dist/constants/config-ts-text.d.ts +0 -1
- package/dist/constants/start-symbol.d.ts +0 -6
- package/dist/functions/transform-color-with-opacity.d.ts +0 -1
- package/dist/index.bundle.js +0 -35
- package/dist/index.bundle.mjs +0 -35
- package/dist/tokens/screen-sizes.d.ts +0 -14
- package/dist/utils/concat-arrays.d.ts +0 -1
- package/dist/utils/get-type.d.ts +0 -7
- package/dist/utils/is-plain-object.d.ts +0 -8
- package/dist/utils/is-symbol.d.ts +0 -7
- package/dist/utils/rgb-to-hex.d.ts +0 -1
package/dist/core.mjs
ADDED
|
@@ -0,0 +1,659 @@
|
|
|
1
|
+
import { SyntaxRule } from './syntax-rule.mjs';
|
|
2
|
+
import { config } from './config/index.mjs';
|
|
3
|
+
import hexToRgb from './utils/hex-to-rgb.mjs';
|
|
4
|
+
import { flattenObject } from './utils/flatten-object.mjs';
|
|
5
|
+
import extendConfig from './utils/extend-config.mjs';
|
|
6
|
+
import SyntaxLayer from './syntax-layer.mjs';
|
|
7
|
+
import { Rule } from './rule.mjs';
|
|
8
|
+
import SyntaxRuleType from './syntax-rule-type.mjs';
|
|
9
|
+
import Layer from './layer.mjs';
|
|
10
|
+
import NonLayer from './non-layer.mjs';
|
|
11
|
+
|
|
12
|
+
class MasterCSS {
|
|
13
|
+
customConfig;
|
|
14
|
+
static config = config;
|
|
15
|
+
definedRules;
|
|
16
|
+
config;
|
|
17
|
+
classUsages;
|
|
18
|
+
layerStatementRule;
|
|
19
|
+
rules;
|
|
20
|
+
animationsNonLayer;
|
|
21
|
+
baseLayer;
|
|
22
|
+
themeLayer;
|
|
23
|
+
presetLayer;
|
|
24
|
+
componentsLayer;
|
|
25
|
+
generalLayer;
|
|
26
|
+
get text() {
|
|
27
|
+
return this.rules.sort((a, b)=>{
|
|
28
|
+
const order = [
|
|
29
|
+
'layer-statement',
|
|
30
|
+
'base',
|
|
31
|
+
'theme',
|
|
32
|
+
'preset',
|
|
33
|
+
'components',
|
|
34
|
+
'general'
|
|
35
|
+
];
|
|
36
|
+
const indexA = order.indexOf(a.name) === -1 ? Infinity : order.indexOf(a.name);
|
|
37
|
+
const indexB = order.indexOf(b.name) === -1 ? Infinity : order.indexOf(b.name);
|
|
38
|
+
return indexA - indexB;
|
|
39
|
+
}).map(({ text })=>text).join('');
|
|
40
|
+
}
|
|
41
|
+
constructor(customConfig){
|
|
42
|
+
this.customConfig = customConfig;
|
|
43
|
+
this.definedRules = [];
|
|
44
|
+
this.classUsages = new Map();
|
|
45
|
+
this.layerStatementRule = new Rule('layer-statement', [
|
|
46
|
+
{
|
|
47
|
+
text: '@layer base,theme,preset,components,general;'
|
|
48
|
+
}
|
|
49
|
+
]);
|
|
50
|
+
this.rules = [
|
|
51
|
+
this.layerStatementRule
|
|
52
|
+
];
|
|
53
|
+
this.animationsNonLayer = new NonLayer(this);
|
|
54
|
+
this.baseLayer = new SyntaxLayer('base', this);
|
|
55
|
+
this.themeLayer = new Layer('theme', this);
|
|
56
|
+
this.presetLayer = new SyntaxLayer('preset', this);
|
|
57
|
+
this.componentsLayer = new SyntaxLayer('components', this);
|
|
58
|
+
this.generalLayer = new SyntaxLayer('general', this);
|
|
59
|
+
this.config = customConfig?.override ? extendConfig(customConfig) : extendConfig(config, customConfig);
|
|
60
|
+
this.resolve();
|
|
61
|
+
if (this.constructor === MasterCSS) {
|
|
62
|
+
masterCSSs.push(this);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
resolve() {
|
|
66
|
+
this.components = {};
|
|
67
|
+
this.selectors = {};
|
|
68
|
+
this.variables = {};
|
|
69
|
+
this.at = {};
|
|
70
|
+
this.animations = {};
|
|
71
|
+
this.definedRules.length = 0;
|
|
72
|
+
const colorVariableNames = {
|
|
73
|
+
current: undefined,
|
|
74
|
+
currentColor: undefined,
|
|
75
|
+
transparent: undefined
|
|
76
|
+
};
|
|
77
|
+
const { components, selectors, variables, utilities, at, rules, animations } = this.config;
|
|
78
|
+
function escapeString(str) {
|
|
79
|
+
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
80
|
+
}
|
|
81
|
+
if (selectors) {
|
|
82
|
+
const resolvedSelectors = flattenObject(selectors);
|
|
83
|
+
for(const eachSelectorName in resolvedSelectors){
|
|
84
|
+
const eachResolvedSelectorText = resolvedSelectors[eachSelectorName];
|
|
85
|
+
const regexp = new RegExp(escapeString(eachSelectorName) + '(?![a-z-])');
|
|
86
|
+
for (const eachNewSelectorText of Array.isArray(eachResolvedSelectorText) ? eachResolvedSelectorText : [
|
|
87
|
+
eachResolvedSelectorText
|
|
88
|
+
]){
|
|
89
|
+
const vendor = eachNewSelectorText.match(/^::-[a-z]+-/m)?.[0] ?? '';
|
|
90
|
+
let selectorValues = this.selectors[vendor];
|
|
91
|
+
if (!selectorValues) {
|
|
92
|
+
selectorValues = this.selectors[vendor] = [];
|
|
93
|
+
}
|
|
94
|
+
let currentSelectValue = selectorValues.find(([_valueRegexp])=>_valueRegexp === regexp);
|
|
95
|
+
if (!currentSelectValue) {
|
|
96
|
+
currentSelectValue = [
|
|
97
|
+
regexp,
|
|
98
|
+
[]
|
|
99
|
+
];
|
|
100
|
+
selectorValues.push(currentSelectValue);
|
|
101
|
+
}
|
|
102
|
+
currentSelectValue[1].push(eachNewSelectorText);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (variables) {
|
|
107
|
+
const unexecutedAliasVariable = {};
|
|
108
|
+
const resolveVariable = (variableDefinition, name, mode)=>{
|
|
109
|
+
if (variableDefinition === undefined || variableDefinition === null) return;
|
|
110
|
+
const addVariable = (name, variable, replacedMode, alpha)=>{
|
|
111
|
+
if (variable === undefined) return;
|
|
112
|
+
const flatName = name.join('-');
|
|
113
|
+
const groups = name.slice(0, -1).filter(Boolean);
|
|
114
|
+
const key = (name[0] === '' ? '-' : '') + name[name.length - 1];
|
|
115
|
+
variable.key = key;
|
|
116
|
+
variable.name = flatName;
|
|
117
|
+
if (groups.length) variable.group = groups.join('.');
|
|
118
|
+
if (variable.type === 'color') {
|
|
119
|
+
if (alpha) {
|
|
120
|
+
const slashIndex = variable.value.indexOf('/');
|
|
121
|
+
variable = {
|
|
122
|
+
...variable,
|
|
123
|
+
value: slashIndex === -1 ? variable.value + ' / ' + (alpha.startsWith('0.') ? alpha.slice(1) : alpha) : variable.value.slice(0, slashIndex + 2) + String(+variable.value.slice(slashIndex + 2) * +alpha).slice(1)
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
colorVariableNames[flatName] = undefined;
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* resolve `variables.screen-*` to `at.*`
|
|
130
|
+
*/ if (variable.name.startsWith('screen-') && variable.type === 'number') {
|
|
131
|
+
this.at[variable.name.slice(7)] = variable.value;
|
|
132
|
+
}
|
|
133
|
+
const currentMode = replacedMode ?? mode;
|
|
134
|
+
if (currentMode !== undefined) {
|
|
135
|
+
if (Object.prototype.hasOwnProperty.call(this.variables, flatName)) {
|
|
136
|
+
const foundVariable = this.variables[flatName];
|
|
137
|
+
if (currentMode) {
|
|
138
|
+
if (!foundVariable.modes) {
|
|
139
|
+
foundVariable.modes = {};
|
|
140
|
+
}
|
|
141
|
+
foundVariable.modes[currentMode] = variable;
|
|
142
|
+
} else {
|
|
143
|
+
foundVariable.value = variable.value;
|
|
144
|
+
if (variable.type === 'color') {
|
|
145
|
+
foundVariable.space = variable.space;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
if (currentMode) {
|
|
150
|
+
const newVariable = {
|
|
151
|
+
key: variable.key,
|
|
152
|
+
name: variable.name,
|
|
153
|
+
group: variable.group,
|
|
154
|
+
type: variable.type,
|
|
155
|
+
modes: {
|
|
156
|
+
[currentMode]: variable
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
if (variable.type === 'color') {
|
|
160
|
+
newVariable.space = variable.space;
|
|
161
|
+
}
|
|
162
|
+
this.variables[flatName] = newVariable;
|
|
163
|
+
} else {
|
|
164
|
+
this.variables[flatName] = variable;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
this.variables[flatName] = variable;
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
if (typeof variableDefinition === 'object') {
|
|
172
|
+
if (Array.isArray(variableDefinition)) {
|
|
173
|
+
addVariable(name, {
|
|
174
|
+
type: 'string',
|
|
175
|
+
value: variableDefinition.join(',')
|
|
176
|
+
});
|
|
177
|
+
} else {
|
|
178
|
+
const keys = Object.keys(variableDefinition);
|
|
179
|
+
for (const eachKey of keys){
|
|
180
|
+
if (eachKey === '' || eachKey.startsWith('@')) {
|
|
181
|
+
resolveVariable(variableDefinition[eachKey], name, eachKey || keys.some((eachKey)=>eachKey.startsWith('@')) ? eachKey.slice(1) : undefined);
|
|
182
|
+
} else {
|
|
183
|
+
resolveVariable(variableDefinition[eachKey], [
|
|
184
|
+
...name,
|
|
185
|
+
eachKey
|
|
186
|
+
]);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
} else if (typeof variableDefinition === 'number') {
|
|
191
|
+
addVariable(name, {
|
|
192
|
+
type: 'number',
|
|
193
|
+
value: variableDefinition
|
|
194
|
+
});
|
|
195
|
+
addVariable([
|
|
196
|
+
'',
|
|
197
|
+
...name
|
|
198
|
+
], {
|
|
199
|
+
type: 'number',
|
|
200
|
+
value: variableDefinition * -1
|
|
201
|
+
});
|
|
202
|
+
} else if (typeof variableDefinition === 'string') {
|
|
203
|
+
const aliasResult = /^\$\((.*?)\)(?: ?\/ ?(.+?))?$/.exec(variableDefinition);
|
|
204
|
+
const flatName = name.join('-');
|
|
205
|
+
if (aliasResult) {
|
|
206
|
+
if (!Object.prototype.hasOwnProperty.call(unexecutedAliasVariable, flatName)) {
|
|
207
|
+
unexecutedAliasVariable[flatName] = {};
|
|
208
|
+
}
|
|
209
|
+
unexecutedAliasVariable[flatName][mode] = ()=>{
|
|
210
|
+
delete unexecutedAliasVariable[flatName][mode];
|
|
211
|
+
const [alias, aliasMode] = aliasResult[1].split('@');
|
|
212
|
+
if (alias) {
|
|
213
|
+
if (Object.prototype.hasOwnProperty.call(unexecutedAliasVariable, alias)) {
|
|
214
|
+
for (const mode of Object.keys(unexecutedAliasVariable[alias])){
|
|
215
|
+
unexecutedAliasVariable[alias][mode]?.();
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const aliasVariable = this.variables[alias];
|
|
219
|
+
if (aliasVariable) {
|
|
220
|
+
if (aliasMode === undefined && aliasVariable.modes) {
|
|
221
|
+
addVariable(name, {
|
|
222
|
+
type: aliasVariable.type,
|
|
223
|
+
value: aliasVariable.value,
|
|
224
|
+
space: aliasVariable.space
|
|
225
|
+
}, '', aliasResult[2]);
|
|
226
|
+
for(const mode in aliasVariable.modes){
|
|
227
|
+
addVariable(name, aliasVariable.modes[mode], mode, aliasResult[2]);
|
|
228
|
+
}
|
|
229
|
+
} else {
|
|
230
|
+
const variable = aliasMode !== undefined ? aliasVariable.modes?.[aliasMode] : aliasVariable;
|
|
231
|
+
if (variable) {
|
|
232
|
+
const newVariable = {
|
|
233
|
+
type: variable.type,
|
|
234
|
+
value: variable.value
|
|
235
|
+
};
|
|
236
|
+
if (variable.type === 'color') {
|
|
237
|
+
newVariable.space = variable.space;
|
|
238
|
+
}
|
|
239
|
+
addVariable(name, newVariable, undefined, aliasResult[2]);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
} else {
|
|
246
|
+
const hexColorResult = /^#([A-Fa-f0-9]{3,4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/.exec(variableDefinition);
|
|
247
|
+
if (hexColorResult) {
|
|
248
|
+
const [r, g, b, a] = hexToRgb(hexColorResult[1]);
|
|
249
|
+
addVariable(name, {
|
|
250
|
+
type: 'color',
|
|
251
|
+
value: `${r} ${g} ${b}${a === 1 ? '' : ' / ' + a}`,
|
|
252
|
+
space: 'rgb'
|
|
253
|
+
});
|
|
254
|
+
} else {
|
|
255
|
+
const rgbFunctionResult = /^rgb\( *([0-9]{1,3})(?: *, *| +)([0-9]{1,3})(?: *, *| +)([0-9]{1,3}) *(?:(?:,|\/) *(.*?) *)?\)$/.exec(variableDefinition);
|
|
256
|
+
if (rgbFunctionResult) {
|
|
257
|
+
addVariable(name, {
|
|
258
|
+
type: 'color',
|
|
259
|
+
value: rgbFunctionResult[1] + ' ' + rgbFunctionResult[2] + ' ' + rgbFunctionResult[3] + (rgbFunctionResult[4] ? ' / ' + (rgbFunctionResult[4].startsWith('0.') ? rgbFunctionResult[4].slice(1) : rgbFunctionResult[4]) : ''),
|
|
260
|
+
space: 'rgb'
|
|
261
|
+
});
|
|
262
|
+
} else {
|
|
263
|
+
const hslFunctionResult = /^hsl\((.*?)\)$/.exec(variableDefinition);
|
|
264
|
+
if (hslFunctionResult) {
|
|
265
|
+
addVariable(name, {
|
|
266
|
+
type: 'color',
|
|
267
|
+
value: hslFunctionResult[1],
|
|
268
|
+
space: 'hsl'
|
|
269
|
+
});
|
|
270
|
+
} else {
|
|
271
|
+
addVariable(name, {
|
|
272
|
+
type: 'string',
|
|
273
|
+
value: variableDefinition
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
};
|
|
281
|
+
for(const parnetKey in variables){
|
|
282
|
+
resolveVariable(variables[parnetKey], [
|
|
283
|
+
parnetKey
|
|
284
|
+
]);
|
|
285
|
+
}
|
|
286
|
+
// todo: address to the target variable
|
|
287
|
+
for (const name of Object.keys(unexecutedAliasVariable)){
|
|
288
|
+
for (const mode of Object.keys(unexecutedAliasVariable[name])){
|
|
289
|
+
unexecutedAliasVariable[name][mode]?.();
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
if (at) {
|
|
294
|
+
Object.assign(this.at, flattenObject(at));
|
|
295
|
+
}
|
|
296
|
+
if (animations) {
|
|
297
|
+
for(const animationName in animations){
|
|
298
|
+
const eachAnimation = this.animations[animationName] = {};
|
|
299
|
+
const eachKeyframes = animations[animationName];
|
|
300
|
+
for(const eachKeyframeValue in eachKeyframes){
|
|
301
|
+
const newValueByPropertyName = eachAnimation[eachKeyframeValue] = {};
|
|
302
|
+
const eachKeyframeDeclarations = eachKeyframes[eachKeyframeValue];
|
|
303
|
+
for(const propertyName in eachKeyframeDeclarations){
|
|
304
|
+
newValueByPropertyName[propertyName] = eachKeyframeDeclarations[propertyName];
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
const flattedStyles = components ? flattenObject(components) : {};
|
|
310
|
+
const utilityNames = Object.keys(flattedStyles);
|
|
311
|
+
const handleUtilityName = (utilityName)=>{
|
|
312
|
+
if (Object.prototype.hasOwnProperty.call(this.components, utilityName)) return;
|
|
313
|
+
const currentClass = this.components[utilityName] = [];
|
|
314
|
+
const className = flattedStyles[utilityName];
|
|
315
|
+
if (!className) return;
|
|
316
|
+
const classNames = className.replace(/(?:\n(?:\s*))+/g, ' ').trim().split(' ');
|
|
317
|
+
for (const eachClassName of classNames){
|
|
318
|
+
const handle = (className)=>{
|
|
319
|
+
if (!currentClass.includes(className)) {
|
|
320
|
+
currentClass.push(className);
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
if (utilityNames.includes(eachClassName)) {
|
|
324
|
+
handleUtilityName(eachClassName);
|
|
325
|
+
for (const parentClassName of this.components[eachClassName]){
|
|
326
|
+
handle(parentClassName);
|
|
327
|
+
}
|
|
328
|
+
} else {
|
|
329
|
+
handle(eachClassName);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
for (const eachUtilityName of utilityNames){
|
|
334
|
+
handleUtilityName(eachUtilityName);
|
|
335
|
+
}
|
|
336
|
+
if (rules || utilities) {
|
|
337
|
+
const rulesEntries = [];
|
|
338
|
+
if (utilities) {
|
|
339
|
+
for(const utilityName in utilities){
|
|
340
|
+
const declarations = utilities[utilityName];
|
|
341
|
+
rulesEntries.push([
|
|
342
|
+
utilityName,
|
|
343
|
+
{
|
|
344
|
+
declarations,
|
|
345
|
+
type: SyntaxRuleType.Utility
|
|
346
|
+
}
|
|
347
|
+
]);
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
if (rules) {
|
|
351
|
+
rulesEntries.push(...Object.entries(rules));
|
|
352
|
+
}
|
|
353
|
+
const rulesEntriesLength = rulesEntries.length;
|
|
354
|
+
const colorNames = Object.keys(colorVariableNames);
|
|
355
|
+
rulesEntries.sort((a, b)=>{
|
|
356
|
+
if (a[1].type !== b[1].type) {
|
|
357
|
+
return (b[1].type || 0) - (a[1].type || 0);
|
|
358
|
+
}
|
|
359
|
+
return b[0].localeCompare(a[0]);
|
|
360
|
+
}).forEach(([id, eachSyntaxRuleDefinition], index)=>{
|
|
361
|
+
const syntax = {
|
|
362
|
+
id,
|
|
363
|
+
keys: [],
|
|
364
|
+
variables: {},
|
|
365
|
+
matchers: {},
|
|
366
|
+
order: rulesEntriesLength - 1 - index,
|
|
367
|
+
definition: eachSyntaxRuleDefinition
|
|
368
|
+
};
|
|
369
|
+
if (!eachSyntaxRuleDefinition.unit) {
|
|
370
|
+
eachSyntaxRuleDefinition.unit = '';
|
|
371
|
+
}
|
|
372
|
+
if (!eachSyntaxRuleDefinition.separators) {
|
|
373
|
+
eachSyntaxRuleDefinition.separators = [
|
|
374
|
+
','
|
|
375
|
+
];
|
|
376
|
+
}
|
|
377
|
+
this.definedRules.push(syntax);
|
|
378
|
+
const { matcher, type, subkey, ambiguousKeys, ambiguousValues, sign } = eachSyntaxRuleDefinition;
|
|
379
|
+
if (type === SyntaxRuleType.Utility) {
|
|
380
|
+
syntax.id = '.' + id;
|
|
381
|
+
syntax.matchers.arbitrary = new RegExp('^' + escapeString(id) + '(?=!|\\*|>|\\+|~|:|\\[|@|_|\\.|$)', 'm');
|
|
382
|
+
}
|
|
383
|
+
// todo: 不可使用 startsWith 判斷,應改為更精準的從 config.variables 取得目標變數群組,但 config.variables 中的值還沒被 resolve 像是 Array
|
|
384
|
+
const addResolvedVariables = (groupName)=>{
|
|
385
|
+
for(const eachVariableName in this.variables){
|
|
386
|
+
const eachVariable = this.variables[eachVariableName];
|
|
387
|
+
if (eachVariable.group === groupName) {
|
|
388
|
+
syntax.variables[eachVariable.key] = eachVariable;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
};
|
|
392
|
+
// 1. custom `config.rules[id].variables`
|
|
393
|
+
if (eachSyntaxRuleDefinition.variables) {
|
|
394
|
+
for (const eachVariableGroup of eachSyntaxRuleDefinition.variables){
|
|
395
|
+
addResolvedVariables(eachVariableGroup);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
// 2. custom `config.variables`
|
|
399
|
+
addResolvedVariables(id);
|
|
400
|
+
const keys = [];
|
|
401
|
+
let { key } = eachSyntaxRuleDefinition;
|
|
402
|
+
if (type === SyntaxRuleType.NativeShorthand || type === SyntaxRuleType.Native) {
|
|
403
|
+
if (!key) eachSyntaxRuleDefinition.key = key = id;
|
|
404
|
+
keys.push(id);
|
|
405
|
+
}
|
|
406
|
+
if (sign) {
|
|
407
|
+
syntax.matchers.arbitrary = new RegExp(`^${sign}[^!*>+~:[@_]+\\|`);
|
|
408
|
+
} else if (!matcher) {
|
|
409
|
+
const colorsPatten = colorNames.join('|');
|
|
410
|
+
if (!key && !subkey) {
|
|
411
|
+
keys.push(id);
|
|
412
|
+
} else {
|
|
413
|
+
if (key && !keys.includes(key)) keys.push(key);
|
|
414
|
+
if (subkey) keys.push(subkey);
|
|
415
|
+
if (type === SyntaxRuleType.Shorthand) {
|
|
416
|
+
keys.push(id);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
if (ambiguousKeys?.length) {
|
|
420
|
+
const ambiguousKeyPattern = ambiguousKeys.length > 1 ? `(?:${ambiguousKeys.join('|')})` : ambiguousKeys[0];
|
|
421
|
+
const variableKeys = Object.keys(syntax.variables);
|
|
422
|
+
if (ambiguousValues?.length) {
|
|
423
|
+
const ambiguousValuePatterns = [];
|
|
424
|
+
for (const eachAmbiguousValue of ambiguousValues){
|
|
425
|
+
if (eachAmbiguousValue instanceof RegExp) {
|
|
426
|
+
ambiguousValuePatterns.push(eachAmbiguousValue.source.replace('\\$colors', colorsPatten));
|
|
427
|
+
} else {
|
|
428
|
+
ambiguousValuePatterns.unshift(`${eachAmbiguousValue}(?:\\b|_)`);
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
syntax.matchers.value = new RegExp(`^${ambiguousKeyPattern}:(?:${ambiguousValuePatterns.join('|')})[^|]*?(?:@|$)`);
|
|
432
|
+
}
|
|
433
|
+
if (variableKeys.length) {
|
|
434
|
+
syntax.matchers.variable = new RegExp(`^${ambiguousKeyPattern}:(?:${variableKeys.join('|')}(?![a-zA-Z0-9-]))[^|]*?(?:@|$)`);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
} else {
|
|
438
|
+
syntax.matchers.arbitrary = matcher;
|
|
439
|
+
}
|
|
440
|
+
if (keys.length) {
|
|
441
|
+
syntax.keys = keys;
|
|
442
|
+
syntax.matchers.key = new RegExp(`^${keys.length > 1 ? `(${keys.join('|')})` : keys[0]}:.`);
|
|
443
|
+
}
|
|
444
|
+
});
|
|
445
|
+
}
|
|
446
|
+
for(const utilityName in this.components){
|
|
447
|
+
const syntaxRulesByStateToken = this.components[utilityName].map((eachSyntax)=>this.create(eachSyntax)).filter((eachSyntax)=>eachSyntax?.text).reduce((obj, eachSyntaxRule)=>{
|
|
448
|
+
if (eachSyntaxRule.stateToken in obj) {
|
|
449
|
+
obj[eachSyntaxRule.stateToken].push(eachSyntaxRule);
|
|
450
|
+
} else {
|
|
451
|
+
obj[eachSyntaxRule.stateToken] = [
|
|
452
|
+
eachSyntaxRule
|
|
453
|
+
];
|
|
454
|
+
}
|
|
455
|
+
return obj;
|
|
456
|
+
}, {});
|
|
457
|
+
this.components[utilityName] = Object.keys(syntaxRulesByStateToken).map((stateToken)=>{
|
|
458
|
+
const syntaxRules = syntaxRulesByStateToken[stateToken];
|
|
459
|
+
return syntaxRules.length === 1 ? syntaxRules[0].name : '{' + syntaxRulesByStateToken[stateToken].map((eachSyntaxRule)=>eachSyntaxRule.name.slice(0, eachSyntaxRule.name.length - eachSyntaxRule.stateToken.length)).join(';') + '}' + stateToken;
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Match check if Master CSS syntax
|
|
465
|
+
* @param className
|
|
466
|
+
* @returns css text
|
|
467
|
+
*/ match(className) {
|
|
468
|
+
/**
|
|
469
|
+
* 1. variable
|
|
470
|
+
* @example fg:primary bg:blue
|
|
471
|
+
*/ for (const eachSyntax of this.definedRules){
|
|
472
|
+
if (eachSyntax.matchers.variable?.test(className)) return eachSyntax;
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* 2. value (ambiguous.key * ambiguous.values)
|
|
476
|
+
* @example bg:current box:content font:12
|
|
477
|
+
*/ for (const eachSyntax of this.definedRules){
|
|
478
|
+
if (eachSyntax.matchers.value?.test(className)) return eachSyntax;
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* 3. full key
|
|
482
|
+
* @example text-align:center color:blue-40
|
|
483
|
+
*/ for (const eachSyntax of this.definedRules){
|
|
484
|
+
if (eachSyntax.matchers.key?.test(className)) return eachSyntax;
|
|
485
|
+
}
|
|
486
|
+
/**
|
|
487
|
+
* 4. arbitrary
|
|
488
|
+
* @example custom RegExp, utility
|
|
489
|
+
*/ for (const eachSyntax of this.definedRules){
|
|
490
|
+
if (eachSyntax.matchers.arbitrary?.test(className)) return eachSyntax;
|
|
491
|
+
}
|
|
492
|
+
}
|
|
493
|
+
/**
|
|
494
|
+
* Generate syntax rules from class name
|
|
495
|
+
* @param className
|
|
496
|
+
* @returns SyntaxRule[]
|
|
497
|
+
*/ generate(className, mode) {
|
|
498
|
+
let syntaxRules = [];
|
|
499
|
+
if (Object.prototype.hasOwnProperty.call(this.components, className)) {
|
|
500
|
+
syntaxRules = this.components[className].map((eachSyntax)=>this.create(eachSyntax, className, mode));
|
|
501
|
+
} else {
|
|
502
|
+
const atIndex = className.indexOf('@');
|
|
503
|
+
if (atIndex !== -1) {
|
|
504
|
+
const name = className.slice(0, atIndex);
|
|
505
|
+
if (Object.prototype.hasOwnProperty.call(this.components, name)) {
|
|
506
|
+
const atToken = className.slice(atIndex);
|
|
507
|
+
syntaxRules = this.components[name].map((eachSyntax)=>this.create(eachSyntax + atToken, className, mode));
|
|
508
|
+
} else {
|
|
509
|
+
syntaxRules = [
|
|
510
|
+
this.create(className, undefined, mode)
|
|
511
|
+
];
|
|
512
|
+
}
|
|
513
|
+
} else {
|
|
514
|
+
syntaxRules = [
|
|
515
|
+
this.create(className, undefined, mode)
|
|
516
|
+
];
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
return syntaxRules.filter((eachSyntax)=>eachSyntax?.text);
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Create syntax rule from given class name
|
|
523
|
+
* @param className
|
|
524
|
+
* @returns SyntaxRule
|
|
525
|
+
*/ create(className, fixedClass, mode) {
|
|
526
|
+
const syntaxRule = this.generalLayer.rules.find(({ key })=>key === (fixedClass ? fixedClass + ' ' : '') + className);
|
|
527
|
+
if (syntaxRule) return syntaxRule;
|
|
528
|
+
const registeredRule = this.match(className);
|
|
529
|
+
if (registeredRule) return new SyntaxRule(className, this, registeredRule, fixedClass, mode);
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Create syntax rule from given selector text
|
|
533
|
+
* @param selectorText
|
|
534
|
+
*/ createFromSelectorText(selectorText) {
|
|
535
|
+
const selectorTextSplits = selectorText.split(' ');
|
|
536
|
+
for(let i = 0; i < selectorTextSplits.length; i++){
|
|
537
|
+
const eachField = selectorTextSplits[i];
|
|
538
|
+
const modeSelector = this.getModeSelector(eachField);
|
|
539
|
+
if (i === 0 && eachField === modeSelector) continue;
|
|
540
|
+
if (eachField[0] === '.') {
|
|
541
|
+
const eachFieldName = eachField.slice(1);
|
|
542
|
+
let className = '';
|
|
543
|
+
for(let l = 0; l < eachFieldName.length; l++){
|
|
544
|
+
const char = eachFieldName[l];
|
|
545
|
+
const nextChar = eachFieldName[l + 1];
|
|
546
|
+
if (char === '\\') {
|
|
547
|
+
l++;
|
|
548
|
+
if (nextChar !== '\\') {
|
|
549
|
+
className += nextChar;
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
} else if ([
|
|
553
|
+
'.',
|
|
554
|
+
'#',
|
|
555
|
+
'[',
|
|
556
|
+
'!',
|
|
557
|
+
'*',
|
|
558
|
+
'>',
|
|
559
|
+
'+',
|
|
560
|
+
'~',
|
|
561
|
+
':'
|
|
562
|
+
].includes(char)) {
|
|
563
|
+
break;
|
|
564
|
+
}
|
|
565
|
+
className += char;
|
|
566
|
+
}
|
|
567
|
+
const syntaxRules = this.generate(className);
|
|
568
|
+
if (syntaxRules.length) return syntaxRules;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
return [];
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* 根據蒐集到的所有 DOM class 重新 create
|
|
575
|
+
*/ refresh(customConfig) {
|
|
576
|
+
this.reset();
|
|
577
|
+
if (customConfig) {
|
|
578
|
+
this.customConfig = customConfig;
|
|
579
|
+
} else {
|
|
580
|
+
customConfig = this.customConfig;
|
|
581
|
+
}
|
|
582
|
+
// @ts-ignore
|
|
583
|
+
this.config = customConfig?.override ? extendConfig(customConfig) : extendConfig(config, customConfig);
|
|
584
|
+
this.resolve();
|
|
585
|
+
/**
|
|
586
|
+
* 拿當前所有的 classNames 按照最新的 colors, config.rules 匹配並生成新的 style
|
|
587
|
+
* 所以 refresh 過後 rules 可能會變多也可能會變少
|
|
588
|
+
*/ this.classUsages.forEach((_, className)=>{
|
|
589
|
+
this.add(className);
|
|
590
|
+
});
|
|
591
|
+
return this;
|
|
592
|
+
}
|
|
593
|
+
reset() {
|
|
594
|
+
this.baseLayer.reset();
|
|
595
|
+
this.themeLayer.reset();
|
|
596
|
+
this.presetLayer.reset();
|
|
597
|
+
this.componentsLayer.reset();
|
|
598
|
+
this.generalLayer.reset();
|
|
599
|
+
this.animationsNonLayer.reset();
|
|
600
|
+
return this;
|
|
601
|
+
}
|
|
602
|
+
destroy() {
|
|
603
|
+
this.reset();
|
|
604
|
+
this.classUsages.clear();
|
|
605
|
+
masterCSSs.splice(masterCSSs.indexOf(this), 1);
|
|
606
|
+
return this;
|
|
607
|
+
}
|
|
608
|
+
add(...classNames) {
|
|
609
|
+
for (const className of classNames){
|
|
610
|
+
this.generate(className).forEach((eachSyntaxRule)=>eachSyntaxRule.layer.insert(eachSyntaxRule));
|
|
611
|
+
}
|
|
612
|
+
return this;
|
|
613
|
+
}
|
|
614
|
+
remove(...classNames) {
|
|
615
|
+
/**
|
|
616
|
+
* class name 從 DOM tree 中被移除,
|
|
617
|
+
* 匹配並刪除對應的 rule
|
|
618
|
+
*/ for (const className of classNames){
|
|
619
|
+
if (Object.prototype.hasOwnProperty.call(this.components, className)) {
|
|
620
|
+
for (const eachSyntax of this.components[className]){
|
|
621
|
+
this.componentsLayer.delete(className + ' ' + eachSyntax);
|
|
622
|
+
}
|
|
623
|
+
} else {
|
|
624
|
+
const atIndex = className.indexOf('@');
|
|
625
|
+
if (atIndex !== -1) {
|
|
626
|
+
const name = className.slice(0, atIndex);
|
|
627
|
+
if (Object.prototype.hasOwnProperty.call(this.components, name)) {
|
|
628
|
+
const atToken = className.slice(atIndex);
|
|
629
|
+
for (const eachSyntax of this.components[name]){
|
|
630
|
+
this.componentsLayer.delete(eachSyntax + atToken + ' ' + className);
|
|
631
|
+
}
|
|
632
|
+
} else {
|
|
633
|
+
this.generalLayer.delete(className);
|
|
634
|
+
}
|
|
635
|
+
} else {
|
|
636
|
+
this.generalLayer.delete(className);
|
|
637
|
+
}
|
|
638
|
+
}
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
getModeSelector(modeName) {
|
|
642
|
+
const mode = this.config.modes?.[modeName];
|
|
643
|
+
if (mode) {
|
|
644
|
+
switch(mode){
|
|
645
|
+
case 'class':
|
|
646
|
+
return '.' + modeName;
|
|
647
|
+
case 'host':
|
|
648
|
+
return ':host(.' + modeName + ')';
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
const masterCSSs = [];
|
|
654
|
+
(()=>{
|
|
655
|
+
globalThis.MasterCSS = MasterCSS;
|
|
656
|
+
globalThis.masterCSSs = masterCSSs;
|
|
657
|
+
})();
|
|
658
|
+
|
|
659
|
+
export { MasterCSS as default, masterCSSs };
|