@master/css 2.0.0-rc.4 → 2.0.0-rc.41

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