diff --git a/src/shared/rewriters/css.ts b/src/shared/rewriters/css.ts index 4464f0f..07fd051 100644 --- a/src/shared/rewriters/css.ts +++ b/src/shared/rewriters/css.ts @@ -9,32 +9,32 @@ export function unrewriteCss(css: string) { } function handleCss(type: "rewrite" | "unrewrite", css: string, meta?: URLMeta) { + // regex from vk6 (https://github.com/ading2210) const urlRegex = /url\(['"]?(.+?)['"]?\)/gm; const Atruleregex = /@import\s+(url\s*?\(.{0,9999}?\)|['"].{0,9999}?['"]|.{0,9999}?)($|\s|;)/gm; css = new String(css).toString(); css = css.replace(urlRegex, (match, url) => { const encodedUrl = - type === "rewrite" - ? rewriteUrl(url.trim(), meta) - : unrewriteUrl(url.trim()); + type === "rewrite" ? rewriteUrl(url, meta) : unrewriteUrl(url); return match.replace(url, encodedUrl); }); - css = css.replace(Atruleregex, (_, importStatement) => { - return importStatement.replace( - /^(url\(['"]?|['"]|)(.+?)(['"]|['"]?\)|)$/gm, - (match, firstQuote, url, endQuote) => { - if (firstQuote.startsWith("url")) { - return match; - } - const encodedUrl = - type === "rewrite" - ? rewriteUrl(url.trim(), meta) - : unrewriteUrl(url.trim()); + css = css.replace(Atruleregex, (match, importStatement) => { + return match.replace( + importStatement, + importStatement.replace( + /^(url\(['"]?|['"]|)(.+?)(['"]|['"]?\)|)$/gm, + (match, firstQuote, url, endQuote) => { + if (firstQuote.startsWith("url")) { + return match; + } + const encodedUrl = + type === "rewrite" ? rewriteUrl(url, meta) : unrewriteUrl(url); - return `${firstQuote}${encodedUrl}${endQuote}`; - } + return `${firstQuote}${encodedUrl}${endQuote}`; + } + ) ); });