mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 06:50:01 -04:00
feat: switch to new css rewriter
This commit is contained in:
parent
257b700e25
commit
b9be6e8c7a
2 changed files with 155 additions and 178 deletions
|
@ -1,64 +1,42 @@
|
|||
// This CSS rewriter uses code from Meteor
|
||||
// You can find the original source code at https://github.com/MeteorProxy/Meteor
|
||||
|
||||
import { URLMeta, rewriteUrl, unrewriteUrl } from "./url";
|
||||
|
||||
export function rewriteCss(css: string, meta: URLMeta) {
|
||||
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 = rewriteUrl(url.trim(), meta);
|
||||
|
||||
if (importStatement) {
|
||||
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`;
|
||||
}
|
||||
|
||||
if (importQuote) {
|
||||
return `@import ${importQuote}${encodedUrl}${importQuote}`;
|
||||
}
|
||||
|
||||
return `url(${urlQuote}${encodedUrl}${urlQuote})`;
|
||||
}
|
||||
);
|
||||
handleCss("unrewrite", css, meta);
|
||||
}
|
||||
|
||||
export function unrewriteCss(css: string) {
|
||||
const regex =
|
||||
/(@import\s+(?!url\())?\s*url\(\s*(['"]?)([^'")]+)\2\s*\)|@import\s+(['"])([^'"]+)\4/g;
|
||||
handleCss("rewrite", css);
|
||||
}
|
||||
|
||||
return css.replace(
|
||||
regex,
|
||||
(
|
||||
match,
|
||||
importStatement,
|
||||
urlQuote,
|
||||
urlContent,
|
||||
importQuote,
|
||||
importContent
|
||||
) => {
|
||||
const url = urlContent || importContent;
|
||||
const encodedUrl = unrewriteUrl(url.trim());
|
||||
function handleCss(type: "rewrite" | "unrewrite", css: string, meta?: URLMeta) {
|
||||
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());
|
||||
|
||||
if (importStatement) {
|
||||
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`;
|
||||
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());
|
||||
|
||||
if (importQuote) {
|
||||
return `@import ${importQuote}${encodedUrl}${importQuote}`;
|
||||
}
|
||||
|
||||
return `url(${urlQuote}${encodedUrl}${urlQuote})`;
|
||||
return `${firstQuote}${encodedUrl}${endQuote}`;
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
return css;
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ export function errorTemplate(trace: string, fetchedURL: string) {
|
|||
`;
|
||||
|
||||
return `<!DOCTYPE html>
|
||||
<html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>Scramjet</title>
|
||||
|
@ -142,8 +142,7 @@ export function errorTemplate(trace: string, fetchedURL: string) {
|
|||
<p id="version-wrapper"><i>Scramjet v<span id="version"></span> (build <span id="build"></span>)</i></p>
|
||||
<script src="${"data:application/javascript," + encodeURIComponent(script)}"></script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
</html>
|
||||
`;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue