@tbela99/css-parser 2.0.1 → 2.2.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 (128) hide show
  1. package/CHANGELOG.md +86 -1
  2. package/README.md +15 -7
  3. package/architecture.png +0 -0
  4. package/dist/{src/data → data}/properties.json.js +86 -2
  5. package/dist/{src/data → data}/validation.json.js +4848 -3750
  6. package/dist/index-umd-web.js +18234 -9498
  7. package/dist/index.d.ts +1251 -373
  8. package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
  9. package/dist/{src/lib → lib}/ast/arena.js +1034 -655
  10. package/dist/{src/lib → lib}/ast/cache.js +36 -31
  11. package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
  12. package/dist/{src/lib → lib}/ast/features/if.js +24 -19
  13. package/dist/{src/lib → lib}/ast/features/index.js +3 -2
  14. package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
  15. package/dist/lib/ast/features/mixin.js +953 -0
  16. package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
  17. package/dist/lib/ast/features/shorthand.js +129 -0
  18. package/dist/lib/ast/features/transform.js +188 -0
  19. package/dist/{src/lib → lib}/ast/features/type.js +2 -2
  20. package/dist/{src/lib → lib}/ast/math/expression.js +150 -150
  21. package/dist/{src/lib → lib}/ast/math/math.js +8 -8
  22. package/dist/{src/lib → lib}/ast/minify.js +932 -354
  23. package/dist/lib/ast/mixins/custom-function.js +393 -0
  24. package/dist/lib/ast/mixins/utils.js +53 -0
  25. package/dist/lib/ast/mixins/variables.js +678 -0
  26. package/dist/lib/ast/strings.js +906 -0
  27. package/dist/lib/ast/transform/compute.js +560 -0
  28. package/dist/lib/ast/transform/matrix.js +67 -0
  29. package/dist/lib/ast/transform/minify.js +325 -0
  30. package/dist/lib/ast/transform/perspective.js +24 -0
  31. package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
  32. package/dist/lib/ast/transform/scale.js +43 -0
  33. package/dist/lib/ast/transform/skew.js +38 -0
  34. package/dist/lib/ast/transform/translate.js +56 -0
  35. package/dist/lib/ast/transform/utils.js +390 -0
  36. package/dist/{src/lib → lib}/ast/types.js +7 -15
  37. package/dist/{src/lib → lib}/ast/walk.js +19 -19
  38. package/dist/{src/lib → lib}/fs/resolve.js +15 -15
  39. package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
  40. package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
  41. package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
  42. package/dist/lib/parser/input.js +85 -0
  43. package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
  44. package/dist/{src/lib → lib}/parser/parse.js +852 -619
  45. package/dist/lib/parser/source.js +71 -0
  46. package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
  47. package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
  48. package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
  49. package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
  50. package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +31 -13
  51. package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
  52. package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
  53. package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
  54. package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
  55. package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
  56. package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
  57. package/dist/lib/parser/utils/cache.js +53 -0
  58. package/dist/lib/parser/utils/canonical.js +78 -0
  59. package/dist/lib/parser/utils/codes.js +93 -0
  60. package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
  61. package/dist/{src/lib → lib}/parser/utils/declaration.js +67 -49
  62. package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
  63. package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
  64. package/dist/lib/parser/utils/selector.js +720 -0
  65. package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
  66. package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
  67. package/dist/{src/lib → lib}/parser/utils/type.js +11 -9
  68. package/dist/{src/lib → lib}/printer/render.js +536 -243
  69. package/dist/lib/printer/result.js +148 -0
  70. package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
  71. package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
  72. package/dist/lib/printer/writer.js +252 -0
  73. package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
  74. package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
  75. package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
  76. package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
  77. package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
  78. package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
  79. package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
  80. package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
  81. package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
  82. package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
  83. package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
  84. package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
  85. package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
  86. package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
  87. package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
  88. package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
  89. package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
  90. package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
  91. package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
  92. package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
  93. package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
  94. package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
  95. package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
  96. package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
  97. package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
  98. package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
  99. package/dist/{src/lib → lib}/syntax/constants.js +30 -3
  100. package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
  101. package/dist/lib/validation/config.js +173 -0
  102. package/dist/lib/validation/first.js +571 -0
  103. package/dist/{src/lib → lib}/validation/match.js +1256 -579
  104. package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
  105. package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
  106. package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
  107. package/dist/{src/node.js → node.js} +92 -80
  108. package/dist/{src/types.js → types.js} +2 -2
  109. package/dist/{src/utils → utils}/sync.js +13 -8
  110. package/dist/version.d.ts +4 -0
  111. package/dist/version.js +13 -0
  112. package/dist/{src/web.js → web.js} +80 -75
  113. package/package.json +13 -13
  114. package/dist/package.json.js +0 -10
  115. package/dist/src/lib/ast/features/shorthand.js +0 -87
  116. package/dist/src/lib/ast/features/transform.js +0 -85
  117. package/dist/src/lib/ast/transform/compute.js +0 -358
  118. package/dist/src/lib/ast/transform/matrix.js +0 -88
  119. package/dist/src/lib/ast/transform/minify.js +0 -267
  120. package/dist/src/lib/ast/transform/perspective.js +0 -16
  121. package/dist/src/lib/ast/transform/scale.js +0 -39
  122. package/dist/src/lib/ast/transform/skew.js +0 -30
  123. package/dist/src/lib/ast/transform/translate.js +0 -39
  124. package/dist/src/lib/ast/transform/utils.js +0 -287
  125. package/dist/src/lib/parser/source.js +0 -127
  126. package/dist/src/lib/parser/utils/cache.js +0 -54
  127. package/dist/src/lib/parser/utils/selector.js +0 -440
  128. package/dist/src/lib/validation/config.js +0 -120
@@ -1,127 +0,0 @@
1
- /*
2
- CSS parser, minifier and validator for node and the browser
3
-
4
- Author(s): Thierry Bela
5
- Version: 2.0.1
6
- License: MIT
7
- */
8
- import { SourceMap } from '../printer/sourcemap/sourcemap.js';
9
- import { LineMap } from './linesmap.js';
10
-
11
- /**
12
- * Source file ID
13
- */
14
- let sourceId = 0;
15
- /**
16
- * Source file helper class
17
- */
18
- class SourceFile {
19
- /**
20
- * Source file ID
21
- */
22
- id;
23
- /**
24
- * Source file path
25
- */
26
- file;
27
- /**
28
- * Line map
29
- */
30
- lineStarts;
31
- /**
32
- * Source file content
33
- */
34
- content;
35
- inputSourceMap = null;
36
- /**
37
- * Constructor
38
- * @param content
39
- * @param lines
40
- * @param file
41
- */
42
- constructor(content, lines, file = null) {
43
- this.id = sourceId++;
44
- this.content = content;
45
- this.file = file;
46
- this.lineStarts = new LineMap(lines);
47
- }
48
- /**
49
- * Update source content
50
- * @param content
51
- */
52
- append(content) {
53
- this.content += content;
54
- }
55
- /**
56
- * get file name
57
- * @returns
58
- */
59
- getFileName() {
60
- return this.file;
61
- }
62
- /**
63
- * get content
64
- * @returns
65
- */
66
- getContent() {
67
- return this.content;
68
- }
69
- /**
70
- * get text
71
- * @param start
72
- * @param length
73
- * @returns
74
- */
75
- getText(start, length) {
76
- return this.content.slice(start, start + length);
77
- }
78
- /**
79
- * Compute line and column of the offset
80
- * @param offset
81
- * @returns
82
- */
83
- getOffsets(offset) {
84
- return this.lineStarts.getOffsets(offset);
85
- }
86
- /**
87
- * get source location
88
- * @param offset
89
- * @returns
90
- */
91
- getSourceLocation(offset) {
92
- // inlined: this.getOffsets(offset)
93
- return [this.file, ...this.lineStarts.getOffsets(offset)];
94
- }
95
- /**
96
- * get line starts
97
- * @returns
98
- */
99
- getLineStarts() {
100
- // inlined: this.lineStarts.getLineStarts()
101
- return this.lineStarts.lineStarts;
102
- }
103
- /**
104
- * add line start
105
- * @param lineStart
106
- */
107
- addLineStart(lineStart) {
108
- // inlined: this.lineStarts.addLineStart(lineStart)
109
- this.lineStarts.lineStarts.push(lineStart);
110
- }
111
- /**
112
- * set input source map
113
- * @param inputSourceMap
114
- */
115
- setInputSourceMap(inputSourceMap) {
116
- this.inputSourceMap = inputSourceMap == null ? null : new SourceMap(inputSourceMap);
117
- }
118
- /**
119
- * return input source map
120
- * @returns
121
- */
122
- getInputSourceMap() {
123
- return this.inputSourceMap;
124
- }
125
- }
126
-
127
- export { SourceFile };
@@ -1,54 +0,0 @@
1
- /*
2
- CSS parser, minifier and validator for node and the browser
3
-
4
- Author(s): Thierry Bela
5
- Version: 2.0.1
6
- License: MIT
7
- */
8
- /**
9
- *
10
- * @param fn
11
- * @returns
12
- */
13
- function memoize(fn) {
14
- const buckets = new Map();
15
- return function (...args) {
16
- const n = args.length;
17
- let bucket = buckets.get(n);
18
- if (bucket == null) {
19
- bucket = new Map();
20
- buckets.set(n, bucket);
21
- }
22
- if (n === 1) {
23
- const key = args[0];
24
- const value = bucket.get(key);
25
- if (value != null)
26
- return bucket.get(key);
27
- const result = fn(key);
28
- bucket.set(key, result);
29
- return result;
30
- }
31
- let node = bucket;
32
- for (let i = 0; i < n; i++) {
33
- const key = args[i];
34
- const next = node.get(key);
35
- if (next == null) {
36
- const newNode = new Map();
37
- node.set(key, newNode);
38
- node = newNode;
39
- }
40
- else {
41
- node = next;
42
- }
43
- }
44
- const last = n === 0 ? undefined : args[n - 1];
45
- const value = node.get(last);
46
- if (value != null)
47
- return value;
48
- const result = fn(...args);
49
- node.set(last, result);
50
- return result;
51
- };
52
- }
53
-
54
- export { memoize };
@@ -1,440 +0,0 @@
1
- /*
2
- CSS parser, minifier and validator for node and the browser
3
-
4
- Author(s): Thierry Bela
5
- Version: 2.0.1
6
- License: MIT
7
- */
8
- import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
9
- import { renderValue } from '../../printer/render.js';
10
- import { pseudoElements, tokensfuncDefMap, combinators } from '../../syntax/constants.js';
11
- import { isHash } from '../../syntax/syntax.js';
12
- import { getParsedSyntax, getSyntaxRule, getSyntaxConfig } from '../../validation/config.js';
13
- import { matchAllSyntaxes, createValidationContext, trimArray, matchSelectorSyntax } from '../../validation/match.js';
14
- import { ValidationSyntaxGroupEnum, ValidationTokenEnum } from '../../validation/parser/typedef.js';
15
- import { splitTokenList } from '../../validation/utils/list.js';
16
- import { trimWhiteSpace } from '../parse.js';
17
- import { equalsIgnoreCase } from './text.js';
18
-
19
- /**
20
- * minify the an+b expression of a pseudo class function
21
- * @param arena
22
- * @param func
23
- */
24
- function minifyNthExpression(arena, func) {
25
- const chi = arena.tokens(func);
26
- const list = [];
27
- let index;
28
- for (index = 0; index < chi.length; index++) {
29
- if (arena.dataTypes[chi[index]] == EnumToken.CommentTokenType ||
30
- arena.dataTypes[chi[index]] == EnumToken.WhitespaceTokenType) {
31
- continue;
32
- }
33
- if (arena.dataTypes[chi[index]] == EnumToken.IdenTokenType &&
34
- equalsIgnoreCase("of", arena.strings[arena.stringValue[chi[index]]])) {
35
- index--;
36
- break;
37
- }
38
- list.push(chi[index]);
39
- }
40
- if (list.length == 2) {
41
- if (arena.dataTypes[list[1]] == EnumToken.NumberTokenType) {
42
- if (arena.numericValue[list[1]] == 0) {
43
- list.length = 1;
44
- if (arena.dataTypes[list[0]] == EnumToken.DimensionTokenType && arena.numericValue[list[0]] == -2) {
45
- arena.numericValue[list[0]] = 2;
46
- }
47
- }
48
- else {
49
- const sign = Math.sign(arena.numericValue[list[1]]);
50
- arena.numericValue[list[1]] *= sign;
51
- list.splice(1, 0, arena.createToken(EnumToken.LiteralTokenType, sign > 0 ? "+" : "-"));
52
- }
53
- }
54
- if (list.length == 3 &&
55
- arena.dataTypes[list[2]] == EnumToken.NumberTokenType &&
56
- arena.dataTypes[list[0]] == EnumToken.DimensionTokenType &&
57
- (arena.numericValue[list[0]] == 2 || arena.numericValue[list[0]] == -2)) {
58
- if (1 == arena.numericValue[list[2]]) {
59
- const ident = arena.createToken(EnumToken.IdenTokenType, "odd");
60
- arena.copyLocation(ident, list[0]);
61
- list.splice(0, 3, ident);
62
- }
63
- else if (0 == arena.numericValue[list[2]]) {
64
- const ident = arena.createToken(EnumToken.IdenTokenType, "even");
65
- arena.copyLocation(ident, list[0]);
66
- list.splice(0, 3, ident);
67
- }
68
- }
69
- chi.splice(0, index, ...list);
70
- }
71
- if (list.length == 1) {
72
- if (arena.dataTypes[list[0]] == EnumToken.IdenTokenType &&
73
- equalsIgnoreCase("-n", arena.strings[arena.stringValue[list[0]]])) {
74
- // inlined: arena.setStr(list[0], "n")
75
- arena.stringValue[list[0]] = arena.internString("n");
76
- }
77
- }
78
- if (list.length == 3) {
79
- if (arena.dataTypes[list[0]] == EnumToken.IdenTokenType &&
80
- ("n" == arena.strings[arena.stringValue[list[0]]] ||
81
- "-n" == arena.strings[arena.stringValue[list[0]]] ||
82
- "+n" == arena.strings[arena.stringValue[list[0]]])) {
83
- if (arena.dataTypes[list[1]] == EnumToken.NextSiblingCombinatorTokenType) {
84
- if (arena.dataTypes[list[2]] == EnumToken.NumberTokenType && 0 == arena.numericValue[list[2]]) {
85
- // inlined: arena.setStr(list[0], "n")
86
- arena.stringValue[list[0]] = arena.internString("n");
87
- chi.splice(0, index, list[0]);
88
- arena.setTokenList(func, chi);
89
- return;
90
- }
91
- }
92
- }
93
- }
94
- const token = chi.find((t) => arena.dataTypes[t] != EnumToken.WhitespaceTokenType && arena.dataTypes[t] != EnumToken.CommentTokenType);
95
- if (token != null) {
96
- if (arena.dataTypes[token] == EnumToken.IdenTokenType || arena.dataTypes[token] == EnumToken.LiteralTokenType) {
97
- const val = arena.strings[arena.stringValue[token]];
98
- if (arena.dataTypes[token] == EnumToken.IdenTokenType && (val == "odd" || val == "even")) {
99
- if (val == "even") {
100
- arena.resetToken(token, EnumToken.DimensionTokenType, 2, "n");
101
- }
102
- }
103
- else {
104
- const matches = /^(([+-]?[0-9]*)?n)?([+-]?[0-9]+)?$/.exec(val);
105
- if (matches != null) {
106
- const a1 = matches[2] === "" ? 1 : matches[2] === "-" ? -1 : +matches[2];
107
- const b1 = +matches[3];
108
- if (b1 === 0) {
109
- if (Math.abs(a1) === 1) {
110
- arena.resetToken(token, EnumToken.IdenTokenType, "n");
111
- }
112
- else {
113
- arena.resetToken(token, EnumToken.DimensionTokenType, a1 * Math.sign(a1), "n");
114
- }
115
- }
116
- }
117
- }
118
- }
119
- else if (arena.dataTypes[token] === EnumToken.DimensionTokenType) {
120
- const index = chi.indexOf(token);
121
- let num = null;
122
- let i = index + 1;
123
- for (; i < chi.length; i++) {
124
- if (arena.dataTypes[chi[i]] == EnumToken.WhitespaceTokenType ||
125
- arena.dataTypes[chi[i]] == EnumToken.CommentTokenType) {
126
- continue;
127
- }
128
- if (arena.dataTypes[chi[i]] == EnumToken.NumberTokenType) {
129
- num = chi[i];
130
- break;
131
- }
132
- }
133
- if (num != null) {
134
- if (arena.numericValue[num] === 0) {
135
- chi.splice(index + 1, i - index);
136
- if (arena.numericValue[token] < 0) {
137
- arena.numericValue[token] *= -1;
138
- }
139
- }
140
- else if (arena.numericValue[token] === 2 && Math.abs(arena.numericValue[num]) === 1) {
141
- arena.resetToken(token, EnumToken.IdenTokenType, "odd");
142
- chi.splice(index + 1, i - index);
143
- }
144
- }
145
- }
146
- }
147
- arena.setTokenList(func, chi);
148
- }
149
- /**
150
- * parse selector
151
- * @param arena
152
- * @param tokens
153
- * @param context parent node id
154
- * @param options
155
- * @param errors
156
- */
157
- function parseSelector(arena, tokens, context, options, errors) {
158
- const contextType = context == null ? null : arena.dataTypes[context];
159
- if (contextType === EnumToken.KeyframesAtRuleNodeType) {
160
- const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "keyframe-selectors"), createValidationContext(arena, tokens), options);
161
- const parts = splitTokenList(arena, tokens);
162
- for (const part of parts) {
163
- trimArray(arena, part);
164
- if (options.minify) {
165
- const filtered = part.filter((token) => arena.dataTypes[token] !== EnumToken.WhitespaceTokenType && arena.dataTypes[token] !== EnumToken.CommentTokenType);
166
- if (filtered.length === 1) {
167
- if (arena.dataTypes[filtered[0]] === EnumToken.IdenTokenType &&
168
- "from" === arena.strings[arena.stringValue[filtered[0]]].toLowerCase()) {
169
- arena.resetToken(filtered[0], EnumToken.PercentageTokenType, 0);
170
- }
171
- else if (arena.dataTypes[filtered[0]] === EnumToken.PercentageTokenType &&
172
- 100 === arena.numericValue[filtered[0]]) {
173
- arena.resetToken(filtered[0], EnumToken.IdenTokenType, "to");
174
- }
175
- part.splice(0, part.length, ...filtered);
176
- }
177
- }
178
- }
179
- tokens.splice(0, tokens.length, ...parts.reduce((acc, curr) => {
180
- if (acc.length > 0) {
181
- acc.push(arena.createToken(EnumToken.CommaTokenType));
182
- }
183
- for (const c of curr) {
184
- acc.push(c);
185
- }
186
- return acc;
187
- }, []));
188
- const node = arena.createRule(EnumToken.KeyframesRuleNodeType, [
189
- ...splitTokenList(arena, trimArray(arena, tokens)).reduce((acc, curr) => {
190
- acc.add(curr.reduce((acc, curr) => acc + renderValue(arena, curr, { minify: false }), ""));
191
- return acc;
192
- }, new Set()),
193
- ].join(), tokens.length === 0 ? null : tokens, result.success ? EnumAstNodeStatus.Validated : EnumAstNodeStatus.Invalid, result.errors);
194
- // inlined: arena.getSource(tokens[0])
195
- arena.setLocation(node, arena.sources[arena.getSourceId(tokens[0])] ?? 0, arena.getSpanStart(tokens[0]), arena.getSpanEnd(tokens[tokens.length - 1]));
196
- return node;
197
- }
198
- const stack = [];
199
- let allowed = true;
200
- let index;
201
- let parent = context;
202
- let nested = false;
203
- let val;
204
- if (contextType !== EnumToken.StyleSheetNodeType && contextType !== EnumToken.RuleNodeType) {
205
- allowed = false;
206
- if (contextType === EnumToken.AtRuleNodeType) {
207
- const syntaxRule = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.strings[arena.name[context]]);
208
- allowed = syntaxRule?.acceptAnyRule ?? false;
209
- if (!allowed) {
210
- const rules = syntaxRule?.getBlockRules?.();
211
- if (rules != null) {
212
- for (const rule of rules) {
213
- if (ValidationTokenEnum.PropertyType === rule.typ) {
214
- if ("block-contents" === rule.val ||
215
- "rule-list" === rule.val) {
216
- allowed = true;
217
- break;
218
- }
219
- }
220
- }
221
- }
222
- }
223
- }
224
- }
225
- while (parent != null && parent > 0) {
226
- if (arena.dataTypes[parent] === EnumToken.AtRuleNodeType && "media" === arena.strings[arena.name[parent]]) {
227
- parent = arena.parents[parent];
228
- continue;
229
- }
230
- nested = arena.dataTypes[parent] == EnumToken.RuleNodeType;
231
- if (nested) {
232
- break;
233
- }
234
- parent = arena.parents[parent];
235
- }
236
- const all = new Array(tokens.length);
237
- let write = 0;
238
- for (let read = 0; read < tokens.length; read++) {
239
- let token = tokens[read];
240
- if (arena.dataTypes[token] == EnumToken.ColonTokenType) {
241
- const next = tokens[read + 1];
242
- if (arena.dataTypes[next] == EnumToken.IdenTokenType) {
243
- arena.dataTypes[token] = EnumToken.PseudoElementTokenType;
244
- // inlined: arena.setStr(token, ":" + arena.strings[arena.stringValue[next]])
245
- arena.stringValue[token] = arena.internString((":" + arena.strings[arena.stringValue[next]]));
246
- arena.spanLocation(token, token, next);
247
- all[write++] = token;
248
- read++;
249
- continue;
250
- }
251
- if (arena.dataTypes[next] == EnumToken.FunctionTokenDefType) {
252
- val = ":" + arena.strings[arena.stringValue[next]];
253
- arena.dataTypes[token] =
254
- val + "()" in getSyntaxConfig().selectors
255
- ? EnumToken.PseudoClassFunctionTokenDefType
256
- : arena.dataTypes[next];
257
- // inlined: arena.setStr(token, val)
258
- arena.stringValue[token] = arena.internString(val);
259
- arena.spanLocation(token, token, next);
260
- all[write++] = token;
261
- read++;
262
- continue;
263
- }
264
- }
265
- if (arena.dataTypes[token] == EnumToken.DoubleColonTokenType) {
266
- const next = tokens[read + 1];
267
- val = ":" + arena.strings[arena.stringValue[next]];
268
- if (arena.dataTypes[next] == EnumToken.IdenTokenType) {
269
- arena.dataTypes[token] = EnumToken.PseudoClassTokenType;
270
- // inlined: arena.setStr(token, (pseudoElements.includes(val) ? "" : ":") + val)
271
- arena.stringValue[token] = arena.internString(((pseudoElements.includes(val) ? "" : ":") + val));
272
- arena.spanLocation(token, token, next);
273
- all[write++] = token;
274
- read++;
275
- continue;
276
- }
277
- if (arena.dataTypes[next] == EnumToken.FunctionTokenDefType) {
278
- val = "::" + arena.strings[arena.stringValue[next]];
279
- arena.dataTypes[token] =
280
- val + "()" in getSyntaxConfig().selectors
281
- ? EnumToken.PseudoClassFunctionTokenDefType
282
- : EnumToken.FunctionTokenDefType;
283
- // inlined: arena.setStr(token, val)
284
- arena.stringValue[token] = arena.internString(val);
285
- arena.spanLocation(token, token, next);
286
- all[write++] = token;
287
- read++;
288
- continue;
289
- }
290
- }
291
- if (arena.dataTypes[token] == EnumToken.ColorTokenType) {
292
- if (isHash(arena.strings[arena.stringValue[token]])) {
293
- arena.dataTypes[token] = EnumToken.HashTokenType;
294
- all[write++] = token;
295
- continue;
296
- }
297
- const node = arena.createRule(EnumToken.RuleNodeType, renderSelector(arena, tokens), tokens, EnumAstNodeStatus.Invalid, [
298
- {
299
- action: "drop",
300
- node: token,
301
- message: "invalid hash id",
302
- },
303
- ]);
304
- // inlined: arena.getSource(tokens[0])
305
- arena.setLocation(node, arena.sources[arena.getSourceId(tokens[0])] ?? 0, arena.getSpanStart(tokens[0]), arena.getSpanEnd(tokens[tokens.length - 1]));
306
- return node;
307
- }
308
- all[write++] = token;
309
- }
310
- if (write !== tokens.length) {
311
- for (let index = 0; index < write; index++) {
312
- tokens[index] = all[index];
313
- }
314
- tokens.length = write;
315
- }
316
- const result = matchSelectorSyntax(arena, tokens, errors, options, nested);
317
- trimArray(arena, tokens);
318
- if (result.success) {
319
- for (let i = 0; i < tokens.length; i++) {
320
- const token = tokens[i];
321
- switch (arena.dataTypes[token]) {
322
- case EnumToken.AttrStartTokenType:
323
- stack.push(token);
324
- break;
325
- case EnumToken.AttrEndTokenType:
326
- // inlined: arena.typeOf(stack.at(-1))
327
- if (arena.dataTypes[stack.at(-1)] == EnumToken.AttrStartTokenType) {
328
- const attr = stack[stack.length - 1];
329
- index = tokens.indexOf(attr);
330
- arena.spanLocation(attr, attr, token);
331
- tokens.splice(i, 1);
332
- arena.dataTypes[attr] = EnumToken.AttrTokenType;
333
- arena.stringValue[attr] = 0;
334
- arena.setTokenList(attr, tokens.splice(index + 1, i - index - 1));
335
- i = index;
336
- stack.pop();
337
- }
338
- break;
339
- case EnumToken.PseudoClassFunctionTokenDefType:
340
- stack.push(token);
341
- break;
342
- case EnumToken.EndParensTokenType:
343
- // inlined: arena.typeOf(stack.at(-1))
344
- if (arena.dataTypes[stack.at(-1)] == EnumToken.PseudoClassFunctionTokenDefType) {
345
- const func = stack[stack.length - 1];
346
- index = tokens.indexOf(func);
347
- arena.spanLocation(func, func, token);
348
- tokens.splice(i, 1);
349
- if (tokensfuncDefMap.has(arena.dataTypes[func])) {
350
- arena.dataTypes[func] = tokensfuncDefMap.get(arena.dataTypes[func]);
351
- arena.setTokenList(func, trimArray(arena, tokens.splice(index + 1, i - index - 1)));
352
- }
353
- if (result.success && options.minify) {
354
- // parse an+b
355
- // an+b produces an ident such as 'n-0', a literal such as '+2n-0' or a list of tokens such as[2n\s?[+-]\s?3]
356
- const name = arena.strings[arena.stringValue[func]];
357
- if (name == ":nth-child" ||
358
- name == ":nth-last-child" ||
359
- name == ":nth-of-type" ||
360
- name == ":nth-last-of-type") {
361
- minifyNthExpression(arena, func);
362
- }
363
- }
364
- i = index;
365
- stack.pop();
366
- }
367
- break;
368
- }
369
- }
370
- }
371
- const node = arena.createRule(EnumToken.RuleNodeType, renderSelector(arena, tokens), tokens, result.success && allowed
372
- ? EnumAstNodeStatus.Validated
373
- : allowed
374
- ? EnumAstNodeStatus.Invalid
375
- : EnumAstNodeStatus.Disallowed, result.success ? [] : result.errors);
376
- // inlined: arena.getSource(tokens[0])
377
- arena.setLocation(node, arena.sources[arena.getSourceId(tokens[0])] ?? 0, arena.getSpanStart(tokens[0]), arena.getSpanEnd(tokens[tokens.length - 1]));
378
- return node;
379
- }
380
- // options of the rendering of the selector tokens
381
- const selectorRenderOptions = { minify: false };
382
- /**
383
- * selector of a rule: the distinct comma separated selectors, the whitespace around the combinators is removed.
384
- * a universal selector is removed before a compound selector
385
- * @param arena
386
- * @param tokens
387
- */
388
- function renderSelector(arena, tokens) {
389
- // the distinct selectors, in order
390
- const selectors = new Set();
391
- // rendered tokens of the current selector
392
- let part = [];
393
- for (let index = 0; index < tokens.length; index++) {
394
- const curr = tokens[index];
395
- if (arena.dataTypes[curr] == EnumToken.WhitespaceTokenType) {
396
- if (trimWhiteSpace.includes(arena.dataTypes[tokens[index - 1]]) ||
397
- trimWhiteSpace.includes(arena.dataTypes[tokens[index + 1]]) ||
398
- // inlined: arena.str(tokens[index - 1])
399
- combinators.includes(arena.strings[arena.stringValue[tokens[index - 1]]]) ||
400
- // inlined: arena.str(tokens[index + 1])
401
- combinators.includes(arena.strings[arena.stringValue[tokens[index + 1]]])) {
402
- continue;
403
- }
404
- }
405
- const t = renderValue(arena, curr, selectorRenderOptions);
406
- if (t == ",") {
407
- addSelector(selectors, part);
408
- part = [];
409
- }
410
- else {
411
- part.push(t);
412
- }
413
- }
414
- addSelector(selectors, part);
415
- let result = "";
416
- let first = true;
417
- for (const selector of selectors) {
418
- result = first ? selector : result + "," + selector;
419
- first = false;
420
- }
421
- return result;
422
- }
423
- /**
424
- * add a selector from its rendered tokens
425
- * @param selectors
426
- * @param curr
427
- */
428
- function addSelector(selectors, curr) {
429
- for (let i = 0; i < curr.length; i++) {
430
- if (i + 1 < curr.length && curr[i] == "*") {
431
- const index = curr[i + 1] == " " ? 2 : 1;
432
- if (![">", "~", "+"].includes(curr[index])) {
433
- curr.splice(i, index);
434
- }
435
- }
436
- }
437
- selectors.add(curr.join(""));
438
- }
439
-
440
- export { parseSelector };