From 9ff893f3e73eadfcfd64db080ff486b8a55c9a81 Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Sat, 27 Jul 2024 09:17:33 +0800 Subject: [PATCH] Treat XHTML without xmlns as HTML The XHTML 1.1 spec says The start tag of the root element of the document MUST explicitly contain an xmlns declaration for the XHTML namespace There doesn't seem to be such a requirement in the WHATWG spec. But at any rate browsers won't render it properly without the XHTML namespace. --- epub.js | 5 +++-- mobi.js | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/epub.js b/epub.js index 6993488..d955753 100644 --- a/epub.js +++ b/epub.js @@ -673,8 +673,9 @@ class Loader { if ([MIME.XHTML, MIME.HTML, MIME.SVG].includes(mediaType)) { let doc = new DOMParser().parseFromString(str, mediaType) // change to HTML if it's not valid XHTML - if (mediaType === MIME.XHTML && doc.querySelector('parsererror')) { - console.warn(doc.querySelector('parsererror').innerText) + if (mediaType === MIME.XHTML && (doc.querySelector('parsererror') + || !doc.documentElement?.namespaceURI)) { + console.warn(doc.querySelector('parsererror')?.innerText ?? 'Invalid XHTML') item.mediaType = MIME.HTML doc = new DOMParser().parseFromString(str, item.mediaType) } diff --git a/mobi.js b/mobi.js index 31ff1dc..116477d 100644 --- a/mobi.js +++ b/mobi.js @@ -1154,7 +1154,7 @@ class KF8 { // by default, type is XHTML; change to HTML if it's not valid XHTML let doc = this.parser.parseFromString(replaced, this.#type) - if (doc.querySelector('parsererror')) { + if (doc.querySelector('parsererror') || !doc.documentElement?.namespaceURI) { this.#type = MIME.HTML doc = this.parser.parseFromString(replaced, this.#type) }