@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,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 { EnumToken,
|
|
9
|
-
import {
|
|
8
|
+
import { EnumToken, ColorType, EnumAstNodeStatus } from '../../ast/types.js';
|
|
9
|
+
import { TokenField } from '../../ast/arena.js';
|
|
10
|
+
import { tokensfuncDefMap, COLORS_NAMES, nonStandardColors, systemColors, deprecatedSystemColors, tokensMap, trimTokenSpace } from '../../syntax/constants.js';
|
|
10
11
|
import { renamedStandardProperties, isColor, parseColor, isWhiteSpace } from '../../syntax/syntax.js';
|
|
11
12
|
import { getSyntaxRule, getParsedSyntax } from '../../validation/config.js';
|
|
12
|
-
import {
|
|
13
|
+
import { matchDeclarationSyntax, trimArray } from '../../validation/match.js';
|
|
13
14
|
import { ValidationSyntaxGroupEnum, ValidationTokenEnum } from '../../validation/parser/typedef.js';
|
|
14
|
-
import {
|
|
15
|
-
import { equalsIgnoreCase } from './text.js';
|
|
15
|
+
import { lowercase, equalsIgnoreCase } from './text.js';
|
|
16
16
|
import { buildExpression } from '../../ast/math/expression.js';
|
|
17
17
|
import { splitTokenList } from '../../validation/utils/list.js';
|
|
18
18
|
|
|
@@ -49,78 +49,122 @@ function parseGridTemplate(template) {
|
|
|
49
49
|
}
|
|
50
50
|
return buffer.length > 0 ? result + buffer : result;
|
|
51
51
|
}
|
|
52
|
+
/**
|
|
53
|
+
* parse the color of an identifier or a color token, see {@link AstArena.visitValues}
|
|
54
|
+
* @param arena
|
|
55
|
+
* @param value
|
|
56
|
+
*/
|
|
57
|
+
function parseColorToken(arena, value) {
|
|
58
|
+
if (arena.dataTypes[value] === EnumToken.IdenTokenType && isColor(arena, value)) {
|
|
59
|
+
parseColor(arena, value);
|
|
60
|
+
}
|
|
61
|
+
else if (arena.dataTypes[value] === EnumToken.ColorTokenType) {
|
|
62
|
+
if (isColor(arena, value)) {
|
|
63
|
+
parseColor(arena, value);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* parse the color of an identifier, see {@link AstArena.visitValues}
|
|
69
|
+
* @param arena
|
|
70
|
+
* @param value
|
|
71
|
+
*/
|
|
72
|
+
function parseColorIdent(arena, value) {
|
|
73
|
+
if (arena.dataTypes[value] === EnumToken.IdenTokenType && isColor(arena, value)) {
|
|
74
|
+
parseColor(arena, value);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Build the declaration node from the name token and the value tokens.
|
|
79
|
+
* @param arena
|
|
80
|
+
* @param name name token id
|
|
81
|
+
* @param tokens
|
|
82
|
+
* @param state
|
|
83
|
+
* @param errors
|
|
84
|
+
*/
|
|
85
|
+
function createDeclarationNode(arena, name, tokens, state, errors) {
|
|
86
|
+
const node = arena.createDeclaration(EnumToken.DeclarationNodeType, arena.strings[arena.stringValue[name]], tokens, state, errors);
|
|
87
|
+
arena.copyLocation(node, name);
|
|
88
|
+
return node;
|
|
89
|
+
}
|
|
52
90
|
/**
|
|
53
91
|
* Parse declaration
|
|
92
|
+
* @param arena
|
|
54
93
|
* @param tokens
|
|
55
|
-
* @param parent
|
|
94
|
+
* @param parent parent node id or at-rule token
|
|
56
95
|
* @param options
|
|
57
96
|
* @param errors
|
|
58
97
|
*/
|
|
59
|
-
function parseDeclaration(tokens, parent, options, errors) {
|
|
98
|
+
function parseDeclaration(arena, tokens, parent, options, errors) {
|
|
60
99
|
const name = tokens.shift();
|
|
61
100
|
let i;
|
|
101
|
+
let nam = arena.strings[arena.stringValue[name]];
|
|
102
|
+
// parent at-rule name
|
|
103
|
+
const isAtRule = parent != null && typeof parent == "number" && arena.dataTypes[parent] === EnumToken.AtRuleNodeType;
|
|
104
|
+
const parentNam = isAtRule ? arena.strings[arena.name[parent]] : null;
|
|
62
105
|
let rules = null;
|
|
63
106
|
let syntaxRules = null;
|
|
64
107
|
let success = true;
|
|
65
108
|
let hasImportantFlag = false;
|
|
66
|
-
let validate =
|
|
67
|
-
if (renamedStandardProperties.has(
|
|
68
|
-
|
|
109
|
+
let validate = !!options.validation;
|
|
110
|
+
if (renamedStandardProperties.has(nam)) {
|
|
111
|
+
nam = renamedStandardProperties.get(nam);
|
|
112
|
+
// inlined: arena.setStr(name, nam)
|
|
113
|
+
arena.stringValue[name] = arena.internString(nam);
|
|
69
114
|
}
|
|
70
115
|
for (i = 0; i < tokens.length; i++) {
|
|
71
116
|
const token = tokens[i];
|
|
72
|
-
if (token
|
|
73
|
-
token
|
|
74
|
-
token
|
|
117
|
+
if (arena.dataTypes[token] == EnumToken.WhitespaceTokenType ||
|
|
118
|
+
arena.dataTypes[token] == EnumToken.CommentTokenType ||
|
|
119
|
+
arena.dataTypes[token] == EnumToken.BadCdoTokenType) {
|
|
75
120
|
continue;
|
|
76
121
|
}
|
|
77
122
|
break;
|
|
78
123
|
}
|
|
79
|
-
if (
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
124
|
+
if (
|
|
125
|
+
// inlined: arena.typeOf(name)
|
|
126
|
+
(arena.dataTypes[name] !== EnumToken.IdenTokenType && arena.dataTypes[name] !== EnumToken.DashedIdenTokenType) ||
|
|
127
|
+
arena.dataTypes[tokens[i]] !== EnumToken.ColonTokenType) {
|
|
128
|
+
if (tokens.length > 0) {
|
|
129
|
+
arena.spanLocation(name, name, tokens[tokens.length - 1]);
|
|
83
130
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
typ: EnumToken.RawNodeTokenType,
|
|
95
|
-
val: tokens,
|
|
96
|
-
});
|
|
131
|
+
// unparsed declaration: stored as a raw node
|
|
132
|
+
const node = arena.createDeclaration(EnumToken.RawNodeTokenType, "", tokens, null, null);
|
|
133
|
+
arena.copyLocation(node, name);
|
|
134
|
+
return node;
|
|
135
|
+
}
|
|
136
|
+
// value tokens, without the leading and trailing whitespace: a single copy
|
|
137
|
+
let start = i + 1;
|
|
138
|
+
let end = tokens.length;
|
|
139
|
+
while (start < end && arena.dataTypes[tokens[start]] === EnumToken.WhitespaceTokenType) {
|
|
140
|
+
start++;
|
|
97
141
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
142
|
+
while (end > start && arena.dataTypes[tokens[end - 1]] === EnumToken.WhitespaceTokenType) {
|
|
143
|
+
end--;
|
|
144
|
+
}
|
|
145
|
+
tokens = tokens.slice(start, end);
|
|
146
|
+
// inlined: arena.typeOf(name)
|
|
147
|
+
if (validate && arena.dataTypes[name] === EnumToken.IdenTokenType) {
|
|
148
|
+
if (isAtRule && parentNam != "import" && parentNam != "supports" && parentNam != "media") {
|
|
105
149
|
// check @at-rule description
|
|
106
150
|
// pick up the syntax or reject immediately
|
|
107
151
|
// else do this
|
|
108
|
-
rules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" +
|
|
152
|
+
rules = getSyntaxRule(ValidationSyntaxGroupEnum.AtRules, "@" + parentNam);
|
|
109
153
|
if (rules != null) {
|
|
110
154
|
const propertyDescriptors = rules.getPropertyDescriptors();
|
|
111
155
|
if (propertyDescriptors != null) {
|
|
112
|
-
syntaxRules = propertyDescriptors[
|
|
156
|
+
syntaxRules = propertyDescriptors[lowercase(nam)];
|
|
113
157
|
}
|
|
114
158
|
else {
|
|
115
159
|
syntaxRules =
|
|
116
160
|
rules.acceptAnyDeclaration && rules.acceptAnyRule
|
|
117
|
-
? getParsedSyntax(ValidationSyntaxGroupEnum.Declarations,
|
|
161
|
+
? getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, lowercase(nam))
|
|
118
162
|
: rules.getBlockRules();
|
|
119
163
|
}
|
|
120
164
|
}
|
|
121
165
|
}
|
|
122
166
|
else {
|
|
123
|
-
syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations,
|
|
167
|
+
syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, lowercase(nam));
|
|
124
168
|
}
|
|
125
169
|
// <declaration-list> or <declaration-rule-list>
|
|
126
170
|
// else
|
|
@@ -129,43 +173,35 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
129
173
|
syntaxRules[0].typ === ValidationTokenEnum.PropertyType &&
|
|
130
174
|
(syntaxRules[0].val === "declaration-list" ||
|
|
131
175
|
syntaxRules[0].val === "declaration-rule-list"))) {
|
|
132
|
-
if (
|
|
133
|
-
(
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
176
|
+
if (isAtRule &&
|
|
177
|
+
(parentNam === "swash" ||
|
|
178
|
+
parentNam === "annotation" ||
|
|
179
|
+
parentNam === "ornaments" ||
|
|
180
|
+
parentNam === "stylistic" ||
|
|
181
|
+
parentNam === "historical-forms")) {
|
|
138
182
|
syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "font-feature-custom-ident");
|
|
139
183
|
}
|
|
140
|
-
else if (
|
|
184
|
+
else if (isAtRule && parentNam === "styleset") {
|
|
141
185
|
syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "font-feature-custom-ident-list");
|
|
142
186
|
}
|
|
143
|
-
else if (
|
|
187
|
+
else if (isAtRule && parentNam === "character-variant") {
|
|
144
188
|
syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, "font-feature-custom-ident-character-variant");
|
|
145
189
|
}
|
|
146
190
|
else {
|
|
147
|
-
syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations,
|
|
191
|
+
syntaxRules = getParsedSyntax(ValidationSyntaxGroupEnum.Declarations, lowercase(nam));
|
|
148
192
|
}
|
|
149
193
|
}
|
|
150
194
|
}
|
|
151
|
-
|
|
195
|
+
// inlined: arena.typeOf(name)
|
|
196
|
+
if (tokens.length === 0 && arena.dataTypes[name] !== EnumToken.DashedIdenTokenType) {
|
|
152
197
|
errors.push({
|
|
153
198
|
action: "drop",
|
|
154
199
|
message: "declaration value missing",
|
|
155
200
|
node: name,
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
if (tokens[tokens.length - 1]?.[LOCEND] != null) {
|
|
159
|
-
name[LOCEND] = tokens[tokens.length - 1][LOCEND];
|
|
160
|
-
}
|
|
161
|
-
name[STATE] = EnumAstNodeStatus.Invalid;
|
|
162
|
-
name[ERRORS] = [errors[errors.length - 1]];
|
|
163
|
-
// @ts-expect-error
|
|
164
|
-
return Object.assign(name, {
|
|
165
|
-
typ: EnumToken.DeclarationNodeType,
|
|
166
|
-
nam: name.val,
|
|
167
|
-
val: tokens,
|
|
201
|
+
// inlined: options.source!.getSourceLocation(arena.getSpanStart(name))
|
|
202
|
+
location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(name))],
|
|
168
203
|
});
|
|
204
|
+
return createDeclarationNode(arena, name, tokens, EnumAstNodeStatus.Invalid, [errors[errors.length - 1]]);
|
|
169
205
|
}
|
|
170
206
|
const stack = [];
|
|
171
207
|
let result = null;
|
|
@@ -173,20 +209,11 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
173
209
|
let index;
|
|
174
210
|
if (syntaxRules != null) {
|
|
175
211
|
const doNotValidate = options.validation === false;
|
|
176
|
-
result = doNotValidate ? null :
|
|
212
|
+
result = doNotValidate ? null : matchDeclarationSyntax(arena, syntaxRules, tokens, options);
|
|
177
213
|
if (doNotValidate || result != null) {
|
|
178
214
|
success = doNotValidate || result?.success;
|
|
179
215
|
if (success) {
|
|
180
|
-
|
|
181
|
-
if (value.typ === EnumToken.IdenTokenType && isColor(value)) {
|
|
182
|
-
parseColor(value);
|
|
183
|
-
}
|
|
184
|
-
else if (value.typ === EnumToken.ColorTokenType) {
|
|
185
|
-
if (isColor(value)) {
|
|
186
|
-
parseColor(value);
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
}
|
|
216
|
+
arena.visitValues(tokens, parseColorToken);
|
|
190
217
|
}
|
|
191
218
|
if (!doNotValidate && !result?.success && result.errors.length > 0) {
|
|
192
219
|
for (index = 0; index < result.errors.length; index++) {
|
|
@@ -197,66 +224,64 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
197
224
|
}
|
|
198
225
|
for (i = 0; i < tokens.length; i++) {
|
|
199
226
|
token = tokens[i];
|
|
200
|
-
if (tokensfuncDefMap.has(token
|
|
201
|
-
token
|
|
202
|
-
token
|
|
227
|
+
if (tokensfuncDefMap.has(arena.dataTypes[token]) ||
|
|
228
|
+
arena.dataTypes[token] == EnumToken.StartParensTokenType ||
|
|
229
|
+
arena.dataTypes[token] == EnumToken.BlockStartTokenType) {
|
|
203
230
|
stack.push(token);
|
|
204
231
|
continue;
|
|
205
232
|
}
|
|
206
|
-
if (token
|
|
233
|
+
if (arena.dataTypes[token] === EnumToken.ColonTokenType && stack.length == 0) {
|
|
207
234
|
stack.push(token);
|
|
208
235
|
continue;
|
|
209
236
|
}
|
|
210
|
-
switch (token
|
|
237
|
+
switch (arena.dataTypes[token]) {
|
|
211
238
|
case EnumToken.Literal:
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
});
|
|
239
|
+
// inlined: arena.typeOf(stack.at(-1))
|
|
240
|
+
if (arena.strings[arena.stringValue[token]] === "/" && arena.dataTypes[stack.at(-1)] == EnumToken.MathFunctionTokenDefType) {
|
|
241
|
+
arena.dataTypes[token] = EnumToken.Div;
|
|
216
242
|
}
|
|
217
|
-
else if (token
|
|
218
|
-
stack.at(-1)
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
});
|
|
243
|
+
else if (arena.strings[arena.stringValue[token]] === "-" &&
|
|
244
|
+
// inlined: arena.typeOf(stack.at(-1))
|
|
245
|
+
arena.dataTypes[stack.at(-1)] == EnumToken.MathFunctionTokenDefType) {
|
|
246
|
+
arena.dataTypes[token] = EnumToken.Sub;
|
|
222
247
|
}
|
|
223
248
|
break;
|
|
224
249
|
case EnumToken.Star:
|
|
225
250
|
case EnumToken.Plus:
|
|
226
|
-
|
|
227
|
-
typ: tokensMap.get(token.typ),
|
|
228
|
-
});
|
|
251
|
+
arena.dataTypes[token] = tokensMap.get(arena.dataTypes[token]);
|
|
229
252
|
break;
|
|
230
253
|
case EnumToken.BlockEndTokenType:
|
|
231
|
-
|
|
254
|
+
// inlined: arena.typeOf(stack.at(-1))
|
|
255
|
+
if (arena.dataTypes[stack.at(-1)] === EnumToken.BlockStartTokenType) {
|
|
232
256
|
// stack.pop();index = tokens.indexOf(stack.at(-1)!);
|
|
233
257
|
index = tokens.indexOf(stack.at(-1));
|
|
234
258
|
tokens.splice(i, 1);
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
chi: trimArray(tokens.splice(index + 1, i - index - 1)),
|
|
238
|
-
});
|
|
259
|
+
arena.dataTypes[tokens[index]] = EnumToken.WrappedValuesTokenType;
|
|
260
|
+
arena.setTokenList(tokens[index], trimArray(arena, tokens.splice(index + 1, i - index - 1)));
|
|
239
261
|
i = index;
|
|
240
262
|
stack.pop();
|
|
241
263
|
}
|
|
242
264
|
break;
|
|
243
265
|
case EnumToken.EndParensTokenType:
|
|
244
|
-
if (stack.
|
|
266
|
+
if (stack.length > 0 &&
|
|
267
|
+
(arena.dataTypes[stack[stack.length - 1]] === EnumToken.StartParensTokenType ||
|
|
268
|
+
tokensfuncDefMap.has(arena.dataTypes[stack[stack.length - 1]]))) {
|
|
245
269
|
index = tokens.indexOf(stack.at(-1));
|
|
246
270
|
tokens.splice(i, 1);
|
|
247
|
-
|
|
248
|
-
|
|
271
|
+
arena.dataTypes[tokens[index]] =
|
|
272
|
+
arena.dataTypes[stack[stack.length - 1]] === EnumToken.StartParensTokenType
|
|
249
273
|
? EnumToken.ParensTokenType
|
|
250
|
-
: tokensfuncDefMap.get(stack.
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
if (
|
|
256
|
-
|
|
274
|
+
: tokensfuncDefMap.get(arena.dataTypes[stack[stack.length - 1]]);
|
|
275
|
+
arena.setTokenList(tokens[index], trimArray(arena, tokens.splice(index + 1, i - index - 1)));
|
|
276
|
+
if (arena.dataTypes[tokens[index]] === EnumToken.WildCardFunctionTokenType &&
|
|
277
|
+
equalsIgnoreCase(arena.strings[arena.stringValue[tokens[index]]], "if")) {
|
|
278
|
+
const last = arena.lastToken[tokens[index]];
|
|
279
|
+
if (last != 0 && arena.dataTypes[last] === EnumToken.SemiColonTokenType) {
|
|
280
|
+
arena.detach(last);
|
|
257
281
|
}
|
|
258
282
|
let foundElse = false;
|
|
259
|
-
|
|
283
|
+
// inlined: arena.setTokens(tokens[index], ...splitTokenList(...)), the array is passed as is
|
|
284
|
+
arena.setTokenList(tokens[index], splitTokenList(arena, arena.tokens(tokens[index]), [EnumToken.SemiColonTokenType], true).reduce((acc, curr) => {
|
|
260
285
|
if (foundElse) {
|
|
261
286
|
// else already found
|
|
262
287
|
// ignore everything after else
|
|
@@ -265,69 +290,67 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
265
290
|
}
|
|
266
291
|
let index = -1;
|
|
267
292
|
while (++index < curr.length) {
|
|
268
|
-
if (curr[index]
|
|
269
|
-
equalsIgnoreCase("else", curr[index]
|
|
293
|
+
if (arena.dataTypes[curr[index]] == EnumToken.IdenTokenType &&
|
|
294
|
+
equalsIgnoreCase("else", arena.strings[arena.stringValue[curr[index]]])) {
|
|
270
295
|
foundElse = true;
|
|
271
296
|
}
|
|
272
|
-
if (curr[index]
|
|
297
|
+
if (arena.dataTypes[curr[index]] === EnumToken.ColonTokenType) {
|
|
273
298
|
break;
|
|
274
299
|
}
|
|
275
300
|
}
|
|
276
301
|
if (foundElse && acc.length > 0) {
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
302
|
+
const condition = arena.createToken(EnumToken.IfConditionTokenType);
|
|
303
|
+
// inlined: arena.setField(condition, TokenField.L, ...trimArray(arena, curr.slice(0, index)))
|
|
304
|
+
arena.setTokenList(arena.fieldList(condition, TokenField.L), trimArray(arena, curr.slice(0, index)));
|
|
305
|
+
// inlined: arena.setField(condition, TokenField.R, ...trimArray(arena, arena.typeOf(curr.at(-1)) === EnumToken.SemiColonTokenType ? curr.slice(index + 1, -1) : curr.slice(index + 1)))
|
|
306
|
+
arena.setTokenList(arena.fieldList(condition, TokenField.R), trimArray(arena, arena.dataTypes[curr.at(-1)] === EnumToken.SemiColonTokenType
|
|
307
|
+
? curr.slice(index + 1, -1)
|
|
308
|
+
: curr.slice(index + 1)));
|
|
309
|
+
const ifElse = arena.createToken(EnumToken.IfElseConditionTokenType);
|
|
310
|
+
// inlined: arena.setField(ifElse, TokenField.L, acc[acc.length - 1])
|
|
311
|
+
arena.setTokenList(arena.fieldList(ifElse, TokenField.L), [acc[acc.length - 1]]);
|
|
312
|
+
// inlined: arena.setField(ifElse, TokenField.R, condition)
|
|
313
|
+
arena.setTokenList(arena.fieldList(ifElse, TokenField.R), [condition]);
|
|
314
|
+
acc[acc.length - 1] = ifElse;
|
|
288
315
|
}
|
|
289
316
|
else {
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
317
|
+
const condition = arena.createToken(EnumToken.IfConditionTokenType);
|
|
318
|
+
// inlined: arena.setField(condition, TokenField.L, ...trimArray(arena, curr.slice(0, index)))
|
|
319
|
+
arena.setTokenList(arena.fieldList(condition, TokenField.L), trimArray(arena, curr.slice(0, index)));
|
|
320
|
+
// inlined: arena.setField(condition, TokenField.R, ...trimArray(arena, curr.slice(index + 1)))
|
|
321
|
+
arena.setTokenList(arena.fieldList(condition, TokenField.R), trimArray(arena, curr.slice(index + 1)));
|
|
322
|
+
acc.push(condition);
|
|
295
323
|
}
|
|
296
324
|
return acc;
|
|
297
|
-
}, []);
|
|
325
|
+
}, []));
|
|
298
326
|
}
|
|
299
|
-
else if (tokens[index]
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
}
|
|
306
|
-
if (tokens[index].chi[l]?.typ === EnumToken.StringTokenType &&
|
|
307
|
-
/^[a-zA-Z0-0/_.-]+$/.test(tokens[index].chi[l].val.slice(1, -1))) {
|
|
308
|
-
Object.assign(tokens[index].chi[l], {
|
|
309
|
-
typ: EnumToken.UrlTokenTokenType,
|
|
310
|
-
val: tokens[index].chi[l].val.slice(1, -1),
|
|
311
|
-
});
|
|
327
|
+
else if (arena.dataTypes[tokens[index]] === EnumToken.UrlFunctionTokenType) {
|
|
328
|
+
const url = arena.findToken(tokens[index], (arena, t) => arena.dataTypes[t] === EnumToken.StringTokenType);
|
|
329
|
+
if (url != null && /^[a-zA-Z0-0/_.-]+$/.test(arena.strings[arena.stringValue[url]].slice(1, -1))) {
|
|
330
|
+
arena.dataTypes[url] = EnumToken.UrlTokenTokenType;
|
|
331
|
+
// inlined: arena.setStr(url, arena.strings[arena.stringValue[url]].slice(1, -1))
|
|
332
|
+
arena.stringValue[url] = arena.internString(arena.strings[arena.stringValue[url]].slice(1, -1));
|
|
312
333
|
}
|
|
313
334
|
}
|
|
314
335
|
i = index;
|
|
315
|
-
if (tokens[index]
|
|
316
|
-
equalsIgnoreCase("calc", tokens[index]
|
|
317
|
-
tokens[index]
|
|
336
|
+
if (arena.dataTypes[tokens[index]] === EnumToken.MathFunctionTokenType &&
|
|
337
|
+
equalsIgnoreCase("calc", arena.strings[arena.stringValue[tokens[index]]])) {
|
|
338
|
+
// inlined: arena.setTokens(tokens[index], buildExpression(arena, arena.tokens(tokens[index])))
|
|
339
|
+
arena.setTokenList(tokens[index], [buildExpression(arena, arena.tokens(tokens[index]))]);
|
|
318
340
|
}
|
|
319
|
-
else if (tokens[index]
|
|
320
|
-
if (isColor(tokens[index])) {
|
|
321
|
-
parseColor(tokens[index]);
|
|
341
|
+
else if (arena.dataTypes[tokens[index]] === EnumToken.ColorTokenType) {
|
|
342
|
+
if (isColor(arena, tokens[index])) {
|
|
343
|
+
parseColor(arena, tokens[index]);
|
|
322
344
|
}
|
|
323
345
|
else {
|
|
324
346
|
success = false;
|
|
325
|
-
tokens[index]
|
|
347
|
+
arena.dataTypes[tokens[index]] = EnumToken.FunctionTokenType;
|
|
326
348
|
errors.push({
|
|
327
349
|
action: "drop",
|
|
328
350
|
message: `invalid color`,
|
|
329
351
|
node: tokens[index],
|
|
330
|
-
|
|
352
|
+
// inlined: options.source!.getSourceLocation(arena.getSpanStart(tokens[index]))
|
|
353
|
+
location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(tokens[index]))],
|
|
331
354
|
});
|
|
332
355
|
}
|
|
333
356
|
}
|
|
@@ -336,31 +359,28 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
336
359
|
break;
|
|
337
360
|
case EnumToken.IdenTokenType:
|
|
338
361
|
{
|
|
339
|
-
const val =
|
|
362
|
+
const val = lowercase(arena.strings[arena.stringValue[token]]);
|
|
340
363
|
if (val in COLORS_NAMES || val === "currentcolor") {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
364
|
+
// inlined: arena.setStr(token, val)
|
|
365
|
+
arena.stringValue[token] = arena.internString(val);
|
|
366
|
+
arena.dataTypes[token] = EnumToken.ColorTokenType;
|
|
367
|
+
// inlined: arena.setColorType(token, ColorType.LIT)
|
|
368
|
+
arena.colorType[token] = ColorType.LIT;
|
|
346
369
|
}
|
|
347
370
|
else if (nonStandardColors.has(val)) {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
});
|
|
371
|
+
arena.dataTypes[token] = EnumToken.ColorTokenType;
|
|
372
|
+
// inlined: arena.setColorType(token, ColorType.NON_STD)
|
|
373
|
+
arena.colorType[token] = ColorType.NON_STD;
|
|
352
374
|
}
|
|
353
375
|
else if (systemColors.has(val)) {
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
});
|
|
376
|
+
arena.dataTypes[token] = EnumToken.ColorTokenType;
|
|
377
|
+
// inlined: arena.setColorType(token, ColorType.SYS)
|
|
378
|
+
arena.colorType[token] = ColorType.SYS;
|
|
358
379
|
}
|
|
359
380
|
else if (deprecatedSystemColors.has(val)) {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
});
|
|
381
|
+
arena.dataTypes[token] = EnumToken.ColorTokenType;
|
|
382
|
+
// inlined: arena.setColorType(token, ColorType.DPSYS)
|
|
383
|
+
arena.colorType[token] = ColorType.DPSYS;
|
|
364
384
|
}
|
|
365
385
|
}
|
|
366
386
|
break;
|
|
@@ -368,8 +388,8 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
368
388
|
hasImportantFlag = true;
|
|
369
389
|
break;
|
|
370
390
|
}
|
|
371
|
-
if (trimTokenSpace.has(token
|
|
372
|
-
if (tokens[i + 1]
|
|
391
|
+
if (trimTokenSpace.has(arena.dataTypes[token])) {
|
|
392
|
+
if (arena.dataTypes[tokens[i + 1]] === EnumToken.WhitespaceTokenType) {
|
|
373
393
|
for (let index = i + 1; index < tokens.length - 1; index++) {
|
|
374
394
|
tokens[index] = tokens[index + 1];
|
|
375
395
|
}
|
|
@@ -377,7 +397,7 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
377
397
|
i--;
|
|
378
398
|
continue;
|
|
379
399
|
}
|
|
380
|
-
if (tokens[i - 1]
|
|
400
|
+
if (arena.dataTypes[tokens[i - 1]] == EnumToken.WhitespaceTokenType) {
|
|
381
401
|
for (let index = i - 1; index < tokens.length - 1; index++) {
|
|
382
402
|
tokens[index] = tokens[index + 1];
|
|
383
403
|
}
|
|
@@ -391,97 +411,76 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
391
411
|
action: "drop",
|
|
392
412
|
message: "unbalanced token",
|
|
393
413
|
node: stack[stack.length - 1],
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
if (tokens[tokens.length - 1][LOCEND] != null) {
|
|
397
|
-
name[LOCEND] = tokens[tokens.length - 1][LOCEND];
|
|
398
|
-
}
|
|
399
|
-
name[STATE] = EnumAstNodeStatus.Invalid;
|
|
400
|
-
name[ERRORS] = result?.errors ?? [];
|
|
401
|
-
//@ts-expect-error
|
|
402
|
-
return Object.assign(name, {
|
|
403
|
-
typ: EnumToken.DeclarationNodeType,
|
|
404
|
-
nam: name.val,
|
|
405
|
-
val: tokens,
|
|
414
|
+
// inlined: options.source!.getSourceLocation(arena.getSpanStart(stack[stack.length - 1]))
|
|
415
|
+
location: [options.source.file, ...options.source.lineStarts.getOffsets(arena.getSpanStart(stack[stack.length - 1]))],
|
|
406
416
|
});
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
if (value.typ === EnumToken.IdenTokenType && isColor(value)) {
|
|
410
|
-
parseColor(value);
|
|
417
|
+
if (tokens.length > 0) {
|
|
418
|
+
arena.spanLocation(name, name, tokens[tokens.length - 1]);
|
|
411
419
|
}
|
|
420
|
+
return createDeclarationNode(arena, name, tokens, EnumAstNodeStatus.Invalid, result?.errors ?? null);
|
|
412
421
|
}
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
422
|
+
arena.visitValues(tokens, parseColorIdent);
|
|
423
|
+
if (nam === "grid" ||
|
|
424
|
+
nam === "grid-template-areas" ||
|
|
425
|
+
nam === "grid-template-rows" ||
|
|
426
|
+
nam === "grid-template-columns") {
|
|
417
427
|
let i = 0;
|
|
418
428
|
for (; i < tokens.length; i++) {
|
|
419
|
-
if (tokens[i]
|
|
420
|
-
if (/[\s.]/.test(tokens[i]
|
|
421
|
-
tokens[i]
|
|
429
|
+
if (arena.dataTypes[tokens[i]] === EnumToken.StringTokenType) {
|
|
430
|
+
if (/[\s.]/.test(arena.strings[arena.stringValue[tokens[i]]])) {
|
|
431
|
+
// inlined: arena.setStr(tokens[i], parseGridTemplate(arena.strings[arena.stringValue[tokens[i]]]))
|
|
432
|
+
arena.stringValue[tokens[i]] = arena.internString(parseGridTemplate(arena.strings[arena.stringValue[tokens[i]]]));
|
|
422
433
|
}
|
|
423
|
-
if (tokens[i + 1]
|
|
424
|
-
tokens[i + 2]
|
|
434
|
+
if (arena.dataTypes[tokens[i + 1]] === EnumToken.WhitespaceTokenType &&
|
|
435
|
+
arena.dataTypes[tokens[i + 2]] === EnumToken.StringTokenType) {
|
|
425
436
|
tokens.splice(i + 1, 1);
|
|
426
437
|
}
|
|
427
438
|
}
|
|
428
439
|
}
|
|
429
440
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
441
|
+
// inlined: arena.typeOf(name)
|
|
442
|
+
if (validate && syntaxRules == null && arena.dataTypes[name] === EnumToken.IdenTokenType) {
|
|
443
|
+
if (tokens.length > 0) {
|
|
444
|
+
arena.spanLocation(name, name, tokens[tokens.length - 1]);
|
|
433
445
|
}
|
|
434
|
-
name
|
|
435
|
-
name[ERRORS] = result?.errors ?? [];
|
|
436
|
-
if (hasImportantFlag) {
|
|
437
|
-
name[STATE] |= EnumAstNodeStatus.ImportantDeclaration;
|
|
438
|
-
}
|
|
439
|
-
// @ts-expect-error
|
|
440
|
-
return Object.assign(name, {
|
|
441
|
-
typ: EnumToken.DeclarationNodeType,
|
|
442
|
-
nam: name.val,
|
|
443
|
-
val: tokens,
|
|
444
|
-
});
|
|
446
|
+
return createDeclarationNode(arena, name, tokens, hasImportantFlag ? EnumAstNodeStatus.Unknown | EnumAstNodeStatus.ImportantDeclaration : EnumAstNodeStatus.Unknown, result?.errors ?? null);
|
|
445
447
|
}
|
|
446
|
-
if (equalsIgnoreCase("composes",
|
|
448
|
+
if (equalsIgnoreCase("composes", nam)) {
|
|
447
449
|
let index = -1;
|
|
448
450
|
for (let i = 0; i < tokens.length; i++) {
|
|
449
|
-
if (tokens[i]
|
|
451
|
+
if (arena.dataTypes[tokens[i]] === EnumToken.IdenTokenType && equalsIgnoreCase("from", arena.strings[arena.stringValue[tokens[i]]])) {
|
|
450
452
|
index = i;
|
|
451
453
|
break;
|
|
452
454
|
}
|
|
453
455
|
}
|
|
454
|
-
const left = trimArray(index == -1 ? tokens : tokens.slice(0, index));
|
|
455
|
-
const right = index == -1 ? null : trimArray(tokens.slice(index + 1));
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
456
|
+
const left = trimArray(arena, index == -1 ? tokens : tokens.slice(0, index));
|
|
457
|
+
const right = index == -1 ? null : trimArray(arena, tokens.slice(index + 1));
|
|
458
|
+
const composes = arena.createToken(EnumToken.ComposesSelectorNodeType);
|
|
459
|
+
const first = tokens[0];
|
|
460
|
+
const last = index != -1 && right.length > 0 ? right[right.length - 1] : left[left.length - 1];
|
|
461
|
+
// inlined: arena.setField(composes, TokenField.L, ...left)
|
|
462
|
+
arena.setTokenList(arena.fieldList(composes, TokenField.L), left);
|
|
463
|
+
if (right != null && right.length > 0) {
|
|
464
|
+
// inlined: arena.setField(composes, TokenField.R, right[0])
|
|
465
|
+
arena.setTokenList(arena.fieldList(composes, TokenField.R), [right[0]]);
|
|
466
|
+
}
|
|
467
|
+
if (first != null && last != null) {
|
|
468
|
+
arena.spanLocation(composes, first, last);
|
|
469
|
+
}
|
|
470
|
+
tokens = [composes];
|
|
466
471
|
}
|
|
467
|
-
if (tokens
|
|
468
|
-
name
|
|
472
|
+
if (tokens.length > 0) {
|
|
473
|
+
arena.spanLocation(name, name, tokens[tokens.length - 1]);
|
|
469
474
|
}
|
|
470
|
-
|
|
475
|
+
let state = success
|
|
471
476
|
? result == null
|
|
472
477
|
? EnumAstNodeStatus.Unvalidated
|
|
473
478
|
: EnumAstNodeStatus.Validated
|
|
474
479
|
: EnumAstNodeStatus.ValidationFailed;
|
|
475
|
-
name[ERRORS] = result?.errors ?? [];
|
|
476
480
|
if (hasImportantFlag) {
|
|
477
|
-
|
|
481
|
+
state |= EnumAstNodeStatus.ImportantDeclaration;
|
|
478
482
|
}
|
|
479
|
-
|
|
480
|
-
return Object.assign(name, {
|
|
481
|
-
typ: EnumToken.DeclarationNodeType,
|
|
482
|
-
nam: name.val,
|
|
483
|
-
val: tokens,
|
|
484
|
-
});
|
|
483
|
+
return createDeclarationNode(arena, name, tokens, state, result?.errors ?? null);
|
|
485
484
|
}
|
|
486
485
|
|
|
487
486
|
export { parseDeclaration };
|