@tbela99/css-parser 1.4.9 → 1.4.10
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 +4 -0
- package/README.md +1 -1
- package/dist/index-umd-web.js +89 -129
- package/dist/index.cjs +89 -129
- package/dist/index.d.ts +1684 -1684
- package/dist/lib/ast/math/math.js +2 -1
- package/dist/lib/ast/transform/compute.js +2 -1
- package/dist/lib/ast/types.js +1 -66
- package/dist/lib/renderer/render.js +6 -47
- package/dist/lib/syntax/color/color.js +3 -21
- package/dist/lib/syntax/color/utils/distance.js +2 -1
- package/dist/lib/syntax/syntax.js +86 -2
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { multiply, toZero, identity } from './utils.js';
|
|
2
|
-
import { EnumToken
|
|
2
|
+
import { EnumToken } from '../types.js';
|
|
3
3
|
import { stripCommaToken } from '../../validation/utils/list.js';
|
|
4
4
|
import { translateX, translateY, translateZ, translate, translate3d } from './translate.js';
|
|
5
5
|
import { getNumber, getAngle } from '../../syntax/color/color.js';
|
|
@@ -10,6 +10,7 @@ import { skew, skewX, skewY } from './skew.js';
|
|
|
10
10
|
import { serialize, matrix } from './matrix.js';
|
|
11
11
|
import { perspective } from './perspective.js';
|
|
12
12
|
import { transformFunctions } from '../../syntax/constants.js';
|
|
13
|
+
import { length2Px } from '../../syntax/syntax.js';
|
|
13
14
|
|
|
14
15
|
function compute(transformLists) {
|
|
15
16
|
transformLists = transformLists.slice();
|
package/dist/lib/ast/types.js
CHANGED
|
@@ -976,70 +976,5 @@ var ModuleScopeEnumOptions;
|
|
|
976
976
|
*/
|
|
977
977
|
ModuleScopeEnumOptions[ModuleScopeEnumOptions["Shortest"] = 512] = "Shortest";
|
|
978
978
|
})(ModuleScopeEnumOptions || (ModuleScopeEnumOptions = {}));
|
|
979
|
-
// https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Values_and_units#absolute_length_units
|
|
980
|
-
/**
|
|
981
|
-
* Convert length to px
|
|
982
|
-
* @param value
|
|
983
|
-
* @returns
|
|
984
|
-
*/
|
|
985
|
-
function length2Px(value) {
|
|
986
|
-
let result = null;
|
|
987
|
-
if (value.typ == EnumToken.NumberTokenType) {
|
|
988
|
-
result = +value.val;
|
|
989
|
-
}
|
|
990
|
-
else {
|
|
991
|
-
switch (value.unit) {
|
|
992
|
-
case "cm":
|
|
993
|
-
// @ts-ignore
|
|
994
|
-
result = value.val * 37.8;
|
|
995
|
-
break;
|
|
996
|
-
case "mm":
|
|
997
|
-
// @ts-ignore
|
|
998
|
-
result = value.val * 3.78;
|
|
999
|
-
break;
|
|
1000
|
-
case "Q":
|
|
1001
|
-
// @ts-ignore
|
|
1002
|
-
result = (value.val * 37.8) / 40;
|
|
1003
|
-
break;
|
|
1004
|
-
case "in":
|
|
1005
|
-
// @ts-ignore
|
|
1006
|
-
result = value.val / 96;
|
|
1007
|
-
break;
|
|
1008
|
-
case "pc":
|
|
1009
|
-
// @ts-ignore
|
|
1010
|
-
result = value.val / 16;
|
|
1011
|
-
break;
|
|
1012
|
-
case "pt":
|
|
1013
|
-
// @ts-ignore
|
|
1014
|
-
result = (value.val * 4) / 3;
|
|
1015
|
-
break;
|
|
1016
|
-
case "px":
|
|
1017
|
-
result = +value.val;
|
|
1018
|
-
break;
|
|
1019
|
-
}
|
|
1020
|
-
}
|
|
1021
|
-
return isNaN(result) ? null : result;
|
|
1022
|
-
}
|
|
1023
|
-
/**
|
|
1024
|
-
* minify number
|
|
1025
|
-
* @param val
|
|
1026
|
-
*/
|
|
1027
|
-
function minifyNumber(val) {
|
|
1028
|
-
val = String(val);
|
|
1029
|
-
if (val === "0") {
|
|
1030
|
-
return "0";
|
|
1031
|
-
}
|
|
1032
|
-
const chr = val.charAt(0);
|
|
1033
|
-
if (chr == "-") {
|
|
1034
|
-
const slice = val.slice(0, 2);
|
|
1035
|
-
if (slice == "-0") {
|
|
1036
|
-
return val.length == 2 ? "0" : "-" + val.slice(2);
|
|
1037
|
-
}
|
|
1038
|
-
}
|
|
1039
|
-
if (chr == "0") {
|
|
1040
|
-
return val.slice(1);
|
|
1041
|
-
}
|
|
1042
|
-
return val;
|
|
1043
|
-
}
|
|
1044
979
|
|
|
1045
|
-
export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, ModuleScopeEnumOptions, SyntaxValidationResult, ValidationLevel
|
|
980
|
+
export { ColorType, EnumAstNodeStatus, EnumToken, ModuleCaseTransformEnum, ModuleScopeEnumOptions, SyntaxValidationResult, ValidationLevel };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { convertColor,
|
|
1
|
+
import { convertColor, getAngle } from '../syntax/color/color.js';
|
|
2
2
|
import { reduceHexValue } from '../syntax/color/hex.js';
|
|
3
|
-
import { EnumToken,
|
|
3
|
+
import { EnumToken, ColorType } from '../ast/types.js';
|
|
4
4
|
import { expand } from '../ast/expand.js';
|
|
5
5
|
import { SourceMap } from './sourcemap/sourcemap.js';
|
|
6
6
|
import { pseudoElements, urlTokenMatcher, PARENT, tokensfuncSet, LOC, colorPrecision } from '../syntax/constants.js';
|
|
7
|
-
import { reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor } from '../syntax/syntax.js';
|
|
7
|
+
import { minifyNumber, reducegradientBackgroundPosition, reduceConicColorStops, reduceColorStops, parseColor, toPrecisionAngle, toPrecisionValue } from '../syntax/syntax.js';
|
|
8
8
|
import { equalsIgnoreCase } from '../parser/utils/text.js';
|
|
9
9
|
import { toDegrees } from '../parser/utils/angle.js';
|
|
10
10
|
import { LineMap } from '../parser/linesmap.js';
|
|
@@ -131,10 +131,10 @@ function updateSourceMap(node, options, cache, sourcemap, sourceLocation, linesM
|
|
|
131
131
|
let srcId = node[LOC]?.srcId ?? 0;
|
|
132
132
|
let sourceFileName = options.sourcesMap?.get(srcId)?.getFileName?.() || null;
|
|
133
133
|
if (sourceFileName != null && options.output != null) {
|
|
134
|
-
if (
|
|
135
|
-
cache
|
|
134
|
+
if (cache[sourceFileName] == null) {
|
|
135
|
+
cache[sourceFileName] = options.resolve(sourceFileName, dirname(options.output)).relative;
|
|
136
136
|
}
|
|
137
|
-
sourceFileName = cache
|
|
137
|
+
sourceFileName = cache[sourceFileName];
|
|
138
138
|
}
|
|
139
139
|
// @ts-ignore
|
|
140
140
|
sourcemap.add(...linesMap.getOffsets(sourceLocation.end), srcId,
|
|
@@ -335,46 +335,6 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
335
335
|
return acc + renderValue(curr, options, cache, reducer, errors);
|
|
336
336
|
};
|
|
337
337
|
}
|
|
338
|
-
// if (token.typ == EnumToken.FunctionTokenType && colorsFunc.includes((token as FunctionToken).val)) {
|
|
339
|
-
// if (isColor(token)) {
|
|
340
|
-
// // @ts-ignore
|
|
341
|
-
// token.typ = EnumToken.ColorTokenType;
|
|
342
|
-
// if (
|
|
343
|
-
// // @ts-ignore
|
|
344
|
-
// (token as ColorToken)!.chi[0]!.typ == EnumToken.IdenTokenType &&
|
|
345
|
-
// // @ts-ignore
|
|
346
|
-
// ((token as ColorToken)!.chi[0] as IdentToken).val == "from"
|
|
347
|
-
// ) {
|
|
348
|
-
// // @ts-ignore
|
|
349
|
-
// (<ColorToken>token).cal = "rel";
|
|
350
|
-
// } else {
|
|
351
|
-
// // @ts-ignore
|
|
352
|
-
// if (
|
|
353
|
-
// (token as ColorToken).val == "color-mix" &&
|
|
354
|
-
// (token as ColorToken).chi?.[0]?.typ == EnumToken.IdenTokenType &&
|
|
355
|
-
// ((token as ColorToken).chi?.[0] as IdentToken).val == "in"
|
|
356
|
-
// ) {
|
|
357
|
-
// // @ts-ignore
|
|
358
|
-
// (<ColorToken>token).cal = "mix";
|
|
359
|
-
// } else {
|
|
360
|
-
// // @ts-ignore
|
|
361
|
-
// if ((token as ColorToken).val == "color") {
|
|
362
|
-
// // @ts-ignore
|
|
363
|
-
// token.cal = "col";
|
|
364
|
-
// }
|
|
365
|
-
// // @ts-ignore
|
|
366
|
-
// (token as ColorToken).chi = (token as ColorToken).chi!.filter(
|
|
367
|
-
// (t: Token) =>
|
|
368
|
-
// ![
|
|
369
|
-
// EnumToken.WhitespaceTokenType,
|
|
370
|
-
// EnumToken.CommaTokenType,
|
|
371
|
-
// EnumToken.CommentTokenType,
|
|
372
|
-
// ].includes(t.typ),
|
|
373
|
-
// );
|
|
374
|
-
// }
|
|
375
|
-
// }
|
|
376
|
-
// }
|
|
377
|
-
// }
|
|
378
338
|
switch (token.typ) {
|
|
379
339
|
case EnumToken.FunctionTokenDefType:
|
|
380
340
|
case EnumToken.UrlFunctionTokenDefType:
|
|
@@ -1169,7 +1129,6 @@ function renderValue(token, options = {}, cache = Object.create(null), reducer,
|
|
|
1169
1129
|
if (token.typ == EnumToken.ResolutionTokenType && unit == "dppx") {
|
|
1170
1130
|
unit = "x";
|
|
1171
1131
|
}
|
|
1172
|
-
// @ts-ignore
|
|
1173
1132
|
return val.includes("/") ? val.replace("/", unit + "/") : minifyNumber(toPrecisionValue(val)) + unit;
|
|
1174
1133
|
case EnumToken.FlexTokenType:
|
|
1175
1134
|
case EnumToken.PercentageTokenType:
|
|
@@ -11,7 +11,7 @@ import { oklch2srgbvalues, lch2srgbvalues, oklab2srgbvalues, lab2srgbvalues, cmy
|
|
|
11
11
|
import { prophotorgb2srgbvalues, srgb2prophotorgbvalues } from './prophotorgb.js';
|
|
12
12
|
import { rec20202srgb, srgb2rec2020values } from './rec2020.js';
|
|
13
13
|
import { srgb2xyz_d65, srgb2xyz } from './xyz.js';
|
|
14
|
-
import { lp32srgbvalues, p32srgbvalues, srgb2p3values } from './p3.js';
|
|
14
|
+
import { lp32srgbvalues, p32srgbvalues, srgb2lp3values, srgb2p3values } from './p3.js';
|
|
15
15
|
import { xyzd502srgb } from './xyzd50.js';
|
|
16
16
|
import { colorMix } from './color-mix.js';
|
|
17
17
|
import { reduceHexValue, rgb2HexToken, color2HexToken, lch2HexToken, lab2HexToken, oklch2HexToken, oklab2HexToken, cmyk2HexToken, hwb2HexToken, hsl2HexToken } from './hex.js';
|
|
@@ -19,7 +19,7 @@ import { parseRelativeColorComponents } from './relative-color.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 { LOC, colorFuncColorSpace
|
|
22
|
+
import { LOC, colorFuncColorSpace } 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';
|
|
@@ -602,23 +602,5 @@ function getAngle(token) {
|
|
|
602
602
|
// @ts-ignore
|
|
603
603
|
return token.val / 360;
|
|
604
604
|
}
|
|
605
|
-
function toPrecisionValue(value, precision = colorPrecision) {
|
|
606
|
-
const div = Math.pow(10, precision);
|
|
607
|
-
value = Math.round(value * div) / div;
|
|
608
|
-
return Math.abs(value) < epsilon ? 0 : value;
|
|
609
|
-
}
|
|
610
|
-
function toPrecisionAngle(angle, precision = colorPrecision, correctValue = true) {
|
|
611
|
-
angle = toPrecisionValue(angle, precision);
|
|
612
|
-
if (correctValue && Math.abs(angle) >= 360) {
|
|
613
|
-
angle %= 360;
|
|
614
|
-
}
|
|
615
|
-
if (Math.abs(angle) < anglePrecision) {
|
|
616
|
-
angle = 0;
|
|
617
|
-
}
|
|
618
|
-
if (correctValue && angle < 0) {
|
|
619
|
-
angle += 360;
|
|
620
|
-
}
|
|
621
|
-
return angle;
|
|
622
|
-
}
|
|
623
605
|
|
|
624
|
-
export { cmyk2colorToken, color2colorToken, color2srgbvalues, convertColor, getAngle, getNumber, hex2colorToken, hsl2colorToken, hwb2colorToken, lab2colorToken, lch2colorToken, minmax, oklab2colorToken, oklch2colorToken, rgb2colorToken
|
|
606
|
+
export { cmyk2colorToken, color2colorToken, color2srgbvalues, convertColor, getAngle, getNumber, hex2colorToken, hsl2colorToken, hwb2colorToken, lab2colorToken, lch2colorToken, minmax, oklab2colorToken, oklch2colorToken, rgb2colorToken };
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { convertColor
|
|
1
|
+
import { convertColor } from '../color.js';
|
|
2
2
|
import { getOKLABComponents } from '../oklab.js';
|
|
3
3
|
import { ColorType } from '../../../ast/types.js';
|
|
4
4
|
import { colorDistancePrecision } from '../../constants.js';
|
|
5
|
+
import { toPrecisionValue } from '../../syntax.js';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Calculate the distance between two okLab colors.
|
|
@@ -8,7 +8,7 @@ import { trimArray } from '../validation/match.js';
|
|
|
8
8
|
import { splitTokenList } from '../validation/utils/list.js';
|
|
9
9
|
import { getColorSpace } from './color/utils/colorspace.js';
|
|
10
10
|
import { getColorComponents } from './color/utils/components.js';
|
|
11
|
-
import { nonStandardColors, systemColors, deprecatedSystemColors, COLORS_NAMES, colorsFunc, colorFuncColorSpace } from './constants.js';
|
|
11
|
+
import { nonStandardColors, systemColors, deprecatedSystemColors, COLORS_NAMES, colorsFunc, colorFuncColorSpace, colorPrecision, epsilon, anglePrecision } from './constants.js';
|
|
12
12
|
import { getSyntaxConfig } from '../validation/config.js';
|
|
13
13
|
|
|
14
14
|
// https://www.w3.org/TR/CSS21/syndata.html#syntax
|
|
@@ -1140,5 +1140,89 @@ function isWhiteSpace(codepoint) {
|
|
|
1140
1140
|
codepoint == 0x2028 ||
|
|
1141
1141
|
codepoint == 0x2029);
|
|
1142
1142
|
}
|
|
1143
|
+
// https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics/Values_and_units#absolute_length_units
|
|
1144
|
+
/**
|
|
1145
|
+
* Convert length to px
|
|
1146
|
+
* @param value
|
|
1147
|
+
* @returns
|
|
1148
|
+
*/
|
|
1149
|
+
function length2Px(value) {
|
|
1150
|
+
let result = null;
|
|
1151
|
+
if (value.typ == EnumToken.NumberTokenType) {
|
|
1152
|
+
result = +value.val;
|
|
1153
|
+
}
|
|
1154
|
+
else {
|
|
1155
|
+
switch (value.unit) {
|
|
1156
|
+
case "cm":
|
|
1157
|
+
// @ts-ignore
|
|
1158
|
+
result = value.val * 37.8;
|
|
1159
|
+
break;
|
|
1160
|
+
case "mm":
|
|
1161
|
+
// @ts-ignore
|
|
1162
|
+
result = value.val * 3.78;
|
|
1163
|
+
break;
|
|
1164
|
+
case "Q":
|
|
1165
|
+
// @ts-ignore
|
|
1166
|
+
result = (value.val * 37.8) / 40;
|
|
1167
|
+
break;
|
|
1168
|
+
case "in":
|
|
1169
|
+
// @ts-ignore
|
|
1170
|
+
result = value.val / 96;
|
|
1171
|
+
break;
|
|
1172
|
+
case "pc":
|
|
1173
|
+
// @ts-ignore
|
|
1174
|
+
result = value.val / 16;
|
|
1175
|
+
break;
|
|
1176
|
+
case "pt":
|
|
1177
|
+
// @ts-ignore
|
|
1178
|
+
result = (value.val * 4) / 3;
|
|
1179
|
+
break;
|
|
1180
|
+
case "px":
|
|
1181
|
+
result = +value.val;
|
|
1182
|
+
break;
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
return isNaN(result) ? null : result;
|
|
1186
|
+
}
|
|
1187
|
+
/**
|
|
1188
|
+
* minify number
|
|
1189
|
+
* @param val
|
|
1190
|
+
*/
|
|
1191
|
+
function minifyNumber(val) {
|
|
1192
|
+
val = String(toPrecisionValue(val));
|
|
1193
|
+
if (val === "0") {
|
|
1194
|
+
return "0";
|
|
1195
|
+
}
|
|
1196
|
+
const chr = val.charAt(0);
|
|
1197
|
+
if (chr == "-") {
|
|
1198
|
+
const slice = val.slice(0, 2);
|
|
1199
|
+
if (slice == "-0") {
|
|
1200
|
+
return val.length == 2 ? "0" : "-" + val.slice(2);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
if (chr == "0") {
|
|
1204
|
+
return val.slice(1);
|
|
1205
|
+
}
|
|
1206
|
+
return val;
|
|
1207
|
+
}
|
|
1208
|
+
function toPrecisionValue(value, precision = colorPrecision) {
|
|
1209
|
+
const div = Math.pow(10, precision);
|
|
1210
|
+
// @ts-ignore
|
|
1211
|
+
value = Math.round(value * div) / div;
|
|
1212
|
+
return Math.abs(value) < epsilon ? 0 : value;
|
|
1213
|
+
}
|
|
1214
|
+
function toPrecisionAngle(angle, precision = colorPrecision, correctValue = true) {
|
|
1215
|
+
angle = toPrecisionValue(angle, precision);
|
|
1216
|
+
if (correctValue && Math.abs(angle) >= 360) {
|
|
1217
|
+
angle %= 360;
|
|
1218
|
+
}
|
|
1219
|
+
if (Math.abs(angle) < anglePrecision) {
|
|
1220
|
+
angle = 0;
|
|
1221
|
+
}
|
|
1222
|
+
if (correctValue && angle < 0) {
|
|
1223
|
+
angle += 360;
|
|
1224
|
+
}
|
|
1225
|
+
return angle;
|
|
1226
|
+
}
|
|
1143
1227
|
|
|
1144
|
-
export { dimensionUnits, isAngle, isColor, isDigit, isFlex, isFrequency, isFunction, isHash, isHexColor, isIdent, isIdentCodepoint, isIdentColor, isIdentStart, isLength, isLetter, isNewLine, isNumber, isPercentage, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isWhiteSpace, parseColor, parseDimension, pseudoAliasMap, reduceColorStops, reduceConicColorStops, reducegradientBackgroundPosition, renamedStandardProperties };
|
|
1228
|
+
export { dimensionUnits, isAngle, isColor, isDigit, isFlex, isFrequency, isFunction, isHash, isHexColor, isIdent, isIdentCodepoint, isIdentColor, isIdentStart, isLength, isLetter, isNewLine, isNumber, isPercentage, isPolarColorspace, isPseudo, isRectangularOrthogonalColorspace, isResolution, isTime, isWhiteSpace, length2Px, minifyNumber, parseColor, parseDimension, pseudoAliasMap, reduceColorStops, reduceConicColorStops, reducegradientBackgroundPosition, renamedStandardProperties, toPrecisionAngle, toPrecisionValue };
|