From d814c2601c095d477cba433d530b8ba2f10edc00 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 22:28:55 +0000 Subject: [PATCH 1/2] fix(web): theme CSS shorthands with var() can no longer load remote files Theme CSS must not fetch, but the client guard read each longhand's value, and a shorthand holding var() leaves its longhands reading "" until substitution. `background: var(--none, url(https://example.com/h))` passed the guard and the browser requested the URL. The guard now also judges pending longhands. While the block's cssText shows the shorthand, that text decides. Once a later declaration overrides one longhand the browser hides the text, so the theme's source decides: declarations that could fetch are split out, the browser parses each one alone, and only the longhands such a shorthand leaves pending are removed from blocks that hide them. A harmless overridden shorthand stays, whatever escapes or fetches the rest of the sheet holds. Co-authored-by: elias1218 <69803556+elias1218@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 --- apps/web/src/themeCss.browser.test.ts | 32 ++++++++++++ apps/web/src/themeCss.ts | 71 ++++++++++++++++++++++++--- packages/shared/src/themeCss.test.ts | 36 +++++++++++++- packages/shared/src/themeCss.ts | 58 ++++++++++++++++++++++ 4 files changed, 189 insertions(+), 8 deletions(-) diff --git a/apps/web/src/themeCss.browser.test.ts b/apps/web/src/themeCss.browser.test.ts index 28fccece4c5c5..6cc423ebaa57c 100644 --- a/apps/web/src/themeCss.browser.test.ts +++ b/apps/web/src/themeCss.browser.test.ts @@ -24,6 +24,9 @@ it("applies theme CSS and drops every rule that would load a remote file", () => mask-image: v\\61r(--none, u\\72l(https://example.com/e)); @media (min-width: 0px) { cursor: url(https://example.com/c), auto; } } + [data-theme-css-probe] { background: var(--none, url(https://example.com/h)); } + @keyframes shorthand { to { border-image: var(--none, url(https://example.com/i)); } } + [data-workspace-area="conversation"] { background: var(--theme-background-image) center / cover no-repeat; } `); const style = getComputedStyle(probe); @@ -36,8 +39,37 @@ it("applies theme CSS and drops every rule that would load a remote file", () => Array.from(sheet.cssRules, (rule) => rule.cssText), ); expect(kept.join("\n")).not.toContain("example.com"); + expect(kept.join("\n")).toContain( + "background: var(--theme-background-image) center / cover no-repeat;", + ); expect(style.cursor).toBe("auto"); + // A later longhand hides the shorthand's text from CSSOM, and the browser still loads its url(). + applyThemeCss(` + [data-theme-css-probe] { mask: v\\61r(--none, u\\72l(https://example.com/k)); mask-size: auto; } + [data-theme-css-probe] { list-style: env(t3-none, url(https://example.com/l)); list-style-type: disc; } + @keyframes hidden { to { background: var(--none, url(https://example.com/m)); background-color: red; } } + [data-theme-css-probe] { border-image: var(--none, url(https://example.com/n)); border-image-slice: 1; } + [data-theme-css-probe] { animation: hidden 1s forwards paused; animation-delay: -1s; } + `); + expect(style.maskImage).toBe("none"); + expect(style.listStyleImage).toBe("none"); + expect(style.borderImageSource).toBe("none"); + expect(style.backgroundImage).toBe("none"); + expect(style.backgroundColor).toBe("rgb(255, 0, 0)"); + + // The same shape without a fetch stays, whatever the rest of the sheet holds. + applyThemeCss(` + [data-theme-css-probe] { + --edge: 4px solid red; border: var(--edge); border-left-width: 0; + background: var(--none, linear-gradient(red, blue)); background-color: red; + } + .unused { c\\6flor: red; b\\61 ckground: var(--none); cursor: url(https://example.com/x), auto; } + `); + expect(style.borderTopWidth).toBe("4px"); + expect(style.borderLeftWidth).toBe("0px"); + expect(style.backgroundImage).toContain("linear-gradient"); + applyThemeCss(undefined); expect(getComputedStyle(probe).color).not.toBe("rgb(1, 2, 3)"); }); diff --git a/apps/web/src/themeCss.ts b/apps/web/src/themeCss.ts index 437182ce6a191..06fc1376f88cf 100644 --- a/apps/web/src/themeCss.ts +++ b/apps/web/src/themeCss.ts @@ -1,4 +1,4 @@ -import { themeCssValueFetches } from "@t3tools/shared/themeCss"; +import { themeCssFetchCandidates, themeCssValueFetches } from "@t3tools/shared/themeCss"; // Theme CSS comes from imported files and from agents, so it must not fetch: a // url() on an attribute selector can send what is on screen to another host. @@ -14,7 +14,62 @@ type RuleParent = Pick; const isRuleParent = (rule: CSSRule): rule is CSSRule & RuleParent => "cssRules" in rule && "deleteRule" in rule && !(rule instanceof CSSKeyframesRule); -function stripDeclarations(rule: CSSRule): boolean { +// A shorthand holding var() leaves its longhands reading "" until substitution. +function pendingLonghands(style: CSSStyleDeclaration): string[] { + return Array.from(style).filter( + (property) => !property.startsWith("--") && style.getPropertyValue(property) === "", + ); +} + +/** Every declaration block the browser finds in `text`, parsed apart from the theme sheet. */ +function parsedStyles(text: string): CSSStyleDeclaration[] { + const styles: CSSStyleDeclaration[] = []; + const collect = (rules: CSSRuleList) => { + for (const rule of rules) { + if ("style" in rule && rule.style instanceof CSSStyleDeclaration) styles.push(rule.style); + if ("cssRules" in rule && rule.cssRules instanceof CSSRuleList) collect(rule.cssRules); + } + }; + const sheet = new CSSStyleSheet(); + sheet.replaceSync(text); + collect(sheet.cssRules); + return styles; +} + +/** + * The pending longhands of `style` that can fetch. cssText shows a pending shorthand's text until + * a later declaration overrides one of its longhands. After that only the theme's source has the + * text, so a longhand that does not come back from parsing cssText is judged by `hiddenFetches`. + */ +function fetchingPendingLonghands( + style: CSSStyleDeclaration, + hiddenFetches: (longhand: string) => boolean, +): string[] { + const pending = pendingLonghands(style); + if (pending.length === 0) return pending; + const text = style.cssText; + if (themeCssValueFetches(text)) return pending; + const suspects = pending.filter(hiddenFetches); + if (suspects.length === 0) return suspects; + const shown = new Set(parsedStyles(`x{${text}}`).flatMap(pendingLonghands)); + return suspects.filter((longhand) => !shown.has(longhand)); +} + +/** + * The longhands that a var() shorthand able to fetch leaves pending anywhere in the CSS. The + * browser parses each candidate alone, as rules and as a block, so escapes resolve as they will. + */ +function fetchingShorthandLonghands(css: string): Set { + const longhands = new Set(); + for (const candidate of themeCssFetchCandidates(css)) { + for (const style of [...parsedStyles(candidate), ...parsedStyles(`x{${candidate}}`)]) { + for (const longhand of fetchingPendingLonghands(style, () => true)) longhands.add(longhand); + } + } + return longhands; +} + +function stripDeclarations(rule: CSSRule, hiddenFetches: ReadonlySet): boolean { if (!("style" in rule) || !(rule.style instanceof CSSStyleDeclaration)) return false; for (let item = rule.style.length - 1; item >= 0; item--) { const property = rule.style.item(item); @@ -22,19 +77,21 @@ function stripDeclarations(rule: CSSRule): boolean { rule.style.removeProperty(property); } } + const fetching = fetchingPendingLonghands(rule.style, (longhand) => hiddenFetches.has(longhand)); + for (const longhand of fetching) rule.style.removeProperty(longhand); return true; } /** Keeps only rule shapes it can check, so a new at-rule that fetches is dropped by default. */ -function stripFetches(parent: RuleParent): void { +function stripFetches(parent: RuleParent, hiddenFetches: ReadonlySet): void { for (let index = parent.cssRules.length - 1; index >= 0; index--) { const rule = parent.cssRules[index]; if (!rule) continue; - const hasStyle = stripDeclarations(rule); + const hasStyle = stripDeclarations(rule, hiddenFetches); if (rule instanceof CSSKeyframesRule) { - for (const frame of rule.cssRules) stripDeclarations(frame); + for (const frame of rule.cssRules) stripDeclarations(frame, hiddenFetches); } else if (isRuleParent(rule)) { - stripFetches(rule); + stripFetches(rule, hiddenFetches); } else if (rule instanceof CSSPropertyRule) { // A registered property's initial value is used wherever var() reads it. if (themeCssValueFetches(rule.initialValue ?? "")) parent.deleteRule(index); @@ -60,5 +117,5 @@ export function applyThemeCss(css: string | undefined): void { document.adoptedStyleSheets = [...document.adoptedStyleSheets, themeSheet]; } themeSheet.replaceSync(next); - stripFetches(themeSheet); + stripFetches(themeSheet, fetchingShorthandLonghands(next)); } diff --git a/packages/shared/src/themeCss.test.ts b/packages/shared/src/themeCss.test.ts index 38cc33a793dbd..c1c7bc30845d7 100644 --- a/packages/shared/src/themeCss.test.ts +++ b/packages/shared/src/themeCss.test.ts @@ -1,6 +1,10 @@ import { describe, expect, it } from "vite-plus/test"; -import { themeCssLoadsRemoteFiles, themeCssValueFetches } from "./themeCss.ts"; +import { + themeCssFetchCandidates, + themeCssLoadsRemoteFiles, + themeCssValueFetches, +} from "./themeCss.ts"; // %23 decodes to # inside the SVG, so url(%23g) names its own gradient. const SVG_WITH_OWN_GRADIENT = @@ -59,3 +63,33 @@ describe("themeCssLoadsRemoteFiles", () => { ).toBe(false); }); }); + +describe("themeCssFetchCandidates", () => { + it.each([ + // A harmless var() shorthand and an escape elsewhere name no shorthand that fetches. + ["a { border: var(--edge); border-left-width: 0; } .b { c\\6flor: red; }", [" c\\6flor: red"]], + [ + "a { mask: var(--none, url(https://e.com/k)); mask-size: auto; }", + [" mask: var(--none, url(https://e.com/k))"], + ], + ["a { background: var(--none, url(data:image/png;base64,AA)); color: red }", []], + // Brackets, strings, comments and url() addresses do not end a declaration. + [ + 'a { background: var(--none, { ; } "}" /* } */ [;] url(https://e.com/;}) url(x)); b: c }', + [' background: var(--none, { ; } "}" /* } */ [;] url(https://e.com/;}) url(x))'], + ], + // An unquoted url() ends at its first unescaped ")", so the quote after this one opens a string. + [ + 'a { color: u\\72l(a"b) c" ); background: var(--none, url(https://e.com/k)); d: "" }', + [' color: u\\72l(a"b) c" ); background: var(--none, url(https://e.com/k)); d: "" '], + ], + [ + "a { color: url(a\\)b;c); background: v\\61r(--none, url(x)) }", + [" color: url(a\\)b;c)", " background: v\\61r(--none, url(x)) "], + ], + // A closing bracket of another kind closes nothing, so the declaration stays whole. + ["a { background: var(--none, ( ] ; url(x))); }", [" background: var(--none, ( ] ; url(x)))"]], + ])("%s", (css, candidates) => { + expect(themeCssFetchCandidates(css)).toEqual(candidates); + }); +}); diff --git a/packages/shared/src/themeCss.ts b/packages/shared/src/themeCss.ts index 9668040f97be4..77440d8de2bb3 100644 --- a/packages/shared/src/themeCss.ts +++ b/packages/shared/src/themeCss.ts @@ -45,6 +45,64 @@ export function themeCssValueFetches(value: string): boolean { return code.includes("\\") || fetchesOutsideStrings(code); } +const WHITESPACE = String.raw`[ \t\n\f\r]`; +const NAME_ESCAPE = String.raw`\\(?:[0-9a-fA-F]{1,6}(?:\r\n|${WHITESPACE})?|[^\n\r\f0-9a-fA-F])`; +const NAME = String.raw`(?:${NAME_ESCAPE}|[-\w\u0080-\uffff])+`; +const BLOCK_TOKENS = new RegExp( + String.raw`\/\*[\s\S]*?(?:\*\/|$)|${STRING}|(${NAME})(\()?|[()[\]{};]`, + "g", +); +// An unquoted url() is one token: quotes, comments and brackets inside it are part of the address. +const UNQUOTED_URL = new RegExp(String.raw`(?!${WHITESPACE}*["'])(?:${NAME_ESCAPE}|[^)])*\)?`, "y"); +const NAME_ESCAPES = new RegExp( + String.raw`\\(?:([0-9a-fA-F]{1,6})(?:\r\n|${WHITESPACE})?|([^]))`, + "g", +); +const CLOSERS: Record = { "(": ")", "[": "]", "{": "}" }; + +const isUrlFunction = (name: string) => + name + .replace(NAME_ESCAPES, (_, hex?: string, char?: string) => + hex ? String.fromCodePoint(Math.min(Number.parseInt(hex, 16), 0x10ffff)) : (char ?? ""), + ) + .toLowerCase() === "url"; + +/** + * The declarations whose text could load a remote file, each as its own source. A browser hides a + * var() shorthand's text once a later declaration overrides one of its longhands, so the client + * parses each alone. Brackets are tracked as the CSS parser does; an unsure split is left out, so + * one candidate can hold several declarations or rules. + */ +export function themeCssFetchCandidates(css: string): string[] { + const candidates: string[] = []; + const open: string[] = []; + let start = 0; + const cut = (end: number) => { + const text = css.slice(start, end); + if (text.includes("\\") || fetchesOutsideStrings(text)) candidates.push(text); + start = end + 1; + }; + BLOCK_TOKENS.lastIndex = 0; + for (let match = BLOCK_TOKENS.exec(css); match; match = BLOCK_TOKENS.exec(css)) { + const [token, name, paren] = match; + if (name !== undefined) { + if (!paren) continue; + UNQUOTED_URL.lastIndex = BLOCK_TOKENS.lastIndex; + const url = isUrlFunction(name) && UNQUOTED_URL.test(css); + if (url) BLOCK_TOKENS.lastIndex = UNQUOTED_URL.lastIndex; + else open.push(")"); + } else if (token in CLOSERS && (token !== "{" || open.length > 0)) { + open.push(CLOSERS[token] ?? ""); + } else if (token === open.at(-1)) { + open.pop(); + } else if (open.length === 0 && (token === ";" || token === "{" || token === "}")) { + cut(match.index); + } + } + cut(css.length); + return candidates; +} + /** A text check for early feedback. CSS escapes can dodge it; the client check is the guard. */ export function themeCssLoadsRemoteFiles(css: string): boolean { const code = withoutStrings(css, true); From 3b7520bdee55002e688bd990f1a3b1c37c2b0995 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 19 Nov 2026 22:40:29 +0000 Subject: [PATCH 2/2] fix(web): a fetching theme shorthand no longer costs unrelated rules theirs The guard removed hidden pending longhands by name across the whole sheet, so `.unused { background: var(--none, url(https://example.com/x)); }` took the gradient from a harmless `background: var(...); background-color: red;` in another rule. A var() shorthand that can fetch is now cut from the theme's source before the sheet is built, so removal is tied to that one declaration and nothing is left for a later declaration to hide. Blocks that keep a pending shorthand are no longer judged by what other rules hold. Co-Authored-By: Claude Fable 5.2 --- apps/web/src/themeCss.browser.test.ts | 13 +++--- apps/web/src/themeCss.ts | 60 +++++++++++++-------------- packages/shared/src/themeCss.test.ts | 17 ++++++-- packages/shared/src/themeCss.ts | 20 +++++---- 4 files changed, 62 insertions(+), 48 deletions(-) diff --git a/apps/web/src/themeCss.browser.test.ts b/apps/web/src/themeCss.browser.test.ts index 6cc423ebaa57c..75ded78f37d04 100644 --- a/apps/web/src/themeCss.browser.test.ts +++ b/apps/web/src/themeCss.browser.test.ts @@ -35,11 +35,12 @@ it("applies theme CSS and drops every rule that would load a remote file", () => expect(style.listStyleImage).toBe("none"); expect(style.borderImageSource).toBe("none"); expect(style.maskImage).toBe("none"); - const kept = document.adoptedStyleSheets.flatMap((sheet) => - Array.from(sheet.cssRules, (rule) => rule.cssText), - ); - expect(kept.join("\n")).not.toContain("example.com"); - expect(kept.join("\n")).toContain( + const themeCssText = () => + document.adoptedStyleSheets + .flatMap((sheet) => Array.from(sheet.cssRules, (rule) => rule.cssText)) + .join("\n"); + expect(themeCssText()).not.toContain("example.com"); + expect(themeCssText()).toContain( "background: var(--theme-background-image) center / cover no-repeat;", ); expect(style.cursor).toBe("auto"); @@ -65,7 +66,9 @@ it("applies theme CSS and drops every rule that would load a remote file", () => background: var(--none, linear-gradient(red, blue)); background-color: red; } .unused { c\\6flor: red; b\\61 ckground: var(--none); cursor: url(https://example.com/x), auto; } + .unused { background: var(--none, url(https://example.com/y)); background-size: auto; } `); + expect(themeCssText()).not.toContain("example.com"); expect(style.borderTopWidth).toBe("4px"); expect(style.borderLeftWidth).toBe("0px"); expect(style.backgroundImage).toContain("linear-gradient"); diff --git a/apps/web/src/themeCss.ts b/apps/web/src/themeCss.ts index 06fc1376f88cf..870a915ef26d2 100644 --- a/apps/web/src/themeCss.ts +++ b/apps/web/src/themeCss.ts @@ -1,4 +1,4 @@ -import { themeCssFetchCandidates, themeCssValueFetches } from "@t3tools/shared/themeCss"; +import { themeCssValueFetches, themeCssWithoutDeclarations } from "@t3tools/shared/themeCss"; // Theme CSS comes from imported files and from agents, so it must not fetch: a // url() on an attribute selector can send what is on screen to another host. @@ -37,39 +37,34 @@ function parsedStyles(text: string): CSSStyleDeclaration[] { } /** - * The pending longhands of `style` that can fetch. cssText shows a pending shorthand's text until - * a later declaration overrides one of its longhands. After that only the theme's source has the - * text, so a longhand that does not come back from parsing cssText is judged by `hiddenFetches`. + * Whether a shorthand that `style` leaves pending can fetch. cssText shows its text until a later + * declaration overrides one of its longhands; `hiddenFetches` says what to make of one that then + * does not come back from parsing cssText. */ -function fetchingPendingLonghands( - style: CSSStyleDeclaration, - hiddenFetches: (longhand: string) => boolean, -): string[] { +function pendingShorthandFetches(style: CSSStyleDeclaration, hiddenFetches: boolean): boolean { const pending = pendingLonghands(style); - if (pending.length === 0) return pending; + if (pending.length === 0) return false; const text = style.cssText; - if (themeCssValueFetches(text)) return pending; - const suspects = pending.filter(hiddenFetches); - if (suspects.length === 0) return suspects; + if (themeCssValueFetches(text)) return true; + if (!hiddenFetches) return false; const shown = new Set(parsedStyles(`x{${text}}`).flatMap(pendingLonghands)); - return suspects.filter((longhand) => !shown.has(longhand)); + return pending.some((longhand) => !shown.has(longhand)); } /** - * The longhands that a var() shorthand able to fetch leaves pending anywhere in the CSS. The - * browser parses each candidate alone, as rules and as a block, so escapes resolve as they will. + * The theme's source without the var() shorthands that can fetch, so that none is left for a later + * declaration to hide. The browser parses each candidate alone, as rules and as a block, so + * escapes resolve as they will. */ -function fetchingShorthandLonghands(css: string): Set { - const longhands = new Set(); - for (const candidate of themeCssFetchCandidates(css)) { - for (const style of [...parsedStyles(candidate), ...parsedStyles(`x{${candidate}}`)]) { - for (const longhand of fetchingPendingLonghands(style, () => true)) longhands.add(longhand); - } - } - return longhands; +function withoutFetchingShorthands(css: string): string { + return themeCssWithoutDeclarations(css, (candidate) => + [...parsedStyles(candidate), ...parsedStyles(`x{${candidate}}`)].some((style) => + pendingShorthandFetches(style, true), + ), + ); } -function stripDeclarations(rule: CSSRule, hiddenFetches: ReadonlySet): boolean { +function stripDeclarations(rule: CSSRule): boolean { if (!("style" in rule) || !(rule.style instanceof CSSStyleDeclaration)) return false; for (let item = rule.style.length - 1; item >= 0; item--) { const property = rule.style.item(item); @@ -77,21 +72,22 @@ function stripDeclarations(rule: CSSRule, hiddenFetches: ReadonlySet): b rule.style.removeProperty(property); } } - const fetching = fetchingPendingLonghands(rule.style, (longhand) => hiddenFetches.has(longhand)); - for (const longhand of fetching) rule.style.removeProperty(longhand); + if (pendingShorthandFetches(rule.style, false)) { + for (const longhand of pendingLonghands(rule.style)) rule.style.removeProperty(longhand); + } return true; } /** Keeps only rule shapes it can check, so a new at-rule that fetches is dropped by default. */ -function stripFetches(parent: RuleParent, hiddenFetches: ReadonlySet): void { +function stripFetches(parent: RuleParent): void { for (let index = parent.cssRules.length - 1; index >= 0; index--) { const rule = parent.cssRules[index]; if (!rule) continue; - const hasStyle = stripDeclarations(rule, hiddenFetches); + const hasStyle = stripDeclarations(rule); if (rule instanceof CSSKeyframesRule) { - for (const frame of rule.cssRules) stripDeclarations(frame, hiddenFetches); + for (const frame of rule.cssRules) stripDeclarations(frame); } else if (isRuleParent(rule)) { - stripFetches(rule, hiddenFetches); + stripFetches(rule); } else if (rule instanceof CSSPropertyRule) { // A registered property's initial value is used wherever var() reads it. if (themeCssValueFetches(rule.initialValue ?? "")) parent.deleteRule(index); @@ -116,6 +112,6 @@ export function applyThemeCss(css: string | undefined): void { themeSheet = new CSSStyleSheet(); document.adoptedStyleSheets = [...document.adoptedStyleSheets, themeSheet]; } - themeSheet.replaceSync(next); - stripFetches(themeSheet, fetchingShorthandLonghands(next)); + themeSheet.replaceSync(withoutFetchingShorthands(next)); + stripFetches(themeSheet); } diff --git a/packages/shared/src/themeCss.test.ts b/packages/shared/src/themeCss.test.ts index c1c7bc30845d7..3a04aa287e00e 100644 --- a/packages/shared/src/themeCss.test.ts +++ b/packages/shared/src/themeCss.test.ts @@ -1,9 +1,9 @@ import { describe, expect, it } from "vite-plus/test"; import { - themeCssFetchCandidates, themeCssLoadsRemoteFiles, themeCssValueFetches, + themeCssWithoutDeclarations, } from "./themeCss.ts"; // %23 decodes to # inside the SVG, so url(%23g) names its own gradient. @@ -64,7 +64,7 @@ describe("themeCssLoadsRemoteFiles", () => { }); }); -describe("themeCssFetchCandidates", () => { +describe("themeCssWithoutDeclarations", () => { it.each([ // A harmless var() shorthand and an escape elsewhere name no shorthand that fetches. ["a { border: var(--edge); border-left-width: 0; } .b { c\\6flor: red; }", [" c\\6flor: red"]], @@ -90,6 +90,17 @@ describe("themeCssFetchCandidates", () => { // A closing bracket of another kind closes nothing, so the declaration stays whole. ["a { background: var(--none, ( ] ; url(x))); }", [" background: var(--none, ( ] ; url(x)))"]], ])("%s", (css, candidates) => { - expect(themeCssFetchCandidates(css)).toEqual(candidates); + const asked: string[] = []; + expect(themeCssWithoutDeclarations(css, (candidate) => asked.push(candidate) < 0)).toBe(css); + expect(asked).toEqual(candidates); + }); + + it("removes only the declarations that are refused", () => { + expect( + themeCssWithoutDeclarations( + "a { mask: var(--none, url(https://e.com/k)); mask-size: auto } b { cursor: url(x) }", + (candidate) => candidate.includes("mask"), + ), + ).toBe("a {; mask-size: auto } b { cursor: url(x) }"); }); }); diff --git a/packages/shared/src/themeCss.ts b/packages/shared/src/themeCss.ts index 77440d8de2bb3..1adaff9cd958f 100644 --- a/packages/shared/src/themeCss.ts +++ b/packages/shared/src/themeCss.ts @@ -68,18 +68,22 @@ const isUrlFunction = (name: string) => .toLowerCase() === "url"; /** - * The declarations whose text could load a remote file, each as its own source. A browser hides a - * var() shorthand's text once a later declaration overrides one of its longhands, so the client - * parses each alone. Brackets are tracked as the CSS parser does; an unsure split is left out, so - * one candidate can hold several declarations or rules. + * The CSS without the declarations that `drop` refuses, asked only for text that could load a + * remote file. A browser hides a var() shorthand's text once a later declaration overrides one of + * its longhands, so only the source has it. Brackets are tracked as the CSS parser does; an unsure + * split is left out, so one candidate can hold several declarations or rules. */ -export function themeCssFetchCandidates(css: string): string[] { - const candidates: string[] = []; +export function themeCssWithoutDeclarations( + css: string, + drop: (candidate: string) => boolean, +): string { + let kept = ""; const open: string[] = []; let start = 0; const cut = (end: number) => { const text = css.slice(start, end); - if (text.includes("\\") || fetchesOutsideStrings(text)) candidates.push(text); + const candidate = text.includes("\\") || fetchesOutsideStrings(text); + kept += (candidate && drop(text) ? "" : text) + css.slice(end, end + 1); start = end + 1; }; BLOCK_TOKENS.lastIndex = 0; @@ -100,7 +104,7 @@ export function themeCssFetchCandidates(css: string): string[] { } } cut(css.length); - return candidates; + return kept; } /** A text check for early feedback. CSS escapes can dodge it; the client check is the guard. */