@tbela99/css-parser 2.1.0 → 2.3.0

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 (130) hide show
  1. package/CHANGELOG.md +66 -1
  2. package/README.md +14 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/lib/ast → ast}/arena-node.js +10 -10
  5. package/dist/{src/lib/ast → ast}/arena.js +304 -139
  6. package/dist/{src/lib/ast → ast}/cache.js +40 -24
  7. package/dist/{src/lib/ast → ast}/features/calc.js +11 -9
  8. package/dist/{src/lib/ast → ast}/features/if.js +6 -6
  9. package/dist/ast/features/index.d.ts +7 -0
  10. package/dist/{src/lib/ast → ast}/features/index.js +3 -2
  11. package/dist/{src/lib/ast → ast}/features/inlinecssvariables.js +12 -10
  12. package/dist/ast/features/mixin.js +953 -0
  13. package/dist/{src/lib/ast → ast}/features/prefix.js +39 -39
  14. package/dist/ast/features/shorthand.js +129 -0
  15. package/dist/ast/features/transform.js +188 -0
  16. package/dist/{src/lib/ast → ast}/features/type.js +2 -2
  17. package/dist/{src/lib/ast → ast}/math/expression.js +24 -24
  18. package/dist/{src/lib/ast → ast}/math/math.js +2 -2
  19. package/dist/{src/lib/ast → ast}/minify.js +745 -201
  20. package/dist/ast/mixins/custom-function.js +393 -0
  21. package/dist/ast/mixins/utils.js +53 -0
  22. package/dist/ast/mixins/variables.js +678 -0
  23. package/dist/ast/strings.js +910 -0
  24. package/dist/ast/transform/compute.js +560 -0
  25. package/dist/ast/transform/matrix.js +67 -0
  26. package/dist/ast/transform/minify.js +325 -0
  27. package/dist/ast/transform/perspective.js +24 -0
  28. package/dist/{src/lib/ast → ast}/transform/rotate.js +16 -7
  29. package/dist/ast/transform/scale.js +43 -0
  30. package/dist/ast/transform/skew.js +38 -0
  31. package/dist/ast/transform/translate.js +56 -0
  32. package/dist/ast/transform/utils.js +390 -0
  33. package/dist/{src/lib/ast → ast}/types.js +2 -2
  34. package/dist/{src/lib/ast → ast}/walk.js +2 -2
  35. package/dist/{src/data → data}/properties.json.js +86 -2
  36. package/dist/{src/data → data}/validation.json.js +30 -21
  37. package/dist/{src/lib/fs → fs}/resolve.js +2 -2
  38. package/dist/index-umd-web.js +19727 -10719
  39. package/dist/index.d.ts +738 -100
  40. package/dist/{src/node.js → node.js} +70 -52
  41. package/dist/{src/lib/parser → parser}/declaration/list.js +25 -8
  42. package/dist/{src/lib/parser → parser}/declaration/map.js +15 -15
  43. package/dist/{src/lib/parser → parser}/declaration/set.js +5 -5
  44. package/dist/{src/lib/parser → parser}/input.js +18 -3
  45. package/dist/{src/lib/parser → parser}/linesmap.js +2 -2
  46. package/dist/{src/lib/parser → parser}/parse.js +444 -278
  47. package/dist/{src/lib/parser → parser}/source.js +3 -3
  48. package/dist/{src/lib/parser → parser}/tokenize.js +480 -312
  49. package/dist/{src/lib/parser → parser}/utils/angle.js +3 -3
  50. package/dist/{src/lib/parser → parser}/utils/at-rule-container.js +12 -12
  51. package/dist/{src/lib/parser → parser}/utils/at-rule-font-feature-values.js +3 -3
  52. package/dist/{src/lib/parser → parser}/utils/at-rule-generic.js +7 -7
  53. package/dist/{src/lib/parser → parser}/utils/at-rule-import.js +10 -10
  54. package/dist/{src/lib/parser → parser}/utils/at-rule-media.js +14 -14
  55. package/dist/{src/lib/parser → parser}/utils/at-rule-support.js +9 -14
  56. package/dist/{src/lib/parser → parser}/utils/at-rule-when-else.js +6 -6
  57. package/dist/{src/lib/parser → parser}/utils/at-rule.js +3 -3
  58. package/dist/{src/lib/parser → parser}/utils/base64.js +2 -2
  59. package/dist/parser/utils/cache.js +53 -0
  60. package/dist/parser/utils/canonical.js +78 -0
  61. package/dist/parser/utils/codes.js +93 -0
  62. package/dist/{src/lib/parser → parser}/utils/config.js +3 -3
  63. package/dist/{src/lib/parser → parser}/utils/declaration.js +31 -26
  64. package/dist/{src/lib/parser → parser}/utils/eq.js +2 -2
  65. package/dist/{src/lib/parser → parser}/utils/hash.js +2 -2
  66. package/dist/{src/lib/parser → parser}/utils/selector.js +339 -87
  67. package/dist/{src/lib/parser → parser}/utils/text.js +2 -2
  68. package/dist/{src/lib/parser → parser}/utils/token.js +2 -2
  69. package/dist/{src/lib/parser → parser}/utils/type.js +9 -7
  70. package/dist/{src/lib/printer/sourcemap → printer}/lib/codec.js +2 -2
  71. package/dist/{src/lib/printer → printer}/render.js +582 -153
  72. package/dist/printer/result.js +148 -0
  73. package/dist/{src/lib/printer/sourcemap → printer}/sourcemap.js +4 -3
  74. package/dist/printer/writer.js +252 -0
  75. package/dist/{src/lib/syntax → syntax}/color/a98rgb.js +2 -2
  76. package/dist/{src/lib/syntax → syntax}/color/alpha.js +10 -10
  77. package/dist/{src/lib/syntax → syntax}/color/cmyk.js +2 -2
  78. package/dist/{src/lib/syntax → syntax}/color/color-mix.js +18 -18
  79. package/dist/{src/lib/syntax → syntax}/color/color.js +18 -18
  80. package/dist/{src/lib/syntax → syntax}/color/hex.js +3 -3
  81. package/dist/{src/lib/syntax → syntax}/color/hsl.js +2 -2
  82. package/dist/{src/lib/syntax → syntax}/color/hwb.js +2 -2
  83. package/dist/{src/lib/syntax → syntax}/color/lab.js +2 -2
  84. package/dist/{src/lib/syntax → syntax}/color/lch.js +2 -2
  85. package/dist/{src/lib/syntax → syntax}/color/oklab.js +2 -2
  86. package/dist/{src/lib/syntax → syntax}/color/oklch.js +3 -3
  87. package/dist/{src/lib/syntax → syntax}/color/p3.js +2 -2
  88. package/dist/{src/lib/syntax → syntax}/color/prophotorgb.js +2 -2
  89. package/dist/{src/lib/syntax → syntax}/color/rec2020.js +2 -2
  90. package/dist/{src/lib/syntax → syntax}/color/relative-color.js +14 -14
  91. package/dist/{src/lib/syntax → syntax}/color/rgb.js +4 -4
  92. package/dist/{src/lib/syntax → syntax}/color/srgb.js +5 -5
  93. package/dist/{src/lib/syntax → syntax}/color/utils/color-type.js +4 -4
  94. package/dist/{src/lib/syntax → syntax}/color/utils/colorspace.js +10 -10
  95. package/dist/{src/lib/syntax → syntax}/color/utils/components.js +5 -5
  96. package/dist/{src/lib/syntax → syntax}/color/utils/distance.js +2 -2
  97. package/dist/{src/lib/syntax → syntax}/color/utils/make-color.js +3 -3
  98. package/dist/{src/lib/syntax → syntax}/color/utils/matrix.js +2 -2
  99. package/dist/{src/lib/syntax → syntax}/color/xyz.js +2 -2
  100. package/dist/{src/lib/syntax → syntax}/color/xyzd50.js +2 -2
  101. package/dist/{src/lib/syntax → syntax}/constants.js +27 -4
  102. package/dist/{src/lib/syntax → syntax}/syntax.js +37 -29
  103. package/dist/{src/types.js → types.js} +2 -2
  104. package/dist/{src/utils → utils}/sync.js +12 -7
  105. package/dist/validation/compiled/builtins.js +32 -0
  106. package/dist/validation/compiled/runtime.js +705 -0
  107. package/dist/validation/compiled/syntaxes.js +1620 -0
  108. package/dist/{src/lib/validation → validation}/config.js +63 -16
  109. package/dist/validation/first.js +574 -0
  110. package/dist/{src/lib/validation → validation}/match.js +376 -161
  111. package/dist/{src/lib/validation → validation}/parser/parse.js +2 -2
  112. package/dist/{src/lib/validation → validation}/parser/typedef.js +2 -2
  113. package/dist/{src/lib/validation → validation}/utils/list.js +3 -19
  114. package/dist/version.js +13 -0
  115. package/dist/{src/web.js → web.js} +68 -55
  116. package/package.json +15 -13
  117. package/dist/package.json.js +0 -10
  118. package/dist/src/lib/ast/features/shorthand.js +0 -95
  119. package/dist/src/lib/ast/features/transform.js +0 -90
  120. package/dist/src/lib/ast/transform/compute.js +0 -362
  121. package/dist/src/lib/ast/transform/matrix.js +0 -88
  122. package/dist/src/lib/ast/transform/minify.js +0 -267
  123. package/dist/src/lib/ast/transform/perspective.js +0 -16
  124. package/dist/src/lib/ast/transform/scale.js +0 -39
  125. package/dist/src/lib/ast/transform/skew.js +0 -30
  126. package/dist/src/lib/ast/transform/translate.js +0 -39
  127. package/dist/src/lib/ast/transform/utils.js +0 -287
  128. package/dist/src/lib/parser/utils/cache.js +0 -54
  129. package/dist/types.d.ts +0 -21
  130. package/dist/utils/sync.d.ts +0 -16
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.3.0
6
6
  License: MIT
7
7
  */
8
8
  /**
@@ -12,7 +12,7 @@
12
12
  * @returns
13
13
  */
14
14
  function toDegrees(arena, angle) {
15
- switch (arena.strings[arena.unit[angle]]) {
15
+ switch (arena.resolveString(arena.unit[angle])) {
16
16
  // case "deg":
17
17
  // return angle;
18
18
  case "rad":
@@ -1,13 +1,13 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.3.0
6
6
  License: MIT
7
7
  */
8
8
  import { TokenField } from '../../ast/arena.js';
9
9
  import { EnumToken } from '../../ast/types.js';
10
- import { tokensfuncDefMap, mFGT, mFLT } from '../../syntax/constants.js';
10
+ import { isFunctionDefType, mFGT, mFLT, tokensfuncDefMap } from '../../syntax/constants.js';
11
11
  import { matchAllSyntaxes, createValidationContext, trimArray } from '../../validation/match.js';
12
12
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
13
13
  import { getSyntaxRule } from '../../validation/config.js';
@@ -16,7 +16,7 @@ import { equalsIgnoreCase } from './text.js';
16
16
  function parseAtRuleContainerQueryList(arena, stream, context, options = {}) {
17
17
  let matchCount = 0;
18
18
  const errors = [];
19
- const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.strings[arena.name[context]]);
19
+ const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.resolveString(arena.name[context]));
20
20
  const syntax = syntaxRules?.getPreludeRules()?.slice?.(1);
21
21
  const parts = [[]];
22
22
  for (const token of stream) {
@@ -26,7 +26,7 @@ function parseAtRuleContainerQueryList(arena, stream, context, options = {}) {
26
26
  continue;
27
27
  }
28
28
  parts[parts.length - 1].push(token);
29
- if (type === EnumToken.StartParensTokenType || tokensfuncDefMap.has(type)) {
29
+ if (type === EnumToken.StartParensTokenType || isFunctionDefType(type)) {
30
30
  matchCount++;
31
31
  }
32
32
  else if (type === EnumToken.EndParensTokenType && matchCount > 0) {
@@ -93,7 +93,7 @@ function parseAtRuleContainerQueryList(arena, stream, context, options = {}) {
93
93
  if (expectAndOr) {
94
94
  let valid = true;
95
95
  // if (arena.dataTypes[stream[i]] === EnumToken.IdenTokenType) {
96
- // const val: string = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
96
+ // const val: string = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
97
97
  // valid = val === "and" || val === "or";
98
98
  // } else {
99
99
  valid = arena.dataTypes[stream[i]] !== EnumToken.CommaTokenType;
@@ -114,7 +114,7 @@ function parseAtRuleContainerQueryList(arena, stream, context, options = {}) {
114
114
  }
115
115
  }
116
116
  if (arena.dataTypes[stream[i]] === EnumToken.StartParensTokenType ||
117
- tokensfuncDefMap.has(arena.dataTypes[stream[i]])) {
117
+ isFunctionDefType(arena.dataTypes[stream[i]])) {
118
118
  scopes.push((currentScope = new Set()));
119
119
  stack.push(stream[i]);
120
120
  continue;
@@ -130,7 +130,7 @@ function parseAtRuleContainerQueryList(arena, stream, context, options = {}) {
130
130
  break;
131
131
  case EnumToken.IdenTokenType:
132
132
  {
133
- const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
133
+ const val = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
134
134
  if (val === "not") {
135
135
  arena.dataTypes[stream[i]] = EnumToken.NotTokenType;
136
136
  stack.push(stream[i]);
@@ -182,7 +182,7 @@ function parseAtRuleContainerQueryList(arena, stream, context, options = {}) {
182
182
  if (arena.dataTypes[op] === EnumToken.ColonTokenType) {
183
183
  const name = names.find((t) => arena.dataTypes[t] === EnumToken.IdenTokenType);
184
184
  if (name != null) {
185
- const val = arena.strings[arena.stringValue[name]];
185
+ const val = arena.resolveString(arena.stringValue[name]);
186
186
  if (val.startsWith("min-")) {
187
187
  // inlined: arena.setStr(name, val.substring(4))
188
188
  arena.stringValue[name] = arena.internString(val.substring(4));
@@ -197,7 +197,7 @@ function parseAtRuleContainerQueryList(arena, stream, context, options = {}) {
197
197
  }
198
198
  // check <style()> or <scroll-state()>
199
199
  }
200
- if (stack.length > 0 && tokensfuncDefMap.has(arena.dataTypes[stack[stack.length - 1]])) {
200
+ if (stack.length > 0 && isFunctionDefType(arena.dataTypes[stack[stack.length - 1]])) {
201
201
  const index = tokens.indexOf(stack[stack.length - 1]);
202
202
  arena.dataTypes[tokens[index]] = tokensfuncDefMap.get(arena.dataTypes[tokens[index]]);
203
203
  arena.setTokenList(tokens[index], trimArray(arena, tokens.slice(index + 1, tokens.length - 1)));
@@ -215,7 +215,7 @@ function parseAtRuleContainerQueryList(arena, stream, context, options = {}) {
215
215
  options.source.file,
216
216
  ...options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i])),
217
217
  ],
218
- message: `expecting '<${arena.strings[arena.stringValue[tokens[index]]]}-query>'`,
218
+ message: `expecting '<${arena.resolveString(arena.stringValue[tokens[index]])}-query>'`,
219
219
  });
220
220
  break;
221
221
  }
@@ -330,7 +330,7 @@ function parseAtRuleContainerQueryList(arena, stream, context, options = {}) {
330
330
  .find((t) => arena.dataTypes[t] === EnumToken.IdenTokenType);
331
331
  if (name != null &&
332
332
  name2 != null &&
333
- equalsIgnoreCase(arena.strings[arena.stringValue[name]], arena.strings[arena.stringValue[name2]])) {
333
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[name]), arena.resolveString(arena.stringValue[name2]))) {
334
334
  switch (arena.dataTypes[leftOp]) {
335
335
  case EnumToken.LtTokenType:
336
336
  arena.dataTypes[leftOp] = EnumToken.GtTokenType;
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.3.0
6
6
  License: MIT
7
7
  */
8
8
  import { getSyntaxRule } from '../../validation/config.js';
@@ -10,7 +10,7 @@ import { trimArray, matchAllSyntaxes, createValidationContext } from '../../vali
10
10
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
11
11
 
12
12
  function parseAtRuleFontFeatureValues(arena, stream, context, options = {}) {
13
- const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.strings[arena.name[context]]);
13
+ const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.resolveString(arena.name[context]));
14
14
  const syntax = syntaxRules?.getPreludeRules()?.slice?.(1);
15
15
  trimArray(arena, stream);
16
16
  const { success, errors /* , ...all */ } = matchAllSyntaxes(arena, syntax, createValidationContext(arena, stream), options);
@@ -1,12 +1,12 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.3.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
9
- import { tokensfuncDefMap } from '../../syntax/constants.js';
9
+ import { isFunctionDefType } from '../../syntax/constants.js';
10
10
  import { equalsIgnoreCase } from './text.js';
11
11
 
12
12
  function matchGenericSyntax(arena, stream, options) {
@@ -23,7 +23,7 @@ function matchGenericSyntax(arena, stream, options) {
23
23
  arena.dataTypes[token] === EnumToken.CommentTokenType) {
24
24
  continue;
25
25
  }
26
- if (arena.dataTypes[token] === EnumToken.StartParensTokenType || tokensfuncDefMap.has(arena.dataTypes[token])) {
26
+ if (arena.dataTypes[token] === EnumToken.StartParensTokenType || isFunctionDefType(arena.dataTypes[token])) {
27
27
  stack.push(token);
28
28
  scopes.push(new Set());
29
29
  expectAndOr = false;
@@ -32,7 +32,7 @@ function matchGenericSyntax(arena, stream, options) {
32
32
  if (arena.dataTypes[token] === EnumToken.EndParensTokenType) {
33
33
  if (stack.length === 0 ||
34
34
  (arena.dataTypes[stack[stack.length - 1]] !== EnumToken.StartParensTokenType &&
35
- !tokensfuncDefMap.has(arena.dataTypes[stack[stack.length - 1]]))) {
35
+ !isFunctionDefType(arena.dataTypes[stack[stack.length - 1]]))) {
36
36
  errors.push({
37
37
  action: "drop",
38
38
  message: `unexpected token ${EnumToken[arena.dataTypes[token]]}`,
@@ -51,7 +51,7 @@ function matchGenericSyntax(arena, stream, options) {
51
51
  continue;
52
52
  }
53
53
  if (arena.dataTypes[token] === EnumToken.IdenTokenType &&
54
- equalsIgnoreCase("and", arena.strings[arena.stringValue[token]])) {
54
+ equalsIgnoreCase("and", arena.resolveString(arena.stringValue[token]))) {
55
55
  if (!expectAndOr || scopes.at(-1)?.has(EnumToken.OrTokenType)) {
56
56
  errors.push({
57
57
  action: "drop",
@@ -72,7 +72,7 @@ function matchGenericSyntax(arena, stream, options) {
72
72
  continue;
73
73
  }
74
74
  if (arena.dataTypes[token] === EnumToken.IdenTokenType &&
75
- equalsIgnoreCase("or", arena.strings[arena.stringValue[token]])) {
75
+ equalsIgnoreCase("or", arena.resolveString(arena.stringValue[token]))) {
76
76
  if (!expectAndOr || scopes.at(-1)?.has(EnumToken.AndTokenType)) {
77
77
  errors.push({
78
78
  action: "drop",
@@ -1,15 +1,15 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.3.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
9
9
  import { getSyntaxRule } from '../../validation/config.js';
10
10
  import { trimArray } from '../../validation/match.js';
11
11
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
12
- import { tokensfuncDefMap } from '../../syntax/constants.js';
12
+ import { tokensfuncDefMap, isFunctionDefType } from '../../syntax/constants.js';
13
13
  import { parseMediaqueryList } from './at-rule-media.js';
14
14
  import { parseAtRuleSupportSyntax } from './at-rule-support.js';
15
15
 
@@ -38,7 +38,7 @@ function matchAtRuleImportSyntax(arena, atRule, stream, options) {
38
38
  matchCount--;
39
39
  }
40
40
  else if (arena.dataTypes[stream[k]] === EnumToken.StartParensTokenType ||
41
- tokensfuncDefMap.has(arena.dataTypes[stream[k]])) {
41
+ isFunctionDefType(arena.dataTypes[stream[k]])) {
42
42
  matchCount++;
43
43
  }
44
44
  if (matchCount === 0) {
@@ -77,11 +77,11 @@ function matchAtRuleImportSyntax(arena, atRule, stream, options) {
77
77
  }
78
78
  // layer | layer(<layer-name>)
79
79
  if (arena.dataTypes[stream[index]] === EnumToken.IdenTokenType &&
80
- "layer".localeCompare(arena.strings[arena.stringValue[stream[index]]]) === 0) {
80
+ "layer".localeCompare(arena.resolveString(arena.stringValue[stream[index]])) === 0) {
81
81
  tokens.push(stream[index++]);
82
82
  }
83
- else if (tokensfuncDefMap.has(arena.dataTypes[stream[index]]) &&
84
- "layer".localeCompare(arena.strings[arena.stringValue[stream[index]]]) === 0) {
83
+ else if (isFunctionDefType(arena.dataTypes[stream[index]]) &&
84
+ "layer".localeCompare(arena.resolveString(arena.stringValue[stream[index]])) === 0) {
85
85
  stack.push(stream[index]);
86
86
  tokens.push(stream[index++]);
87
87
  // <layer-name">
@@ -140,8 +140,8 @@ function matchAtRuleImportSyntax(arena, atRule, stream, options) {
140
140
  }
141
141
  // supports([ <supports-condition> | <declaration> ])
142
142
  if (index < stream.length &&
143
- tokensfuncDefMap.has(arena.dataTypes[stream[index]]) &&
144
- "supports".localeCompare(arena.strings[arena.stringValue[stream[index]]]) === 0) {
143
+ isFunctionDefType(arena.dataTypes[stream[index]]) &&
144
+ "supports".localeCompare(arena.resolveString(arena.stringValue[stream[index]])) === 0) {
145
145
  stack.push(stream[index]);
146
146
  tokens.push(stream[index++]);
147
147
  matchCount = 1;
@@ -150,7 +150,7 @@ function matchAtRuleImportSyntax(arena, atRule, stream, options) {
150
150
  // match the next ')'
151
151
  while (index < stream.length && matchCount > 0) {
152
152
  if (arena.dataTypes[stream[index]] === EnumToken.StartParensTokenType ||
153
- tokensfuncDefMap.has(arena.dataTypes[stream[index]])) {
153
+ isFunctionDefType(arena.dataTypes[stream[index]])) {
154
154
  matchCount++;
155
155
  }
156
156
  else if (arena.dataTypes[stream[index]] === EnumToken.EndParensTokenType) {
@@ -1,15 +1,15 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.3.0
6
6
  License: MIT
7
7
  */
8
8
  import { TokenField } from '../../ast/arena.js';
9
9
  import { EnumToken } from '../../ast/types.js';
10
10
  import { evaluate } from '../../ast/math/expression.js';
11
11
  import { gcd } from '../../ast/math/math.js';
12
- import { tokensfuncDefMap, mediaTypes, mFLT, mFGT } from '../../syntax/constants.js';
12
+ import { isFunctionDefType, mediaTypes, tokensfuncDefMap, mFLT, mFGT } from '../../syntax/constants.js';
13
13
  import { trimArray, matchAllSyntaxes, createValidationContext, getMFInfo, isMFValue } from '../../validation/match.js';
14
14
  import { ValidationSyntaxGroupEnum, MediaFeatureType } from '../../validation/parser/typedef.js';
15
15
  import { getParsedSyntax } from '../../validation/config.js';
@@ -32,7 +32,7 @@ function parseMediaqueryList(arena, stream, options) {
32
32
  continue;
33
33
  }
34
34
  parts[parts.length - 1].push(token);
35
- if (type === EnumToken.StartParensTokenType || tokensfuncDefMap.has(type)) {
35
+ if (type === EnumToken.StartParensTokenType || isFunctionDefType(type)) {
36
36
  matchCount++;
37
37
  }
38
38
  else if (type === EnumToken.EndParensTokenType && matchCount > 0) {
@@ -57,7 +57,7 @@ function parseMediaqueryList(arena, stream, options) {
57
57
  // [not | only] <media-type> [ and <media-condition-without-or> ]?
58
58
  if (i < stream.length) {
59
59
  if (arena.dataTypes[stream[i]] === EnumToken.IdenTokenType) {
60
- const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
60
+ const val = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
61
61
  if (mediaTypes.includes(val)) {
62
62
  tokens.push(stream[i]);
63
63
  i++;
@@ -97,7 +97,7 @@ function parseMediaqueryList(arena, stream, options) {
97
97
  continue;
98
98
  }
99
99
  if (arena.dataTypes[stream[i]] === EnumToken.StartParensTokenType ||
100
- tokensfuncDefMap.has(arena.dataTypes[stream[i]])) {
100
+ isFunctionDefType(arena.dataTypes[stream[i]])) {
101
101
  scopes.push((currentScope = new Set()));
102
102
  stack.push(stream[i]);
103
103
  continue;
@@ -113,7 +113,7 @@ function parseMediaqueryList(arena, stream, options) {
113
113
  break;
114
114
  case EnumToken.IdenTokenType:
115
115
  {
116
- const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
116
+ const val = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
117
117
  if (val === "and" || val === "or") {
118
118
  arena.dataTypes[stream[i]] =
119
119
  val === "and" ? EnumToken.AndTokenType : EnumToken.OrTokenType;
@@ -153,13 +153,13 @@ function parseMediaqueryList(arena, stream, options) {
153
153
  }
154
154
  break;
155
155
  case EnumToken.EndParensTokenType:
156
- if (stack.length > 0 && tokensfuncDefMap.has(arena.dataTypes[stack[stack.length - 1]])) {
156
+ if (stack.length > 0 && isFunctionDefType(arena.dataTypes[stack[stack.length - 1]])) {
157
157
  const index = tokens.indexOf(stack[stack.length - 1]);
158
158
  arena.spanLocation(tokens[index], tokens[index], stream[i]);
159
159
  arena.dataTypes[tokens[index]] = tokensfuncDefMap.get(arena.dataTypes[tokens[index]]);
160
160
  arena.setTokenList(tokens[index], trimArray(arena, tokens.slice(index + 1, tokens.length - 1)));
161
161
  tokens.length = index + 1;
162
- const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, arena.strings[arena.stringValue[tokens[index]]] + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(tokens[index])), options);
162
+ const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, arena.resolveString(arena.stringValue[tokens[index]]) + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(tokens[index])), options);
163
163
  stack.pop();
164
164
  scopes.pop();
165
165
  currentScope = scopes.at(-1);
@@ -189,7 +189,7 @@ function parseMediaqueryList(arena, stream, options) {
189
189
  const names = trimArray(arena, tokens.slice(index2 + 1, index));
190
190
  const filteredNames = names.filter((n) => arena.dataTypes[n] !== EnumToken.WhitespaceTokenType &&
191
191
  arena.dataTypes[n] !== EnumToken.CommentTokenType);
192
- const name = arena.strings[arena.stringValue[filteredNames[0]]];
192
+ const name = arena.resolveString(arena.stringValue[filteredNames[0]]);
193
193
  const mfInfo = getMFInfo(name);
194
194
  for (const val of [left, right]) {
195
195
  if (options.computeCalcExpression) {
@@ -198,7 +198,7 @@ function parseMediaqueryList(arena, stream, options) {
198
198
  mfInfo.type !== MediaFeatureType.StringType)) {
199
199
  for (let l = 0; l < val.length; l++) {
200
200
  if (arena.dataTypes[val[l]] === EnumToken.MathFunctionTokenType &&
201
- arena.strings[arena.stringValue[val[l]]] === "calc") {
201
+ arena.resolveString(arena.stringValue[val[l]]) === "calc") {
202
202
  const value = evaluate(arena, [val[l]]);
203
203
  if (value.length === 1) {
204
204
  arena.copyLocation(value[0], val[l]);
@@ -259,7 +259,7 @@ function parseMediaqueryList(arena, stream, options) {
259
259
  let values = trimArray(arena, tokens.slice(index2 + 1, tokens.length - 1));
260
260
  const filteredNames = (names).filter((n) => arena.dataTypes[n] !== EnumToken.WhitespaceTokenType &&
261
261
  arena.dataTypes[n] !== EnumToken.CommentTokenType);
262
- const name = arena.strings[arena.stringValue[filteredNames[0]]];
262
+ const name = arena.resolveString(arena.stringValue[filteredNames[0]]);
263
263
  const mfInfo = getMFInfo(name);
264
264
  if (options.computeCalcExpression) {
265
265
  if (mfInfo == null ||
@@ -268,7 +268,7 @@ function parseMediaqueryList(arena, stream, options) {
268
268
  const val = values;
269
269
  for (let l = 0; l < val.length; l++) {
270
270
  if (arena.dataTypes[val[l]] === EnumToken.MathFunctionTokenType &&
271
- arena.strings[arena.stringValue[val[l]]] === "calc") {
271
+ arena.resolveString(arena.stringValue[val[l]]) === "calc") {
272
272
  const value = evaluate(arena, [val[l]]);
273
273
  if (value.length === 1) {
274
274
  arena.copyLocation(value[0], val[l]);
@@ -416,7 +416,7 @@ function parseMediaqueryList(arena, stream, options) {
416
416
  arena.dataTypes[rightNames[0]] === EnumToken.IdenTokenType &&
417
417
  (arena.dataTypes[rightOp] === EnumToken.LtTokenType ||
418
418
  arena.dataTypes[rightOp] === EnumToken.LteTokenType) &&
419
- equalsIgnoreCase(arena.strings[arena.stringValue[leftNames[0]]], arena.strings[arena.stringValue[rightNames[0]]])) {
419
+ equalsIgnoreCase(arena.resolveString(arena.stringValue[leftNames[0]]), arena.resolveString(arena.stringValue[rightNames[0]]))) {
420
420
  const rangeQuery = arena.createToken(EnumToken.MediaRangeQueryTokenType);
421
421
  arena.dataTypes[leftOp] =
422
422
  arena.dataTypes[leftOp] === EnumToken.GteTokenType
@@ -1,12 +1,12 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.3.0
6
6
  License: MIT
7
7
  */
8
8
  import { EnumToken } from '../../ast/types.js';
9
- import { pseudoElements, tokensfuncDefMap } from '../../syntax/constants.js';
9
+ import { pseudoElements, isFunctionDefType, tokensfuncDefMap } from '../../syntax/constants.js';
10
10
  import { getSyntaxConfig, getParsedSyntax } from '../../validation/config.js';
11
11
  import { trimArray, matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
12
12
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
@@ -35,7 +35,7 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
35
35
  tokens.push(stream[i]);
36
36
  if (arena.dataTypes[stream[i]] === EnumToken.ColonTokenType) {
37
37
  if (arena.dataTypes[stream[i + 1]] === EnumToken.IdenTokenType) {
38
- const val = arena.strings[arena.stringValue[stream[i + 1]]];
38
+ const val = arena.resolveString(arena.stringValue[stream[i + 1]]);
39
39
  arena.dataTypes[stream[i]] = pseudoElements.includes(val)
40
40
  ? EnumToken.PseudoElementTokenType
41
41
  : EnumToken.PseudoClassTokenType;
@@ -47,7 +47,7 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
47
47
  continue;
48
48
  }
49
49
  if (arena.dataTypes[stream[i + 1]] === EnumToken.FunctionTokenDefType) {
50
- val = ":" + arena.strings[arena.stringValue[stream[i + 1]]];
50
+ val = ":" + arena.resolveString(arena.stringValue[stream[i + 1]]);
51
51
  arena.dataTypes[stream[i]] =
52
52
  val + "()" in getSyntaxConfig().selectors
53
53
  ? EnumToken.PseudoClassFunctionTokenDefType
@@ -82,7 +82,7 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
82
82
  expectAndOr = false;
83
83
  }
84
84
  if (arena.dataTypes[stream[i]] === EnumToken.StartParensTokenType ||
85
- tokensfuncDefMap.has(arena.dataTypes[stream[i]])) {
85
+ isFunctionDefType(arena.dataTypes[stream[i]])) {
86
86
  scopes.push((scope = new Set()));
87
87
  stack.push(stream[i]);
88
88
  continue;
@@ -114,7 +114,7 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
114
114
  scope = scopes[scopes.length - 1];
115
115
  expectAndOr = true;
116
116
  }
117
- else if (stack.length > 0 && tokensfuncDefMap.has(arena.dataTypes[stack[stack.length - 1]])) {
117
+ else if (stack.length > 0 && isFunctionDefType(arena.dataTypes[stack[stack.length - 1]])) {
118
118
  const func = stack[stack.length - 1];
119
119
  const index = tokens.indexOf(func);
120
120
  arena.dataTypes[func] = tokensfuncDefMap.get(arena.dataTypes[func]);
@@ -122,7 +122,7 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
122
122
  arena.spanLocation(func, func, stream[i]);
123
123
  if (arena.dataTypes[tokens[index]] === EnumToken.PseudoClassFuncTokenType) {
124
124
  // not a declaration
125
- const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, arena.strings[arena.stringValue[tokens[index]]] + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(tokens[index])), options);
125
+ const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, arena.resolveString(arena.stringValue[tokens[index]]) + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(tokens[index])), options);
126
126
  if (!result.success) {
127
127
  return {
128
128
  success: false,
@@ -133,11 +133,6 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
133
133
  tokens.pop();
134
134
  break;
135
135
  }
136
- //
137
- let k = stack.length - 1;
138
- while (tokensfuncDefMap.has(arena.dataTypes[stack[k]])) {
139
- k--;
140
- }
141
136
  stack.pop();
142
137
  tokens.pop();
143
138
  scopes.pop();
@@ -182,7 +177,7 @@ function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
182
177
  break;
183
178
  case EnumToken.IdenTokenType:
184
179
  {
185
- const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
180
+ const val = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
186
181
  if ("not" === val) {
187
182
  stack.push(stream[i]);
188
183
  arena.dataTypes[stream[i]] = EnumToken.NotTokenType;
@@ -1,14 +1,14 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.3.0
6
6
  License: MIT
7
7
  */
8
8
  import { TokenField } from '../../ast/arena.js';
9
9
  import { EnumToken } from '../../ast/types.js';
10
10
  import { trimArray } from '../../validation/match.js';
11
- import { tokensfuncDefMap } from '../../syntax/constants.js';
11
+ import { tokensfuncDefMap, isFunctionDefType } from '../../syntax/constants.js';
12
12
  import { parseMediaqueryList } from './at-rule-media.js';
13
13
  import { parseAtRuleSupportSyntax } from './at-rule-support.js';
14
14
 
@@ -36,7 +36,7 @@ function matchAtRuleWhenElseSyntax(arena, stream, context, options = {}) {
36
36
  switch (arena.dataTypes[stream[i]]) {
37
37
  case EnumToken.IdenTokenType:
38
38
  {
39
- const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
39
+ const val = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
40
40
  if ("and" === val || "or" === val) {
41
41
  arena.dataTypes[stream[i]] = "or" === val ? EnumToken.OrTokenType : EnumToken.AndTokenType;
42
42
  // scope.add(arena.dataTypes[stream[i]]);
@@ -58,12 +58,12 @@ function matchAtRuleWhenElseSyntax(arena, stream, context, options = {}) {
58
58
  }
59
59
  }
60
60
  else if (arena.dataTypes[stream[j]] === EnumToken.StartParensTokenType ||
61
- tokensfuncDefMap.has(arena.dataTypes[stream[j]])) {
61
+ isFunctionDefType(arena.dataTypes[stream[j]])) {
62
62
  matchCount++;
63
63
  }
64
64
  }
65
65
  const slice = stream.slice(i, j + 1);
66
- const funcName = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
66
+ const funcName = arena.resolveString(arena.stringValue[stream[i]]).toLowerCase();
67
67
  // the function token may be converted by the sub parsers
68
68
  const isSupportsFunction = arena.dataTypes[stream[i]] === EnumToken.SupportsFunctionTokenDefType;
69
69
  const funcType = tokensfuncDefMap.get(arena.dataTypes[stream[i]]);
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.3.0
6
6
  License: MIT
7
7
  */
8
8
  import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
@@ -10,7 +10,7 @@ import { getSyntaxRule } from '../../validation/config.js';
10
10
  import { trimArray, matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
11
11
 
12
12
  function matchAtRuleSyntax(arena, atRule, stream, options) {
13
- const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.strings[arena.name[atRule]]);
13
+ const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.resolveString(arena.name[atRule]));
14
14
  const syntax = syntaxRules?.getPreludeRules()?.slice?.(1);
15
15
  trimArray(arena, stream);
16
16
  if (syntax.length === 0) {
@@ -1,8 +1,8 @@
1
1
  /*
2
- CSS parser, minifier and validator for node and the browser
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
3
 
4
4
  Author(s): Thierry Bela
5
- Version: 2.1.0
5
+ Version: 2.3.0
6
6
  License: MIT
7
7
  */
8
8
  /**
@@ -0,0 +1,53 @@
1
+ /*
2
+ CSS parser, minifier and validator for node, deno, bun and the browser
3
+
4
+ Author(s): Thierry Bela
5
+ Version: 2.3.0
6
+ License: MIT
7
+ */
8
+ /**
9
+ * memoize a function by its arguments (compared with `Map` semantics). the `null` and `undefined` results are cached
10
+ * too: an unknown name is looked up once
11
+ * @param fn
12
+ * @returns
13
+ */
14
+ function memoize(fn) {
15
+ const buckets = new Map();
16
+ return function (...args) {
17
+ const n = args.length;
18
+ let bucket = buckets.get(n);
19
+ if (bucket == null) {
20
+ bucket = new Map();
21
+ buckets.set(n, bucket);
22
+ }
23
+ if (n === 1) {
24
+ const key = args[0];
25
+ let value = bucket.get(key);
26
+ if (value === undefined && !bucket.has(key)) {
27
+ value = fn(key);
28
+ bucket.set(key, value);
29
+ }
30
+ return value;
31
+ }
32
+ // the maps of the first arguments, then the results by last argument
33
+ let node = bucket;
34
+ for (let i = 0; i < n - 1; i++) {
35
+ const key = args[i];
36
+ let next = node.get(key);
37
+ if (next == null) {
38
+ next = new Map();
39
+ node.set(key, next);
40
+ }
41
+ node = next;
42
+ }
43
+ const last = n === 0 ? undefined : args[n - 1];
44
+ let value = node.get(last);
45
+ if (value === undefined && !node.has(last)) {
46
+ value = fn(...args);
47
+ node.set(last, value);
48
+ }
49
+ return value;
50
+ };
51
+ }
52
+
53
+ export { memoize };