mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
fix css rewriting
This commit is contained in:
parent
e6f973175d
commit
016b41552e
1 changed files with 27 additions and 6 deletions
|
@ -1,11 +1,32 @@
|
|||
import { encodeUrl } from "./url";
|
||||
|
||||
export function rewriteCss(css: string, origin?: URL) {
|
||||
css = css.replace(/(?<=url\("?'?)[^"'][\S]*[^"'](?="?'?\);?)/gm, (match) => encodeUrl(match, origin));
|
||||
//painful regex simulator
|
||||
css = css.replace(/@import\s+(['"])?([^'");]+)\1?\s*(?:;|$)/gm, (_, quote, url) => {
|
||||
return `@import ${quote || ""}${encodeUrl(url.trim(), origin)}${quote || ""};`;
|
||||
});
|
||||
const regex =
|
||||
/(@import\s+(?!url\())?\s*url\(\s*(['"]?)([^'")]+)\2\s*\)|@import\s+(['"])([^'"]+)\4/g
|
||||
|
||||
return css.replace(
|
||||
regex,
|
||||
(
|
||||
match,
|
||||
importStatement,
|
||||
urlQuote,
|
||||
urlContent,
|
||||
importQuote,
|
||||
importContent
|
||||
) => {
|
||||
const url = urlContent || importContent
|
||||
const encodedUrl = encodeUrl(url.trim(), origin)
|
||||
|
||||
if (importStatement) {
|
||||
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`
|
||||
}
|
||||
|
||||
if (importQuote) {
|
||||
return `@import ${importQuote}${encodedUrl}${importQuote}`
|
||||
}
|
||||
|
||||
return `url(${urlQuote}${encodedUrl}${urlQuote})`
|
||||
}
|
||||
)
|
||||
|
||||
return css;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue