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";
|
import { URLMeta, rewriteUrl, unrewriteUrl } from "./url";
|
||||||
|
|
||||||
export function rewriteCss(css: string, meta: URLMeta) {
|
export function rewriteCss(css: string, meta: URLMeta) {
|
||||||
const regex =
|
handleCss("unrewrite", css, meta);
|
||||||
/(@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})`;
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function unrewriteCss(css: string) {
|
export function unrewriteCss(css: string) {
|
||||||
const regex =
|
handleCss("rewrite", css);
|
||||||
/(@import\s+(?!url\())?\s*url\(\s*(['"]?)([^'")]+)\2\s*\)|@import\s+(['"])([^'"]+)\4/g;
|
}
|
||||||
|
|
||||||
return css.replace(
|
function handleCss(type: "rewrite" | "unrewrite", css: string, meta?: URLMeta) {
|
||||||
regex,
|
const urlRegex = /url\(['"]?(.+?)['"]?\)/gm;
|
||||||
(
|
const Atruleregex =
|
||||||
match,
|
/@import\s+(url\s*?\(.{0,9999}?\)|['"].{0,9999}?['"]|.{0,9999}?)($|\s|;)/gm;
|
||||||
importStatement,
|
css = new String(css).toString();
|
||||||
urlQuote,
|
css = css.replace(urlRegex, (match, url) => {
|
||||||
urlContent,
|
const encodedUrl =
|
||||||
importQuote,
|
type === "rewrite"
|
||||||
importContent
|
? rewriteUrl(url.trim(), meta)
|
||||||
) => {
|
: unrewriteUrl(url.trim());
|
||||||
const url = urlContent || importContent;
|
|
||||||
const encodedUrl = unrewriteUrl(url.trim());
|
|
||||||
|
|
||||||
if (importStatement) {
|
return match.replace(url, encodedUrl);
|
||||||
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`;
|
});
|
||||||
|
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 `${firstQuote}${encodedUrl}${endQuote}`;
|
||||||
return `@import ${importQuote}${encodedUrl}${importQuote}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return `url(${urlQuote}${encodedUrl}${urlQuote})`;
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
return css;
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,7 @@ export function errorTemplate(trace: string, fetchedURL: string) {
|
||||||
`;
|
`;
|
||||||
|
|
||||||
return `<!DOCTYPE html>
|
return `<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<title>Scramjet</title>
|
<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>
|
<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>
|
<script src="${"data:application/javascript," + encodeURIComponent(script)}"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
||||||
`;
|
`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue