From d4696cea4b9f23f658f9523595d79eba5d9fe8e6 Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Wed, 12 Mar 2025 17:21:07 +0800 Subject: [PATCH] FB2: only check ``s when getting data Fixes https://github.com/johnfactotum/foliate/issues/1492 --- fb2.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/fb2.js b/fb2.js index ade3645..ce9bdc0 100644 --- a/fb2.js +++ b/fb2.js @@ -70,27 +70,30 @@ const BODY = { 'section': ['section', SECTION], } -const getImageSrc = el => { - const href = el.getAttributeNS(NS.XLINK, 'href') - if (!href) return 'data:,' - const [, id] = href.split('#') - if (!id) return href - const bin = el.getRootNode().getElementById(id) - return bin - ? `data:${bin.getAttribute('content-type')};base64,${bin.textContent}` - : href -} - class FB2Converter { constructor(fb2) { this.fb2 = fb2 this.doc = document.implementation.createDocument(NS.XHTML, 'html') + // use this instead of `getElementById` to allow images like + // `` + this.bins = new Map(Array.from(this.fb2.getElementsByTagName('binary'), + el => [el.id, el])) + } + getImageSrc(el) { + const href = el.getAttributeNS(NS.XLINK, 'href') + if (!href) return 'data:,' + const [, id] = href.split('#') + if (!id) return href + const bin = this.bins.get(id) + return bin + ? `data:${bin.getAttribute('content-type')};base64,${bin.textContent}` + : href } image(node) { const el = this.doc.createElement('img') el.alt = node.getAttribute('alt') el.title = node.getAttribute('title') - el.setAttribute('src', getImageSrc(node)) + el.setAttribute('src', this.getImageSrc(node)) return el } anchor(node) { @@ -267,7 +270,7 @@ export const makeFB2 = async blob => { subject: $$('title-info genre').map(getElementText), } if ($('coverpage image')) { - const src = getImageSrc($('coverpage image')) + const src = converter.getImageSrc($('coverpage image')) book.getCover = () => fetch(src).then(res => res.blob()) } else book.getCover = () => null