@master/css 2.0.0-rc.26 → 2.0.0-rc.27

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