@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
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// from https://www.w3.org/TR/css-color-4/multiply-matrices.js
|
|
2
|
+
/**
|
|
3
|
+
* Simple matrix (and vector) multiplication
|
|
4
|
+
* Warning: No error handling for incompatible dimensions!
|
|
5
|
+
* @author Lea Verou 2020 MIT License
|
|
6
|
+
*/
|
|
7
|
+
// A is m x n. B is n x p. product is m x p.
|
|
8
|
+
function multiplyMatrices(A, B) {
|
|
9
|
+
let m = A.length;
|
|
10
|
+
if (!Array.isArray(A[0])) {
|
|
11
|
+
// A is vector, convert to [[a, b, c, ...]]
|
|
12
|
+
A = [A];
|
|
13
|
+
}
|
|
14
|
+
if (!Array.isArray(B[0])) {
|
|
15
|
+
// B is vector, convert to [[a], [b], [c], ...]]
|
|
16
|
+
B = B.map((x) => [x]);
|
|
17
|
+
}
|
|
18
|
+
let p = B[0].length;
|
|
19
|
+
let B_cols = B[0].map((_, i) => B.map((x) => x[i])); // transpose B
|
|
20
|
+
let product = A.map((row) => B_cols.map((col) => {
|
|
21
|
+
if (!Array.isArray(row)) {
|
|
22
|
+
return col.reduce((a, c) => a + c * row, 0);
|
|
23
|
+
}
|
|
24
|
+
return row.reduce((a, c, i) => a + c * (col[i] || 0), 0);
|
|
25
|
+
}));
|
|
26
|
+
if (m === 1) {
|
|
27
|
+
product = product[0]; // Avoid [[a, b, c, ...]]
|
|
28
|
+
}
|
|
29
|
+
if (p === 1) {
|
|
30
|
+
return product.map((x) => x[0]); // Avoid [[a], [b], [c], ...]]
|
|
31
|
+
}
|
|
32
|
+
return product;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export { multiplyMatrices };
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { multiplyMatrices } from './utils/matrix.js';
|
|
2
|
+
import './utils/constants.js';
|
|
3
|
+
import '../../ast/types.js';
|
|
4
|
+
import '../../ast/minify.js';
|
|
5
|
+
import '../../parser/parse.js';
|
|
6
|
+
import { lsrgb2srgbvalues, srgb2lsrgbvalues } from './srgb.js';
|
|
7
|
+
import '../sourcemap/lib/encode.js';
|
|
8
|
+
|
|
9
|
+
function xyzd502srgb(x, y, z) {
|
|
10
|
+
// @ts-ignore
|
|
11
|
+
return lsrgb2srgbvalues(
|
|
12
|
+
/* r: */
|
|
13
|
+
x * 3.1341359569958707 -
|
|
14
|
+
y * 1.6173863321612538 -
|
|
15
|
+
0.4906619460083532 * z,
|
|
16
|
+
/* g: */
|
|
17
|
+
x * -0.978795502912089 +
|
|
18
|
+
y * 1.916254567259524 +
|
|
19
|
+
0.03344273116131949 * z,
|
|
20
|
+
/* b: */
|
|
21
|
+
x * 0.07195537988411677 -
|
|
22
|
+
y * 0.2289768264158322 +
|
|
23
|
+
1.405386058324125 * z);
|
|
24
|
+
}
|
|
25
|
+
function XYZ_to_lin_sRGB(x, y, z) {
|
|
26
|
+
// convert XYZ to linear-light sRGB
|
|
27
|
+
const M = [
|
|
28
|
+
[12831 / 3959, -329 / 214, -1974 / 3959],
|
|
29
|
+
[-851781 / 878810, 1648619 / 878810, 36519 / 878810],
|
|
30
|
+
[705 / 12673, -2585 / 12673, 705 / 667],
|
|
31
|
+
];
|
|
32
|
+
const XYZ = [x, y, z]; // convert to XYZ
|
|
33
|
+
return multiplyMatrices(M, XYZ).map((v) => v);
|
|
34
|
+
}
|
|
35
|
+
function XYZ_D50_to_D65(x, y, z) {
|
|
36
|
+
// Bradford chromatic adaptation from D50 to D65
|
|
37
|
+
const M = [
|
|
38
|
+
[0.9554734527042182, -0.023098536874261423, 0.0632593086610217],
|
|
39
|
+
[-0.028369706963208136, 1.0099954580058226, 0.021041398966943008],
|
|
40
|
+
[0.012314001688319899, -0.020507696433477912, 1.3303659366080753]
|
|
41
|
+
];
|
|
42
|
+
const XYZ = [x, y, z];
|
|
43
|
+
return multiplyMatrices(M, XYZ); //.map((v: number) => v);
|
|
44
|
+
}
|
|
45
|
+
function srgb2xyz(r, g, b, alpha) {
|
|
46
|
+
[r, g, b] = srgb2lsrgbvalues(r, g, b);
|
|
47
|
+
const rgb = [
|
|
48
|
+
0.436065742824811 * r +
|
|
49
|
+
0.3851514688337912 * g +
|
|
50
|
+
0.14307845442264197 * b,
|
|
51
|
+
0.22249319175623702 * r +
|
|
52
|
+
0.7168870538238823 * g +
|
|
53
|
+
0.06061979053616537 * b,
|
|
54
|
+
0.013923904500943465 * r +
|
|
55
|
+
0.09708128566574634 * g +
|
|
56
|
+
0.7140993584005155 * b
|
|
57
|
+
];
|
|
58
|
+
if (alpha != null && alpha != 1) {
|
|
59
|
+
rgb.push(alpha);
|
|
60
|
+
}
|
|
61
|
+
return rgb;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { XYZ_D50_to_D65, XYZ_to_lin_sRGB, srgb2xyz, xyzd502srgb };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { multiplyMatrices } from './utils/matrix.js';
|
|
2
|
+
import './utils/constants.js';
|
|
3
|
+
import '../../ast/types.js';
|
|
4
|
+
import '../../ast/minify.js';
|
|
5
|
+
import '../../parser/parse.js';
|
|
6
|
+
import { xyz2lab } from './lab.js';
|
|
7
|
+
import { lab2lchvalues } from './lch.js';
|
|
8
|
+
import { XYZ_D50_to_D65 } from './xyz.js';
|
|
9
|
+
import '../sourcemap/lib/encode.js';
|
|
10
|
+
|
|
11
|
+
function xyzd502lch(x, y, z, alpha) {
|
|
12
|
+
// @ts-ignore
|
|
13
|
+
const [l, a, b] = xyz2lab(...XYZ_D50_to_D65(x, y, z));
|
|
14
|
+
// L in range [0,100]. For use in CSS, add a percent
|
|
15
|
+
// @ts-ignore
|
|
16
|
+
return lab2lchvalues(l, a, b, alpha);
|
|
17
|
+
}
|
|
18
|
+
function XYZ_D65_to_D50(x, y, z) {
|
|
19
|
+
// Bradford chromatic adaptation from D65 to D50
|
|
20
|
+
// The matrix below is the result of three operations:
|
|
21
|
+
// - convert from XYZ to retinal cone domain
|
|
22
|
+
// - scale components from one reference white to another
|
|
23
|
+
// - convert back to XYZ
|
|
24
|
+
// see https://github.com/LeaVerou/color.js/pull/354/files
|
|
25
|
+
var M = [
|
|
26
|
+
[1.0479297925449969, 0.022946870601609652, -0.05019226628920524],
|
|
27
|
+
[0.02962780877005599, 0.9904344267538799, -0.017073799063418826],
|
|
28
|
+
[-0.009243040646204504, 0.015055191490298152, 0.7518742814281371]
|
|
29
|
+
];
|
|
30
|
+
return multiplyMatrices(M, [x, y, z]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { XYZ_D65_to_D50, xyzd502lch };
|
|
@@ -1,14 +1,19 @@
|
|
|
1
|
-
import { getAngle,
|
|
1
|
+
import { getAngle, color2srgbvalues, clamp } from './color/color.js';
|
|
2
|
+
import { colorFuncColorSpace, COLORS_NAMES } from './color/utils/constants.js';
|
|
3
|
+
import { getComponents } from './color/utils/components.js';
|
|
4
|
+
import { reduceHexValue, srgb2hexvalues, rgb2hex, hsl2hex, hwb2hex, cmyk2hex, oklab2hex, oklch2hex, lab2hex, lch2hex } from './color/hex.js';
|
|
2
5
|
import { EnumToken } from '../ast/types.js';
|
|
3
6
|
import '../ast/minify.js';
|
|
4
7
|
import { expand } from '../ast/expand.js';
|
|
8
|
+
import { colorMix } from './color/colormix.js';
|
|
9
|
+
import { parseRelativeColor } from './color/relativecolor.js';
|
|
5
10
|
import { SourceMap } from './sourcemap/sourcemap.js';
|
|
6
11
|
import '../parser/parse.js';
|
|
7
|
-
import { isNewLine } from '../parser/utils/syntax.js';
|
|
12
|
+
import { isColor, isNewLine } from '../parser/utils/syntax.js';
|
|
8
13
|
|
|
9
|
-
const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk'];
|
|
14
|
+
const colorsFunc = ['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'device-cmyk', 'color-mix', 'color', 'oklab', 'lab', 'oklch', 'lch'];
|
|
10
15
|
function reduceNumber(val) {
|
|
11
|
-
val = (+val)
|
|
16
|
+
val = String(+val);
|
|
12
17
|
if (val === '0') {
|
|
13
18
|
return '0';
|
|
14
19
|
}
|
|
@@ -47,7 +52,7 @@ function doRender(data, options = {}) {
|
|
|
47
52
|
newLine: '\n',
|
|
48
53
|
compress: false,
|
|
49
54
|
removeComments: false,
|
|
50
|
-
}), sourcemap: false,
|
|
55
|
+
}), sourcemap: false, convertColor: true, expandNestingRules: false, preserveLicense: false, ...options
|
|
51
56
|
};
|
|
52
57
|
const startTime = performance.now();
|
|
53
58
|
const errors = [];
|
|
@@ -83,12 +88,10 @@ function updateSourceMap(node, options, cache, sourcemap, position, str) {
|
|
|
83
88
|
if ([EnumToken.RuleNodeType, EnumToken.AtRuleNodeType].includes(node.typ)) {
|
|
84
89
|
let src = node.loc?.src ?? '';
|
|
85
90
|
let output = options.output ?? '';
|
|
86
|
-
// if (src !== '') {
|
|
87
91
|
if (!(src in cache)) {
|
|
88
92
|
// @ts-ignore
|
|
89
93
|
cache[src] = options.resolve(src, options.cwd ?? '').relative;
|
|
90
94
|
}
|
|
91
|
-
// }
|
|
92
95
|
if (!(output in cache)) {
|
|
93
96
|
// @ts-ignore
|
|
94
97
|
cache[output] = options.resolve(output, options.cwd).relative;
|
|
@@ -199,6 +202,27 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
199
202
|
return acc + renderToken(curr, options, cache, reducer, errors);
|
|
200
203
|
};
|
|
201
204
|
}
|
|
205
|
+
if (token.typ == EnumToken.FunctionTokenType && colorsFunc.includes(token.val)) {
|
|
206
|
+
if (isColor(token)) {
|
|
207
|
+
// @ts-ignore
|
|
208
|
+
token.typ = EnumToken.ColorTokenType;
|
|
209
|
+
if (token.chi[0].typ == EnumToken.IdenTokenType && token.chi[0].val == 'from') {
|
|
210
|
+
// @ts-ignore
|
|
211
|
+
token.cal = 'rel';
|
|
212
|
+
}
|
|
213
|
+
else if (token.val == 'color-mix' && token.chi[0].typ == EnumToken.IdenTokenType && token.chi[0].val == 'in') {
|
|
214
|
+
// @ts-ignore
|
|
215
|
+
token.cal = 'mix';
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
if (token.val == 'color') {
|
|
219
|
+
// @ts-ignore
|
|
220
|
+
token.cal = 'col';
|
|
221
|
+
}
|
|
222
|
+
token.chi = token.chi.filter((t) => ![EnumToken.WhitespaceTokenType, EnumToken.CommaTokenType, EnumToken.CommentTokenType].includes(t.typ));
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
202
226
|
switch (token.typ) {
|
|
203
227
|
case EnumToken.ListToken:
|
|
204
228
|
return token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), '');
|
|
@@ -241,16 +265,76 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
241
265
|
case EnumToken.Div:
|
|
242
266
|
return '/';
|
|
243
267
|
case EnumToken.ColorTokenType:
|
|
244
|
-
if (options.
|
|
245
|
-
if (token.
|
|
268
|
+
if (options.convertColor) {
|
|
269
|
+
if (token.cal == 'mix' && token.val == 'color-mix') {
|
|
270
|
+
const children = token.chi.reduce((acc, t) => {
|
|
271
|
+
if (t.typ == EnumToken.ColorTokenType) {
|
|
272
|
+
acc.push([t]);
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ)) {
|
|
276
|
+
acc[acc.length - 1].push(t);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
return acc;
|
|
280
|
+
}, [[]]);
|
|
281
|
+
const value = colorMix(children[0][1], children[0][2], children[1][0], children[1][1], children[2][0], children[2][1]);
|
|
282
|
+
if (value != null) {
|
|
283
|
+
token = value;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (token.cal == 'rel' && ['rgb', 'hsl', 'hwb', 'lab', 'lch', 'oklab', 'oklch', 'color'].includes(token.val)) {
|
|
287
|
+
const chi = getComponents(token);
|
|
288
|
+
const offset = token.val == 'color' ? 2 : 1;
|
|
289
|
+
// @ts-ignore
|
|
290
|
+
const color = chi[1];
|
|
291
|
+
const components = parseRelativeColor(token.val == 'color' ? chi[offset].val : token.val, color, chi[offset + 1], chi[offset + 2], chi[offset + 3], chi[offset + 4]);
|
|
292
|
+
if (components != null) {
|
|
293
|
+
token.chi = [...(token.val == 'color' ? [chi[offset]] : []), ...Object.values(components)];
|
|
294
|
+
delete token.cal;
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
if (token.val == 'color') {
|
|
298
|
+
if (token.chi[0].typ == EnumToken.IdenTokenType && colorFuncColorSpace.includes(token.chi[0].val.toLowerCase())) {
|
|
299
|
+
// @ts-ignore
|
|
300
|
+
return reduceHexValue(srgb2hexvalues(...color2srgbvalues(token)));
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
if (token.cal != null) {
|
|
304
|
+
let slice = false;
|
|
305
|
+
if (token.cal == 'rel') {
|
|
306
|
+
const last = token.chi.at(-1);
|
|
307
|
+
if ((last.typ == EnumToken.NumberTokenType && last.val == '1') || (last.typ == EnumToken.IdenTokenType && last.val == 'none')) {
|
|
308
|
+
const prev = token.chi.at(-2);
|
|
309
|
+
if (prev.typ == EnumToken.LiteralTokenType && prev.val == '/') {
|
|
310
|
+
slice = true;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
return clamp(token).val + '(' + (slice ? token.chi.slice(0, -2) : token.chi).reduce((acc, curr) => {
|
|
315
|
+
const val = renderToken(curr, options, cache);
|
|
316
|
+
if ([EnumToken.LiteralTokenType, EnumToken.CommaTokenType].includes(curr.typ)) {
|
|
317
|
+
return acc + val;
|
|
318
|
+
}
|
|
319
|
+
if (acc.length > 0) {
|
|
320
|
+
return acc + (['/', ','].includes(acc.at(-1)) ? '' : ' ') + val;
|
|
321
|
+
}
|
|
322
|
+
return val;
|
|
323
|
+
}, '') + ')';
|
|
324
|
+
}
|
|
325
|
+
if (token.kin == 'lit' && token.val.localeCompare('currentcolor', undefined, { sensitivity: 'base' }) == 0) {
|
|
246
326
|
return 'currentcolor';
|
|
247
327
|
}
|
|
328
|
+
clamp(token);
|
|
329
|
+
if (Array.isArray(token.chi) && token.chi.some((t) => t.typ == EnumToken.FunctionTokenType || (t.typ == EnumToken.ColorTokenType && Array.isArray(t.chi)))) {
|
|
330
|
+
return (token.val.endsWith('a') ? token.val.slice(0, -1) : token.val) + '(' + token.chi.reduce((acc, curr) => acc + (acc.length > 0 && !(acc.endsWith('/') || curr.typ == EnumToken.LiteralTokenType) ? ' ' : '') + renderToken(curr, options, cache), '') + ')';
|
|
331
|
+
}
|
|
248
332
|
let value = token.kin == 'hex' ? token.val.toLowerCase() : (token.kin == 'lit' ? COLORS_NAMES[token.val.toLowerCase()] : '');
|
|
249
333
|
if (token.val == 'rgb' || token.val == 'rgba') {
|
|
250
|
-
value =
|
|
334
|
+
value = rgb2hex(token);
|
|
251
335
|
}
|
|
252
336
|
else if (token.val == 'hsl' || token.val == 'hsla') {
|
|
253
|
-
value =
|
|
337
|
+
value = hsl2hex(token);
|
|
254
338
|
}
|
|
255
339
|
else if (token.val == 'hwb') {
|
|
256
340
|
value = hwb2hex(token);
|
|
@@ -258,36 +342,36 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
258
342
|
else if (token.val == 'device-cmyk') {
|
|
259
343
|
value = cmyk2hex(token);
|
|
260
344
|
}
|
|
261
|
-
|
|
345
|
+
else if (token.val == 'oklab') {
|
|
346
|
+
value = oklab2hex(token);
|
|
347
|
+
}
|
|
348
|
+
else if (token.val == 'oklch') {
|
|
349
|
+
value = oklch2hex(token);
|
|
350
|
+
}
|
|
351
|
+
else if (token.val == 'lab') {
|
|
352
|
+
value = lab2hex(token);
|
|
353
|
+
}
|
|
354
|
+
else if (token.val == 'lch') {
|
|
355
|
+
value = lch2hex(token);
|
|
356
|
+
}
|
|
262
357
|
if (value !== '') {
|
|
263
|
-
|
|
264
|
-
if (value[1] == value[2] &&
|
|
265
|
-
value[3] == value[4] &&
|
|
266
|
-
value[5] == value[6]) {
|
|
267
|
-
value = `#${value[1]}${value[3]}${value[5]}`;
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
else if (value.length == 9) {
|
|
271
|
-
if (value[1] == value[2] &&
|
|
272
|
-
value[3] == value[4] &&
|
|
273
|
-
value[5] == value[6] &&
|
|
274
|
-
value[7] == value[8]) {
|
|
275
|
-
value = `#${value[1]}${value[3]}${value[5]}${value[7]}`;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
return named_color != null && named_color.length <= value.length ? named_color : value;
|
|
358
|
+
return reduceHexValue(value);
|
|
279
359
|
}
|
|
280
360
|
}
|
|
281
361
|
if (token.kin == 'hex' || token.kin == 'lit') {
|
|
282
362
|
return token.val;
|
|
283
363
|
}
|
|
364
|
+
if (Array.isArray(token.chi)) {
|
|
365
|
+
return (token.val.endsWith('a') ? token.val.slice(0, -1) : token.val) + '(' + token.chi.reduce((acc, curr) => acc + (acc.length > 0 && !(acc.endsWith('/') || curr.typ == EnumToken.LiteralTokenType) ? ' ' : '') + renderToken(curr, options, cache), '') + ')';
|
|
366
|
+
}
|
|
284
367
|
case EnumToken.ParensTokenType:
|
|
285
368
|
case EnumToken.FunctionTokenType:
|
|
286
369
|
case EnumToken.UrlFunctionTokenType:
|
|
287
370
|
case EnumToken.ImageFunctionTokenType:
|
|
288
|
-
case EnumToken.PseudoClassFuncTokenType:
|
|
289
371
|
case EnumToken.TimingFunctionTokenType:
|
|
372
|
+
case EnumToken.PseudoClassFuncTokenType:
|
|
290
373
|
case EnumToken.TimelineFunctionTokenType:
|
|
374
|
+
case EnumToken.GridTemplateFuncTokenType:
|
|
291
375
|
if (token.typ == EnumToken.FunctionTokenType &&
|
|
292
376
|
token.val == 'calc' &&
|
|
293
377
|
token.chi.length == 1 &&
|
|
@@ -350,6 +434,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
350
434
|
case EnumToken.ImportantTokenType:
|
|
351
435
|
return '!important';
|
|
352
436
|
case EnumToken.AttrTokenType:
|
|
437
|
+
case EnumToken.IdenListTokenType:
|
|
353
438
|
return '[' + token.chi.reduce(reducer, '') + ']';
|
|
354
439
|
case EnumToken.TimeTokenType:
|
|
355
440
|
case EnumToken.AngleTokenType:
|
|
@@ -416,10 +501,23 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
416
501
|
}
|
|
417
502
|
return '0';
|
|
418
503
|
}
|
|
504
|
+
if (token.typ == EnumToken.TimeTokenType) {
|
|
505
|
+
if (unit == 'ms') {
|
|
506
|
+
// @ts-ignore
|
|
507
|
+
const v = reduceNumber(val / 1000);
|
|
508
|
+
if (v.length + 1 <= val.length) {
|
|
509
|
+
return v + 's';
|
|
510
|
+
}
|
|
511
|
+
return val + 'ms';
|
|
512
|
+
}
|
|
513
|
+
return val + 's';
|
|
514
|
+
}
|
|
419
515
|
return val.includes('/') ? val.replace('/', unit + '/') : val + unit;
|
|
516
|
+
case EnumToken.FlexTokenType:
|
|
420
517
|
case EnumToken.PercentageTokenType:
|
|
518
|
+
const uni = token.typ == EnumToken.PercentageTokenType ? '%' : 'fr';
|
|
421
519
|
const perc = token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : reduceNumber(token.val);
|
|
422
|
-
return options.minify && perc == '0' ? '0' : (perc.includes('/') ? perc.replace('/', '
|
|
520
|
+
return options.minify && perc == '0' ? '0' : (perc.includes('/') ? perc.replace('/', uni + '/') : perc + uni);
|
|
423
521
|
case EnumToken.NumberTokenType:
|
|
424
522
|
return token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : reduceNumber(token.val);
|
|
425
523
|
case EnumToken.CommentTokenType:
|
package/dist/node/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import { doRender } from '../lib/renderer/render.js';
|
|
|
6
6
|
export { renderToken } from '../lib/renderer/render.js';
|
|
7
7
|
import { doParse } from '../lib/parser/parse.js';
|
|
8
8
|
export { parseString, parseTokens } from '../lib/parser/parse.js';
|
|
9
|
-
import '../lib/renderer/utils/
|
|
9
|
+
import '../lib/renderer/color/utils/constants.js';
|
|
10
10
|
import { resolve, dirname } from '../lib/fs/resolve.js';
|
|
11
11
|
import { load } from './load.js';
|
|
12
12
|
|
package/dist/node/load.js
CHANGED
package/dist/web/index.js
CHANGED
|
@@ -6,7 +6,7 @@ import { doRender } from '../lib/renderer/render.js';
|
|
|
6
6
|
export { renderToken } from '../lib/renderer/render.js';
|
|
7
7
|
import { doParse } from '../lib/parser/parse.js';
|
|
8
8
|
export { parseString, parseTokens } from '../lib/parser/parse.js';
|
|
9
|
-
import '../lib/renderer/utils/
|
|
9
|
+
import '../lib/renderer/color/utils/constants.js';
|
|
10
10
|
import { resolve, dirname } from '../lib/fs/resolve.js';
|
|
11
11
|
import { load } from './load.js';
|
|
12
12
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbela99/css-parser",
|
|
3
3
|
"description": "CSS parser for node and the browser",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./dist/node/index.js",
|
|
7
7
|
"./umd": "./dist/index-umd-web.js",
|
|
@@ -12,11 +12,11 @@
|
|
|
12
12
|
"typings": "dist/index.d.ts",
|
|
13
13
|
"scripts": {
|
|
14
14
|
"build": "rollup -c;./build.sh dist/index.d.ts 'declare interface' 'declare type'",
|
|
15
|
-
"test": "web-test-runner \"test
|
|
16
|
-
"test:cov": "c8 --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test
|
|
17
|
-
"test:web-cov": "web-test-runner
|
|
15
|
+
"test": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=.; mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
|
|
16
|
+
"test:cov": "c8 --reporter=html --reporter=text --reporter=json-summary mocha --reporter-options='maxDiffSize=1801920' \"test/**/node.spec.js\"",
|
|
17
|
+
"test:web-cov": "web-test-runner \"test/**/web.spec.js\" --node-resolve --playwright --browsers chromium firefox webkit --root-dir=. --coverage",
|
|
18
18
|
"profile": "node --inspect-brk test/inspect.mjs",
|
|
19
|
-
"debug": "web-test-runner \"test
|
|
19
|
+
"debug": "web-test-runner \"test/**/web.spec.js\" --manual --open --node-resolve --root-dir=."
|
|
20
20
|
},
|
|
21
21
|
"repository": {
|
|
22
22
|
"type": "git",
|
|
@@ -45,19 +45,20 @@
|
|
|
45
45
|
"homepage": "https://github.com/tbela99/css-parser#readme",
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@esm-bundle/chai": "^4.3.4-fix.0",
|
|
48
|
-
"@rollup/plugin-commonjs": "^25.0.
|
|
49
|
-
"@rollup/plugin-json": "^6.
|
|
50
|
-
"@rollup/plugin-node-resolve": "^15.
|
|
51
|
-
"@rollup/plugin-typescript": "^11.1.
|
|
52
|
-
"@types/chai": "^4.3.
|
|
53
|
-
"@types/mocha": "^10.0.
|
|
54
|
-
"@types/node": "^20.
|
|
55
|
-
"@web/test-runner": "^0.
|
|
48
|
+
"@rollup/plugin-commonjs": "^25.0.7",
|
|
49
|
+
"@rollup/plugin-json": "^6.1.0",
|
|
50
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
51
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
52
|
+
"@types/chai": "^4.3.12",
|
|
53
|
+
"@types/mocha": "^10.0.6",
|
|
54
|
+
"@types/node": "^20.11.25",
|
|
55
|
+
"@web/test-runner": "^0.18.1",
|
|
56
56
|
"@web/test-runner-playwright": "^0.11.0",
|
|
57
|
-
"c8": "^
|
|
58
|
-
"mocha": "^10.
|
|
59
|
-
"
|
|
60
|
-
"rollup
|
|
61
|
-
"
|
|
57
|
+
"c8": "^9.1.0",
|
|
58
|
+
"mocha": "^10.4.0",
|
|
59
|
+
"playwright": "^1.42.1",
|
|
60
|
+
"rollup": "^4.13.0",
|
|
61
|
+
"rollup-plugin-dts": "^6.1.0",
|
|
62
|
+
"tslib": "^2.6.2"
|
|
62
63
|
}
|
|
63
64
|
}
|
package/quickjs.sh
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
qjsc -e -o c/css.c dist/index-umd-web.js
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
class IterableWeakMap {
|
|
2
|
-
#map;
|
|
3
|
-
#set;
|
|
4
|
-
constructor(iterable) {
|
|
5
|
-
this.#map = new WeakMap;
|
|
6
|
-
this.#set = new Set;
|
|
7
|
-
if (iterable) {
|
|
8
|
-
for (const [key, value] of iterable) {
|
|
9
|
-
const ref = new WeakRef(key);
|
|
10
|
-
this.#set.add(ref);
|
|
11
|
-
this.#map.set(key, value);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
has(key) {
|
|
16
|
-
return this.#map.has(key);
|
|
17
|
-
}
|
|
18
|
-
set(key, value) {
|
|
19
|
-
if (!this.#map.has(key)) {
|
|
20
|
-
this.#set.add(new WeakRef(key));
|
|
21
|
-
}
|
|
22
|
-
this.#map.set(key, value);
|
|
23
|
-
return this;
|
|
24
|
-
}
|
|
25
|
-
get(key) {
|
|
26
|
-
return this.#map.get(key);
|
|
27
|
-
}
|
|
28
|
-
delete(key) {
|
|
29
|
-
if (this.#map.has(key)) {
|
|
30
|
-
for (const ref of this.#set) {
|
|
31
|
-
if (ref.deref() === key) {
|
|
32
|
-
this.#set.delete(ref);
|
|
33
|
-
break;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return this.#map.delete(key);
|
|
37
|
-
}
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
*[Symbol.iterator]() {
|
|
41
|
-
for (const ref of new Set(this.#set)) {
|
|
42
|
-
const key = ref.deref();
|
|
43
|
-
if (key == null) {
|
|
44
|
-
this.#set.delete(ref);
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
// @ts-ignore
|
|
48
|
-
yield [key, this.#map.get(key)];
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
export { IterableWeakMap };
|