EPUB: support srcset

Closes #98
This commit is contained in:
John Factotum
2025-09-24 14:01:27 +08:00
parent b1f72c0395
commit 26788cdae9
+5 -1
View File
@@ -836,7 +836,6 @@ class Loader {
} }
} }
// replace hrefs (excluding anchors) // replace hrefs (excluding anchors)
// TODO: srcset?
const replace = async (el, attr) => el.setAttribute(attr, const replace = async (el, attr) => el.setAttribute(attr,
await this.loadHref(el.getAttribute(attr), href, parents)) await this.loadHref(el.getAttribute(attr), href, parents))
for (const el of doc.querySelectorAll('link[href]')) await replace(el, 'href') 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])')) for (const el of doc.querySelectorAll('[*|href]:not([href])'))
el.setAttributeNS(NS.XLINK, 'href', await this.loadHref( el.setAttributeNS(NS.XLINK, 'href', await this.loadHref(
el.getAttributeNS(NS.XLINK, 'href'), href, parents)) 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 // replace inline styles
for (const el of doc.querySelectorAll('style')) for (const el of doc.querySelectorAll('style'))
if (el.textContent) el.textContent = if (el.textContent) el.textContent =