@tbela99/css-parser 1.8.2 → 2.0.1
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.
- package/CHANGELOG.md +66 -0
- package/README.md +129 -31
- package/dist/index-umd-web.js +27383 -24083
- package/dist/index.d.ts +2737 -2200
- package/dist/package.json.js +2 -2
- package/dist/src/data/properties.json.js +1 -1
- package/dist/src/data/validation.json.js +1 -1
- package/dist/src/lib/ast/arena-node.js +548 -0
- package/dist/src/lib/ast/arena.js +3198 -0
- package/dist/src/lib/ast/cache.js +333 -0
- package/dist/src/lib/ast/features/calc.js +85 -76
- package/dist/src/lib/ast/features/if.js +155 -115
- package/dist/src/lib/ast/features/index.js +1 -1
- package/dist/src/lib/ast/features/inlinecssvariables.js +77 -84
- package/dist/src/lib/ast/features/prefix.js +184 -183
- package/dist/src/lib/ast/features/shorthand.js +40 -46
- package/dist/src/lib/ast/features/transform.js +24 -24
- package/dist/src/lib/ast/features/type.js +1 -1
- package/dist/src/lib/ast/math/expression.js +458 -543
- package/dist/src/lib/ast/math/math.js +19 -41
- package/dist/src/lib/ast/minify.js +590 -647
- package/dist/src/lib/ast/transform/compute.js +82 -85
- package/dist/src/lib/ast/transform/matrix.js +21 -38
- package/dist/src/lib/ast/transform/minify.js +82 -193
- package/dist/src/lib/ast/transform/perspective.js +2 -3
- package/dist/src/lib/ast/transform/rotate.js +1 -1
- package/dist/src/lib/ast/transform/scale.js +1 -1
- package/dist/src/lib/ast/transform/skew.js +1 -1
- package/dist/src/lib/ast/transform/translate.js +1 -1
- package/dist/src/lib/ast/transform/utils.js +2 -2
- package/dist/src/lib/ast/types.js +263 -258
- package/dist/src/lib/ast/walk.js +246 -610
- package/dist/src/lib/fs/resolve.js +1 -1
- package/dist/src/lib/parser/declaration/list.js +223 -139
- package/dist/src/lib/parser/declaration/map.js +251 -238
- package/dist/src/lib/parser/declaration/set.js +74 -66
- package/dist/src/lib/parser/linesmap.js +1 -1
- package/dist/src/lib/parser/parse.js +1379 -1658
- package/dist/src/lib/parser/source.js +7 -4
- package/dist/src/lib/parser/tokenize.js +406 -199
- package/dist/src/lib/parser/utils/angle.js +11 -13
- package/dist/src/lib/parser/utils/at-rule-container.js +183 -168
- package/dist/src/lib/parser/utils/at-rule-font-feature-values.js +5 -5
- package/dist/src/lib/parser/utils/at-rule-generic.js +31 -24
- package/dist/src/lib/parser/utils/at-rule-import.js +44 -44
- package/dist/src/lib/parser/utils/at-rule-media.js +216 -205
- package/dist/src/lib/parser/utils/at-rule-support.js +83 -88
- package/dist/src/lib/parser/utils/at-rule-when-else.js +47 -47
- package/dist/src/lib/parser/utils/at-rule.js +5 -5
- package/dist/src/lib/parser/utils/base64.js +2 -2
- package/dist/src/lib/parser/utils/cache.js +1 -1
- package/dist/src/lib/parser/utils/config.js +1 -1
- package/dist/src/lib/parser/utils/declaration.js +233 -234
- package/dist/src/lib/parser/utils/eq.js +50 -3
- package/dist/src/lib/parser/utils/hash.js +2 -35
- package/dist/src/lib/parser/utils/selector.js +309 -387
- package/dist/src/lib/parser/utils/text.js +18 -4
- package/dist/src/lib/parser/utils/token.js +16 -69
- package/dist/src/lib/parser/utils/type.js +37 -24
- package/dist/src/lib/printer/render.js +965 -511
- package/dist/src/lib/printer/sourcemap/lib/codec.js +1 -1
- package/dist/src/lib/printer/sourcemap/sourcemap.js +1 -1
- package/dist/src/lib/syntax/color/a98rgb.js +1 -1
- package/dist/src/lib/syntax/color/alpha.js +36 -37
- package/dist/src/lib/syntax/color/cmyk.js +35 -52
- package/dist/src/lib/syntax/color/color-mix.js +96 -132
- package/dist/src/lib/syntax/color/color.js +222 -231
- package/dist/src/lib/syntax/color/hex.js +45 -55
- package/dist/src/lib/syntax/color/hsl.js +54 -62
- package/dist/src/lib/syntax/color/hwb.js +60 -68
- package/dist/src/lib/syntax/color/lab.js +59 -67
- package/dist/src/lib/syntax/color/lch.js +59 -67
- package/dist/src/lib/syntax/color/oklab.js +60 -68
- package/dist/src/lib/syntax/color/oklch.js +60 -67
- package/dist/src/lib/syntax/color/p3.js +1 -1
- package/dist/src/lib/syntax/color/prophotorgb.js +1 -1
- package/dist/src/lib/syntax/color/rec2020.js +1 -1
- package/dist/src/lib/syntax/color/relative-color.js +94 -144
- package/dist/src/lib/syntax/color/rgb.js +54 -57
- package/dist/src/lib/syntax/color/srgb.js +55 -53
- package/dist/src/lib/syntax/color/utils/color-type.js +13 -10
- package/dist/src/lib/syntax/color/utils/colorspace.js +11 -11
- package/dist/src/lib/syntax/color/utils/components.js +19 -20
- package/dist/src/lib/syntax/color/utils/distance.js +15 -13
- package/dist/src/lib/syntax/color/utils/make-color.js +15 -19
- package/dist/src/lib/syntax/color/utils/matrix.js +2 -2
- package/dist/src/lib/syntax/color/xyz.js +1 -1
- package/dist/src/lib/syntax/color/xyzd50.js +1 -1
- package/dist/src/lib/syntax/constants.js +2 -16
- package/dist/src/lib/syntax/syntax.js +301 -407
- package/dist/src/lib/validation/config.js +1 -1
- package/dist/src/lib/validation/match.js +959 -723
- package/dist/src/lib/validation/parser/parse.js +230 -112
- package/dist/src/lib/validation/parser/typedef.js +1 -1
- package/dist/src/lib/validation/utils/list.js +27 -12
- package/dist/src/node.js +12 -72
- package/dist/src/types.js +1 -1
- package/dist/src/utils/sync.js +9 -6
- package/dist/src/web.js +11 -73
- package/package.json +9 -10
- package/dist/index.cjs +0 -35870
- package/dist/src/lib/ast/clone.js +0 -51
- package/dist/src/lib/ast/expand.js +0 -311
- package/dist/src/lib/ast/find.js +0 -192
- package/dist/src/lib/ast/node.js +0 -59
|
@@ -2,17 +2,18 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.1
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { EnumToken } from '../../ast/types.js';
|
|
9
|
-
import { pseudoElements,
|
|
9
|
+
import { pseudoElements, 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';
|
|
13
13
|
import { parseDeclaration } from './declaration.js';
|
|
14
|
+
import { TokenField } from '../../ast/arena.js';
|
|
14
15
|
|
|
15
|
-
function parseAtRuleSupportSyntax(stream, context, options = {}) {
|
|
16
|
+
function parseAtRuleSupportSyntax(arena, stream, context, options = {}) {
|
|
16
17
|
const tokens = [];
|
|
17
18
|
const stack = [];
|
|
18
19
|
let i = 0;
|
|
@@ -32,38 +33,40 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
|
|
|
32
33
|
]);
|
|
33
34
|
for (; i < stream.length; i++) {
|
|
34
35
|
tokens.push(stream[i]);
|
|
35
|
-
if (stream[i]
|
|
36
|
-
if (stream[i + 1]
|
|
37
|
-
const val = stream[i + 1]
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
stream[i][
|
|
36
|
+
if (arena.dataTypes[stream[i]] == EnumToken.ColonTokenType) {
|
|
37
|
+
if (arena.dataTypes[stream[i + 1]] == EnumToken.IdenTokenType) {
|
|
38
|
+
const val = arena.strings[arena.stringValue[stream[i + 1]]];
|
|
39
|
+
arena.dataTypes[stream[i]] = pseudoElements.includes(val)
|
|
40
|
+
? EnumToken.PseudoElementTokenType
|
|
41
|
+
: EnumToken.PseudoClassTokenType;
|
|
42
|
+
// inlined: arena.setStr(stream[i], ":" + val)
|
|
43
|
+
arena.stringValue[stream[i]] = arena.internString((":" + val));
|
|
44
|
+
// inlined: arena.setSpanEnd(stream[i], arena.getSpanEnd(stream[i + 1]))
|
|
45
|
+
arena.setSpan(stream[i], arena.getSourceId(stream[i]), arena.getSpanStart(stream[i]), arena.getSpanEnd(stream[i + 1]));
|
|
45
46
|
stream.splice(i + 1, 1);
|
|
46
47
|
continue;
|
|
47
48
|
}
|
|
48
|
-
if (stream[i + 1]
|
|
49
|
-
val = ":" + stream[i + 1]
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
if (arena.dataTypes[stream[i + 1]] == EnumToken.FunctionTokenDefType) {
|
|
50
|
+
val = ":" + arena.strings[arena.stringValue[stream[i + 1]]];
|
|
51
|
+
arena.dataTypes[stream[i]] =
|
|
52
|
+
val + "()" in getSyntaxConfig().selectors
|
|
52
53
|
? EnumToken.PseudoClassFunctionTokenDefType
|
|
53
|
-
: stream[i + 1]
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
: arena.dataTypes[stream[i + 1]];
|
|
55
|
+
// inlined: arena.setStr(stream[i], val)
|
|
56
|
+
arena.stringValue[stream[i]] = arena.internString(val);
|
|
56
57
|
stack.push(stream[i]);
|
|
57
|
-
stream[i]
|
|
58
|
+
// inlined: arena.setSpanEnd(stream[i], arena.getSpanEnd(stream[i + 1]))
|
|
59
|
+
arena.setSpan(stream[i], arena.getSourceId(stream[i]), arena.getSpanStart(stream[i]), arena.getSpanEnd(stream[i + 1]));
|
|
58
60
|
stream.splice(i + 1, 1);
|
|
59
61
|
continue;
|
|
60
62
|
}
|
|
61
63
|
}
|
|
62
|
-
if (trimWhiteSpace.has(stream[i]
|
|
63
|
-
|
|
64
|
+
if (trimWhiteSpace.has(arena.dataTypes[stream[i]])) {
|
|
65
|
+
// inlined: arena.typeOf(tokens.at(-2))
|
|
66
|
+
if (arena.dataTypes[tokens.at(-2)] === EnumToken.WhitespaceTokenType) {
|
|
64
67
|
tokens.splice(tokens.length - 2, 1);
|
|
65
68
|
}
|
|
66
|
-
if (stream[i + 1]
|
|
69
|
+
if (arena.dataTypes[stream[i + 1]] === EnumToken.WhitespaceTokenType) {
|
|
67
70
|
i++;
|
|
68
71
|
continue;
|
|
69
72
|
}
|
|
@@ -71,62 +74,53 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
|
|
|
71
74
|
if (expectAndOr) {
|
|
72
75
|
let k = i;
|
|
73
76
|
while (k < stream.length &&
|
|
74
|
-
(stream[k]
|
|
77
|
+
(arena.dataTypes[stream[k]] === EnumToken.WhitespaceTokenType || arena.dataTypes[stream[k]] === EnumToken.CommentTokenType)) {
|
|
75
78
|
tokens.push(stream[k]);
|
|
76
79
|
k++;
|
|
77
80
|
}
|
|
78
81
|
expectAndOr = false;
|
|
79
82
|
}
|
|
80
|
-
if (stream[i]
|
|
83
|
+
if (arena.dataTypes[stream[i]] === EnumToken.StartParensTokenType || tokensfuncDefMap.has(arena.dataTypes[stream[i]])) {
|
|
81
84
|
scopes.push((scope = new Set()));
|
|
82
85
|
stack.push(stream[i]);
|
|
83
86
|
continue;
|
|
84
87
|
}
|
|
85
|
-
switch (stream[i]
|
|
88
|
+
switch (arena.dataTypes[stream[i]]) {
|
|
86
89
|
case EnumToken.EndParensTokenType:
|
|
87
90
|
{
|
|
88
|
-
if (stack[stack.length - 1]
|
|
91
|
+
if (arena.dataTypes[stack[stack.length - 1]] === EnumToken.ColonTokenType) {
|
|
89
92
|
// match declaration
|
|
90
93
|
const index = tokens.indexOf(stack[stack.length - 2]);
|
|
91
|
-
const slice = trimArray(tokens.splice(index + 1, tokens.length - index - 2));
|
|
92
|
-
const declaration = parseDeclaration(slice, context, { ...options, validation: false }, errors);
|
|
94
|
+
const slice = trimArray(arena, tokens.splice(index + 1, tokens.length - index - 2));
|
|
95
|
+
const declaration = parseDeclaration(arena, slice, context, { ...options, validation: false }, errors);
|
|
93
96
|
tokens.splice(index + 1, 0, declaration);
|
|
94
97
|
stack.pop();
|
|
95
98
|
}
|
|
96
99
|
// match <support-condition-name>
|
|
97
100
|
// @supports (--condition-name) {}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
chi: slice,
|
|
107
|
-
[LOCSRCID]: stack.at(-1)[LOCSRCID],
|
|
108
|
-
[LOCSTA]: stack.at(-1)[LOCSTA],
|
|
109
|
-
[LOCEND]: stream[i]?.[LOCEND],
|
|
110
|
-
};
|
|
101
|
+
// inlined: arena.typeOf(stack.at(-1))
|
|
102
|
+
if (arena.dataTypes[stack.at(-1)] === EnumToken.StartParensTokenType) {
|
|
103
|
+
const parens = stack[stack.length - 1];
|
|
104
|
+
const index = tokens.indexOf(parens);
|
|
105
|
+
const slice = trimArray(arena, tokens.splice(index + 1, tokens.length - index - 2));
|
|
106
|
+
arena.dataTypes[parens] = EnumToken.ParensTokenType;
|
|
107
|
+
arena.setTokenList(parens, slice);
|
|
108
|
+
arena.spanLocation(parens, parens, stream[i]);
|
|
111
109
|
stack.pop();
|
|
112
110
|
tokens.pop();
|
|
113
111
|
scopes.pop();
|
|
114
112
|
scope = scopes[scopes.length - 1];
|
|
115
113
|
expectAndOr = true;
|
|
116
114
|
}
|
|
117
|
-
else if (tokensfuncDefMap.has(stack.
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
[LOCSTA]: stack.at(-1)[LOCSTA],
|
|
125
|
-
[LOCEND]: stream[i]?.[LOCEND],
|
|
126
|
-
};
|
|
127
|
-
if (tokens[index].typ === EnumToken.PseudoClassFuncTokenType) {
|
|
115
|
+
else if (stack.length > 0 && tokensfuncDefMap.has(arena.dataTypes[stack[stack.length - 1]])) {
|
|
116
|
+
const func = stack[stack.length - 1];
|
|
117
|
+
const index = tokens.indexOf(func);
|
|
118
|
+
arena.dataTypes[func] = tokensfuncDefMap.get(arena.dataTypes[func]);
|
|
119
|
+
arena.setTokenList(func, trimArray(arena, tokens.splice(index + 1, tokens.length - index - 2)));
|
|
120
|
+
arena.spanLocation(func, func, stream[i]);
|
|
121
|
+
if (arena.dataTypes[tokens[index]] === EnumToken.PseudoClassFuncTokenType) {
|
|
128
122
|
// not a declaration
|
|
129
|
-
const result = matchAllSyntaxes(getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, tokens[index]
|
|
123
|
+
const result = matchAllSyntaxes(arena, getParsedSyntax(ValidationSyntaxGroupEnum.Selectors, arena.strings[arena.stringValue[tokens[index]]] + "()")?.[0]?.chi, createValidationContext(arena, arena.tokens(tokens[index])), options);
|
|
130
124
|
if (!result.success) {
|
|
131
125
|
return {
|
|
132
126
|
success: false,
|
|
@@ -139,7 +133,7 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
|
|
|
139
133
|
}
|
|
140
134
|
//
|
|
141
135
|
let k = stack.length - 1;
|
|
142
|
-
while (tokensfuncDefMap.has(stack[k]
|
|
136
|
+
while (tokensfuncDefMap.has(arena.dataTypes[stack[k]])) {
|
|
143
137
|
k--;
|
|
144
138
|
}
|
|
145
139
|
stack.pop();
|
|
@@ -147,33 +141,34 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
|
|
|
147
141
|
scopes.pop();
|
|
148
142
|
scope = scopes[scopes.length - 1];
|
|
149
143
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
144
|
+
// inlined: arena.typeOf(stack.at(-1))
|
|
145
|
+
if (arena.dataTypes[stack.at(-1)] === EnumToken.NotTokenType) {
|
|
146
|
+
const not = stack[stack.length - 1];
|
|
147
|
+
const index = tokens.indexOf(not);
|
|
148
|
+
const unary = arena.createToken(EnumToken.SupportsQueryUnaryConditionTokenType);
|
|
149
|
+
// inlined: arena.setField(unary, TokenField.R, ...trimArray(arena, tokens.splice(index + 1, i - index - 1)))
|
|
150
|
+
arena.setTokenList(arena.fieldList(unary, TokenField.R), trimArray(arena, tokens.splice(index + 1, i - index - 1)));
|
|
151
|
+
// inlined: arena.setField(unary, TokenField.L, not)
|
|
152
|
+
arena.setTokenList(arena.fieldList(unary, TokenField.L), [not]);
|
|
153
|
+
arena.spanLocation(unary, not, stream[i]);
|
|
154
|
+
tokens[index] = unary;
|
|
160
155
|
stack.pop();
|
|
161
156
|
}
|
|
162
|
-
|
|
157
|
+
// inlined: arena.typeOf(stack.at(-1))
|
|
158
|
+
if (arena.dataTypes[stack.at(-1)] === EnumToken.AndTokenType || arena.dataTypes[stack.at(-1)] === EnumToken.OrTokenType) {
|
|
163
159
|
const index = tokens.indexOf(stack.at(-1));
|
|
164
160
|
const index2 = stack.length > 1 ? tokens.indexOf(stack.at(-2)) + 1 : 0;
|
|
165
|
-
const
|
|
166
|
-
left
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
};
|
|
161
|
+
const op = stack[stack.length - 1];
|
|
162
|
+
const left = trimArray(arena, tokens.slice(index2, index));
|
|
163
|
+
const condition = arena.createToken(EnumToken.SupportsQueryConditionTokenType);
|
|
164
|
+
// inlined: arena.setField(condition, TokenField.L, ...left)
|
|
165
|
+
arena.setTokenList(arena.fieldList(condition, TokenField.L), left);
|
|
166
|
+
// inlined: arena.setField(condition, TokenField.R, ...trimArray(arena, tokens.slice(index + 1)))
|
|
167
|
+
arena.setTokenList(arena.fieldList(condition, TokenField.R), trimArray(arena, tokens.slice(index + 1)));
|
|
168
|
+
// inlined: arena.setField(condition, TokenField.Op, op)
|
|
169
|
+
arena.setTokenList(arena.fieldList(condition, TokenField.Op), [op]);
|
|
170
|
+
arena.spanLocation(condition, op, stream[i]);
|
|
171
|
+
tokens[index2] = condition;
|
|
177
172
|
tokens.length = index2 + 1;
|
|
178
173
|
stack.pop();
|
|
179
174
|
}
|
|
@@ -184,16 +179,18 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
|
|
|
184
179
|
break;
|
|
185
180
|
case EnumToken.IdenTokenType:
|
|
186
181
|
{
|
|
187
|
-
const val = stream[i].
|
|
182
|
+
const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
|
|
188
183
|
if ("not" === val) {
|
|
189
184
|
stack.push(stream[i]);
|
|
190
|
-
|
|
185
|
+
arena.dataTypes[stream[i]] = EnumToken.NotTokenType;
|
|
191
186
|
break;
|
|
192
187
|
}
|
|
193
188
|
if ("and" === val || "or" === val) {
|
|
194
189
|
if ("or" === val && scopes.length === 1 && scopes[0].size > 0) {
|
|
195
|
-
|
|
196
|
-
const
|
|
190
|
+
// inlined: options.source!.getFileName()
|
|
191
|
+
const fileName = options.source.file ?? "";
|
|
192
|
+
// inlined: options.source!.getOffsets(arena.getSpanStart(stream[i]))
|
|
193
|
+
const [line, column] = options.source.lineStarts.getOffsets(arena.getSpanStart(stream[i]));
|
|
197
194
|
return {
|
|
198
195
|
success: false,
|
|
199
196
|
errors: [
|
|
@@ -205,10 +202,8 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
|
|
|
205
202
|
],
|
|
206
203
|
};
|
|
207
204
|
}
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
});
|
|
211
|
-
scope.add(stream[i].typ);
|
|
205
|
+
arena.dataTypes[stream[i]] = "or" === val ? EnumToken.OrTokenType : EnumToken.AndTokenType;
|
|
206
|
+
scope.add(arena.dataTypes[stream[i]]);
|
|
212
207
|
stack.push(stream[i]);
|
|
213
208
|
break;
|
|
214
209
|
}
|
|
@@ -217,7 +212,7 @@ function parseAtRuleSupportSyntax(stream, context, options = {}) {
|
|
|
217
212
|
}
|
|
218
213
|
}
|
|
219
214
|
stream.length = 0;
|
|
220
|
-
for (const token of trimArray(tokens)) {
|
|
215
|
+
for (const token of trimArray(arena, tokens)) {
|
|
221
216
|
stream.push(token);
|
|
222
217
|
}
|
|
223
218
|
return { success, errors };
|
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.1
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
|
+
import { TokenField } from '../../ast/arena.js';
|
|
8
9
|
import { EnumToken } from '../../ast/types.js';
|
|
9
10
|
import { trimArray } from '../../validation/match.js';
|
|
10
|
-
import { tokensfuncDefMap
|
|
11
|
+
import { tokensfuncDefMap } from '../../syntax/constants.js';
|
|
11
12
|
import { parseMediaqueryList } from './at-rule-media.js';
|
|
12
13
|
import { parseAtRuleSupportSyntax } from './at-rule-support.js';
|
|
13
14
|
|
|
14
|
-
function matchAtRuleWhenElseSyntax(stream, context, options = {}) {
|
|
15
|
+
function matchAtRuleWhenElseSyntax(arena, stream, context, options = {}) {
|
|
15
16
|
const tokens = [];
|
|
16
17
|
const stack = [];
|
|
17
18
|
let i = 0;
|
|
@@ -25,21 +26,19 @@ function matchAtRuleWhenElseSyntax(stream, context, options = {}) {
|
|
|
25
26
|
if (expectAndOr) {
|
|
26
27
|
let k = i;
|
|
27
28
|
while (k < stream.length &&
|
|
28
|
-
(stream[k]
|
|
29
|
+
(arena.dataTypes[stream[k]] === EnumToken.WhitespaceTokenType || arena.dataTypes[stream[k]] === EnumToken.CommentTokenType)) {
|
|
29
30
|
tokens.push(stream[k]);
|
|
30
31
|
k++;
|
|
31
32
|
}
|
|
32
33
|
expectAndOr = false;
|
|
33
34
|
}
|
|
34
|
-
switch (stream[i]
|
|
35
|
+
switch (arena.dataTypes[stream[i]]) {
|
|
35
36
|
case EnumToken.IdenTokenType:
|
|
36
37
|
{
|
|
37
|
-
const val = stream[i].
|
|
38
|
+
const val = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
|
|
38
39
|
if ("and" === val || "or" === val) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
});
|
|
42
|
-
// scope.add(stream[i].typ);
|
|
40
|
+
arena.dataTypes[stream[i]] = "or" === val ? EnumToken.OrTokenType : EnumToken.AndTokenType;
|
|
41
|
+
// scope.add(arena.dataTypes[stream[i]]);
|
|
43
42
|
stack.push(stream[i]);
|
|
44
43
|
// break;
|
|
45
44
|
}
|
|
@@ -51,66 +50,67 @@ function matchAtRuleWhenElseSyntax(stream, context, options = {}) {
|
|
|
51
50
|
let matchCount = 1;
|
|
52
51
|
let j = i + 1;
|
|
53
52
|
for (; j < stream.length; j++) {
|
|
54
|
-
if (stream[j]
|
|
53
|
+
if (arena.dataTypes[stream[j]] === EnumToken.EndParensTokenType) {
|
|
55
54
|
matchCount--;
|
|
56
55
|
if (matchCount === 0) {
|
|
57
56
|
break;
|
|
58
57
|
}
|
|
59
58
|
}
|
|
60
|
-
else if (stream[j]
|
|
61
|
-
tokensfuncDefMap.has(stream[j]
|
|
59
|
+
else if (arena.dataTypes[stream[j]] === EnumToken.StartParensTokenType ||
|
|
60
|
+
tokensfuncDefMap.has(arena.dataTypes[stream[j]])) {
|
|
62
61
|
matchCount++;
|
|
63
62
|
}
|
|
64
63
|
}
|
|
65
64
|
const slice = stream.slice(i, j + 1);
|
|
66
|
-
const funcName = stream[i].
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
},
|
|
74
|
-
// @ts-expect-error
|
|
75
|
-
].concat(slice.slice(1));
|
|
65
|
+
const funcName = arena.strings[arena.stringValue[stream[i]]].toLowerCase();
|
|
66
|
+
// the function token may be converted by the sub parsers
|
|
67
|
+
const isSupportsFunction = arena.dataTypes[stream[i]] === EnumToken.SupportsFunctionTokenDefType;
|
|
68
|
+
const funcType = tokensfuncDefMap.get(arena.dataTypes[stream[i]]);
|
|
69
|
+
const parens = arena.createToken(EnumToken.StartParensTokenType);
|
|
70
|
+
arena.spanLocation(parens, stream[i], stream[j]);
|
|
71
|
+
const tokenList = [parens].concat(slice.slice(1));
|
|
76
72
|
if ("media" === funcName) {
|
|
77
|
-
const result = parseMediaqueryList(tokenList, options);
|
|
73
|
+
const result = parseMediaqueryList(arena, tokenList, options);
|
|
78
74
|
if (!result.success) {
|
|
79
75
|
return result;
|
|
80
76
|
}
|
|
81
77
|
}
|
|
82
78
|
else if ("supports" === funcName) {
|
|
83
|
-
const result = parseAtRuleSupportSyntax(tokenList, context, options);
|
|
79
|
+
const result = parseAtRuleSupportSyntax(arena, tokenList, context, options);
|
|
84
80
|
if (!result.success) {
|
|
85
81
|
return result;
|
|
86
82
|
}
|
|
87
83
|
}
|
|
88
|
-
else if (
|
|
84
|
+
else if (isSupportsFunction) {
|
|
89
85
|
tokenList.splice(0, tokenList.length, ...slice);
|
|
90
|
-
const result = parseAtRuleSupportSyntax(stream.slice(i, j + 1), context, options);
|
|
86
|
+
const result = parseAtRuleSupportSyntax(arena, stream.slice(i, j + 1), context, options);
|
|
91
87
|
if (!result.success) {
|
|
92
88
|
return result;
|
|
93
89
|
}
|
|
94
90
|
}
|
|
95
|
-
stream[i][
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
91
|
+
arena.spanLocation(stream[i], stream[i], stream[j]);
|
|
92
|
+
const chi = isSupportsFunction
|
|
93
|
+
? arena.firstToken[stream[i]] != 0
|
|
94
|
+
? arena.tokens(stream[i])
|
|
95
|
+
: trimArray(arena, slice.slice(1, -1))
|
|
96
|
+
: arena.tokens(tokenList[0]);
|
|
97
|
+
arena.dataTypes[stream[i]] = funcType;
|
|
98
|
+
arena.setTokenList(stream[i], chi);
|
|
99
|
+
if (stack.length > 0 &&
|
|
100
|
+
(arena.dataTypes[stack[stack.length - 1]] === EnumToken.AndTokenType ||
|
|
101
|
+
arena.dataTypes[stack[stack.length - 1]] === EnumToken.OrTokenType)) {
|
|
102
|
+
const op = stack[stack.length - 1];
|
|
103
|
+
const index = tokens.indexOf(op);
|
|
104
|
+
const index2 = stack.length > 1 ? tokens.indexOf(stack[stack.length - 2]) + 1 : 0;
|
|
105
|
+
const condition = arena.createToken(EnumToken.WhenElseQueryConditionTokenType);
|
|
106
|
+
// inlined: arena.setField(condition, TokenField.L, ...trimArray(arena, tokens.slice(index2, index)))
|
|
107
|
+
arena.setTokenList(arena.fieldList(condition, TokenField.L), trimArray(arena, tokens.slice(index2, index)));
|
|
108
|
+
// inlined: arena.setField(condition, TokenField.R, ...trimArray(arena, tokens.slice(index + 1)))
|
|
109
|
+
arena.setTokenList(arena.fieldList(condition, TokenField.R), trimArray(arena, tokens.slice(index + 1)));
|
|
110
|
+
// inlined: arena.setField(condition, TokenField.Op, op)
|
|
111
|
+
arena.setTokenList(arena.fieldList(condition, TokenField.Op), [op]);
|
|
112
|
+
arena.spanLocation(condition, op, stream[i]);
|
|
113
|
+
tokens[index2] = condition;
|
|
114
114
|
tokens.length = index2 + 1;
|
|
115
115
|
stack.pop();
|
|
116
116
|
}
|
|
@@ -121,7 +121,7 @@ function matchAtRuleWhenElseSyntax(stream, context, options = {}) {
|
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
stream.length = 0;
|
|
124
|
-
for (const token of trimArray(tokens)) {
|
|
124
|
+
for (const token of trimArray(arena, tokens)) {
|
|
125
125
|
stream.push(token);
|
|
126
126
|
}
|
|
127
127
|
return { success, errors };
|
|
@@ -2,21 +2,21 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.1
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { ValidationSyntaxGroupEnum } from '../../validation/parser/typedef.js';
|
|
9
9
|
import { getSyntaxRule } from '../../validation/config.js';
|
|
10
10
|
import { trimArray, matchAllSyntaxes, createValidationContext } from '../../validation/match.js';
|
|
11
11
|
|
|
12
|
-
function matchAtRuleSyntax(atRule, stream, options) {
|
|
13
|
-
const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + atRule
|
|
12
|
+
function matchAtRuleSyntax(arena, atRule, stream, options) {
|
|
13
|
+
const syntaxRules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + arena.strings[arena.name[atRule]]);
|
|
14
14
|
const syntax = syntaxRules?.getPreludeRules()?.slice?.(1);
|
|
15
|
-
trimArray(stream);
|
|
15
|
+
trimArray(arena, stream);
|
|
16
16
|
if (syntax.length === 0) {
|
|
17
17
|
return { success: true, errors: [] };
|
|
18
18
|
}
|
|
19
|
-
const { success, errors } = matchAllSyntaxes(syntax, createValidationContext(stream), options);
|
|
19
|
+
const { success, errors } = matchAllSyntaxes(arena, syntax, createValidationContext(arena, stream), options);
|
|
20
20
|
return { success, errors };
|
|
21
21
|
}
|
|
22
22
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
CSS parser, minifier and validator for node and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version:
|
|
5
|
+
Version: 2.0.1
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
/**
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* @returns
|
|
12
12
|
*/
|
|
13
13
|
function toBase64(value) {
|
|
14
|
-
let result =
|
|
14
|
+
let result = "";
|
|
15
15
|
for (const c of new TextEncoder().encode(value)) {
|
|
16
16
|
result += String.fromCharCode(c);
|
|
17
17
|
}
|