@master/css 2.0.0-rc.26 → 2.0.0-rc.28
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/dist/common.cjs +124 -0
- package/dist/common.d.ts +22 -0
- package/dist/common.mjs +106 -0
- package/dist/config/functions.cjs +40 -41
- package/dist/config/functions.mjs +39 -40
- package/dist/config/index.cjs +6 -5
- package/dist/config/index.d.ts +13 -11
- package/dist/config/index.mjs +6 -5
- 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/queries.cjs +4 -0
- package/dist/config/queries.d.ts +4 -0
- package/dist/config/queries.mjs +4 -0
- package/dist/config/rules.cjs +964 -851
- package/dist/config/rules.d.ts +8 -5
- package/dist/config/rules.mjs +966 -852
- package/dist/config/selectors.cjs +0 -1
- package/dist/config/selectors.d.ts +0 -1
- package/dist/config/selectors.mjs +0 -1
- package/dist/config/{semantics.cjs → utilities.cjs} +2 -5
- package/dist/config/{semantics.d.ts → utilities.d.ts} +2 -5
- package/dist/config/{semantics.mjs → utilities.mjs} +2 -5
- package/dist/core.cjs +193 -133
- package/dist/core.d.ts +18 -14
- package/dist/core.mjs +194 -134
- package/dist/index.cjs +36 -16
- package/dist/index.d.ts +13 -8
- package/dist/index.mjs +14 -11
- package/dist/layer.cjs +25 -20
- package/dist/layer.d.ts +26 -9
- package/dist/layer.mjs +22 -19
- package/dist/rule.cjs +46 -52
- package/dist/rule.d.ts +37 -24
- package/dist/rule.mjs +46 -52
- 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 +1 -1
- package/dist/utils/hex-to-rgb.d.ts +1 -1
- package/dist/utils/hex-to-rgb.mjs +1 -1
- 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/{functions → utils}/reorder-for-readable-classes.cjs +5 -11
- package/dist/{functions → utils}/reorder-for-readable-classes.d.ts +1 -5
- package/dist/{functions → utils}/reorder-for-readable-classes.mjs +6 -12
- package/package.json +1 -1
- package/dist/constants/base-unit-regex.cjs +0 -5
- package/dist/constants/base-unit-regex.d.ts +0 -1
- package/dist/constants/base-unit-regex.mjs +0 -3
- package/dist/constants/start-symbol.cjs +0 -10
- package/dist/constants/start-symbol.d.ts +0 -6
- package/dist/constants/start-symbol.mjs +0 -8
- package/dist/utils/concat-arrays.cjs +0 -12
- package/dist/utils/concat-arrays.d.ts +0 -1
- package/dist/utils/concat-arrays.mjs +0 -10
- package/dist/utils/get-type.cjs +0 -12
- package/dist/utils/get-type.d.ts +0 -7
- package/dist/utils/get-type.mjs +0 -10
- package/dist/utils/is-plain-object.cjs +0 -15
- package/dist/utils/is-plain-object.d.ts +0 -8
- package/dist/utils/is-plain-object.mjs +0 -13
- package/dist/utils/is-symbol.cjs +0 -14
- package/dist/utils/is-symbol.d.ts +0 -7
- package/dist/utils/is-symbol.mjs +0 -12
- package/dist/utils/rgb-to-hex.cjs +0 -7
- package/dist/utils/rgb-to-hex.d.ts +0 -1
- package/dist/utils/rgb-to-hex.mjs +0 -5
- /package/dist/{functions → utils}/are-rule-modes-equal.cjs +0 -0
- /package/dist/{functions → utils}/are-rule-modes-equal.d.ts +0 -0
- /package/dist/{functions → utils}/are-rule-modes-equal.mjs +0 -0
- /package/dist/{functions → utils}/are-rule-queries-equal.cjs +0 -0
- /package/dist/{functions → utils}/are-rule-queries-equal.d.ts +0 -0
- /package/dist/{functions → utils}/are-rule-queries-equal.mjs +0 -0
- /package/dist/{functions → utils}/are-rule-selectors-equal.cjs +0 -0
- /package/dist/{functions → utils}/are-rule-selectors-equal.d.ts +0 -0
- /package/dist/{functions → utils}/are-rule-selectors-equal.mjs +0 -0
- /package/dist/{functions → utils}/are-rule-states-equal.cjs +0 -0
- /package/dist/{functions → utils}/are-rule-states-equal.d.ts +0 -0
- /package/dist/{functions → utils}/are-rule-states-equal.mjs +0 -0
- /package/dist/{functions → utils}/are-rules-duplicated.cjs +0 -0
- /package/dist/{functions → utils}/are-rules-duplicated.d.ts +0 -0
- /package/dist/{functions → utils}/are-rules-duplicated.mjs +0 -0
- /package/dist/{functions → utils}/extend-config.cjs +0 -0
- /package/dist/{functions → utils}/extend-config.d.ts +0 -0
- /package/dist/{functions → utils}/extend-config.mjs +0 -0
package/dist/core.d.ts
CHANGED
|
@@ -1,25 +1,30 @@
|
|
|
1
1
|
import { Rule, type NativeRule, type RegisteredRule } from './rule';
|
|
2
2
|
import type { Config, AnimationDefinitions } from './config';
|
|
3
3
|
import './types/global';
|
|
4
|
-
type
|
|
4
|
+
type VariableCommon = {
|
|
5
|
+
usage?: number;
|
|
6
|
+
group?: string;
|
|
7
|
+
name: string;
|
|
8
|
+
key: string;
|
|
9
|
+
modes?: {
|
|
10
|
+
[mode: string]: TypeVariable;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export type StringVariable = {
|
|
5
14
|
type: 'string';
|
|
6
15
|
value: string;
|
|
7
|
-
}
|
|
16
|
+
};
|
|
17
|
+
export type NumberVariable = {
|
|
8
18
|
type: 'number';
|
|
9
19
|
value: number;
|
|
10
|
-
}
|
|
20
|
+
};
|
|
21
|
+
export type ColorVariable = {
|
|
11
22
|
type: 'color';
|
|
12
23
|
value: string;
|
|
13
24
|
space: 'rgb' | 'hsl';
|
|
14
25
|
};
|
|
15
|
-
export type
|
|
16
|
-
|
|
17
|
-
space?: any;
|
|
18
|
-
usage?: number;
|
|
19
|
-
modes?: {
|
|
20
|
-
[mode: string]: VariableValue;
|
|
21
|
-
};
|
|
22
|
-
};
|
|
26
|
+
export type TypeVariable = StringVariable | NumberVariable | ColorVariable;
|
|
27
|
+
export type Variable = TypeVariable & VariableCommon;
|
|
23
28
|
export default class MasterCSS {
|
|
24
29
|
customConfig: Config;
|
|
25
30
|
static config: Config;
|
|
@@ -27,8 +32,7 @@ export default class MasterCSS {
|
|
|
27
32
|
readonly ruleBy: Record<string, Rule>;
|
|
28
33
|
readonly classesUsage: Record<string, number>;
|
|
29
34
|
readonly config: Config;
|
|
30
|
-
readonly
|
|
31
|
-
readonly RegisteredRules: RegisteredRule[];
|
|
35
|
+
readonly Rules: RegisteredRule[];
|
|
32
36
|
constructor(customConfig?: Config);
|
|
33
37
|
resolve(): void;
|
|
34
38
|
/**
|
|
@@ -72,7 +76,7 @@ export default class MasterCSS {
|
|
|
72
76
|
* 11. media width where selectors
|
|
73
77
|
* 12. media width selectors
|
|
74
78
|
*/
|
|
75
|
-
insert(rule: Rule):
|
|
79
|
+
insert(rule: Rule): number | undefined;
|
|
76
80
|
get text(): string;
|
|
77
81
|
handleRuleWithAnimationNames(rule: Rule, initializing?: boolean): void;
|
|
78
82
|
handleRuleWithVariableNames(rule: Rule, initializing?: boolean): void;
|
package/dist/core.mjs
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { Rule } from './rule.mjs';
|
|
2
2
|
import { config } from './config/index.mjs';
|
|
3
|
-
import
|
|
4
|
-
import
|
|
3
|
+
import Layer from './layer.mjs';
|
|
4
|
+
import hexToRgb from './utils/hex-to-rgb.mjs';
|
|
5
5
|
import { flattenObject } from './utils/flatten-object.mjs';
|
|
6
|
-
import extendConfig from './
|
|
6
|
+
import extendConfig from './utils/extend-config.mjs';
|
|
7
7
|
|
|
8
8
|
class MasterCSS {
|
|
9
9
|
customConfig;
|
|
@@ -12,15 +12,13 @@ class MasterCSS {
|
|
|
12
12
|
ruleBy;
|
|
13
13
|
classesUsage;
|
|
14
14
|
config;
|
|
15
|
-
|
|
16
|
-
RegisteredRules;
|
|
15
|
+
Rules;
|
|
17
16
|
constructor(customConfig = config){
|
|
18
17
|
this.customConfig = customConfig;
|
|
19
18
|
this.rules = [];
|
|
20
19
|
this.ruleBy = {};
|
|
21
20
|
this.classesUsage = {};
|
|
22
|
-
this.
|
|
23
|
-
this.RegisteredRules = [];
|
|
21
|
+
this.Rules = [];
|
|
24
22
|
if (!customConfig?.override) {
|
|
25
23
|
this.config = extendConfig(config, customConfig);
|
|
26
24
|
} else {
|
|
@@ -38,8 +36,7 @@ class MasterCSS {
|
|
|
38
36
|
this.variables = {};
|
|
39
37
|
this.queries = {};
|
|
40
38
|
this.animations = {};
|
|
41
|
-
this.
|
|
42
|
-
this.RegisteredSemanticRules.length = 0;
|
|
39
|
+
this.Rules.length = 0;
|
|
43
40
|
this.variablesNativeRules = {};
|
|
44
41
|
this.hasKeyframesRule = false;
|
|
45
42
|
const colorVariableNames = {
|
|
@@ -47,7 +44,7 @@ class MasterCSS {
|
|
|
47
44
|
currentColor: undefined,
|
|
48
45
|
transparent: undefined
|
|
49
46
|
};
|
|
50
|
-
const { styles, selectors, variables,
|
|
47
|
+
const { styles, selectors, variables, utilities, queries, rules, animations } = this.config;
|
|
51
48
|
function escapeString(str) {
|
|
52
49
|
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
53
50
|
}
|
|
@@ -78,89 +75,104 @@ class MasterCSS {
|
|
|
78
75
|
}
|
|
79
76
|
if (variables) {
|
|
80
77
|
const unexecutedAliasVariable = {};
|
|
81
|
-
const
|
|
82
|
-
if (!
|
|
83
|
-
const addVariable = (name,
|
|
84
|
-
if (
|
|
85
|
-
|
|
78
|
+
const resolveVariable = (variableDefinition, name, mode)=>{
|
|
79
|
+
if (!variableDefinition === undefined || variableDefinition === null) return;
|
|
80
|
+
const addVariable = (name, variable, replacedMode, alpha)=>{
|
|
81
|
+
if (variable === undefined) return;
|
|
82
|
+
const flatName = name.join('-');
|
|
83
|
+
const groups = name.slice(0, -1).filter(Boolean);
|
|
84
|
+
const key = (name[0] === '' ? '-' : '') + name[name.length - 1];
|
|
85
|
+
variable.key = key;
|
|
86
|
+
variable.name = flatName;
|
|
87
|
+
if (groups.length) variable.group = groups.join('.');
|
|
88
|
+
if (variable.type === 'color') {
|
|
86
89
|
if (alpha) {
|
|
87
|
-
const slashIndex =
|
|
88
|
-
|
|
89
|
-
...
|
|
90
|
-
value: slashIndex === -1 ?
|
|
90
|
+
const slashIndex = variable.value.indexOf('/');
|
|
91
|
+
variable = {
|
|
92
|
+
...variable,
|
|
93
|
+
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)
|
|
91
94
|
};
|
|
92
95
|
}
|
|
93
|
-
colorVariableNames[
|
|
96
|
+
colorVariableNames[flatName] = undefined;
|
|
94
97
|
}
|
|
95
98
|
const currentMode = replacedMode ?? mode;
|
|
96
99
|
if (currentMode !== undefined) {
|
|
97
|
-
if (Object.prototype.hasOwnProperty.call(this.variables,
|
|
98
|
-
const
|
|
100
|
+
if (Object.prototype.hasOwnProperty.call(this.variables, flatName)) {
|
|
101
|
+
const foundVariable = this.variables[flatName];
|
|
99
102
|
if (currentMode) {
|
|
100
|
-
if (!
|
|
101
|
-
|
|
103
|
+
if (!foundVariable.modes) {
|
|
104
|
+
foundVariable.modes = {};
|
|
102
105
|
}
|
|
103
|
-
|
|
106
|
+
foundVariable.modes[currentMode] = variable;
|
|
104
107
|
} else {
|
|
105
|
-
|
|
106
|
-
if (
|
|
107
|
-
|
|
108
|
+
foundVariable.value = variable.value;
|
|
109
|
+
if (variable.type === 'color') {
|
|
110
|
+
foundVariable.space = variable.space;
|
|
108
111
|
}
|
|
109
112
|
}
|
|
110
113
|
} else {
|
|
111
114
|
if (currentMode) {
|
|
112
115
|
const newVariable = {
|
|
113
|
-
|
|
116
|
+
key: variable.key,
|
|
117
|
+
name: variable.name,
|
|
118
|
+
group: variable.group,
|
|
119
|
+
type: variable.type,
|
|
114
120
|
modes: {
|
|
115
|
-
[currentMode]:
|
|
121
|
+
[currentMode]: variable
|
|
116
122
|
}
|
|
117
123
|
};
|
|
118
|
-
if (
|
|
119
|
-
newVariable.space =
|
|
124
|
+
if (variable.type === 'color') {
|
|
125
|
+
newVariable.space = variable.space;
|
|
120
126
|
}
|
|
121
|
-
this.variables[
|
|
127
|
+
this.variables[flatName] = newVariable;
|
|
122
128
|
} else {
|
|
123
|
-
this.variables[
|
|
129
|
+
this.variables[flatName] = variable;
|
|
124
130
|
}
|
|
125
131
|
}
|
|
126
132
|
} else {
|
|
127
|
-
this.variables[
|
|
133
|
+
this.variables[flatName] = variable;
|
|
128
134
|
}
|
|
129
135
|
};
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
if (Array.isArray(variable)) {
|
|
136
|
+
if (typeof variableDefinition === 'object') {
|
|
137
|
+
if (Array.isArray(variableDefinition)) {
|
|
133
138
|
addVariable(name, {
|
|
134
139
|
type: 'string',
|
|
135
|
-
value:
|
|
140
|
+
value: variableDefinition.join(',')
|
|
136
141
|
});
|
|
137
142
|
} else {
|
|
138
|
-
const keys = Object.keys(
|
|
143
|
+
const keys = Object.keys(variableDefinition);
|
|
139
144
|
for (const eachKey of keys){
|
|
140
145
|
if (eachKey === '' || eachKey.startsWith('@')) {
|
|
141
|
-
|
|
146
|
+
resolveVariable(variableDefinition[eachKey], name, eachKey || keys.some((eachKey)=>eachKey.startsWith('@')) ? eachKey.slice(1) : undefined);
|
|
142
147
|
} else {
|
|
143
|
-
|
|
148
|
+
resolveVariable(variableDefinition[eachKey], [
|
|
149
|
+
...name,
|
|
150
|
+
eachKey
|
|
151
|
+
]);
|
|
144
152
|
}
|
|
145
153
|
}
|
|
146
154
|
}
|
|
147
|
-
} else if (
|
|
155
|
+
} else if (typeof variableDefinition === 'number') {
|
|
148
156
|
addVariable(name, {
|
|
149
157
|
type: 'number',
|
|
150
|
-
value:
|
|
158
|
+
value: variableDefinition
|
|
151
159
|
});
|
|
152
|
-
addVariable(
|
|
160
|
+
addVariable([
|
|
161
|
+
'',
|
|
162
|
+
...name
|
|
163
|
+
], {
|
|
153
164
|
type: 'number',
|
|
154
|
-
value:
|
|
165
|
+
value: variableDefinition * -1
|
|
155
166
|
});
|
|
156
|
-
} else if (
|
|
157
|
-
const aliasResult = /^\$\((.*?)\)(?: ?\/ ?(.+?))?$/.exec(
|
|
167
|
+
} else if (typeof variableDefinition === 'string') {
|
|
168
|
+
const aliasResult = /^\$\((.*?)\)(?: ?\/ ?(.+?))?$/.exec(variableDefinition);
|
|
169
|
+
const flatName = name.join('-');
|
|
158
170
|
if (aliasResult) {
|
|
159
|
-
if (!Object.prototype.hasOwnProperty.call(unexecutedAliasVariable,
|
|
160
|
-
unexecutedAliasVariable[
|
|
171
|
+
if (!Object.prototype.hasOwnProperty.call(unexecutedAliasVariable, flatName)) {
|
|
172
|
+
unexecutedAliasVariable[flatName] = {};
|
|
161
173
|
}
|
|
162
|
-
unexecutedAliasVariable[
|
|
163
|
-
delete unexecutedAliasVariable[
|
|
174
|
+
unexecutedAliasVariable[flatName][mode] = ()=>{
|
|
175
|
+
delete unexecutedAliasVariable[flatName][mode];
|
|
164
176
|
const [alias, aliasMode] = aliasResult[1].split('@');
|
|
165
177
|
if (alias) {
|
|
166
178
|
if (Object.prototype.hasOwnProperty.call(unexecutedAliasVariable, alias)) {
|
|
@@ -196,7 +208,7 @@ class MasterCSS {
|
|
|
196
208
|
}
|
|
197
209
|
};
|
|
198
210
|
} else {
|
|
199
|
-
const hexColorResult = /^#([A-Fa-f0-9]{3,4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/.exec(
|
|
211
|
+
const hexColorResult = /^#([A-Fa-f0-9]{3,4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/.exec(variableDefinition);
|
|
200
212
|
if (hexColorResult) {
|
|
201
213
|
const [r, g, b, a] = hexToRgb(hexColorResult[1]);
|
|
202
214
|
addVariable(name, {
|
|
@@ -205,7 +217,7 @@ class MasterCSS {
|
|
|
205
217
|
space: 'rgb'
|
|
206
218
|
});
|
|
207
219
|
} else {
|
|
208
|
-
const rgbFunctionResult = /^rgb\( *([0-9]{1,3})(?: *, *| +)([0-9]{1,3})(?: *, *| +)([0-9]{1,3}) *(?:(?:,|\/) *(.*?) *)?\)$/.exec(
|
|
220
|
+
const rgbFunctionResult = /^rgb\( *([0-9]{1,3})(?: *, *| +)([0-9]{1,3})(?: *, *| +)([0-9]{1,3}) *(?:(?:,|\/) *(.*?) *)?\)$/.exec(variableDefinition);
|
|
209
221
|
if (rgbFunctionResult) {
|
|
210
222
|
addVariable(name, {
|
|
211
223
|
type: 'color',
|
|
@@ -213,7 +225,7 @@ class MasterCSS {
|
|
|
213
225
|
space: 'rgb'
|
|
214
226
|
});
|
|
215
227
|
} else {
|
|
216
|
-
const hslFunctionResult = /^hsl\((.*?)\)$/.exec(
|
|
228
|
+
const hslFunctionResult = /^hsl\((.*?)\)$/.exec(variableDefinition);
|
|
217
229
|
if (hslFunctionResult) {
|
|
218
230
|
addVariable(name, {
|
|
219
231
|
type: 'color',
|
|
@@ -223,7 +235,7 @@ class MasterCSS {
|
|
|
223
235
|
} else {
|
|
224
236
|
addVariable(name, {
|
|
225
237
|
type: 'string',
|
|
226
|
-
value:
|
|
238
|
+
value: variableDefinition
|
|
227
239
|
});
|
|
228
240
|
}
|
|
229
241
|
}
|
|
@@ -232,8 +244,11 @@ class MasterCSS {
|
|
|
232
244
|
}
|
|
233
245
|
};
|
|
234
246
|
for(const parnetKey in variables){
|
|
235
|
-
|
|
247
|
+
resolveVariable(variables[parnetKey], [
|
|
248
|
+
parnetKey
|
|
249
|
+
]);
|
|
236
250
|
}
|
|
251
|
+
// todo: address to the target variable
|
|
237
252
|
for (const name of Object.keys(unexecutedAliasVariable)){
|
|
238
253
|
for (const mode of Object.keys(unexecutedAliasVariable[name])){
|
|
239
254
|
unexecutedAliasVariable[name][mode]?.();
|
|
@@ -257,31 +272,31 @@ class MasterCSS {
|
|
|
257
272
|
}
|
|
258
273
|
}
|
|
259
274
|
const flattedStyles = styles ? flattenObject(styles) : {};
|
|
260
|
-
const
|
|
261
|
-
const
|
|
262
|
-
if (Object.prototype.hasOwnProperty.call(this.styles,
|
|
263
|
-
const currentClass = this.styles[
|
|
264
|
-
const className = flattedStyles[
|
|
275
|
+
const utilityNames = Object.keys(flattedStyles);
|
|
276
|
+
const handleUtilityName = (utilityName)=>{
|
|
277
|
+
if (Object.prototype.hasOwnProperty.call(this.styles, utilityName)) return;
|
|
278
|
+
const currentClass = this.styles[utilityName] = [];
|
|
279
|
+
const className = flattedStyles[utilityName];
|
|
265
280
|
if (!className) return;
|
|
266
281
|
const classNames = className.replace(/(?:\n(?:\s*))+/g, ' ').trim().split(' ');
|
|
267
282
|
for (const eachClassName of classNames){
|
|
268
283
|
const handle = (className)=>{
|
|
269
284
|
if (Object.prototype.hasOwnProperty.call(this.stylesBy, className)) {
|
|
270
285
|
const currentRelation = this.stylesBy[className];
|
|
271
|
-
if (!currentRelation.includes(
|
|
272
|
-
currentRelation.push(
|
|
286
|
+
if (!currentRelation.includes(utilityName)) {
|
|
287
|
+
currentRelation.push(utilityName);
|
|
273
288
|
}
|
|
274
289
|
} else {
|
|
275
290
|
this.stylesBy[className] = [
|
|
276
|
-
|
|
291
|
+
utilityName
|
|
277
292
|
];
|
|
278
293
|
}
|
|
279
294
|
if (!currentClass.includes(className)) {
|
|
280
295
|
currentClass.push(className);
|
|
281
296
|
}
|
|
282
297
|
};
|
|
283
|
-
if (
|
|
284
|
-
|
|
298
|
+
if (utilityNames.includes(eachClassName)) {
|
|
299
|
+
handleUtilityName(eachClassName);
|
|
285
300
|
for (const parentClassName of this.styles[eachClassName]){
|
|
286
301
|
handle(parentClassName);
|
|
287
302
|
}
|
|
@@ -290,49 +305,54 @@ class MasterCSS {
|
|
|
290
305
|
}
|
|
291
306
|
}
|
|
292
307
|
};
|
|
293
|
-
for (const
|
|
294
|
-
|
|
308
|
+
for (const eachUtilityName of utilityNames){
|
|
309
|
+
handleUtilityName(eachUtilityName);
|
|
295
310
|
}
|
|
296
|
-
if (
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
+
if (rules || utilities) {
|
|
312
|
+
const rulesEntries = [];
|
|
313
|
+
if (utilities) {
|
|
314
|
+
for(const utilityName in utilities){
|
|
315
|
+
const declarations = utilities[utilityName];
|
|
316
|
+
rulesEntries.push([
|
|
317
|
+
utilityName,
|
|
318
|
+
{
|
|
319
|
+
declarations,
|
|
320
|
+
layer: Layer.Utility
|
|
321
|
+
}
|
|
322
|
+
]);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
if (rules) {
|
|
326
|
+
rulesEntries.push(...Object.entries(rules));
|
|
327
|
+
}
|
|
328
|
+
const rulesEntriesLength = rulesEntries.length;
|
|
329
|
+
const colorNames = Object.keys(colorVariableNames);
|
|
330
|
+
rulesEntries.sort((a, b)=>{
|
|
311
331
|
if (a[1].layer !== b[1].layer) {
|
|
312
332
|
return (b[1].layer || 0) - (a[1].layer || 0);
|
|
313
333
|
}
|
|
314
334
|
return b[0].localeCompare(a[0]);
|
|
315
|
-
})
|
|
316
|
-
|
|
317
|
-
const colorNames = Object.keys(colorVariableNames);
|
|
318
|
-
rulesEntries.forEach(([id, eachRuleDefinition], index)=>{
|
|
319
|
-
const eachRegisteredRule = {
|
|
335
|
+
}).forEach(([id, eachRuleDefinition], index)=>{
|
|
336
|
+
const EachRule = {
|
|
320
337
|
id,
|
|
338
|
+
keys: [],
|
|
321
339
|
variables: {},
|
|
322
|
-
|
|
340
|
+
matchers: {},
|
|
341
|
+
order: rulesEntriesLength - 1 - index,
|
|
323
342
|
definition: eachRuleDefinition
|
|
324
343
|
};
|
|
325
|
-
this.
|
|
326
|
-
const
|
|
344
|
+
this.Rules.push(EachRule);
|
|
345
|
+
const { matcher, layer, subkey, ambiguousKeys, ambiguousValues, sign } = eachRuleDefinition;
|
|
346
|
+
if (layer === Layer.Utility) {
|
|
347
|
+
EachRule.id = '.' + id;
|
|
348
|
+
EachRule.matchers.arbitrary = new RegExp('^' + escapeString(id) + '(?=!|\\*|>|\\+|~|:|\\[|@|_|\\.|$)', 'm');
|
|
349
|
+
}
|
|
327
350
|
// todo: 不可使用 startsWith 判斷,應改為更精準的從 config.variables 取得目標變數群組,但 config.variables 中的值還沒被 resolve 像是 Array
|
|
328
|
-
const addResolvedVariables = (
|
|
351
|
+
const addResolvedVariables = (groupName)=>{
|
|
329
352
|
for(const eachVariableName in this.variables){
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
...this.variables[eachVariableName],
|
|
334
|
-
name: eachVariableName
|
|
335
|
-
};
|
|
353
|
+
const eachVariable = this.variables[eachVariableName];
|
|
354
|
+
if (eachVariable.group === groupName) {
|
|
355
|
+
EachRule.variables[eachVariable.key] = eachVariable;
|
|
336
356
|
}
|
|
337
357
|
}
|
|
338
358
|
};
|
|
@@ -344,26 +364,49 @@ class MasterCSS {
|
|
|
344
364
|
}
|
|
345
365
|
// 2. custom `config.variables`
|
|
346
366
|
addResolvedVariables(id);
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
367
|
+
const keys = [];
|
|
368
|
+
let { key } = eachRuleDefinition;
|
|
369
|
+
if (layer === Layer.NativeShorthand || layer === Layer.Native) {
|
|
370
|
+
if (!key) eachRuleDefinition.key = key = id;
|
|
371
|
+
keys.push(id);
|
|
372
|
+
}
|
|
373
|
+
if (sign) {
|
|
374
|
+
EachRule.matchers.arbitrary = new RegExp(`^${sign}[^!*>+~:[@_]+\\|`);
|
|
375
|
+
} else if (!matcher) {
|
|
376
|
+
const colorsPatten = colorNames.join('|');
|
|
377
|
+
if (!key && !subkey) {
|
|
378
|
+
keys.push(id);
|
|
379
|
+
} else {
|
|
380
|
+
if (key && !keys.includes(key)) keys.push(key);
|
|
381
|
+
if (subkey) keys.push(subkey);
|
|
382
|
+
if (layer === Layer.Shorthand) {
|
|
383
|
+
keys.push(id);
|
|
356
384
|
}
|
|
357
|
-
|
|
358
|
-
|
|
385
|
+
}
|
|
386
|
+
if (ambiguousKeys?.length) {
|
|
387
|
+
const ambiguousKeyPattern = ambiguousKeys.length > 1 ? `(?:${ambiguousKeys.join('|')})` : ambiguousKeys[0];
|
|
388
|
+
const variableKeys = Object.keys(EachRule.variables);
|
|
389
|
+
if (ambiguousValues?.length) {
|
|
390
|
+
const ambiguousValuePatterns = [];
|
|
391
|
+
for (const eachAmbiguousValue of ambiguousValues){
|
|
392
|
+
if (eachAmbiguousValue instanceof RegExp) {
|
|
393
|
+
ambiguousValuePatterns.push(eachAmbiguousValue.source.replace('\\$colors', colorsPatten));
|
|
394
|
+
} else {
|
|
395
|
+
ambiguousValuePatterns.unshift(`${eachAmbiguousValue}(?:\\b|_)`);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
EachRule.matchers.value = new RegExp(`^${ambiguousKeyPattern}:(?:${ambiguousValuePatterns.join('|')})[^|]*?(?:@|$)`);
|
|
359
399
|
}
|
|
360
|
-
if (
|
|
361
|
-
|
|
400
|
+
if (variableKeys.length) {
|
|
401
|
+
EachRule.matchers.variable = new RegExp(`^${ambiguousKeyPattern}:(?:${variableKeys.join('|')}(?![a-zA-Z0-9-]))[^|]*?(?:@|$)`);
|
|
362
402
|
}
|
|
363
|
-
if (valueMatches.length) eachRegisteredRule.match = new RegExp(`^${key}:(?:${valueMatches.join('|')})[^|]*?(?:@|$)`);
|
|
364
|
-
} else {
|
|
365
|
-
eachRegisteredRule.match = match;
|
|
366
403
|
}
|
|
404
|
+
} else {
|
|
405
|
+
EachRule.matchers.arbitrary = matcher;
|
|
406
|
+
}
|
|
407
|
+
if (keys.length) {
|
|
408
|
+
EachRule.keys = keys;
|
|
409
|
+
EachRule.matchers.key = new RegExp(`^${keys.length > 1 ? `(${keys.join('|')})` : keys[0]}:`);
|
|
367
410
|
}
|
|
368
411
|
});
|
|
369
412
|
}
|
|
@@ -373,17 +416,29 @@ class MasterCSS {
|
|
|
373
416
|
* @param className
|
|
374
417
|
* @returns css text
|
|
375
418
|
*/ match(className) {
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
419
|
+
/**
|
|
420
|
+
* 1. variable
|
|
421
|
+
* @example fg:primary bg:blue
|
|
422
|
+
*/ for (const EachRule of this.Rules){
|
|
423
|
+
if (EachRule.matchers.variable?.test(className)) return EachRule;
|
|
381
424
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
425
|
+
/**
|
|
426
|
+
* 2. value (ambiguous.key * ambiguous.values)
|
|
427
|
+
* @example bg:current box:content font:12
|
|
428
|
+
*/ for (const EachRule of this.Rules){
|
|
429
|
+
if (EachRule.matchers.value?.test(className)) return EachRule;
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* 3. full key
|
|
433
|
+
* @example text-align:center color:blue-40
|
|
434
|
+
*/ for (const EachRule of this.Rules){
|
|
435
|
+
if (EachRule.matchers.key?.test(className)) return EachRule;
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* 4. arbitrary
|
|
439
|
+
* @example custom RegExp, utility
|
|
440
|
+
*/ for (const EachRule of this.Rules){
|
|
441
|
+
if (EachRule.matchers.arbitrary?.test(className)) return EachRule;
|
|
387
442
|
}
|
|
388
443
|
}
|
|
389
444
|
/**
|
|
@@ -401,14 +456,19 @@ class MasterCSS {
|
|
|
401
456
|
* @returns Rule
|
|
402
457
|
*/ create(syntax) {
|
|
403
458
|
if (Object.prototype.hasOwnProperty.call(this.ruleBy, syntax)) return this.ruleBy[syntax];
|
|
404
|
-
const
|
|
405
|
-
if (
|
|
406
|
-
return new Rule(syntax,
|
|
459
|
+
const RegisteredRule = this.match(syntax);
|
|
460
|
+
if (RegisteredRule) {
|
|
461
|
+
return new Rule(syntax, RegisteredRule, this);
|
|
407
462
|
}
|
|
408
463
|
}
|
|
409
464
|
/**
|
|
410
465
|
* 根據蒐集到的所有 DOM class 重新 create
|
|
411
|
-
*/ refresh(customConfig
|
|
466
|
+
*/ refresh(customConfig) {
|
|
467
|
+
if (customConfig) {
|
|
468
|
+
this.customConfig = customConfig;
|
|
469
|
+
} else {
|
|
470
|
+
customConfig = this.customConfig;
|
|
471
|
+
}
|
|
412
472
|
if (!customConfig?.override) {
|
|
413
473
|
// @ts-ignore
|
|
414
474
|
this.config = extendConfig(config, customConfig);
|
|
@@ -526,7 +586,7 @@ class MasterCSS {
|
|
|
526
586
|
if (!keyframe.usage) keyframe.usage = 0;
|
|
527
587
|
if (!--keyframe.usage && keyframe.native) {
|
|
528
588
|
const nativeIndex = keyframeRule.natives.indexOf(keyframe.native);
|
|
529
|
-
|
|
589
|
+
sheet?.deleteRule(Object.keys(this.variablesNativeRules).length + nativeIndex);
|
|
530
590
|
keyframeRule.natives.splice(nativeIndex, 1);
|
|
531
591
|
keyframe.native = undefined;
|
|
532
592
|
}
|
|
@@ -752,7 +812,6 @@ class MasterCSS {
|
|
|
752
812
|
}
|
|
753
813
|
this.rules.splice(index, 0, rule);
|
|
754
814
|
this.ruleBy[className] = rule;
|
|
755
|
-
// 只在瀏覽器端運行
|
|
756
815
|
if (this.style) {
|
|
757
816
|
const sheet = this.style.sheet;
|
|
758
817
|
let cssRuleIndex = 0;
|
|
@@ -788,6 +847,7 @@ class MasterCSS {
|
|
|
788
847
|
// animations
|
|
789
848
|
this.handleRuleWithAnimationNames(rule);
|
|
790
849
|
rule.definition.insert?.call(rule);
|
|
850
|
+
return index;
|
|
791
851
|
}
|
|
792
852
|
get text() {
|
|
793
853
|
return this.rules.map((eachRule)=>eachRule.text).join('');
|
|
@@ -851,14 +911,14 @@ class MasterCSS {
|
|
|
851
911
|
if (variable.usage) {
|
|
852
912
|
variable.usage++;
|
|
853
913
|
} else {
|
|
854
|
-
const addProperty = (mode,
|
|
914
|
+
const addProperty = (mode, variable)=>{
|
|
855
915
|
let nativeRule = this.variablesNativeRules[mode];
|
|
856
916
|
if (!nativeRule) {
|
|
857
917
|
let cssRule;
|
|
858
918
|
let mediaConditionText;
|
|
859
919
|
let selectorText;
|
|
860
920
|
if (mode) {
|
|
861
|
-
switch(this.config.
|
|
921
|
+
switch(this.config.modes?.[mode]){
|
|
862
922
|
case 'media':
|
|
863
923
|
mediaConditionText = `@media(prefers-color-scheme:${mode})`;
|
|
864
924
|
selectorText = ':root';
|
|
@@ -921,7 +981,7 @@ class MasterCSS {
|
|
|
921
981
|
}
|
|
922
982
|
const propertyName = '--' + eachVariableName;
|
|
923
983
|
if (!initializing || !nativeRule.cssRule.style.getPropertyValue(propertyName)) {
|
|
924
|
-
nativeRule.cssRule.style.setProperty(propertyName, String(
|
|
984
|
+
nativeRule.cssRule.style.setProperty(propertyName, String(variable.value));
|
|
925
985
|
}
|
|
926
986
|
};
|
|
927
987
|
if (variable.value) {
|