From b9be6e8c7ad2f5e2eb8daef0b87442cb8e045370 Mon Sep 17 00:00:00 2001 From: Percs <83934299+Percslol@users.noreply.github.com> Date: Sat, 26 Oct 2024 22:56:07 -0500 Subject: [PATCH] feat: switch to new css rewriter --- src/shared/rewriters/css.ts | 92 ++++++-------- src/worker/error.ts | 241 ++++++++++++++++++------------------ 2 files changed, 155 insertions(+), 178 deletions(-) diff --git a/src/shared/rewriters/css.ts b/src/shared/rewriters/css.ts index b09668a..55f65b0 100644 --- a/src/shared/rewriters/css.ts +++ b/src/shared/rewriters/css.ts @@ -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; } diff --git a/src/worker/error.ts b/src/worker/error.ts index 525b34a..03e458c 100644 --- a/src/worker/error.ts +++ b/src/worker/error.ts @@ -12,138 +12,137 @@ export function errorTemplate(trace: string, fetchedURL: string) { `; return ` - -
- -There was an error loading
- - -Try:
-If you're the administrator of , try:
-Scramjet v (build )
- - - + #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; + } + + + + +There was an error loading
+ +Try:
+If you're the administrator of , try:
+Scramjet v (build )
+ + +