@tbela99/css-parser 1.1.1 → 1.3.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 +11 -0
- package/README.md +53 -6
- package/dist/index-umd-web.js +5503 -5037
- package/dist/index.cjs +5510 -5044
- package/dist/index.d.ts +148 -41
- package/dist/lib/ast/expand.js +81 -65
- package/dist/lib/ast/features/calc.js +14 -36
- package/dist/lib/ast/features/inlinecssvariables.js +6 -17
- package/dist/lib/ast/features/prefix.js +22 -19
- package/dist/lib/ast/features/shorthand.js +1 -1
- package/dist/lib/ast/features/transform.js +17 -2
- package/dist/lib/ast/math/expression.js +74 -172
- package/dist/lib/ast/math/math.js +24 -22
- package/dist/lib/ast/minify.js +249 -199
- package/dist/lib/ast/transform/compute.js +18 -41
- package/dist/lib/ast/transform/matrix.js +36 -36
- package/dist/lib/ast/transform/minify.js +37 -59
- package/dist/lib/ast/transform/perspective.js +1 -1
- package/dist/lib/ast/transform/rotate.js +13 -13
- package/dist/lib/ast/transform/scale.js +8 -8
- package/dist/lib/ast/transform/skew.js +4 -4
- package/dist/lib/ast/transform/translate.js +8 -8
- package/dist/lib/ast/transform/utils.js +80 -28
- package/dist/lib/ast/types.js +122 -2
- package/dist/lib/fs/resolve.js +1 -14
- package/dist/lib/parser/declaration/list.js +1 -1
- package/dist/lib/parser/declaration/map.js +1 -1
- package/dist/lib/parser/declaration/set.js +3 -3
- package/dist/lib/parser/parse.js +53 -107
- package/dist/lib/parser/tokenize.js +29 -53
- package/dist/lib/parser/utils/declaration.js +1 -1
- package/dist/lib/parser/utils/type.js +2 -2
- package/dist/lib/renderer/render.js +79 -194
- package/dist/lib/{renderer → syntax}/color/a98rgb.js +2 -2
- package/dist/lib/syntax/color/cmyk.js +104 -0
- package/dist/lib/{renderer → syntax}/color/color-mix.js +31 -33
- package/dist/lib/syntax/color/color.js +581 -0
- package/dist/lib/syntax/color/hex.js +179 -0
- package/dist/lib/syntax/color/hsl.js +201 -0
- package/dist/lib/syntax/color/hwb.js +204 -0
- package/dist/lib/syntax/color/lab.js +262 -0
- package/dist/lib/syntax/color/lch.js +194 -0
- package/dist/lib/syntax/color/oklab.js +237 -0
- package/dist/lib/syntax/color/oklch.js +166 -0
- package/dist/lib/{renderer → syntax}/color/p3.js +3 -3
- package/dist/lib/{renderer → syntax}/color/rec2020.js +11 -11
- package/dist/lib/{renderer → syntax}/color/relativecolor.js +54 -41
- package/dist/lib/syntax/color/rgb.js +140 -0
- package/dist/lib/{renderer → syntax}/color/srgb.js +58 -46
- package/dist/lib/syntax/color/utils/components.js +46 -0
- package/dist/lib/{renderer → syntax}/color/utils/constants.js +6 -33
- package/dist/lib/syntax/color/utils/distance.js +40 -0
- package/dist/lib/{renderer → syntax}/color/xyz.js +27 -14
- package/dist/lib/{renderer → syntax}/color/xyzd50.js +8 -8
- package/dist/lib/syntax/syntax.js +78 -77
- package/dist/lib/syntax/utils.js +70 -0
- package/dist/lib/validation/at-rules/container.js +1 -1
- package/dist/lib/validation/at-rules/counter-style.js +1 -1
- package/dist/lib/validation/at-rules/custom-media.js +1 -1
- package/dist/lib/validation/at-rules/document.js +2 -2
- package/dist/lib/validation/at-rules/font-feature-values.js +2 -2
- package/dist/lib/validation/at-rules/import.js +5 -5
- package/dist/lib/validation/at-rules/keyframes.js +3 -13
- package/dist/lib/validation/at-rules/layer.js +1 -1
- package/dist/lib/validation/at-rules/media.js +1 -1
- package/dist/lib/validation/at-rules/namespace.js +1 -1
- package/dist/lib/validation/at-rules/page-margin-box.js +1 -1
- package/dist/lib/validation/at-rules/page.js +1 -1
- package/dist/lib/validation/at-rules/supports.js +7 -7
- package/dist/lib/validation/at-rules/when.js +1 -1
- package/dist/lib/validation/atrule.js +2 -2
- package/dist/lib/validation/config.js +0 -3
- package/dist/lib/validation/config.json.js +1 -1
- package/dist/lib/validation/parser/parse.js +8 -11
- package/dist/lib/validation/selector.js +1 -9
- package/dist/lib/validation/syntax.js +67 -137
- package/dist/lib/validation/syntaxes/complex-selector-list.js +2 -19
- package/dist/lib/validation/syntaxes/complex-selector.js +1 -1
- package/dist/lib/validation/syntaxes/compound-selector.js +5 -24
- package/dist/lib/validation/syntaxes/family-name.js +5 -40
- package/dist/lib/validation/syntaxes/keyframe-selector.js +3 -22
- package/dist/lib/validation/syntaxes/layer-name.js +1 -1
- package/dist/lib/validation/syntaxes/relative-selector-list.js +1 -25
- package/dist/lib/validation/syntaxes/relative-selector.js +1 -1
- package/dist/lib/validation/syntaxes/url.js +3 -34
- package/dist/lib/validation/utils/list.js +2 -9
- package/dist/lib/validation/utils/whitespace.js +1 -1
- package/dist/node/index.js +4 -2
- package/dist/web/index.js +4 -2
- package/package.json +4 -4
- package/.editorconfig +0 -484
- package/dist/lib/ast/transform/convert.js +0 -33
- package/dist/lib/ast/utils/utils.js +0 -104
- package/dist/lib/renderer/color/color.js +0 -654
- package/dist/lib/renderer/color/hex.js +0 -105
- package/dist/lib/renderer/color/hsl.js +0 -125
- package/dist/lib/renderer/color/hwb.js +0 -103
- package/dist/lib/renderer/color/lab.js +0 -148
- package/dist/lib/renderer/color/lch.js +0 -90
- package/dist/lib/renderer/color/oklab.js +0 -131
- package/dist/lib/renderer/color/oklch.js +0 -75
- package/dist/lib/renderer/color/rgb.js +0 -50
- package/dist/lib/renderer/color/utils/components.js +0 -34
- package/dist/lib/validation/syntaxes/keyframe-block-list.js +0 -28
- package/dist/lib/{renderer → syntax}/color/hsv.js +0 -0
- package/dist/lib/{renderer → syntax}/color/prophotorgb.js +1 -1
- /package/dist/lib/{renderer → syntax}/color/utils/matrix.js +0 -0
|
@@ -1,33 +1,13 @@
|
|
|
1
|
-
import { getAngle,
|
|
2
|
-
import { colorsFunc,
|
|
3
|
-
import {
|
|
4
|
-
import { reduceHexValue, srgb2hexvalues, rgb2hex, hsl2hex, hwb2hex, cmyk2hex, oklab2hex, oklch2hex, lab2hex, lch2hex } from './color/hex.js';
|
|
5
|
-
import { EnumToken } from '../ast/types.js';
|
|
1
|
+
import { getAngle, convertColor } from '../syntax/color/color.js';
|
|
2
|
+
import { colorsFunc, funcLike } from '../syntax/color/utils/constants.js';
|
|
3
|
+
import { EnumToken, ColorType } from '../ast/types.js';
|
|
6
4
|
import '../ast/minify.js';
|
|
7
5
|
import '../ast/walk.js';
|
|
8
6
|
import { expand } from '../ast/expand.js';
|
|
9
|
-
import { colorMix } from './color/color-mix.js';
|
|
10
|
-
import { parseRelativeColor } from './color/relativecolor.js';
|
|
11
|
-
import { SourceMap } from './sourcemap/sourcemap.js';
|
|
12
7
|
import { isColor, pseudoElements, mathFuncs, isNewLine } from '../syntax/syntax.js';
|
|
8
|
+
import { minifyNumber } from '../syntax/utils.js';
|
|
9
|
+
import { SourceMap } from './sourcemap/sourcemap.js';
|
|
13
10
|
|
|
14
|
-
function reduceNumber(val) {
|
|
15
|
-
val = String(+val);
|
|
16
|
-
if (val === '0') {
|
|
17
|
-
return '0';
|
|
18
|
-
}
|
|
19
|
-
const chr = val.charAt(0);
|
|
20
|
-
if (chr == '-') {
|
|
21
|
-
const slice = val.slice(0, 2);
|
|
22
|
-
if (slice == '-0') {
|
|
23
|
-
return val.length == 2 ? '0' : '-' + val.slice(2);
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
if (chr == '0') {
|
|
27
|
-
return val.slice(1);
|
|
28
|
-
}
|
|
29
|
-
return val;
|
|
30
|
-
}
|
|
31
11
|
function update(position, str) {
|
|
32
12
|
let i = 0;
|
|
33
13
|
for (; i < str.length; i++) {
|
|
@@ -236,10 +216,8 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
|
|
|
236
216
|
case EnumToken.InvalidDeclarationNodeType:
|
|
237
217
|
case EnumToken.InvalidRuleTokenType:
|
|
238
218
|
case EnumToken.InvalidAtRuleTokenType:
|
|
239
|
-
return '';
|
|
240
219
|
default:
|
|
241
|
-
|
|
242
|
-
throw new Error(`render: unexpected token ${JSON.stringify(data, null, 1)}`);
|
|
220
|
+
return '';
|
|
243
221
|
}
|
|
244
222
|
}
|
|
245
223
|
/**
|
|
@@ -315,7 +293,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
315
293
|
case EnumToken.FractionTokenType:
|
|
316
294
|
const fraction = renderToken(token.l) + '/' + renderToken(token.r);
|
|
317
295
|
if (+token.r.val != 0) {
|
|
318
|
-
const value =
|
|
296
|
+
const value = minifyNumber(+token.l.val / +token.r.val);
|
|
319
297
|
if (value.length <= fraction.length) {
|
|
320
298
|
return value;
|
|
321
299
|
}
|
|
@@ -331,143 +309,41 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
331
309
|
case EnumToken.Div:
|
|
332
310
|
return '/';
|
|
333
311
|
case EnumToken.ColorTokenType:
|
|
334
|
-
if (token.kin ==
|
|
312
|
+
if (token.kin == ColorType.LIGHT_DARK || ('chi' in token && options.convertColor === false)) {
|
|
335
313
|
return token.val + '(' + token.chi.reduce((acc, curr) => acc + renderToken(curr, options, cache), '') + ')';
|
|
336
314
|
}
|
|
337
|
-
if (options.convertColor) {
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
}
|
|
343
|
-
else {
|
|
344
|
-
if (![EnumToken.WhitespaceTokenType, EnumToken.CommentTokenType, EnumToken.CommaTokenType].includes(t.typ)) {
|
|
345
|
-
acc[acc.length - 1].push(t);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
return acc;
|
|
349
|
-
}, [[]]);
|
|
350
|
-
const value = colorMix(children[0][1], children[0][2], children[1][0], children[1][1], children[2][0], children[2][1]);
|
|
351
|
-
if (value != null) {
|
|
352
|
-
token = value;
|
|
353
|
-
}
|
|
354
|
-
else if (!token.chi.some(t => t.typ == EnumToken.CommaTokenType)) {
|
|
355
|
-
token.chi = children.reduce((acc, curr, index) => {
|
|
356
|
-
if (acc.length > 0) {
|
|
357
|
-
acc.push({ typ: EnumToken.CommaTokenType });
|
|
358
|
-
}
|
|
359
|
-
acc.push(...curr);
|
|
360
|
-
return acc;
|
|
361
|
-
}, []);
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
if (token.cal == 'rel' && ['rgb', 'hsl', 'hwb', 'lab', 'lch', 'oklab', 'oklch', 'color'].includes(token.val)) {
|
|
365
|
-
const chi = getComponents(token);
|
|
366
|
-
const offset = token.val == 'color' ? 2 : 1;
|
|
367
|
-
if (chi != null) {
|
|
368
|
-
// @ts-ignore
|
|
369
|
-
const color = chi[1];
|
|
370
|
-
const components = parseRelativeColor(token.val == 'color' ? chi[offset].val : token.val, color, chi[offset + 1], chi[offset + 2], chi[offset + 3], chi[offset + 4]);
|
|
371
|
-
if (components != null) {
|
|
372
|
-
token.chi = [...(token.val == 'color' ? [chi[offset]] : []), ...Object.values(components)];
|
|
373
|
-
delete token.cal;
|
|
374
|
-
}
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
if (token.val == 'color') {
|
|
378
|
-
if (token.chi[0].typ == EnumToken.IdenTokenType && colorFuncColorSpace.includes(token.chi[0].val.toLowerCase())) {
|
|
379
|
-
const values = color2srgbvalues(token);
|
|
380
|
-
if (Array.isArray(values) && values.every(t => !Number.isNaN(t))) {
|
|
381
|
-
// @ts-ignore
|
|
382
|
-
return reduceHexValue(srgb2hexvalues(...values));
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
if (token.cal != null) {
|
|
387
|
-
let slice = false;
|
|
388
|
-
if (token.cal == 'rel') {
|
|
389
|
-
const last = token.chi.at(-1);
|
|
390
|
-
if ((last.typ == EnumToken.NumberTokenType && last.val == '1') || (last.typ == EnumToken.IdenTokenType && last.val == 'none')) {
|
|
391
|
-
const prev = token.chi.at(-2);
|
|
392
|
-
if (prev.typ == EnumToken.LiteralTokenType && prev.val == '/') {
|
|
393
|
-
slice = true;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
return clamp(token).val + '(' + (slice ? token.chi.slice(0, -2) : token.chi).reduce((acc, curr) => {
|
|
398
|
-
const val = renderToken(curr, options, cache);
|
|
399
|
-
if (curr.typ == EnumToken.LiteralTokenType && curr.val == '/') {
|
|
400
|
-
return acc.trimEnd() + '/';
|
|
401
|
-
}
|
|
402
|
-
if (curr.typ == EnumToken.CommaTokenType) {
|
|
403
|
-
return acc.trimEnd() + ',';
|
|
404
|
-
}
|
|
405
|
-
if (curr.typ == EnumToken.WhitespaceTokenType) {
|
|
406
|
-
const v = acc.at(-1);
|
|
407
|
-
if (v == ' ' || v == ',' || v == '/') {
|
|
408
|
-
return acc.trimEnd();
|
|
409
|
-
}
|
|
410
|
-
return acc.trimEnd() + ' ';
|
|
411
|
-
}
|
|
412
|
-
if (acc.length > 0) {
|
|
413
|
-
return acc + (['/', ',', ' '].includes(acc.at(-1)) ? '' : ' ') + val;
|
|
414
|
-
}
|
|
415
|
-
return val;
|
|
416
|
-
}, '') + ')';
|
|
417
|
-
}
|
|
418
|
-
if (token.kin == ColorKind.LIT && token.val.localeCompare('currentcolor', undefined, { sensitivity: 'base' }) == 0) {
|
|
419
|
-
return 'currentcolor';
|
|
420
|
-
}
|
|
421
|
-
clamp(token);
|
|
422
|
-
if (Array.isArray(token.chi) && token.chi.some((t) => t.typ == EnumToken.FunctionTokenType || (t.typ == EnumToken.ColorTokenType && Array.isArray(t.chi)))) {
|
|
423
|
-
const replaceSemiColon = /^((rgba?)|(hsla?)|(hwb)|((ok)?lab)|((ok)?lch))$/i.test(token.val);
|
|
424
|
-
return (token.val.endsWith('a') ? token.val.slice(0, -1) : token.val) + '(' + token.chi.reduce((acc, curr, index, array) => {
|
|
425
|
-
if (curr.typ == EnumToken.Literal && curr.val == '/') {
|
|
426
|
-
return acc.trimEnd() + '/';
|
|
427
|
-
}
|
|
428
|
-
if (curr.typ == EnumToken.CommaTokenType) {
|
|
429
|
-
return acc.trimEnd() + (replaceSemiColon ? ' ' : ',');
|
|
430
|
-
}
|
|
431
|
-
if (curr.typ == EnumToken.WhitespaceTokenType) {
|
|
432
|
-
return /[,\/\s]/.test(acc.at(-1)) ? acc.trimEnd() : acc.trimEnd() + ' ';
|
|
433
|
-
}
|
|
434
|
-
return acc + renderToken(curr, options, cache);
|
|
435
|
-
}, '') + ')';
|
|
436
|
-
}
|
|
437
|
-
let value = token.kin == ColorKind.HEX ? token.val.toLowerCase() : (token.kin == ColorKind.LIT ? COLORS_NAMES[token.val.toLowerCase()] : '');
|
|
438
|
-
if (token.val == 'rgb' || token.val == 'rgba') {
|
|
439
|
-
value = rgb2hex(token);
|
|
440
|
-
}
|
|
441
|
-
else if (token.val == 'hsl' || token.val == 'hsla') {
|
|
442
|
-
value = hsl2hex(token);
|
|
443
|
-
}
|
|
444
|
-
else if (token.val == 'hwb') {
|
|
445
|
-
value = hwb2hex(token);
|
|
446
|
-
}
|
|
447
|
-
else if (token.val == 'device-cmyk') {
|
|
448
|
-
value = cmyk2hex(token);
|
|
449
|
-
}
|
|
450
|
-
else if (token.val == 'oklab') {
|
|
451
|
-
value = oklab2hex(token);
|
|
452
|
-
}
|
|
453
|
-
else if (token.val == 'oklch') {
|
|
454
|
-
value = oklch2hex(token);
|
|
455
|
-
}
|
|
456
|
-
else if (token.val == 'lab') {
|
|
457
|
-
value = lab2hex(token);
|
|
458
|
-
}
|
|
459
|
-
else if (token.val == 'lch') {
|
|
460
|
-
value = lch2hex(token);
|
|
461
|
-
}
|
|
462
|
-
if (value !== '' && value != null) {
|
|
463
|
-
return reduceHexValue(value);
|
|
315
|
+
if (options.convertColor !== false) {
|
|
316
|
+
const value = convertColor(token, typeof options.convertColor == 'boolean' ? ColorType.HEX : ColorType[ColorType[options.convertColor ?? 'HEX']?.toUpperCase?.().replaceAll?.('-', '_')] ?? ColorType.HEX);
|
|
317
|
+
//
|
|
318
|
+
if (value != null) {
|
|
319
|
+
token = value;
|
|
464
320
|
}
|
|
465
321
|
}
|
|
466
|
-
if ([
|
|
322
|
+
if ([ColorType.HEX, ColorType.LIT, ColorType.SYS, ColorType.DPSYS].includes(token.kin)) {
|
|
467
323
|
return token.val;
|
|
468
324
|
}
|
|
469
325
|
if (Array.isArray(token.chi)) {
|
|
470
|
-
|
|
326
|
+
const isLegacy = ['rgb', 'rgba', 'hsl', 'hsla'].includes(token.val.toLowerCase());
|
|
327
|
+
const useAlpha = (['rgb', 'rgba', 'hsl', 'hsla', 'hwb', 'oklab', 'oklch', 'lab', 'lch'].includes(token.val.toLowerCase()) && token.chi.length == 4) ||
|
|
328
|
+
('color' == token.val.toLowerCase() && token.chi.length == 5);
|
|
329
|
+
return (token.val.endsWith('a') ? token.val.slice(0, -1) : token.val) + '(' + token.chi.reduce((acc, curr, index, array) => {
|
|
330
|
+
if (/[,/]\s*$/.test(acc)) {
|
|
331
|
+
if (curr.typ == EnumToken.WhitespaceTokenType) {
|
|
332
|
+
return acc.trimEnd();
|
|
333
|
+
}
|
|
334
|
+
return acc.trimStart() + renderToken(curr, options, cache);
|
|
335
|
+
}
|
|
336
|
+
if (isLegacy && curr.typ == EnumToken.CommaTokenType) {
|
|
337
|
+
return acc.trimEnd() + ' ';
|
|
338
|
+
}
|
|
339
|
+
if (curr.typ == EnumToken.WhitespaceTokenType) {
|
|
340
|
+
return acc.trimEnd() + ' ';
|
|
341
|
+
}
|
|
342
|
+
if (curr.typ == EnumToken.CommaTokenType || (curr.typ == EnumToken.LiteralTokenType && curr.val == '/')) {
|
|
343
|
+
return acc.trimEnd() + (curr.typ == EnumToken.CommaTokenType ? ',' : '/');
|
|
344
|
+
}
|
|
345
|
+
return acc.trimEnd() + (useAlpha && index == array.length - 1 ? '/' : ' ') + renderToken(curr, options, cache);
|
|
346
|
+
}, '').trimStart() + ')';
|
|
471
347
|
}
|
|
472
348
|
case EnumToken.ParensTokenType:
|
|
473
349
|
case EnumToken.FunctionTokenType:
|
|
@@ -554,7 +430,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
554
430
|
case EnumToken.DimensionTokenType:
|
|
555
431
|
case EnumToken.FrequencyTokenType:
|
|
556
432
|
case EnumToken.ResolutionTokenType:
|
|
557
|
-
let val = token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) :
|
|
433
|
+
let val = token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : minifyNumber(token.val);
|
|
558
434
|
let unit = token.unit;
|
|
559
435
|
if (token.typ == EnumToken.AngleTokenType && !val.includes('/')) {
|
|
560
436
|
const angle = getAngle(token);
|
|
@@ -566,7 +442,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
566
442
|
}
|
|
567
443
|
switch (u) {
|
|
568
444
|
case 'turn':
|
|
569
|
-
v =
|
|
445
|
+
v = minifyNumber(angle);
|
|
570
446
|
if (v.length + 4 < value.length) {
|
|
571
447
|
val = v;
|
|
572
448
|
unit = u;
|
|
@@ -574,7 +450,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
574
450
|
}
|
|
575
451
|
break;
|
|
576
452
|
case 'deg':
|
|
577
|
-
v =
|
|
453
|
+
v = minifyNumber(angle * 360);
|
|
578
454
|
if (v.length + 3 < value.length) {
|
|
579
455
|
val = v;
|
|
580
456
|
unit = u;
|
|
@@ -582,7 +458,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
582
458
|
}
|
|
583
459
|
break;
|
|
584
460
|
case 'rad':
|
|
585
|
-
v =
|
|
461
|
+
v = minifyNumber(angle * (2 * Math.PI));
|
|
586
462
|
if (v.length + 3 < value.length) {
|
|
587
463
|
val = v;
|
|
588
464
|
unit = u;
|
|
@@ -590,7 +466,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
590
466
|
}
|
|
591
467
|
break;
|
|
592
468
|
case 'grad':
|
|
593
|
-
v =
|
|
469
|
+
v = minifyNumber(angle * 400);
|
|
594
470
|
if (v.length + 4 < value.length) {
|
|
595
471
|
val = v;
|
|
596
472
|
unit = u;
|
|
@@ -616,7 +492,7 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
616
492
|
if (token.typ == EnumToken.TimeTokenType) {
|
|
617
493
|
if (unit == 'ms') {
|
|
618
494
|
// @ts-ignore
|
|
619
|
-
const v =
|
|
495
|
+
const v = minifyNumber(val / 1000);
|
|
620
496
|
if (v.length + 1 <= val.length) {
|
|
621
497
|
return v + 's';
|
|
622
498
|
}
|
|
@@ -631,10 +507,10 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
631
507
|
case EnumToken.FlexTokenType:
|
|
632
508
|
case EnumToken.PercentageTokenType:
|
|
633
509
|
const uni = token.typ == EnumToken.PercentageTokenType ? '%' : 'fr';
|
|
634
|
-
const perc = token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) :
|
|
510
|
+
const perc = token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : minifyNumber(token.val);
|
|
635
511
|
return options.minify && perc == '0' ? '0' : (perc.includes('/') ? perc.replace('/', uni + '/') : perc + uni);
|
|
636
512
|
case EnumToken.NumberTokenType:
|
|
637
|
-
return token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) :
|
|
513
|
+
return token.val.typ == EnumToken.FractionTokenType ? renderToken(token.val, options, cache) : minifyNumber(token.val);
|
|
638
514
|
case EnumToken.CommentTokenType:
|
|
639
515
|
if (options.removeComments && (!options.preserveLicense || !token.val.startsWith('/*!'))) {
|
|
640
516
|
return '';
|
|
@@ -646,32 +522,41 @@ function renderToken(token, options = {}, cache = Object.create(null), reducer,
|
|
|
646
522
|
return token.val.slice(1);
|
|
647
523
|
}
|
|
648
524
|
case EnumToken.UrlTokenTokenType:
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
525
|
+
// if (token.typ == EnumToken.UrlTokenTokenType) {
|
|
526
|
+
//
|
|
527
|
+
// if (options.output != null) {
|
|
528
|
+
//
|
|
529
|
+
// if (!('original' in token)) {
|
|
530
|
+
//
|
|
531
|
+
// // do not modify original token
|
|
532
|
+
// token = {...token};
|
|
533
|
+
// Object.defineProperty(token, 'original', {
|
|
534
|
+
// enumerable: false,
|
|
535
|
+
// writable: false,
|
|
536
|
+
// value: (token as UrlToken).val
|
|
537
|
+
// })
|
|
538
|
+
// }
|
|
539
|
+
//
|
|
540
|
+
// // @ts-ignore
|
|
541
|
+
// if (!(token.original in cache)) {
|
|
542
|
+
//
|
|
543
|
+
// let output: string = <string>options.output ?? '';
|
|
544
|
+
// const key = output + 'abs';
|
|
545
|
+
//
|
|
546
|
+
// if (!(key in cache)) {
|
|
547
|
+
//
|
|
548
|
+
// // @ts-ignore
|
|
549
|
+
// cache[key] = options.dirname(options.resolve(output, options.cwd).absolute);
|
|
550
|
+
// }
|
|
551
|
+
//
|
|
552
|
+
// // @ts-ignore
|
|
553
|
+
// cache[token.original] = options.resolve(token.original, cache[key]).relative;
|
|
554
|
+
// }
|
|
555
|
+
//
|
|
556
|
+
// // @ts-ignore
|
|
557
|
+
// token.val = cache[token.original];
|
|
558
|
+
// }
|
|
559
|
+
// }
|
|
675
560
|
case EnumToken.HashTokenType:
|
|
676
561
|
case EnumToken.IdenTokenType:
|
|
677
562
|
case EnumToken.AtRuleTokenType:
|
|
@@ -718,4 +603,4 @@ function filterValues(values) {
|
|
|
718
603
|
return values;
|
|
719
604
|
}
|
|
720
605
|
|
|
721
|
-
export { doRender, filterValues,
|
|
606
|
+
export { doRender, filterValues, renderToken };
|
|
@@ -8,7 +8,7 @@ import '../../parser/parse.js';
|
|
|
8
8
|
import '../../parser/tokenize.js';
|
|
9
9
|
import '../../parser/utils/config.js';
|
|
10
10
|
import { srgb2xyz } from './xyz.js';
|
|
11
|
-
import '
|
|
11
|
+
import '../../renderer/sourcemap/lib/encode.js';
|
|
12
12
|
|
|
13
13
|
function a98rgb2srgbvalues(r, g, b, a = null) {
|
|
14
14
|
// @ts-ignore
|
|
@@ -16,7 +16,7 @@ function a98rgb2srgbvalues(r, g, b, a = null) {
|
|
|
16
16
|
}
|
|
17
17
|
function srgb2a98values(r, g, b, a = null) {
|
|
18
18
|
// @ts-ignore
|
|
19
|
-
return la98rgb2a98rgb(xyz2la98rgb(...srgb2xyz(r, g, b, a)));
|
|
19
|
+
return la98rgb2a98rgb(...xyz2la98rgb(...srgb2xyz(r, g, b, a)));
|
|
20
20
|
}
|
|
21
21
|
// a98-rgb functions
|
|
22
22
|
function a98rgb2la98(r, g, b, a = null) {
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { ColorType, EnumToken } from '../../ast/types.js';
|
|
2
|
+
import '../../ast/minify.js';
|
|
3
|
+
import '../../ast/walk.js';
|
|
4
|
+
import '../../parser/parse.js';
|
|
5
|
+
import '../../parser/tokenize.js';
|
|
6
|
+
import '../../parser/utils/config.js';
|
|
7
|
+
import { color2srgbvalues } from './color.js';
|
|
8
|
+
import { hsl2srgbvalues } from './rgb.js';
|
|
9
|
+
import './utils/constants.js';
|
|
10
|
+
import { lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, hwb2srgbvalues, rgb2srgbvalues } from './srgb.js';
|
|
11
|
+
import '../../renderer/sourcemap/lib/encode.js';
|
|
12
|
+
|
|
13
|
+
function rgb2cmykToken(token) {
|
|
14
|
+
const components = rgb2srgbvalues(token);
|
|
15
|
+
if (components == null || components.length < 3) {
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
// @ts-ignore
|
|
19
|
+
return cmyktoken(srgb2cmykvalues(...components));
|
|
20
|
+
}
|
|
21
|
+
function hsl2cmykToken(token) {
|
|
22
|
+
const values = hsl2srgbvalues(token);
|
|
23
|
+
if (values == null) {
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
// @ts-ignore
|
|
27
|
+
return cmyktoken(srgb2cmykvalues(...values));
|
|
28
|
+
}
|
|
29
|
+
function hwb2cmykToken(token) {
|
|
30
|
+
const values = hwb2srgbvalues(token);
|
|
31
|
+
if (values == null) {
|
|
32
|
+
return null;
|
|
33
|
+
}
|
|
34
|
+
// @ts-ignore
|
|
35
|
+
return cmyktoken(srgb2cmykvalues(...values));
|
|
36
|
+
}
|
|
37
|
+
function lab2cmykToken(token) {
|
|
38
|
+
const components = lab2srgbvalues(token);
|
|
39
|
+
if (components == null || components.length < 3) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
// @ts-ignore
|
|
43
|
+
return cmyktoken(srgb2cmykvalues(...components));
|
|
44
|
+
}
|
|
45
|
+
function lch2cmykToken(token) {
|
|
46
|
+
const components = lch2srgbvalues(token);
|
|
47
|
+
if (components == null || components.length < 3) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
// @ts-ignore
|
|
51
|
+
return cmyktoken(srgb2cmykvalues(...components));
|
|
52
|
+
}
|
|
53
|
+
function oklab2cmyk(token) {
|
|
54
|
+
const components = oklab2srgbvalues(token);
|
|
55
|
+
if (components == null || components.length < 3) {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
// @ts-ignore
|
|
59
|
+
return cmyktoken(srgb2cmykvalues(...components));
|
|
60
|
+
}
|
|
61
|
+
function oklch2cmykToken(token) {
|
|
62
|
+
const components = oklch2srgbvalues(token);
|
|
63
|
+
if (components == null || components.length < 3) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
// @ts-ignore
|
|
67
|
+
return cmyktoken(srgb2cmykvalues(...components));
|
|
68
|
+
}
|
|
69
|
+
function color2cmykToken(token) {
|
|
70
|
+
const values = color2srgbvalues(token);
|
|
71
|
+
if (values == null) {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
// @ts-ignore
|
|
75
|
+
return cmyktoken(srgb2cmykvalues(...values));
|
|
76
|
+
}
|
|
77
|
+
function srgb2cmykvalues(r, g, b, a = null) {
|
|
78
|
+
const k = 1 - Math.max(r, g, b);
|
|
79
|
+
const c = k == 1 ? 0 : (1 - r - k) / (1 - k);
|
|
80
|
+
const m = k == 1 ? 0 : (1 - g - k) / (1 - k);
|
|
81
|
+
const y = k == 1 ? 0 : (1 - b - k) / (1 - k);
|
|
82
|
+
const result = [c, m, y, k];
|
|
83
|
+
if (a != null && a < 1) {
|
|
84
|
+
result.push(a);
|
|
85
|
+
}
|
|
86
|
+
return result;
|
|
87
|
+
}
|
|
88
|
+
function cmyktoken(values) {
|
|
89
|
+
return {
|
|
90
|
+
typ: EnumToken.ColorTokenType,
|
|
91
|
+
val: 'device-cmyk',
|
|
92
|
+
chi: values.reduce((acc, curr, index) => index < 4 ? [...acc, {
|
|
93
|
+
typ: EnumToken.PercentageTokenType,
|
|
94
|
+
// @ts-ignore
|
|
95
|
+
val: curr * 100
|
|
96
|
+
}] : [...acc, {
|
|
97
|
+
typ: EnumToken.LiteralTokenType,
|
|
98
|
+
val: '/'
|
|
99
|
+
}, { typ: EnumToken.PercentageTokenType, val: curr * 100 }], []),
|
|
100
|
+
kin: ColorType.DEVICE_CMYK
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export { color2cmykToken, hsl2cmykToken, hwb2cmykToken, lab2cmykToken, lch2cmykToken, oklab2cmyk, oklch2cmykToken, rgb2cmykToken, srgb2cmykvalues };
|