From 26788cdae9286861cc65690bf35cec4fae105d38 Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Wed, 24 Sep 2025 14:01:27 +0800 Subject: [PATCH] EPUB: support `srcset` Closes #98 --- epub.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/epub.js b/epub.js index 12532eb..8297e02 100644 --- a/epub.js +++ b/epub.js @@ -836,7 +836,6 @@ class Loader { } } // replace hrefs (excluding anchors) - // TODO: srcset? const replace = async (el, attr) => el.setAttribute(attr, await this.loadHref(el.getAttribute(attr), href, parents)) for (const el of doc.querySelectorAll('link[href]')) await replace(el, 'href') @@ -846,6 +845,11 @@ class Loader { for (const el of doc.querySelectorAll('[*|href]:not([href])')) el.setAttributeNS(NS.XLINK, 'href', await this.loadHref( el.getAttributeNS(NS.XLINK, 'href'), href, parents)) + for (const el of doc.querySelectorAll('[srcset]')) + el.setAttribute('srcset', await replaceSeries(el.getAttribute('srcset'), + /(\s*)(.+?)\s*((?:\s[\d.]+[wx])+\s*(?:,|$)|,\s+|$)/g, + (_, p1, p2, p3) => this.loadHref(p2, href, parents) + .then(p2 => `${p1}${p2}${p3}`))) // replace inline styles for (const el of doc.querySelectorAll('style')) if (el.textContent) el.textContent =