@tbela99/css-parser 1.4.11 → 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 +39 -0
- package/README.md +18 -11
- package/dist/index-umd-web.js +6558 -5310
- package/dist/index.cjs +6576 -5313
- package/dist/index.d.ts +805 -279
- package/dist/lib/ast/clone.js +13 -8
- package/dist/lib/ast/expand.js +65 -15
- package/dist/lib/ast/features/calc.js +19 -56
- package/dist/lib/ast/features/if.js +28 -4
- package/dist/lib/ast/features/inlinecssvariables.js +6 -5
- package/dist/lib/ast/features/prefix.js +52 -73
- package/dist/lib/ast/features/shorthand.js +11 -6
- package/dist/lib/ast/features/transform.js +6 -3
- package/dist/lib/ast/find.js +7 -5
- package/dist/lib/ast/math/expression.js +190 -32
- package/dist/lib/ast/minify.js +117 -49
- package/dist/lib/ast/node.js +52 -0
- 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/types.js +1 -1
- package/dist/lib/ast/walk.js +55 -11
- package/dist/lib/fs/resolve.js +68 -27
- package/dist/lib/parser/declaration/list.js +22 -7
- package/dist/lib/parser/declaration/map.js +40 -13
- package/dist/lib/parser/declaration/set.js +3 -1
- package/dist/lib/parser/linesmap.js +2 -12
- package/dist/lib/parser/parse.js +843 -912
- package/dist/lib/parser/source.js +16 -2
- package/dist/lib/parser/tokenize.js +1489 -725
- package/dist/lib/parser/utils/at-rule-container.js +29 -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 +164 -141
- package/dist/lib/parser/utils/text.js +4 -2
- package/dist/lib/renderer/render.js +230 -125
- package/dist/lib/renderer/sourcemap/lib/codec.js +78 -0
- package/dist/lib/renderer/sourcemap/sourcemap.js +187 -34
- 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 +45 -258
- package/dist/lib/validation/config.json.js +3 -0
- package/dist/lib/validation/match.js +110 -27
- package/dist/node.js +55 -33
- package/dist/types.d.ts +5 -1
- package/dist/types.js +4 -0
- package/dist/utils/sync.d.ts +16 -0
- package/dist/utils/sync.js +73 -0
- package/dist/web.js +37 -30
- package/package.json +1 -1
- package/dist/lib/renderer/sourcemap/lib/encode.js +0 -37
- package/llms.txt +0 -73
|
@@ -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';
|
|
@@ -10,11 +10,16 @@ import { parseTokens } from '../parser/parse.js';
|
|
|
10
10
|
const config = getSyntaxConfig();
|
|
11
11
|
// @ts-expect-error
|
|
12
12
|
const allValues = config.declarations.all.syntax.split(/[\s|]+/g);
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
/**
|
|
14
|
+
* @type {Array.<EnumToken>}
|
|
15
|
+
*/
|
|
16
|
+
const funcTypes = Array.from(tokensfuncDefMap.values());
|
|
17
|
+
funcTypes.push(EnumToken.FunctionTokenType, EnumToken.PseudoClassFuncTokenType);
|
|
18
|
+
/**
|
|
19
|
+
* trim leading and trailing whitespace
|
|
20
|
+
* @param tokens
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
18
23
|
function trimArray(tokens) {
|
|
19
24
|
while (tokens[0]?.typ === EnumToken.WhitespaceTokenType) {
|
|
20
25
|
tokens.shift();
|
|
@@ -115,6 +120,11 @@ function isMFValue(featureName, tokens, isMFRange) {
|
|
|
115
120
|
success: true,
|
|
116
121
|
};
|
|
117
122
|
}
|
|
123
|
+
/**
|
|
124
|
+
* create validation context
|
|
125
|
+
* @param tokens
|
|
126
|
+
* @returns
|
|
127
|
+
*/
|
|
118
128
|
function createValidationContext(tokens) {
|
|
119
129
|
tokens = trimArray(tokens.filter((t) => t.typ !== EnumToken.CommentTokenType));
|
|
120
130
|
if (tokens.at(-1)?.typ === EnumToken.ImportantTokenType) {
|
|
@@ -278,6 +288,14 @@ function createValidationContext(tokens) {
|
|
|
278
288
|
};
|
|
279
289
|
return token;
|
|
280
290
|
}
|
|
291
|
+
/**
|
|
292
|
+
* match selector syntax
|
|
293
|
+
* @param stream
|
|
294
|
+
* @param errors
|
|
295
|
+
* @param options
|
|
296
|
+
* @param nested
|
|
297
|
+
* @returns
|
|
298
|
+
*/
|
|
281
299
|
function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
282
300
|
const stack = [];
|
|
283
301
|
const tokens = [];
|
|
@@ -319,7 +337,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
319
337
|
message: `Unexpected token ${EnumToken[stream[i].typ]}`,
|
|
320
338
|
node: stream[i],
|
|
321
339
|
// @ts-expect-error
|
|
322
|
-
location: options.source.getSourceLocation(stream[i][
|
|
340
|
+
location: options.source.getSourceLocation(stream[i][LOCSTA]),
|
|
323
341
|
},
|
|
324
342
|
],
|
|
325
343
|
};
|
|
@@ -354,7 +372,9 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
354
372
|
if (!result.success) {
|
|
355
373
|
success = false;
|
|
356
374
|
if (result.errors.length > 0) {
|
|
357
|
-
|
|
375
|
+
for (const error of result.errors) {
|
|
376
|
+
errors.push(error);
|
|
377
|
+
}
|
|
358
378
|
}
|
|
359
379
|
}
|
|
360
380
|
}
|
|
@@ -373,7 +393,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
373
393
|
message: `Nesting selector is not allowed`,
|
|
374
394
|
node: token,
|
|
375
395
|
// @ts-expect-error
|
|
376
|
-
location: options.source.getSourceLocation(token[
|
|
396
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
377
397
|
},
|
|
378
398
|
],
|
|
379
399
|
};
|
|
@@ -407,7 +427,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
407
427
|
message: `Unexpected combinator ${EnumToken[token.typ]}`,
|
|
408
428
|
node: token,
|
|
409
429
|
// @ts-expect-error
|
|
410
|
-
location: options.source.getSourceLocation(token[
|
|
430
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
411
431
|
},
|
|
412
432
|
],
|
|
413
433
|
};
|
|
@@ -451,7 +471,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
451
471
|
message: `Unexpected token ${EnumToken[token.typ]}`,
|
|
452
472
|
node: token,
|
|
453
473
|
// @ts-expect-error
|
|
454
|
-
location: options.source.getSourceLocation(token[
|
|
474
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
455
475
|
},
|
|
456
476
|
],
|
|
457
477
|
};
|
|
@@ -502,7 +522,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
502
522
|
message: `Unexpected token ${EnumToken[slice[0].typ]}`,
|
|
503
523
|
node: slice[0],
|
|
504
524
|
// @ts-expect-error
|
|
505
|
-
location: options.source.getSourceLocation(slice[0][
|
|
525
|
+
location: options.source.getSourceLocation(slice[0][LOCSTA]),
|
|
506
526
|
},
|
|
507
527
|
],
|
|
508
528
|
};
|
|
@@ -514,8 +534,8 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
514
534
|
// errors: [
|
|
515
535
|
// {
|
|
516
536
|
// action: "drop",
|
|
517
|
-
// message: `Unexpected token ${EnumToken[slice[0].typ]} at ${slice[0][LOC]!.src}:${slice[0][
|
|
518
|
-
// slice[0][
|
|
537
|
+
// message: `Unexpected token ${EnumToken[slice[0].typ]} at ${slice[0][LOC]!.src}:${slice[0][LOCSTA].lin}:${
|
|
538
|
+
// slice[0][LOCSTA].col
|
|
519
539
|
// }`,
|
|
520
540
|
// node: slice[0],
|
|
521
541
|
// location: slice[0][LOC],
|
|
@@ -553,8 +573,8 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
553
573
|
// errors: [
|
|
554
574
|
// {
|
|
555
575
|
// action: "drop",
|
|
556
|
-
// message: `Unexpected token ${EnumToken[token.typ]} at ${token[LOC]!.src}:${token[
|
|
557
|
-
// token[
|
|
576
|
+
// message: `Unexpected token ${EnumToken[token.typ]} at ${token[LOC]!.src}:${token[LOCSTA].lin}:${
|
|
577
|
+
// token[LOCSTA].col
|
|
558
578
|
// }`,
|
|
559
579
|
// node: token,
|
|
560
580
|
// location: token[LOC],
|
|
@@ -586,8 +606,8 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
586
606
|
// errors: [
|
|
587
607
|
// {
|
|
588
608
|
// action: "drop",
|
|
589
|
-
// message: `Unexpected token ${EnumToken[token.typ]} at ${token[LOC]!.src}:${token[
|
|
590
|
-
// token[
|
|
609
|
+
// message: `Unexpected token ${EnumToken[token.typ]} at ${token[LOC]!.src}:${token[LOCSTA].lin}:${
|
|
610
|
+
// token[LOCSTA].col
|
|
591
611
|
// }`,
|
|
592
612
|
// node: token,
|
|
593
613
|
// location: token[LOC],
|
|
@@ -616,7 +636,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
616
636
|
message: `Unexpected token ${EnumToken[token.typ]}`,
|
|
617
637
|
node: token,
|
|
618
638
|
// @ts-expect-error
|
|
619
|
-
location: options.source.getSourceLocation(token[
|
|
639
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
620
640
|
},
|
|
621
641
|
],
|
|
622
642
|
};
|
|
@@ -645,7 +665,9 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
645
665
|
if (!result.success) {
|
|
646
666
|
success = false;
|
|
647
667
|
if (result.errors.length > 0) {
|
|
648
|
-
|
|
668
|
+
for (const error of result.errors) {
|
|
669
|
+
errors.push(error);
|
|
670
|
+
}
|
|
649
671
|
}
|
|
650
672
|
}
|
|
651
673
|
stack.pop();
|
|
@@ -659,7 +681,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
659
681
|
message: `Unexpected token ${EnumToken[token.typ]}`,
|
|
660
682
|
node: token,
|
|
661
683
|
// @ts-expect-error
|
|
662
|
-
location: options.source.getSourceLocation(token[
|
|
684
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
663
685
|
},
|
|
664
686
|
],
|
|
665
687
|
};
|
|
@@ -681,7 +703,7 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
681
703
|
message: `Unsupported selector token ${EnumToken[token.typ]}`,
|
|
682
704
|
node: token,
|
|
683
705
|
// @ts-expect-error
|
|
684
|
-
location: options.source.getSourceLocation(token[
|
|
706
|
+
location: options.source.getSourceLocation(token[LOCSTA]),
|
|
685
707
|
},
|
|
686
708
|
],
|
|
687
709
|
};
|
|
@@ -707,15 +729,24 @@ function matchSelectorSyntax(stream, errors, options, nested = true) {
|
|
|
707
729
|
message: `Unmatched token ${EnumToken[stack.at(-1).typ]}`,
|
|
708
730
|
node: stack.at(-1),
|
|
709
731
|
// @ts-expect-error
|
|
710
|
-
location: options.source.getSourceLocation(stack.at(-1)[
|
|
732
|
+
location: options.source.getSourceLocation(stack.at(-1)[LOCSTA]),
|
|
711
733
|
},
|
|
712
734
|
],
|
|
713
735
|
};
|
|
714
736
|
}
|
|
715
737
|
stream.length = 0;
|
|
716
|
-
|
|
738
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
739
|
+
stream.push(tokens[i]);
|
|
740
|
+
}
|
|
717
741
|
return { success, errors };
|
|
718
742
|
}
|
|
743
|
+
/**
|
|
744
|
+
* matches all syntaxes
|
|
745
|
+
* @param syntaxes
|
|
746
|
+
* @param context
|
|
747
|
+
* @param options
|
|
748
|
+
* @returns
|
|
749
|
+
*/
|
|
719
750
|
function matchAllSyntaxes(syntaxes, context, options) {
|
|
720
751
|
const result = matchSyntax(syntaxes, context, {
|
|
721
752
|
...options,
|
|
@@ -747,13 +778,20 @@ function matchAllSyntaxes(syntaxes, context, options) {
|
|
|
747
778
|
message: result.errors[0]?.message || "could not match syntax",
|
|
748
779
|
node: result.token,
|
|
749
780
|
syntax: result.syntaxToken,
|
|
750
|
-
location: options.source.getSourceLocation((result.token?.[
|
|
781
|
+
location: options.source.getSourceLocation((result.token?.[LOCSTA] ?? context.tokens.at(-1)?.[LOCSTA])),
|
|
751
782
|
},
|
|
752
783
|
]
|
|
753
784
|
: result.errors,
|
|
754
785
|
syntaxToken: !result.success ? result.syntaxToken : null,
|
|
755
786
|
};
|
|
756
787
|
}
|
|
788
|
+
/**
|
|
789
|
+
* matches a list of syntaxes
|
|
790
|
+
* @param syntax
|
|
791
|
+
* @param context
|
|
792
|
+
* @param options
|
|
793
|
+
* @returns
|
|
794
|
+
*/
|
|
757
795
|
function matchListSyntax(syntax, context, options) {
|
|
758
796
|
const { isList, match, isOptional, ...rest } = syntax;
|
|
759
797
|
let success = true;
|
|
@@ -798,6 +836,13 @@ function matchListSyntax(syntax, context, options) {
|
|
|
798
836
|
token: context.peek(),
|
|
799
837
|
};
|
|
800
838
|
}
|
|
839
|
+
/**
|
|
840
|
+
* matches a list of syntaxes
|
|
841
|
+
* @param syntax
|
|
842
|
+
* @param context
|
|
843
|
+
* @param options
|
|
844
|
+
* @returns
|
|
845
|
+
*/
|
|
801
846
|
function matchOccurenceSyntax(syntax, context, options) {
|
|
802
847
|
const { match, ...rest } = syntax;
|
|
803
848
|
let result = null;
|
|
@@ -828,7 +873,7 @@ function matchOccurenceSyntax(syntax, context, options) {
|
|
|
828
873
|
action: "drop",
|
|
829
874
|
message: "could not match syntax",
|
|
830
875
|
node: context.peek(),
|
|
831
|
-
// location: options.source!.getSourceLocation(context.peek()?.[
|
|
876
|
+
// location: options.source!.getSourceLocation(context.peek()?.[LOCSTA]),
|
|
832
877
|
},
|
|
833
878
|
],
|
|
834
879
|
syntaxToken: null,
|
|
@@ -839,6 +884,13 @@ function matchOccurenceSyntax(syntax, context, options) {
|
|
|
839
884
|
}
|
|
840
885
|
return result;
|
|
841
886
|
}
|
|
887
|
+
/**
|
|
888
|
+
* matches a list of syntaxes
|
|
889
|
+
* @param syntaxes
|
|
890
|
+
* @param context
|
|
891
|
+
* @param options
|
|
892
|
+
* @returns
|
|
893
|
+
*/
|
|
842
894
|
function matchSyntax(syntaxes, context, options) {
|
|
843
895
|
if (syntaxes == null) {
|
|
844
896
|
return {
|
|
@@ -941,6 +993,7 @@ function matchSyntax(syntaxes, context, options) {
|
|
|
941
993
|
return result;
|
|
942
994
|
}
|
|
943
995
|
if (tokensfuncDefMap.has(token.typ) &&
|
|
996
|
+
// @ts-ignore
|
|
944
997
|
token.typ === EnumToken.WildCardFunctionTokenDefType) {
|
|
945
998
|
const range = trimArray(context.peekRange());
|
|
946
999
|
result = matchSyntax(getParsedSyntax(ValidationSyntaxGroupEnum.Syntaxes, token.val + "()")?.[0]?.chi, createValidationContext(range.slice(1, -1)), options);
|
|
@@ -971,7 +1024,7 @@ function matchSyntax(syntaxes, context, options) {
|
|
|
971
1024
|
if (syntaxes[i].isList) {
|
|
972
1025
|
result = matchListSyntax(syntaxes[i], context.slice(), options);
|
|
973
1026
|
if (result.success) {
|
|
974
|
-
options.visited.get(token)
|
|
1027
|
+
options.visited.get(token)?.delete?.(syntaxes[i]);
|
|
975
1028
|
if (result.context.done()) {
|
|
976
1029
|
context.end();
|
|
977
1030
|
return {
|
|
@@ -1479,6 +1532,13 @@ function matchSyntax(syntaxes, context, options) {
|
|
|
1479
1532
|
errors: [],
|
|
1480
1533
|
};
|
|
1481
1534
|
}
|
|
1535
|
+
/**
|
|
1536
|
+
* matches a column of syntaxes
|
|
1537
|
+
* @param syntax
|
|
1538
|
+
* @param context
|
|
1539
|
+
* @param options
|
|
1540
|
+
* @returns
|
|
1541
|
+
*/
|
|
1482
1542
|
function matchColumnSyntax(syntax, context, options) {
|
|
1483
1543
|
let syntaxes = syntax.chi.slice();
|
|
1484
1544
|
let i = 0;
|
|
@@ -1515,6 +1575,13 @@ function matchColumnSyntax(syntax, context, options) {
|
|
|
1515
1575
|
errors: [],
|
|
1516
1576
|
};
|
|
1517
1577
|
}
|
|
1578
|
+
/**
|
|
1579
|
+
* matches an ampersand of syntaxes
|
|
1580
|
+
* @param syntax
|
|
1581
|
+
* @param context
|
|
1582
|
+
* @param options
|
|
1583
|
+
* @returns
|
|
1584
|
+
*/
|
|
1518
1585
|
function matchAmpersandSyntax(syntax, context, options) {
|
|
1519
1586
|
const syntaxes = [syntax.l, syntax.r];
|
|
1520
1587
|
let result;
|
|
@@ -1533,6 +1600,13 @@ function matchAmpersandSyntax(syntax, context, options) {
|
|
|
1533
1600
|
}
|
|
1534
1601
|
return result;
|
|
1535
1602
|
}
|
|
1603
|
+
/**
|
|
1604
|
+
* matches a property
|
|
1605
|
+
* @param property
|
|
1606
|
+
* @param context
|
|
1607
|
+
* @param options
|
|
1608
|
+
* @returns
|
|
1609
|
+
*/
|
|
1536
1610
|
function matchProperty(property, context, options) {
|
|
1537
1611
|
let success = false;
|
|
1538
1612
|
let t = context.peek()?.typ;
|
|
@@ -1920,7 +1994,9 @@ function matchProperty(property, context, options) {
|
|
|
1920
1994
|
// )
|
|
1921
1995
|
// ) {
|
|
1922
1996
|
const newRange = range.map((t) => cloneNode(t, true));
|
|
1997
|
+
// @ts-ignore
|
|
1923
1998
|
parseTokens(newRange, { parseColor: true }, errors);
|
|
1999
|
+
// @ts-ignore
|
|
1924
2000
|
success = newRange.length == 1 && isColor(newRange[0], errors);
|
|
1925
2001
|
if (success) {
|
|
1926
2002
|
context.update(range.at(-1));
|
|
@@ -2265,6 +2341,13 @@ function matchProperty(property, context, options) {
|
|
|
2265
2341
|
errors: [],
|
|
2266
2342
|
};
|
|
2267
2343
|
}
|
|
2344
|
+
/**
|
|
2345
|
+
* matches a repeatable syntax
|
|
2346
|
+
* @param syntax
|
|
2347
|
+
* @param context
|
|
2348
|
+
* @param options
|
|
2349
|
+
* @returns
|
|
2350
|
+
*/
|
|
2268
2351
|
function matchRepeatableSyntax(syntax, context, options) {
|
|
2269
2352
|
const { isRepeatable, isOptional, isMandatatoryGroup, isRepeatableAtLeastOnce, ...rest } = syntax;
|
|
2270
2353
|
let result = null;
|
package/dist/node.js
CHANGED
|
@@ -8,12 +8,13 @@ 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';
|
|
15
15
|
import { SourceFile } from './lib/parser/source.js';
|
|
16
16
|
import { cwd } from 'node:process';
|
|
17
|
+
import { parseResult, validateSyncArguments } from './utils/sync.js';
|
|
17
18
|
export { minify } from './lib/ast/minify.js';
|
|
18
19
|
export { expand } from './lib/ast/expand.js';
|
|
19
20
|
export { WalkerEvent, WalkerOptionEnum, walk, walkValues } from './lib/ast/walk.js';
|
|
@@ -24,6 +25,7 @@ export { cloneNode } from './lib/ast/clone.js';
|
|
|
24
25
|
export { replaceNodeOrValue } from './lib/parser/utils/token.js';
|
|
25
26
|
export { SourceMap } from './lib/renderer/sourcemap/sourcemap.js';
|
|
26
27
|
export { FeatureWalkMode } from './lib/ast/features/type.js';
|
|
28
|
+
export { getNodeProperty, setNodeProperty } from './lib/ast/node.js';
|
|
27
29
|
|
|
28
30
|
/**
|
|
29
31
|
* Load file or url
|
|
@@ -50,6 +52,9 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
50
52
|
if (responseType == ResponseType.ArrayBuffer) {
|
|
51
53
|
return response.arrayBuffer();
|
|
52
54
|
}
|
|
55
|
+
if (responseType == ResponseType.JSON) {
|
|
56
|
+
return response.json();
|
|
57
|
+
}
|
|
53
58
|
return responseType == ResponseType.ReadableStream
|
|
54
59
|
? response.body
|
|
55
60
|
: response.text();
|
|
@@ -58,8 +63,8 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
58
63
|
try {
|
|
59
64
|
const stats = await lstat(resolved.absolute);
|
|
60
65
|
if (stats.isFile()) {
|
|
61
|
-
if (responseType == ResponseType.Text) {
|
|
62
|
-
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);
|
|
63
68
|
}
|
|
64
69
|
if (responseType == ResponseType.ArrayBuffer) {
|
|
65
70
|
return readFile(resolved.absolute).then((buffer) => buffer.buffer);
|
|
@@ -70,9 +75,7 @@ async function load(url, currentDirectory = ".", responseType = false) {
|
|
|
70
75
|
}));
|
|
71
76
|
}
|
|
72
77
|
}
|
|
73
|
-
catch (error) {
|
|
74
|
-
console.warn(error);
|
|
75
|
-
}
|
|
78
|
+
catch (error) { }
|
|
76
79
|
throw new Error(`File not found: '${resolved.absolute || url}'`);
|
|
77
80
|
}
|
|
78
81
|
/**
|
|
@@ -108,7 +111,7 @@ function render(data, options = {}, mapping) {
|
|
|
108
111
|
return doRender(data, Object.assign(options, { resolve, dirname, cwd: options.cwd ?? resolve$1() }), mapping);
|
|
109
112
|
}
|
|
110
113
|
/**
|
|
111
|
-
* Parse
|
|
114
|
+
* Parse CSS file
|
|
112
115
|
* @param file url or path
|
|
113
116
|
* @param options
|
|
114
117
|
* @param asStream load file as stream
|
|
@@ -134,17 +137,17 @@ function render(data, options = {}, mapping) {
|
|
|
134
137
|
*/
|
|
135
138
|
const parseFile = deprecate(async (file, options = {}, asStream = false) => parse({ file, asStream, ...options }), "parseFile is deprecated, use parse instead as parse({file, asStream, ...options})");
|
|
136
139
|
/**
|
|
137
|
-
* Parse
|
|
140
|
+
* Parse CSS
|
|
138
141
|
* @param args
|
|
139
142
|
*
|
|
140
143
|
* Parsing a string
|
|
141
144
|
*
|
|
142
145
|
* ```ts
|
|
143
146
|
*
|
|
144
|
-
* import {
|
|
147
|
+
* import {parseSync} from '@tbela99/css-parser';
|
|
145
148
|
*
|
|
146
149
|
* // css string
|
|
147
|
-
* let result =
|
|
150
|
+
* let result = parseSync(css, {nestingRules: true});
|
|
148
151
|
* console.log(result.ast);
|
|
149
152
|
* ```
|
|
150
153
|
*
|
|
@@ -161,9 +164,12 @@ function parseSync(...args) {
|
|
|
161
164
|
options = opt;
|
|
162
165
|
stream = input;
|
|
163
166
|
}
|
|
167
|
+
if (options != null) {
|
|
168
|
+
validateSyncArguments(options);
|
|
169
|
+
}
|
|
164
170
|
options ??= {};
|
|
165
171
|
options.src ??= "";
|
|
166
|
-
options.sourcesMap ??= new Map;
|
|
172
|
+
options.sourcesMap ??= new Map();
|
|
167
173
|
Object.assign(options, {
|
|
168
174
|
resolve,
|
|
169
175
|
dirname,
|
|
@@ -183,26 +189,26 @@ function parseSync(...args) {
|
|
|
183
189
|
time: 0,
|
|
184
190
|
source: options.source,
|
|
185
191
|
position: 0,
|
|
186
|
-
currentPosition:
|
|
192
|
+
currentPosition: 0,
|
|
187
193
|
};
|
|
188
|
-
const result = doParseSync(
|
|
189
|
-
|
|
190
|
-
|
|
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);
|
|
191
198
|
}
|
|
192
199
|
/**
|
|
193
|
-
* Transform
|
|
194
|
-
* @param css
|
|
195
|
-
* @param options
|
|
200
|
+
* Transform CSS
|
|
196
201
|
*
|
|
197
202
|
* ```ts
|
|
198
203
|
*
|
|
199
|
-
* import {
|
|
204
|
+
* import {transformSync} from '@tbela99/css-parser';
|
|
200
205
|
*
|
|
201
206
|
* // css string
|
|
202
|
-
* const result =
|
|
207
|
+
* const result = transformSync(css);
|
|
203
208
|
* console.log(result.code);
|
|
204
209
|
* ```
|
|
205
210
|
*
|
|
211
|
+
* @param args
|
|
206
212
|
*/
|
|
207
213
|
function transformSync(...args) {
|
|
208
214
|
let options;
|
|
@@ -218,7 +224,15 @@ function transformSync(...args) {
|
|
|
218
224
|
stream = input;
|
|
219
225
|
}
|
|
220
226
|
options ??= {};
|
|
221
|
-
options
|
|
227
|
+
if (options.minify == null) {
|
|
228
|
+
options.minify = true;
|
|
229
|
+
}
|
|
230
|
+
if (options.removeEmpty == null) {
|
|
231
|
+
options.removeEmpty = true;
|
|
232
|
+
}
|
|
233
|
+
if (options.removeCharset == null) {
|
|
234
|
+
options.removeCharset = true;
|
|
235
|
+
}
|
|
222
236
|
const startTime = performance.now();
|
|
223
237
|
const parseResult = parseSync(stream, options);
|
|
224
238
|
let mapping = null;
|
|
@@ -251,7 +265,7 @@ function transformSync(...args) {
|
|
|
251
265
|
};
|
|
252
266
|
}
|
|
253
267
|
/**
|
|
254
|
-
* Parse
|
|
268
|
+
* Parse CSS
|
|
255
269
|
* @param args
|
|
256
270
|
*
|
|
257
271
|
* @throws Error file not found
|
|
@@ -314,7 +328,7 @@ async function parse(...args) {
|
|
|
314
328
|
}
|
|
315
329
|
options ??= {};
|
|
316
330
|
options.src ??= "";
|
|
317
|
-
options.sourcesMap ??= new Map;
|
|
331
|
+
options.sourcesMap ??= new Map();
|
|
318
332
|
Object.assign(options, {
|
|
319
333
|
load,
|
|
320
334
|
resolve,
|
|
@@ -335,15 +349,16 @@ async function parse(...args) {
|
|
|
335
349
|
time: 0,
|
|
336
350
|
source: options.source,
|
|
337
351
|
position: 0,
|
|
338
|
-
currentPosition:
|
|
352
|
+
currentPosition: 0,
|
|
339
353
|
};
|
|
340
|
-
return doParse(stream instanceof ReadableStream
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
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));
|
|
344
359
|
}
|
|
345
360
|
/**
|
|
346
|
-
* Transform
|
|
361
|
+
* Transform CSS file
|
|
347
362
|
* @param file url or path
|
|
348
363
|
* @param options
|
|
349
364
|
* @param asStream load file as stream
|
|
@@ -372,9 +387,7 @@ const transformFile = deprecate(async (file, options = {}, asStream = false) =>
|
|
|
372
387
|
...options,
|
|
373
388
|
}), "transformFile is deprecated, use transform instead as transform({file, asStream, ...options})");
|
|
374
389
|
/**
|
|
375
|
-
* Transform
|
|
376
|
-
* @param css
|
|
377
|
-
* @param options
|
|
390
|
+
* Transform CSS
|
|
378
391
|
*
|
|
379
392
|
* Parsing a string
|
|
380
393
|
*
|
|
@@ -413,6 +426,7 @@ const transformFile = deprecate(async (file, options = {}, asStream = false) =>
|
|
|
413
426
|
*
|
|
414
427
|
* console.log(result.code);
|
|
415
428
|
* ```
|
|
429
|
+
* @param args
|
|
416
430
|
*/
|
|
417
431
|
async function transform(...args) {
|
|
418
432
|
let options;
|
|
@@ -433,7 +447,15 @@ async function transform(...args) {
|
|
|
433
447
|
}
|
|
434
448
|
}
|
|
435
449
|
options ??= {};
|
|
436
|
-
options
|
|
450
|
+
if (options.minify == null) {
|
|
451
|
+
options.minify = true;
|
|
452
|
+
}
|
|
453
|
+
if (options.removeEmpty == null) {
|
|
454
|
+
options.removeEmpty = true;
|
|
455
|
+
}
|
|
456
|
+
if (options.removeCharset == null) {
|
|
457
|
+
options.removeCharset = true;
|
|
458
|
+
}
|
|
437
459
|
const startTime = performance.now();
|
|
438
460
|
return parse(stream, options).then((parseResult) => {
|
|
439
461
|
let mapping = null;
|
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 };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ParseResult, ParserOptions, ParserSyncOptions } from "../@types/index.d.ts";
|
|
2
|
+
/**
|
|
3
|
+
* parse result. process input sourcemap
|
|
4
|
+
* @param result
|
|
5
|
+
* @param options
|
|
6
|
+
* @returns
|
|
7
|
+
* @private
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseResult(result: ParseResult, options: ParserOptions): ParseResult;
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @param options
|
|
13
|
+
* @param prefix
|
|
14
|
+
* @private
|
|
15
|
+
*/
|
|
16
|
+
export declare function validateSyncArguments(options: ParserSyncOptions, prefix?: string): void;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { EnumToken } from '../lib/ast/types.js';
|
|
2
|
+
import { dirname } from '../lib/fs/resolve.js';
|
|
3
|
+
import { ResponseType } from '../types.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* parse result. process input sourcemap
|
|
7
|
+
* @param result
|
|
8
|
+
* @param options
|
|
9
|
+
* @returns
|
|
10
|
+
* @private
|
|
11
|
+
*/
|
|
12
|
+
function parseResult(result, options) {
|
|
13
|
+
if (options.sourcemap != null && options.source.getInputSourceMap() == null) {
|
|
14
|
+
if (options.inputSourceMap != null) {
|
|
15
|
+
options.source.setInputSourceMap(options.inputSourceMap);
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
// extract inline source map from the input CSS
|
|
19
|
+
const token = result.ast.chi.at(-1);
|
|
20
|
+
if (token?.typ == EnumToken.CommentTokenType &&
|
|
21
|
+
token.val.startsWith("/*# sourceMappingURL=")) {
|
|
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
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (options.module) {
|
|
46
|
+
const { revMapping, ...res } = result;
|
|
47
|
+
return res;
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
*
|
|
53
|
+
* @param options
|
|
54
|
+
* @param prefix
|
|
55
|
+
* @private
|
|
56
|
+
*/
|
|
57
|
+
function validateSyncArguments(options, prefix = "options.") {
|
|
58
|
+
const args = Object.entries(options);
|
|
59
|
+
let i;
|
|
60
|
+
for (i = 0; i < args.length; i++) {
|
|
61
|
+
const [key, value] = args[i];
|
|
62
|
+
if (typeof value == "function") {
|
|
63
|
+
if (value instanceof Promise || Object.getPrototypeOf(value).constructor.name == "AsyncFunction") {
|
|
64
|
+
throw new Error(`[${prefix + key}]: Async functions are not supported in sync mode. Use parse() or transform() instead.`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else if (value != null && typeof value == "object") {
|
|
68
|
+
validateSyncArguments(value, prefix + key + ".");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export { parseResult, validateSyncArguments };
|