feat: switch to new css rewriter

This commit is contained in:
Percs 2024-10-26 22:56:07 -05:00
parent 257b700e25
commit b9be6e8c7a
2 changed files with 155 additions and 178 deletions

View file

@ -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;
return css.replace(
regex,
(
match,
importStatement,
urlQuote,
urlContent,
importQuote,
importContent
) => {
const url = urlContent || importContent;
const encodedUrl = unrewriteUrl(url.trim());
if (importStatement) {
return `@import url(${urlQuote}${encodedUrl}${urlQuote})`;
}
if (importQuote) {
return `@import ${importQuote}${encodedUrl}${importQuote}`;
}
return `url(${urlQuote}${encodedUrl}${urlQuote})`;
}
);
handleCss("rewrite", css);
}
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());
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());
return `${firstQuote}${encodedUrl}${endQuote}`;
}
);
});
return css;
}

View file

@ -12,138 +12,137 @@ export function errorTemplate(trace: string, fetchedURL: string) {
`;
return `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Scramjet</title>
<style>
:root {
--deep: #080602;
--shallow: #181412;
--beach: #f1e8e1;
--shore: #b1a8a1;
--accent: #ffa938;
--font-sans: -apple-system, system-ui, BlinkMacSystemFont, sans-serif;
--font-monospace: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}
<html>
<head>
<meta charset="utf-8" />
<title>Scramjet</title>
<style>
:root {
--deep: #080602;
--shallow: #181412;
--beach: #f1e8e1;
--shore: #b1a8a1;
--accent: #ffa938;
--font-sans: -apple-system, system-ui, BlinkMacSystemFont, sans-serif;
--font-monospace: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
}
*:not(div,p,span,ul,li,i,span) {
background-color: var(--deep);
color: var(--beach);
font-family: var(--font-sans);
}
*:not(div,p,span,ul,li,i,span) {
background-color: var(--deep);
color: var(--beach);
font-family: var(--font-sans);
}
textarea,
button {
background-color: var(--shallow);
border-radius: 0.6em;
padding: 0.6em;
border: none;
appearance: none;
font-family: var(--font-sans);
color: var(--beach);
}
textarea,
button {
background-color: var(--shallow);
border-radius: 0.6em;
padding: 0.6em;
border: none;
appearance: none;
font-family: var(--font-sans);
color: var(--beach);
}
button.primary {
background-color: var(--accent);
color: var(--deep);
font-weight: bold;
}
button.primary {
background-color: var(--accent);
color: var(--deep);
font-weight: bold;
}
textarea {
resize: none;
height: 20em;
text-align: left;
font-family: var(--font-monospace);
}
textarea {
resize: none;
height: 20em;
text-align: left;
font-family: var(--font-monospace);
}
body {
width: 100vw;
height: 100vh;
justify-content: center;
align-items: center;
}
body {
width: 100vw;
height: 100vh;
justify-content: center;
align-items: center;
}
body,
html,
#inner {
display: flex;
align-items: center;
flex-direction: column;
gap: 0.5em;
overflow: hidden;
}
body,
html,
#inner {
display: flex;
align-items: center;
flex-direction: column;
gap: 0.5em;
overflow: hidden;
}
#inner {
z-index: 100;
}
#inner {
z-index: 100;
}
#cover {
position: absolute;
width: 100%;
height: 100%;
background-color: color-mix(in srgb, var(--deep) 70%, transparent);
z-index: 99;
}
#cover {
position: absolute;
width: 100%;
height: 100%;
background-color: color-mix(in srgb, var(--deep) 70%, transparent);
z-index: 99;
}
#info {
display: flex;
flex-direction: row;
align-items: flex-start;
gap: 1em;
}
#info {
display: flex;
flex-direction: row;
align-items: flex-start;
gap: 1em;
}
#version-wrapper {
width: 100%;
text-align: center;
position: absolute;
bottom: 0.2rem;
font-size: 0.8rem;
color: var(--shore)!important;
i {
background-color: color-mix(in srgb, var(--deep), transparent 50%);
border-radius: 9999px;
padding: 0.2em 0.5em;
}
z-index: 101;
}
</style>
</head>
<body>
<div id="cover"></div>
<div id="inner">
<h1 id="errorTitle">Uh oh!</h1>
<p>There was an error loading <b id="fetchedURL"></b></p>
<!-- <p id="errorMessage">Internal Server Error</p> -->
<div id="info">
<textarea id="errorTrace" cols="40" rows="10" readonly>
</textarea>
<div id="troubleshooting">
<p>Try:</p>
<ul>
<li>Checking your internet connection</li>
<li>Verifying you entered the correct address</li>
<li>Clearing the site data</li>
<li>Contacting <b id="hostname"></b>'s administrator</li>
<li>Verify the server isn't censored</li>
</ul>
<p>If you're the administrator of <b id="hostname"></b>, try:</p>
<ul>
<li>Restarting your server</li>
<li>Updating Scramjet</li>
<li>Troubleshooting the error on the <a href="https://github.com/MercuryWorkshop/scramjet" target="_blank">GitHub repository</a></li>
</ul>
</div>
</div>
<br>
<button id="reload" class="primary">Reload</button>
</div>
<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>
#version-wrapper {
width: 100%;
text-align: center;
position: absolute;
bottom: 0.2rem;
font-size: 0.8rem;
color: var(--shore)!important;
i {
background-color: color-mix(in srgb, var(--deep), transparent 50%);
border-radius: 9999px;
padding: 0.2em 0.5em;
}
z-index: 101;
}
</style>
</head>
<body>
<div id="cover"></div>
<div id="inner">
<h1 id="errorTitle">Uh oh!</h1>
<p>There was an error loading <b id="fetchedURL"></b></p>
<!-- <p id="errorMessage">Internal Server Error</p> -->
<div id="info">
<textarea id="errorTrace" cols="40" rows="10" readonly>
</textarea>
<div id="troubleshooting">
<p>Try:</p>
<ul>
<li>Checking your internet connection</li>
<li>Verifying you entered the correct address</li>
<li>Clearing the site data</li>
<li>Contacting <b id="hostname"></b>'s administrator</li>
<li>Verify the server isn't censored</li>
</ul>
<p>If you're the administrator of <b id="hostname"></b>, try:</p>
<ul>
<li>Restarting your server</li>
<li>Updating Scramjet</li>
<li>Troubleshooting the error on the <a href="https://github.com/MercuryWorkshop/scramjet" target="_blank">GitHub repository</a></li>
</ul>
</div>
</div>
<br>
<button id="reload" class="primary">Reload</button>
</div>
<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>
`;
}