Skip to content

Commit

Permalink
Update css-calc.js
Browse files Browse the repository at this point in the history
  • Loading branch information
asamuzaK committed Dec 27, 2024
1 parent 0d8f129 commit 5327323
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/js/css-calc.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ const {
CloseParen: PAREN_CLOSE, Comment: COMMENT, Dimension: DIM, EOF,
Function: FUNC, OpenParen: PAREN_OPEN, Whitespace: W_SPACE
} = TokenType;
const DEG_HALF = 180;
const HEX = 16;

/* regexp */
Expand Down Expand Up @@ -174,20 +173,22 @@ export const cssCalc = (value, opt = {}) => {
return cachedResults.get(cacheKey);
}
}
const tokens = tokenize({ css: value });
const values = parseTokens(tokens, opt);
let resolvedValue = calc(values.join(''));
let resolvedValue;
if (dimension) {
const tokens = tokenize({ css: value });
const values = parseTokens(tokens, opt);
resolvedValue = calc(values.join(''), {
toCanonicalUnits: true
});
} else {
resolvedValue = calc(value, {
toCanonicalUnits: true
});
}
if (REG_START_MATH_VAR.test(value)) {
if (REG_LENGTH.test(resolvedValue)) {
const [, val, unit] = REG_LENGTH.exec(resolvedValue);
// FIXME:
// workaround for https://github.com/csstools/postcss-plugins/issues/1544
if (unit === 'rad') {
resolvedValue =
`${roundToPrecision(Number(val) * DEG_HALF / Math.PI, HEX)}deg`;
} else {
resolvedValue = `${roundToPrecision(Number(val), HEX)}${unit}`;
}
resolvedValue = `${roundToPrecision(Number(val), HEX)}${unit}`;
} else if (resolvedValue.includes('NaN * 1rad')) {
resolvedValue = resolvedValue.replace('NaN * 1rad', 'NaN * 1deg');
}
Expand Down

0 comments on commit 5327323

Please sign in to comment.