From f81461c0445974cbbaf5b901359121e5b87af21f Mon Sep 17 00:00:00 2001 From: litruv Date: Tue, 3 Nov 2026 02:32:08 +1100 Subject: [PATCH] Merge the mutually recursive CSS scope helpers. [skip build] --- .../features/room-app/sanitizeRoomAppCss.ts | 50 ++++++++----------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/src/app/features/room-app/sanitizeRoomAppCss.ts b/src/app/features/room-app/sanitizeRoomAppCss.ts index 3b9643b9..4c469150 100644 --- a/src/app/features/room-app/sanitizeRoomAppCss.ts +++ b/src/app/features/room-app/sanitizeRoomAppCss.ts @@ -35,32 +35,7 @@ export function scopeRoomAppCss(css: string, scopeSelector: string): string { return `${scoped}{${body}}`; }; - const rewriteBlock = (block: string): string => { - const trimmed = block.trim(); - if (!trimmed) return ''; - - if (trimmed.startsWith('@')) { - const open = trimmed.indexOf('{'); - if (open === -1) return `${trimmed};`; - const header = trimmed.slice(0, open).trim(); - const inner = trimmed.slice(open + 1); - // @keyframes / @font-face: keep as-is (namespaced risk is low for MVP) - if (/^@(keyframes|font-face)\b/i.test(header)) { - return `${header}{${inner}}`; - } - // @media / @supports: re-scope nested rules - const nested = rewriteCssChunk(inner); - return `${header}{${nested}}`; - } - - const open = trimmed.indexOf('{'); - if (open === -1) return ''; - const selectors = trimmed.slice(0, open); - const body = trimmed.slice(open + 1); - return scopeRule(selectors, body); - }; - - const rewriteCssChunk = (chunk: string): string => { + function rewriteChunk(chunk: string): string { const parts: string[] = []; let depth = 0; let start = 0; @@ -70,15 +45,32 @@ export function scopeRoomAppCss(css: string, scopeSelector: string): string { else if (ch === '}') { depth -= 1; if (depth === 0) { - parts.push(rewriteBlock(chunk.slice(start, i))); + const block = chunk.slice(start, i).trim(); start = i + 1; + if (block) { + const open = block.indexOf('{'); + if (open === -1) { + parts.push(`${block};`); + } else { + const header = block.slice(0, open).trim(); + const inner = block.slice(open + 1); + if (!header.startsWith('@')) { + parts.push(scopeRule(header, inner)); + } else if (/^@(keyframes|font-face)\b/i.test(header)) { + parts.push(`${header}{${inner}}`); + } else { + // @media / @supports: re-scope nested rules + parts.push(`${header}{${rewriteChunk(inner)}}`); + } + } + } } } } return parts.filter(Boolean).join('\n'); - }; + } - return rewriteCssChunk(sanitized); + return rewriteChunk(sanitized); } export function isAllowedRoomAppImageSrc(src: string): boolean {