@tbela99/css-parser 0.2.0 → 0.4.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/{LICENSE → LICENSE.md} +1 -1
- package/README.md +250 -77
- package/dist/config.json.js +245 -1
- package/dist/index-umd-web.js +4241 -1583
- package/dist/index.cjs +4242 -1584
- package/dist/index.d.ts +71 -23
- package/dist/lib/ast/expand.js +1 -1
- package/dist/lib/ast/features/calc.js +31 -192
- package/dist/lib/ast/features/index.js +3 -3
- package/dist/lib/ast/features/inlinecssvariables.js +6 -6
- package/dist/lib/ast/features/shorthand.js +5 -6
- package/dist/lib/ast/math/expression.js +220 -0
- package/dist/lib/ast/{features/utils → math}/math.js +4 -4
- package/dist/lib/ast/minify.js +0 -1
- package/dist/lib/ast/types.js +31 -13
- package/dist/lib/ast/utils/minifyfeature.js +4 -3
- package/dist/lib/ast/walk.js +24 -4
- package/dist/lib/fs/resolve.js +4 -3
- package/dist/lib/parser/declaration/list.js +6 -2
- package/dist/lib/parser/declaration/map.js +158 -24
- package/dist/lib/parser/declaration/set.js +42 -22
- package/dist/lib/parser/parse.js +345 -349
- package/dist/lib/parser/tokenize.js +220 -223
- package/dist/lib/parser/utils/declaration.js +67 -0
- package/dist/lib/parser/utils/syntax.js +172 -6
- package/dist/lib/parser/utils/type.js +2 -2
- package/dist/lib/renderer/color/a98rgb.js +64 -0
- package/dist/lib/renderer/color/color.js +521 -0
- package/dist/lib/renderer/color/colormix.js +337 -0
- package/dist/lib/renderer/color/hex.js +92 -0
- package/dist/lib/renderer/color/hsl.js +118 -0
- package/dist/lib/renderer/color/hsv.js +20 -0
- package/dist/lib/renderer/color/hwb.js +101 -0
- package/dist/lib/renderer/color/lab.js +136 -0
- package/dist/lib/renderer/color/lch.js +79 -0
- package/dist/lib/renderer/color/oklab.js +121 -0
- package/dist/lib/renderer/color/oklch.js +65 -0
- package/dist/lib/renderer/color/p3.js +57 -0
- package/dist/lib/renderer/color/prophotorgb.js +56 -0
- package/dist/lib/renderer/color/rec2020.js +70 -0
- package/dist/lib/renderer/color/relativecolor.js +152 -0
- package/dist/lib/renderer/color/rgb.js +44 -0
- package/dist/lib/renderer/color/srgb.js +261 -0
- package/dist/lib/renderer/color/utils/components.js +20 -0
- package/dist/lib/renderer/color/utils/constants.js +191 -0
- package/dist/lib/renderer/color/utils/matrix.js +35 -0
- package/dist/lib/renderer/color/xyz.js +64 -0
- package/dist/lib/renderer/color/xyzd50.js +33 -0
- package/dist/lib/renderer/render.js +128 -30
- package/dist/node/index.js +1 -1
- package/dist/node/load.js +1 -1
- package/dist/web/index.js +1 -1
- package/package.json +19 -18
- package/quickjs.sh +1 -0
- package/dist/lib/iterable/weakmap.js +0 -53
- package/dist/lib/renderer/utils/color.js +0 -499
- /package/dist/lib/iterable/{set.js → weakset.js} +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -56,21 +56,26 @@ declare enum EnumToken {
|
|
|
56
56
|
BadStringTokenType = 54,
|
|
57
57
|
BinaryExpressionTokenType = 55,
|
|
58
58
|
UnaryExpressionTokenType = 56,
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
59
|
+
FlexTokenType = 57,
|
|
60
|
+
ListToken = 58,
|
|
61
|
+
Add = 59,
|
|
62
|
+
Mul = 60,
|
|
63
|
+
Div = 61,
|
|
64
|
+
Sub = 62,
|
|
65
|
+
ColumnCombinatorTokenType = 63,
|
|
66
|
+
ContainMatchTokenType = 64,
|
|
67
|
+
StartMatchTokenType = 65,
|
|
68
|
+
EndMatchTokenType = 66,
|
|
69
|
+
MatchExpressionTokenType = 67,
|
|
70
|
+
NameSpaceAttributeTokenType = 68,
|
|
71
|
+
FractionTokenType = 69,
|
|
72
|
+
IdenListTokenType = 70,
|
|
73
|
+
GridTemplateFuncTokenType = 71,
|
|
71
74
|
Time = 25,
|
|
72
75
|
Iden = 7,
|
|
76
|
+
EOF = 47,
|
|
73
77
|
Hash = 28,
|
|
78
|
+
Flex = 57,
|
|
74
79
|
Angle = 24,
|
|
75
80
|
Color = 49,
|
|
76
81
|
Comma = 9,
|
|
@@ -85,7 +90,9 @@ declare enum EnumToken {
|
|
|
85
90
|
Frequency = 26,
|
|
86
91
|
Resolution = 27,
|
|
87
92
|
Whitespace = 36,
|
|
93
|
+
IdenList = 70,
|
|
88
94
|
DashedIden = 8,
|
|
95
|
+
GridTemplateFunc = 71,
|
|
89
96
|
ImageFunc = 19,
|
|
90
97
|
CommentNodeType = 0,
|
|
91
98
|
CDOCOMMNodeType = 1,
|
|
@@ -98,7 +105,7 @@ declare function minify(ast: AstNode, options?: ParserOptions | MinifyOptions, r
|
|
|
98
105
|
}): AstNode;
|
|
99
106
|
|
|
100
107
|
declare function walk(node: AstNode, filter?: WalkerFilter): Generator<WalkResult>;
|
|
101
|
-
declare function walkValues(values: Token[]): Generator<WalkAttributesResult>;
|
|
108
|
+
declare function walkValues(values: Token[], root?: AstNode | null, filter?: WalkerValueFilter): Generator<WalkAttributesResult>;
|
|
102
109
|
|
|
103
110
|
declare function expand(ast: AstNode): AstNode;
|
|
104
111
|
|
|
@@ -123,12 +130,19 @@ export declare interface LiteralToken extends BaseToken {
|
|
|
123
130
|
val: string;
|
|
124
131
|
}
|
|
125
132
|
|
|
133
|
+
|
|
126
134
|
export declare interface IdentToken extends BaseToken {
|
|
127
135
|
|
|
128
136
|
typ: EnumToken.IdenTokenType,
|
|
129
137
|
val: string;
|
|
130
138
|
}
|
|
131
139
|
|
|
140
|
+
export declare interface IdentListToken extends BaseToken {
|
|
141
|
+
|
|
142
|
+
typ: EnumToken.IdenListTokenType,
|
|
143
|
+
val: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
132
146
|
export declare interface DashedIdentToken extends BaseToken {
|
|
133
147
|
|
|
134
148
|
typ: EnumToken.DashedIdenTokenType,
|
|
@@ -168,6 +182,12 @@ export declare interface PercentageToken extends BaseToken {
|
|
|
168
182
|
val: string | FractionToken;
|
|
169
183
|
}
|
|
170
184
|
|
|
185
|
+
export declare interface FlexToken extends BaseToken {
|
|
186
|
+
|
|
187
|
+
typ: EnumToken.FlexTokenType,
|
|
188
|
+
val: string | FractionToken;
|
|
189
|
+
}
|
|
190
|
+
|
|
171
191
|
export declare interface FunctionToken extends BaseToken {
|
|
172
192
|
|
|
173
193
|
typ: EnumToken.FunctionTokenType,
|
|
@@ -175,6 +195,13 @@ export declare interface FunctionToken extends BaseToken {
|
|
|
175
195
|
chi: Token[];
|
|
176
196
|
}
|
|
177
197
|
|
|
198
|
+
export declare interface GridTemplateFuncToken extends BaseToken {
|
|
199
|
+
|
|
200
|
+
typ: EnumToken.GridTemplateFuncTokenType,
|
|
201
|
+
val: string;
|
|
202
|
+
chi: Token[];
|
|
203
|
+
}
|
|
204
|
+
|
|
178
205
|
export declare interface FunctionURLToken extends BaseToken {
|
|
179
206
|
|
|
180
207
|
typ: EnumToken.UrlFunctionTokenType,
|
|
@@ -195,6 +222,7 @@ export declare interface TimingFunctionToken extends BaseToken {
|
|
|
195
222
|
val: string;
|
|
196
223
|
chi: Token[];
|
|
197
224
|
}
|
|
225
|
+
|
|
198
226
|
export declare interface TimelineFunctionToken extends BaseToken {
|
|
199
227
|
|
|
200
228
|
typ: EnumToken.TimelineFunctionTokenType;
|
|
@@ -430,12 +458,18 @@ export declare interface ImportantToken extends BaseToken {
|
|
|
430
458
|
typ: EnumToken.ImportantTokenType;
|
|
431
459
|
}
|
|
432
460
|
|
|
461
|
+
export declare type ColorKind = 'lit' | 'hex' | 'rgb' | 'rgba' | 'hsl' | 'hsla' | 'hwb' | 'device-cmyk' | 'oklab' | 'oklch' | 'lab' | 'lch' | 'color';
|
|
462
|
+
|
|
463
|
+
// export declare type HueInterpolationMethod = 'shorter' | 'longer' | 'increasing' | 'decreasing';
|
|
464
|
+
|
|
433
465
|
export declare interface ColorToken extends BaseToken {
|
|
434
466
|
|
|
435
467
|
typ: EnumToken.ColorTokenType;
|
|
436
468
|
val: string;
|
|
437
|
-
kin:
|
|
469
|
+
kin: ColorKind;
|
|
438
470
|
chi?: Token[];
|
|
471
|
+
/* calculated */
|
|
472
|
+
cal?: 'rel' | 'mix';
|
|
439
473
|
}
|
|
440
474
|
|
|
441
475
|
export declare interface AttrToken extends BaseToken {
|
|
@@ -517,11 +551,13 @@ export declare type UnaryExpressionNode =
|
|
|
517
551
|
| AngleToken
|
|
518
552
|
| FrequencyToken;
|
|
519
553
|
|
|
520
|
-
export declare type BinaryExpressionNode = NumberToken | DimensionToken | PercentageToken | FractionToken |
|
|
554
|
+
export declare type BinaryExpressionNode = NumberToken | DimensionToken | PercentageToken | FlexToken | FractionToken |
|
|
521
555
|
AngleToken | LengthToken | FrequencyToken | BinaryExpressionToken | FunctionToken | ParensToken;
|
|
556
|
+
|
|
522
557
|
export declare type Token =
|
|
523
558
|
LiteralToken
|
|
524
559
|
| IdentToken
|
|
560
|
+
| IdentListToken
|
|
525
561
|
| DashedIdentToken
|
|
526
562
|
| CommaToken
|
|
527
563
|
| ColonToken
|
|
@@ -530,11 +566,13 @@ export declare type Token =
|
|
|
530
566
|
NumberToken
|
|
531
567
|
| AtRuleToken
|
|
532
568
|
| PercentageToken
|
|
569
|
+
| FlexToken
|
|
533
570
|
| FunctionURLToken
|
|
534
571
|
| FunctionImageToken
|
|
535
572
|
| TimingFunctionToken
|
|
536
573
|
| TimelineFunctionToken
|
|
537
574
|
| FunctionToken
|
|
575
|
+
| GridTemplateFuncToken
|
|
538
576
|
| DimensionToken
|
|
539
577
|
| LengthToken
|
|
540
578
|
|
|
|
@@ -674,21 +712,21 @@ export declare type AstNode =
|
|
|
674
712
|
/**
|
|
675
713
|
* Declaration visitor handler
|
|
676
714
|
*/
|
|
677
|
-
export declare type DeclarationVisitorHandler = (node: AstDeclaration) => AstDeclaration | AstDeclaration[] | null
|
|
715
|
+
export declare type DeclarationVisitorHandler = (node: AstDeclaration) => AstDeclaration | AstDeclaration[] | null | Promise<AstDeclaration | AstDeclaration[] | null>;
|
|
678
716
|
/**
|
|
679
717
|
* Rule visitor handler
|
|
680
718
|
*/
|
|
681
|
-
export declare type RuleVisitorHandler = (node: AstRule$1) => AstRule$1 | AstRule$1[] | null
|
|
719
|
+
export declare type RuleVisitorHandler = (node: AstRule$1) => AstRule$1 | AstRule$1[] | null | Promise<AstRule$1 | AstRule$1[] | null>;
|
|
682
720
|
|
|
683
721
|
/**
|
|
684
722
|
* AtRule visitor handler
|
|
685
723
|
*/
|
|
686
|
-
export declare type AtRuleVisitorHandler = (node: AstAtRule$1) => AstAtRule$1 | AstAtRule$1[] | null
|
|
724
|
+
export declare type AtRuleVisitorHandler = (node: AstAtRule$1) => AstAtRule$1 | AstAtRule$1[] | null | Promise<AstAtRule$1 | AstAtRule$1[] | null>;
|
|
687
725
|
|
|
688
726
|
/**
|
|
689
727
|
* Value visitor handler
|
|
690
728
|
*/
|
|
691
|
-
export declare type ValueVisitorHandler = (node: Token) => Token | Token[] | null
|
|
729
|
+
export declare type ValueVisitorHandler = (node: Token) => Token | Token[] | null | Promise<Token | Token[] | null>;
|
|
692
730
|
|
|
693
731
|
|
|
694
732
|
export declare interface VisitorNodeMap {
|
|
@@ -701,7 +739,7 @@ export declare interface VisitorNodeMap {
|
|
|
701
739
|
|
|
702
740
|
export declare type WalkerOption = 'ignore' | 'stop' | 'children' | 'ignore-children' | null;
|
|
703
741
|
/**
|
|
704
|
-
*
|
|
742
|
+
* returned value:
|
|
705
743
|
* - 'ignore': ignore this node and its children
|
|
706
744
|
* - 'stop': stop walking the tree
|
|
707
745
|
* - 'children': walk the children and ignore the node itself
|
|
@@ -709,6 +747,15 @@ export declare type WalkerOption = 'ignore' | 'stop' | 'children' | 'ignore-chil
|
|
|
709
747
|
*/
|
|
710
748
|
export declare type WalkerFilter = (node: AstNode) => WalkerOption;
|
|
711
749
|
|
|
750
|
+
/**
|
|
751
|
+
* returned value:
|
|
752
|
+
* - 'ignore': ignore this node and its children
|
|
753
|
+
* - 'stop': stop walking the tree
|
|
754
|
+
* - 'children': walk the children and ignore the node itself
|
|
755
|
+
* - 'ignore-children': walk the node and ignore children
|
|
756
|
+
*/
|
|
757
|
+
export declare type WalkerValueFilter = (node: Token) => WalkerOption;
|
|
758
|
+
|
|
712
759
|
export declare interface WalkResult {
|
|
713
760
|
node: AstNode;
|
|
714
761
|
parent?: AstRuleList;
|
|
@@ -717,7 +764,8 @@ export declare interface WalkResult {
|
|
|
717
764
|
|
|
718
765
|
export declare interface WalkAttributesResult {
|
|
719
766
|
value: Token;
|
|
720
|
-
|
|
767
|
+
root?: AstNode;
|
|
768
|
+
parent: FunctionToken | ParensToken | BinaryExpressionToken | null;
|
|
721
769
|
}
|
|
722
770
|
|
|
723
771
|
export declare interface ErrorDescription {
|
|
@@ -756,6 +804,7 @@ export declare interface ParserOptions extends PropertyListOptions {
|
|
|
756
804
|
resolveUrls?: boolean;
|
|
757
805
|
resolveImport?: boolean;
|
|
758
806
|
cwd?: string;
|
|
807
|
+
parseColor?: boolean;
|
|
759
808
|
removeDuplicateDeclarations?: boolean;
|
|
760
809
|
computeShorthand?: boolean;
|
|
761
810
|
inlineCssVariables?: boolean;
|
|
@@ -789,7 +838,7 @@ export declare interface RenderOptions {
|
|
|
789
838
|
indent?: string;
|
|
790
839
|
newLine?: string;
|
|
791
840
|
removeComments?: boolean;
|
|
792
|
-
|
|
841
|
+
convertColor?: boolean;
|
|
793
842
|
output?: string;
|
|
794
843
|
cwd?: string;
|
|
795
844
|
load?: (url: string, currentUrl: string) => Promise<string>;
|
|
@@ -834,7 +883,6 @@ export declare interface TransformResult extends ParseResult, RenderResult {
|
|
|
834
883
|
}
|
|
835
884
|
|
|
836
885
|
export declare interface ParseTokenOptions extends ParserOptions {
|
|
837
|
-
parseColor?: boolean;
|
|
838
886
|
}
|
|
839
887
|
|
|
840
888
|
export declare interface SourceMapObject {
|
package/dist/lib/ast/expand.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { splitRule, combinators } from './minify.js';
|
|
2
2
|
import { parseString } from '../parser/parse.js';
|
|
3
3
|
import { renderToken } from '../renderer/render.js';
|
|
4
|
-
import '../renderer/utils/color.js';
|
|
5
4
|
import { EnumToken } from './types.js';
|
|
6
5
|
import { walkValues } from './walk.js';
|
|
6
|
+
import '../renderer/color/utils/constants.js';
|
|
7
7
|
|
|
8
8
|
function expand(ast) {
|
|
9
9
|
//
|
|
@@ -1,28 +1,27 @@
|
|
|
1
1
|
import { EnumToken } from '../types.js';
|
|
2
|
-
import { reduceNumber } from '../../renderer/render.js';
|
|
3
2
|
import { walkValues } from '../walk.js';
|
|
4
3
|
import { MinifyFeature } from '../utils/minifyfeature.js';
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
4
|
+
import { IterableWeakSet } from '../../iterable/weakset.js';
|
|
5
|
+
import { evaluate } from '../math/expression.js';
|
|
7
6
|
|
|
8
|
-
class
|
|
7
|
+
class ComputeCalcExpressionFeature extends MinifyFeature {
|
|
9
8
|
static get ordering() {
|
|
10
9
|
return 1;
|
|
11
10
|
}
|
|
12
11
|
static register(options) {
|
|
13
12
|
if (options.computeCalcExpression) {
|
|
14
13
|
for (const feature of options.features) {
|
|
15
|
-
if (feature instanceof
|
|
14
|
+
if (feature instanceof ComputeCalcExpressionFeature) {
|
|
16
15
|
return;
|
|
17
16
|
}
|
|
18
17
|
}
|
|
19
18
|
// @ts-ignore
|
|
20
|
-
options.features.push(new
|
|
19
|
+
options.features.push(new ComputeCalcExpressionFeature());
|
|
21
20
|
}
|
|
22
21
|
}
|
|
23
22
|
run(ast) {
|
|
24
23
|
if (!('chi' in ast)) {
|
|
25
|
-
return
|
|
24
|
+
return;
|
|
26
25
|
}
|
|
27
26
|
// @ts-ignore
|
|
28
27
|
for (const node of ast.chi) {
|
|
@@ -30,196 +29,36 @@ class ComputeCalcExpression extends MinifyFeature {
|
|
|
30
29
|
continue;
|
|
31
30
|
}
|
|
32
31
|
const set = new IterableWeakSet;
|
|
33
|
-
for (const { parent } of walkValues(node.val)) {
|
|
34
|
-
if (
|
|
32
|
+
for (const { value, parent } of walkValues(node.val)) {
|
|
33
|
+
if (value != null && value.typ == EnumToken.FunctionTokenType && value.val == 'calc') {
|
|
35
34
|
if (!set.has(parent)) {
|
|
36
|
-
set.add(
|
|
37
|
-
|
|
35
|
+
set.add(value);
|
|
36
|
+
value.chi = evaluate(value.chi);
|
|
37
|
+
if (value.chi.length == 1 && value.chi[0].typ != EnumToken.BinaryExpressionTokenType) {
|
|
38
|
+
if (parent != null) {
|
|
39
|
+
if (parent.typ == EnumToken.BinaryExpressionTokenType) {
|
|
40
|
+
if (parent.l == value) {
|
|
41
|
+
parent.l = value.chi[0];
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
parent.r = value.chi[0];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
else {
|
|
48
|
+
for (let i = 0; i < parent.chi.length; i++) {
|
|
49
|
+
if (parent.chi[i] == value) {
|
|
50
|
+
parent.chi.splice(i, 1, value.chi[0]);
|
|
51
|
+
break;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
38
57
|
}
|
|
39
58
|
}
|
|
40
59
|
}
|
|
41
60
|
}
|
|
42
|
-
return ast;
|
|
43
61
|
}
|
|
44
62
|
}
|
|
45
|
-
/**
|
|
46
|
-
* evaluate arithmetic operation
|
|
47
|
-
* @param l
|
|
48
|
-
* @param r
|
|
49
|
-
* @param op
|
|
50
|
-
*/
|
|
51
|
-
function doEvaluate(l, r, op) {
|
|
52
|
-
const defaultReturn = {
|
|
53
|
-
typ: EnumToken.BinaryExpressionTokenType,
|
|
54
|
-
op,
|
|
55
|
-
l,
|
|
56
|
-
r
|
|
57
|
-
};
|
|
58
|
-
if (!isScalarToken(l) || !isScalarToken(r)) {
|
|
59
|
-
return defaultReturn;
|
|
60
|
-
}
|
|
61
|
-
if ((op == EnumToken.Add || op == EnumToken.Sub)) {
|
|
62
|
-
// @ts-ignore
|
|
63
|
-
if (l.typ != r.typ) {
|
|
64
|
-
return defaultReturn;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
const typ = l.typ == EnumToken.NumberTokenType ? r.typ : l.typ;
|
|
68
|
-
// @ts-ignore
|
|
69
|
-
const val = compute(typeof l.val == 'string' ? +l.val : l.val, typeof r.val == 'string' ? +r.val : r.val, op);
|
|
70
|
-
return { ...(l.typ == EnumToken.NumberTokenType ? r : l), typ, val: typeof val == 'number' ? reduceNumber(val) : val };
|
|
71
|
-
}
|
|
72
|
-
/**
|
|
73
|
-
* evaluate an array of tokens
|
|
74
|
-
* @param tokens
|
|
75
|
-
*/
|
|
76
|
-
function evaluate(tokens) {
|
|
77
|
-
const nodes = inlineExpression(evaluateExpression(buildExpression(tokens)));
|
|
78
|
-
if (nodes.length <= 1) {
|
|
79
|
-
return nodes;
|
|
80
|
-
}
|
|
81
|
-
const map = new Map;
|
|
82
|
-
let token;
|
|
83
|
-
let i;
|
|
84
|
-
for (i = 0; i < nodes.length; i++) {
|
|
85
|
-
token = nodes[i];
|
|
86
|
-
if (token.typ == EnumToken.Add) {
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
if (token.typ == EnumToken.Sub) {
|
|
90
|
-
if (!isScalarToken(nodes[i + 1])) {
|
|
91
|
-
token = { typ: EnumToken.ListToken, chi: [nodes[i], nodes[i + 1]] };
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
token = doEvaluate(nodes[i + 1], { typ: EnumToken.NumberTokenType, val: '-1' }, EnumToken.Mul);
|
|
95
|
-
}
|
|
96
|
-
i++;
|
|
97
|
-
}
|
|
98
|
-
if (!map.has(token.typ)) {
|
|
99
|
-
map.set(token.typ, [token]);
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
map.get(token.typ).push(token);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
return [...map].reduce((acc, curr) => {
|
|
106
|
-
const token = curr[1].reduce((acc, curr) => doEvaluate(acc, curr, EnumToken.Add));
|
|
107
|
-
if (token.typ != EnumToken.BinaryExpressionTokenType) {
|
|
108
|
-
if ('val' in token && +token.val < 0) {
|
|
109
|
-
acc.push({ typ: EnumToken.Sub }, { ...token, val: String(-token.val) });
|
|
110
|
-
return acc;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
if (acc.length > 0 && curr[0] != EnumToken.ListToken) {
|
|
114
|
-
acc.push({ typ: EnumToken.Add });
|
|
115
|
-
}
|
|
116
|
-
acc.push(token);
|
|
117
|
-
return acc;
|
|
118
|
-
}, []);
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* convert BinaryExpression into an array
|
|
122
|
-
* @param token
|
|
123
|
-
*/
|
|
124
|
-
function inlineExpression(token) {
|
|
125
|
-
const result = [];
|
|
126
|
-
if (token.typ == EnumToken.ParensTokenType && token.chi.length == 1) {
|
|
127
|
-
result.push(token.chi[0]);
|
|
128
|
-
}
|
|
129
|
-
else if (token.typ == EnumToken.BinaryExpressionTokenType) {
|
|
130
|
-
if ([EnumToken.Mul, EnumToken.Div].includes(token.op)) {
|
|
131
|
-
result.push(token);
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
result.push(...inlineExpression(token.l), { typ: token.op }, ...inlineExpression(token.r));
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
else {
|
|
138
|
-
result.push(token);
|
|
139
|
-
}
|
|
140
|
-
return result;
|
|
141
|
-
}
|
|
142
|
-
/**
|
|
143
|
-
* evaluate expression
|
|
144
|
-
* @param token
|
|
145
|
-
*/
|
|
146
|
-
function evaluateExpression(token) {
|
|
147
|
-
if (token.typ != EnumToken.BinaryExpressionTokenType) {
|
|
148
|
-
return token;
|
|
149
|
-
}
|
|
150
|
-
if (token.r.typ == EnumToken.BinaryExpressionTokenType) {
|
|
151
|
-
token.r = evaluateExpression(token.r);
|
|
152
|
-
}
|
|
153
|
-
if (token.l.typ == EnumToken.BinaryExpressionTokenType) {
|
|
154
|
-
token.l = evaluateExpression(token.l);
|
|
155
|
-
}
|
|
156
|
-
return doEvaluate(token.l, token.r, token.op);
|
|
157
|
-
}
|
|
158
|
-
function isScalarToken(token) {
|
|
159
|
-
return token.typ != EnumToken.BinaryExpressionTokenType && token.typ != EnumToken.ParensTokenType && token.typ != EnumToken.FunctionTokenType;
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
*
|
|
163
|
-
* generate binary expression tree
|
|
164
|
-
* @param tokens
|
|
165
|
-
*/
|
|
166
|
-
function buildExpression(tokens) {
|
|
167
|
-
return factor(factor(tokens.filter(t => t.typ != EnumToken.WhitespaceTokenType), ['/', '*']), ['+', '-'])[0];
|
|
168
|
-
}
|
|
169
|
-
function getArithmeticOperation(op) {
|
|
170
|
-
if (op == '+') {
|
|
171
|
-
return EnumToken.Add;
|
|
172
|
-
}
|
|
173
|
-
if (op == '-') {
|
|
174
|
-
return EnumToken.Sub;
|
|
175
|
-
}
|
|
176
|
-
if (op == '/') {
|
|
177
|
-
return EnumToken.Div;
|
|
178
|
-
}
|
|
179
|
-
return EnumToken.Mul;
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
*
|
|
183
|
-
* generate binary expression tree
|
|
184
|
-
* @param token
|
|
185
|
-
*/
|
|
186
|
-
function factorToken(token) {
|
|
187
|
-
if (token.typ == EnumToken.ParensTokenType || (token.typ == EnumToken.FunctionTokenType && token.val == 'calc')) {
|
|
188
|
-
if (token.typ == EnumToken.FunctionTokenType && token.val == 'calc') {
|
|
189
|
-
token = { ...token, typ: EnumToken.ParensTokenType };
|
|
190
|
-
// @ts-ignore
|
|
191
|
-
delete token.val;
|
|
192
|
-
}
|
|
193
|
-
return buildExpression(token.chi);
|
|
194
|
-
}
|
|
195
|
-
return token;
|
|
196
|
-
}
|
|
197
|
-
/**
|
|
198
|
-
* generate binary expression tree
|
|
199
|
-
* @param tokens
|
|
200
|
-
* @param ops
|
|
201
|
-
*/
|
|
202
|
-
function factor(tokens, ops) {
|
|
203
|
-
let isOp;
|
|
204
|
-
const opList = ops.map(x => getArithmeticOperation(x));
|
|
205
|
-
if (tokens.length == 1) {
|
|
206
|
-
return [factorToken(tokens[0])];
|
|
207
|
-
}
|
|
208
|
-
for (let i = 0; i < tokens.length; i++) {
|
|
209
|
-
isOp = opList.includes(tokens[i].typ);
|
|
210
|
-
if (isOp ||
|
|
211
|
-
// @ts-ignore
|
|
212
|
-
(tokens[i].typ == EnumToken.LiteralTokenType && ops.includes(tokens[i].val))) {
|
|
213
|
-
tokens.splice(i - 1, 3, {
|
|
214
|
-
typ: EnumToken.BinaryExpressionTokenType,
|
|
215
|
-
op: isOp ? tokens[i].typ : getArithmeticOperation(tokens[i].val),
|
|
216
|
-
l: factorToken(tokens[i - 1]),
|
|
217
|
-
r: factorToken(tokens[i + 1])
|
|
218
|
-
});
|
|
219
|
-
i--;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
return tokens;
|
|
223
|
-
}
|
|
224
63
|
|
|
225
|
-
export {
|
|
64
|
+
export { ComputeCalcExpressionFeature };
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
1
|
+
export { InlineCssVariablesFeature } from './inlinecssvariables.js';
|
|
2
|
+
export { ComputeShorthandFeature } from './shorthand.js';
|
|
3
|
+
export { ComputeCalcExpressionFeature } from './calc.js';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { EnumToken } from '../types.js';
|
|
2
2
|
import { walkValues } from '../walk.js';
|
|
3
3
|
import { MinifyFeature } from '../utils/minifyfeature.js';
|
|
4
|
-
import { IterableWeakSet } from '../../iterable/
|
|
4
|
+
import { IterableWeakSet } from '../../iterable/weakset.js';
|
|
5
5
|
|
|
6
6
|
function replace(node, variableScope) {
|
|
7
7
|
for (const { value, parent: parentValue } of walkValues(node.val)) {
|
|
@@ -26,26 +26,26 @@ function replace(node, variableScope) {
|
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
|
-
class
|
|
29
|
+
class InlineCssVariablesFeature extends MinifyFeature {
|
|
30
30
|
static get ordering() {
|
|
31
31
|
return 0;
|
|
32
32
|
}
|
|
33
33
|
static register(options) {
|
|
34
34
|
if (options.inlineCssVariables) {
|
|
35
35
|
for (const feature of options.features) {
|
|
36
|
-
if (feature instanceof
|
|
36
|
+
if (feature instanceof InlineCssVariablesFeature) {
|
|
37
37
|
return;
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
// @ts-ignore
|
|
41
|
-
options.features.push(new
|
|
41
|
+
options.features.push(new InlineCssVariablesFeature());
|
|
42
42
|
}
|
|
43
43
|
}
|
|
44
44
|
run(ast, options = {}, parent, context) {
|
|
45
45
|
if (!('variableScope' in context)) {
|
|
46
46
|
context.variableScope = new Map;
|
|
47
47
|
}
|
|
48
|
-
const isRoot = parent.typ == EnumToken.StyleSheetNodeType && ast.typ == EnumToken.RuleNodeType &&
|
|
48
|
+
const isRoot = parent.typ == EnumToken.StyleSheetNodeType && ast.typ == EnumToken.RuleNodeType && [':root', 'html'].includes(ast.sel);
|
|
49
49
|
const variableScope = context.variableScope;
|
|
50
50
|
// @ts-ignore
|
|
51
51
|
for (const node of ast.chi) {
|
|
@@ -127,4 +127,4 @@ class InlineCssVariables extends MinifyFeature {
|
|
|
127
127
|
}
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
-
export {
|
|
130
|
+
export { InlineCssVariablesFeature };
|
|
@@ -1,25 +1,24 @@
|
|
|
1
1
|
import { PropertyList } from '../../parser/declaration/list.js';
|
|
2
|
-
import '../../renderer/utils/color.js';
|
|
3
2
|
import { EnumToken } from '../types.js';
|
|
4
3
|
import '../minify.js';
|
|
5
4
|
import '../../parser/parse.js';
|
|
5
|
+
import '../../renderer/color/utils/constants.js';
|
|
6
6
|
import '../../renderer/sourcemap/lib/encode.js';
|
|
7
|
-
import '../../parser/declaration/map.js';
|
|
8
7
|
import { MinifyFeature } from '../utils/minifyfeature.js';
|
|
9
8
|
|
|
10
|
-
class
|
|
9
|
+
class ComputeShorthandFeature extends MinifyFeature {
|
|
11
10
|
static get ordering() {
|
|
12
11
|
return 2;
|
|
13
12
|
}
|
|
14
13
|
static register(options) {
|
|
15
14
|
if (options.computeShorthand) {
|
|
16
15
|
for (const feature of options.features) {
|
|
17
|
-
if (feature instanceof
|
|
16
|
+
if (feature instanceof ComputeShorthandFeature) {
|
|
18
17
|
return;
|
|
19
18
|
}
|
|
20
19
|
}
|
|
21
20
|
// @ts-ignore
|
|
22
|
-
options.features.push(new
|
|
21
|
+
options.features.push(new ComputeShorthandFeature());
|
|
23
22
|
}
|
|
24
23
|
}
|
|
25
24
|
run(ast, options = {}, parent, context) {
|
|
@@ -43,4 +42,4 @@ class ComputeShorthand extends MinifyFeature {
|
|
|
43
42
|
}
|
|
44
43
|
}
|
|
45
44
|
|
|
46
|
-
export {
|
|
45
|
+
export { ComputeShorthandFeature };
|