@master/css 2.0.0-rc.60 → 2.0.0-rc.62

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 (122) hide show
  1. package/dist/animation-rule.cjs +24 -0
  2. package/dist/animation-rule.d.ts +11 -0
  3. package/dist/animation-rule.mjs +22 -0
  4. package/dist/common.cjs +50 -1
  5. package/dist/common.d.ts +14 -0
  6. package/dist/common.mjs +45 -2
  7. package/dist/config/at.cjs +13 -10
  8. package/dist/config/at.d.ts +4 -1
  9. package/dist/config/at.mjs +13 -10
  10. package/dist/config/index.cjs +3 -0
  11. package/dist/config/index.d.ts +2 -1
  12. package/dist/config/index.mjs +3 -1
  13. package/dist/config/modes.d.ts +4 -2
  14. package/dist/config/rules.cjs +28 -46
  15. package/dist/config/rules.d.ts +4 -2
  16. package/dist/config/rules.mjs +28 -46
  17. package/dist/config/screens.cjs +17 -0
  18. package/dist/config/screens.d.ts +14 -0
  19. package/dist/config/screens.mjs +15 -0
  20. package/dist/config/selectors.cjs +11 -15
  21. package/dist/config/selectors.d.ts +10 -8
  22. package/dist/config/selectors.mjs +11 -15
  23. package/dist/config/utilities.cjs +3 -0
  24. package/dist/config/utilities.d.ts +3 -0
  25. package/dist/config/utilities.mjs +3 -0
  26. package/dist/config/variables.cjs +0 -13
  27. package/dist/config/variables.d.ts +0 -13
  28. package/dist/config/variables.mjs +0 -13
  29. package/dist/core.cjs +438 -476
  30. package/dist/core.d.ts +14 -6
  31. package/dist/core.mjs +438 -476
  32. package/dist/factories/with-syntax-layer.cjs +37 -260
  33. package/dist/factories/with-syntax-layer.mjs +37 -260
  34. package/dist/index.cjs +38 -12
  35. package/dist/index.d.ts +19 -7
  36. package/dist/index.mjs +17 -7
  37. package/dist/layer.cjs +11 -7
  38. package/dist/layer.d.ts +4 -3
  39. package/dist/layer.mjs +11 -7
  40. package/dist/non-layer.cjs +10 -17
  41. package/dist/non-layer.d.ts +1 -1
  42. package/dist/non-layer.mjs +10 -17
  43. package/dist/rule.cjs +4 -6
  44. package/dist/rule.d.ts +3 -10
  45. package/dist/rule.mjs +4 -6
  46. package/dist/syntax-rule-type.cjs +7 -8
  47. package/dist/syntax-rule-type.d.ts +7 -7
  48. package/dist/syntax-rule-type.mjs +7 -8
  49. package/dist/syntax-rule.cjs +125 -315
  50. package/dist/syntax-rule.d.ts +18 -21
  51. package/dist/syntax-rule.mjs +126 -316
  52. package/dist/types/common.d.ts +2 -0
  53. package/dist/types/config.d.ts +12 -7
  54. package/dist/types/syntax.d.ts +5 -24
  55. package/dist/utils/compare-rule-priority.cjs +143 -0
  56. package/dist/utils/compare-rule-priority.d.ts +7 -0
  57. package/dist/utils/compare-rule-priority.mjs +138 -0
  58. package/dist/utils/equal-at-rules.cjs +22 -0
  59. package/dist/utils/equal-at-rules.d.ts +2 -0
  60. package/dist/utils/equal-at-rules.mjs +20 -0
  61. package/dist/utils/equal-declarations.cjs +10 -0
  62. package/dist/utils/equal-declarations.d.ts +2 -0
  63. package/dist/utils/equal-declarations.mjs +8 -0
  64. package/dist/utils/equal-selectors.cjs +70 -0
  65. package/dist/utils/equal-selectors.d.ts +2 -0
  66. package/dist/utils/equal-selectors.mjs +68 -0
  67. package/dist/utils/equal-variants.cjs +10 -0
  68. package/dist/utils/equal-variants.d.ts +2 -0
  69. package/dist/utils/equal-variants.mjs +8 -0
  70. package/dist/utils/extend-config.cjs +0 -1
  71. package/dist/utils/extend-config.mjs +0 -1
  72. package/dist/utils/generate-at.cjs +30 -0
  73. package/dist/utils/generate-at.d.ts +2 -0
  74. package/dist/utils/generate-at.mjs +28 -0
  75. package/dist/utils/generate-selector.cjs +49 -0
  76. package/dist/utils/generate-selector.d.ts +2 -0
  77. package/dist/utils/generate-selector.mjs +47 -0
  78. package/dist/utils/get-single-at-number-rule-node.cjs +9 -0
  79. package/dist/utils/get-single-at-number-rule-node.d.ts +2 -0
  80. package/dist/utils/get-single-at-number-rule-node.mjs +7 -0
  81. package/dist/utils/parse-at.cjs +157 -0
  82. package/dist/utils/parse-at.d.ts +39 -0
  83. package/dist/utils/parse-at.mjs +155 -0
  84. package/dist/utils/parse-pair.cjs +51 -0
  85. package/dist/utils/parse-pair.d.ts +8 -0
  86. package/dist/utils/parse-pair.mjs +49 -0
  87. package/dist/utils/parse-selector.cjs +112 -0
  88. package/dist/utils/parse-selector.d.ts +19 -0
  89. package/dist/utils/parse-selector.mjs +110 -0
  90. package/dist/utils/parse-value.cjs +39 -0
  91. package/dist/utils/parse-value.d.ts +2 -0
  92. package/dist/utils/parse-value.mjs +37 -0
  93. package/dist/utils/replace-char-outside-quotes.cjs +21 -0
  94. package/dist/utils/replace-char-outside-quotes.d.ts +1 -0
  95. package/dist/utils/replace-char-outside-quotes.mjs +19 -0
  96. package/dist/utils/sort-readable-classes.cjs +74 -0
  97. package/dist/utils/{reorder-for-readable-classes.d.ts → sort-readable-classes.d.ts} +1 -1
  98. package/dist/utils/sort-readable-classes.mjs +72 -0
  99. package/dist/utils/split-char-outside-quotes.cjs +29 -0
  100. package/dist/utils/split-char-outside-quotes.d.ts +1 -0
  101. package/dist/utils/split-char-outside-quotes.mjs +27 -0
  102. package/dist/variable-rule.cjs +86 -0
  103. package/dist/variable-rule.d.ts +23 -0
  104. package/dist/variable-rule.mjs +81 -0
  105. package/package.json +1 -1
  106. package/dist/utils/are-rule-at-equal.cjs +0 -22
  107. package/dist/utils/are-rule-at-equal.d.ts +0 -2
  108. package/dist/utils/are-rule-at-equal.mjs +0 -20
  109. package/dist/utils/are-rule-modes-equal.cjs +0 -7
  110. package/dist/utils/are-rule-modes-equal.d.ts +0 -2
  111. package/dist/utils/are-rule-modes-equal.mjs +0 -5
  112. package/dist/utils/are-rule-selectors-equal.cjs +0 -29
  113. package/dist/utils/are-rule-selectors-equal.d.ts +0 -2
  114. package/dist/utils/are-rule-selectors-equal.mjs +0 -27
  115. package/dist/utils/are-rule-states-equal.cjs +0 -11
  116. package/dist/utils/are-rule-states-equal.d.ts +0 -2
  117. package/dist/utils/are-rule-states-equal.mjs +0 -9
  118. package/dist/utils/are-rules-duplicated.cjs +0 -17
  119. package/dist/utils/are-rules-duplicated.d.ts +0 -2
  120. package/dist/utils/are-rules-duplicated.mjs +0 -15
  121. package/dist/utils/reorder-for-readable-classes.cjs +0 -62
  122. package/dist/utils/reorder-for-readable-classes.mjs +0 -60
@@ -3,23 +3,35 @@
3
3
  var cssEscape = require('./shared/utils/css-escape.cjs');
4
4
  var syntaxRuleType = require('./syntax-rule-type.cjs');
5
5
  var common = require('./common.cjs');
6
- var rule = require('./rule.cjs');
6
+ var parseValue = require('./utils/parse-value.cjs');
7
+ var parseAt = require('./utils/parse-at.cjs');
8
+ var generateAt = require('./utils/generate-at.cjs');
9
+ var parseSelector = require('./utils/parse-selector.cjs');
10
+ var generateSelector = require('./utils/generate-selector.cjs');
11
+ var compareRulePriority = require('./utils/compare-rule-priority.cjs');
7
12
 
8
- class SyntaxRule extends rule.Rule {
13
+ class SyntaxRule {
9
14
  name;
10
15
  css;
11
16
  registeredSyntax;
12
17
  fixedClass;
13
- at;
18
+ native;
19
+ atRules;
14
20
  priority;
15
- order;
16
21
  type;
17
22
  declarations;
18
23
  layer;
24
+ valid;
19
25
  animationNames;
20
26
  variableNames;
21
27
  constructor(name, css, registeredSyntax, fixedClass, mode){
22
- super(name), this.name = name, this.css = css, this.registeredSyntax = registeredSyntax, this.fixedClass = fixedClass, this.at = {}, this.priority = -1, this.order = 0, this.type = 0, this.resolveValue = (valueComponents, unit, bypassVariableNames, bypassParsing)=>{
28
+ this.name = name;
29
+ this.css = css;
30
+ this.registeredSyntax = registeredSyntax;
31
+ this.fixedClass = fixedClass;
32
+ this.type = syntaxRuleType.Normal;
33
+ this.valid = true;
34
+ this.resolveValue = (valueComponents, unit, bypassVariableNames, bypassParsing)=>{
23
35
  const { functions } = this.css.config;
24
36
  let currentValue = '';
25
37
  for (const eachValueComponent of valueComponents){
@@ -45,11 +57,12 @@ class SyntaxRule extends rule.Rule {
45
57
  normalHandler(themeVariable);
46
58
  }
47
59
  } else {
48
- if (!this.variableNames) {
49
- this.variableNames = [];
50
- }
51
- if (!this.variableNames.includes(eachValueComponent.name)) {
52
- this.variableNames.push(eachValueComponent.name);
60
+ if (this.variableNames) {
61
+ this.variableNames.add(eachValueComponent.name);
62
+ } else {
63
+ this.variableNames = new Set([
64
+ eachValueComponent.name
65
+ ]);
53
66
  }
54
67
  varHandler();
55
68
  }
@@ -110,7 +123,8 @@ class SyntaxRule extends rule.Rule {
110
123
  }
111
124
  }
112
125
  return currentValue;
113
- }, this.parseValues = (currentValueComponents, i, value, unit, endSymbol, parentFunctionName, bypassParsing = false, bypassVariableNames = [])=>{
126
+ };
127
+ this.parseValues = (currentValueComponents, i, value, unit, endSymbol, parentFunctionName, bypassParsing = false, bypassVariableNames = [])=>{
114
128
  const root = parentFunctionName === undefined;
115
129
  const isVarFunction = !root && (parentFunctionName.endsWith('$') || parentFunctionName.endsWith('var'));
116
130
  const checkIsString = (value)=>value === '\'' || value === '"';
@@ -164,8 +178,9 @@ class SyntaxRule extends rule.Rule {
164
178
  token: currentValue
165
179
  });
166
180
  } else {
181
+ const parsedValue = this.parseValue(currentValue, unit);
167
182
  currentValueComponents.push({
168
- ...this.parseValue(currentValue, unit),
183
+ ...parsedValue,
169
184
  token: currentValue
170
185
  });
171
186
  }
@@ -255,20 +270,17 @@ class SyntaxRule extends rule.Rule {
255
270
  const { id, definition } = registeredSyntax;
256
271
  const { analyze, transformValue, declare, transformValueComponents, create, type, unit } = definition;
257
272
  this.type = type;
258
- const { scope, important, modes } = css.config;
259
- const { at, animations } = css;
260
273
  if (create) create.call(this, name);
261
274
  // 1. value / selectorToken
262
275
  this.declarations = definition.declarations;
263
276
  let stateToken;
264
- let prefixToken;
265
277
  if (this.type === syntaxRuleType.Utility) {
266
278
  // TODO: id 使用其他方式傳遞
267
279
  stateToken = name.slice(id.length - 1);
268
280
  } else {
269
281
  let valueToken;
270
282
  if (analyze) {
271
- [valueToken, prefixToken] = analyze.call(this, name);
283
+ [valueToken] = analyze.call(this, name);
272
284
  } else {
273
285
  const indexOfColon = name.indexOf(':');
274
286
  this.keyToken = name.slice(0, indexOfColon + 1);
@@ -285,186 +297,57 @@ class SyntaxRule extends rule.Rule {
285
297
  stateToken = stateToken.slice(1);
286
298
  }
287
299
  this.stateToken = stateToken;
288
- // 3. prefix selector
289
- if (prefixToken) {
290
- this.vendorPrefixSelectors = css.generateVendorSelectors(prefixToken);
291
- }
292
300
  // 4. suffix selector
293
301
  const stateTokens = stateToken.split('@');
294
302
  const suffixSelector = stateTokens[0];
295
303
  if (suffixSelector) {
296
- this.vendorSuffixSelectors = css.generateVendorSelectors(suffixSelector);
297
- for (const suffixSelectors of Object.values(this.vendorSuffixSelectors)){
298
- for (const eachSuffixSelector of suffixSelectors){
299
- const SORTED_SELECTORS = [
300
- ':disabled',
301
- ':active',
302
- ':focus',
303
- ':hover'
304
- ];
305
- for(let i = 0; i < SORTED_SELECTORS.length; i++){
306
- if (eachSuffixSelector.includes(SORTED_SELECTORS[i])) {
307
- if (this.priority === -1 || this.priority > i) {
308
- this.priority = i;
309
- }
310
- break;
311
- }
312
- }
313
- }
314
- }
315
- }
316
- // selector combinations
317
- const suffixSelectorVendorsByPrefixSelectorVendor = {};
318
- if (this.vendorPrefixSelectors) {
319
- if (this.vendorSuffixSelectors) {
320
- for(const prefixSelectorVendor in this.vendorPrefixSelectors){
321
- const suffixSelectorVendors = suffixSelectorVendorsByPrefixSelectorVendor[prefixSelectorVendor] = [];
322
- if (Object.prototype.hasOwnProperty.call(this.vendorSuffixSelectors, prefixSelectorVendor)) {
323
- suffixSelectorVendors.push(prefixSelectorVendor);
324
- } else {
325
- for(const suffixVendor in this.vendorSuffixSelectors){
326
- suffixSelectorVendors.push(suffixVendor);
327
- }
328
- }
329
- }
330
- } else {
331
- for(const vendor in this.vendorPrefixSelectors){
332
- suffixSelectorVendorsByPrefixSelectorVendor[vendor] = [
333
- ''
334
- ];
335
- }
336
- }
337
- } else {
338
- suffixSelectorVendorsByPrefixSelectorVendor[''] = this.vendorSuffixSelectors ? Object.keys(this.vendorSuffixSelectors) : [
339
- ''
340
- ];
304
+ this.selectorNodes = parseSelector(suffixSelector, css);
341
305
  }
342
306
  // 5. atTokens
343
307
  for(let i = 1; i < stateTokens.length; i++){
344
308
  const atToken = stateTokens[i];
309
+ if (css.config.modes?.[atToken]) {
310
+ this.mode = atToken;
311
+ continue;
312
+ }
345
313
  this.atToken = (this.atToken || '') + '@' + atToken;
346
- if (atToken) {
347
- if (atToken === 'base' && !this.fixedClass) {
314
+ const atRule = parseAt(atToken, css);
315
+ const targetNodes = this.atRules?.[atRule.id];
316
+ if (targetNodes) {
317
+ targetNodes.push(...atRule.nodes);
318
+ } else {
319
+ this.atRules = {
320
+ ...this.atRules,
321
+ [atRule.id]: atRule.nodes
322
+ };
323
+ }
324
+ }
325
+ if (this.mode && css.config.modes?.[this.mode] === 'media') {
326
+ const atComp = {
327
+ name: 'prefers-color-scheme',
328
+ value: this.mode
329
+ };
330
+ if (this.atRules?.media) {
331
+ this.atRules.media.push(atComp);
332
+ } else {
333
+ this.atRules = {
334
+ media: [
335
+ atComp
336
+ ]
337
+ };
338
+ }
339
+ }
340
+ if (this.fixedClass) {
341
+ this.layer = css.componentsLayer;
342
+ } else {
343
+ const onlyNode = this.atRules?.layer?.length === 1 && this.atRules.layer[0];
344
+ if (onlyNode) {
345
+ if (onlyNode.value === 'base') {
348
346
  this.layer = css.baseLayer;
349
- continue;
350
- } else if (atToken === 'preset' && !this.fixedClass) {
347
+ this.atRules.layer = undefined;
348
+ } else if (onlyNode.value === 'preset') {
351
349
  this.layer = css.presetLayer;
352
- continue;
353
- } else if (atToken === 'rtl' || atToken === 'ltr') {
354
- this.direction = atToken;
355
- } else {
356
- let queryType;
357
- const atComponents = [];
358
- // x font-face, counter-style, keyframes, font-feature-values, property, layer
359
- const queryTypeRegExp = /^(media|supports|container|layer)/;
360
- const atRuleResult = queryTypeRegExp.exec(atToken);
361
- if (atRuleResult) {
362
- queryType = atRuleResult[1];
363
- atComponents.push({
364
- type: 'arbitrary',
365
- token: atToken,
366
- value: atToken.slice(queryType.length).replace(/\|/g, ' ')
367
- });
368
- } else {
369
- const analyzeToken = (atComponentToken)=>{
370
- if (atComponentToken === '&') {
371
- atComponents.push({
372
- type: 'operator',
373
- token: atComponentToken,
374
- value: 'and'
375
- });
376
- } else if (atComponentToken.startsWith('')) {
377
- const targetAt = at.get(atComponentToken);
378
- if (typeof targetAt === 'string') {
379
- const match = targetAt.match(queryTypeRegExp);
380
- queryType = match ? match[1] : '';
381
- if (!queryType) throw new Error(`Invalid query '${atComponentToken}': '${targetAt}'`);
382
- atComponents.push({
383
- type: 'arbitrary',
384
- value: targetAt.slice(match ? match[1].length + 1 : 0)
385
- });
386
- } else {
387
- // todo: container queries
388
- queryType = 'media';
389
- let featureName = '';
390
- let extremumOperator = '';
391
- let correction = 0;
392
- if (atComponentToken.startsWith('<=')) {
393
- extremumOperator = '<=';
394
- featureName = 'max-width';
395
- } else if (atComponentToken.startsWith('>') && !atComponentToken.startsWith('>=')) {
396
- extremumOperator = '>';
397
- featureName = 'min-width';
398
- correction = .02;
399
- } else if (atComponentToken.startsWith('<')) {
400
- extremumOperator = '<';
401
- featureName = 'max-width';
402
- correction = -0.02;
403
- } else if (atComponentToken.startsWith('>=') || targetAt) {
404
- extremumOperator = '>=';
405
- featureName = 'min-width';
406
- }
407
- const token = extremumOperator ? atComponentToken.replace(extremumOperator, '') : atComponentToken;
408
- const viewport = at.get(token);
409
- switch(featureName){
410
- case 'max-width':
411
- case 'min-width':
412
- if (typeof viewport === 'number') {
413
- atComponents.push({
414
- type: 'feature',
415
- name: featureName,
416
- valueType: 'number',
417
- value: viewport + correction,
418
- unit: 'px'
419
- });
420
- } else {
421
- const valueComponent = this.parseValue(token, 'px');
422
- if (valueComponent.type === 'number') {
423
- atComponents.push({
424
- type: 'feature',
425
- name: featureName,
426
- valueType: 'number',
427
- value: valueComponent.value + correction,
428
- unit: valueComponent.unit
429
- });
430
- } else {
431
- atComponents.push({
432
- type: 'feature',
433
- name: featureName,
434
- valueType: 'string',
435
- value: token
436
- });
437
- }
438
- }
439
- break;
440
- }
441
- }
442
- }
443
- };
444
- const isAmpersandIncluded = atToken.includes('&');
445
- if (isAmpersandIncluded) {
446
- const typeOrFeatureTokens = atToken.split(/(&|,)/);
447
- for (const atComponentToken of typeOrFeatureTokens){
448
- analyzeToken(atComponentToken);
449
- }
450
- } else {
451
- analyzeToken(atToken);
452
- }
453
- if (!atComponents.length) {
454
- if (!isAmpersandIncluded && modes?.[atToken]) {
455
- this.mode = atToken;
456
- continue;
457
- }
458
- }
459
- }
460
- if (queryType) {
461
- const eachAtComponents = this.at[queryType];
462
- if (eachAtComponents) {
463
- eachAtComponents.push(...atComponents);
464
- } else {
465
- this.at[queryType] = atComponents;
466
- }
467
- }
350
+ this.atRules.layer = undefined;
468
351
  }
469
352
  }
470
353
  }
@@ -486,155 +369,82 @@ class SyntaxRule extends rule.Rule {
486
369
  };
487
370
  }
488
371
  }
489
- const propertiesText = [];
490
- for(const propertyName in this.declarations){
491
- const push = (propertyText)=>{
492
- // animations
493
- if (animations && (propertyText.startsWith('animation') || propertyText.startsWith('animation-name'))) {
494
- const animationNames = propertyText.split(':')[1].split('!important')[0].split(' ').filter((eachValue)=>this.css.animations.has(eachValue) && (!this.animationNames || !this.animationNames.includes(eachValue)));
495
- if (animationNames.length) {
496
- if (!this.animationNames) {
497
- this.animationNames = [];
498
- }
499
- this.animationNames = animationNames;
500
- }
501
- }
502
- propertiesText.push(propertyText + ((this.important || important) && !propertyText.endsWith('!important') ? '!important' : ''));
503
- };
504
- const keyToken = propertyName + ':';
505
- const declarations = this.declarations[propertyName];
506
- if (Array.isArray(declarations)) {
507
- for (const value of declarations){
508
- push(keyToken + String(value));
509
- }
510
- } else {
511
- push(keyToken + String(declarations));
512
- }
513
- }
514
- // 創建 Natives
515
- if (propertiesText.length) {
516
- for(const prefixSelectorVendor in suffixSelectorVendorsByPrefixSelectorVendor){
517
- for (const eachSuffixSelectorVendor of suffixSelectorVendorsByPrefixSelectorVendor[prefixSelectorVendor]){
518
- const prefixSelectors = this.vendorPrefixSelectors?.[prefixSelectorVendor];
519
- const suffixSelectors = this.vendorSuffixSelectors?.[eachSuffixSelectorVendor];
520
- if (this.css.supportVendors) {
521
- if (prefixSelectorVendor && !this.css.supportVendors?.has(prefixSelectorVendor) || eachSuffixSelectorVendor && !this.css.supportVendors?.has(eachSuffixSelectorVendor)) {
372
+ if (!Object.entries(this.declarations ?? {}).length) {
373
+ this.valid = false;
374
+ } else {
375
+ for(const propertyName in this.declarations){
376
+ if (this.css.animations && (propertyName === 'animation' || propertyName === 'animation-name')) {
377
+ const propertyValue = this.declarations[propertyName];
378
+ if (!propertyValue) continue;
379
+ const rawValues = propertyValue.split(' ');
380
+ for (const rawValue of rawValues){
381
+ if (this.css.animations.has(rawValue)) {
382
+ if (!this.animationNames) {
383
+ this.animationNames = new Set([
384
+ rawValue
385
+ ]);
386
+ } else {
387
+ this.animationNames.add(rawValue);
388
+ }
522
389
  continue;
523
390
  }
524
391
  }
525
- let prefixText = '';
526
- if (this.direction) {
527
- prefixText += '[dir=' + this.direction + '] ';
528
- }
529
- const prefixTexts = prefixSelectors ? prefixSelectors.map((eachPrefixSelector)=>eachPrefixSelector + prefixText) : [
530
- prefixText
531
- ];
532
- const getCssText = (name)=>prefixTexts.map((eachPrefixText)=>{
533
- let modePrefix = '';
534
- if (this.mode) {
535
- const modeSelector = this.css.getModeSelector(this.mode);
536
- if (modeSelector) {
537
- modePrefix = modeSelector + ' ';
538
- }
539
- }
540
- return modePrefix + (scope ? scope + ' ' : '') + eachPrefixText;
541
- }).reduce((arr, eachPrefixText)=>{
542
- arr.push(suffixSelectors ? suffixSelectors.reduce((_arr, eachSuffixSelector)=>{
543
- _arr.push(eachPrefixText + '.' + cssEscape(name) + eachSuffixSelector);
544
- return _arr;
545
- }, []).join(',') : eachPrefixText + '.' + cssEscape(name));
546
- return arr;
547
- }, []).join(',');
548
- const selectorText = getCssText(fixedClass ?? name);
549
- let cssText = selectorText + '{' + propertiesText.join(';') + '}';
550
- for (const key of Object.keys(this.at).sort((_a, b)=>b === 'supports' ? -1 : 1)){
551
- cssText = '@' + key + (key.includes(' ') ? '' : ' ') + this.at[key].map((eachAtComponent)=>this.resolveAtComponent(eachAtComponent)).join(' ') + '{' + cssText + '}';
552
- }
553
- if (this.mode && modes?.[this.mode] === 'media') {
554
- cssText = `@media(prefers-color-scheme:${this.mode}){` + cssText + '}';
555
- }
556
- const node = {
557
- text: cssText,
558
- selectorText
559
- };
560
- if (prefixSelectors) node.prefixSelectors = prefixSelectors;
561
- if (suffixSelectors) node.suffixSelectors = suffixSelectors;
562
- this.nodes.push(node);
563
392
  }
564
393
  }
394
+ this.priority = compareRulePriority.calcRulePriority(this);
565
395
  }
566
- if (this.fixedClass) {
567
- this.layer = css.componentsLayer;
396
+ }
397
+ get text() {
398
+ if (!this.valid) return '';
399
+ const propertiesText = [];
400
+ for(const propertyName in this.declarations){
401
+ const propertyValue = this.declarations[propertyName];
402
+ const propertyText = propertyName + ':' + String(propertyValue);
403
+ propertiesText.push(propertyText + ((this.important || this.css.config.important) && !propertyText.endsWith('!important') ? '!important' : ''));
568
404
  }
405
+ let text = this.selectorText + '{' + propertiesText.join(';') + '}';
406
+ if (this.atRules !== undefined) common.AT_IDENTIFIERS.forEach((id)=>{
407
+ const nodes = this.atRules?.[id];
408
+ if (!nodes) return;
409
+ text = generateAt({
410
+ id,
411
+ nodes
412
+ }) + '{' + text + '}';
413
+ });
414
+ return text;
569
415
  }
570
- resolveAtComponent(atComponent) {
571
- switch(atComponent.type){
572
- case 'arbitrary':
573
- return atComponent.value;
574
- case 'feature':
575
- return '(' + atComponent.name + ':' + atComponent.value + (atComponent.unit || '') + ')';
576
- case 'operator':
577
- return atComponent.value;
416
+ get selectorText() {
417
+ let pre = '';
418
+ if (this.css.config.scope) {
419
+ pre = this.css.config.scope + ' ' + pre;
578
420
  }
421
+ if (this.mode) {
422
+ const modeSelector = this.css.getModeSelector(this.mode);
423
+ if (modeSelector) {
424
+ pre = modeSelector + ' ' + pre;
425
+ }
426
+ }
427
+ const body = pre + '.' + cssEscape(this.fixedClass ?? this.name);
428
+ return this.selectorNodes ? generateSelector(this.selectorNodes, body) : body;
579
429
  }
580
430
  resolveValue;
581
431
  parseValues;
582
432
  parseValue(token, unit = this.definition.unit) {
583
- const defaultUnit = unit ?? this.definition.unit;
584
- let newUnit = '';
585
- let value;
586
- if (typeof token === 'number') {
587
- /**
588
- * 當無單位值且 defaultUnit === 'rem',
589
- * 將 pxValue / 16 轉為 remValue
590
- */ if (defaultUnit && !newUnit) {
591
- if (defaultUnit === 'rem' || defaultUnit === 'em') {
592
- value = token / (this.css.config.rootSize || 1);
593
- }
594
- newUnit = defaultUnit || '';
595
- } else {
596
- value = token;
597
- }
598
- return {
599
- value,
600
- unit: newUnit,
601
- type: 'number'
602
- };
603
- } else if (defaultUnit) {
604
- // w:1/2 -> width: 50%
605
- if (/^\d+\/\d+/.test(token)) {
606
- const [dividend, divisor] = token.split('/');
433
+ const parsed = parseValue(token, unit, this.css.config.rootSize);
434
+ // exclude like `aspect:1/2` from being parsed as 50%
435
+ if (this.definition.unit && parsed.type === 'string') {
436
+ // 1/2 → 50%
437
+ if (/^\d+\/\d+$/.test(parsed.value)) {
438
+ const [numerator, denominator] = parsed.value.split('/').map(Number);
607
439
  return {
608
- value: +dividend / +divisor * 100,
440
+ token,
441
+ value: numerator / denominator * 100,
609
442
  unit: '%',
610
443
  type: 'number'
611
444
  };
612
445
  }
613
- const matches = token.match(common.UNIT_REGEX);
614
- // ['0.5deg', '0.5', 'deg', index: 0, input: '0.5deg', groups: undefined]
615
- if (matches) {
616
- value = +matches[1];
617
- newUnit = matches[3] || '';
618
- /**
619
- * 當無單位值且 defaultUnit === 'rem',
620
- * 將 pxValue / 16 轉為 remValue
621
- */ if (!newUnit) {
622
- if (defaultUnit === 'rem' || defaultUnit === 'em') {
623
- value = value / (this.css.config.rootSize || 1);
624
- }
625
- newUnit = defaultUnit || '';
626
- }
627
- return {
628
- value,
629
- unit: newUnit,
630
- type: 'number'
631
- };
632
- }
633
446
  }
634
- return {
635
- value: token,
636
- type: 'string'
637
- };
447
+ return parsed;
638
448
  }
639
449
  get key() {
640
450
  return (this.fixedClass ? this.fixedClass + ' ' : '') + this.name;
@@ -1,40 +1,37 @@
1
1
  import MasterCSS from './core';
2
- import SyntaxRuleType from './syntax-rule-type';
3
2
  import { type PropertiesHyphen } from 'csstype';
4
- import { Rule } from './rule';
5
3
  import Layer from './layer';
6
- import type { AtComponent, DefinedRule, ValueComponent } from './types/syntax';
7
- export declare class SyntaxRule extends Rule {
4
+ import type { NumberValueComponent, DefinedRule, ValueComponent } from './types/syntax';
5
+ import { AtRuleNode } from './utils/parse-at';
6
+ import { AtIdentifier } from './types/config';
7
+ import { SelectorNode } from './utils/parse-selector';
8
+ import { RulePriority } from './utils/compare-rule-priority';
9
+ import { SyntaxRuleTypeValue } from './types/common';
10
+ export declare class SyntaxRule {
8
11
  readonly name: string;
9
12
  css: MasterCSS;
10
13
  readonly registeredSyntax: DefinedRule;
11
14
  fixedClass?: string | undefined;
12
- readonly at: Record<string, AtComponent[]>;
13
- readonly priority: number;
14
- readonly order: number;
15
- readonly type: SyntaxRuleType;
15
+ native?: CSSRule;
16
+ readonly atRules?: Partial<Record<AtIdentifier, AtRuleNode[]>>;
17
+ readonly priority: RulePriority;
18
+ readonly type: SyntaxRuleTypeValue;
16
19
  readonly declarations?: PropertiesHyphen;
17
20
  readonly layer: Layer;
18
- animationNames?: string[];
19
- variableNames?: string[];
21
+ readonly valid: boolean;
22
+ animationNames?: Set<string>;
23
+ variableNames?: Set<string>;
20
24
  constructor(name: string, css: MasterCSS, registeredSyntax: DefinedRule, fixedClass?: string | undefined, mode?: string);
21
- resolveAtComponent(atComponent: AtComponent): string;
25
+ get text(): string;
26
+ get selectorText(): string;
22
27
  resolveValue: (valueComponents: ValueComponent[], unit: string, bypassVariableNames: string[], bypassParsing: boolean) => string;
23
28
  parseValues: (currentValueComponents: ValueComponent[], i: number, value: string, unit: string, endSymbol: string, parentFunctionName?: string, bypassParsing?: boolean, bypassVariableNames?: string[]) => number;
24
- parseValue(token: string | number, unit?: any): {
25
- value: string;
26
- type: 'string';
27
- } | {
28
- value: number;
29
- unit: string;
30
- type: 'number';
31
- };
29
+ parseValue(token: string | number, unit?: any): import(".").StringValueComponent | NumberValueComponent;
32
30
  get key(): string;
33
31
  }
34
32
  export interface SyntaxRule extends DefinedRule {
35
33
  token: string;
36
- vendorPrefixSelectors: Record<string, string[]>;
37
- vendorSuffixSelectors: Record<string, string[]>;
34
+ selectorNodes?: SelectorNode[];
38
35
  important: boolean;
39
36
  direction: string;
40
37
  mode: string;