Merge the mutually recursive CSS scope helpers. [skip build]
Checks / checks (push) Successful in 2m0s
Trigger cinny-mobile / dispatch (push) Successful in 1s

This commit is contained in:
2026-11-03 02:32:08 +11:00
parent 96d0d27549
commit f81461c044
+21 -29
View File
@@ -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 {