Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

feat(epub): enhance EPUB viewer with section navigation and improved metadata handling

myhloliabb6fd585b95
diff --git a/mineru/integrations/docvortex.py b/mineru/integrations/docvortex.py
index 0934c225..33eb0e66 100644
--- a/mineru/integrations/docvortex.py
+++ b/mineru/integrations/docvortex.py
@@ -38,10 +38,10 @@ def read_source_properties(
try:
result = extract_metadata(data, file_suffix=suffix, source_context=source_context)
except DocumentError as exc:
- logger.warning("Source metadata unavailable: {}", exc)
+ logger.warning("metadata_unavailable: source metadata skipped: {}", exc)
return DocumentProperties()
for diagnostic in result.diagnostics:
- logger.warning("Source metadata: {}", diagnostic.message)
+ logger.warning("metadata_diagnostic: {}", diagnostic.message)
return result.metadata.document or DocumentProperties()
diff --git a/mineru/kit/gradio/i18n.py b/mineru/kit/gradio/i18n.py
index 93c67bb8..3f3caefd 100644
--- a/mineru/kit/gradio/i18n.py
+++ b/mineru/kit/gradio/i18n.py
@@ -76,10 +76,9 @@ MESSAGES: dict[str, tuple[str, str]] = {
"HTML 预览生成失败,仍可尝试解析该文件。",
),
"epub_loading": ("Loading EPUB…", "正在加载 EPUB…"),
- "epub_previous": ("Previous page", "上一页"),
- "epub_next": ("Next page", "下一页"),
- "epub_page": ("Page", "页码"),
- "epub_spine": ("Spine", "章节"),
+ "epub_previous": ("Previous section", "上一节"),
+ "epub_next": ("Next section", "下一节"),
+ "epub_spine": ("Section", "章节"),
"epub_contents": ("Contents", "目录"),
"epub_preview_failed": (
"Could not preview this EPUB file. You can still try converting it.",
diff --git a/mineru/resources/epub_preview/viewer.html b/mineru/resources/epub_preview/viewer.html
index a939b87c..e723b042 100644
--- a/mineru/resources/epub_preview/viewer.html
+++ b/mineru/resources/epub_preview/viewer.html
@@ -11,13 +11,13 @@
html, body { height: 100%; margin: 0; overflow: hidden; }
body { display: flex; flex-direction: column; font: 13px system-ui, sans-serif; background: #fff; color: #1f2937; }
#toolbar { display: flex; align-items: center; gap: 6px; min-height: 42px; padding: 4px 8px; box-sizing: border-box; border-bottom: 1px solid rgba(127,127,127,.25); }
- #toolbar button, #page, #toc { border: 1px solid rgba(127,127,127,.35); border-radius: 5px; background: transparent; color: inherit; padding: 5px 8px; font: inherit; }
+ #toolbar button, #section, #toc { border: 1px solid rgba(127,127,127,.35); border-radius: 5px; background: transparent; color: inherit; padding: 5px 8px; font: inherit; }
#toolbar button { min-width: 30px; cursor: pointer; }
- #toolbar button:disabled, #page:disabled { opacity: .45; cursor: default; }
+ #toolbar button:disabled, #section:disabled { opacity: .45; cursor: default; }
#toolbar button:hover:enabled { background: rgba(127,127,127,.12); }
- #page { width: 52px; text-align: center; padding: 5px 2px; appearance: textfield; }
- #page::-webkit-inner-spin-button, #page::-webkit-outer-spin-button { appearance: none; }
- #pages { min-width: 18px; text-align: center; }
+ #section { width: 52px; text-align: center; padding: 5px 2px; appearance: textfield; }
+ #section::-webkit-inner-spin-button, #section::-webkit-outer-spin-button { appearance: none; }
+ #sections { min-width: 18px; text-align: center; }
#toolbar > .separator { width: 1px; height: 20px; background: rgba(127,127,127,.35); margin: 0 2px; }
#toc { min-width: 0; max-width: min(48vw, 360px); }
#status { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; opacity: .72; }
@@ -38,11 +38,11 @@
</head>
<body>
<div id="toolbar">
- <button id="previous" type="button" aria-label="Previous page" disabled>‹</button>
- <label><span id="page-label" hidden></span><input id="page" type="number" min="1" value="1" disabled></label>
+ <button id="previous" type="button" aria-label="Previous section" disabled>‹</button>
+ <input id="section" type="number" min="1" value="1" aria-label="Section" disabled>
<span aria-hidden="true">/</span>
- <output id="pages">—</output>
- <button id="next" type="button" aria-label="Next page" disabled>›</button>
+ <output id="sections">—</output>
+ <button id="next" type="button" aria-label="Next section" disabled>›</button>
<span class="separator" aria-hidden="true"></span>
<select id="toc" aria-label="Table of contents" hidden></select>
<span id="status" role="status"></span>
diff --git a/mineru/resources/epub_preview/viewer.js b/mineru/resources/epub_preview/viewer.js
index 6ffab193..3a922822 100644
--- a/mineru/resources/epub_preview/viewer.js
+++ b/mineru/resources/epub_preview/viewer.js
@@ -5,8 +5,8 @@
const status = document.getElementById("status");
const viewer = document.getElementById("viewer");
const toc = document.getElementById("toc");
- const pageInput = document.getElementById("page");
- const pagesOutput = document.getElementById("pages");
+ const sectionInput = document.getElementById("section");
+ const sectionsOutput = document.getElementById("sections");
const previous = document.getElementById("previous");
const next = document.getElementById("next");
let book;
@@ -18,23 +18,32 @@
let resizeObserver;
let observedScrollContainer;
let activeSpineIndex = null;
- let navigationSpineIndex = null;
+ let pendingSpineIndex = null;
+ let navigationSequence = 0;
+ let navigationQueue = Promise.resolve();
const SPINE_SWITCH_RATIO = 0.5;
+ const MAX_EPUB_BYTES = 128 * 1024 * 1024;
+ const SANITIZED_MARKER = "data-mineru-sanitized";
+ const SANITIZED_TOKEN = Array.from(crypto.getRandomValues(new Uint32Array(4)), (value) => value.toString(16)).join("-");
+ const FONT_OBFUSCATION_ALGORITHMS = new Set([
+ "http://www.idpf.org/2008/embedding",
+ "http://ns.adobe.com/pdf/enc#RC",
+ ]);
const fail = (error) => {
failed = true;
status.textContent = messages.epub_preview_failed || "Could not load EPUB preview.";
status.setAttribute("role", "alert");
- pageInput.disabled = previous.disabled = next.disabled = toc.disabled = true;
+ sectionInput.disabled = previous.disabled = next.disabled = toc.disabled = true;
console.warn("EPUB preview failed", error);
};
- previous.title = previous.ariaLabel = messages.epub_previous || "Previous page";
- next.title = next.ariaLabel = messages.epub_next || "Next page";
- pageInput.setAttribute("aria-label", messages.epub_spine || messages.epub_page || "Spine");
+ previous.title = previous.ariaLabel = messages.epub_previous || "Previous section";
+ next.title = next.ariaLabel = messages.epub_next || "Next section";
+ sectionInput.setAttribute("aria-label", messages.epub_spine || "Section");
toc.ariaLabel = messages.epub_contents || "Contents";
status.textContent = messages.epub_loading || "Loading EPUB…";
- pageInput.disabled = previous.disabled = next.disabled = true;
+ sectionInput.disabled = previous.disabled = next.disabled = true;
const isExternalLink = (href) => /^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test(href);
@@ -47,7 +56,7 @@
return `${resolved.pathname.replace(/^\/+/, "")}${resolved.search}${resolved.hash}`;
};
- // 根据当前视口内各个 spine 的可见比例更新顶部编号,避免长章节滚动时误切页。
+ // 根据当前视口内各个 spine 的可见比例更新顶部编号,避免长章节滚动时误切章节。
const updateActiveSpine = () => {
if (!book || !rendition || !viewer) return;
const bounds = viewer.getBoundingClientRect();
@@ -80,26 +89,62 @@
manager.settings.offset = Math.max(400, Math.round(viewer.clientHeight || 400));
};
- // 等待连续管理器完成相邻 spine 的挂载后,把目标 spine 直接对齐到阅读窗口顶部。
- const focusDisplayedSection = async (section, target = "") => {
- if (!section || !scrollContainer) return;
+ // 等待连续管理器完成目标 spine 挂载;最终只滚动一次,锚点暂不可定位时回退到章节顶部。
+ const focusDisplayedSection = async (section, target = "", navigationId = null) => {
+ if (!section || !scrollContainer) return false;
+ let fallbackTop = null;
+ let resolvedTop = null;
for (let attempt = 0; attempt < 12; attempt += 1) {
await new Promise((resolve) => requestAnimationFrame(resolve));
+ if (navigationId !== null && navigationId !== navigationSequence) return false;
let targetView;
const views = rendition?.views?.();
views?.forEach?.((view) => {
if (view?.section?.index === section.index || view?.index === section.index) targetView = view;
});
if (!targetView?.element) continue;
- let top = targetView.element.offsetTop;
+ fallbackTop = targetView.element.offsetTop;
+ let top = fallbackTop;
if (target.includes("#") && typeof targetView.locationOf === "function") {
const location = targetView.locationOf(target);
- if (Number.isFinite(location?.top)) top += Math.max(0, location.top);
+ if (!Number.isFinite(location?.top)) continue;
+ top += Math.max(0, location.top);
}
- scrollContainer.scrollTo({top: Math.max(0, top), behavior: "auto"});
+ resolvedTop = top;
+ break;
}
+ const top = resolvedTop ?? fallbackTop;
+ if (!Number.isFinite(top)) return false;
+ scrollContainer.scrollTo({top: Math.max(0, top), behavior: "auto"});
+ return true;
};
+ // 串行执行章节导航,并用递增序号淘汰旧请求,避免快速点击时旧 display 结果覆盖最后一次操作。
+ const enqueueNavigation = (section, target) => {
+ if (!section || failed || disposed || !rendition) return Promise.resolve();
+ const navigationId = ++navigationSequence;
+ pendingSpineIndex = Number.isInteger(section.index) ? section.index : null;
+ scheduleSectionUpdate();
+ navigationQueue = navigationQueue.catch(() => undefined).then(async () => {
+ if (navigationId !== navigationSequence || failed || disposed) return;
+ try {
+ await rendition.display(target);
+ if (navigationId !== navigationSequence || failed || disposed) return;
+ await focusDisplayedSection(section, target, navigationId);
+ if (navigationId !== navigationSequence || failed || disposed) return;
+ activeSpineIndex = Number.isInteger(section.index) ? section.index : activeSpineIndex;
+ pendingSpineIndex = null;
+ scheduleSectionUpdate();
+ } catch (error) {
+ if (navigationId !== navigationSequence || disposed) return;
+ pendingSpineIndex = null;
+ fail(error);
+ }
+ });
+ return navigationQueue;
+ };
+
+ // 将目录、脚注等包内链接统一映射为 spine,再交给串行导航入口处理。
const displayTarget = async (href, sourceHref = "") => {
const rawTarget = resolveInternalTarget(href, sourceHref);
if (!rawTarget || failed || disposed || !rendition) return;
@@ -108,66 +153,48 @@
// 无法映射到 OPF spine 的内部链接只在当前视图中降级,不能让整个阅读器进入失败状态。
if (!section) {
console.warn("EPUB internal target not found", {href, sourceHref, target: rawTarget});
- navigationSpineIndex = null;
return;
}
const target = `${section.href}${targetHash ? `#${targetHash}` : ""}`;
- navigationSpineIndex = Number.isInteger(section?.index) ? section.index : null;
- try {
- await rendition.display(target);
- await focusDisplayedSection(section, target);
- if (navigationSpineIndex !== null) activeSpineIndex = navigationSpineIndex;
- navigationSpineIndex = null;
- schedulePageUpdate();
- } catch (error) {
- navigationSpineIndex = null;
- fail(error);
- }
+ await enqueueNavigation(section, target);
};
- // 直接显示相邻 spine 的起点,上一页/下一页不再模拟窗口分页。
+ // 直接显示相邻 spine 的起点,上一节/下一节不再模拟窗口分页。
const goToSpine = async (value) => {
if (disposed || failed || !book || !rendition) return;
const index = Number(value) - 1;
const count = Number(book.spine?.length || 0);
if (!Number.isInteger(index) || index < 0 || index >= count) {
- schedulePageUpdate();
+ scheduleSectionUpdate();
return;
}
const section = book.spine.get(index);
if (!section) return;
- navigationSpineIndex = index;
- try {
- await rendition.display(section.href);
- await focusDisplayedSection(section, section.href);
- activeSpineIndex = index;
- navigationSpineIndex = null;
- schedulePageUpdate();
- } catch (error) {
- navigationSpineIndex = null;
- fail(error);
- }
+ await enqueueNavigation(section, section.href);
};
- const updatePageControls = () => {
+
+ // 根据当前可见或待导航 spine 刷新章节编号和按钮状态。
+ const updateSectionControls = () => {
updateFrame = undefined;
if (disposed || failed) return;
- updateActiveSpine();
+ if (pendingSpineIndex === null) updateActiveSpine();
const spineCount = Math.max(1, Number(book?.spine?.length || 1));
- const currentSpine = Math.min(spineCount, Math.max(1, (activeSpineIndex ?? 0) + 1));
- pageInput.disabled = previous.disabled = next.disabled = false;
- pageInput.max = String(spineCount);
- pageInput.value = String(currentSpine);
- pagesOutput.textContent = String(spineCount);
+ const currentIndex = pendingSpineIndex ?? activeSpineIndex ?? 0;
+ const currentSpine = Math.min(spineCount, Math.max(1, currentIndex + 1));
+ sectionInput.disabled = previous.disabled = next.disabled = false;
+ sectionInput.max = String(spineCount);
+ sectionInput.value = String(currentSpine);
+ sectionsOutput.textContent = String(spineCount);
previous.disabled = currentSpine <= 1;
next.disabled = currentSpine >= spineCount;
- status.textContent = `${currentSpine} / ${spineCount}`;
+ status.textContent = `${messages.epub_spine || "Section"} ${currentSpine} / ${spineCount}`;
};
- const schedulePageUpdate = () => {
- if (updateFrame === undefined) updateFrame = requestAnimationFrame(updatePageControls);
+ const scheduleSectionUpdate = () => {
+ if (updateFrame === undefined) updateFrame = requestAnimationFrame(updateSectionControls);
};
- // 在未挂载的章节 DOM 中注入 CSP,并绑定统一的 EPUB 内部链接导航。
+ // 在章节序列化进 iframe 前移除主动内容和远程资源,并注入严格 CSP。
const sanitizeDocument = (contents) => {
const candidates = [contents?.document, contents?.ownerDocument, contents];
const doc = candidates.find((candidate) => candidate && typeof candidate.querySelectorAll === "function");
@@ -188,19 +215,115 @@
const stylesheetResource = localName === "link" && name === "href"
&& /(?:^|\s)stylesheet(?:\s|$)/i.test(node.getAttribute("rel") || "");
const embeddedResource = /^(?:blob:|data:)/i.test(value) && (imageResource || stylesheetResource);
- if (!embeddedResource) {
- node.removeAttribute(attribute.name);
- }
+ if (!embeddedResource) node.removeAttribute(attribute.name);
}
});
});
const head = doc.querySelector("head");
if (!head) throw new Error("Invalid EPUB chapter: missing head");
- const policy = doc.createElementNS("http://www.w3.org/1999/xhtml", "meta");
+ const namespace = doc.documentElement?.namespaceURI || "http://www.w3.org/1999/xhtml";
+ const policy = doc.createElementNS(namespace, "meta");
policy.setAttribute("http-equiv", "Content-Security-Policy");
policy.setAttribute("content", "default-src 'none'; script-src 'none'; style-src 'unsafe-inline' blob: data:; " +
"img-src blob: data:; font-src blob: data:; frame-src 'none'; object-src 'none'; form-action 'none'");
head.prepend(policy);
+ doc.documentElement?.setAttribute(SANITIZED_MARKER, SANITIZED_TOKEN);
+ return doc;
+ };
+
+ // 用 namespace-safe 的实现恢复 EPUB.js 默认 base/canonical/identifier 元数据,避免 XMLDocument.createElement 的兼容问题。
+ const restoreSpineMetadata = (doc, section) => {
+ const head = doc.querySelector("head");
+ if (!head) throw new Error("Invalid EPUB chapter: missing head");
+ const namespace = doc.documentElement?.namespaceURI || "http://www.w3.org/1999/xhtml";
+ const createElement = (name) => doc.createElementNS(namespace, name);
+
+ let base = head.querySelector("base");
+ if (!base) {
+ base = createElement("base");
+ head.appendChild(base);
+ }
+ let sectionUrl = String(section?.url || "");
+ if (sectionUrl && !sectionUrl.includes("://")) sectionUrl = new URL(sectionUrl, location.origin).href;
+ if (sectionUrl) base.setAttribute("href", sectionUrl);
+
+ let canonical = head.querySelector('link[rel="canonical"]');
+ if (!canonical) {
+ canonical = createElement("link");
+ canonical.setAttribute("rel", "canonical");
+ head.appendChild(canonical);
+ }
+ if (section?.canonical) canonical.setAttribute("href", String(section.canonical));
+
+ let identifier = head.querySelector('meta[name="dc.identifier"], meta[property="dc.identifier"], link[property="dc.identifier"]');
+ if (!identifier) {
+ identifier = createElement("meta");
+ identifier.setAttribute("name", "dc.identifier");
+ head.appendChild(identifier);
+ }
+ if (section?.idref) identifier.setAttribute("content", String(section.idref));
+ };
+
+ // 替换 EPUB.js 依赖 XMLDocument.createElement 的默认 spine hooks,同时保留其元数据语义并确保安全清理发生在 iframe 创建前。
+ const installSafeSpineContentHook = (currentBook) => {
+ currentBook.spine.hooks.content.clear();
+ currentBook.spine.hooks.content.register((document, section) => {
+ const doc = sanitizeDocument(document);
+ restoreSpineMetadata(doc, section);
+ });
+ };
+
+ // 读取响应时实时限制字节数,避免超大 EPUB 在完整 arrayBuffer 分配后才被拒绝。
+ const readResponseWithLimit = async (response, maxBytes) => {
+ const lengthHeader = response.headers.get("content-length");
+ const declaredSize = lengthHeader ? Number(lengthHeader) : Number.NaN;
+ if (Number.isFinite(declaredSize) && declaredSize > maxBytes) {
+ await response.body?.cancel?.();
+ throw new Error("EPUB exceeds preview size limit");
+ }
+ const reader = response.body?.getReader?.();
+ if (!reader) {
+ const payload = await response.arrayBuffer();
+ if (payload.byteLength > maxBytes) throw new Error("EPUB exceeds preview size limit");
+ return payload;
+ }
+ const chunks = [];
+ let total = 0;
+ while (true) {
+ const {done, value} = await reader.read();
+ if (done) break;
+ total += value.byteLength;
+ if (total > maxBytes) {
+ await reader.cancel();
+ throw new Error("EPUB exceeds preview size limit");
+ }
+ chunks.push(value);
+ }
+ const bytes = new Uint8Array(total);
+ let offset = 0;
+ for (const chunk of chunks) {
+ bytes.set(chunk, offset);
+ offset += chunk.byteLength;
+ }
+ return bytes.buffer;
+ };
+
+ // encryption.xml 也用于 EPUB 标准字体混淆;仅拒绝其中出现的未知/真实加密算法。
+ const ensureSupportedEncryption = async (currentBook) => {
+ const encryption = await currentBook.archive?.getText?.("/META-INF/encryption.xml");
+ if (!encryption) return;
+ const document = new DOMParser().parseFromString(encryption, "application/xml");
+ const elements = Array.from(document.getElementsByTagName("*"));
+ if (elements.some((element) => element.localName === "parsererror")) {
+ throw new Error("Invalid EPUB encryption metadata");
+ }
+ const encryptedItems = elements.filter((element) => element.localName === "EncryptedData");
+ const unsupported = encryptedItems.map((item) => {
+ const method = Array.from(item.getElementsByTagName("*"))
+ .find((element) => element.localName === "EncryptionMethod");
+ return method?.getAttribute("Algorithm")?.trim() || "";
+ }).filter((algorithm) => !FONT_OBFUSCATION_ALGORITHMS.has(algorithm));
+ if (unsupported.length) throw new Error("Encrypted EPUB preview is unsupported");
};
try {
@@ -210,11 +333,10 @@
}
const response = await fetch(url, {credentials: "same-origin", signal: AbortSignal.timeout(30000)});
if (!response.ok) throw new Error(`EPUB fetch failed: ${response.status}`);
- const payload = await response.arrayBuffer();
- if (payload.byteLength > 128 * 1024 * 1024) throw new Error("EPUB exceeds preview size limit");
- const archive = await JSZip.loadAsync(payload);
- if (archive.file("META-INF/encryption.xml")) throw new Error("Encrypted EPUB preview is unsupported");
+ const payload = await readResponseWithLimit(response, MAX_EPUB_BYTES);
book = ePub();
+ await book.open(payload, "binary");
+ await ensureSupportedEncryption(book);
rendition = book.renderTo(viewer, {
manager: "continuous",
flow: "scrolled-continuous",
@@ -226,13 +348,12 @@
// 初始只加载当前 spine 附近内容,具体范围会在 viewport 建立后动态更新。
offset: 800,
});
- // 只保留 iframe 内的安全清理;EPUB.js 的原始 spine hook 依赖 XMLDocument.createElement,
- // 在浏览器沙箱的 XMLDocument 实现中会阻断后续章节加载。
- book.spine.hooks.content.clear();
- // Safari 会阻止父页面向只有 allow-same-origin 的 srcdoc iframe 派发点击事件;
- // 这里仅为事件兼容补上 allow-scripts,章节脚本仍由清理逻辑和 CSP 的 script-src 'none' 禁止。
+ installSafeSpineContentHook(book);
+ // Safari 的章节点击兼容仅对已经在序列化前清理且带严格 CSP 的 srcdoc 开放脚本 sandbox 能力。
const enableChapterIframeEvents = () => {
viewer.querySelectorAll(".epub-view > iframe").forEach((frame) => {
+ const srcdoc = frame.getAttribute("srcdoc") || "";
+ if (!srcdoc.includes(`${SANITIZED_MARKER}="${SANITIZED_TOKEN}"`) || !srcdoc.includes("script-src 'none'")) return;
const sandbox = frame.getAttribute("sandbox") || "";
if (!/\ballow-scripts\b/.test(sandbox)) {
frame.setAttribute("sandbox", `${sandbox} allow-scripts`.trim());
@@ -247,16 +368,18 @@
const bindScrollContainer = () => {
const nextScrollContainer = viewer.querySelector(".epub-container") || viewer;
if (nextScrollContainer === observedScrollContainer) return;
- observedScrollContainer?.removeEventListener("scroll", schedulePageUpdate);
+ observedScrollContainer?.removeEventListener("scroll", scheduleSectionUpdate);
observedScrollContainer = nextScrollContainer;
scrollContainer = nextScrollContainer;
- scrollContainer.addEventListener("scroll", schedulePageUpdate, {passive: true});
+ scrollContainer.addEventListener("scroll", scheduleSectionUpdate, {passive: true});
resizeObserver?.observe(scrollContainer);
updateLoadWindow();
- schedulePageUpdate();
+ scheduleSectionUpdate();
};
rendition.hooks.content.register((contents) => {
- sanitizeDocument(contents.document);
+ if (contents.document?.documentElement?.getAttribute(SANITIZED_MARKER) !== SANITIZED_TOKEN) {
+ throw new Error("EPUB chapter reached iframe without sanitization");
+ }
// EPUB.js Contents 使用 sectionIndex 标识所属 spine;不能使用不存在的 contents.index。
const section = Number.isInteger(contents?.sectionIndex) ? book.spine.get(contents.sectionIndex) : null;
const sectionHref = section?.href || contents.section?.href || contents.href || "";
@@ -276,7 +399,7 @@
}
bindScrollContainer();
updateLoadWindow();
- schedulePageUpdate();
+ scheduleSectionUpdate();
});
window.addEventListener("pagehide", () => {
disposed = true;
@@ -287,16 +410,16 @@
resizeObserver = new ResizeObserver(() => {
bindScrollContainer();
updateLoadWindow();
- schedulePageUpdate();
+ scheduleSectionUpdate();
});
resizeObserver.observe(viewer);
bindScrollContainer();
- previous.addEventListener("click", () => goToSpine(Number(pageInput.value) - 1));
- next.addEventListener("click", () => goToSpine(Number(pageInput.value) + 1));
- pageInput.addEventListener("change", () => goToSpine(pageInput.value));
- pageInput.addEventListener("keydown", (event) => { if (event.key === "Enter") goToSpine(pageInput.value); });
+ previous.addEventListener("click", () => goToSpine(Number(sectionInput.value) - 1));
+ next.addEventListener("click", () => goToSpine(Number(sectionInput.value) + 1));
+ sectionInput.addEventListener("change", () => goToSpine(sectionInput.value));
+ sectionInput.addEventListener("keydown", (event) => { if (event.key === "Enter") goToSpine(sectionInput.value); });
rendition.on("displayerror", fail);
- rendition.on("relocated", schedulePageUpdate);
+ rendition.on("relocated", scheduleSectionUpdate);
const appendContents = (items, depth = 0) => {
for (const item of items) {
const href = item.href || "";
@@ -310,7 +433,6 @@
}
};
toc.addEventListener("change", () => displayTarget(toc.value));
- await book.open(payload, "binary");
// 导航目录在 book.open() 后才由 EPUB 包的 navigation 文档填充,不能提前读取。
await book.loaded.navigation;
appendContents(book.navigation?.toc || []);
@@ -319,7 +441,7 @@
await new Promise((resolve) => requestAnimationFrame(() => requestAnimationFrame(resolve)));
updateLoadWindow();
updateActiveSpine();
- schedulePageUpdate();
+ scheduleSectionUpdate();
} catch (error) {
fail(error);
}
diff --git a/mineru/resources/gradio_source_preview.js b/mineru/resources/gradio_source_preview.js
index 16cc8920..8340d10b 100644
--- a/mineru/resources/gradio_source_preview.js
+++ b/mineru/resources/gradio_source_preview.js
@@ -41,7 +41,7 @@
const book = fileUrl(window[fileKey], viewerUrl);
const params = new URLSearchParams({
book,
- messages: JSON.stringify(Object.fromEntries(["epub_preview_failed", "epub_loading", "epub_previous", "epub_next", "epub_page", "epub_spine", "epub_contents"]
+ messages: JSON.stringify(Object.fromEntries(["epub_preview_failed", "epub_loading", "epub_previous", "epub_next", "epub_spine", "epub_contents"]
.map((name) => [name, text(name, "")]))),
});
const url = new URL(viewerUrl);
diff --git a/tests/unittest/test_kit_gradio_epub_preview.py b/tests/unittest/test_kit_gradio_epub_preview.py
index 90723d2b..6b34df8b 100644
--- a/tests/unittest/test_kit_gradio_epub_preview.py
+++ b/tests/unittest/test_kit_gradio_epub_preview.py
@@ -10,6 +10,7 @@ from fastapi import FastAPI
from fastapi.testclient import TestClient
from mineru.kit.gradio.epub_preview import register_epub_preview_resources
+from mineru.kit.gradio.i18n import MESSAGES
def test_epub_preview_static_routes_are_local_and_complete(tmp_path: Path) -> None:
@@ -23,6 +24,7 @@ def test_epub_preview_static_routes_are_local_and_complete(tmp_path: Path) -> No
(entry, "text/html"),
(entry.with_name("epub.min.js"), "text/javascript"),
(entry.with_name("jszip.min.js"), "text/javascript"),
+ (entry.with_name("viewer.js"), "text/javascript"),
):
response = client.get(f"/gradio_api/file={quote(str(path), safe='/')}")
assert response.status_code == 200
@@ -30,8 +32,8 @@ def test_epub_preview_static_routes_are_local_and_complete(tmp_path: Path) -> No
assert response.headers["content-disposition"].startswith("inline")
-def test_epub_preview_viewer_contains_script_and_remote_resource_guards() -> None:
- """viewer 默认关闭 EPUB 脚本、弹窗和外部资源访问。"""
+def test_epub_preview_viewer_sanitizes_before_enabling_chapter_events() -> None:
+ """章节必须在进入 iframe 前完成清理,Safari 事件兼容也只对已清理 srcdoc 开启。"""
entry = register_epub_preview_resources()
document = entry.read_text(encoding="utf-8")
viewer = entry.with_name("viewer.js").read_text(encoding="utf-8")
@@ -40,14 +42,46 @@ def test_epub_preview_viewer_contains_script_and_remote_resource_guards() -> Non
assert 'connect-src \'self\'' in document
assert "[a-z][a-z\\d+.-]*:" in viewer
assert "node.removeAttribute" in viewer
+ assert 'const SANITIZED_MARKER = "data-mineru-sanitized"' in viewer
+ assert "sanitizeDocument(document)" in viewer
+ assert "restoreSpineMetadata(doc, section)" in viewer
+ assert "installSafeSpineContentHook(book)" in viewer
+ assert "SANITIZED_TOKEN = Array.from(crypto.getRandomValues" in viewer
+ assert 'srcdoc.includes(`${SANITIZED_MARKER}="${SANITIZED_TOKEN}"`)' in viewer
+ assert 'getAttribute(SANITIZED_MARKER) !== SANITIZED_TOKEN' in viewer
+ assert 'srcdoc.includes("script-src \'none\'")' in viewer
+ assert "sanitizeDocument(contents.document)" not in viewer
+ assert viewer.index("installSafeSpineContentHook(book)") < viewer.index("await displayTarget(book.spine.first()?.href")
-def test_epub_preview_viewer_uses_continuous_spine_navigation_and_internal_links() -> None:
- """viewer 使用连续管理器、真实 spine 编号和统一的包内链接跳转。"""
+def test_epub_preview_viewer_accepts_font_obfuscation_and_stream_limits_payload() -> None:
+ """encryption.xml 只拒绝未知算法,并在下载过程中限制 EPUB 大小。"""
+ entry = register_epub_preview_resources()
+ viewer = entry.with_name("viewer.js").read_text(encoding="utf-8")
+ assert "MAX_EPUB_BYTES = 128 * 1024 * 1024" in viewer
+ assert 'response.headers.get("content-length")' in viewer
+ assert "response.body?.cancel?.()" in viewer
+ assert "response.body?.getReader?.()" in viewer
+ assert "await reader.cancel()" in viewer
+ assert "JSZip.loadAsync(payload)" not in viewer
+ assert 'currentBook.archive?.getText?.("/META-INF/encryption.xml")' in viewer
+ assert "http://www.idpf.org/2008/embedding" in viewer
+ assert "http://ns.adobe.com/pdf/enc#RC" in viewer
+ assert "archive.file(\"META-INF/encryption.xml\")" not in viewer
+ assert 'await book.open(payload, "binary")' in viewer
+ assert "await ensureSupportedEncryption(book)" in viewer
+
+
+def test_epub_preview_viewer_uses_serialized_section_navigation_and_internal_links() -> None:
+ """viewer 使用连续管理器、章节语义和串行导航,快速操作不会被旧 display 覆盖。"""
entry = register_epub_preview_resources()
document = entry.read_text(encoding="utf-8")
viewer = entry.with_name("viewer.js").read_text(encoding="utf-8")
- assert 'id="page"' in document and 'id="pages"' in document
+ assert 'id="section"' in document and 'id="sections"' in document
+ assert 'id="page"' not in document and 'id="pages"' not in document
+ assert MESSAGES["epub_previous"] == ("Previous section", "上一节")
+ assert MESSAGES["epub_next"] == ("Next section", "下一节")
+ assert MESSAGES["epub_spine"] == ("Section", "章节")
assert "#viewer .epub-container" in document and "min-height: 100%" in document
assert "margin: 0 !important" in document
assert 'manager: "continuous"' in viewer
@@ -63,16 +97,16 @@ def test_epub_preview_viewer_uses_continuous_spine_navigation_and_internal_links
assert "resolveInternalTarget" in viewer
assert "contents.document?.addEventListener" in viewer
assert "event.stopImmediatePropagation" in viewer
- assert "book.spine.hooks.content.clear()" in viewer
assert 'localName === "image"' in viewer
- assert 'stylesheetResource' in viewer
+ assert "stylesheetResource" in viewer
assert "contents.sectionIndex" in viewer
assert "book.spine.get(contents.index)" not in viewer
assert "EPUB internal target not found" in viewer
- assert "if (!section)" in viewer
- assert "MutationObserver" in viewer
- assert "allow-scripts" in viewer
- assert "script-src 'none'" in viewer
- assert viewer.index("if (!section)") < viewer.index("fail(error)")
+ assert "navigationSequence" in viewer and "navigationQueue" in viewer
+ assert "navigationId !== navigationSequence" in viewer
+ assert "pendingSpineIndex" in viewer
+ assert viewer.count("scrollContainer.scrollTo(") == 1
+ assert "return true;" in viewer
+ assert 'if (!section) {\n console.warn("EPUB internal target not found"' in viewer
assert viewer.index('await book.open(payload, "binary")') < viewer.index("appendContents(book.navigation?.toc || [])")
assert viewer.index("await book.loaded.navigation") < viewer.index("appendContents(book.navigation?.toc || [])")
diff --git a/tests/unittest/test_kit_gradio_source_preview.cjs b/tests/unittest/test_kit_gradio_source_preview.cjs
index c543fb3d..8c2a39c0 100644
--- a/tests/unittest/test_kit_gradio_source_preview.cjs
+++ b/tests/unittest/test_kit_gradio_source_preview.cjs
@@ -25,7 +25,8 @@ const epubTicket = JSON.parse(preview('begin', epub)[0]);
const epubFrame = preview('apply', JSON.stringify({id: epubTicket.id, kind: 'epub'}), viewer);
assert.match(epubFrame, /class="mineru-epub-frame"/);
assert.match(epubFrame, /book=https%3A%2F%2Fdemo.example.test%2Fgradio_api%2Ffile%3D%2Ftmp%2F%25E4%25B8%25AD%25E6%2596%2587%2520book.epub/);
-assert.match(epubFrame, /epub_page/);
+assert.match(epubFrame, /epub_spine/);
+assert.doesNotMatch(epubFrame, /epub_page/);
preview('clear');
assert.equal(preview('apply', JSON.stringify({id: epubTicket.id, kind: 'epub'}), viewer).__type__, 'update');
console.log('Source preview: switch, clear, re-upload and stale receipts passed');