@tbela99/css-parser 1.6.6 → 1.6.8
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 +106 -0
- package/README.md +1 -58
- package/architecture.png +0 -0
- package/dist/index-umd-web.js +731 -459
- package/dist/index.cjs +724 -459
- package/dist/index.d.ts +67 -11
- package/dist/package.json.js +10 -0
- package/dist/{data → src/data}/properties.json.js +7 -0
- package/dist/{data → src/data}/validation.json.js +7 -0
- package/dist/{lib → src/lib}/ast/clone.js +7 -0
- package/dist/{lib → src/lib}/ast/expand.js +19 -10
- package/dist/{lib → src/lib}/ast/features/calc.js +7 -0
- package/dist/{lib → src/lib}/ast/features/if.js +7 -0
- package/dist/{lib → src/lib}/ast/features/index.js +7 -0
- package/dist/{lib → src/lib}/ast/features/inlinecssvariables.js +7 -0
- package/dist/{lib → src/lib}/ast/features/prefix.js +8 -3
- package/dist/{lib → src/lib}/ast/features/shorthand.js +7 -0
- package/dist/{lib → src/lib}/ast/features/transform.js +8 -1
- package/dist/{lib → src/lib}/ast/features/type.js +7 -0
- package/dist/{lib → src/lib}/ast/find.js +7 -0
- package/dist/{lib → src/lib}/ast/math/expression.js +7 -0
- package/dist/{lib → src/lib}/ast/math/math.js +7 -0
- package/dist/{lib → src/lib}/ast/minify.js +7 -0
- package/dist/{lib → src/lib}/ast/node.js +7 -0
- package/dist/{lib → src/lib}/ast/transform/compute.js +7 -0
- package/dist/{lib → src/lib}/ast/transform/matrix.js +7 -0
- package/dist/{lib → src/lib}/ast/transform/minify.js +7 -0
- package/dist/{lib → src/lib}/ast/transform/perspective.js +7 -0
- package/dist/{lib → src/lib}/ast/transform/rotate.js +7 -0
- package/dist/{lib → src/lib}/ast/transform/scale.js +7 -0
- package/dist/{lib → src/lib}/ast/transform/skew.js +7 -0
- package/dist/{lib → src/lib}/ast/transform/translate.js +7 -0
- package/dist/{lib → src/lib}/ast/transform/utils.js +7 -0
- package/dist/{lib → src/lib}/ast/types.js +28 -9
- package/dist/{lib → src/lib}/ast/walk.js +16 -0
- package/dist/{lib → src/lib}/fs/resolve.js +7 -0
- package/dist/{lib → src/lib}/parser/declaration/list.js +24 -8
- package/dist/{lib → src/lib}/parser/declaration/map.js +9 -1
- package/dist/{lib → src/lib}/parser/declaration/set.js +7 -0
- package/dist/{lib → src/lib}/parser/linesmap.js +7 -0
- package/dist/{lib → src/lib}/parser/parse.js +93 -49
- package/dist/{lib → src/lib}/parser/source.js +7 -0
- package/dist/{lib → src/lib}/parser/tokenize.js +24 -3
- package/dist/{lib → src/lib}/parser/utils/angle.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/at-rule-container.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/at-rule-font-feature-values.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/at-rule-generic.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/at-rule-import.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/at-rule-media.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/at-rule-support.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/at-rule-when-else.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/at-rule.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/base64.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/cache.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/config.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/declaration.js +17 -0
- package/dist/{lib → src/lib}/parser/utils/eq.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/hash.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/selector.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/text.js +7 -0
- package/dist/{lib → src/lib}/parser/utils/token.js +15 -4
- package/dist/{lib → src/lib}/parser/utils/type.js +7 -0
- package/dist/{lib → src/lib}/printer/render.js +20 -9
- package/dist/{lib → src/lib}/printer/sourcemap/lib/codec.js +7 -0
- package/dist/{lib → src/lib}/printer/sourcemap/sourcemap.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/a98rgb.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/alpha.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/cmyk.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/color-mix.js +139 -48
- package/dist/{lib → src/lib}/syntax/color/color.js +7 -1
- package/dist/{lib → src/lib}/syntax/color/hex.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/hsl.js +52 -38
- package/dist/{lib → src/lib}/syntax/color/hwb.js +90 -53
- package/dist/{lib → src/lib}/syntax/color/lab.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/lch.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/oklab.js +8 -1
- package/dist/{lib → src/lib}/syntax/color/oklch.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/p3.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/prophotorgb.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/rec2020.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/relative-color.js +36 -0
- package/dist/{lib → src/lib}/syntax/color/rgb.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/srgb.js +88 -58
- package/dist/{lib → src/lib}/syntax/color/utils/color-type.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/utils/colorspace.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/utils/components.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/utils/distance.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/utils/make-color.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/utils/matrix.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/xyz.js +7 -0
- package/dist/{lib → src/lib}/syntax/color/xyzd50.js +7 -0
- package/dist/{lib → src/lib}/syntax/constants.js +7 -0
- package/dist/{lib → src/lib}/syntax/syntax.js +7 -0
- package/dist/{lib → src/lib}/validation/config.js +7 -0
- package/dist/{lib → src/lib}/validation/match.js +37 -19
- package/dist/{lib → src/lib}/validation/parser/parse.js +7 -0
- package/dist/{lib → src/lib}/validation/parser/typedef.js +7 -0
- package/dist/{lib → src/lib}/validation/utils/list.js +7 -0
- package/dist/{node.js → src/node.js} +8 -0
- package/dist/{types.js → src/types.js} +7 -0
- package/dist/{utils → src/utils}/sync.js +7 -0
- package/dist/{web.js → src/web.js} +8 -0
- package/package.json +7 -6
- package/dist/lib/syntax/color/hsv.js +0 -20
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 1.6.8
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
1
8
|
import { EnumToken, EnumAstNodeStatus, ColorType } from '../../ast/types.js';
|
|
2
9
|
import { LOCEND, STATE, ERRORS, LOCSTA, tokensfuncDefMap, COLORS_NAMES, nonStandardColors, systemColors, deprecatedSystemColors, tokensMap, trimTokenSpace, LOCSRCID } from '../../syntax/constants.js';
|
|
3
10
|
import { renamedStandardProperties, isColor, parseColor, isWhiteSpace } from '../../syntax/syntax.js';
|
|
@@ -55,6 +62,7 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
55
62
|
let rules = null;
|
|
56
63
|
let syntaxRules = null;
|
|
57
64
|
let success = true;
|
|
65
|
+
let hasImportantFlag = false;
|
|
58
66
|
let validate = typeof options.validation === "boolean" ? options.validation : !!options.validation;
|
|
59
67
|
if (renamedStandardProperties.has(name.val)) {
|
|
60
68
|
name.val = renamedStandardProperties.get(name.val);
|
|
@@ -356,6 +364,9 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
356
364
|
}
|
|
357
365
|
}
|
|
358
366
|
break;
|
|
367
|
+
case EnumToken.ImportantTokenType:
|
|
368
|
+
hasImportantFlag = true;
|
|
369
|
+
break;
|
|
359
370
|
}
|
|
360
371
|
if (trimTokenSpace.has(token.typ)) {
|
|
361
372
|
if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
|
|
@@ -422,6 +433,9 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
422
433
|
}
|
|
423
434
|
name[STATE] = EnumAstNodeStatus.Unknown;
|
|
424
435
|
name[ERRORS] = result?.errors ?? [];
|
|
436
|
+
if (hasImportantFlag) {
|
|
437
|
+
name[STATE] |= EnumAstNodeStatus.ImportantDeclaration;
|
|
438
|
+
}
|
|
425
439
|
// @ts-expect-error
|
|
426
440
|
return Object.assign(name, {
|
|
427
441
|
typ: EnumToken.DeclarationNodeType,
|
|
@@ -459,6 +473,9 @@ function parseDeclaration(tokens, parent, options, errors) {
|
|
|
459
473
|
: EnumAstNodeStatus.Validated
|
|
460
474
|
: EnumAstNodeStatus.ValidationFailed;
|
|
461
475
|
name[ERRORS] = result?.errors ?? [];
|
|
476
|
+
if (hasImportantFlag) {
|
|
477
|
+
name[STATE] |= EnumAstNodeStatus.ImportantDeclaration;
|
|
478
|
+
}
|
|
462
479
|
// @ts-expect-error
|
|
463
480
|
return Object.assign(name, {
|
|
464
481
|
typ: EnumToken.DeclarationNodeType,
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 1.6.8
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
1
8
|
import { EnumToken, EnumAstNodeStatus } from '../../ast/types.js';
|
|
2
9
|
import { renderValue } from '../../printer/render.js';
|
|
3
10
|
import { LOCEND, LOCSTA, LOCSRCID, ERRORS, STATE, TOKENS, pseudoElements, combinators, tokensfuncDefMap, PARENT } from '../../syntax/constants.js';
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 1.6.8
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
1
8
|
import { EnumToken } from '../../ast/types.js';
|
|
2
9
|
import { PARENT, tokensfuncSet } from '../../syntax/constants.js';
|
|
3
10
|
|
|
@@ -9,12 +16,15 @@ const trimTokenSpace = new Set([
|
|
|
9
16
|
EnumToken.LtTokenType,
|
|
10
17
|
EnumToken.LteTokenType,
|
|
11
18
|
EnumToken.ChildCombinatorTokenType,
|
|
19
|
+
EnumToken.PipeTokenType,
|
|
12
20
|
]);
|
|
13
21
|
const trimSpaceAfter = new Set([
|
|
14
22
|
...tokensfuncSet.values(),
|
|
15
23
|
EnumToken.ChildCombinatorTokenType,
|
|
16
24
|
EnumToken.SupportsQueryConditionTokenType,
|
|
17
25
|
EnumToken.SupportsQueryUnaryConditionTokenType,
|
|
26
|
+
EnumToken.CommaTokenType,
|
|
27
|
+
EnumToken.PipeTokenType,
|
|
18
28
|
]);
|
|
19
29
|
/**
|
|
20
30
|
* Replace token in its parent node
|
|
@@ -84,17 +94,18 @@ function trimWhiteSpaceTokens(tokens) {
|
|
|
84
94
|
return tokens;
|
|
85
95
|
}
|
|
86
96
|
for (; i < tokens.length; i++) {
|
|
87
|
-
if (
|
|
88
|
-
tokens.splice(i + 1, 1);
|
|
89
|
-
}
|
|
90
|
-
else if (trimTokenSpace.has(tokens[i].typ)) {
|
|
97
|
+
if (trimTokenSpace.has(tokens[i].typ)) {
|
|
91
98
|
if (tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
|
|
92
99
|
tokens.splice(i + 1, 1);
|
|
93
100
|
}
|
|
94
101
|
if (i > 0 && tokens[i - 1].typ === EnumToken.Whitespace) {
|
|
95
102
|
tokens.splice(--i, 1);
|
|
103
|
+
continue;
|
|
96
104
|
}
|
|
97
105
|
}
|
|
106
|
+
if (trimSpaceAfter.has(tokens[i].typ) && tokens[i + 1]?.typ === EnumToken.WhitespaceTokenType) {
|
|
107
|
+
tokens.splice(i + 1, 1);
|
|
108
|
+
}
|
|
98
109
|
}
|
|
99
110
|
return tokens;
|
|
100
111
|
}
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 1.6.8
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
1
8
|
import { EnumToken } from '../../ast/types.js';
|
|
2
9
|
import { tokensfuncSet } from '../../syntax/constants.js';
|
|
3
10
|
import { isColor } from '../../syntax/syntax.js';
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 1.6.8
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
1
8
|
import { convertColor, getAngle } from '../syntax/color/color.js';
|
|
2
9
|
import { reduceHexValue } from '../syntax/color/hex.js';
|
|
3
10
|
import { EnumToken, ColorType } from '../ast/types.js';
|
|
@@ -1128,7 +1135,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1128
1135
|
return ",";
|
|
1129
1136
|
case EnumToken.ImportantTokenType:
|
|
1130
1137
|
return "!important";
|
|
1131
|
-
case EnumToken.
|
|
1138
|
+
case EnumToken.PipeTokenType:
|
|
1132
1139
|
return "|";
|
|
1133
1140
|
case EnumToken.AttrTokenType:
|
|
1134
1141
|
case EnumToken.IdenListTokenType:
|
|
@@ -1251,6 +1258,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1251
1258
|
pseudoElements.includes(token.val.slice(1))) {
|
|
1252
1259
|
return token.val.slice(1);
|
|
1253
1260
|
}
|
|
1261
|
+
case EnumToken.CssTypeTokenType:
|
|
1254
1262
|
case EnumToken.UrlTokenTokenType:
|
|
1255
1263
|
case EnumToken.HashTokenType:
|
|
1256
1264
|
case EnumToken.IdenTokenType:
|
|
@@ -1284,17 +1292,20 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1284
1292
|
? ""
|
|
1285
1293
|
: token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
|
|
1286
1294
|
":" +
|
|
1295
|
+
(options.indent ?? "") +
|
|
1287
1296
|
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "");
|
|
1288
1297
|
case EnumToken.IfElseConditionTokenType:
|
|
1289
|
-
return renderValue(token.l) +
|
|
1298
|
+
return (renderValue(token.l, options, cache, reducer) +
|
|
1299
|
+
(options.indent ?? "") +
|
|
1300
|
+
renderValue(token.r, options, cache, reducer));
|
|
1290
1301
|
case EnumToken.DeclarationNodeType:
|
|
1291
1302
|
return (token.nam +
|
|
1292
1303
|
":" +
|
|
1293
|
-
(options.minifyValues ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
|
|
1304
|
+
(options.minifyValues ? filterValues(token.val) : token.val).reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
|
|
1294
1305
|
case EnumToken.MediaQueryUnaryFeatureTokenType:
|
|
1295
1306
|
return (renderValue(token.l, options, cache, reducer) +
|
|
1296
1307
|
" " +
|
|
1297
|
-
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
|
|
1308
|
+
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
|
|
1298
1309
|
case EnumToken.MediaQueryConditionTokenType: {
|
|
1299
1310
|
const indent = token.op.typ == EnumToken.LtTokenType ||
|
|
1300
1311
|
token.op.typ == EnumToken.GtTokenType ||
|
|
@@ -1308,14 +1319,14 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1308
1319
|
indent +
|
|
1309
1320
|
renderValue(token.op, options, cache, reducer) +
|
|
1310
1321
|
indent +
|
|
1311
|
-
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
|
|
1322
|
+
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
|
|
1312
1323
|
}
|
|
1313
1324
|
case EnumToken.MediaRangeQueryTokenType:
|
|
1314
|
-
return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache), "") +
|
|
1315
|
-
renderValue(token.op1) +
|
|
1325
|
+
return (token.l.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
|
|
1326
|
+
renderValue(token.op1, options, cache, reducer) +
|
|
1316
1327
|
token.val.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), "") +
|
|
1317
|
-
renderValue(token.op2) +
|
|
1318
|
-
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache), ""));
|
|
1328
|
+
renderValue(token.op2, options, cache, reducer) +
|
|
1329
|
+
token.r.reduce((acc, curr) => acc + renderValue(curr, options, cache, reducer), ""));
|
|
1319
1330
|
case EnumToken.MediaFeatureTokenType:
|
|
1320
1331
|
return token.val;
|
|
1321
1332
|
case EnumToken.NotTokenType:
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 1.6.8
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
1
8
|
import { xyz2srgb } from './srgb.js';
|
|
2
9
|
import { multiplyMatrices } from './utils/matrix.js';
|
|
3
10
|
import { srgb2xyz } from './xyz.js';
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 1.6.8
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
1
8
|
import { ColorType, EnumToken } from '../../ast/types.js';
|
|
2
9
|
import { convertColor } from './color.js';
|
|
3
10
|
import { getColorComponents } from './utils/components.js';
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 1.6.8
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
1
8
|
import { ColorType, EnumToken } from '../../ast/types.js';
|
|
2
9
|
import { color2srgbvalues } from './color.js';
|
|
3
10
|
import { lch2srgbvalues, lab2srgbvalues, oklch2srgbvalues, oklab2srgbvalues, hwb2srgbvalues, rgb2srgbvalues } from './srgb.js';
|
|
@@ -1,12 +1,20 @@
|
|
|
1
|
-
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 1.6.8
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
8
|
+
import { EnumToken, ColorType, EnumAstNodeStatus } from '../../ast/types.js';
|
|
2
9
|
import { minmax, getNumber } from './color.js';
|
|
3
|
-
import { srgbvalues, srgb2lsrgbvalues } from './srgb.js';
|
|
4
|
-
import { srgb2lch, xyz2lchvalues } from './lch.js';
|
|
10
|
+
import { srgbvalues, srgb2lsrgbvalues, hslvalues } from './srgb.js';
|
|
11
|
+
import { srgb2lch, xyz2lchvalues, getLCHComponents } from './lch.js';
|
|
5
12
|
import { srgb2hslvalues } from './hsl.js';
|
|
13
|
+
import { srgb2hwb, hwbvalues } from './hwb.js';
|
|
6
14
|
import { srgb2labvalues } from './lab.js';
|
|
7
15
|
import { srgb2lp3values, srgb2p3values } from './p3.js';
|
|
8
16
|
import { getColorComponents } from './utils/components.js';
|
|
9
|
-
import { srgb2oklch } from './oklch.js';
|
|
17
|
+
import { srgb2oklch, getOKLCHComponents } from './oklch.js';
|
|
10
18
|
import { srgb2oklab } from './oklab.js';
|
|
11
19
|
import { srgb2prophotorgbvalues } from './prophotorgb.js';
|
|
12
20
|
import { srgb2xyz_d65 } from './xyz.js';
|
|
@@ -15,6 +23,7 @@ import { srgb2rec2020values } from './rec2020.js';
|
|
|
15
23
|
import { isRectangularOrthogonalColorspace, isPolarColorspace } from '../syntax.js';
|
|
16
24
|
import { equalsIgnoreCase } from '../../parser/utils/text.js';
|
|
17
25
|
import { srgb2a98values } from './a98rgb.js';
|
|
26
|
+
import { STATE, colorRange } from '../constants.js';
|
|
18
27
|
|
|
19
28
|
function interpolateHue(interpolationMethod, h1, h2) {
|
|
20
29
|
switch (interpolationMethod) {
|
|
@@ -70,6 +79,7 @@ function colorMix(...args) {
|
|
|
70
79
|
const percentages = [];
|
|
71
80
|
const srgbComponentValues = [];
|
|
72
81
|
const colorComponents = [];
|
|
82
|
+
const colors = [];
|
|
73
83
|
if (args[i]?.typ === EnumToken.IdenTokenType) {
|
|
74
84
|
if (!equalsIgnoreCase(args[i++].val, "in")) {
|
|
75
85
|
return null;
|
|
@@ -119,9 +129,10 @@ function colorMix(...args) {
|
|
|
119
129
|
if (values == null) {
|
|
120
130
|
return null;
|
|
121
131
|
}
|
|
122
|
-
if (colorSpace == "hwb") {
|
|
123
|
-
|
|
124
|
-
}
|
|
132
|
+
// if (colorSpace == "hwb") {
|
|
133
|
+
// colorSpace = "srgb";
|
|
134
|
+
// }
|
|
135
|
+
colors.push(processPowerlessComponents(args[i]));
|
|
125
136
|
switch (colorSpace) {
|
|
126
137
|
case "srgb":
|
|
127
138
|
break;
|
|
@@ -151,17 +162,12 @@ function colorMix(...args) {
|
|
|
151
162
|
values = srgb2xyz_d65(values[0], values[1], values[2], values[3]);
|
|
152
163
|
values = XYZ_D65_to_D50(values[0], values[1], values[2], values[3]);
|
|
153
164
|
break;
|
|
154
|
-
// case "rgb":
|
|
155
|
-
// for (let j = 0; j < values.length; j++) {
|
|
156
|
-
// values[j] = j == 3 ? values[j] : srgb2rgb(values[j]);
|
|
157
|
-
// }
|
|
158
|
-
// break;
|
|
159
165
|
case "hsl":
|
|
160
166
|
values = srgb2hslvalues(values[0], values[1], values[2], values[3]);
|
|
161
167
|
break;
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
168
|
+
case "hwb":
|
|
169
|
+
values = srgb2hwb(values[0], values[1], values[2], values[3]);
|
|
170
|
+
break;
|
|
165
171
|
case "lab":
|
|
166
172
|
values = srgb2labvalues(values[0], values[1], values[2], values[3]);
|
|
167
173
|
break;
|
|
@@ -223,7 +229,8 @@ function colorMix(...args) {
|
|
|
223
229
|
}
|
|
224
230
|
}
|
|
225
231
|
const stack = [];
|
|
226
|
-
const lchSpaces = ["lch", "oklch"];
|
|
232
|
+
// const lchSpaces: string[] = ["lch", "oklch"];
|
|
233
|
+
const isLCHOrOKLCH = colorSpace === "lch" || colorSpace == "oklch";
|
|
227
234
|
i = srgbComponentValues.length;
|
|
228
235
|
while (i--) {
|
|
229
236
|
stack.push({
|
|
@@ -241,46 +248,73 @@ function colorMix(...args) {
|
|
|
241
248
|
let premult1;
|
|
242
249
|
let premult2;
|
|
243
250
|
let mixedPremult;
|
|
244
|
-
let
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
if (colorComponents[0][3] != null &&
|
|
250
|
-
colorComponents[0][3].typ == EnumToken.IdenTokenType &&
|
|
251
|
-
colorComponents[0][3].val == "none" &&
|
|
252
|
-
colorComponents[1].length == 4) {
|
|
251
|
+
let offset1 = colors[0].kin == ColorType.COLOR ? 1 : 0;
|
|
252
|
+
if (colorComponents[0][offset1 + 3] != null &&
|
|
253
|
+
colorComponents[0][offset1 + 3].typ == EnumToken.IdenTokenType &&
|
|
254
|
+
colorComponents[0][offset1 + 3].val == "none" &&
|
|
255
|
+
colorComponents[1].length == offset1 + 4) {
|
|
253
256
|
// fix powerless alpha for last color if previous color has alpha
|
|
254
257
|
stack[stack.length - 1].color[3] = stack[stack.length - 2].color[3];
|
|
255
258
|
}
|
|
256
|
-
//
|
|
257
|
-
if (
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
259
|
+
// console.error(stack);
|
|
260
|
+
if (stack.length > 1) {
|
|
261
|
+
// powerless hue for lch spaces
|
|
262
|
+
// if (lchSpaces.includes(colorSpace)) {
|
|
263
|
+
// console.error(JSON.stringify({colorSpace1, colorSpace, stack, colorComponents}, null, 1));
|
|
264
|
+
if ((stack[stack.length - 1].color[0] == 0 && colorComponents[0][offset1].typ == EnumToken.IdenTokenType &&
|
|
265
|
+
colorComponents[0][offset1].val == "none") ||
|
|
266
|
+
colorComponents[0][0][STATE] & EnumAstNodeStatus.PowerlessColorComponent) {
|
|
267
|
+
stack[stack.length - 1].color[0] = stack[stack.length - 2].color[0];
|
|
268
|
+
}
|
|
269
|
+
if ((stack[stack.length - 1].color[1] == 0 && colorComponents[0][offset1 + 1].typ == EnumToken.IdenTokenType &&
|
|
270
|
+
colorComponents[0][offset1 + 1].val == "none") ||
|
|
271
|
+
colorComponents[0][1][STATE] & EnumAstNodeStatus.PowerlessColorComponent) {
|
|
272
|
+
stack[stack.length - 1].color[1] = stack[stack.length - 2].color[1];
|
|
273
|
+
}
|
|
274
|
+
if ((stack[stack.length - 1].color[2] == 0 && colorComponents[0][offset1 + 2].typ == EnumToken.IdenTokenType &&
|
|
275
|
+
colorComponents[0][offset1 + 2].val == "none") ||
|
|
276
|
+
colorComponents[0][2][STATE] & EnumAstNodeStatus.PowerlessColorComponent ||
|
|
277
|
+
(isLCHOrOKLCH &&
|
|
278
|
+
(stack[stack.length - 1].color[1] == 0 ||
|
|
279
|
+
stack[stack.length - 1].color[0] == 0 ||
|
|
280
|
+
stack[stack.length - 1].color[0] ==
|
|
281
|
+
colorRange[colors[0].kin == ColorType.LCH ? "lch" : "oklch"].l[1]))) {
|
|
262
282
|
stack[stack.length - 1].color[2] = stack[stack.length - 2].color[2];
|
|
263
283
|
}
|
|
284
|
+
// }
|
|
264
285
|
}
|
|
286
|
+
// console.error([colorComponents[0][offset1].typ == EnumToken.IdenTokenType, (colorComponents[0][offset1] as IdentToken).val == "none"]);
|
|
287
|
+
// console.error(isLCHOrOKLCH, colorComponents, stack);
|
|
265
288
|
while (stack.length > 1) {
|
|
266
289
|
r1 = stack.pop();
|
|
267
290
|
r2 = stack.pop();
|
|
291
|
+
offset1 = colors[++currentIndex].kin == ColorType.COLOR ? 1 : 0;
|
|
268
292
|
// r2 powerless alpha
|
|
269
|
-
if (colorComponents[
|
|
270
|
-
colorComponents[currentIndex][3].typ == EnumToken.IdenTokenType &&
|
|
271
|
-
colorComponents[currentIndex][3].val == "none") {
|
|
293
|
+
if (colorComponents[currentIndex][offset1 + 3] != null &&
|
|
294
|
+
colorComponents[currentIndex][offset1 + 3].typ == EnumToken.IdenTokenType &&
|
|
295
|
+
colorComponents[currentIndex][offset1 + 3].val == "none") {
|
|
272
296
|
// fix powerless alpha for last color if previous color has alpha
|
|
273
297
|
r2.color[3] = r1.color[3];
|
|
274
298
|
}
|
|
275
|
-
// @ts-expect-error
|
|
276
|
-
colorSpace1 = ColorType[colorComponents[currentIndex].kin]?.toLowerCase?.();
|
|
277
299
|
// powerless hue for lch spaces
|
|
278
|
-
if (
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
300
|
+
if ((r2.color[0] == 0 && colorComponents[currentIndex][offset1].typ == EnumToken.IdenTokenType &&
|
|
301
|
+
colorComponents[currentIndex][offset1].val == "none") ||
|
|
302
|
+
colorComponents[currentIndex][offset1][STATE] & EnumAstNodeStatus.PowerlessColorComponent) {
|
|
303
|
+
r2.color[0] = r1.color[0];
|
|
304
|
+
}
|
|
305
|
+
if ((r2.color[1] == 0 && colorComponents[currentIndex][offset1 + 1].typ == EnumToken.IdenTokenType &&
|
|
306
|
+
colorComponents[currentIndex][offset1 + 1].val == "none") ||
|
|
307
|
+
colorComponents[currentIndex][offset1 + 1][STATE] & EnumAstNodeStatus.PowerlessColorComponent) {
|
|
308
|
+
r2.color[1] = r1.color[1];
|
|
309
|
+
}
|
|
310
|
+
if ((r2.color[2] == 0 && colorComponents[currentIndex][offset1 + 2].typ == EnumToken.IdenTokenType &&
|
|
311
|
+
colorComponents[currentIndex][offset1 + 2].val == "none") ||
|
|
312
|
+
colorComponents[currentIndex][offset1 + 2][STATE] & EnumAstNodeStatus.PowerlessColorComponent ||
|
|
313
|
+
(isLCHOrOKLCH &&
|
|
314
|
+
(r2.color[1] == 0 ||
|
|
315
|
+
r2.color[0] == 0 ||
|
|
316
|
+
r2.color[0] == colorRange[colors[currentIndex].kin == ColorType.LCH ? "lch" : "oklch"].l[1]))) {
|
|
317
|
+
r2.color[2] = r1.color[2];
|
|
284
318
|
}
|
|
285
319
|
if (hueInterpolationMethod != null) {
|
|
286
320
|
let hueIndex = 2;
|
|
@@ -308,6 +342,7 @@ function colorMix(...args) {
|
|
|
308
342
|
alpha: mult,
|
|
309
343
|
};
|
|
310
344
|
}
|
|
345
|
+
// console.error({ r1, r2, r, premult1, mult1, premult2, mult2, mixedPremult, mult });
|
|
311
346
|
stack.push(r);
|
|
312
347
|
}
|
|
313
348
|
const result = stack.pop();
|
|
@@ -362,16 +397,16 @@ function colorMix(...args) {
|
|
|
362
397
|
};
|
|
363
398
|
// case "rgb":
|
|
364
399
|
case "hsl":
|
|
365
|
-
|
|
400
|
+
case "hwb":
|
|
366
401
|
case "lab":
|
|
367
402
|
case "lch":
|
|
368
403
|
case "oklab":
|
|
369
404
|
case "oklch":
|
|
370
|
-
if (colorSpace == "hsl") {
|
|
405
|
+
if (colorSpace == "hsl" || colorSpace == "hwb") {
|
|
371
406
|
// @ts-ignore
|
|
372
407
|
if (values[2] < 0) {
|
|
373
408
|
// @ts-ignore
|
|
374
|
-
values[2] +=
|
|
409
|
+
values[2] += 360;
|
|
375
410
|
}
|
|
376
411
|
}
|
|
377
412
|
else if (colorSpace == "lch" || colorSpace == "oklch") {
|
|
@@ -399,15 +434,71 @@ function colorMix(...args) {
|
|
|
399
434
|
};
|
|
400
435
|
if (colorSpace == "hsl") {
|
|
401
436
|
// @ts-ignore
|
|
402
|
-
result.chi[0] = { typ: EnumToken.AngleTokenType, val: result.chi[0].val
|
|
437
|
+
result.chi[0] = { typ: EnumToken.AngleTokenType, val: result.chi[0].val };
|
|
403
438
|
// @ts-ignore
|
|
404
|
-
result.chi[1] = { typ: EnumToken.PercentageTokenType, val: result.chi[1].val
|
|
439
|
+
result.chi[1] = { typ: EnumToken.PercentageTokenType, val: result.chi[1].val };
|
|
405
440
|
// @ts-ignore
|
|
406
|
-
result.chi[2] = { typ: EnumToken.PercentageTokenType, val: result.chi[2].val
|
|
441
|
+
result.chi[2] = { typ: EnumToken.PercentageTokenType, val: result.chi[2].val };
|
|
407
442
|
}
|
|
408
443
|
return result;
|
|
409
444
|
}
|
|
410
445
|
return null;
|
|
411
446
|
}
|
|
447
|
+
function processPowerlessComponents(color) {
|
|
448
|
+
let components;
|
|
449
|
+
let values;
|
|
450
|
+
switch (color.kin) {
|
|
451
|
+
case ColorType.LCH:
|
|
452
|
+
case ColorType.OKLCH:
|
|
453
|
+
values = color.kin == ColorType.LCH ? getLCHComponents(color) : getOKLCHComponents(color);
|
|
454
|
+
components = getColorComponents(color);
|
|
455
|
+
if (values == null || values.length < 3 || components == null) {
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
const max = color.kin == ColorType.LCH ? colorRange.lch.l[1] : colorRange.oklch.l[1];
|
|
459
|
+
// https://www.w3.org/TR/css-color-4/#specifying-lab-lch
|
|
460
|
+
// https://www.w3.org/TR/css-color-4/#specifying-oklab-oklch
|
|
461
|
+
const epsilon = color.kin == ColorType.LCH ? 0.0015 : 0.000004;
|
|
462
|
+
if (values[0] < epsilon || Math.abs(values[0] - max) < epsilon) {
|
|
463
|
+
Object.assign(components[0], { val: values[0] < epsilon ? 0 : max, typ: EnumToken.NumberTokenType });
|
|
464
|
+
Object.assign(components[1], { val: "none", typ: EnumToken.IdenTokenType });
|
|
465
|
+
Object.assign(components[2], { val: "none", typ: EnumToken.IdenTokenType });
|
|
466
|
+
setColorFlag(components[1], EnumAstNodeStatus.PowerlessColorComponent);
|
|
467
|
+
setColorFlag(components[2], EnumAstNodeStatus.PowerlessColorComponent);
|
|
468
|
+
}
|
|
469
|
+
else if (values[1] < epsilon) {
|
|
470
|
+
values[1] = 0;
|
|
471
|
+
Object.assign(components[1], { val: 0, typ: EnumToken.NumberTokenType });
|
|
472
|
+
Object.assign(components[2], { val: "none", typ: EnumToken.IdenTokenType });
|
|
473
|
+
setColorFlag(components[2], EnumAstNodeStatus.PowerlessColorComponent);
|
|
474
|
+
}
|
|
475
|
+
break;
|
|
476
|
+
case ColorType.HSL:
|
|
477
|
+
case ColorType.HWB:
|
|
478
|
+
values = Object.values((color.kin == ColorType.HSL ? hslvalues(color) : hwbvalues(color)) ?? {});
|
|
479
|
+
components = getColorComponents(color);
|
|
480
|
+
if (values == null || values.length < 3 || components == null) {
|
|
481
|
+
break;
|
|
482
|
+
}
|
|
483
|
+
// https://www.w3.org/TR/css-color-4/#the-hsl-notation
|
|
484
|
+
// https://www.w3.org/TR/css-color-4/#the-hwb-notation
|
|
485
|
+
if ((color.kin == ColorType.HSL && values[1] <= 0.001) ||
|
|
486
|
+
(color.kin == ColorType.HWB && values[1] + values[2] >= 99.999)) {
|
|
487
|
+
Object.assign(components[0], { val: "none", typ: EnumToken.IdenTokenType });
|
|
488
|
+
setColorFlag(components[0], EnumAstNodeStatus.PowerlessColorComponent);
|
|
489
|
+
}
|
|
490
|
+
break;
|
|
491
|
+
}
|
|
492
|
+
// console.error({ color: color.chi ?? color, values });
|
|
493
|
+
return color;
|
|
494
|
+
}
|
|
495
|
+
function setColorFlag(token, flag) {
|
|
496
|
+
if (token[STATE] == null) {
|
|
497
|
+
token[STATE] = flag;
|
|
498
|
+
}
|
|
499
|
+
else {
|
|
500
|
+
token[STATE] |= flag;
|
|
501
|
+
}
|
|
502
|
+
}
|
|
412
503
|
|
|
413
504
|
export { colorMix };
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 1.6.8
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
1
8
|
import { ColorType, EnumToken } from '../../ast/types.js';
|
|
2
9
|
import { color2RgbToken, lch2RgbToken, lab2RgbToken, oklch2RgbToken, oklab2RgbToken, cmyk2RgbToken, hwb2RgbToken, hsl2RgbToken, hex2RgbToken } from './rgb.js';
|
|
3
10
|
import { color2HslToken, lch2HslToken, lab2HslToken, oklch2HslToken, oklab2HslToken, cmyk2HslToken, hwb2HslToken, hex2HslToken, rgb2HslToken } from './hsl.js';
|
|
@@ -582,7 +589,6 @@ function getAngle(token) {
|
|
|
582
589
|
return +token.val;
|
|
583
590
|
}
|
|
584
591
|
}
|
|
585
|
-
// @ts-ignore
|
|
586
592
|
return token.val / 360;
|
|
587
593
|
}
|
|
588
594
|
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/*
|
|
2
|
+
CSS parser, minifier and validator for node and the browser
|
|
3
|
+
|
|
4
|
+
Author(s): Thierry Bela
|
|
5
|
+
Version: 1.6.8
|
|
6
|
+
License: MIT
|
|
7
|
+
*/
|
|
1
8
|
import { EnumToken, ColorType } from '../../ast/types.js';
|
|
2
9
|
import { color2srgbvalues, getNumber } from './color.js';
|
|
3
10
|
import { srgb2rgb, cmyk2rgbvalues, hwb2rgbvalues, hsl2rgbvalues } from './rgb.js';
|