@tbela99/css-parser 1.5.0 → 1.6.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.
- package/CHANGELOG.md +33 -0
- package/README.md +7 -7
- package/dist/index-umd-web.js +5562 -4731
- package/dist/index.cjs +5565 -4736
- package/dist/index.d.ts +51 -37
- package/dist/lib/ast/expand.js +21 -7
- package/dist/lib/ast/features/calc.js +17 -55
- package/dist/lib/ast/features/if.js +7 -3
- package/dist/lib/ast/features/inlinecssvariables.js +6 -5
- package/dist/lib/ast/features/prefix.js +41 -44
- package/dist/lib/ast/features/shorthand.js +10 -5
- package/dist/lib/ast/features/transform.js +1 -1
- package/dist/lib/ast/math/expression.js +190 -32
- package/dist/lib/ast/minify.js +37 -14
- package/dist/lib/ast/node.js +5 -3
- package/dist/lib/ast/transform/compute.js +63 -4
- package/dist/lib/ast/transform/minify.js +6 -6
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +1 -1
- package/dist/lib/ast/transform/scale.js +1 -1
- package/dist/lib/ast/transform/skew.js +1 -1
- package/dist/lib/ast/transform/translate.js +1 -1
- package/dist/lib/ast/transform/utils.js +46 -18
- package/dist/lib/ast/walk.js +28 -4
- package/dist/lib/fs/resolve.js +11 -2
- package/dist/lib/parser/declaration/list.js +22 -7
- package/dist/lib/parser/declaration/map.js +26 -8
- package/dist/lib/parser/declaration/set.js +3 -1
- package/dist/lib/parser/linesmap.js +2 -4
- package/dist/lib/parser/parse.js +388 -227
- package/dist/lib/parser/tokenize.js +1478 -818
- package/dist/lib/parser/utils/at-rule-container.js +28 -196
- package/dist/lib/parser/utils/at-rule-generic.js +6 -7
- package/dist/lib/parser/utils/at-rule-import.js +17 -13
- package/dist/lib/parser/utils/at-rule-media.js +36 -19
- package/dist/lib/parser/utils/at-rule-support.js +19 -9
- package/dist/lib/parser/utils/at-rule-when-else.js +11 -38
- package/dist/lib/parser/utils/at-rule.js +0 -18
- package/dist/lib/parser/utils/declaration.js +30 -95
- package/dist/lib/parser/utils/hash.js +4 -4
- package/dist/lib/parser/utils/selector.js +89 -144
- package/dist/lib/parser/utils/text.js +4 -2
- package/dist/lib/renderer/render.js +102 -72
- package/dist/lib/renderer/sourcemap/sourcemap.js +2 -5
- package/dist/lib/syntax/color/a98rgb.js +14 -4
- package/dist/lib/syntax/color/cmyk.js +10 -18
- package/dist/lib/syntax/color/color-mix.js +21 -34
- package/dist/lib/syntax/color/color.js +32 -49
- package/dist/lib/syntax/color/hsl.js +15 -15
- package/dist/lib/syntax/color/hwb.js +18 -17
- package/dist/lib/syntax/color/lab.js +17 -17
- package/dist/lib/syntax/color/lch.js +13 -13
- package/dist/lib/syntax/color/oklab.js +10 -11
- package/dist/lib/syntax/color/oklch.js +15 -18
- package/dist/lib/syntax/color/p3.js +18 -12
- package/dist/lib/syntax/color/prophotorgb.js +26 -28
- package/dist/lib/syntax/color/rec2020.js +8 -4
- package/dist/lib/syntax/color/relative-color.js +26 -10
- package/dist/lib/syntax/color/srgb.js +5 -4
- package/dist/lib/syntax/color/utils/distance.js +1 -1
- package/dist/lib/syntax/color/xyz.js +2 -2
- package/dist/lib/syntax/color/xyzd50.js +2 -2
- package/dist/lib/syntax/constants.js +12 -2
- package/dist/lib/syntax/syntax.js +32 -108
- package/dist/lib/validation/config.json.js +3 -0
- package/dist/lib/validation/match.js +29 -26
- package/dist/node.js +16 -9
- package/dist/types.d.ts +5 -1
- package/dist/types.js +4 -0
- package/dist/utils/sync.js +22 -1
- package/dist/web.js +13 -4
- package/package.json +1 -1
- package/llms.txt +0 -80
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getParsedSyntax, getSyntaxConfig } from './config.js';
|
|
2
2
|
import { EnumToken } from '../ast/types.js';
|
|
3
3
|
import { ValidationSyntaxGroupEnum, ValidationTokenEnum, MediaFeatureType } from './parser/typedef.js';
|
|
4
|
-
import {
|
|
4
|
+
import { LOCSTA, tokensfuncDefMap, tokensfuncSet, funcLike, mFLT, mFGT } from '../syntax/constants.js';
|
|
5
5
|
import { isColor } from '../syntax/syntax.js';
|
|
6
6
|
import { equalsIgnoreCase } from '../parser/utils/text.js';
|
|
7
7
|
import { cloneNode } from '../ast/clone.js';
|
|
@@ -13,11 +13,8 @@ const allValues = config.declarations.all.syntax.split(/[\s|]+/g);
|
|
|
13
13
|
/**
|
|
14
14
|
* @type {Array.<EnumToken>}
|
|
15
15
|
*/
|
|
16
|
-
const funcTypes =
|
|
17
|
-
|
|
18
|
-
EnumToken.FunctionTokenType,
|
|
19
|
-
EnumToken.PseudoClassFuncTokenType,
|
|
20
|
-
];
|
|
16
|
+
const funcTypes = Array.from(tokensfuncDefMap.values());
|
|
17
|
+
funcTypes.push(EnumToken.FunctionTokenType, EnumToken.PseudoClassFuncTokenType);
|
|
21
18
|
/**
|
|
22
19
|
* trim leading and trailing whitespace
|
|
23
20
|
* @param tokens
|
|
@@ -340,7 +337,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
340
337
|
message: `Unexpected token ${EnumToken[stream[i].typ]}`,
|
|
341
338
|
node: stream[i],
|
|
342
339
|
// @ts-expect-error
|
|
343
|
-
location: options.source.getSourceLocation(stream[i][
|
|
340
|
+
location: options.source.getSourceLocation(stream[i][LOCSTA]),
|
|
344
341
|
},
|
|
345
342
|
],
|
|
346
343
|
};
|
|
@@ -375,7 +372,9 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
375
372
|
if (!result.success) {
|
|
376
373
|
success = false;
|
|
377
374
|
if (result.errors.length > 0) {
|
|
378
|
-
|
|
375
|
+
for (const error of result.errors) {
|
|
376
|
+
errors.push(error);
|
|
377
|
+
}
|
|
379
378
|
}
|
|
380
379
|
}
|
|
381
380
|
}
|
|
@@ -394,7 +393,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
394
393
|
message: `Nesting selector is not allowed`,
|
|
395
394
|
node: token,
|
|
396
395
|
// @ts-expect-error
|
|
397
|
-
location: options.source.getSourceLocation(token[
|
|
396
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
398
397
|
},
|
|
399
398
|
],
|
|
400
399
|
};
|
|
@@ -428,7 +427,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
428
427
|
message: `Unexpected combinator ${EnumToken[token.typ]}`,
|
|
429
428
|
node: token,
|
|
430
429
|
// @ts-expect-error
|
|
431
|
-
location: options.source.getSourceLocation(token[
|
|
430
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
432
431
|
},
|
|
433
432
|
],
|
|
434
433
|
};
|
|
@@ -472,7 +471,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
472
471
|
message: `Unexpected token ${EnumToken[token.typ]}`,
|
|
473
472
|
node: token,
|
|
474
473
|
// @ts-expect-error
|
|
475
|
-
location: options.source.getSourceLocation(token[
|
|
474
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
476
475
|
},
|
|
477
476
|
],
|
|
478
477
|
};
|
|
@@ -523,7 +522,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
523
522
|
message: `Unexpected token ${EnumToken[slice[0].typ]}`,
|
|
524
523
|
node: slice[0],
|
|
525
524
|
// @ts-expect-error
|
|
526
|
-
location: options.source.getSourceLocation(slice[0][
|
|
525
|
+
location: options.source.getSourceLocation(slice[0][LOCSTA]),
|
|
527
526
|
},
|
|
528
527
|
],
|
|
529
528
|
};
|
|
@@ -535,8 +534,8 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
535
534
|
// errors: [
|
|
536
535
|
// {
|
|
537
536
|
// action: "drop",
|
|
538
|
-
// message: `Unexpected token ${EnumToken[slice[0].typ]} at ${slice[0][LOC]!.src}:${slice[0][
|
|
539
|
-
// slice[0][
|
|
537
|
+
// message: `Unexpected token ${EnumToken[slice[0].typ]} at ${slice[0][LOC]!.src}:${slice[0][LOCSTA].lin}:${
|
|
538
|
+
// slice[0][LOCSTA].col
|
|
540
539
|
// }`,
|
|
541
540
|
// node: slice[0],
|
|
542
541
|
// location: slice[0][LOC],
|
|
@@ -574,8 +573,8 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
574
573
|
// errors: [
|
|
575
574
|
// {
|
|
576
575
|
// action: "drop",
|
|
577
|
-
// message: `Unexpected token ${EnumToken[token.typ]} at ${token[LOC]!.src}:${token[
|
|
578
|
-
// token[
|
|
576
|
+
// message: `Unexpected token ${EnumToken[token.typ]} at ${token[LOC]!.src}:${token[LOCSTA].lin}:${
|
|
577
|
+
// token[LOCSTA].col
|
|
579
578
|
// }`,
|
|
580
579
|
// node: token,
|
|
581
580
|
// location: token[LOC],
|
|
@@ -607,8 +606,8 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
607
606
|
// errors: [
|
|
608
607
|
// {
|
|
609
608
|
// action: "drop",
|
|
610
|
-
// message: `Unexpected token ${EnumToken[token.typ]} at ${token[LOC]!.src}:${token[
|
|
611
|
-
// token[
|
|
609
|
+
// message: `Unexpected token ${EnumToken[token.typ]} at ${token[LOC]!.src}:${token[LOCSTA].lin}:${
|
|
610
|
+
// token[LOCSTA].col
|
|
612
611
|
// }`,
|
|
613
612
|
// node: token,
|
|
614
613
|
// location: token[LOC],
|
|
@@ -637,7 +636,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
637
636
|
message: `Unexpected token ${EnumToken[token.typ]}`,
|
|
638
637
|
node: token,
|
|
639
638
|
// @ts-expect-error
|
|
640
|
-
location: options.source.getSourceLocation(token[
|
|
639
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
641
640
|
},
|
|
642
641
|
],
|
|
643
642
|
};
|
|
@@ -666,7 +665,9 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
666
665
|
if (!result.success) {
|
|
667
666
|
success = false;
|
|
668
667
|
if (result.errors.length > 0) {
|
|
669
|
-
|
|
668
|
+
for (const error of result.errors) {
|
|
669
|
+
errors.push(error);
|
|
670
|
+
}
|
|
670
671
|
}
|
|
671
672
|
}
|
|
672
673
|
stack.pop();
|
|
@@ -680,7 +681,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
680
681
|
message: `Unexpected token ${EnumToken[token.typ]}`,
|
|
681
682
|
node: token,
|
|
682
683
|
// @ts-expect-error
|
|
683
|
-
location: options.source.getSourceLocation(token[
|
|
684
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
684
685
|
},
|
|
685
686
|
],
|
|
686
687
|
};
|
|
@@ -702,7 +703,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
702
703
|
message: `Unsupported selector token ${EnumToken[token.typ]}`,
|
|
703
704
|
node: token,
|
|
704
705
|
// @ts-expect-error
|
|
705
|
-
location: options.source.getSourceLocation(token[
|
|
706
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
706
707
|
},
|
|
707
708
|
],
|
|
708
709
|
};
|
|
@@ -728,13 +729,15 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
728
729
|
message: `Unmatched token ${EnumToken[stack.at(-1).typ]}`,
|
|
729
730
|
node: stack.at(-1),
|
|
730
731
|
// @ts-expect-error
|
|
731
|
-
location: options.source.getSourceLocation(stack.at(-1)[
|
|
732
|
+
location: options.source.getSourceLocation(stack.at(-1)[LOCSTA]),
|
|
732
733
|
},
|
|
733
734
|
],
|
|
734
735
|
};
|
|
735
736
|
}
|
|
736
737
|
stream.length = 0;
|
|
737
|
-
|
|
738
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
739
|
+
stream.push(tokens[i]);
|
|
740
|
+
}
|
|
738
741
|
return { success, errors };
|
|
739
742
|
}
|
|
740
743
|
/**
|
|
@@ -775,7 +778,7 @@ function matchAllSyntaxes(syntaxes, context, options) {
|
|
|
775
778
|
message: result.errors[0]?.message || "could not match syntax",
|
|
776
779
|
node: result.token,
|
|
777
780
|
syntax: result.syntaxToken,
|
|
778
|
-
location: options.source.getSourceLocation((result.token?.[
|
|
781
|
+
location: options.source.getSourceLocation((result.token?.[LOCSTA] ?? context.tokens.at(-1)?.[LOCSTA])),
|
|
779
782
|
},
|
|
780
783
|
]
|
|
781
784
|
: result.errors,
|
|
@@ -870,7 +873,7 @@ function matchOccurenceSyntax(syntax, context, options) {
|
|
|
870
873
|
action: "drop",
|
|
871
874
|
message: "could not match syntax",
|
|
872
875
|
node: context.peek(),
|
|
873
|
-
// location: options.source!.getSourceLocation(context.peek()?.[
|
|
876
|
+
// location: options.source!.getSourceLocation(context.peek()?.[LOCSTA]),
|
|
874
877
|
},
|
|
875
878
|
],
|
|
876
879
|
syntaxToken: null,
|
package/dist/node.js
CHANGED
|
@@ -8,7 +8,7 @@ import { doRender } from './lib/renderer/render.js';
|
|
|
8
8
|
export { renderValue as renderToken } from './lib/renderer/render.js';
|
|
9
9
|
import { ModuleScopeEnumOptions } from './lib/ast/types.js';
|
|
10
10
|
export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, ValidationLevel } from './lib/ast/types.js';
|
|
11
|
-
import {
|
|
11
|
+
import { Tokenizer } from './lib/parser/tokenize.js';
|
|
12
12
|
import { dirname, resolve, matchUrl } from './lib/fs/resolve.js';
|
|
13
13
|
import { ResponseType } from './types.js';
|
|
14
14
|
import { resolve as resolve$1 } from 'node:path';
|
|
@@ -52,6 +52,9 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
52
52
|
if (responseType == ResponseType.ArrayBuffer) {
|
|
53
53
|
return response.arrayBuffer();
|
|
54
54
|
}
|
|
55
|
+
if (responseType == ResponseType.JSON) {
|
|
56
|
+
return response.json();
|
|
57
|
+
}
|
|
55
58
|
return responseType == ResponseType.ReadableStream
|
|
56
59
|
? response.body
|
|
57
60
|
: response.text();
|
|
@@ -60,8 +63,8 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
60
63
|
try {
|
|
61
64
|
const stats = await lstat(resolved.absolute);
|
|
62
65
|
if (stats.isFile()) {
|
|
63
|
-
if (responseType == ResponseType.Text) {
|
|
64
|
-
return readFile(resolved.absolute, "utf-8");
|
|
66
|
+
if (responseType == ResponseType.Text || responseType == ResponseType.JSON) {
|
|
67
|
+
return readFile(resolved.absolute, "utf-8").then((buffer) => responseType == ResponseType.JSON ? JSON.parse(buffer) : buffer);
|
|
65
68
|
}
|
|
66
69
|
if (responseType == ResponseType.ArrayBuffer) {
|
|
67
70
|
return readFile(resolved.absolute).then((buffer) => buffer.buffer);
|
|
@@ -72,9 +75,7 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
72
75
|
}));
|
|
73
76
|
}
|
|
74
77
|
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
console.warn(error);
|
|
77
|
-
}
|
|
78
|
+
catch (error) { }
|
|
78
79
|
throw new Error(`File not found: '${resolved.absolute || url}'`);
|
|
79
80
|
}
|
|
80
81
|
/**
|
|
@@ -190,8 +191,10 @@ function parseSync(...args) {
|
|
|
190
191
|
position: 0,
|
|
191
192
|
currentPosition: 0,
|
|
192
193
|
};
|
|
193
|
-
const result = doParseSync(
|
|
194
|
-
return
|
|
194
|
+
const result = doParseSync(new Tokenizer(options.parseInfo), options);
|
|
195
|
+
return options.module == null && options.inputSourceMap == null && !options.sourcemap
|
|
196
|
+
? result
|
|
197
|
+
: parseResult(result, options);
|
|
195
198
|
}
|
|
196
199
|
/**
|
|
197
200
|
* Transform CSS
|
|
@@ -348,7 +351,11 @@ async function parse(...args) {
|
|
|
348
351
|
position: 0,
|
|
349
352
|
currentPosition: 0,
|
|
350
353
|
};
|
|
351
|
-
return doParse(stream instanceof ReadableStream
|
|
354
|
+
return doParse(stream instanceof ReadableStream
|
|
355
|
+
? new Tokenizer(options.parseInfo, stream).tokenizeStream()
|
|
356
|
+
: new Tokenizer(options.parseInfo), options).then((result) => options.module == null && options.inputSourceMap == null && !options.sourcemap
|
|
357
|
+
? result
|
|
358
|
+
: parseResult(result, options));
|
|
352
359
|
}
|
|
353
360
|
/**
|
|
354
361
|
* Transform CSS file
|
package/dist/types.d.ts
CHANGED
package/dist/types.js
CHANGED
|
@@ -15,6 +15,10 @@ var ResponseType;
|
|
|
15
15
|
* return an arraybuffer
|
|
16
16
|
*/
|
|
17
17
|
ResponseType[ResponseType["ArrayBuffer"] = 2] = "ArrayBuffer";
|
|
18
|
+
/**
|
|
19
|
+
* return a json object
|
|
20
|
+
*/
|
|
21
|
+
ResponseType[ResponseType["JSON"] = 3] = "JSON";
|
|
18
22
|
})(ResponseType || (ResponseType = {}));
|
|
19
23
|
|
|
20
24
|
export { ResponseType };
|
package/dist/utils/sync.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { EnumToken } from '../lib/ast/types.js';
|
|
2
|
+
import { dirname } from '../lib/fs/resolve.js';
|
|
3
|
+
import { ResponseType } from '../types.js';
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* parse result. process input sourcemap
|
|
@@ -17,7 +19,26 @@ function parseResult(result, options) {
|
|
|
17
19
|
const token = result.ast.chi.at(-1);
|
|
18
20
|
if (token?.typ == EnumToken.CommentTokenType &&
|
|
19
21
|
token.val.startsWith("/*# sourceMappingURL=")) {
|
|
20
|
-
|
|
22
|
+
let data = token.val.slice(21, -2).trim();
|
|
23
|
+
if (data.endsWith(".map")) {
|
|
24
|
+
if (options.load == null) {
|
|
25
|
+
data = "";
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
options
|
|
29
|
+
.load(options.resolve(data, dirname(options.src)).absolute, ".", ResponseType.JSON)
|
|
30
|
+
.catch((error) => console.error({ error }))
|
|
31
|
+
.then((res) => {
|
|
32
|
+
if (res != null) {
|
|
33
|
+
// @ts-expect-error
|
|
34
|
+
options.source.setInputSourceMap(res);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
options.source.setInputSourceMap(data);
|
|
41
|
+
}
|
|
21
42
|
}
|
|
22
43
|
}
|
|
23
44
|
}
|
package/dist/web.js
CHANGED
|
@@ -4,7 +4,7 @@ import { doRender } from './lib/renderer/render.js';
|
|
|
4
4
|
export { renderValue as renderToken } from './lib/renderer/render.js';
|
|
5
5
|
import { ModuleScopeEnumOptions } from './lib/ast/types.js';
|
|
6
6
|
export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, ValidationLevel } from './lib/ast/types.js';
|
|
7
|
-
import {
|
|
7
|
+
import { Tokenizer } from './lib/parser/tokenize.js';
|
|
8
8
|
import { matchUrl, resolve, dirname } from './lib/fs/resolve.js';
|
|
9
9
|
import { ResponseType } from './types.js';
|
|
10
10
|
import { SourceFile } from './lib/parser/source.js';
|
|
@@ -58,6 +58,9 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
58
58
|
if (responseType == ResponseType.ArrayBuffer) {
|
|
59
59
|
return response.arrayBuffer();
|
|
60
60
|
}
|
|
61
|
+
if (responseType == ResponseType.JSON) {
|
|
62
|
+
return response.json();
|
|
63
|
+
}
|
|
61
64
|
return responseType == ResponseType.ReadableStream ? response.body : response.text();
|
|
62
65
|
});
|
|
63
66
|
}
|
|
@@ -184,8 +187,10 @@ function parseSync(...args) {
|
|
|
184
187
|
position: 0,
|
|
185
188
|
currentPosition: 0,
|
|
186
189
|
};
|
|
187
|
-
const result = doParseSync(
|
|
188
|
-
return
|
|
190
|
+
const result = doParseSync(new Tokenizer(options.parseInfo), options);
|
|
191
|
+
return options.module == null && options.inputSourceMap == null && !options.sourcemap
|
|
192
|
+
? result
|
|
193
|
+
: parseResult(result, options);
|
|
189
194
|
}
|
|
190
195
|
/**
|
|
191
196
|
* Transform CSS
|
|
@@ -318,7 +323,11 @@ async function parse(...args) {
|
|
|
318
323
|
position: 0,
|
|
319
324
|
currentPosition: 0,
|
|
320
325
|
};
|
|
321
|
-
return doParse(stream instanceof ReadableStream
|
|
326
|
+
return doParse(stream instanceof ReadableStream
|
|
327
|
+
? new Tokenizer(options.parseInfo, stream).tokenizeStream()
|
|
328
|
+
: new Tokenizer(options.parseInfo), options).then((result) => options.module == null && options.inputSourceMap == null && !options.sourcemap
|
|
329
|
+
? result
|
|
330
|
+
: parseResult(result, options));
|
|
322
331
|
}
|
|
323
332
|
/**
|
|
324
333
|
* Transform CSS file
|
package/package.json
CHANGED
package/llms.txt
DELETED
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
# css-parser
|
|
2
|
-
|
|
3
|
-
## Project overview
|
|
4
|
-
- This repository contains css-parser, a dependency-free all-in-one CSS parsing solution for Node.js and browsers.
|
|
5
|
-
- The library follows the CSS Syntax Module Level 3 specification and uses MDN syntax data for validation.
|
|
6
|
-
- It is designed for fault-tolerant parsing, full AST and typed-token analysis, AST manipulation, CSS Modules, minification, color processing, and syntax lowering.
|
|
7
|
-
- CSS is always fully parsed into a structured AST, and token values are typed so plugins and transforms can work against semantic data instead of raw strings.
|
|
8
|
-
|
|
9
|
-
## Installation
|
|
10
|
-
- npm: npm install @tbela99/css-parser
|
|
11
|
-
- jsr: deno add @tbela99/css-parser
|
|
12
|
-
- Main entry points:
|
|
13
|
-
- @tbela99/css-parser or @tbela99/css-parser/node
|
|
14
|
-
- @tbela99/css-parser/web
|
|
15
|
-
|
|
16
|
-
## Core APIs
|
|
17
|
-
- parse(css, options): async api to parse CSS and returns an AST, errors, and stats.
|
|
18
|
-
- parseSync(css, options): sync api to parse CSS and returns an AST, errors, and stats.
|
|
19
|
-
- render(ast, options): renders an AST back to CSS text.
|
|
20
|
-
- transform(css, options): async api to parse CSS and generate output in one step.
|
|
21
|
-
- transformSync(css, options): sync api to parse CSS and generate output in one step.
|
|
22
|
-
- walk(ast, filter): traverses the AST for custom transformations.
|
|
23
|
-
|
|
24
|
-
## Common capabilities
|
|
25
|
-
- Parse CSS into an AST and inspect or modify it.
|
|
26
|
-
- Validate declarations against CSS syntax definitions.
|
|
27
|
-
- Support CSS Modules with scoped class generation.
|
|
28
|
-
- Minify CSS safely with options such as inlineCssVariables, computeCalcExpression, removeDuplicateDeclarations, and beautify.
|
|
29
|
-
- Transform ASTs through visitors and custom traversal logic.
|
|
30
|
-
- Support plugin-style extensions through the visitor API, where custom handlers can observe and mutate AST nodes during enter/visit/leave phases.
|
|
31
|
-
- Lower modern CSS syntax such as nested CSS and if() to broadly compatible output.
|
|
32
|
-
- Generate source maps and handle advanced color functions and conversions.
|
|
33
|
-
|
|
34
|
-
## Visitor-based plugin model
|
|
35
|
-
- The parser exposes a visitor option that accepts node-specific handlers keyed by AST node type and event type.
|
|
36
|
-
- Plugins can be implemented as reusable visitor maps that inspect or transform nodes such as Rule, AtRule, Declaration, KeyframesRule, and Value nodes.
|
|
37
|
-
- This allows extension code to run alongside the core parser without modifying the parser itself.
|
|
38
|
-
|
|
39
|
-
## Typical usage
|
|
40
|
-
```ts
|
|
41
|
-
import {transform, ColorType} from '@tbela99/css-parser';
|
|
42
|
-
|
|
43
|
-
const css = `.foo { color: red; }`;
|
|
44
|
-
const result = await transform(css, {
|
|
45
|
-
beautify: true,
|
|
46
|
-
convertColor: ColorType.SRGB,
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
console.log(result.code);
|
|
50
|
-
```
|
|
51
|
-
|
|
52
|
-
## Important behavior notes
|
|
53
|
-
- parse(), parseSync(), transform() and transformSync() are lenient by default and preserve unknown constructs unless configured otherwise.
|
|
54
|
-
- Comments are removed by default; preserve them with removeComments: false or preserveLicense: true.
|
|
55
|
-
- Validation errors are available through the parse/transform result and through node.state and node.errors.
|
|
56
|
-
- The library prioritizes compact output while preserving semantics.
|
|
57
|
-
|
|
58
|
-
## Useful concepts
|
|
59
|
-
- parse(), parseSync() return AST, errors, and stats.
|
|
60
|
-
- render() turns an AST into CSS text.
|
|
61
|
-
- transform(), transformSync() perform parsing and rendering together.
|
|
62
|
-
- AST node types include StyleSheet, Rule, AtRule, Declaration, Comment, and Keyframes variants.
|
|
63
|
-
|
|
64
|
-
## Documentation files
|
|
65
|
-
- files/getting-started.md
|
|
66
|
-
- files/usage.md
|
|
67
|
-
- files/validation.md
|
|
68
|
-
- files/css-module.md
|
|
69
|
-
- files/minification.md
|
|
70
|
-
- files/transform.md
|
|
71
|
-
- files/syntax-lowering.md
|
|
72
|
-
- files/ast.md
|
|
73
|
-
- files/utilities.md
|
|
74
|
-
|
|
75
|
-
## Repository layout
|
|
76
|
-
- src/: parser implementation and core library code
|
|
77
|
-
- test/: regression and behavior tests
|
|
78
|
-
- files/: documentation content used by the docs site
|
|
79
|
-
- benchmark/: performance and minification benchmarks
|
|
80
|
-
- scripts/: helper scripts and patches
|