@tbela99/css-parser 2.0.1 → 2.2.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 +86 -1
- package/README.md +15 -7
- package/architecture.png +0 -0
- package/dist/{src/data → data}/properties.json.js +86 -2
- package/dist/{src/data → data}/validation.json.js +4848 -3750
- package/dist/index-umd-web.js +18234 -9498
- package/dist/index.d.ts +1251 -373
- package/dist/{src/lib → lib}/ast/arena-node.js +119 -57
- package/dist/{src/lib → lib}/ast/arena.js +1034 -655
- package/dist/{src/lib → lib}/ast/cache.js +36 -31
- package/dist/{src/lib → lib}/ast/features/calc.js +37 -27
- package/dist/{src/lib → lib}/ast/features/if.js +24 -19
- package/dist/{src/lib → lib}/ast/features/index.js +3 -2
- package/dist/{src/lib → lib}/ast/features/inlinecssvariables.js +42 -23
- package/dist/lib/ast/features/mixin.js +953 -0
- package/dist/{src/lib → lib}/ast/features/prefix.js +82 -69
- package/dist/lib/ast/features/shorthand.js +129 -0
- package/dist/lib/ast/features/transform.js +188 -0
- package/dist/{src/lib → lib}/ast/features/type.js +2 -2
- package/dist/{src/lib → lib}/ast/math/expression.js +150 -150
- package/dist/{src/lib → lib}/ast/math/math.js +8 -8
- package/dist/{src/lib → lib}/ast/minify.js +932 -354
- package/dist/lib/ast/mixins/custom-function.js +393 -0
- package/dist/lib/ast/mixins/utils.js +53 -0
- package/dist/lib/ast/mixins/variables.js +678 -0
- package/dist/lib/ast/strings.js +906 -0
- package/dist/lib/ast/transform/compute.js +560 -0
- package/dist/lib/ast/transform/matrix.js +67 -0
- package/dist/lib/ast/transform/minify.js +325 -0
- package/dist/lib/ast/transform/perspective.js +24 -0
- package/dist/{src/lib → lib}/ast/transform/rotate.js +16 -7
- package/dist/lib/ast/transform/scale.js +43 -0
- package/dist/lib/ast/transform/skew.js +38 -0
- package/dist/lib/ast/transform/translate.js +56 -0
- package/dist/lib/ast/transform/utils.js +390 -0
- package/dist/{src/lib → lib}/ast/types.js +7 -15
- package/dist/{src/lib → lib}/ast/walk.js +19 -19
- package/dist/{src/lib → lib}/fs/resolve.js +15 -15
- package/dist/{src/lib → lib}/parser/declaration/list.js +55 -26
- package/dist/{src/lib → lib}/parser/declaration/map.js +85 -72
- package/dist/{src/lib → lib}/parser/declaration/set.js +32 -27
- package/dist/lib/parser/input.js +85 -0
- package/dist/{src/lib → lib}/parser/linesmap.js +3 -16
- package/dist/{src/lib → lib}/parser/parse.js +852 -619
- package/dist/lib/parser/source.js +71 -0
- package/dist/{src/lib → lib}/parser/tokenize.js +886 -722
- package/dist/{src/lib → lib}/parser/utils/angle.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-container.js +68 -35
- package/dist/{src/lib → lib}/parser/utils/at-rule-font-feature-values.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/at-rule-generic.js +31 -13
- package/dist/{src/lib → lib}/parser/utils/at-rule-import.js +29 -16
- package/dist/{src/lib → lib}/parser/utils/at-rule-media.js +77 -58
- package/dist/{src/lib → lib}/parser/utils/at-rule-support.js +18 -20
- package/dist/{src/lib → lib}/parser/utils/at-rule-when-else.js +9 -8
- package/dist/{src/lib → lib}/parser/utils/at-rule.js +3 -3
- package/dist/{src/lib → lib}/parser/utils/base64.js +2 -2
- package/dist/lib/parser/utils/cache.js +53 -0
- package/dist/lib/parser/utils/canonical.js +78 -0
- package/dist/lib/parser/utils/codes.js +93 -0
- package/dist/{src/lib → lib}/parser/utils/config.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/declaration.js +67 -49
- package/dist/{src/lib → lib}/parser/utils/eq.js +14 -14
- package/dist/{src/lib → lib}/parser/utils/hash.js +4 -4
- package/dist/lib/parser/utils/selector.js +720 -0
- package/dist/{src/lib → lib}/parser/utils/text.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/token.js +2 -2
- package/dist/{src/lib → lib}/parser/utils/type.js +11 -9
- package/dist/{src/lib → lib}/printer/render.js +536 -243
- package/dist/lib/printer/result.js +148 -0
- package/dist/{src/lib → lib}/printer/sourcemap/lib/codec.js +5 -8
- package/dist/{src/lib → lib}/printer/sourcemap/sourcemap.js +13 -13
- package/dist/lib/printer/writer.js +252 -0
- package/dist/{src/lib → lib}/syntax/color/a98rgb.js +6 -6
- package/dist/{src/lib → lib}/syntax/color/alpha.js +12 -11
- package/dist/{src/lib → lib}/syntax/color/cmyk.js +11 -6
- package/dist/{src/lib → lib}/syntax/color/color-mix.js +78 -67
- package/dist/{src/lib → lib}/syntax/color/color.js +52 -47
- package/dist/{src/lib → lib}/syntax/color/hex.js +17 -17
- package/dist/{src/lib → lib}/syntax/color/hsl.js +8 -8
- package/dist/{src/lib → lib}/syntax/color/hwb.js +6 -7
- package/dist/{src/lib → lib}/syntax/color/lab.js +8 -8
- package/dist/{src/lib → lib}/syntax/color/lch.js +12 -11
- package/dist/{src/lib → lib}/syntax/color/oklab.js +9 -9
- package/dist/{src/lib → lib}/syntax/color/oklch.js +5 -5
- package/dist/{src/lib → lib}/syntax/color/p3.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/prophotorgb.js +4 -4
- package/dist/{src/lib → lib}/syntax/color/rec2020.js +3 -3
- package/dist/{src/lib → lib}/syntax/color/relative-color.js +41 -34
- package/dist/{src/lib → lib}/syntax/color/rgb.js +17 -17
- package/dist/{src/lib → lib}/syntax/color/srgb.js +20 -20
- package/dist/{src/lib → lib}/syntax/color/utils/color-type.js +6 -4
- package/dist/{src/lib → lib}/syntax/color/utils/colorspace.js +12 -14
- package/dist/{src/lib → lib}/syntax/color/utils/components.js +13 -13
- package/dist/{src/lib → lib}/syntax/color/utils/distance.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/utils/make-color.js +6 -4
- package/dist/{src/lib → lib}/syntax/color/utils/matrix.js +2 -2
- package/dist/{src/lib → lib}/syntax/color/xyz.js +5 -10
- package/dist/{src/lib → lib}/syntax/color/xyzd50.js +3 -3
- package/dist/{src/lib → lib}/syntax/constants.js +30 -3
- package/dist/{src/lib → lib}/syntax/syntax.js +151 -116
- package/dist/lib/validation/config.js +173 -0
- package/dist/lib/validation/first.js +571 -0
- package/dist/{src/lib → lib}/validation/match.js +1256 -579
- package/dist/{src/lib → lib}/validation/parser/parse.js +85 -59
- package/dist/{src/lib → lib}/validation/parser/typedef.js +6 -2
- package/dist/{src/lib → lib}/validation/utils/list.js +3 -19
- package/dist/{src/node.js → node.js} +92 -80
- package/dist/{src/types.js → types.js} +2 -2
- package/dist/{src/utils → utils}/sync.js +13 -8
- package/dist/version.d.ts +4 -0
- package/dist/version.js +13 -0
- package/dist/{src/web.js → web.js} +80 -75
- package/package.json +13 -13
- package/dist/package.json.js +0 -10
- package/dist/src/lib/ast/features/shorthand.js +0 -87
- package/dist/src/lib/ast/features/transform.js +0 -85
- package/dist/src/lib/ast/transform/compute.js +0 -358
- package/dist/src/lib/ast/transform/matrix.js +0 -88
- package/dist/src/lib/ast/transform/minify.js +0 -267
- package/dist/src/lib/ast/transform/perspective.js +0 -16
- package/dist/src/lib/ast/transform/scale.js +0 -39
- package/dist/src/lib/ast/transform/skew.js +0 -30
- package/dist/src/lib/ast/transform/translate.js +0 -39
- package/dist/src/lib/ast/transform/utils.js +0 -287
- package/dist/src/lib/parser/source.js +0 -127
- package/dist/src/lib/parser/utils/cache.js +0 -54
- package/dist/src/lib/parser/utils/selector.js +0 -440
- package/dist/src/lib/validation/config.js +0 -120
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { getColorComponents } from './utils/components.js';
|
|
@@ -78,7 +78,7 @@ function oklchToken(arena, values) {
|
|
|
78
78
|
arena.createToken(EnumToken.NumberTokenType, values[1]),
|
|
79
79
|
arena.createToken(EnumToken.NumberTokenType, values[2]),
|
|
80
80
|
];
|
|
81
|
-
if (values.length
|
|
81
|
+
if (values.length === 4) {
|
|
82
82
|
chi.push(arena.createToken(EnumToken.LiteralTokenType, "/"), arena.createToken(EnumToken.PercentageTokenType, values[3] * 100));
|
|
83
83
|
}
|
|
84
84
|
return arena.createColor("oklch", ColorType.OKLCH, chi);
|
|
@@ -156,14 +156,14 @@ function getOKLCHComponents(arena, token) {
|
|
|
156
156
|
// @ts-ignore
|
|
157
157
|
t = components[1];
|
|
158
158
|
// @ts-ignore
|
|
159
|
-
const c = getNumber(arena, t) * (arena.dataTypes[t]
|
|
159
|
+
const c = getNumber(arena, t) * (arena.dataTypes[t] === EnumToken.PercentageTokenType ? 0.4 : 1);
|
|
160
160
|
// @ts-ignore
|
|
161
161
|
t = components[2];
|
|
162
162
|
// @ts-ignore
|
|
163
163
|
const h = getAngle(arena, t) * 360;
|
|
164
164
|
// @ts-ignore
|
|
165
165
|
t = components[3];
|
|
166
|
-
const alpha = t == null || (arena.dataTypes[t]
|
|
166
|
+
const alpha = t == null || (arena.dataTypes[t] === EnumToken.IdenTokenType && arena.resolveString(arena.stringValue[t]) === "none")
|
|
167
167
|
? 1
|
|
168
168
|
: getNumber(arena, t);
|
|
169
169
|
return [l, c, h, alpha];
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { lsrgb2srgbvalues, xyz2srgb, srgb2lsrgbvalues } from './srgb.js';
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { XYZ_D65_to_D50, xyzd502srgb } from './xyzd50.js';
|
|
@@ -31,7 +31,7 @@ function prophotorgb2lin_ProPhoto(r, g, b, a = null) {
|
|
|
31
31
|
}
|
|
32
32
|
return v / 16;
|
|
33
33
|
})
|
|
34
|
-
.concat(a == null || a
|
|
34
|
+
.concat(a == null || a === 1 ? [] : [a]);
|
|
35
35
|
}
|
|
36
36
|
function prophotorgb2xyz50(r, g, b, a = null) {
|
|
37
37
|
[r, g, b, a] = prophotorgb2lin_ProPhoto(r, g, b, a);
|
|
@@ -40,7 +40,7 @@ function prophotorgb2xyz50(r, g, b, a = null) {
|
|
|
40
40
|
0.2880748288194013 * r + 0.7118352342418731 * g + 0.0000899369387256 * b,
|
|
41
41
|
0.8251046025104602 * b,
|
|
42
42
|
];
|
|
43
|
-
return xyz.concat(a == null || a
|
|
43
|
+
return xyz.concat(a == null || a === 1 ? [] : [a]);
|
|
44
44
|
}
|
|
45
45
|
function xyz50_to_prophotorgb(x, y, z, a) {
|
|
46
46
|
// @ts-ignore
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { xyz2srgb } from './srgb.js';
|
|
@@ -53,7 +53,7 @@ function lrec20202rec2020(r, g, b, a) {
|
|
|
53
53
|
}
|
|
54
54
|
return 4.5 * val;
|
|
55
55
|
})
|
|
56
|
-
.concat(a == null || a
|
|
56
|
+
.concat(a == null || a === 1 ? [] : [a]);
|
|
57
57
|
}
|
|
58
58
|
function lrec20202xyz(r, g, b, a) {
|
|
59
59
|
// convert an array of linear-light rec2020 values to CIE XYZ
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { convertColor, getNumber } from './color.js';
|
|
@@ -15,6 +15,7 @@ import { ValueWalker } from '../../ast/walk.js';
|
|
|
15
15
|
|
|
16
16
|
/**
|
|
17
17
|
* Parse relative color components
|
|
18
|
+
* @param arena
|
|
18
19
|
* @param relativeKeys
|
|
19
20
|
* @param original
|
|
20
21
|
* @param rExp
|
|
@@ -50,8 +51,8 @@ function parseRelativeColorComponents(arena, relativeKeys, original, rExp, gExp,
|
|
|
50
51
|
if (component == null) {
|
|
51
52
|
continue;
|
|
52
53
|
}
|
|
53
|
-
if (arena.dataTypes[component]
|
|
54
|
-
val = arena.
|
|
54
|
+
if (arena.dataTypes[component] === EnumToken.IdenTokenType) {
|
|
55
|
+
val = arena.resolveString(arena.stringValue[component]);
|
|
55
56
|
if (
|
|
56
57
|
// @ts-expect-error
|
|
57
58
|
typeof Math[val.toUpperCase()] !== "number" &&
|
|
@@ -72,23 +73,23 @@ function parseRelativeColorComponents(arena, relativeKeys, original, rExp, gExp,
|
|
|
72
73
|
continue;
|
|
73
74
|
}
|
|
74
75
|
if (arena.dataTypes[component] === EnumToken.MathFunctionTokenType &&
|
|
75
|
-
equalsIgnoreCase("calc", arena.
|
|
76
|
+
equalsIgnoreCase("calc", arena.resolveString(arena.stringValue[component]))) {
|
|
76
77
|
// inlined: arena.walkValues(arena.tokens(component))
|
|
77
78
|
for (const value of new ValueWalker(arena, arena.tokens(component))) {
|
|
78
|
-
if (arena.dataTypes[value]
|
|
79
|
-
val = arena.
|
|
79
|
+
if (arena.dataTypes[value] === EnumToken.IdenTokenType) {
|
|
80
|
+
val = arena.resolveString(arena.stringValue[value]).toLowerCase();
|
|
80
81
|
if (
|
|
81
82
|
// @ts-expect-error
|
|
82
83
|
typeof Math[val.toUpperCase()] !== "number" &&
|
|
83
|
-
val
|
|
84
|
-
val
|
|
85
|
-
val
|
|
86
|
-
val
|
|
87
|
-
val
|
|
88
|
-
val
|
|
89
|
-
val
|
|
90
|
-
val
|
|
91
|
-
val
|
|
84
|
+
val !== "in" &&
|
|
85
|
+
val !== "hue" &&
|
|
86
|
+
val !== "from" &&
|
|
87
|
+
val !== "alpha" &&
|
|
88
|
+
val !== "none" &&
|
|
89
|
+
val !== "shorter" &&
|
|
90
|
+
val !== "longer" &&
|
|
91
|
+
val !== "increasing" &&
|
|
92
|
+
val !== "decreasing" &&
|
|
92
93
|
!colorsFunc.includes(val) &&
|
|
93
94
|
!colorFuncColorSpace.includes(val) &&
|
|
94
95
|
!validKeys.includes(val)) {
|
|
@@ -102,8 +103,10 @@ function parseRelativeColorComponents(arena, relativeKeys, original, rExp, gExp,
|
|
|
102
103
|
if (converted == null) {
|
|
103
104
|
return null;
|
|
104
105
|
}
|
|
105
|
-
const children = arena
|
|
106
|
-
|
|
106
|
+
const children = arena
|
|
107
|
+
.tokens(converted)
|
|
108
|
+
.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.LiteralTokenType, EnumToken.CommentTokenType].includes(arena.dataTypes[t]));
|
|
109
|
+
[r, g, b, alpha] = arena.colorType[converted] === ColorType.COLOR ? children.slice(1) : children;
|
|
107
110
|
values = {
|
|
108
111
|
[names[0]]: getValue(arena, r, converted, names[0]),
|
|
109
112
|
[names[1]]: getValue(arena, g, converted, names[1]), // string,
|
|
@@ -111,9 +114,9 @@ function parseRelativeColorComponents(arena, relativeKeys, original, rExp, gExp,
|
|
|
111
114
|
// @ts-ignore
|
|
112
115
|
alpha: alpha == null
|
|
113
116
|
? numberToken(arena, 1, b)
|
|
114
|
-
: arena.dataTypes[alpha]
|
|
117
|
+
: arena.dataTypes[alpha] === EnumToken.IdenTokenType && arena.resolveString(arena.stringValue[alpha]) === "none"
|
|
115
118
|
? numberToken(arena, 0, alpha)
|
|
116
|
-
: arena.dataTypes[alpha]
|
|
119
|
+
: arena.dataTypes[alpha] === EnumToken.PercentageTokenType
|
|
117
120
|
? numberToken(arena, getNumber(arena, alpha), alpha)
|
|
118
121
|
: alpha,
|
|
119
122
|
};
|
|
@@ -124,17 +127,17 @@ function parseRelativeColorComponents(arena, relativeKeys, original, rExp, gExp,
|
|
|
124
127
|
// @ts-ignore
|
|
125
128
|
alpha: getValue(arena, aExp == null
|
|
126
129
|
? numberToken(arena, 1, bExp)
|
|
127
|
-
: arena.dataTypes[aExp]
|
|
130
|
+
: arena.dataTypes[aExp] === EnumToken.IdenTokenType && arena.resolveString(arena.stringValue[aExp]) === "none"
|
|
128
131
|
? numberToken(arena, 0, aExp)
|
|
129
132
|
: aExp),
|
|
130
133
|
};
|
|
131
134
|
const result = computeComponentValue(arena, keys, values);
|
|
132
135
|
if (result != null) {
|
|
133
|
-
if ("hsl"
|
|
136
|
+
if ("hsl" === relativeKeys) {
|
|
134
137
|
toPercentage(arena, result.s);
|
|
135
138
|
toPercentage(arena, result.l);
|
|
136
139
|
}
|
|
137
|
-
else if ("hwb"
|
|
140
|
+
else if ("hwb" === relativeKeys) {
|
|
138
141
|
toPercentage(arena, result.w);
|
|
139
142
|
toPercentage(arena, result.b);
|
|
140
143
|
}
|
|
@@ -149,13 +152,14 @@ function parseRelativeColorComponents(arena, relativeKeys, original, rExp, gExp,
|
|
|
149
152
|
}
|
|
150
153
|
/**
|
|
151
154
|
* Get token numeric value
|
|
155
|
+
* @param arena
|
|
152
156
|
* @param t
|
|
153
157
|
* @param converted
|
|
154
158
|
* @param component
|
|
155
159
|
* @returns
|
|
156
160
|
*/
|
|
157
161
|
function getValue(arena, t, converted, component) {
|
|
158
|
-
if (arena.dataTypes[t]
|
|
162
|
+
if (arena.dataTypes[t] === EnumToken.PercentageTokenType) {
|
|
159
163
|
let value = getNumber(arena, t);
|
|
160
164
|
if (converted != null) {
|
|
161
165
|
let colorSpace = ColorType[arena.colorType[converted]].toLowerCase().replaceAll("-", "_");
|
|
@@ -170,6 +174,7 @@ function getValue(arena, t, converted, component) {
|
|
|
170
174
|
}
|
|
171
175
|
/**
|
|
172
176
|
* Compute component value
|
|
177
|
+
* @param arena
|
|
173
178
|
* @param expr
|
|
174
179
|
* @param values
|
|
175
180
|
* @returns
|
|
@@ -180,7 +185,7 @@ function computeComponentValue(arena, expr, values) {
|
|
|
180
185
|
// normalize hue
|
|
181
186
|
// inlined: arena.walkValues([object.h as Token])
|
|
182
187
|
for (const k of new ValueWalker(arena, [object.h])) {
|
|
183
|
-
if (arena.dataTypes[k]
|
|
188
|
+
if (arena.dataTypes[k] === EnumToken.AngleTokenType && arena.resolveString(arena.unit[k]) === "deg") {
|
|
184
189
|
arena.dataTypes[k] = EnumToken.NumberTokenType;
|
|
185
190
|
}
|
|
186
191
|
}
|
|
@@ -193,15 +198,16 @@ function computeComponentValue(arena, expr, values) {
|
|
|
193
198
|
EnumToken.AngleTokenType,
|
|
194
199
|
EnumToken.LengthTokenType,
|
|
195
200
|
].includes(arena.dataTypes[exp])) ;
|
|
196
|
-
else if (arena.dataTypes[exp]
|
|
197
|
-
expr[key] = values[arena.
|
|
201
|
+
else if (arena.dataTypes[exp] === EnumToken.IdenTokenType && arena.resolveString(arena.stringValue[exp]) in values) {
|
|
202
|
+
expr[key] = values[arena.resolveString(arena.stringValue[exp])];
|
|
198
203
|
}
|
|
199
|
-
else if (arena.dataTypes[exp]
|
|
200
|
-
(arena.dataTypes[exp]
|
|
204
|
+
else if (arena.dataTypes[exp] === EnumToken.MathFunctionTokenType ||
|
|
205
|
+
(arena.dataTypes[exp] === EnumToken.FunctionTokenType &&
|
|
206
|
+
mathFuncs.includes(arena.resolveString(arena.stringValue[exp])))) {
|
|
201
207
|
// inlined: arena.walkValues(arena.tokens(exp))
|
|
202
208
|
for (const value of new ValueWalker(arena, arena.tokens(exp))) {
|
|
203
|
-
if (arena.dataTypes[value]
|
|
204
|
-
const name = arena.
|
|
209
|
+
if (arena.dataTypes[value] === EnumToken.IdenTokenType) {
|
|
210
|
+
const name = arena.resolveString(arena.stringValue[value]);
|
|
205
211
|
const replacement = values[name];
|
|
206
212
|
// replace the identifier by its value
|
|
207
213
|
if (replacement != null) {
|
|
@@ -215,11 +221,12 @@ function computeComponentValue(arena, expr, values) {
|
|
|
215
221
|
}
|
|
216
222
|
}
|
|
217
223
|
const result = (arena.dataTypes[exp] === EnumToken.MathFunctionTokenType ||
|
|
218
|
-
(arena.dataTypes[exp]
|
|
219
|
-
|
|
224
|
+
(arena.dataTypes[exp] === EnumToken.FunctionTokenType &&
|
|
225
|
+
mathFuncs.includes(arena.resolveString(arena.stringValue[exp])))) &&
|
|
226
|
+
arena.resolveString(arena.stringValue[exp]) !== "calc"
|
|
220
227
|
? evaluateFunc(arena, exp)
|
|
221
228
|
: evaluate(arena, arena.tokens(exp));
|
|
222
|
-
if (result.length
|
|
229
|
+
if (result.length === 1 && arena.dataTypes[result[0]] !== EnumToken.BinaryExpressionTokenType) {
|
|
223
230
|
expr[key] = result[0];
|
|
224
231
|
}
|
|
225
232
|
}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { color2srgbvalues, minmax } from './color.js';
|
|
@@ -71,7 +71,7 @@ function color2RgbToken(arena, token) {
|
|
|
71
71
|
if (values == null) {
|
|
72
72
|
return null;
|
|
73
73
|
}
|
|
74
|
-
return rgb2RgbToken(arena, values.map((t, index) => (index
|
|
74
|
+
return rgb2RgbToken(arena, values.map((t, index) => (index === 3 ? t : srgb2rgb(t))));
|
|
75
75
|
}
|
|
76
76
|
function rgb2RgbToken(arena, values) {
|
|
77
77
|
const chi = [
|
|
@@ -79,21 +79,21 @@ function rgb2RgbToken(arena, values) {
|
|
|
79
79
|
arena.createToken(EnumToken.NumberTokenType, values[1]),
|
|
80
80
|
arena.createToken(EnumToken.NumberTokenType, values[2]),
|
|
81
81
|
];
|
|
82
|
-
if (values.length
|
|
82
|
+
if (values.length === 4) {
|
|
83
83
|
chi.push(arena.createToken(EnumToken.PercentageTokenType, values[3] * 100));
|
|
84
84
|
}
|
|
85
85
|
return arena.createColor("rgb", ColorType.RGB, chi);
|
|
86
86
|
}
|
|
87
87
|
function hex2rgbvalues(arena, token) {
|
|
88
|
-
const value = expandHexValue(arena.colorType[token]
|
|
89
|
-
? COLORS_NAMES[arena.
|
|
90
|
-
: arena.
|
|
88
|
+
const value = expandHexValue(arena.colorType[token] === ColorType.LIT
|
|
89
|
+
? COLORS_NAMES[arena.resolveString(arena.stringValue[token]).toLowerCase()]
|
|
90
|
+
: arena.resolveString(arena.stringValue[token]));
|
|
91
91
|
const rgb = [];
|
|
92
92
|
for (let i = 1; i < value.length; i += 2) {
|
|
93
93
|
rgb.push(parseInt(value.slice(i, i + 2), 16));
|
|
94
94
|
}
|
|
95
|
-
if (rgb.length
|
|
96
|
-
if (rgb[3]
|
|
95
|
+
if (rgb.length === 4) {
|
|
96
|
+
if (rgb[3] === 255) {
|
|
97
97
|
rgb.pop();
|
|
98
98
|
}
|
|
99
99
|
else {
|
|
@@ -103,7 +103,7 @@ function hex2rgbvalues(arena, token) {
|
|
|
103
103
|
return rgb;
|
|
104
104
|
}
|
|
105
105
|
function hwb2rgbvalues(arena, token) {
|
|
106
|
-
return hwb2srgbvalues(arena, token)?.map?.((t, index) => (index
|
|
106
|
+
return hwb2srgbvalues(arena, token)?.map?.((t, index) => (index === 3 ? t : srgb2rgb(t))) ?? null;
|
|
107
107
|
}
|
|
108
108
|
function hsl2rgbvalues(arena, token) {
|
|
109
109
|
let { h, s, l, a } = hslvalues(arena, token) ?? {};
|
|
@@ -112,29 +112,29 @@ function hsl2rgbvalues(arena, token) {
|
|
|
112
112
|
}
|
|
113
113
|
return hslvalues2srgbvalues(h, s, l)
|
|
114
114
|
.map((t) => minmax(Math.round(t * 255), 0, 255))
|
|
115
|
-
.concat(a
|
|
115
|
+
.concat(a === 1 || a == null ? [] : [a]);
|
|
116
116
|
}
|
|
117
117
|
function hsl2srgbvalues(arena, token) {
|
|
118
118
|
let { h, s, l, a } = hslvalues(arena, token) ?? {};
|
|
119
119
|
if (h == null || s == null || l == null) {
|
|
120
120
|
return null;
|
|
121
121
|
}
|
|
122
|
-
return hslvalues2srgbvalues(h, s, l).concat(a
|
|
122
|
+
return hslvalues2srgbvalues(h, s, l).concat(a === 1 || a == null ? [] : [a]);
|
|
123
123
|
}
|
|
124
124
|
function cmyk2rgbvalues(arena, token) {
|
|
125
|
-
return cmyk2srgbvalues(arena, token)?.map?.((t, index) => (index
|
|
125
|
+
return cmyk2srgbvalues(arena, token)?.map?.((t, index) => (index === 3 ? t : srgb2rgb(t))) ?? null;
|
|
126
126
|
}
|
|
127
127
|
function oklab2rgbvalues(arena, token) {
|
|
128
|
-
return oklab2srgbvalues(arena, token)?.map?.((t, index) => (index
|
|
128
|
+
return oklab2srgbvalues(arena, token)?.map?.((t, index) => (index === 3 ? t : srgb2rgb(t))) ?? null;
|
|
129
129
|
}
|
|
130
130
|
function oklch2rgbvalues(arena, token) {
|
|
131
|
-
return oklch2srgbvalues(arena, token)?.map?.((t, index) => (index
|
|
131
|
+
return oklch2srgbvalues(arena, token)?.map?.((t, index) => (index === 3 ? t : srgb2rgb(t))) ?? null;
|
|
132
132
|
}
|
|
133
133
|
function lab2rgbvalues(arena, token) {
|
|
134
|
-
return lab2srgbvalues(arena, token)?.map?.((t, index) => (index
|
|
134
|
+
return lab2srgbvalues(arena, token)?.map?.((t, index) => (index === 3 ? t : srgb2rgb(t))) ?? null;
|
|
135
135
|
}
|
|
136
136
|
function lch2rgbvalues(arena, token) {
|
|
137
|
-
return lch2srgbvalues(arena, token)?.map?.((t, index) => (index
|
|
137
|
+
return lch2srgbvalues(arena, token)?.map?.((t, index) => (index === 3 ? t : srgb2rgb(t))) ?? null;
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
export { cmyk2RgbToken, cmyk2rgbvalues, color2RgbToken, hex2RgbToken, hex2rgbvalues, hsl2RgbToken, hsl2rgbvalues, hsl2srgbvalues, hwb2RgbToken, hwb2rgbvalues, lab2RgbToken, lab2rgbvalues, lch2RgbToken, lch2rgbvalues, oklab2RgbToken, oklab2rgbvalues, oklch2RgbToken, oklch2rgbvalues, srgb2rgb };
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { getColorComponents } from './utils/components.js';
|
|
@@ -51,16 +51,16 @@ function srgbvalues(arena, token) {
|
|
|
51
51
|
return null;
|
|
52
52
|
}
|
|
53
53
|
function rgb2srgb(arena, token) {
|
|
54
|
-
return (getColorComponents(arena, token)?.map?.((t, index) => index
|
|
55
|
-
? arena.dataTypes[t]
|
|
54
|
+
return (getColorComponents(arena, token)?.map?.((t, index) => index === 3
|
|
55
|
+
? arena.dataTypes[t] === EnumToken.IdenTokenType && arena.resolveString(arena.stringValue[t]) === "none"
|
|
56
56
|
? 1
|
|
57
57
|
: getNumber(arena, t)
|
|
58
|
-
: ((arena.dataTypes[t]
|
|
58
|
+
: ((arena.dataTypes[t] === EnumToken.PercentageTokenType ? 255 : 1) *
|
|
59
59
|
getNumber(arena, t)) /
|
|
60
60
|
255) ?? null);
|
|
61
61
|
}
|
|
62
62
|
function rgb2srgbvalues(arena, token) {
|
|
63
|
-
return (getColorComponents(arena, token)?.map?.((t, index) => index
|
|
63
|
+
return (getColorComponents(arena, token)?.map?.((t, index) => index === 3
|
|
64
64
|
? getNumber(arena, t)
|
|
65
65
|
: getNumber(arena, t) / 255) ?? null);
|
|
66
66
|
}
|
|
@@ -74,14 +74,14 @@ function rgb2srgbvalues(arena, token) {
|
|
|
74
74
|
// return result;
|
|
75
75
|
// }
|
|
76
76
|
function hex2srgbvalues(arena, token) {
|
|
77
|
-
const value = expandHexValue(arena.colorType[token]
|
|
78
|
-
? COLORS_NAMES[arena.
|
|
79
|
-
: arena.
|
|
77
|
+
const value = expandHexValue(arena.colorType[token] === ColorType.LIT
|
|
78
|
+
? COLORS_NAMES[arena.resolveString(arena.stringValue[token]).toLowerCase()]
|
|
79
|
+
: arena.resolveString(arena.stringValue[token]));
|
|
80
80
|
const rgb = [];
|
|
81
81
|
for (let i = 1; i < value.length; i += 2) {
|
|
82
82
|
rgb.push(parseInt(value.slice(i, i + 2), 16) / 255);
|
|
83
83
|
}
|
|
84
|
-
if (rgb.length
|
|
84
|
+
if (rgb.length === 4) {
|
|
85
85
|
rgb[3] = +rgb[3].toFixed(2);
|
|
86
86
|
}
|
|
87
87
|
return rgb;
|
|
@@ -98,7 +98,7 @@ function hwb2srgbvalues(arena, token) {
|
|
|
98
98
|
return [];
|
|
99
99
|
}
|
|
100
100
|
const rgb = hwb2srgb(h, w, b);
|
|
101
|
-
if (a != null && a
|
|
101
|
+
if (a != null && a !== 1) {
|
|
102
102
|
rgb.push(a);
|
|
103
103
|
}
|
|
104
104
|
return rgb;
|
|
@@ -171,7 +171,7 @@ function cmyk2srgbvalues(arena, token) {
|
|
|
171
171
|
const k = getNumber(arena, t);
|
|
172
172
|
const mul = 1 - k;
|
|
173
173
|
const rgb = [1 - Math.min(1, c * mul + k), 1 - Math.min(1, m * mul + k), 1 - Math.min(1, y * mul + k)];
|
|
174
|
-
if (components.length
|
|
174
|
+
if (components.length === 5) {
|
|
175
175
|
rgb.push(getNumber(arena, components[4]));
|
|
176
176
|
}
|
|
177
177
|
return rgb;
|
|
@@ -182,7 +182,7 @@ function oklab2srgbvalues(arena, token) {
|
|
|
182
182
|
return null;
|
|
183
183
|
}
|
|
184
184
|
const rgb = OKLab_to_sRGB(l, a, b);
|
|
185
|
-
if (alpha != null && alpha
|
|
185
|
+
if (alpha != null && alpha !== 1) {
|
|
186
186
|
rgb.push(alpha);
|
|
187
187
|
}
|
|
188
188
|
return rgb;
|
|
@@ -194,7 +194,7 @@ function oklch2srgbvalues(arena, token) {
|
|
|
194
194
|
}
|
|
195
195
|
const values = lchvalues2labvalues(l, c, h);
|
|
196
196
|
const rgb = OKLab_to_sRGB(values[0], values[1], values[2]);
|
|
197
|
-
if (alpha
|
|
197
|
+
if (alpha !== 1) {
|
|
198
198
|
rgb.push(alpha);
|
|
199
199
|
}
|
|
200
200
|
return rgb;
|
|
@@ -215,14 +215,14 @@ function hslvalues(arena, token) {
|
|
|
215
215
|
t = components[2];
|
|
216
216
|
// @ts-ignore
|
|
217
217
|
let l = getNumber(arena, t);
|
|
218
|
-
if (arena.dataTypes[components[1]]
|
|
218
|
+
if (arena.dataTypes[components[1]] === EnumToken.PercentageTokenType) {
|
|
219
219
|
s *= 100;
|
|
220
220
|
}
|
|
221
|
-
if (arena.dataTypes[components[2]]
|
|
221
|
+
if (arena.dataTypes[components[2]] === EnumToken.PercentageTokenType) {
|
|
222
222
|
l *= 100;
|
|
223
223
|
}
|
|
224
224
|
let a = null;
|
|
225
|
-
if (components.length
|
|
225
|
+
if (components.length === 4) {
|
|
226
226
|
// @ts-ignore
|
|
227
227
|
t = components[3];
|
|
228
228
|
// @ts-ignore
|
|
@@ -288,7 +288,7 @@ function lch2srgbvalues(arena, token) {
|
|
|
288
288
|
}
|
|
289
289
|
// https://www.w3.org/TR/css-color-4/#lab-to-lch
|
|
290
290
|
const rgb = Lab_to_sRGB(l, a, b);
|
|
291
|
-
if (alpha
|
|
291
|
+
if (alpha !== 1) {
|
|
292
292
|
rgb.push(alpha);
|
|
293
293
|
}
|
|
294
294
|
return rgb;
|
|
@@ -308,7 +308,7 @@ function srgb2lsrgbvalues(r, g, b, a = null) {
|
|
|
308
308
|
}
|
|
309
309
|
return (Math.sign(val) || 1) * Math.pow((abs + 0.055) / 1.055, 2.4);
|
|
310
310
|
});
|
|
311
|
-
if (a
|
|
311
|
+
if (a !== 1 && a != null) {
|
|
312
312
|
rgb.push(a);
|
|
313
313
|
}
|
|
314
314
|
return rgb;
|
|
@@ -327,7 +327,7 @@ function lsrgb2srgbvalues(r, g, b, alpha = null) {
|
|
|
327
327
|
}
|
|
328
328
|
return 12.92 * val;
|
|
329
329
|
});
|
|
330
|
-
if (alpha
|
|
330
|
+
if (alpha !== 1 && alpha != null) {
|
|
331
331
|
rgb.push(alpha);
|
|
332
332
|
}
|
|
333
333
|
return rgb;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { ColorType } from '../../../ast/types.js';
|
|
@@ -13,13 +13,15 @@ import { ColorType } from '../../../ast/types.js';
|
|
|
13
13
|
* @param color
|
|
14
14
|
*/
|
|
15
15
|
function getColorType(arena, color) {
|
|
16
|
-
if (arena.colorType[color] === ColorType.COLOR_MIX || arena.
|
|
16
|
+
if (arena.colorType[color] === ColorType.COLOR_MIX || arena.resolveString(arena.unit[color]) === "rel") {
|
|
17
17
|
const token = arena.getTokenAt(color, 2);
|
|
18
18
|
if (token == null) {
|
|
19
19
|
return null;
|
|
20
20
|
}
|
|
21
21
|
return arena.colorType[color] === ColorType.COLOR_MIX
|
|
22
|
-
? ColorType[arena.
|
|
22
|
+
? ColorType[arena.resolveString(arena.stringValue[token])
|
|
23
|
+
?.toUpperCase?.()
|
|
24
|
+
?.replace?.("-", "_")]
|
|
23
25
|
: getColorType(arena, token);
|
|
24
26
|
}
|
|
25
27
|
return arena.colorType[color];
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { equalsIgnoreCase } from '../../../parser/utils/text.js';
|
|
@@ -10,38 +10,36 @@ import { getColorComponents } from './components.js';
|
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* Return the color space of the color
|
|
13
|
+
* @param arena
|
|
13
14
|
* @param color
|
|
14
15
|
* @returns
|
|
15
16
|
*/
|
|
16
17
|
function getColorSpace(arena, color) {
|
|
17
|
-
// if (!("chi" in color)) {
|
|
18
|
-
// return "rgb";
|
|
19
|
-
// }
|
|
20
18
|
let name;
|
|
21
19
|
const components = getColorComponents(arena, color);
|
|
22
20
|
if (components == null || components.length < 3) {
|
|
23
21
|
return null;
|
|
24
22
|
}
|
|
25
|
-
if (equalsIgnoreCase("color", arena.
|
|
26
|
-
if (equalsIgnoreCase("from", arena.
|
|
27
|
-
name = arena.
|
|
23
|
+
if (equalsIgnoreCase("color", arena.resolveString(arena.stringValue[color]))) {
|
|
24
|
+
if (equalsIgnoreCase("from", arena.resolveString(arena.stringValue[components[0]]))) {
|
|
25
|
+
name = arena.resolveString(arena.stringValue[components[2]]);
|
|
28
26
|
}
|
|
29
27
|
else {
|
|
30
|
-
name = arena.
|
|
28
|
+
name = arena.resolveString(arena.stringValue[components[0]]);
|
|
31
29
|
}
|
|
32
30
|
}
|
|
33
|
-
else if (equalsIgnoreCase("color-mix", arena.
|
|
34
|
-
if (equalsIgnoreCase("in", arena.
|
|
35
|
-
name = arena.
|
|
31
|
+
else if (equalsIgnoreCase("color-mix", arena.resolveString(arena.stringValue[color]))) {
|
|
32
|
+
if (equalsIgnoreCase("in", arena.resolveString(arena.stringValue[components[0]]))) {
|
|
33
|
+
name = arena.resolveString(arena.stringValue[components[1]]);
|
|
36
34
|
}
|
|
37
35
|
else {
|
|
38
36
|
return "oklab";
|
|
39
37
|
}
|
|
40
38
|
}
|
|
41
39
|
else {
|
|
42
|
-
name = arena.
|
|
40
|
+
name = arena.resolveString(arena.stringValue[color]);
|
|
43
41
|
}
|
|
44
|
-
return name
|
|
42
|
+
return name === "device-cmyk"
|
|
45
43
|
? "cmyk"
|
|
46
44
|
: name.startsWith("xyz")
|
|
47
45
|
? "xyz"
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/*
|
|
2
|
-
CSS parser, minifier and validator for node and the browser
|
|
2
|
+
CSS parser, minifier and validator for node, deno, bun and the browser
|
|
3
3
|
|
|
4
4
|
Author(s): Thierry Bela
|
|
5
|
-
Version: 2.0
|
|
5
|
+
Version: 2.2.0
|
|
6
6
|
License: MIT
|
|
7
7
|
*/
|
|
8
8
|
import { ColorType, EnumToken } from '../../../ast/types.js';
|
|
@@ -11,12 +11,12 @@ import { expandHexValue } from '../hex.js';
|
|
|
11
11
|
import { equalsIgnoreCase } from '../../../parser/utils/text.js';
|
|
12
12
|
|
|
13
13
|
function getColorComponents(arena, token) {
|
|
14
|
-
if (arena.colorType[token]
|
|
15
|
-
const val = arena.
|
|
14
|
+
if (arena.colorType[token] === ColorType.HEX || arena.colorType[token] === ColorType.LIT) {
|
|
15
|
+
const val = arena.resolveString(arena.stringValue[token]);
|
|
16
16
|
if (equalsIgnoreCase("currentcolor", val)) {
|
|
17
17
|
return null;
|
|
18
18
|
}
|
|
19
|
-
const value = expandHexValue(arena.colorType[token]
|
|
19
|
+
const value = expandHexValue(arena.colorType[token] === ColorType.LIT ? COLORS_NAMES[val.toLowerCase()] : val);
|
|
20
20
|
// @ts-ignore
|
|
21
21
|
return value
|
|
22
22
|
.slice(1)
|
|
@@ -24,23 +24,23 @@ function getColorComponents(arena, token) {
|
|
|
24
24
|
.map((t, index) => arena.createToken(EnumToken.Number, index < 3 ? parseInt(t, 16) : parseInt(t, 16) / 255));
|
|
25
25
|
}
|
|
26
26
|
const result = [];
|
|
27
|
-
for (let child = arena.firstToken[token]; child
|
|
27
|
+
for (let child = arena.firstToken[token]; child !== 0; child = arena.nextSibling[child]) {
|
|
28
28
|
const typ = arena.dataTypes[child];
|
|
29
|
-
if (typ
|
|
30
|
-
typ
|
|
31
|
-
typ
|
|
32
|
-
typ
|
|
29
|
+
if (typ === EnumToken.LiteralTokenType ||
|
|
30
|
+
typ === EnumToken.CommentTokenType ||
|
|
31
|
+
typ === EnumToken.CommaTokenType ||
|
|
32
|
+
typ === EnumToken.WhitespaceTokenType) {
|
|
33
33
|
continue;
|
|
34
34
|
}
|
|
35
35
|
if (typ === EnumToken.FunctionTokenType ||
|
|
36
36
|
typ === EnumToken.WildCardFunctionTokenType ||
|
|
37
37
|
typ === EnumToken.MathFunctionTokenType) {
|
|
38
|
-
if ("var"
|
|
38
|
+
if ("var" === arena.resolveString(arena.stringValue[child]).toLowerCase()) {
|
|
39
39
|
return null;
|
|
40
40
|
}
|
|
41
41
|
}
|
|
42
|
-
if (typ
|
|
43
|
-
equalsIgnoreCase("currentcolor", arena.
|
|
42
|
+
if (typ === EnumToken.ColorTokenType &&
|
|
43
|
+
equalsIgnoreCase("currentcolor", arena.resolveString(arena.stringValue[child]))) {
|
|
44
44
|
return null;
|
|
45
45
|
}
|
|
46
46
|
result.push(child);
|