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