encode the hash seperately from the rest of url

This commit is contained in:
Percs 2025-04-19 12:29:49 -05:00
parent 4791bf0ca8
commit 3f6c2a6674
2 changed files with 12 additions and 8 deletions

View file

@ -55,7 +55,7 @@ export function createLocationProxy(
if (prop === "hash") {
self.location.hash = args[0];
const ev = new UrlChangeEvent(client.url.href);
if (client.frame) client.frame.dispatchEvent(ev);
if (!client.isSubframe) client.frame?.dispatchEvent(ev);
return;
}

View file

@ -46,14 +46,15 @@ export function rewriteUrl(url: string | URL, meta: URLMeta) {
if (base.startsWith("about:")) base = unrewriteUrl(self.location.href); // jank!!!!! weird jank!!!
const realUrl = tryCanParseURL(url, base);
if (!realUrl) return url;
const hash = realUrl.hash;
const encodedHash = $scramjet.codec.encode(realUrl.hash.slice(1));
realUrl.hash = "";
return (
location.origin +
$scramjet.config.prefix +
$scramjet.codec.encode(realUrl.href) +
hash
"#" +
encodedHash
);
}
}
@ -75,11 +76,14 @@ export function unrewriteUrl(url: string | URL) {
return url.substring(prefixed.length);
} else if (url.startsWith("mailto:") || url.startsWith("about:")) {
return url;
} else if (tryCanParseURL(url)) {
return $scramjet.codec.decode(
url.slice((location.origin + $scramjet.config.prefix).length)
);
} else {
return url;
const realUrl = tryCanParseURL(url);
if (!realUrl) return url;
const decodedHash = $scramjet.codec.decode(realUrl.hash.slice(1));
realUrl.hash = "";
return $scramjet.codec.decode(
realUrl.href.slice(prefixed.length) + "#" + decodedHash
);
}
}