Skip to content

Commit

Permalink
fix oversight in css rewriter
Browse files Browse the repository at this point in the history
  • Loading branch information
Percslol committed Oct 27, 2024
1 parent 39d7529 commit 635494f
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions src/shared/rewriters/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
}
)
);
});

Expand Down

0 comments on commit 635494f

Please sign in to comment.