@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.
Files changed (57) hide show
  1. package/{LICENSE → LICENSE.md} +1 -1
  2. package/README.md +250 -77
  3. package/dist/config.json.js +245 -1
  4. package/dist/index-umd-web.js +4241 -1583
  5. package/dist/index.cjs +4242 -1584
  6. package/dist/index.d.ts +71 -23
  7. package/dist/lib/ast/expand.js +1 -1
  8. package/dist/lib/ast/features/calc.js +31 -192
  9. package/dist/lib/ast/features/index.js +3 -3
  10. package/dist/lib/ast/features/inlinecssvariables.js +6 -6
  11. package/dist/lib/ast/features/shorthand.js +5 -6
  12. package/dist/lib/ast/math/expression.js +220 -0
  13. package/dist/lib/ast/{features/utils → math}/math.js +4 -4
  14. package/dist/lib/ast/minify.js +0 -1
  15. package/dist/lib/ast/types.js +31 -13
  16. package/dist/lib/ast/utils/minifyfeature.js +4 -3
  17. package/dist/lib/ast/walk.js +24 -4
  18. package/dist/lib/fs/resolve.js +4 -3
  19. package/dist/lib/parser/declaration/list.js +6 -2
  20. package/dist/lib/parser/declaration/map.js +158 -24
  21. package/dist/lib/parser/declaration/set.js +42 -22
  22. package/dist/lib/parser/parse.js +345 -349
  23. package/dist/lib/parser/tokenize.js +220 -223
  24. package/dist/lib/parser/utils/declaration.js +67 -0
  25. package/dist/lib/parser/utils/syntax.js +172 -6
  26. package/dist/lib/parser/utils/type.js +2 -2
  27. package/dist/lib/renderer/color/a98rgb.js +64 -0
  28. package/dist/lib/renderer/color/color.js +521 -0
  29. package/dist/lib/renderer/color/colormix.js +337 -0
  30. package/dist/lib/renderer/color/hex.js +92 -0
  31. package/dist/lib/renderer/color/hsl.js +118 -0
  32. package/dist/lib/renderer/color/hsv.js +20 -0
  33. package/dist/lib/renderer/color/hwb.js +101 -0
  34. package/dist/lib/renderer/color/lab.js +136 -0
  35. package/dist/lib/renderer/color/lch.js +79 -0
  36. package/dist/lib/renderer/color/oklab.js +121 -0
  37. package/dist/lib/renderer/color/oklch.js +65 -0
  38. package/dist/lib/renderer/color/p3.js +57 -0
  39. package/dist/lib/renderer/color/prophotorgb.js +56 -0
  40. package/dist/lib/renderer/color/rec2020.js +70 -0
  41. package/dist/lib/renderer/color/relativecolor.js +152 -0
  42. package/dist/lib/renderer/color/rgb.js +44 -0
  43. package/dist/lib/renderer/color/srgb.js +261 -0
  44. package/dist/lib/renderer/color/utils/components.js +20 -0
  45. package/dist/lib/renderer/color/utils/constants.js +191 -0
  46. package/dist/lib/renderer/color/utils/matrix.js +35 -0
  47. package/dist/lib/renderer/color/xyz.js +64 -0
  48. package/dist/lib/renderer/color/xyzd50.js +33 -0
  49. package/dist/lib/renderer/render.js +128 -30
  50. package/dist/node/index.js +1 -1
  51. package/dist/node/load.js +1 -1
  52. package/dist/web/index.js +1 -1
  53. package/package.json +19 -18
  54. package/quickjs.sh +1 -0
  55. package/dist/lib/iterable/weakmap.js +0 -53
  56. package/dist/lib/renderer/utils/color.js +0 -499
  57. /package/dist/lib/iterable/{set.js → weakset.js} +0 -0
@@ -0,0 +1,521 @@
1
+ import { EnumToken } from '../../ast/types.js';
2
+ import '../../ast/minify.js';
3
+ import '../../parser/parse.js';
4
+ import { srgb2rgb, lch2rgb, lab2rgb, oklch2rgb, oklab2rgb, hwb2rgb, hsl2rgb, hex2rgb } from './rgb.js';
5
+ import { srgb2hsl, lch2hsl, lab2hsl, oklch2hsl, oklab2hsl, hwb2hsl, hex2hsl, rgb2hsl } from './hsl.js';
6
+ import { lch2hwb, lab2hwb, oklch2hwb, oklab2hwb, hsl2hwb, rgb2hwb } from './hwb.js';
7
+ import { srgb2lab, oklch2lab, oklab2lab, lch2lab, hwb2lab, hsl2lab, rgb2lab, hex2lab } from './lab.js';
8
+ import { srgb2lch, oklch2lch, oklab2lch, lab2lch, hwb2lch, hsl2lch, rgb2lch, hex2lch } from './lch.js';
9
+ import { srgb2oklab, oklch2oklab, lch2oklab, lab2oklab, hwb2oklab, hsl2oklab, rgb2oklab, hex2oklab } from './oklab.js';
10
+ import { lch2oklch, oklab2oklch, lab2oklch, hwb2oklch, hsl2oklch, rgb2oklch, hex2oklch, srgb2oklch } from './oklch.js';
11
+ import { colorFuncColorSpace } from './utils/constants.js';
12
+ import { getComponents } from './utils/components.js';
13
+ import { xyz2srgb, lsrgb2srgbvalues, srgb2lsrgbvalues, lch2srgb, oklab2srgb, lab2srgb, hwb2srgb, hsl2srgb, rgb2srgb, hex2srgb } from './srgb.js';
14
+ import { prophotorgb2srgbvalues, srgb2prophotorgbvalues } from './prophotorgb.js';
15
+ import { a98rgb2srgbvalues, srgb2a98values } from './a98rgb.js';
16
+ import { rec20202srgb, srgb2rec2020values } from './rec2020.js';
17
+ import { xyzd502srgb, srgb2xyz } from './xyz.js';
18
+ import { p32srgbvalues, srgb2p3values } from './p3.js';
19
+ import { XYZ_D65_to_D50 } from './xyzd50.js';
20
+ import '../sourcemap/lib/encode.js';
21
+
22
+ function convert(token, to) {
23
+ if (token.kin == to) {
24
+ return token;
25
+ }
26
+ if (token.kin == 'color') {
27
+ const colorSpace = token.chi.find(t => ![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType].includes(t.typ));
28
+ if (colorSpace.val == to) {
29
+ return token;
30
+ }
31
+ }
32
+ let values = [];
33
+ if (to == 'hsl') {
34
+ switch (token.kin) {
35
+ case 'rgb':
36
+ case 'rgba':
37
+ values.push(...rgb2hsl(token));
38
+ break;
39
+ case 'hex':
40
+ case 'lit':
41
+ values.push(...hex2hsl(token));
42
+ break;
43
+ case 'hwb':
44
+ values.push(...hwb2hsl(token));
45
+ break;
46
+ case 'oklab':
47
+ values.push(...oklab2hsl(token));
48
+ break;
49
+ case 'oklch':
50
+ values.push(...oklch2hsl(token));
51
+ break;
52
+ case 'lab':
53
+ values.push(...lab2hsl(token));
54
+ break;
55
+ case 'lch':
56
+ values.push(...lch2hsl(token));
57
+ break;
58
+ case 'color':
59
+ // @ts-ignore
60
+ values.push(...srgb2hsl(...color2srgbvalues(token)));
61
+ break;
62
+ }
63
+ if (values.length > 0) {
64
+ return values2hsltoken(values);
65
+ }
66
+ }
67
+ else if (to == 'hwb') {
68
+ switch (token.kin) {
69
+ case 'rgb':
70
+ case 'rgba':
71
+ values.push(...rgb2hwb(token));
72
+ break;
73
+ case 'hex':
74
+ case 'lit':
75
+ values.push(...hex2hsl(token));
76
+ break;
77
+ case 'hsl':
78
+ case 'hsla':
79
+ values.push(...hsl2hwb(token));
80
+ break;
81
+ case 'oklab':
82
+ values.push(...oklab2hwb(token));
83
+ break;
84
+ case 'oklch':
85
+ values.push(...oklch2hwb(token));
86
+ break;
87
+ case 'lab':
88
+ values.push(...lab2hwb(token));
89
+ break;
90
+ case 'lch':
91
+ values.push(...lch2hwb(token));
92
+ break;
93
+ }
94
+ if (values.length > 0) {
95
+ return values2hwbtoken(values);
96
+ }
97
+ }
98
+ else if (to == 'rgb') {
99
+ switch (token.kin) {
100
+ case 'hex':
101
+ case 'lit':
102
+ values.push(...hex2rgb(token));
103
+ break;
104
+ case 'hsl':
105
+ values.push(...hsl2rgb(token));
106
+ break;
107
+ case 'hwb':
108
+ values.push(...hwb2rgb(token));
109
+ break;
110
+ case 'oklab':
111
+ values.push(...oklab2rgb(token));
112
+ break;
113
+ case 'oklch':
114
+ values.push(...oklch2rgb(token));
115
+ break;
116
+ case 'lab':
117
+ values.push(...lab2rgb(token));
118
+ break;
119
+ case 'lch':
120
+ values.push(...lch2rgb(token));
121
+ break;
122
+ case 'color':
123
+ // @ts-ignore
124
+ values.push(...srgb2rgb(...color2srgbvalues(token)));
125
+ break;
126
+ }
127
+ if (values.length > 0) {
128
+ return values2rgbtoken(values);
129
+ }
130
+ }
131
+ else if (to == 'lab') {
132
+ switch (token.kin) {
133
+ case 'hex':
134
+ case 'lit':
135
+ values.push(...hex2lab(token));
136
+ break;
137
+ case 'rgb':
138
+ case 'rgba':
139
+ values.push(...rgb2lab(token));
140
+ break;
141
+ case 'hsl':
142
+ case 'hsla':
143
+ values.push(...hsl2lab(token));
144
+ break;
145
+ case 'hwb':
146
+ values.push(...hwb2lab(token));
147
+ break;
148
+ case 'lch':
149
+ values.push(...lch2lab(token));
150
+ break;
151
+ case 'oklab':
152
+ values.push(...oklab2lab(token));
153
+ break;
154
+ case 'oklch':
155
+ values.push(...oklch2lab(token));
156
+ break;
157
+ case 'color':
158
+ // @ts-ignore
159
+ values.push(...srgb2lab(...color2srgbvalues(token)));
160
+ break;
161
+ }
162
+ if (values.length > 0) {
163
+ return values2colortoken(values, to);
164
+ }
165
+ }
166
+ else if (to == 'lch') {
167
+ switch (token.kin) {
168
+ case 'hex':
169
+ case 'lit':
170
+ values.push(...hex2lch(token));
171
+ break;
172
+ case 'rgb':
173
+ case 'rgba':
174
+ values.push(...rgb2lch(token));
175
+ break;
176
+ case 'hsl':
177
+ case 'hsla':
178
+ values.push(...hsl2lch(token));
179
+ break;
180
+ case 'hwb':
181
+ values.push(...hwb2lch(token));
182
+ break;
183
+ case 'lab':
184
+ values.push(...lab2lch(token));
185
+ break;
186
+ case 'oklab':
187
+ values.push(...oklab2lch(token));
188
+ break;
189
+ case 'oklch':
190
+ values.push(...oklch2lch(token));
191
+ break;
192
+ case 'color':
193
+ // @ts-ignore
194
+ values.push(...srgb2lch(...color2srgbvalues(token)));
195
+ break;
196
+ }
197
+ if (values.length > 0) {
198
+ return values2colortoken(values, to);
199
+ }
200
+ }
201
+ else if (to == 'oklab') {
202
+ switch (token.kin) {
203
+ case 'hex':
204
+ case 'lit':
205
+ values.push(...hex2oklab(token));
206
+ break;
207
+ case 'rgb':
208
+ case 'rgba':
209
+ values.push(...rgb2oklab(token));
210
+ break;
211
+ case 'hsl':
212
+ case 'hsla':
213
+ values.push(...hsl2oklab(token));
214
+ break;
215
+ case 'hwb':
216
+ values.push(...hwb2oklab(token));
217
+ break;
218
+ case 'lab':
219
+ values.push(...lab2oklab(token));
220
+ break;
221
+ case 'lch':
222
+ values.push(...lch2oklab(token));
223
+ break;
224
+ case 'oklch':
225
+ values.push(...oklch2oklab(token));
226
+ break;
227
+ case 'color':
228
+ // @ts-ignore
229
+ values.push(...srgb2oklab(...color2srgbvalues(token)));
230
+ break;
231
+ }
232
+ if (values.length > 0) {
233
+ return values2colortoken(values, to);
234
+ }
235
+ }
236
+ else if (to == 'oklch') {
237
+ switch (token.kin) {
238
+ case 'hex':
239
+ case 'lit':
240
+ values.push(...hex2oklch(token));
241
+ break;
242
+ case 'rgb':
243
+ case 'rgba':
244
+ values.push(...rgb2oklch(token));
245
+ break;
246
+ case 'hsl':
247
+ case 'hsla':
248
+ values.push(...hsl2oklch(token));
249
+ break;
250
+ case 'hwb':
251
+ values.push(...hwb2oklch(token));
252
+ break;
253
+ case 'lab':
254
+ values.push(...lab2oklch(token));
255
+ break;
256
+ case 'oklab':
257
+ values.push(...oklab2oklch(token));
258
+ break;
259
+ case 'lch':
260
+ values.push(...lch2oklch(token));
261
+ break;
262
+ case 'color':
263
+ // @ts-ignore
264
+ let val = color2srgbvalues(token);
265
+ switch (to) {
266
+ case 'srgb':
267
+ values.push(...val);
268
+ break;
269
+ case 'srgb-linear':
270
+ // @ts-ignore
271
+ values.push(...srgb2lsrgbvalues(...val));
272
+ break;
273
+ case 'display-p3':
274
+ // @ts-ignore
275
+ values.push(...srgb2p3values(...val));
276
+ break;
277
+ case 'prophoto-rgb':
278
+ // @ts-ignore
279
+ values.push(...srgb2prophotorgbvalues(...val));
280
+ break;
281
+ case 'a98-rgb':
282
+ // @ts-ignore
283
+ values.push(...srgb2a98values(...val));
284
+ break;
285
+ case 'rec2020':
286
+ // @ts-ignore
287
+ values.push(...srgb2rec2020values(...val));
288
+ break;
289
+ case 'xyz':
290
+ case 'xyz-d65':
291
+ // @ts-ignore
292
+ values.push(...srgb2xyz(...val));
293
+ break;
294
+ case 'xyz-d50':
295
+ // @ts-ignore
296
+ values.push(...(XYZ_D65_to_D50(...srgb2xyz(...val))));
297
+ break;
298
+ }
299
+ break;
300
+ }
301
+ if (values.length > 0) {
302
+ return values2colortoken(values, to);
303
+ }
304
+ }
305
+ else if (colorFuncColorSpace.includes(to)) {
306
+ switch (token.kin) {
307
+ case 'hex':
308
+ case 'lit':
309
+ values.push(...hex2srgb(token));
310
+ break;
311
+ case 'rgb':
312
+ case 'rgba':
313
+ values.push(...rgb2srgb(token));
314
+ break;
315
+ case 'hsl':
316
+ case 'hsla':
317
+ values.push(...hsl2srgb(token));
318
+ break;
319
+ case 'hwb':
320
+ values.push(...hwb2srgb(token));
321
+ break;
322
+ case 'lab':
323
+ values.push(...lab2srgb(token));
324
+ break;
325
+ case 'oklab':
326
+ values.push(...oklab2srgb(token));
327
+ break;
328
+ case 'lch':
329
+ values.push(...lch2srgb(token));
330
+ break;
331
+ case 'color':
332
+ // @ts-ignore
333
+ values.push(...srgb2oklch(...color2srgbvalues(token)));
334
+ break;
335
+ }
336
+ if (values.length > 0) {
337
+ return values2colortoken(values, to);
338
+ }
339
+ }
340
+ return null;
341
+ }
342
+ function minmax(value, min, max) {
343
+ if (value < min) {
344
+ return min;
345
+ }
346
+ if (value > max) {
347
+ return max;
348
+ }
349
+ return value;
350
+ }
351
+ function color2srgbvalues(token) {
352
+ const components = getComponents(token);
353
+ const colorSpace = components.shift();
354
+ let values = components.map((val) => getNumber(val));
355
+ switch (colorSpace.val) {
356
+ case 'display-p3':
357
+ // @ts-ignore
358
+ values = p32srgbvalues(...values);
359
+ break;
360
+ case 'srgb-linear':
361
+ // @ts-ignore
362
+ values = lsrgb2srgbvalues(...values);
363
+ break;
364
+ case 'prophoto-rgb':
365
+ // @ts-ignore
366
+ values = prophotorgb2srgbvalues(...values);
367
+ break;
368
+ case 'a98-rgb':
369
+ // @ts-ignore
370
+ values = a98rgb2srgbvalues(...values);
371
+ break;
372
+ case 'rec2020':
373
+ // @ts-ignore
374
+ values = rec20202srgb(...values);
375
+ break;
376
+ case 'xyz':
377
+ case 'xyz-d65':
378
+ // @ts-ignore
379
+ values = xyz2srgb(...values);
380
+ break;
381
+ case 'xyz-d50':
382
+ // @ts-ignore
383
+ values = xyzd502srgb(...values);
384
+ break;
385
+ // case srgb:
386
+ }
387
+ return values;
388
+ }
389
+ function values2hsltoken(values) {
390
+ const to = 'hsl';
391
+ const chi = [
392
+ { typ: EnumToken.AngleTokenType, val: String(values[0] * 360), unit: 'deg' },
393
+ { typ: EnumToken.PercentageTokenType, val: String(values[1] * 100) },
394
+ { typ: EnumToken.PercentageTokenType, val: String(values[2] * 100) },
395
+ ];
396
+ if (values.length == 4) {
397
+ chi.push({ typ: EnumToken.PercentageTokenType, val: String(values[3] * 100) });
398
+ }
399
+ return {
400
+ typ: EnumToken.ColorTokenType,
401
+ val: to,
402
+ chi,
403
+ kin: to
404
+ };
405
+ }
406
+ function values2rgbtoken(values) {
407
+ const to = 'rgb';
408
+ const chi = [
409
+ { typ: EnumToken.NumberTokenType, val: String(values[0]) },
410
+ { typ: EnumToken.NumberTokenType, val: String(values[1]) },
411
+ { typ: EnumToken.NumberTokenType, val: String(values[2]) },
412
+ ];
413
+ if (values.length == 4) {
414
+ chi.push({ typ: EnumToken.PercentageTokenType, val: String(values[3] * 100) });
415
+ }
416
+ return {
417
+ typ: EnumToken.ColorTokenType,
418
+ val: to,
419
+ chi,
420
+ kin: to
421
+ };
422
+ }
423
+ function values2hwbtoken(values) {
424
+ const to = 'hwb';
425
+ const chi = [
426
+ { typ: EnumToken.AngleTokenType, val: String(values[0] * 360), unit: 'deg' },
427
+ { typ: EnumToken.PercentageTokenType, val: String(values[1] * 100) },
428
+ { typ: EnumToken.PercentageTokenType, val: String(values[2] * 100) },
429
+ ];
430
+ if (values.length == 4) {
431
+ chi.push({ typ: EnumToken.PercentageTokenType, val: String(values[3] * 100) });
432
+ }
433
+ return {
434
+ typ: EnumToken.ColorTokenType,
435
+ val: to,
436
+ chi,
437
+ kin: to
438
+ };
439
+ }
440
+ function values2colortoken(values, to) {
441
+ const chi = [
442
+ { typ: EnumToken.NumberTokenType, val: String(values[0]) },
443
+ { typ: EnumToken.NumberTokenType, val: String(values[1]) },
444
+ { typ: EnumToken.NumberTokenType, val: String(values[2]) },
445
+ ];
446
+ if (values.length == 4) {
447
+ chi.push({ typ: EnumToken.PercentageTokenType, val: String(values[3] * 100) });
448
+ }
449
+ return colorFuncColorSpace.includes(to) ? {
450
+ typ: EnumToken.ColorTokenType,
451
+ val: 'color',
452
+ chi: [{ typ: EnumToken.IdenTokenType, val: to }].concat(chi),
453
+ kin: 'color'
454
+ } : {
455
+ typ: EnumToken.ColorTokenType,
456
+ val: to,
457
+ chi,
458
+ kin: to
459
+ };
460
+ }
461
+ /**
462
+ * clamp color values
463
+ * @param token
464
+ */
465
+ function clamp(token) {
466
+ if (token.kin == 'rgb' || token.kin == 'rgba') {
467
+ token.chi.filter((token) => ![EnumToken.LiteralTokenType, EnumToken.CommaTokenType, EnumToken.WhitespaceTokenType].includes(token.typ)).forEach((token, index) => {
468
+ if (index <= 2) {
469
+ if (token.typ == EnumToken.NumberTokenType) {
470
+ token.val = String(minmax(+token.val, 0, 255));
471
+ }
472
+ else if (token.typ == EnumToken.PercentageTokenType) {
473
+ token.val = String(minmax(+token.val, 0, 100));
474
+ }
475
+ }
476
+ else {
477
+ if (token.typ == EnumToken.NumberTokenType) {
478
+ token.val = String(minmax(+token.val, 0, 1));
479
+ }
480
+ else if (token.typ == EnumToken.PercentageTokenType) {
481
+ token.val = String(minmax(+token.val, 0, 100));
482
+ }
483
+ }
484
+ });
485
+ }
486
+ return token;
487
+ }
488
+ function getNumber(token) {
489
+ if (token.typ == EnumToken.IdenTokenType && token.val == 'none') {
490
+ return 0;
491
+ }
492
+ // @ts-ignore
493
+ return token.typ == EnumToken.PercentageTokenType ? token.val / 100 : +token.val;
494
+ }
495
+ function getAngle(token) {
496
+ if (token.typ == EnumToken.IdenTokenType) {
497
+ if (token.val == 'none') {
498
+ return 0;
499
+ }
500
+ }
501
+ if (token.typ == EnumToken.AngleTokenType) {
502
+ switch (token.unit) {
503
+ case 'deg':
504
+ // @ts-ignore
505
+ return token.val / 360;
506
+ case 'rad':
507
+ // @ts-ignore
508
+ return token.val / (2 * Math.PI);
509
+ case 'grad':
510
+ // @ts-ignore
511
+ return token.val / 400;
512
+ case 'turn':
513
+ // @ts-ignore
514
+ return +token.val;
515
+ }
516
+ }
517
+ // @ts-ignore
518
+ return token.val / 360;
519
+ }
520
+
521
+ export { clamp, color2srgbvalues, convert, getAngle, getNumber, minmax, values2hsltoken };