@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,19 +1,23 @@
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
- import config from '../../data/validation.json.js';
8
+ import config from '../data/validation.json.js';
9
9
  import { trimSyntaxArray, parseSyntax } from './parser/parse.js';
10
- import { ValidationSyntaxGroupEnum, ValidationTokenEnum } from './parser/typedef.js';
10
+ import { ValidationTokenEnum, ValidationSyntaxGroupEnum } from './parser/typedef.js';
11
11
  import { memoize } from '../parser/utils/cache.js';
12
12
 
13
- const parsedSyntaxes = new Map();
13
+ /**
14
+ * parsed syntaxes, by syntax node: the names that share a definition (`-webkit-transform` and `transform`, the aliases)
15
+ * share the parsed syntax, and the validations cached by syntax (see `matchDeclarationSyntax()`)
16
+ */
17
+ const parsedSyntaxes = new WeakMap();
14
18
  Object.freeze(config);
15
19
  function getSyntaxConfig() {
16
- // @ts-expect-error
20
+ // @ts-ignore
17
21
  return config;
18
22
  }
19
23
  /**
@@ -51,8 +55,17 @@ function lookupNode(group, key) {
51
55
  * @param key the type name without the angle brackets, or the function name followed by `()`
52
56
  */
53
57
  function hasSyntax(key) {
54
- return lookupNode(ValidationSyntaxGroupEnum.Syntaxes, key) != null;
58
+ let defined = definedSyntaxes.get(key);
59
+ if (defined === undefined) {
60
+ defined = lookupNode(ValidationSyntaxGroupEnum.Syntaxes, key) != null;
61
+ definedSyntaxes.set(key, defined);
62
+ }
63
+ return defined;
55
64
  }
65
+ /**
66
+ * the types and the functions that are defined, by name, see {@link hasSyntax}
67
+ */
68
+ const definedSyntaxes = new Map();
56
69
  /**
57
70
  * return the raw syntax of a type or a function
58
71
  * @param key the type name without the angle brackets, or the function name followed by `()`
@@ -77,18 +90,52 @@ function findNode(group, key) {
77
90
  return obj;
78
91
  }
79
92
  const getParsedSyntax = memoize((group, key) => {
80
- // @ts-expect-error
93
+ // @ts-ignore
81
94
  const obj = findNode(group, key);
82
95
  if (obj == null) {
83
96
  return null;
84
97
  }
85
- const index = group + "." + key;
86
- if (!parsedSyntaxes.has(index)) {
87
- const syntax = Object.freeze(trimSyntaxArray(parseSyntax(obj.syntax)));
88
- parsedSyntaxes.set(index, syntax);
98
+ let syntax = parsedSyntaxes.get(obj);
99
+ if (syntax === undefined) {
100
+ syntax = Object.freeze(trimSyntaxArray(parseSyntax(obj.syntax)));
101
+ parsedSyntaxes.set(obj, syntax);
102
+ syntaxNodes.set(syntax, obj);
89
103
  }
90
- return parsedSyntaxes.get(index);
104
+ return syntax;
91
105
  });
106
+ /**
107
+ * the definition node of a parsed syntax, see {@link syntaxKey}
108
+ */
109
+ const syntaxNodes = new WeakMap();
110
+ /**
111
+ * the key of the definitions that are compiled (`group/name`, see scripts/compile-syntax.ts), by definition node
112
+ */
113
+ let nodeKeys = null;
114
+ /**
115
+ * the key of the definition of a parsed syntax (`declarations/width`, `types/color`), see scripts/compile-syntax.ts.
116
+ * null if the syntax is not the syntax of a declaration, a type or a function (a descriptor, ...)
117
+ * @param syntaxes a syntax returned by {@link getParsedSyntax}
118
+ */
119
+ function syntaxKey(syntaxes) {
120
+ const node = syntaxNodes.get(syntaxes);
121
+ if (node === undefined) {
122
+ return null;
123
+ }
124
+ if (nodeKeys === null) {
125
+ nodeKeys = new Map();
126
+ const groups = config;
127
+ for (const group of [
128
+ ValidationSyntaxGroupEnum.Declarations,
129
+ ValidationSyntaxGroupEnum.Syntaxes,
130
+ ValidationSyntaxGroupEnum.Types,
131
+ ]) {
132
+ for (const [key, value] of Object.entries(groups[group] ?? {})) {
133
+ nodeKeys.set(value, group + "/" + key);
134
+ }
135
+ }
136
+ }
137
+ return nodeKeys.get(node) ?? null;
138
+ }
92
139
  const getSyntaxRule = memoize((group, key) => {
93
140
  const node = findNode(group, key);
94
141
  if (node == null) {
@@ -120,10 +167,10 @@ const getSyntaxRule = memoize((group, key) => {
120
167
  let block = blockStart === -1 ? null : trimSyntaxArray(syntaxRules.slice(blockStart + 1, blockEnd));
121
168
  let prelude = trimSyntaxArray(blockStart === -1 ? syntaxRules.slice() : syntaxRules.slice(0, blockStart));
122
169
  let propertyDescriptors = null;
123
- // @ts-expect-error
170
+ // @ts-ignore
124
171
  if (node.descriptors != null) {
125
172
  propertyDescriptors = {};
126
- // @ts-expect-error
173
+ // @ts-ignore
127
174
  for (const [key, value] of Object.entries(node.descriptors)) {
128
175
  // @ts-expect-error
129
176
  propertyDescriptors[key] = parseSyntax(typeof value === "string" ? value : value.syntax);
@@ -157,4 +204,4 @@ const getSyntaxRule = memoize((group, key) => {
157
204
  };
158
205
  });
159
206
 
160
- export { getParsedSyntax, getSyntax, getSyntaxConfig, getSyntaxRule, hasSyntax };
207
+ export { getParsedSyntax, getSyntax, getSyntaxConfig, getSyntaxRule, hasSyntax, syntaxKey };
@@ -0,0 +1,574 @@
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
+ import { EnumToken } from '../ast/types.js';
9
+ import { getSyntaxConfig, getParsedSyntax } from './config.js';
10
+ import { ValidationTokenEnum, ValidationSyntaxGroupEnum } from './parser/typedef.js';
11
+ import { tokensfuncDefMap, isFunctionDefType, isFunctionType, funcLike } from '../syntax/constants.js';
12
+ import { equalsIgnoreCase, lowercase } from '../parser/utils/text.js';
13
+
14
+ /**
15
+ * the syntaxes that may match no token, see {@link isNullable}
16
+ */
17
+ const nullableSyntaxes = new WeakMap();
18
+ /**
19
+ * true if the syntax may match no token: `<a>?`, `<a>*`, `[ <a>? <b>? ]`, `[ [ and <a> ]* | [ or <a> ]* ]`
20
+ * @param syntax
21
+ */
22
+ function isNullable(syntax) {
23
+ if (syntax.typ === ValidationTokenEnum.Whitespace) {
24
+ return true;
25
+ }
26
+ // `[ <a>? <b>? ]!`: at least one token
27
+ if (syntax.isMandatatoryGroup) {
28
+ return false;
29
+ }
30
+ if (syntax.isOptional || syntax.isRepeatable || syntax.match?.min?.val === 0) {
31
+ return true;
32
+ }
33
+ let nullable = nullableSyntaxes.get(syntax);
34
+ if (nullable == null) {
35
+ switch (syntax.typ) {
36
+ case ValidationTokenEnum.Bracket:
37
+ nullable = syntax.chi.every(isNullable);
38
+ break;
39
+ case ValidationTokenEnum.PipeToken:
40
+ nullable = syntax.chi.some((syntaxes) => syntaxes.every(isNullable));
41
+ break;
42
+ case ValidationTokenEnum.AmpersandToken:
43
+ nullable =
44
+ syntax.l.every(isNullable) &&
45
+ syntax.r.every(isNullable);
46
+ break;
47
+ default:
48
+ nullable = false;
49
+ }
50
+ nullableSyntaxes.set(syntax, nullable);
51
+ }
52
+ return nullable;
53
+ }
54
+ /**
55
+ * the types of the function tokens (`name(`, `name(...)`): the types whose name is a key of the sets
56
+ */
57
+ const functionTokenTypes = new Set();
58
+ /**
59
+ * the types of the function definition tokens (`name(` followed by the arguments and `)`)
60
+ */
61
+ const functionDefTypes = [];
62
+ /**
63
+ * the function types accepted by the `Function` syntaxes of the matcher (see `funcTypes` in match.ts)
64
+ */
65
+ const funcTypes = [
66
+ ...tokensfuncDefMap.values(),
67
+ EnumToken.FunctionTokenType,
68
+ EnumToken.PseudoClassFuncTokenType,
69
+ ];
70
+ for (const value of Object.values(EnumToken)) {
71
+ if (typeof value !== "number") {
72
+ continue;
73
+ }
74
+ if (isFunctionDefType(value)) {
75
+ functionDefTypes.push(value);
76
+ }
77
+ if (isFunctionDefType(value) || isFunctionType(value) || funcLike.includes(value) || funcTypes.includes(value)) {
78
+ functionTokenTypes.add(value);
79
+ }
80
+ }
81
+ /**
82
+ * the types of the tokens accepted by the built-in types of the matcher (`matchProperty()`). the built-in types that
83
+ * are not listed accept any token (complex checks: `<declaration-value>`, `<an+b>`, the selectors, the media features,
84
+ * ...).
85
+ * the css wide keywords, which are accepted by the built-in types, are not looked up
86
+ */
87
+ const builtinTypes = new Map([
88
+ ["dashed-ident", [EnumToken.DashedIdenTokenType]],
89
+ ["extension-name", [EnumToken.DashedIdenTokenType]],
90
+ ["custom-property-name", [EnumToken.DashedIdenTokenType]],
91
+ ["namespace-prefix", [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType]],
92
+ ["ident", [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType]],
93
+ ["ident-token", [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType]],
94
+ ["custom-ident", [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType]],
95
+ ["counter-name", [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType]],
96
+ ["counter-style-name", [EnumToken.IdenTokenType, EnumToken.DashedIdenTokenType]],
97
+ ["unicode-range-token", [EnumToken.UniCodeRange]],
98
+ ["hash-token", [EnumToken.HashTokenType, EnumToken.ColorTokenType]],
99
+ ["string", [EnumToken.StringTokenType]],
100
+ ["wq-name", [EnumToken.IdenTokenType]],
101
+ ["number", [EnumToken.NumberTokenType]],
102
+ ["zero", [EnumToken.NumberTokenType]],
103
+ ["integer", [EnumToken.NumberTokenType]],
104
+ [
105
+ "dimension",
106
+ [
107
+ EnumToken.DimensionTokenType,
108
+ EnumToken.AngleTokenType,
109
+ EnumToken.LengthTokenType,
110
+ EnumToken.PercentageTokenType,
111
+ EnumToken.ResolutionTokenType,
112
+ EnumToken.TimeTokenType,
113
+ EnumToken.FrequencyTokenType,
114
+ EnumToken.UserDefinedDimensionTokenType,
115
+ EnumToken.FlexTokenType,
116
+ ],
117
+ ],
118
+ ["angle", [EnumToken.AngleTokenType, EnumToken.UserDefinedDimensionTokenType]],
119
+ [
120
+ "angle-percentage",
121
+ [EnumToken.AngleTokenType, EnumToken.UserDefinedDimensionTokenType, EnumToken.PercentageTokenType],
122
+ ],
123
+ ["percentage", [EnumToken.PercentageTokenType]],
124
+ [
125
+ "length-percentage",
126
+ [
127
+ EnumToken.PercentageTokenType,
128
+ EnumToken.LengthTokenType,
129
+ EnumToken.UserDefinedDimensionTokenType,
130
+ EnumToken.NumberTokenType,
131
+ ],
132
+ ],
133
+ ["flex", [EnumToken.FlexTokenType, EnumToken.UserDefinedDimensionTokenType]],
134
+ ["length", [EnumToken.LengthTokenType, EnumToken.UserDefinedDimensionTokenType, EnumToken.NumberTokenType]],
135
+ ["resolution", [EnumToken.ResolutionTokenType]],
136
+ ["frequency", [EnumToken.FrequencyTokenType]],
137
+ ["decibel", [EnumToken.DimensionTokenType]],
138
+ ["semitones", [EnumToken.DimensionTokenType]],
139
+ ["time", [EnumToken.TimeTokenType, EnumToken.UserDefinedDimensionTokenType]],
140
+ ["quote", [EnumToken.IdenTokenType]],
141
+ ["display-box", [EnumToken.IdenTokenType]],
142
+ ["display-inside", [EnumToken.IdenTokenType]],
143
+ ["display-internal", [EnumToken.IdenTokenType]],
144
+ ["display-outside", [EnumToken.IdenTokenType]],
145
+ ["display-legacy", [EnumToken.IdenTokenType]],
146
+ ["content-position", [EnumToken.IdenTokenType]],
147
+ ["mf-name", [EnumToken.IdenTokenType]],
148
+ ["language-code", [EnumToken.IdenTokenType]],
149
+ ["url", [EnumToken.UrlFunctionTokenDefType, EnumToken.UrlFunctionTokenType]],
150
+ // `isColor()`: the color tokens, the color names and the color functions
151
+ ["color", [EnumToken.ColorTokenType, EnumToken.IdenTokenType, EnumToken.FunctionTokenType, ...functionDefTypes]],
152
+ [
153
+ "color-function",
154
+ [EnumToken.ColorTokenType, EnumToken.IdenTokenType, EnumToken.FunctionTokenType, ...functionDefTypes],
155
+ ],
156
+ ["function-token", functionDefTypes],
157
+ ["dashed-function", [EnumToken.CustomFunctionTokenDefType, EnumToken.CustomFunctionTokenType]],
158
+ ["block-contents", [EnumToken.StartParensTokenType]],
159
+ ["css-type", [EnumToken.FunctionTokenDefType, EnumToken.FunctionTokenType, EnumToken.CssTypeTokenType]],
160
+ // any token
161
+ ["declaration-value", null],
162
+ ["calc-sum", null],
163
+ ["an+b", null],
164
+ ["any-value", null],
165
+ ["type-selector", null],
166
+ ["compound-selector", null],
167
+ ["complex-selector", null],
168
+ ["subclass-selector", null],
169
+ ["attribute-selector", null],
170
+ ["complex-selector-unit", null],
171
+ ["pseudo-compound-selector", null],
172
+ ["forgiving-relative-selector-list", null],
173
+ ["complex-selector-list", null],
174
+ ["size-feature", null],
175
+ ["style-feature", null],
176
+ ]);
177
+ /**
178
+ * a set that accepts any token
179
+ */
180
+ const anyToken = Object.freeze({
181
+ any: true,
182
+ nullable: true,
183
+ types: new Set(),
184
+ keywords: new Set(),
185
+ functions: new Set(),
186
+ });
187
+ /**
188
+ * first token sets of the syntax tokens
189
+ */
190
+ const tokenSets = new WeakMap();
191
+ /**
192
+ * first token sets of the syntax sequences
193
+ */
194
+ const sequenceSets = new WeakMap();
195
+ /**
196
+ * the sequences whose set is being computed: a cyclic reference accepts any token
197
+ */
198
+ const computing = new Set();
199
+ /**
200
+ * an empty set
201
+ */
202
+ function createSet() {
203
+ return {
204
+ any: false,
205
+ nullable: false,
206
+ types: new Set(),
207
+ keywords: new Set(),
208
+ functions: new Set(),
209
+ };
210
+ }
211
+ /**
212
+ * add the tokens of a set to another set
213
+ * @param target
214
+ * @param source
215
+ */
216
+ function merge(target, source) {
217
+ target.any ||= source.any;
218
+ for (const type of source.types) {
219
+ target.types.add(type);
220
+ }
221
+ for (const keyword of source.keywords) {
222
+ target.keywords.add(keyword);
223
+ }
224
+ for (const name of source.functions) {
225
+ target.functions.add(name);
226
+ }
227
+ }
228
+ /**
229
+ * first tokens of a sequence of syntaxes: the first tokens of its syntaxes until a syntax that must match a token
230
+ * @param syntaxes
231
+ */
232
+ function firstOfSequence(syntaxes) {
233
+ if (syntaxes == null) {
234
+ // no syntax: the matcher accepts the value
235
+ return anyToken;
236
+ }
237
+ let set = sequenceSets.get(syntaxes);
238
+ if (set !== undefined) {
239
+ return set;
240
+ }
241
+ if (computing.has(syntaxes)) {
242
+ return anyToken;
243
+ }
244
+ computing.add(syntaxes);
245
+ try {
246
+ set = createSet();
247
+ set.nullable = true;
248
+ for (const syntax of syntaxes) {
249
+ const first = firstOf(syntax);
250
+ merge(set, first);
251
+ if (set.any || !first.nullable) {
252
+ set.nullable = false;
253
+ break;
254
+ }
255
+ }
256
+ if (set.any) {
257
+ set = anyToken;
258
+ }
259
+ }
260
+ finally {
261
+ computing.delete(syntaxes);
262
+ }
263
+ sequenceSets.set(syntaxes, set);
264
+ return set;
265
+ }
266
+ /**
267
+ * first tokens of a syntax token
268
+ * @param syntax
269
+ */
270
+ function firstOf(syntax) {
271
+ let set = tokenSets.get(syntax);
272
+ if (set !== undefined) {
273
+ return set;
274
+ }
275
+ set = createSet();
276
+ // the syntax may match no token through its content: an empty alternative (`| -moz-center`), a reference to a
277
+ // syntax that may match no token, ...
278
+ let nullable = false;
279
+ let first;
280
+ switch (syntax.typ) {
281
+ case ValidationTokenEnum.Whitespace:
282
+ nullable = true;
283
+ break;
284
+ case ValidationTokenEnum.Keyword:
285
+ set.keywords.add(lowercase(syntax.val));
286
+ break;
287
+ case ValidationTokenEnum.Comma:
288
+ set.types.add(EnumToken.CommaTokenType);
289
+ break;
290
+ case ValidationTokenEnum.Colon:
291
+ set.types.add(EnumToken.ColonTokenType);
292
+ break;
293
+ case ValidationTokenEnum.Separator:
294
+ set.types.add(EnumToken.LiteralTokenType);
295
+ break;
296
+ case ValidationTokenEnum.OpenParenthesis:
297
+ set.types.add(EnumToken.StartParensTokenType);
298
+ break;
299
+ case ValidationTokenEnum.CloseParenthesis:
300
+ set.types.add(EnumToken.EndParensTokenType);
301
+ break;
302
+ case ValidationTokenEnum.OpenCurlyBrace:
303
+ set.types.add(EnumToken.BlockStartTokenType);
304
+ break;
305
+ case ValidationTokenEnum.Number:
306
+ set.types.add(EnumToken.NumberTokenType);
307
+ break;
308
+ case ValidationTokenEnum.Dimension:
309
+ set.types.add(EnumToken[syntax.unit]);
310
+ break;
311
+ case ValidationTokenEnum.StringToken:
312
+ // `'+'`, `'-'`, `'/'`
313
+ set.types.add(EnumToken.Add);
314
+ set.types.add(EnumToken.Sub);
315
+ set.types.add(EnumToken.LiteralTokenType);
316
+ set.types.add(EnumToken.Plus);
317
+ break;
318
+ case ValidationTokenEnum.Function:
319
+ case ValidationTokenEnum.FunctionDefinition:
320
+ set.functions.add(lowercase(syntax.val));
321
+ break;
322
+ case ValidationTokenEnum.Bracket:
323
+ first = firstOfSequence(syntax.chi);
324
+ merge(set, first);
325
+ nullable = first.nullable;
326
+ break;
327
+ case ValidationTokenEnum.OptionalGroupToken:
328
+ // the group is skipped when it does not match
329
+ merge(set, firstOfSequence(syntax.chi));
330
+ nullable = true;
331
+ break;
332
+ case ValidationTokenEnum.PipeToken:
333
+ for (const syntaxes of syntax.chi) {
334
+ first = firstOfSequence(syntaxes);
335
+ merge(set, first);
336
+ nullable ||= first.nullable;
337
+ }
338
+ break;
339
+ case ValidationTokenEnum.ColumnToken:
340
+ // one or more operands, in any order: at least one token
341
+ for (const syntaxes of syntax.chi) {
342
+ merge(set, firstOfSequence(syntaxes));
343
+ }
344
+ break;
345
+ case ValidationTokenEnum.AmpersandToken:
346
+ // all the operands, in any order
347
+ nullable = true;
348
+ for (const syntaxes of [syntax.l, syntax.r]) {
349
+ first = firstOfSequence(syntaxes);
350
+ merge(set, first);
351
+ nullable &&= first.nullable;
352
+ }
353
+ break;
354
+ case ValidationTokenEnum.DeclarationType:
355
+ first = firstOfSequence(getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, syntax.val));
356
+ merge(set, first);
357
+ nullable = first.nullable;
358
+ break;
359
+ case ValidationTokenEnum.PropertyType: {
360
+ const name = syntax.val;
361
+ if (builtinTypes.has(name)) {
362
+ const types = builtinTypes.get(name);
363
+ if (types == null) {
364
+ set.any = true;
365
+ nullable = true;
366
+ }
367
+ else {
368
+ for (const type of types) {
369
+ set.types.add(type);
370
+ }
371
+ }
372
+ }
373
+ else {
374
+ first = firstOfSequence(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, name));
375
+ merge(set, first);
376
+ nullable = first.nullable;
377
+ }
378
+ break;
379
+ }
380
+ default:
381
+ set.any = true;
382
+ nullable = true;
383
+ }
384
+ // the syntaxes that may match no token: `<a>?`, `<a>*`, `<a>{0,2}`, `[ <a>? ]`, ...
385
+ set.nullable =
386
+ nullable ||
387
+ syntax.isOptional === true ||
388
+ syntax.isRepeatable === true ||
389
+ syntax.match?.min?.val === 0 ||
390
+ isNullable(syntax);
391
+ tokenSets.set(syntax, set);
392
+ return set;
393
+ }
394
+ /**
395
+ * the css wide keywords, accepted by every syntax
396
+ */
397
+ const cssWideKeywords = getSyntaxConfig().declarations.all.syntax.split(/[\s|]+/g);
398
+ /**
399
+ * the token may match a syntax that does not accept its type (`var()`, `env()`, `attr()`, the custom function calls,
400
+ * the math functions, the css wide keywords, ...): the first token sets do not apply
401
+ * @param arena
402
+ * @param token
403
+ * @param typ
404
+ */
405
+ function matchesAnySyntax(arena, token, typ) {
406
+ switch (typ) {
407
+ case EnumToken.WildCardFunctionTokenType:
408
+ case EnumToken.WildCardFunctionTokenDefType:
409
+ case EnumToken.MathFunctionTokenType:
410
+ case EnumToken.MathFunctionTokenDefType:
411
+ // a custom function call is substituted by its result, like var()
412
+ case EnumToken.CustomFunctionTokenType:
413
+ case EnumToken.CustomFunctionTokenDefType:
414
+ return true;
415
+ case EnumToken.IdenTokenType: {
416
+ const value = arena.resolveString(arena.stringValue[token]);
417
+ for (let i = 0; i < cssWideKeywords.length; i++) {
418
+ if (equalsIgnoreCase(cssWideKeywords[i], value)) {
419
+ return true;
420
+ }
421
+ }
422
+ }
423
+ }
424
+ return false;
425
+ }
426
+ /**
427
+ * true if a set may start with a token
428
+ * @param set
429
+ * @param typ token type
430
+ * @param name lowercase name of an identifier or of a function, null for the other tokens
431
+ */
432
+ function accepts(set, typ, name) {
433
+ if (set.any || set.types.has(typ)) {
434
+ return true;
435
+ }
436
+ if (name === null) {
437
+ return false;
438
+ }
439
+ return typ === EnumToken.IdenTokenType ? set.keywords.has(name) : set.functions.has(name);
440
+ }
441
+ /**
442
+ * lowercase name of an identifier or of a function token, null for the other tokens
443
+ * @param arena
444
+ * @param token
445
+ * @param typ
446
+ */
447
+ function tokenName(arena, token, typ) {
448
+ return typ === EnumToken.IdenTokenType || functionTokenTypes.has(typ)
449
+ ? arena.resolveString(arena.lowerStringId(arena.stringValue[token]))
450
+ : null;
451
+ }
452
+ /**
453
+ * maximum number of named lookups memoized by an index
454
+ */
455
+ const MAX_NAMES = 512;
456
+ /**
457
+ * the alternatives of a syntax indexed by their first tokens
458
+ */
459
+ class AlternativeIndex {
460
+ /**
461
+ * first tokens of each alternative
462
+ */
463
+ sets;
464
+ /**
465
+ * the alternatives cannot be filtered: all of them accept any token
466
+ */
467
+ filtered;
468
+ /**
469
+ * the alternatives that may start with a token, by token type (the tokens without a name)
470
+ */
471
+ byType = new Map();
472
+ /**
473
+ * the alternatives that may start with an identifier or a function, by token type and by name
474
+ */
475
+ byName = new Map();
476
+ /**
477
+ * number of memoized named lookups
478
+ */
479
+ names = 0;
480
+ constructor(alternatives) {
481
+ this.sets = alternatives.map((syntaxes) => firstOfSequence(syntaxes));
482
+ this.filtered = this.sets.some((set) => !set.any && !set.nullable);
483
+ }
484
+ /**
485
+ * the indexes of the alternatives that may match a token, in order. null when all the alternatives may match
486
+ * @param typ
487
+ * @param name
488
+ */
489
+ lookup(typ, name) {
490
+ if (name === null) {
491
+ let result = this.byType.get(typ);
492
+ if (result === undefined) {
493
+ result = this.compute(typ, null);
494
+ this.byType.set(typ, result);
495
+ }
496
+ return result;
497
+ }
498
+ let names = this.byName.get(typ);
499
+ if (names === undefined) {
500
+ names = new Map();
501
+ this.byName.set(typ, names);
502
+ }
503
+ let result = names.get(name);
504
+ if (result === undefined) {
505
+ result = this.compute(typ, name);
506
+ if (this.names < MAX_NAMES) {
507
+ this.names++;
508
+ names.set(name, result);
509
+ }
510
+ }
511
+ return result;
512
+ }
513
+ /**
514
+ * the alternatives that may match a token. null when all the alternatives may match
515
+ * @param typ
516
+ * @param name
517
+ */
518
+ compute(typ, name) {
519
+ const result = [];
520
+ for (let i = 0; i < this.sets.length; i++) {
521
+ if (this.sets[i].nullable || accepts(this.sets[i], typ, name)) {
522
+ result.push(i);
523
+ }
524
+ }
525
+ return result.length === this.sets.length ? null : result;
526
+ }
527
+ }
528
+ /**
529
+ * the indexes of the alternatives, by list of alternatives (`ValidationPipeToken.chi`: the lists are shared by the
530
+ * copies of the syntax tokens)
531
+ */
532
+ const alternativeIndexes = new WeakMap();
533
+ /**
534
+ * the alternatives of a syntax (`<a> | <b> | <c>`) that may match the first token of a value, in order. the other
535
+ * alternatives cannot match the value. null when all the alternatives may match
536
+ * @param arena
537
+ * @param alternatives
538
+ * @param token the first token of the value
539
+ */
540
+ function matchingAlternatives(arena, alternatives, token) {
541
+ let index = alternativeIndexes.get(alternatives);
542
+ if (index === undefined) {
543
+ index = new AlternativeIndex(alternatives);
544
+ alternativeIndexes.set(alternatives, index);
545
+ }
546
+ if (!index.filtered || token == null) {
547
+ return null;
548
+ }
549
+ const typ = arena.dataTypes[token];
550
+ if (matchesAnySyntax(arena, token, typ)) {
551
+ return null;
552
+ }
553
+ return index.lookup(typ, tokenName(arena, token, typ));
554
+ }
555
+ /**
556
+ * true if a sequence of syntaxes may match tokens starting with a token. a sequence that does not accept the token
557
+ * matches no token
558
+ * @param arena
559
+ * @param syntaxes
560
+ * @param token
561
+ */
562
+ function mayStartWith(arena, syntaxes, token) {
563
+ if (token == null) {
564
+ return true;
565
+ }
566
+ const set = firstOfSequence(syntaxes);
567
+ if (set.any) {
568
+ return true;
569
+ }
570
+ const typ = arena.dataTypes[token];
571
+ return matchesAnySyntax(arena, token, typ) || accepts(set, typ, tokenName(arena, token, typ));
572
+ }
573
+
574
+ export { isNullable, matchingAlternatives, mayStartWith };