fix(core): read a font shorthand whose size nests parentheses

`font: calc(var(--size) * 1px)/1 "X"` cut at the first closing parenthesis, so a
bad family name failed the render.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Miao Yang
2026-11-02 11:27:54 +08:00
co-authored by Claude Opus 5.5
parent 8f12457056
commit d1362f4283
2 changed files with 17 additions and 4 deletions
@@ -37,6 +37,7 @@ describe("a bundled family named only in the font shorthand loads", () => {
["style, variant, weight, size", 'p { font: italic small-caps bold 1.5rem "Inter", serif; }'],
["a keyword size", "p { font: bold large Inter; }"],
["a calc() size", "p { font: calc(10px + 2vw) Inter; }"],
["a nested calc() size", 'p { font: calc(var(--size) * 1px)/1 "Inter"; }'],
])("%s", async (_case, css) => {
expect(await inject(css)).toMatch(/@font-face\s*\{[^}]*font-family:\s*"Inter"/i);
});
+16 -4
View File
@@ -267,11 +267,23 @@ const FONT_SIZE_KEYWORD =
/^(?:xx-small|x-small|small|medium|large|x-large|xx-large|xxx-large|smaller|larger)$/i;
const FONT_SIZE_TOKEN = /^(?:\d*\.?\d+[a-z%]+|(?:calc|min|max|clamp|var)\(.*\))(?:\/\S+)?$/i;
function maskParens(value: string): string {
let depth = 0;
return value.replace(/[\s\S]/g, (char) => {
if (char === "(") depth += 1;
const inside = depth > 0;
if (char === ")") depth -= 1;
return inside ? "_" : char;
});
}
function fontShorthandFamilies(value: string): string {
for (const token of value.matchAll(/(?:[^\s(]|\([^)]*\))+/g)) {
const size = token[0].split("/")[0] ?? "";
if (FONT_SIZE_KEYWORD.test(size) || FONT_SIZE_TOKEN.test(token[0])) {
return value.slice(token.index + token[0].length).trim();
for (const { 0: masked, index } of maskParens(value).matchAll(/\S+/g)) {
const end = index + masked.length;
const token = value.slice(index, end);
const size = token.split("/")[0] ?? "";
if (FONT_SIZE_KEYWORD.test(size) || FONT_SIZE_TOKEN.test(token)) {
return value.slice(end).trim();
}
}
return "";