@master/css 2.0.0-rc.5 → 2.0.0-rc.51

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