@master/css 2.0.0-rc.46 → 2.0.0-rc.48

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 (72) hide show
  1. package/dist/common.cjs +1 -1
  2. package/dist/common.mjs +1 -1
  3. package/dist/config/functions.d.ts +1 -1
  4. package/dist/config/index.cjs +3 -3
  5. package/dist/config/index.d.ts +3 -48
  6. package/dist/config/index.mjs +3 -3
  7. package/dist/config/modes.d.ts +1 -1
  8. package/dist/config/{syntaxes.cjs → rules.cjs} +282 -282
  9. package/dist/config/rules.d.ts +282 -0
  10. package/dist/config/{syntaxes.mjs → rules.mjs} +282 -282
  11. package/dist/core.cjs +225 -607
  12. package/dist/core.d.ts +36 -67
  13. package/dist/core.mjs +225 -607
  14. package/dist/index.cjs +14 -6
  15. package/dist/index.d.ts +8 -4
  16. package/dist/index.mjs +7 -3
  17. package/dist/layer.cjs +107 -23
  18. package/dist/layer.d.ts +13 -27
  19. package/dist/layer.mjs +107 -23
  20. package/dist/non-layer.cjs +65 -0
  21. package/dist/non-layer.d.ts +12 -0
  22. package/dist/non-layer.mjs +63 -0
  23. package/dist/rule.cjs +8 -708
  24. package/dist/rule.d.ts +6 -147
  25. package/dist/rule.mjs +8 -708
  26. package/dist/shared/utils/find-native-css-rule-index.cjs +12 -0
  27. package/dist/shared/utils/find-native-css-rule-index.mjs +10 -0
  28. package/dist/syntax-layer.cjs +291 -0
  29. package/dist/syntax-layer.d.ts +17 -0
  30. package/dist/syntax-layer.mjs +289 -0
  31. package/dist/syntax-rule-type.cjs +27 -0
  32. package/dist/syntax-rule-type.d.ts +28 -0
  33. package/dist/syntax-rule-type.mjs +25 -0
  34. package/dist/syntax-rule.cjs +717 -0
  35. package/dist/syntax-rule.d.ts +47 -0
  36. package/dist/syntax-rule.mjs +715 -0
  37. package/dist/types/config.cjs +2 -0
  38. package/dist/types/config.d.ts +70 -0
  39. package/dist/types/config.mjs +1 -0
  40. package/dist/types/syntax.cjs +2 -0
  41. package/dist/types/syntax.d.ts +106 -0
  42. package/dist/types/syntax.mjs +1 -0
  43. package/dist/utils/are-rule-at-equal.cjs +7 -7
  44. package/dist/utils/are-rule-at-equal.d.ts +2 -2
  45. package/dist/utils/are-rule-at-equal.mjs +7 -7
  46. package/dist/utils/are-rule-modes-equal.cjs +2 -2
  47. package/dist/utils/are-rule-modes-equal.d.ts +2 -2
  48. package/dist/utils/are-rule-modes-equal.mjs +2 -2
  49. package/dist/utils/are-rule-selectors-equal.cjs +11 -11
  50. package/dist/utils/are-rule-selectors-equal.d.ts +2 -2
  51. package/dist/utils/are-rule-selectors-equal.mjs +11 -11
  52. package/dist/utils/are-rule-states-equal.cjs +2 -2
  53. package/dist/utils/are-rule-states-equal.d.ts +2 -2
  54. package/dist/utils/are-rule-states-equal.mjs +2 -2
  55. package/dist/utils/are-rules-duplicated.cjs +17 -0
  56. package/dist/utils/are-rules-duplicated.d.ts +2 -0
  57. package/dist/utils/are-rules-duplicated.mjs +15 -0
  58. package/dist/utils/autofill-solid-style.d.ts +2 -2
  59. package/dist/utils/extend-config.cjs +5 -5
  60. package/dist/utils/extend-config.d.ts +1 -1
  61. package/dist/utils/extend-config.mjs +5 -5
  62. package/dist/utils/generate-css.cjs +1 -1
  63. package/dist/utils/generate-css.mjs +1 -1
  64. package/dist/utils/is-core-rule.cjs +2 -2
  65. package/dist/utils/is-core-rule.mjs +2 -2
  66. package/dist/utils/reorder-for-readable-classes.cjs +11 -11
  67. package/dist/utils/reorder-for-readable-classes.mjs +11 -11
  68. package/package.json +1 -1
  69. package/dist/config/syntaxes.d.ts +0 -282
  70. package/dist/utils/are-syntaxes-duplicated.cjs +0 -17
  71. package/dist/utils/are-syntaxes-duplicated.d.ts +0 -2
  72. package/dist/utils/are-syntaxes-duplicated.mjs +0 -15
package/dist/core.cjs CHANGED
@@ -2,53 +2,83 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var rule = require('./rule.cjs');
5
+ var syntaxRule = require('./syntax-rule.cjs');
6
6
  var index = require('./config/index.cjs');
7
- var layer = require('./layer.cjs');
8
7
  var hexToRgb = require('./utils/hex-to-rgb.cjs');
9
8
  var flattenObject = require('./utils/flatten-object.cjs');
10
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');
11
15
 
12
16
  class MasterCSS {
13
17
  customConfig;
14
18
  static config = index.config;
15
- syntaxes;
16
- ruleBy;
17
- classesUsage;
19
+ definedRules;
18
20
  config;
19
- Rules;
21
+ classesUsage;
22
+ layerStatementRule;
23
+ rules;
24
+ animationsNonLayer;
25
+ baseLayer;
26
+ themeLayer;
27
+ presetLayer;
28
+ stylesLayer;
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
+ }
20
45
  constructor(customConfig){
21
46
  this.customConfig = customConfig;
22
- this.syntaxes = [];
23
- this.ruleBy = {};
47
+ this.definedRules = [];
24
48
  this.classesUsage = {};
25
- this.Rules = [];
26
- if (!customConfig?.override) {
27
- this.config = extendConfig(index.config, customConfig);
28
- } else {
29
- this.config = extendConfig(customConfig);
30
- }
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.stylesLayer = new syntaxLayer('components', this);
62
+ this.generalLayer = new syntaxLayer('general', this);
63
+ this.config = customConfig?.override ? extendConfig(customConfig) : extendConfig(index.config, customConfig);
31
64
  this.resolve();
32
65
  if (this.constructor === MasterCSS) {
33
66
  masterCSSs.push(this);
34
67
  }
35
68
  }
36
69
  resolve() {
37
- this.styles = {};
38
- this.stylesBy = {};
70
+ this.components = {};
39
71
  this.selectors = {};
40
72
  this.variables = {};
41
73
  this.at = {};
42
74
  this.animations = {};
43
- this.Rules.length = 0;
44
- this.variablesNativeRules = {};
45
- this.hasKeyframesRule = false;
75
+ this.definedRules.length = 0;
46
76
  const colorVariableNames = {
47
77
  current: undefined,
48
78
  currentColor: undefined,
49
79
  transparent: undefined
50
80
  };
51
- const { styles, selectors, variables, utilities, at, syntaxes, animations } = this.config;
81
+ const { components, selectors, variables, utilities, at, rules, animations } = this.config;
52
82
  function escapeString(str) {
53
83
  return str.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
54
84
  }
@@ -280,33 +310,23 @@ class MasterCSS {
280
310
  }
281
311
  }
282
312
  }
283
- const flattedStyles = styles ? flattenObject.flattenObject(styles) : {};
313
+ const flattedStyles = components ? flattenObject.flattenObject(components) : {};
284
314
  const utilityNames = Object.keys(flattedStyles);
285
315
  const handleUtilityName = (utilityName)=>{
286
- if (Object.prototype.hasOwnProperty.call(this.styles, utilityName)) return;
287
- const currentClass = this.styles[utilityName] = [];
316
+ if (Object.prototype.hasOwnProperty.call(this.components, utilityName)) return;
317
+ const currentClass = this.components[utilityName] = [];
288
318
  const className = flattedStyles[utilityName];
289
319
  if (!className) return;
290
320
  const classNames = className.replace(/(?:\n(?:\s*))+/g, ' ').trim().split(' ');
291
321
  for (const eachClassName of classNames){
292
322
  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
323
  if (!currentClass.includes(className)) {
304
324
  currentClass.push(className);
305
325
  }
306
326
  };
307
327
  if (utilityNames.includes(eachClassName)) {
308
328
  handleUtilityName(eachClassName);
309
- for (const parentClassName of this.styles[eachClassName]){
329
+ for (const parentClassName of this.components[eachClassName]){
310
330
  handle(parentClassName);
311
331
  }
312
332
  } else {
@@ -317,7 +337,7 @@ class MasterCSS {
317
337
  for (const eachUtilityName of utilityNames){
318
338
  handleUtilityName(eachUtilityName);
319
339
  }
320
- if (syntaxes || utilities) {
340
+ if (rules || utilities) {
321
341
  const rulesEntries = [];
322
342
  if (utilities) {
323
343
  for(const utilityName in utilities){
@@ -326,61 +346,69 @@ class MasterCSS {
326
346
  utilityName,
327
347
  {
328
348
  declarations,
329
- layer: layer.Utility
349
+ type: syntaxRuleType.Utility
330
350
  }
331
351
  ]);
332
352
  }
333
353
  }
334
- if (syntaxes) {
335
- rulesEntries.push(...Object.entries(syntaxes));
354
+ if (rules) {
355
+ rulesEntries.push(...Object.entries(rules));
336
356
  }
337
357
  const rulesEntriesLength = rulesEntries.length;
338
358
  const colorNames = Object.keys(colorVariableNames);
339
359
  rulesEntries.sort((a, b)=>{
340
- if (a[1].layer !== b[1].layer) {
341
- return (b[1].layer || 0) - (a[1].layer || 0);
360
+ if (a[1].type !== b[1].type) {
361
+ return (b[1].type || 0) - (a[1].type || 0);
342
362
  }
343
363
  return b[0].localeCompare(a[0]);
344
- }).forEach(([id, eachSyntaxDefinition], index)=>{
345
- const EachRule = {
364
+ }).forEach(([id, eachSyntaxRuleDefinition], index)=>{
365
+ const syntax = {
346
366
  id,
347
367
  keys: [],
348
368
  variables: {},
349
369
  matchers: {},
350
370
  order: rulesEntriesLength - 1 - index,
351
- definition: eachSyntaxDefinition
371
+ definition: eachSyntaxRuleDefinition
352
372
  };
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');
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');
358
386
  }
359
387
  // todo: 不可使用 startsWith 判斷,應改為更精準的從 config.variables 取得目標變數群組,但 config.variables 中的值還沒被 resolve 像是 Array
360
388
  const addResolvedVariables = (groupName)=>{
361
389
  for(const eachVariableName in this.variables){
362
390
  const eachVariable = this.variables[eachVariableName];
363
391
  if (eachVariable.group === groupName) {
364
- EachRule.variables[eachVariable.key] = eachVariable;
392
+ syntax.variables[eachVariable.key] = eachVariable;
365
393
  }
366
394
  }
367
395
  };
368
- // 1. custom `config.syntaxes[id].variables`
369
- if (eachSyntaxDefinition.variables) {
370
- for (const eachVariableGroup of eachSyntaxDefinition.variables){
396
+ // 1. custom `config.rules[id].variables`
397
+ if (eachSyntaxRuleDefinition.variables) {
398
+ for (const eachVariableGroup of eachSyntaxRuleDefinition.variables){
371
399
  addResolvedVariables(eachVariableGroup);
372
400
  }
373
401
  }
374
402
  // 2. custom `config.variables`
375
403
  addResolvedVariables(id);
376
404
  const keys = [];
377
- let { key } = eachSyntaxDefinition;
378
- if (layer$1 === layer.NativeShorthand || layer$1 === layer.Native) {
379
- if (!key) eachSyntaxDefinition.key = key = id;
405
+ let { key } = eachSyntaxRuleDefinition;
406
+ if (type === syntaxRuleType.NativeShorthand || type === syntaxRuleType.Native) {
407
+ if (!key) eachSyntaxRuleDefinition.key = key = id;
380
408
  keys.push(id);
381
409
  }
382
410
  if (sign) {
383
- EachRule.matchers.arbitrary = new RegExp(`^${sign}[^!*>+~:[@_]+\\|`);
411
+ syntax.matchers.arbitrary = new RegExp(`^${sign}[^!*>+~:[@_]+\\|`);
384
412
  } else if (!matcher) {
385
413
  const colorsPatten = colorNames.join('|');
386
414
  if (!key && !subkey) {
@@ -388,13 +416,13 @@ class MasterCSS {
388
416
  } else {
389
417
  if (key && !keys.includes(key)) keys.push(key);
390
418
  if (subkey) keys.push(subkey);
391
- if (layer$1 === layer.Shorthand) {
419
+ if (type === syntaxRuleType.Shorthand) {
392
420
  keys.push(id);
393
421
  }
394
422
  }
395
423
  if (ambiguousKeys?.length) {
396
424
  const ambiguousKeyPattern = ambiguousKeys.length > 1 ? `(?:${ambiguousKeys.join('|')})` : ambiguousKeys[0];
397
- const variableKeys = Object.keys(EachRule.variables);
425
+ const variableKeys = Object.keys(syntax.variables);
398
426
  if (ambiguousValues?.length) {
399
427
  const ambiguousValuePatterns = [];
400
428
  for (const eachAmbiguousValue of ambiguousValues){
@@ -404,21 +432,37 @@ class MasterCSS {
404
432
  ambiguousValuePatterns.unshift(`${eachAmbiguousValue}(?:\\b|_)`);
405
433
  }
406
434
  }
407
- EachRule.matchers.value = new RegExp(`^${ambiguousKeyPattern}:(?:${ambiguousValuePatterns.join('|')})[^|]*?(?:@|$)`);
435
+ syntax.matchers.value = new RegExp(`^${ambiguousKeyPattern}:(?:${ambiguousValuePatterns.join('|')})[^|]*?(?:@|$)`);
408
436
  }
409
437
  if (variableKeys.length) {
410
- EachRule.matchers.variable = new RegExp(`^${ambiguousKeyPattern}:(?:${variableKeys.join('|')}(?![a-zA-Z0-9-]))[^|]*?(?:@|$)`);
438
+ syntax.matchers.variable = new RegExp(`^${ambiguousKeyPattern}:(?:${variableKeys.join('|')}(?![a-zA-Z0-9-]))[^|]*?(?:@|$)`);
411
439
  }
412
440
  }
413
441
  } else {
414
- EachRule.matchers.arbitrary = matcher;
442
+ syntax.matchers.arbitrary = matcher;
415
443
  }
416
444
  if (keys.length) {
417
- EachRule.keys = keys;
418
- EachRule.matchers.key = new RegExp(`^${keys.length > 1 ? `(${keys.join('|')})` : keys[0]}:.`);
445
+ syntax.keys = keys;
446
+ syntax.matchers.key = new RegExp(`^${keys.length > 1 ? `(${keys.join('|')})` : keys[0]}:.`);
419
447
  }
420
448
  });
421
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
+ }
422
466
  }
423
467
  /**
424
468
  * Match check if Master CSS syntax
@@ -428,613 +472,187 @@ class MasterCSS {
428
472
  /**
429
473
  * 1. variable
430
474
  * @example fg:primary bg:blue
431
- */ for (const EachRule of this.Rules){
432
- if (EachRule.matchers.variable?.test(className)) return EachRule;
475
+ */ for (const eachSyntax of this.definedRules){
476
+ if (eachSyntax.matchers.variable?.test(className)) return eachSyntax;
433
477
  }
434
478
  /**
435
479
  * 2. value (ambiguous.key * ambiguous.values)
436
480
  * @example bg:current box:content font:12
437
- */ for (const EachRule of this.Rules){
438
- if (EachRule.matchers.value?.test(className)) return EachRule;
481
+ */ for (const eachSyntax of this.definedRules){
482
+ if (eachSyntax.matchers.value?.test(className)) return eachSyntax;
439
483
  }
440
484
  /**
441
485
  * 3. full key
442
486
  * @example text-align:center color:blue-40
443
- */ for (const EachRule of this.Rules){
444
- if (EachRule.matchers.key?.test(className)) return EachRule;
487
+ */ for (const eachSyntax of this.definedRules){
488
+ if (eachSyntax.matchers.key?.test(className)) return eachSyntax;
445
489
  }
446
490
  /**
447
491
  * 4. arbitrary
448
492
  * @example custom RegExp, utility
449
- */ for (const EachRule of this.Rules){
450
- if (EachRule.matchers.arbitrary?.test(className)) return EachRule;
493
+ */ for (const eachSyntax of this.definedRules){
494
+ if (eachSyntax.matchers.arbitrary?.test(className)) return eachSyntax;
451
495
  }
452
496
  }
453
497
  /**
454
- * Generate syntaxes from class name
498
+ * Generate syntax rules from class name
455
499
  * @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);
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);
461
524
  }
462
525
  /**
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);
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
+ }
471
574
  }
575
+ return [];
472
576
  }
473
577
  /**
474
578
  * 根據蒐集到的所有 DOM class 重新 create
475
579
  */ refresh(customConfig) {
580
+ this.reset();
476
581
  if (customConfig) {
477
582
  this.customConfig = customConfig;
478
583
  } else {
479
584
  customConfig = this.customConfig;
480
585
  }
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
586
  // @ts-ignore
491
- this.ruleBy = {};
587
+ this.config = customConfig?.override ? extendConfig(customConfig) : extendConfig(index.config, customConfig);
588
+ this.resolve();
492
589
  /**
493
- * 拿當前所有的 classNames 按照最新的 colors, config.syntaxes 匹配並生成新的 style
494
- * 所以 refresh 過後 syntaxes 可能會變多也可能會變少
590
+ * 拿當前所有的 classNames 按照最新的 colors, config.rules 匹配並生成新的 style
591
+ * 所以 refresh 過後 rules 可能會變多也可能會變少
495
592
  */ for(const name in this.classesUsage){
496
593
  this.add(name);
497
594
  }
498
595
  return this;
499
596
  }
500
597
  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
- }
598
+ this.baseLayer.reset();
599
+ this.themeLayer.reset();
600
+ this.presetLayer.reset();
601
+ this.stylesLayer.reset();
602
+ this.generalLayer.reset();
603
+ this.animationsNonLayer.reset();
517
604
  return this;
518
605
  }
519
606
  destroy() {
520
- this.reset();
607
+ // @ts-ignore
608
+ this.classesUsage = {};
521
609
  masterCSSs.splice(masterCSSs.indexOf(this), 1);
522
610
  return this;
523
611
  }
524
612
  add(...classNames) {
525
613
  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
- }
614
+ this.generate(className).forEach((eachSyntaxRule)=>eachSyntaxRule.layer.insert(eachSyntaxRule));
532
615
  }
533
616
  return this;
534
617
  }
535
- delete(...classNames) {
618
+ remove(...classNames) {
536
619
  /**
537
620
  * class name 從 DOM tree 中被移除,
538
621
  * 匹配並刪除對應的 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 (const {} of rule.natives){
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;
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.stylesLayer.delete(className + ' ' + eachSyntax);
606
626
  }
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
627
  } 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
- }
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.stylesLayer.delete(eachSyntax + atToken + ' ' + className);
750
635
  }
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
636
  } 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
- }
637
+ this.generalLayer.delete(className);
784
638
  }
785
639
  } 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 (const eachCSSRule of sheet.cssRules){
883
- if (eachCSSRule.constructor.name === 'CSSKeyframesRule' && eachCSSRule.name === eachKeyframeName) {
884
- cssRule = eachCSSRule;
885
- break;
886
- }
887
- }
888
- }
889
- if (cssRule) {
890
- nativeRule.cssRule = cssRule;
891
- } else {
892
- const cssRuleIndex = Object.keys(this.variablesNativeRules).length + keyframeRule.natives.length - 1;
893
- sheet.insertRule(nativeRule.text, cssRuleIndex);
894
- nativeRule.cssRule = sheet.cssRules[cssRuleIndex];
895
- }
896
- }
897
- animation.usage = 1;
898
- animation.native = nativeRule;
640
+ this.generalLayer.delete(className);
899
641
  }
900
642
  }
901
643
  }
902
644
  }
903
- handleRuleWithVariableNames(rule, initializing = false) {
904
- if (rule.variableNames) {
905
- const sheet = this.style?.sheet;
906
- for (const eachVariableName of rule.variableNames){
907
- const variable = this.variables[eachVariableName];
908
- if (variable.usage) {
909
- variable.usage++;
910
- } else {
911
- const addProperty = (mode, variable)=>{
912
- let nativeRule = this.variablesNativeRules[mode];
913
- if (!nativeRule) {
914
- let cssRule;
915
- let mediaConditionText;
916
- let selectorText;
917
- if (mode) {
918
- switch(this.config.modes?.[mode]){
919
- case 'media':
920
- mediaConditionText = `@media(prefers-color-scheme:${mode})`;
921
- selectorText = ':root';
922
- break;
923
- case 'host':
924
- selectorText = `:host(.${mode})`;
925
- if (this.config.defaultMode === mode) {
926
- selectorText += ',:host';
927
- }
928
- break;
929
- case 'class':
930
- selectorText = '.' + mode;
931
- if (this.config.defaultMode === mode) {
932
- selectorText += ',:root';
933
- }
934
- break;
935
- default:
936
- return;
937
- }
938
- } else {
939
- selectorText = ':root';
940
- }
941
- if (sheet) {
942
- const newCSSRuleIndex = Object.keys(this.variablesNativeRules).length ? this.syntaxes[0].natives.length : 0;
943
- sheet.insertRule((mediaConditionText ? mediaConditionText + '{' : '') + selectorText + '{}' + (mediaConditionText ? mediaConditionText + '}' : ''), newCSSRuleIndex);
944
- cssRule = mediaConditionText ? sheet.cssRules[newCSSRuleIndex].cssRules[0] : sheet.cssRules[newCSSRuleIndex];
945
- } else {
946
- const styleMap = new Map();
947
- const style = Object.defineProperties({}, {
948
- getPropertyValue: {
949
- value: (property)=>styleMap.get(property)
950
- },
951
- removeProperty: {
952
- value: (property)=>{
953
- styleMap.delete(property);
954
- for(let i = 0; i < style.length; i++){
955
- if (style[i] === property) {
956
- delete style[i];
957
- }
958
- }
959
- }
960
- },
961
- setProperty: {
962
- value: (property, value)=>{
963
- style[style.length] = property;
964
- styleMap.set(property, value);
965
- }
966
- },
967
- length: {
968
- get () {
969
- return Object.keys(style).length;
970
- }
971
- }
972
- });
973
- cssRule = {
974
- selectorText,
975
- style,
976
- styleMap
977
- };
978
- if (mediaConditionText) {
979
- // @ts-ignore
980
- cssRule.parentRule = {
981
- conditionText: mediaConditionText
982
- };
983
- }
984
- }
985
- nativeRule = this.pushVariableNativeRule(mode, cssRule);
986
- }
987
- const propertyName = '--' + eachVariableName;
988
- if (!initializing || !nativeRule.cssRule.style.getPropertyValue(propertyName)) {
989
- nativeRule.cssRule.style.setProperty(propertyName, String(variable.value));
990
- }
991
- };
992
- if (variable.value) {
993
- addProperty('', variable);
994
- }
995
- if (variable.modes) {
996
- for(const mode in variable.modes){
997
- addProperty(mode, variable.modes[mode]);
998
- }
999
- }
1000
- variable.usage = 1;
1001
- }
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 + ')';
1002
653
  }
1003
654
  }
1004
655
  }
1005
- pushVariableNativeRule(mode, variableCSSRule) {
1006
- if (!Object.keys(this.variablesNativeRules).length) {
1007
- this.variablesNativeRules = {};
1008
- const newRule = {
1009
- natives: [],
1010
- get text () {
1011
- return this.natives.map((eachNative)=>eachNative.text).join('');
1012
- }
1013
- };
1014
- this.syntaxes.splice(0, 0, newRule);
1015
- }
1016
- let prefix = '';
1017
- let suffix = '}';
1018
- if (variableCSSRule.parentRule) {
1019
- prefix += '@media' + variableCSSRule.parentRule.conditionText.replace(/ /g, '') + '{';
1020
- suffix += '}';
1021
- }
1022
- prefix += variableCSSRule.selectorText + '{';
1023
- const nativeRule = {
1024
- cssRule: variableCSSRule,
1025
- get text () {
1026
- const properties = [];
1027
- // eslint-disable-next-line @typescript-eslint/prefer-for-of
1028
- for(let i = 0; i < variableCSSRule.style.length; i++){
1029
- const property = variableCSSRule.style[i];
1030
- properties.push(property + ':' + variableCSSRule.style.getPropertyValue(property));
1031
- }
1032
- return prefix + properties.join(';') + suffix;
1033
- }
1034
- };
1035
- this.syntaxes[0].natives.push(this.variablesNativeRules[mode] = nativeRule);
1036
- return nativeRule;
1037
- }
1038
656
  }
1039
657
  const masterCSSs = [];
1040
658
  (()=>{