@tbela99/css-parser 1.4.6 → 1.4.7
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 +17 -0
- package/README.md +1 -0
- package/dist/index-umd-web.js +6548 -6964
- package/dist/index.cjs +7784 -8198
- package/dist/index.d.ts +919 -51
- package/dist/lib/ast/clone.js +16 -23
- package/dist/lib/ast/expand.js +2 -2
- package/dist/lib/ast/features/if.js +88 -87
- package/dist/lib/ast/features/inlinecssvariables.js +2 -2
- package/dist/lib/ast/features/prefix.js +26 -22
- package/dist/lib/ast/features/transform.js +3 -3
- package/dist/lib/ast/find.js +17 -14
- package/dist/lib/ast/minify.js +97 -94
- package/dist/lib/ast/types.js +6 -2
- package/dist/lib/ast/walk.js +1 -1
- package/dist/lib/fs/resolve.js +46 -44
- package/dist/lib/parser/declaration/list.js +9 -10
- package/dist/lib/parser/declaration/map.js +13 -18
- package/dist/lib/parser/parse.js +331 -504
- package/dist/lib/parser/tokenize.js +39 -33
- package/dist/lib/parser/utils/at-rule-container.js +244 -247
- package/dist/lib/parser/utils/at-rule-generic.js +25 -25
- package/dist/lib/parser/utils/at-rule-import.js +194 -234
- package/dist/lib/parser/utils/at-rule-media.js +286 -286
- package/dist/lib/parser/utils/at-rule-support.js +279 -251
- package/dist/lib/parser/utils/at-rule-when-else.js +256 -259
- package/dist/lib/parser/utils/at-rule.js +19 -16
- package/dist/lib/parser/utils/cache.js +33 -5
- package/dist/lib/parser/utils/declaration.js +260 -298
- package/dist/lib/parser/utils/hash.js +15 -15
- package/dist/lib/parser/utils/selector.js +182 -222
- package/dist/lib/parser/utils/token.js +4 -7
- package/dist/lib/renderer/render.js +150 -120
- package/dist/lib/syntax/color/alpha.js +1 -4
- package/dist/lib/syntax/color/color.js +5 -5
- package/dist/lib/syntax/color/utils/colorspace.js +3 -3
- package/dist/lib/syntax/color/utils/components.js +21 -22
- package/dist/lib/syntax/color/utils/matrix.js +12 -11
- package/dist/lib/syntax/constants.js +22 -3
- package/dist/lib/syntax/syntax.js +252 -311
- package/dist/lib/validation/config.js +16 -17
- package/dist/lib/validation/match.js +1118 -1131
- package/dist/lib/validation/parser/parse.js +130 -286
- package/dist/node.js +16 -12
- package/dist/web.js +10 -8
- package/package.json +4 -4
- package/dist/lib/validation/utils/whitespace.js +0 -17
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { EnumToken } from '../../ast/types.js';
|
|
2
|
-
import {
|
|
2
|
+
import { PARENT, tokensfuncSet } from '../../syntax/constants.js';
|
|
3
3
|
|
|
4
4
|
const trimTokenSpace = new Set([
|
|
5
5
|
EnumToken.CommaTokenType,
|
|
@@ -28,12 +28,9 @@ function replaceNodeOrValue(parent, node, replacement) {
|
|
|
28
28
|
if (replacement == null || (Array.isArray(replacement) && replacement.length === 0)) {
|
|
29
29
|
throw new TypeError(`replacement is null`);
|
|
30
30
|
}
|
|
31
|
-
for (const
|
|
32
|
-
if (
|
|
33
|
-
|
|
34
|
-
...definedPropertySettings,
|
|
35
|
-
value: node.parent,
|
|
36
|
-
});
|
|
31
|
+
for (const newNode of Array.isArray(replacement) ? replacement : [replacement]) {
|
|
32
|
+
if (newNode[PARENT] != node[PARENT]) {
|
|
33
|
+
newNode[PARENT] = node[PARENT];
|
|
37
34
|
}
|
|
38
35
|
}
|
|
39
36
|
if (parent.typ == EnumToken.BinaryExpressionTokenType) {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { convertColor, getAngle } from '../syntax/color/color.js';
|
|
1
|
+
import { convertColor, toPrecisionAngle, getAngle } from '../syntax/color/color.js';
|
|
2
2
|
import { reduceHexValue } from '../syntax/color/hex.js';
|
|
3
3
|
import { EnumToken, minifyNumber, ColorType } from '../ast/types.js';
|
|
4
4
|
import { expand } from '../ast/expand.js';
|
|
5
5
|
import { SourceMap } from './sourcemap/sourcemap.js';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
6
|
+
import { pseudoElements, urlTokenMatcher, PARENT, tokensfuncSet, LOC, colorPrecision } from '../syntax/constants.js';
|
|
7
|
+
import { reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor } from '../syntax/syntax.js';
|
|
8
8
|
import { move } from '../parser/tokenize.js';
|
|
9
9
|
import { equalsIgnoreCase } from '../parser/utils/text.js';
|
|
10
10
|
import { toDegrees } from '../parser/utils/angle.js';
|
|
@@ -47,13 +47,12 @@ function doRender(data, options = {}, mapping) {
|
|
|
47
47
|
};
|
|
48
48
|
if (options.withParents) {
|
|
49
49
|
// @ts-ignore
|
|
50
|
-
let parent = data
|
|
51
|
-
|
|
52
|
-
while (data.parent != null) {
|
|
50
|
+
let parent = data[PARENT];
|
|
51
|
+
while (data[PARENT] != null) {
|
|
53
52
|
// @ts-ignore
|
|
54
|
-
parent = { ...data
|
|
53
|
+
parent = { ...data[PARENT], chi: [{ ...data }] };
|
|
55
54
|
// @ts-ignore
|
|
56
|
-
parent
|
|
55
|
+
parent[PARENT] = data[PARENT][PARENT];
|
|
57
56
|
// @ts-ignore
|
|
58
57
|
data = parent;
|
|
59
58
|
}
|
|
@@ -127,11 +126,12 @@ function updateSourceMap(node, options, cache, sourcemap, position, str) {
|
|
|
127
126
|
EnumToken.KeyFramesRuleNodeType,
|
|
128
127
|
EnumToken.KeyframesAtRuleNodeType,
|
|
129
128
|
].includes(node.typ)) {
|
|
130
|
-
let src = node
|
|
129
|
+
let src = node[LOC]?.src ?? "";
|
|
130
|
+
// console.debug(src);
|
|
131
131
|
sourcemap.add(
|
|
132
132
|
// @ts-ignore
|
|
133
133
|
{ src, sta: { ...position } }, {
|
|
134
|
-
...node
|
|
134
|
+
...node[LOC],
|
|
135
135
|
// @ts-ignore
|
|
136
136
|
src,
|
|
137
137
|
});
|
|
@@ -182,12 +182,12 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
|
|
|
182
182
|
return css;
|
|
183
183
|
}
|
|
184
184
|
if (css === "") {
|
|
185
|
-
if (sourcemap != null && node
|
|
185
|
+
if (sourcemap != null && node[LOC] != null) {
|
|
186
186
|
updateSourceMap(node, options, cache, sourcemap, position, str);
|
|
187
187
|
}
|
|
188
188
|
return str;
|
|
189
189
|
}
|
|
190
|
-
if (sourcemap != null && node
|
|
190
|
+
if (sourcemap != null && node[LOC] != null) {
|
|
191
191
|
move(position, options.newLine);
|
|
192
192
|
updateSourceMap(node, options, cache, sourcemap, position, str);
|
|
193
193
|
}
|
|
@@ -211,24 +211,24 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
|
|
|
211
211
|
: node.val;
|
|
212
212
|
}
|
|
213
213
|
else if (node.typ == EnumToken.DeclarationNodeType) {
|
|
214
|
-
if (!node.nam.startsWith("--") && node.val.length === 0) {
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
214
|
+
// if (!(<AstDeclaration>node).nam.startsWith("--") && (<AstDeclaration>node).val.length === 0) {
|
|
215
|
+
// // @ts-ignore
|
|
216
|
+
// errors.push(<ErrorDescription>{
|
|
217
|
+
// action: "ignore",
|
|
218
|
+
// message: `render: invalid declaration ${JSON.stringify(node)}`,
|
|
219
|
+
// location: node[LOC],
|
|
220
|
+
// });
|
|
221
|
+
// return "";
|
|
222
|
+
// }
|
|
223
223
|
str = `${node.nam}:${options.indent}${(options.minify
|
|
224
224
|
? filterValues(node.val)
|
|
225
225
|
: node.val)
|
|
226
226
|
.reduce(reducer, "")
|
|
227
227
|
.trimEnd()};`;
|
|
228
228
|
}
|
|
229
|
-
else if (node.typ == EnumToken.AtRuleNodeType && !("chi" in node)) {
|
|
230
|
-
|
|
231
|
-
}
|
|
229
|
+
// else if (node.typ == EnumToken.AtRuleNodeType && !("chi" in node)) {
|
|
230
|
+
// str = `${(<AstAtRule>node).val === "" ? "" : options.indent || " "}${(<AstAtRule>node).val};`;
|
|
231
|
+
// }
|
|
232
232
|
else {
|
|
233
233
|
str = renderAstNode(node, options, sourcemap, { ...position }, errors, reducer, cache, level + 1, indents);
|
|
234
234
|
}
|
|
@@ -257,22 +257,24 @@ function renderAstNode(data, options, sourcemap, position, errors, reducer, cach
|
|
|
257
257
|
(children === "" ? "" : indentSub + children + options.newLine) +
|
|
258
258
|
indent +
|
|
259
259
|
`}`);
|
|
260
|
-
case EnumToken.CssVariableTokenType:
|
|
261
|
-
case EnumToken.CssVariableImportTokenType:
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
case EnumToken.
|
|
260
|
+
// case EnumToken.CssVariableTokenType:
|
|
261
|
+
// case EnumToken.CssVariableImportTokenType:
|
|
262
|
+
// return `@value ${(<CssVariableToken | CssVariableImportTokenType>data).val}:${options.indent}${filterValues(
|
|
263
|
+
// options.minify
|
|
264
|
+
// ? (<CssVariableToken | CssVariableImportTokenType>data).val
|
|
265
|
+
// : (<CssVariableToken>data).val,
|
|
266
|
+
// )
|
|
267
|
+
// .reduce(reducer, "")
|
|
268
|
+
// .trim()};`;
|
|
269
|
+
// case EnumToken.CssVariableDeclarationMapTokenType:
|
|
270
|
+
// return `@value ${filterValues((data as CssVariableMapTokenType).vars)
|
|
271
|
+
// .reduce((acc, curr) => acc + renderValue(curr), "")
|
|
272
|
+
// .trim()} from ${filterValues((data as CssVariableMapTokenType).from)
|
|
273
|
+
// .reduce((acc, curr) => acc + renderValue(curr), "")
|
|
274
|
+
// .trim()};`;
|
|
275
|
+
// case EnumToken.InvalidDeclarationNodeType:
|
|
276
|
+
// case EnumToken.InvalidRuleNodeType:
|
|
277
|
+
// case EnumToken.InvalidAtRuleNodeType:
|
|
276
278
|
default:
|
|
277
279
|
return "";
|
|
278
280
|
}
|
|
@@ -300,42 +302,46 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
300
302
|
return acc + renderValue(curr, options, cache, reducer, errors);
|
|
301
303
|
};
|
|
302
304
|
}
|
|
303
|
-
if (token.typ == EnumToken.FunctionTokenType && colorsFunc.includes(token.val)) {
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
305
|
+
// if (token.typ == EnumToken.FunctionTokenType && colorsFunc.includes((token as FunctionToken).val)) {
|
|
306
|
+
// if (isColor(token)) {
|
|
307
|
+
// // @ts-ignore
|
|
308
|
+
// token.typ = EnumToken.ColorTokenType;
|
|
309
|
+
// if (
|
|
310
|
+
// // @ts-ignore
|
|
311
|
+
// (token as ColorToken)!.chi[0]!.typ == EnumToken.IdenTokenType &&
|
|
312
|
+
// // @ts-ignore
|
|
313
|
+
// ((token as ColorToken)!.chi[0] as IdentToken).val == "from"
|
|
314
|
+
// ) {
|
|
315
|
+
// // @ts-ignore
|
|
316
|
+
// (<ColorToken>token).cal = "rel";
|
|
317
|
+
// } else {
|
|
318
|
+
// // @ts-ignore
|
|
319
|
+
// if (
|
|
320
|
+
// (token as ColorToken).val == "color-mix" &&
|
|
321
|
+
// (token as ColorToken).chi?.[0]?.typ == EnumToken.IdenTokenType &&
|
|
322
|
+
// ((token as ColorToken).chi?.[0] as IdentToken).val == "in"
|
|
323
|
+
// ) {
|
|
324
|
+
// // @ts-ignore
|
|
325
|
+
// (<ColorToken>token).cal = "mix";
|
|
326
|
+
// } else {
|
|
327
|
+
// // @ts-ignore
|
|
328
|
+
// if ((token as ColorToken).val == "color") {
|
|
329
|
+
// // @ts-ignore
|
|
330
|
+
// token.cal = "col";
|
|
331
|
+
// }
|
|
332
|
+
// // @ts-ignore
|
|
333
|
+
// (token as ColorToken).chi = (token as ColorToken).chi!.filter(
|
|
334
|
+
// (t: Token) =>
|
|
335
|
+
// ![
|
|
336
|
+
// EnumToken.WhitespaceTokenType,
|
|
337
|
+
// EnumToken.CommaTokenType,
|
|
338
|
+
// EnumToken.CommentTokenType,
|
|
339
|
+
// ].includes(t.typ),
|
|
340
|
+
// );
|
|
341
|
+
// }
|
|
342
|
+
// }
|
|
343
|
+
// }
|
|
344
|
+
// }
|
|
339
345
|
switch (token.typ) {
|
|
340
346
|
case EnumToken.FunctionTokenDefType:
|
|
341
347
|
case EnumToken.UrlFunctionTokenDefType:
|
|
@@ -399,6 +405,10 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
399
405
|
return "*";
|
|
400
406
|
case EnumToken.Div:
|
|
401
407
|
return "/";
|
|
408
|
+
case EnumToken.WrappedValuesTokenType:
|
|
409
|
+
return ("{" +
|
|
410
|
+
token.chi.reduce((acc, curr) => acc + renderValue(curr, options, cache), "") +
|
|
411
|
+
"}");
|
|
402
412
|
case EnumToken.ColorTokenType:
|
|
403
413
|
if (token.kin == ColorType.LIGHT_DARK ||
|
|
404
414
|
("chi" in token && options.convertColor === false && token.chi.length == 2)) {
|
|
@@ -569,20 +579,22 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
569
579
|
else if (slice[i].typ == EnumToken.AngleTokenType) {
|
|
570
580
|
if ((180 - toDegrees(slice[i]).val) % 360 === 0) {
|
|
571
581
|
k = i + 1;
|
|
572
|
-
while (k < slice.length && slice[k].typ !== EnumToken.CommaTokenType) {
|
|
573
|
-
|
|
574
|
-
}
|
|
582
|
+
// while (k < slice.length && slice[k].typ !== EnumToken.CommaTokenType) {
|
|
583
|
+
// k++;
|
|
584
|
+
// }
|
|
575
585
|
slice.splice(i, k - i + 1);
|
|
576
586
|
}
|
|
577
587
|
}
|
|
578
588
|
}
|
|
579
589
|
if (slice[i]?.typ === EnumToken.CommaTokenType) {
|
|
580
590
|
i++;
|
|
581
|
-
while (
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
591
|
+
// while (
|
|
592
|
+
// i < slice.length &&
|
|
593
|
+
// (slice[i].typ === EnumToken.WhitespaceTokenType ||
|
|
594
|
+
// slice[i].typ === EnumToken.CommentTokenType)
|
|
595
|
+
// ) {
|
|
596
|
+
// i++;
|
|
597
|
+
// }
|
|
586
598
|
}
|
|
587
599
|
if (slice[i]?.typ === EnumToken.ColorTokenType) {
|
|
588
600
|
slice.push(...reduceColorStops(slice.splice(i, slice.length - i)));
|
|
@@ -593,11 +605,13 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
593
605
|
case "repeating-radial-gradient":
|
|
594
606
|
{
|
|
595
607
|
let i = 0;
|
|
596
|
-
while (
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
608
|
+
// while (
|
|
609
|
+
// i < slice.length &&
|
|
610
|
+
// (slice[i].typ === EnumToken.WhitespaceTokenType ||
|
|
611
|
+
// slice[i].typ === EnumToken.CommentTokenType)
|
|
612
|
+
// ) {
|
|
613
|
+
// i++;
|
|
614
|
+
// }
|
|
601
615
|
const positions = [];
|
|
602
616
|
const form = [];
|
|
603
617
|
const size = [];
|
|
@@ -809,11 +823,13 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
809
823
|
const angles = [];
|
|
810
824
|
const positions = [];
|
|
811
825
|
const colorSpaceDef = [];
|
|
812
|
-
while (
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
826
|
+
// while (
|
|
827
|
+
// i < slice.length &&
|
|
828
|
+
// (slice[i].typ === EnumToken.WhitespaceTokenType ||
|
|
829
|
+
// slice[i].typ === EnumToken.CommentTokenType)
|
|
830
|
+
// ) {
|
|
831
|
+
// i++;
|
|
832
|
+
// }
|
|
817
833
|
if (slice[i]?.typ === EnumToken.IdenTokenType &&
|
|
818
834
|
equalsIgnoreCase(slice[i].val, "from")) {
|
|
819
835
|
angles.push(slice[i++]);
|
|
@@ -897,13 +913,18 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
897
913
|
i++;
|
|
898
914
|
}
|
|
899
915
|
const result = [];
|
|
916
|
+
if (positions.length > 0) {
|
|
917
|
+
if (positions.length > 0) {
|
|
918
|
+
if (angles.length > 0) {
|
|
919
|
+
angles.push({ typ: EnumToken.WhitespaceTokenType });
|
|
920
|
+
}
|
|
921
|
+
angles.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }, ...positions);
|
|
922
|
+
}
|
|
923
|
+
}
|
|
900
924
|
if (angles.length > 0) {
|
|
901
925
|
result.push(...angles, { typ: EnumToken.CommaTokenType });
|
|
902
926
|
}
|
|
903
|
-
if (
|
|
904
|
-
if (positions.length > 0) {
|
|
905
|
-
result.push({ typ: EnumToken.IdenTokenType, val: "at" }, { typ: EnumToken.WhitespaceTokenType }, ...positions);
|
|
906
|
-
}
|
|
927
|
+
if (colorSpaceDef.length > 0) {
|
|
907
928
|
if (colorSpaceDef.length > 0) {
|
|
908
929
|
if (result.length > 0) {
|
|
909
930
|
result.push({ typ: EnumToken.WhitespaceTokenType });
|
|
@@ -948,23 +969,32 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
948
969
|
"(" +
|
|
949
970
|
token.chi.reduce(reducer, "") +
|
|
950
971
|
")");
|
|
951
|
-
case EnumToken.MatchExpressionTokenType:
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
972
|
+
// case EnumToken.MatchExpressionTokenType:
|
|
973
|
+
// return (
|
|
974
|
+
// renderValue((token as MatchExpressionToken).l as Token, options, cache, reducer, errors) +
|
|
975
|
+
// renderValue((token as MatchExpressionToken).op, options, cache, reducer, errors) +
|
|
976
|
+
// renderValue((token as MatchExpressionToken).r, options, cache, reducer, errors) +
|
|
977
|
+
// ((token as MatchExpressionToken).attr ? " " + (token as MatchExpressionToken).attr : "")
|
|
978
|
+
// );
|
|
979
|
+
// case EnumToken.NameSpaceAttributeTokenType:
|
|
980
|
+
// return (
|
|
981
|
+
// ((token as NameSpaceAttributeToken).l == null
|
|
982
|
+
// ? ""
|
|
983
|
+
// : renderValue((token as NameSpaceAttributeToken).l as Token, options, cache, reducer, errors)) +
|
|
984
|
+
// "|" +
|
|
985
|
+
// renderValue((token as NameSpaceAttributeToken).r, options, cache, reducer, errors)
|
|
986
|
+
// );
|
|
987
|
+
// case EnumToken.ComposesSelectorNodeType:
|
|
988
|
+
// return (
|
|
989
|
+
// (token as ComposesSelectorToken).l.reduce(
|
|
990
|
+
// (acc: string, curr: Token) => acc + renderValue(curr, options, cache),
|
|
991
|
+
// "",
|
|
992
|
+
// ) +
|
|
993
|
+
// ((token as ComposesSelectorToken).r == null
|
|
994
|
+
// ? ""
|
|
995
|
+
// : " from " +
|
|
996
|
+
// renderValue((token as ComposesSelectorToken).r as Token, options, cache, reducer, errors))
|
|
997
|
+
// );
|
|
968
998
|
case EnumToken.BlockStartTokenType:
|
|
969
999
|
return "{";
|
|
970
1000
|
case EnumToken.BlockEndTokenType:
|
|
@@ -1045,7 +1075,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1045
1075
|
}
|
|
1046
1076
|
switch (u) {
|
|
1047
1077
|
case "turn":
|
|
1048
|
-
v = minifyNumber(angle);
|
|
1078
|
+
v = minifyNumber(toPrecisionAngle(angle, colorPrecision, false));
|
|
1049
1079
|
if (v.length + 4 < value.length) {
|
|
1050
1080
|
val = v;
|
|
1051
1081
|
unit = u;
|
|
@@ -1053,7 +1083,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1053
1083
|
}
|
|
1054
1084
|
break;
|
|
1055
1085
|
case "deg":
|
|
1056
|
-
v = minifyNumber(angle * 360);
|
|
1086
|
+
v = minifyNumber(toPrecisionAngle(angle * 360, colorPrecision, false));
|
|
1057
1087
|
if (v.length + 3 < value.length) {
|
|
1058
1088
|
val = v;
|
|
1059
1089
|
unit = u;
|
|
@@ -1061,7 +1091,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1061
1091
|
}
|
|
1062
1092
|
break;
|
|
1063
1093
|
case "rad":
|
|
1064
|
-
v = minifyNumber(angle * (2 * Math.PI));
|
|
1094
|
+
v = minifyNumber(toPrecisionAngle(angle * (2 * Math.PI), colorPrecision, false));
|
|
1065
1095
|
if (v.length + 3 < value.length) {
|
|
1066
1096
|
val = v;
|
|
1067
1097
|
unit = u;
|
|
@@ -1069,7 +1099,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1069
1099
|
}
|
|
1070
1100
|
break;
|
|
1071
1101
|
case "grad":
|
|
1072
|
-
v = minifyNumber(angle * 400);
|
|
1102
|
+
v = minifyNumber(toPrecisionAngle(angle * 400, colorPrecision, false));
|
|
1073
1103
|
if (v.length + 4 < value.length) {
|
|
1074
1104
|
val = v;
|
|
1075
1105
|
unit = u;
|
|
@@ -1209,7 +1239,7 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1209
1239
|
case EnumToken.OrTokenType:
|
|
1210
1240
|
return "or";
|
|
1211
1241
|
case EnumToken.InvalidMediaQueryTokenType:
|
|
1212
|
-
case EnumToken.InvalidDeclarationNodeType:
|
|
1242
|
+
// case EnumToken.InvalidDeclarationNodeType:
|
|
1213
1243
|
case EnumToken.InvalidCommentTokenType:
|
|
1214
1244
|
case EnumToken.BadCommentTokenType:
|
|
1215
1245
|
case EnumToken.BadCdoTokenType:
|
|
@@ -22,9 +22,6 @@ function alpha(color, alpha) {
|
|
|
22
22
|
if (color.kin === ColorType.DEVICE_CMYK) {
|
|
23
23
|
return null;
|
|
24
24
|
}
|
|
25
|
-
if (alpha == null) {
|
|
26
|
-
return color;
|
|
27
|
-
}
|
|
28
25
|
if (color.kin === ColorType.COLOR_MIX || color.cal === "rel") {
|
|
29
26
|
color = convertColor(color, getColorType(color));
|
|
30
27
|
if (color == null) {
|
|
@@ -35,7 +32,7 @@ function alpha(color, alpha) {
|
|
|
35
32
|
if (components == null) {
|
|
36
33
|
return null;
|
|
37
34
|
}
|
|
38
|
-
if (alpha
|
|
35
|
+
if (alpha?.typ === EnumToken.IdenTokenType && equalsIgnoreCase(alpha.val, "alpha")) {
|
|
39
36
|
alpha = components[3] ?? {
|
|
40
37
|
typ: EnumToken.NumberTokenType,
|
|
41
38
|
val: 1,
|
|
@@ -19,7 +19,7 @@ import { parseRelativeColorComponents } from './relativecolor.js';
|
|
|
19
19
|
import { isIdentColor } from '../syntax.js';
|
|
20
20
|
import { color2cmykToken, lch2cmykToken, lab2cmykToken, oklch2cmykToken, oklab2cmyk, hwb2cmykToken, hsl2cmykToken, rgb2cmykToken } from './cmyk.js';
|
|
21
21
|
import { a98rgb2srgbvalues, srgb2a98values } from './a98rgb.js';
|
|
22
|
-
import {
|
|
22
|
+
import { LOC, colorPrecision, colorFuncColorSpace, epsilon, anglePrecision } from '../constants.js';
|
|
23
23
|
import { trimArray } from '../../validation/match.js';
|
|
24
24
|
import { alpha } from './alpha.js';
|
|
25
25
|
import { equalsIgnoreCase } from '../../parser/utils/text.js';
|
|
@@ -86,7 +86,7 @@ function convertColor(token, to) {
|
|
|
86
86
|
chi: [...(token.val == "color" ? [chi[offset]] : []), ...Object.values(components)],
|
|
87
87
|
kin: ColorType[token.val.toUpperCase().replaceAll("-", "_")],
|
|
88
88
|
};
|
|
89
|
-
|
|
89
|
+
tk[LOC] = token[LOC];
|
|
90
90
|
token = tk;
|
|
91
91
|
}
|
|
92
92
|
}
|
|
@@ -599,15 +599,15 @@ function toPrecisionValue(value, precision = colorPrecision) {
|
|
|
599
599
|
value = Math.round(value * div) / div;
|
|
600
600
|
return Math.abs(value) < epsilon ? 0 : value;
|
|
601
601
|
}
|
|
602
|
-
function toPrecisionAngle(angle, precision = colorPrecision) {
|
|
602
|
+
function toPrecisionAngle(angle, precision = colorPrecision, correctValue = true) {
|
|
603
603
|
angle = toPrecisionValue(angle, precision);
|
|
604
|
-
if (Math.abs(angle) >= 360) {
|
|
604
|
+
if (correctValue && Math.abs(angle) >= 360) {
|
|
605
605
|
angle %= 360;
|
|
606
606
|
}
|
|
607
607
|
if (Math.abs(angle) < anglePrecision) {
|
|
608
608
|
angle = 0;
|
|
609
609
|
}
|
|
610
|
-
if (angle < 0) {
|
|
610
|
+
if (correctValue && angle < 0) {
|
|
611
611
|
angle += 360;
|
|
612
612
|
}
|
|
613
613
|
return angle;
|
|
@@ -7,9 +7,9 @@ import { getColorComponents } from './components.js';
|
|
|
7
7
|
* @returns
|
|
8
8
|
*/
|
|
9
9
|
function getColorSpace(color) {
|
|
10
|
-
if (!("chi" in color)) {
|
|
11
|
-
|
|
12
|
-
}
|
|
10
|
+
// if (!("chi" in color)) {
|
|
11
|
+
// return "rgb";
|
|
12
|
+
// }
|
|
13
13
|
let name;
|
|
14
14
|
const components = getColorComponents(color);
|
|
15
15
|
if (components == null || components.length < 3) {
|
|
@@ -1,19 +1,16 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { walkValues } from '../../../ast/walk.js';
|
|
1
|
+
import { ColorType, EnumToken } from '../../../ast/types.js';
|
|
3
2
|
import { COLORS_NAMES } from '../../constants.js';
|
|
4
3
|
import { expandHexValue } from '../hex.js';
|
|
5
|
-
import { isColor, parseColor } from '../../syntax.js';
|
|
6
4
|
import { equalsIgnoreCase } from '../../../parser/utils/text.js';
|
|
7
5
|
|
|
8
6
|
function getColorComponents(token) {
|
|
9
|
-
if (token.typ === EnumToken.IdenTokenType) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
7
|
+
// if (token.typ === EnumToken.IdenTokenType) {
|
|
8
|
+
// if (isColor(token)) {
|
|
9
|
+
// parseColor(token);
|
|
10
|
+
// } else {
|
|
11
|
+
// return null;
|
|
12
|
+
// }
|
|
13
|
+
// }
|
|
17
14
|
if (token.kin == ColorType.HEX || token.kin == ColorType.LIT) {
|
|
18
15
|
if (equalsIgnoreCase('currentcolor', token.val)) {
|
|
19
16
|
return null;
|
|
@@ -37,23 +34,25 @@ function getColorComponents(token) {
|
|
|
37
34
|
].includes(child.typ)) {
|
|
38
35
|
continue;
|
|
39
36
|
}
|
|
40
|
-
if (child.typ === EnumToken.IdenTokenType && isColor(child)) {
|
|
41
|
-
|
|
42
|
-
}
|
|
37
|
+
// if (child.typ === EnumToken.IdenTokenType && isColor(child)) {
|
|
38
|
+
// parseColor(child);
|
|
39
|
+
// }
|
|
43
40
|
if (child.typ === EnumToken.FunctionTokenType ||
|
|
44
41
|
child.typ === EnumToken.WildCardFunctionTokenType ||
|
|
45
42
|
child.typ === EnumToken.MathFunctionTokenType) {
|
|
46
43
|
if ("var" == child.val.toLowerCase()) {
|
|
47
44
|
return null;
|
|
48
45
|
}
|
|
49
|
-
else {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
46
|
+
// else {
|
|
47
|
+
// for (const { value } of walkValues((child as FunctionToken).chi)) {
|
|
48
|
+
// if (
|
|
49
|
+
// value.typ == EnumToken.WildCardFunctionTokenDefType &&
|
|
50
|
+
// "var" === (value as FunctionToken).val.toLowerCase()
|
|
51
|
+
// ) {
|
|
52
|
+
// return null;
|
|
53
|
+
// }
|
|
54
|
+
// }
|
|
55
|
+
// }
|
|
57
56
|
}
|
|
58
57
|
if (child.typ == EnumToken.ColorTokenType && equalsIgnoreCase("currentcolor", child.val)) {
|
|
59
58
|
return null;
|
|
@@ -6,27 +6,28 @@
|
|
|
6
6
|
*/
|
|
7
7
|
// A is m x n. B is n x p. product is m x p.
|
|
8
8
|
function multiplyMatrices(A, B) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
}
|
|
9
|
+
// if (!Array.isArray(A[0])) {
|
|
10
|
+
// // A is vector, convert to [[a, b, c, ...]]
|
|
11
|
+
// A = <number[][]>[A];
|
|
12
|
+
// }
|
|
14
13
|
if (!Array.isArray(B[0])) {
|
|
15
14
|
// B is vector, convert to [[a], [b], [c], ...]]
|
|
16
15
|
B = B.map((x) => [x]);
|
|
17
16
|
}
|
|
18
17
|
let p = B[0].length;
|
|
19
18
|
let B_cols = B[0].map((_, i) => B.map((x) => x[i])); // transpose B
|
|
19
|
+
// @ts-expect-error
|
|
20
20
|
let product = A.map((row) => B_cols.map((col) => {
|
|
21
|
-
if (!Array.isArray(row)) {
|
|
22
|
-
|
|
23
|
-
}
|
|
21
|
+
// if (!Array.isArray(row)) {
|
|
22
|
+
// return col.reduce((a: number, c: number) => a + c * row, 0);
|
|
23
|
+
// }
|
|
24
24
|
return row.reduce((a, c, i) => a + c * (col[i] || 0), 0);
|
|
25
25
|
}));
|
|
26
|
-
if (m === 1) {
|
|
27
|
-
|
|
28
|
-
}
|
|
26
|
+
// if (m === 1) {
|
|
27
|
+
// product = <number[]>product[0]; // Avoid [[a, b, c, ...]]
|
|
28
|
+
// }
|
|
29
29
|
if (p === 1) {
|
|
30
|
+
// @ts-expect-error
|
|
30
31
|
return product.map((x) => x[0]); // Avoid [[a], [b], [c], ...]]
|
|
31
32
|
}
|
|
32
33
|
return product;
|