@master/css 2.0.0-rc.3 → 2.0.0-rc.31
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 +5 -0
- package/dist/_virtual/_commonjsHelpers.mjs +3 -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 +145 -0
- package/dist/common.d.ts +25 -0
- package/dist/common.mjs +124 -0
- package/dist/config/animations.cjs +113 -0
- package/dist/config/animations.mjs +111 -0
- package/dist/config/functions.cjs +351 -0
- package/dist/config/functions.mjs +349 -0
- package/dist/config/index.cjs +36 -0
- package/dist/config/index.d.ts +15 -12
- package/dist/config/index.mjs +26 -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/queries.cjs +17 -0
- package/dist/config/queries.d.ts +22 -0
- package/dist/config/queries.mjs +15 -0
- package/dist/config/rules.cjs +2303 -0
- package/dist/config/rules.d.ts +8 -5
- package/dist/config/rules.mjs +2298 -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.mjs +1 -0
- package/dist/config/utilities.cjs +191 -0
- package/dist/config/{semantics.d.ts → utilities.d.ts} +2 -5
- package/dist/config/utilities.mjs +189 -0
- package/dist/config/variables.cjs +116 -0
- package/dist/config/variables.d.ts +132 -177
- package/dist/config/variables.mjs +114 -0
- package/dist/core.cjs +1046 -0
- package/dist/core.d.ts +35 -31
- package/dist/core.mjs +1041 -0
- package/dist/index.cjs +72 -0
- package/dist/index.d.ts +15 -8
- package/dist/index.mjs +22 -0
- package/dist/layer.cjs +28 -0
- package/dist/layer.d.ts +26 -9
- package/dist/layer.mjs +26 -0
- package/dist/polyfills/css-escape.cjs +8 -0
- package/dist/polyfills/css-escape.mjs +2 -0
- package/dist/polyfills/css-escape2.cjs +68 -0
- package/dist/polyfills/css-escape2.mjs +64 -0
- package/dist/rule.cjs +722 -0
- package/dist/rule.d.ts +60 -22
- package/dist/rule.mjs +720 -0
- package/dist/shared/utils/css-escape.cjs +56 -0
- package/dist/shared/utils/css-escape.mjs +54 -0
- package/dist/tokens/screen-sizes.cjs +17 -0
- package/dist/tokens/screen-sizes.mjs +15 -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/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-queries-equal.cjs +22 -0
- package/dist/utils/are-rule-queries-equal.d.ts +2 -0
- package/dist/utils/are-rule-queries-equal.mjs +20 -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/extend-config.cjs +72 -0
- package/dist/utils/extend-config.d.ts +1 -1
- package/dist/utils/extend-config.mjs +70 -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/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,1041 @@
|
|
|
1
|
+
import { Rule } from './rule.mjs';
|
|
2
|
+
import { config } from './config/index.mjs';
|
|
3
|
+
import Layer from './layer.mjs';
|
|
4
|
+
import hexToRgb from './utils/hex-to-rgb.mjs';
|
|
5
|
+
import { flattenObject } from './utils/flatten-object.mjs';
|
|
6
|
+
import extendConfig from './utils/extend-config.mjs';
|
|
7
|
+
|
|
8
|
+
class MasterCSS {
|
|
9
|
+
customConfig;
|
|
10
|
+
static config = config;
|
|
11
|
+
rules;
|
|
12
|
+
ruleBy;
|
|
13
|
+
classesUsage;
|
|
14
|
+
config;
|
|
15
|
+
Rules;
|
|
16
|
+
constructor(customConfig = config){
|
|
17
|
+
this.customConfig = customConfig;
|
|
18
|
+
this.rules = [];
|
|
19
|
+
this.ruleBy = {};
|
|
20
|
+
this.classesUsage = {};
|
|
21
|
+
this.Rules = [];
|
|
22
|
+
if (!customConfig?.override) {
|
|
23
|
+
this.config = extendConfig(config, customConfig);
|
|
24
|
+
} else {
|
|
25
|
+
this.config = extendConfig(customConfig);
|
|
26
|
+
}
|
|
27
|
+
this.resolve();
|
|
28
|
+
if (this.constructor === MasterCSS) {
|
|
29
|
+
masterCSSs.push(this);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
resolve() {
|
|
33
|
+
this.styles = {};
|
|
34
|
+
this.stylesBy = {};
|
|
35
|
+
this.selectors = {};
|
|
36
|
+
this.variables = {};
|
|
37
|
+
this.queries = {};
|
|
38
|
+
this.animations = {};
|
|
39
|
+
this.Rules.length = 0;
|
|
40
|
+
this.variablesNativeRules = {};
|
|
41
|
+
this.hasKeyframesRule = false;
|
|
42
|
+
const colorVariableNames = {
|
|
43
|
+
current: undefined,
|
|
44
|
+
currentColor: undefined,
|
|
45
|
+
transparent: undefined
|
|
46
|
+
};
|
|
47
|
+
const { styles, selectors, variables, utilities, queries, rules, animations } = this.config;
|
|
48
|
+
function escapeString(str) {
|
|
49
|
+
return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
50
|
+
}
|
|
51
|
+
if (selectors) {
|
|
52
|
+
const resolvedSelectors = flattenObject(selectors);
|
|
53
|
+
for(const eachSelectorName in resolvedSelectors){
|
|
54
|
+
const eachResolvedSelectorText = resolvedSelectors[eachSelectorName];
|
|
55
|
+
const regexp = new RegExp(escapeString(eachSelectorName) + '(?![a-z-])');
|
|
56
|
+
for (const eachNewSelectorText of Array.isArray(eachResolvedSelectorText) ? eachResolvedSelectorText : [
|
|
57
|
+
eachResolvedSelectorText
|
|
58
|
+
]){
|
|
59
|
+
const vendor = eachNewSelectorText.match(/^::-[a-z]+-/m)?.[0] ?? '';
|
|
60
|
+
let selectorValues = this.selectors[vendor];
|
|
61
|
+
if (!selectorValues) {
|
|
62
|
+
selectorValues = this.selectors[vendor] = [];
|
|
63
|
+
}
|
|
64
|
+
let currentSelectValue = selectorValues.find(([_valueRegexp])=>_valueRegexp === regexp);
|
|
65
|
+
if (!currentSelectValue) {
|
|
66
|
+
currentSelectValue = [
|
|
67
|
+
regexp,
|
|
68
|
+
[]
|
|
69
|
+
];
|
|
70
|
+
selectorValues.push(currentSelectValue);
|
|
71
|
+
}
|
|
72
|
+
currentSelectValue[1].push(eachNewSelectorText);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (variables) {
|
|
77
|
+
const unexecutedAliasVariable = {};
|
|
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') {
|
|
89
|
+
if (alpha) {
|
|
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)
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
colorVariableNames[flatName] = undefined;
|
|
97
|
+
}
|
|
98
|
+
const currentMode = replacedMode ?? mode;
|
|
99
|
+
if (currentMode !== undefined) {
|
|
100
|
+
if (Object.prototype.hasOwnProperty.call(this.variables, flatName)) {
|
|
101
|
+
const foundVariable = this.variables[flatName];
|
|
102
|
+
if (currentMode) {
|
|
103
|
+
if (!foundVariable.modes) {
|
|
104
|
+
foundVariable.modes = {};
|
|
105
|
+
}
|
|
106
|
+
foundVariable.modes[currentMode] = variable;
|
|
107
|
+
} else {
|
|
108
|
+
foundVariable.value = variable.value;
|
|
109
|
+
if (variable.type === 'color') {
|
|
110
|
+
foundVariable.space = variable.space;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
} else {
|
|
114
|
+
if (currentMode) {
|
|
115
|
+
const newVariable = {
|
|
116
|
+
key: variable.key,
|
|
117
|
+
name: variable.name,
|
|
118
|
+
group: variable.group,
|
|
119
|
+
type: variable.type,
|
|
120
|
+
modes: {
|
|
121
|
+
[currentMode]: variable
|
|
122
|
+
}
|
|
123
|
+
};
|
|
124
|
+
if (variable.type === 'color') {
|
|
125
|
+
newVariable.space = variable.space;
|
|
126
|
+
}
|
|
127
|
+
this.variables[flatName] = newVariable;
|
|
128
|
+
} else {
|
|
129
|
+
this.variables[flatName] = variable;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
} else {
|
|
133
|
+
this.variables[flatName] = variable;
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
if (typeof variableDefinition === 'object') {
|
|
137
|
+
if (Array.isArray(variableDefinition)) {
|
|
138
|
+
addVariable(name, {
|
|
139
|
+
type: 'string',
|
|
140
|
+
value: variableDefinition.join(',')
|
|
141
|
+
});
|
|
142
|
+
} else {
|
|
143
|
+
const keys = Object.keys(variableDefinition);
|
|
144
|
+
for (const eachKey of keys){
|
|
145
|
+
if (eachKey === '' || eachKey.startsWith('@')) {
|
|
146
|
+
resolveVariable(variableDefinition[eachKey], name, eachKey || keys.some((eachKey)=>eachKey.startsWith('@')) ? eachKey.slice(1) : undefined);
|
|
147
|
+
} else {
|
|
148
|
+
resolveVariable(variableDefinition[eachKey], [
|
|
149
|
+
...name,
|
|
150
|
+
eachKey
|
|
151
|
+
]);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
} else if (typeof variableDefinition === 'number') {
|
|
156
|
+
addVariable(name, {
|
|
157
|
+
type: 'number',
|
|
158
|
+
value: variableDefinition
|
|
159
|
+
});
|
|
160
|
+
addVariable([
|
|
161
|
+
'',
|
|
162
|
+
...name
|
|
163
|
+
], {
|
|
164
|
+
type: 'number',
|
|
165
|
+
value: variableDefinition * -1
|
|
166
|
+
});
|
|
167
|
+
} else if (typeof variableDefinition === 'string') {
|
|
168
|
+
const aliasResult = /^\$\((.*?)\)(?: ?\/ ?(.+?))?$/.exec(variableDefinition);
|
|
169
|
+
const flatName = name.join('-');
|
|
170
|
+
if (aliasResult) {
|
|
171
|
+
if (!Object.prototype.hasOwnProperty.call(unexecutedAliasVariable, flatName)) {
|
|
172
|
+
unexecutedAliasVariable[flatName] = {};
|
|
173
|
+
}
|
|
174
|
+
unexecutedAliasVariable[flatName][mode] = ()=>{
|
|
175
|
+
delete unexecutedAliasVariable[flatName][mode];
|
|
176
|
+
const [alias, aliasMode] = aliasResult[1].split('@');
|
|
177
|
+
if (alias) {
|
|
178
|
+
if (Object.prototype.hasOwnProperty.call(unexecutedAliasVariable, alias)) {
|
|
179
|
+
for (const mode of Object.keys(unexecutedAliasVariable[alias])){
|
|
180
|
+
unexecutedAliasVariable[alias][mode]?.();
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
const aliasVariable = this.variables[alias];
|
|
184
|
+
if (aliasVariable) {
|
|
185
|
+
if (aliasMode === undefined && aliasVariable.modes) {
|
|
186
|
+
addVariable(name, {
|
|
187
|
+
type: aliasVariable.type,
|
|
188
|
+
value: aliasVariable.value,
|
|
189
|
+
space: aliasVariable.space
|
|
190
|
+
}, '', aliasResult[2]);
|
|
191
|
+
for(const mode in aliasVariable.modes){
|
|
192
|
+
addVariable(name, aliasVariable.modes[mode], mode, aliasResult[2]);
|
|
193
|
+
}
|
|
194
|
+
} else {
|
|
195
|
+
const variable = aliasMode !== undefined ? aliasVariable.modes?.[aliasMode] : aliasVariable;
|
|
196
|
+
if (variable) {
|
|
197
|
+
const newVariable = {
|
|
198
|
+
type: variable.type,
|
|
199
|
+
value: variable.value
|
|
200
|
+
};
|
|
201
|
+
if (variable.type === 'color') {
|
|
202
|
+
newVariable.space = variable.space;
|
|
203
|
+
}
|
|
204
|
+
addVariable(name, newVariable, undefined, aliasResult[2]);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
} else {
|
|
211
|
+
const hexColorResult = /^#([A-Fa-f0-9]{3,4}|[A-Fa-f0-9]{6}|[A-Fa-f0-9]{8})$/.exec(variableDefinition);
|
|
212
|
+
if (hexColorResult) {
|
|
213
|
+
const [r, g, b, a] = hexToRgb(hexColorResult[1]);
|
|
214
|
+
addVariable(name, {
|
|
215
|
+
type: 'color',
|
|
216
|
+
value: `${r} ${g} ${b}${a === 1 ? '' : ' / ' + a}`,
|
|
217
|
+
space: 'rgb'
|
|
218
|
+
});
|
|
219
|
+
} else {
|
|
220
|
+
const rgbFunctionResult = /^rgb\( *([0-9]{1,3})(?: *, *| +)([0-9]{1,3})(?: *, *| +)([0-9]{1,3}) *(?:(?:,|\/) *(.*?) *)?\)$/.exec(variableDefinition);
|
|
221
|
+
if (rgbFunctionResult) {
|
|
222
|
+
addVariable(name, {
|
|
223
|
+
type: 'color',
|
|
224
|
+
value: rgbFunctionResult[1] + ' ' + rgbFunctionResult[2] + ' ' + rgbFunctionResult[3] + (rgbFunctionResult[4] ? ' / ' + (rgbFunctionResult[4].startsWith('0.') ? rgbFunctionResult[4].slice(1) : rgbFunctionResult[4]) : ''),
|
|
225
|
+
space: 'rgb'
|
|
226
|
+
});
|
|
227
|
+
} else {
|
|
228
|
+
const hslFunctionResult = /^hsl\((.*?)\)$/.exec(variableDefinition);
|
|
229
|
+
if (hslFunctionResult) {
|
|
230
|
+
addVariable(name, {
|
|
231
|
+
type: 'color',
|
|
232
|
+
value: hslFunctionResult[1],
|
|
233
|
+
space: 'hsl'
|
|
234
|
+
});
|
|
235
|
+
} else {
|
|
236
|
+
addVariable(name, {
|
|
237
|
+
type: 'string',
|
|
238
|
+
value: variableDefinition
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
for(const parnetKey in variables){
|
|
247
|
+
resolveVariable(variables[parnetKey], [
|
|
248
|
+
parnetKey
|
|
249
|
+
]);
|
|
250
|
+
}
|
|
251
|
+
// todo: address to the target variable
|
|
252
|
+
for (const name of Object.keys(unexecutedAliasVariable)){
|
|
253
|
+
for (const mode of Object.keys(unexecutedAliasVariable[name])){
|
|
254
|
+
unexecutedAliasVariable[name][mode]?.();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
if (queries) {
|
|
259
|
+
this.queries = flattenObject(queries);
|
|
260
|
+
}
|
|
261
|
+
if (animations) {
|
|
262
|
+
for(const animationName in animations){
|
|
263
|
+
const eachAnimation = this.animations[animationName] = {};
|
|
264
|
+
const eachKeyframes = animations[animationName];
|
|
265
|
+
for(const eachKeyframeValue in eachKeyframes){
|
|
266
|
+
const newValueByPropertyName = eachAnimation[eachKeyframeValue] = {};
|
|
267
|
+
const eachKeyframeDeclarations = eachKeyframes[eachKeyframeValue];
|
|
268
|
+
for(const propertyName in eachKeyframeDeclarations){
|
|
269
|
+
newValueByPropertyName[propertyName] = eachKeyframeDeclarations[propertyName];
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
const flattedStyles = styles ? flattenObject(styles) : {};
|
|
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];
|
|
280
|
+
if (!className) return;
|
|
281
|
+
const classNames = className.replace(/(?:\n(?:\s*))+/g, ' ').trim().split(' ');
|
|
282
|
+
for (const eachClassName of classNames){
|
|
283
|
+
const handle = (className)=>{
|
|
284
|
+
if (Object.prototype.hasOwnProperty.call(this.stylesBy, className)) {
|
|
285
|
+
const currentRelation = this.stylesBy[className];
|
|
286
|
+
if (!currentRelation.includes(utilityName)) {
|
|
287
|
+
currentRelation.push(utilityName);
|
|
288
|
+
}
|
|
289
|
+
} else {
|
|
290
|
+
this.stylesBy[className] = [
|
|
291
|
+
utilityName
|
|
292
|
+
];
|
|
293
|
+
}
|
|
294
|
+
if (!currentClass.includes(className)) {
|
|
295
|
+
currentClass.push(className);
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
if (utilityNames.includes(eachClassName)) {
|
|
299
|
+
handleUtilityName(eachClassName);
|
|
300
|
+
for (const parentClassName of this.styles[eachClassName]){
|
|
301
|
+
handle(parentClassName);
|
|
302
|
+
}
|
|
303
|
+
} else {
|
|
304
|
+
handle(eachClassName);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
};
|
|
308
|
+
for (const eachUtilityName of utilityNames){
|
|
309
|
+
handleUtilityName(eachUtilityName);
|
|
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)=>{
|
|
331
|
+
if (a[1].layer !== b[1].layer) {
|
|
332
|
+
return (b[1].layer || 0) - (a[1].layer || 0);
|
|
333
|
+
}
|
|
334
|
+
return b[0].localeCompare(a[0]);
|
|
335
|
+
}).forEach(([id, eachRuleDefinition], index)=>{
|
|
336
|
+
const EachRule = {
|
|
337
|
+
id,
|
|
338
|
+
keys: [],
|
|
339
|
+
variables: {},
|
|
340
|
+
matchers: {},
|
|
341
|
+
order: rulesEntriesLength - 1 - index,
|
|
342
|
+
definition: eachRuleDefinition
|
|
343
|
+
};
|
|
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
|
+
}
|
|
350
|
+
// todo: 不可使用 startsWith 判斷,應改為更精準的從 config.variables 取得目標變數群組,但 config.variables 中的值還沒被 resolve 像是 Array
|
|
351
|
+
const addResolvedVariables = (groupName)=>{
|
|
352
|
+
for(const eachVariableName in this.variables){
|
|
353
|
+
const eachVariable = this.variables[eachVariableName];
|
|
354
|
+
if (eachVariable.group === groupName) {
|
|
355
|
+
EachRule.variables[eachVariable.key] = eachVariable;
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
// 1. custom `config.rules[id].variables`
|
|
360
|
+
if (eachRuleDefinition.variables) {
|
|
361
|
+
for (const eachVariableGroup of eachRuleDefinition.variables){
|
|
362
|
+
addResolvedVariables(eachVariableGroup);
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
// 2. custom `config.variables`
|
|
366
|
+
addResolvedVariables(id);
|
|
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);
|
|
384
|
+
}
|
|
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('|')})[^|]*?(?:@|$)`);
|
|
399
|
+
}
|
|
400
|
+
if (variableKeys.length) {
|
|
401
|
+
EachRule.matchers.variable = new RegExp(`^${ambiguousKeyPattern}:(?:${variableKeys.join('|')}(?![a-zA-Z0-9-]))[^|]*?(?:@|$)`);
|
|
402
|
+
}
|
|
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]}:`);
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Match check if Master CSS syntax
|
|
416
|
+
* @param className
|
|
417
|
+
* @returns css text
|
|
418
|
+
*/ match(className) {
|
|
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;
|
|
424
|
+
}
|
|
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;
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
/**
|
|
445
|
+
* Generate rules from class name
|
|
446
|
+
* @param className
|
|
447
|
+
* @returns Rule[]
|
|
448
|
+
*/ generate(className) {
|
|
449
|
+
return (Object.prototype.hasOwnProperty.call(this.styles, className) ? this.styles[className].map((eachSyntax)=>this.create(eachSyntax)) : [
|
|
450
|
+
this.create(className)
|
|
451
|
+
]).filter((eachRule)=>eachRule && eachRule.text);
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Create rule from given syntax
|
|
455
|
+
* @param syntax
|
|
456
|
+
* @returns Rule
|
|
457
|
+
*/ create(syntax) {
|
|
458
|
+
if (Object.prototype.hasOwnProperty.call(this.ruleBy, syntax)) return this.ruleBy[syntax];
|
|
459
|
+
const RegisteredRule = this.match(syntax);
|
|
460
|
+
if (RegisteredRule) {
|
|
461
|
+
return new Rule(syntax, RegisteredRule, this);
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
/**
|
|
465
|
+
* 根據蒐集到的所有 DOM class 重新 create
|
|
466
|
+
*/ refresh(customConfig) {
|
|
467
|
+
if (customConfig) {
|
|
468
|
+
this.customConfig = customConfig;
|
|
469
|
+
} else {
|
|
470
|
+
customConfig = this.customConfig;
|
|
471
|
+
}
|
|
472
|
+
if (!customConfig?.override) {
|
|
473
|
+
// @ts-ignore
|
|
474
|
+
this.config = extendConfig(config, customConfig);
|
|
475
|
+
} else {
|
|
476
|
+
// @ts-ignore
|
|
477
|
+
this.config = extendConfig(customConfig);
|
|
478
|
+
}
|
|
479
|
+
this.resolve();
|
|
480
|
+
this.rules.length = 0;
|
|
481
|
+
// @ts-ignore
|
|
482
|
+
this.ruleBy = {};
|
|
483
|
+
/**
|
|
484
|
+
* 拿當前所有的 classNames 按照最新的 colors, config.rules 匹配並生成新的 style
|
|
485
|
+
* 所以 refresh 過後 rules 可能會變多也可能會變少
|
|
486
|
+
*/ for(const name in this.classesUsage){
|
|
487
|
+
this.add(name);
|
|
488
|
+
}
|
|
489
|
+
return this;
|
|
490
|
+
}
|
|
491
|
+
reset() {
|
|
492
|
+
// @ts-ignore
|
|
493
|
+
this.ruleBy = {};
|
|
494
|
+
// @ts-ignore
|
|
495
|
+
this.classesUsage = {};
|
|
496
|
+
this.rules.length = 0;
|
|
497
|
+
this.hasKeyframesRule = false;
|
|
498
|
+
this.variablesNativeRules = {};
|
|
499
|
+
for(const keyframeName in this.animations){
|
|
500
|
+
const animation = this.animations[keyframeName];
|
|
501
|
+
animation.usage = 0;
|
|
502
|
+
animation.native = undefined;
|
|
503
|
+
}
|
|
504
|
+
for(const variableName in this.variables){
|
|
505
|
+
const variable = this.variables[variableName];
|
|
506
|
+
variable.usage = 0;
|
|
507
|
+
}
|
|
508
|
+
return this;
|
|
509
|
+
}
|
|
510
|
+
destroy() {
|
|
511
|
+
this.reset();
|
|
512
|
+
masterCSSs.splice(masterCSSs.indexOf(this), 1);
|
|
513
|
+
return this;
|
|
514
|
+
}
|
|
515
|
+
add(...classNames) {
|
|
516
|
+
for (const className of classNames){
|
|
517
|
+
const rules = this.generate(className);
|
|
518
|
+
if (rules.length) {
|
|
519
|
+
for (const rule of rules){
|
|
520
|
+
this.insert(rule);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
return this;
|
|
525
|
+
}
|
|
526
|
+
delete(...classNames) {
|
|
527
|
+
/**
|
|
528
|
+
* class name 從 DOM tree 中被移除,
|
|
529
|
+
* 匹配並刪除對應的 rule
|
|
530
|
+
*/ const sheet = this.style?.sheet;
|
|
531
|
+
const deleteRule = (name)=>{
|
|
532
|
+
const rule = this.ruleBy[name];
|
|
533
|
+
if (!rule || Object.prototype.hasOwnProperty.call(this.stylesBy, name) && this.stylesBy[name].some((eachClassName)=>Object.prototype.hasOwnProperty.call(this.classesUsage, eachClassName))) return;
|
|
534
|
+
if (sheet && rule.natives.length) {
|
|
535
|
+
const firstNative = rule.natives[0];
|
|
536
|
+
for(let index = 0; index < sheet.cssRules.length; index++){
|
|
537
|
+
const eachCSSRule = sheet.cssRules[index];
|
|
538
|
+
if (eachCSSRule === firstNative.cssRule) {
|
|
539
|
+
for(let i = 0; i < rule.natives.length; i++){
|
|
540
|
+
sheet.deleteRule(index);
|
|
541
|
+
}
|
|
542
|
+
break;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
this.rules.splice(this.rules.indexOf(rule), 1);
|
|
547
|
+
delete this.ruleBy[name];
|
|
548
|
+
// variables
|
|
549
|
+
if (rule.variableNames) {
|
|
550
|
+
for (const eachVariableName of rule.variableNames){
|
|
551
|
+
const variable = this.variables[eachVariableName];
|
|
552
|
+
if (!variable.usage) variable.usage = 0;
|
|
553
|
+
if (!--variable.usage) {
|
|
554
|
+
const removeProperty = (mode)=>{
|
|
555
|
+
const nativeRule = this.variablesNativeRules[mode];
|
|
556
|
+
nativeRule.cssRule.style.removeProperty('--' + eachVariableName);
|
|
557
|
+
if (!nativeRule.cssRule.style.length) {
|
|
558
|
+
const variablesRule = this.rules[0];
|
|
559
|
+
const index = variablesRule.natives.indexOf(nativeRule);
|
|
560
|
+
sheet?.deleteRule(index);
|
|
561
|
+
variablesRule.natives.splice(index, 1);
|
|
562
|
+
delete this.variablesNativeRules[mode];
|
|
563
|
+
if (!variablesRule.natives.length) {
|
|
564
|
+
this.rules.splice(0, 1);
|
|
565
|
+
this.variablesNativeRules = {};
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
if (variable.value) {
|
|
570
|
+
removeProperty('');
|
|
571
|
+
}
|
|
572
|
+
if (variable.modes) {
|
|
573
|
+
for(const mode in variable.modes){
|
|
574
|
+
removeProperty(mode);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
// animations
|
|
581
|
+
if (rule.animationNames) {
|
|
582
|
+
const keyframeRulesIndex = Object.keys(this.variablesNativeRules).length ? 1 : 0;
|
|
583
|
+
const keyframeRule = this.rules[keyframeRulesIndex];
|
|
584
|
+
for (const eachKeyframeName of rule.animationNames){
|
|
585
|
+
const keyframe = this.animations[eachKeyframeName];
|
|
586
|
+
if (!keyframe.usage) keyframe.usage = 0;
|
|
587
|
+
if (!--keyframe.usage && keyframe.native) {
|
|
588
|
+
const nativeIndex = keyframeRule.natives.indexOf(keyframe.native);
|
|
589
|
+
sheet?.deleteRule(Object.keys(this.variablesNativeRules).length + nativeIndex);
|
|
590
|
+
keyframeRule.natives.splice(nativeIndex, 1);
|
|
591
|
+
keyframe.native = undefined;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
if (!keyframeRule.natives.length) {
|
|
595
|
+
this.rules.splice(keyframeRulesIndex, 1);
|
|
596
|
+
this.hasKeyframesRule = false;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
rule.definition.delete?.call(rule, name);
|
|
600
|
+
};
|
|
601
|
+
for (const className of classNames){
|
|
602
|
+
if (Object.prototype.hasOwnProperty.call(this.styles, className)) {
|
|
603
|
+
for (const eachClassName of this.styles[className]){
|
|
604
|
+
if (!Object.prototype.hasOwnProperty.call(this.classesUsage, eachClassName)) {
|
|
605
|
+
deleteRule(eachClassName);
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
delete this.ruleBy[className];
|
|
609
|
+
} else {
|
|
610
|
+
deleteRule(className);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
/**
|
|
615
|
+
* 加工插入規則
|
|
616
|
+
* 1. where
|
|
617
|
+
* 2. normal
|
|
618
|
+
* 3. where selectors
|
|
619
|
+
* 4. normal selectors
|
|
620
|
+
* 5. media where
|
|
621
|
+
* 6. media normal
|
|
622
|
+
* 7. media where selectors
|
|
623
|
+
* 8. media selectors
|
|
624
|
+
* 9. media width where
|
|
625
|
+
* 10. media width
|
|
626
|
+
* 11. media width where selectors
|
|
627
|
+
* 12. media width selectors
|
|
628
|
+
*/ insert(rule) {
|
|
629
|
+
if (this.ruleBy[rule.className]) return;
|
|
630
|
+
let index;
|
|
631
|
+
/**
|
|
632
|
+
* 必須按斷點值遞增,並透過索引插入,
|
|
633
|
+
* 以實現響應式先後套用的規則
|
|
634
|
+
* @example <1 <2 <3 ALL >=1 >=2 >=3
|
|
635
|
+
* @description
|
|
636
|
+
*/ const endIndex = this.rules.length - 1;
|
|
637
|
+
const { at, atToken, order, priority, hasWhere, className } = rule;
|
|
638
|
+
const findIndex = (startIndex, stopCheck, matchCheck)=>{
|
|
639
|
+
let i = startIndex;
|
|
640
|
+
for(; i <= endIndex; i++){
|
|
641
|
+
const eachRule = this.rules[i];
|
|
642
|
+
if (stopCheck?.(eachRule)) return matchCheck ? -1 : i - 1;
|
|
643
|
+
if (matchCheck?.(eachRule)) return i;
|
|
644
|
+
}
|
|
645
|
+
return matchCheck ? -1 : i - 1;
|
|
646
|
+
};
|
|
647
|
+
let matchStartIndex;
|
|
648
|
+
let matchEndIndex;
|
|
649
|
+
if (atToken) {
|
|
650
|
+
const mediaStartIndex = this.rules.findIndex((eachRule)=>eachRule.at?.media);
|
|
651
|
+
if (mediaStartIndex === -1) {
|
|
652
|
+
index = endIndex + 1;
|
|
653
|
+
} else {
|
|
654
|
+
const maxWidthFeature = at.media?.find(({ name })=>name === 'max-width');
|
|
655
|
+
const minWidthFeature = at.media?.find(({ name })=>name === 'min-width');
|
|
656
|
+
if (maxWidthFeature || minWidthFeature) {
|
|
657
|
+
const mediaWidthStartIndex = this.rules.findIndex((eachRule)=>eachRule.at?.media?.find(({ name })=>name === 'max-width' || name === 'min-width'));
|
|
658
|
+
if (mediaWidthStartIndex === -1) {
|
|
659
|
+
index = endIndex + 1;
|
|
660
|
+
} else {
|
|
661
|
+
if (maxWidthFeature && minWidthFeature) {
|
|
662
|
+
/**
|
|
663
|
+
* 範圍越小 ( 越限定 越侷限 ) 越優先,
|
|
664
|
+
* 按照範圍 max-width - min-width 遞減排序
|
|
665
|
+
* find 第一個所遇到同樣 feature 且範圍值比自己大的 rule,
|
|
666
|
+
* 並插入在該 rule 之後,讓自己優先被套用
|
|
667
|
+
*/ if (priority === -1) {
|
|
668
|
+
matchStartIndex = findIndex(mediaWidthStartIndex, (eachRule)=>eachRule.priority !== -1, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'max-width') && eachRule.at.media?.find(({ name })=>name === 'min-width'));
|
|
669
|
+
matchEndIndex = findIndex(mediaWidthStartIndex, (eachRule)=>eachRule.priority !== -1);
|
|
670
|
+
} else {
|
|
671
|
+
matchStartIndex = findIndex(mediaWidthStartIndex, undefined, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'max-width') && eachRule.at.media?.find(({ name })=>name === 'min-width') && eachRule.priority !== -1);
|
|
672
|
+
matchEndIndex = endIndex;
|
|
673
|
+
}
|
|
674
|
+
if (matchStartIndex !== -1) {
|
|
675
|
+
const range = maxWidthFeature.value - minWidthFeature.value;
|
|
676
|
+
let i = matchEndIndex;
|
|
677
|
+
const endI = matchStartIndex;
|
|
678
|
+
matchStartIndex = undefined;
|
|
679
|
+
for(; i >= endI; i--){
|
|
680
|
+
const eachRule = this.rules[i];
|
|
681
|
+
const eachMaxWidthFeature = eachRule.at.media?.find(({ name })=>name === 'max-width');
|
|
682
|
+
const eachMinWidthFeature = eachRule.at.media?.find(({ name })=>name === 'min-width');
|
|
683
|
+
const eachRange = eachMaxWidthFeature.value - eachMinWidthFeature.value;
|
|
684
|
+
if (eachRange < range) {
|
|
685
|
+
matchEndIndex = i - 1;
|
|
686
|
+
} else if (eachRange === range) {
|
|
687
|
+
matchStartIndex = i;
|
|
688
|
+
} else {
|
|
689
|
+
break;
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
if (matchStartIndex !== -1 && matchStartIndex !== undefined) {
|
|
694
|
+
const range = maxWidthFeature.value - minWidthFeature.value;
|
|
695
|
+
for(let i = matchEndIndex; i >= matchStartIndex; i--){
|
|
696
|
+
const eachRule = this.rules[i];
|
|
697
|
+
const eachMaxWidthFeature = eachRule.at.media?.find(({ name })=>name === 'max-width');
|
|
698
|
+
const eachMinWidthFeature = eachRule.at.media?.find(({ name })=>name === 'min-width');
|
|
699
|
+
const eachRange = eachMaxWidthFeature.value - eachMinWidthFeature.value;
|
|
700
|
+
if (eachRange < range) {
|
|
701
|
+
matchEndIndex = i - 1;
|
|
702
|
+
} else if (eachRange > range) {
|
|
703
|
+
matchStartIndex = i + 1;
|
|
704
|
+
break;
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
} else if (minWidthFeature) {
|
|
709
|
+
/**
|
|
710
|
+
* find 第一個所遇到同樣 feature 且值比自己大的 rule,
|
|
711
|
+
* 並插入在該 rule 之後,讓自己優先被套用
|
|
712
|
+
*/ if (priority === -1) {
|
|
713
|
+
matchStartIndex = findIndex(mediaWidthStartIndex, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'max-width') && eachRule.at.media?.find(({ name })=>name === 'min-width') || eachRule.priority !== -1, (eachRule)=>!eachRule.at.media?.find(({ name })=>name === 'max-width') && eachRule.at.media?.find(({ name })=>name === 'min-width'));
|
|
714
|
+
matchEndIndex = findIndex(mediaWidthStartIndex, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'max-width') && eachRule.at.media?.find(({ name })=>name === 'min-width') || eachRule.priority !== -1);
|
|
715
|
+
} else {
|
|
716
|
+
matchStartIndex = findIndex(mediaWidthStartIndex, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'max-width') && eachRule.at.media?.find(({ name })=>name === 'min-width') && eachRule.priority !== -1, (eachRule)=>!eachRule.at.media?.find(({ name })=>name === 'max-width') && eachRule.at.media?.find(({ name })=>name === 'min-width') && eachRule.priority !== -1);
|
|
717
|
+
matchEndIndex = findIndex(mediaWidthStartIndex, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'max-width') && eachRule.at.media?.find(({ name })=>name === 'min-width') && eachRule.priority !== -1);
|
|
718
|
+
}
|
|
719
|
+
if (matchStartIndex !== -1) {
|
|
720
|
+
for(let i = matchEndIndex; i >= matchStartIndex; i--){
|
|
721
|
+
const value = (this.rules[i].at.media?.find(({ name })=>name === 'min-width')).value;
|
|
722
|
+
if (value > minWidthFeature.value) {
|
|
723
|
+
matchEndIndex = i - 1;
|
|
724
|
+
} else if (value < minWidthFeature.value) {
|
|
725
|
+
matchStartIndex = i + 1;
|
|
726
|
+
break;
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
} else {
|
|
731
|
+
/**
|
|
732
|
+
* find 第一個所遇到同樣 feature 且值比自己大的 rule,
|
|
733
|
+
* 並插入在該 rule 之後,讓自己優先被套用
|
|
734
|
+
*/ if (priority === -1) {
|
|
735
|
+
matchStartIndex = findIndex(mediaWidthStartIndex, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'min-width') || eachRule.priority !== -1, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'max-width'));
|
|
736
|
+
matchEndIndex = findIndex(mediaWidthStartIndex, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'min-width') || eachRule.priority !== -1);
|
|
737
|
+
} else {
|
|
738
|
+
matchStartIndex = findIndex(mediaWidthStartIndex, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'min-width') && eachRule.priority !== -1, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'max-width') && eachRule.priority !== -1);
|
|
739
|
+
matchEndIndex = findIndex(mediaWidthStartIndex, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'min-width') && eachRule.priority !== -1);
|
|
740
|
+
}
|
|
741
|
+
if (matchStartIndex !== -1) {
|
|
742
|
+
for(let i = matchEndIndex; i >= matchStartIndex; i--){
|
|
743
|
+
const value = (this.rules[i].at.media?.find(({ name })=>name === 'max-width')).value;
|
|
744
|
+
if (value < maxWidthFeature.value) {
|
|
745
|
+
matchEndIndex = i - 1;
|
|
746
|
+
} else if (value > maxWidthFeature.value) {
|
|
747
|
+
matchStartIndex = i + 1;
|
|
748
|
+
break;
|
|
749
|
+
}
|
|
750
|
+
}
|
|
751
|
+
}
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
} else {
|
|
755
|
+
if (priority === -1) {
|
|
756
|
+
matchStartIndex = mediaStartIndex;
|
|
757
|
+
matchEndIndex = findIndex(mediaStartIndex, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'max-width' || name === 'min-width') || eachRule.priority !== -1);
|
|
758
|
+
} else {
|
|
759
|
+
matchStartIndex = findIndex(mediaStartIndex, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'max-width' || name === 'min-width'), (eachRule)=>eachRule.priority !== -1);
|
|
760
|
+
matchEndIndex = findIndex(mediaStartIndex, (eachRule)=>eachRule.at.media?.find(({ name })=>name === 'max-width' || name === 'min-width'));
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
} else {
|
|
765
|
+
const findStartIndex = Object.keys(this.variablesNativeRules).length ? this.hasKeyframesRule ? 2 : 1 : this.hasKeyframesRule ? 1 : 0;
|
|
766
|
+
if (priority === -1) {
|
|
767
|
+
matchStartIndex = findStartIndex;
|
|
768
|
+
matchEndIndex = findIndex(findStartIndex, (eachRule)=>eachRule.atToken || eachRule.priority !== -1);
|
|
769
|
+
} else {
|
|
770
|
+
matchStartIndex = findIndex(findStartIndex, (eachRule)=>eachRule.atToken, (eachRule)=>eachRule.priority !== -1);
|
|
771
|
+
matchEndIndex = findIndex(findStartIndex, (eachRule)=>eachRule.atToken);
|
|
772
|
+
}
|
|
773
|
+
}
|
|
774
|
+
if (index === undefined && matchEndIndex !== undefined && matchStartIndex !== undefined) {
|
|
775
|
+
if (matchStartIndex === -1) {
|
|
776
|
+
index = matchEndIndex + 1;
|
|
777
|
+
} else {
|
|
778
|
+
if (priority === -1) {
|
|
779
|
+
for(let i = matchStartIndex; i <= matchEndIndex; i++){
|
|
780
|
+
const currentRule = this.rules[i];
|
|
781
|
+
if (!hasWhere && currentRule.hasWhere) continue;
|
|
782
|
+
if (hasWhere && !currentRule.hasWhere || currentRule.order >= order) {
|
|
783
|
+
index = i;
|
|
784
|
+
break;
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
} else {
|
|
788
|
+
for(let i = matchStartIndex; i <= matchEndIndex; i++){
|
|
789
|
+
const currentRule = this.rules[i];
|
|
790
|
+
if (!hasWhere && currentRule.hasWhere) continue;
|
|
791
|
+
if (hasWhere && !currentRule.hasWhere) {
|
|
792
|
+
index = i;
|
|
793
|
+
break;
|
|
794
|
+
}
|
|
795
|
+
if (currentRule.priority < priority) {
|
|
796
|
+
index = i;
|
|
797
|
+
break;
|
|
798
|
+
} else if (currentRule.priority === priority) {
|
|
799
|
+
if (currentRule.order >= order) {
|
|
800
|
+
index = i;
|
|
801
|
+
break;
|
|
802
|
+
}
|
|
803
|
+
} else {
|
|
804
|
+
index = i + 1;
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
if (index === undefined) {
|
|
809
|
+
index = matchEndIndex + 1;
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
}
|
|
813
|
+
this.rules.splice(index, 0, rule);
|
|
814
|
+
this.ruleBy[className] = rule;
|
|
815
|
+
if (this.style) {
|
|
816
|
+
const sheet = this.style.sheet;
|
|
817
|
+
let cssRuleIndex = 0;
|
|
818
|
+
const getCssRuleIndex = (index)=>{
|
|
819
|
+
const previousRule = this.rules[index];
|
|
820
|
+
if (previousRule) {
|
|
821
|
+
if (!previousRule.natives.length) return getCssRuleIndex(index - 1);
|
|
822
|
+
const lastNative = previousRule.natives[previousRule.natives.length - 1];
|
|
823
|
+
const lastNativeCssRule = lastNative.cssRule?.parentRule ?? lastNative.cssRule;
|
|
824
|
+
if (sheet) for(let i = 0; i < sheet.cssRules.length; i++){
|
|
825
|
+
if (sheet.cssRules[i] === lastNativeCssRule) {
|
|
826
|
+
cssRuleIndex = i + 1;
|
|
827
|
+
break;
|
|
828
|
+
}
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
};
|
|
832
|
+
getCssRuleIndex(index - 1);
|
|
833
|
+
for(let i = 0; i < rule.natives.length;){
|
|
834
|
+
try {
|
|
835
|
+
const native = rule.natives[i];
|
|
836
|
+
sheet?.insertRule(native.text, cssRuleIndex);
|
|
837
|
+
native.cssRule = sheet?.cssRules[cssRuleIndex++];
|
|
838
|
+
i++;
|
|
839
|
+
} catch (error) {
|
|
840
|
+
console.error(error);
|
|
841
|
+
rule.natives.splice(i, 1);
|
|
842
|
+
}
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
// variables
|
|
846
|
+
this.handleRuleWithVariableNames(rule);
|
|
847
|
+
// animations
|
|
848
|
+
this.handleRuleWithAnimationNames(rule);
|
|
849
|
+
rule.definition.insert?.call(rule);
|
|
850
|
+
return index;
|
|
851
|
+
}
|
|
852
|
+
get text() {
|
|
853
|
+
return this.rules.map((eachRule)=>eachRule.text).join('');
|
|
854
|
+
}
|
|
855
|
+
handleRuleWithAnimationNames(rule, initializing = false) {
|
|
856
|
+
if (rule.animationNames) {
|
|
857
|
+
const sheet = this.style?.sheet;
|
|
858
|
+
for (const eachKeyframeName of rule.animationNames){
|
|
859
|
+
const animation = this.animations[eachKeyframeName];
|
|
860
|
+
if (animation.usage) {
|
|
861
|
+
animation.usage++;
|
|
862
|
+
} else {
|
|
863
|
+
const nativeRule = {
|
|
864
|
+
text: `@keyframes ${eachKeyframeName}{` + Object.entries(animation).filter(([key])=>key !== 'usage' && key !== 'native').map(([key, variables])=>`${key}{${Object.entries(variables).map(([name, value])=>name + ':' + value).join(';')}}`).join('') + '}'
|
|
865
|
+
};
|
|
866
|
+
const keyframeRulesIndex = Object.keys(this.variablesNativeRules).length ? 1 : 0;
|
|
867
|
+
let keyframeRule;
|
|
868
|
+
if (this.hasKeyframesRule) {
|
|
869
|
+
(keyframeRule = this.rules[keyframeRulesIndex]).natives.push(nativeRule);
|
|
870
|
+
} else {
|
|
871
|
+
this.rules.splice(keyframeRulesIndex, 0, keyframeRule = {
|
|
872
|
+
natives: [
|
|
873
|
+
nativeRule
|
|
874
|
+
],
|
|
875
|
+
get text () {
|
|
876
|
+
return this.natives.map((eachNative)=>eachNative.text).join('');
|
|
877
|
+
}
|
|
878
|
+
});
|
|
879
|
+
this.hasKeyframesRule = true;
|
|
880
|
+
}
|
|
881
|
+
if (sheet) {
|
|
882
|
+
let cssRule;
|
|
883
|
+
if (initializing) {
|
|
884
|
+
for(let i = 0; i < sheet.cssRules.length; i++){
|
|
885
|
+
const eachCSSRule = sheet.cssRules[i];
|
|
886
|
+
if (eachCSSRule.constructor.name === 'CSSKeyframesRule' && eachCSSRule.name === eachKeyframeName) {
|
|
887
|
+
cssRule = eachCSSRule;
|
|
888
|
+
break;
|
|
889
|
+
}
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
if (cssRule) {
|
|
893
|
+
nativeRule.cssRule = cssRule;
|
|
894
|
+
} else {
|
|
895
|
+
const cssRuleIndex = Object.keys(this.variablesNativeRules).length + keyframeRule.natives.length - 1;
|
|
896
|
+
sheet.insertRule(nativeRule.text, cssRuleIndex);
|
|
897
|
+
nativeRule.cssRule = sheet.cssRules[cssRuleIndex];
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
animation.usage = 1;
|
|
901
|
+
animation.native = nativeRule;
|
|
902
|
+
}
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
}
|
|
906
|
+
handleRuleWithVariableNames(rule, initializing = false) {
|
|
907
|
+
if (rule.variableNames) {
|
|
908
|
+
const sheet = this.style?.sheet;
|
|
909
|
+
for (const eachVariableName of rule.variableNames){
|
|
910
|
+
const variable = this.variables[eachVariableName];
|
|
911
|
+
if (variable.usage) {
|
|
912
|
+
variable.usage++;
|
|
913
|
+
} else {
|
|
914
|
+
const addProperty = (mode, variable)=>{
|
|
915
|
+
let nativeRule = this.variablesNativeRules[mode];
|
|
916
|
+
if (!nativeRule) {
|
|
917
|
+
let cssRule;
|
|
918
|
+
let mediaConditionText;
|
|
919
|
+
let selectorText;
|
|
920
|
+
if (mode) {
|
|
921
|
+
switch(this.config.modes?.[mode]){
|
|
922
|
+
case 'media':
|
|
923
|
+
mediaConditionText = `@media(prefers-color-scheme:${mode})`;
|
|
924
|
+
selectorText = ':root';
|
|
925
|
+
break;
|
|
926
|
+
case 'host':
|
|
927
|
+
selectorText = `:host(.${mode})`;
|
|
928
|
+
break;
|
|
929
|
+
case 'class':
|
|
930
|
+
selectorText = `.${mode}`;
|
|
931
|
+
break;
|
|
932
|
+
default:
|
|
933
|
+
return;
|
|
934
|
+
}
|
|
935
|
+
} else {
|
|
936
|
+
selectorText = ':root';
|
|
937
|
+
}
|
|
938
|
+
if (sheet) {
|
|
939
|
+
const newCSSRuleIndex = Object.keys(this.variablesNativeRules).length ? this.rules[0].natives.length : 0;
|
|
940
|
+
sheet.insertRule((mediaConditionText ? mediaConditionText + '{' : '') + selectorText + '{}' + (mediaConditionText ? mediaConditionText + '}' : ''), newCSSRuleIndex);
|
|
941
|
+
cssRule = mediaConditionText ? sheet.cssRules[newCSSRuleIndex].cssRules[0] : sheet.cssRules[newCSSRuleIndex];
|
|
942
|
+
} else {
|
|
943
|
+
const styleMap = new Map();
|
|
944
|
+
const style = Object.defineProperties({}, {
|
|
945
|
+
getPropertyValue: {
|
|
946
|
+
value: (property)=>styleMap.get(property)
|
|
947
|
+
},
|
|
948
|
+
removeProperty: {
|
|
949
|
+
value: (property)=>{
|
|
950
|
+
styleMap.delete(property);
|
|
951
|
+
for(let i = 0; i < style.length; i++){
|
|
952
|
+
if (style[i] === property) {
|
|
953
|
+
delete style[i];
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
},
|
|
958
|
+
setProperty: {
|
|
959
|
+
value: (property, value)=>{
|
|
960
|
+
style[style.length] = property;
|
|
961
|
+
styleMap.set(property, value);
|
|
962
|
+
}
|
|
963
|
+
},
|
|
964
|
+
length: {
|
|
965
|
+
get () {
|
|
966
|
+
return Object.keys(style).length;
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
});
|
|
970
|
+
cssRule = {
|
|
971
|
+
selectorText,
|
|
972
|
+
style,
|
|
973
|
+
styleMap
|
|
974
|
+
};
|
|
975
|
+
if (mediaConditionText) {
|
|
976
|
+
// @ts-ignore
|
|
977
|
+
cssRule.parentRule = {
|
|
978
|
+
conditionText: mediaConditionText
|
|
979
|
+
};
|
|
980
|
+
}
|
|
981
|
+
}
|
|
982
|
+
nativeRule = this.pushVariableNativeRule(mode, cssRule);
|
|
983
|
+
}
|
|
984
|
+
const propertyName = '--' + eachVariableName;
|
|
985
|
+
if (!initializing || !nativeRule.cssRule.style.getPropertyValue(propertyName)) {
|
|
986
|
+
nativeRule.cssRule.style.setProperty(propertyName, String(variable.value));
|
|
987
|
+
}
|
|
988
|
+
};
|
|
989
|
+
if (variable.value) {
|
|
990
|
+
addProperty('', variable);
|
|
991
|
+
}
|
|
992
|
+
if (variable.modes) {
|
|
993
|
+
for(const mode in variable.modes){
|
|
994
|
+
addProperty(mode, variable.modes[mode]);
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
variable.usage = 1;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
pushVariableNativeRule(mode, variableCSSRule) {
|
|
1003
|
+
if (!Object.keys(this.variablesNativeRules).length) {
|
|
1004
|
+
this.variablesNativeRules = {};
|
|
1005
|
+
const newRule = {
|
|
1006
|
+
natives: [],
|
|
1007
|
+
get text () {
|
|
1008
|
+
return this.natives.map((eachNative)=>eachNative.text).join('');
|
|
1009
|
+
}
|
|
1010
|
+
};
|
|
1011
|
+
this.rules.splice(0, 0, newRule);
|
|
1012
|
+
}
|
|
1013
|
+
let prefix = '';
|
|
1014
|
+
let suffix = '}';
|
|
1015
|
+
if (variableCSSRule.parentRule) {
|
|
1016
|
+
prefix += variableCSSRule.parentRule.conditionText.replace(/ /g, '') + '{';
|
|
1017
|
+
suffix += '}';
|
|
1018
|
+
}
|
|
1019
|
+
prefix += variableCSSRule.selectorText + '{';
|
|
1020
|
+
const nativeRule = {
|
|
1021
|
+
cssRule: variableCSSRule,
|
|
1022
|
+
get text () {
|
|
1023
|
+
const properties = [];
|
|
1024
|
+
for(let i = 0; i < variableCSSRule.style.length; i++){
|
|
1025
|
+
const property = variableCSSRule.style[i];
|
|
1026
|
+
properties.push(property + ':' + variableCSSRule.style.getPropertyValue(property));
|
|
1027
|
+
}
|
|
1028
|
+
return prefix + properties.join(';') + suffix;
|
|
1029
|
+
}
|
|
1030
|
+
};
|
|
1031
|
+
this.rules[0].natives.push(this.variablesNativeRules[mode] = nativeRule);
|
|
1032
|
+
return nativeRule;
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
const masterCSSs = [];
|
|
1036
|
+
(()=>{
|
|
1037
|
+
globalThis.MasterCSS = MasterCSS;
|
|
1038
|
+
globalThis.masterCSSs = masterCSSs;
|
|
1039
|
+
})();
|
|
1040
|
+
|
|
1041
|
+
export { MasterCSS as default, masterCSSs };
|