mirror of
https://github.com/MercuryWorkshop/scramjet.git
synced 2025-05-14 15:00:01 -04:00
Merge pull request #16 from gennaroterry/main
fix: Improve srcset rewriter to handle descriptors correctly.
This commit is contained in:
commit
f0ee17720d
3 changed files with 72 additions and 17 deletions
|
@ -52,10 +52,13 @@ export function rewriteHtml(
|
|||
|
||||
const script = (src) => new Element("script", { src });
|
||||
|
||||
// for compatibility purpose
|
||||
const base64Injected = bytesToBase64(new TextEncoder().encode(injected));
|
||||
|
||||
head.children.unshift(
|
||||
script($scramjet.config.files.wasm),
|
||||
script($scramjet.config.files.shared),
|
||||
script("data:application/javascript;base64," + btoa(injected)),
|
||||
script("data:application/javascript;base64," + base64Injected),
|
||||
script($scramjet.config.files.client)
|
||||
);
|
||||
}
|
||||
|
@ -283,17 +286,20 @@ function traverseParsedHtml(
|
|||
}
|
||||
|
||||
export function rewriteSrcset(srcset: string, meta: URLMeta) {
|
||||
const urls = srcset.split(/ [0-9]+x,? ?/g);
|
||||
if (!urls) return "";
|
||||
const sufixes = srcset.match(/ [0-9]+x,? ?/g);
|
||||
if (!sufixes) return "";
|
||||
const rewrittenUrls = urls.map((url, i) => {
|
||||
if (url && sufixes[i]) {
|
||||
return rewriteUrl(url, meta) + sufixes[i];
|
||||
}
|
||||
const sources = srcset.split(",").map((src) => src.trim());
|
||||
const rewrittenSources = sources.map((source) => {
|
||||
// Split into URLs and descriptors (if any)
|
||||
// e.g. url0, url1 1.5x, url2 2x
|
||||
const [url, ...descriptors] = source.split(/\s+/);
|
||||
|
||||
// Rewrite the URLs and keep the descriptors (if any)
|
||||
const rewrittenUrl = rewriteUrl(url.trim(), meta);
|
||||
return descriptors.length > 0
|
||||
? `${rewrittenUrl} ${descriptors.join(" ")}`
|
||||
: rewrittenUrl;
|
||||
});
|
||||
|
||||
return rewrittenUrls.join("");
|
||||
return rewrittenSources.join(", ");
|
||||
}
|
||||
|
||||
// function base64ToBytes(base64) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue