Skip to content

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

preserve content outside HTML wire root

myhloli482346ef8426
diff --git a/mineru/model/flash/html/wire.py b/mineru/model/flash/html/wire.py
index 8421d4d1..8691a173 100644
--- a/mineru/model/flash/html/wire.py
+++ b/mineru/model/flash/html/wire.py
@@ -142,6 +142,7 @@ def inspect_mineru_html_wire(body: etree._Element) -> MineruHtmlWireInspection:
if (root.get("data-mineru-html-version") or "").strip() != _MINERU_HTML_VERSION:
return MineruHtmlWireInspection(None, "unsupported_version")
try:
+ _validate_wire_root_ownership(body, root)
return MineruHtmlWireInspection(_validate_wire_root(root))
except _WireValidationError as exc:
return MineruHtmlWireInspection(None, exc.reason)
@@ -211,6 +212,25 @@ def _validate_wire_root(root: etree._Element) -> MineruHtmlWirePlan:
return MineruHtmlWirePlan(root, mode, specs) # type: ignore[arg-type]
+def _validate_wire_root_ownership(body: etree._Element, root: etree._Element) -> None:
+ """要求 wire 根独占通往 body 的内容路径,避免精确物化丢弃外部正文。"""
+ current = root
+ while current is not body:
+ parent = current.getparent()
+ if parent is None:
+ raise _WireValidationError("invalid_root_location")
+ if (parent.text or "").strip():
+ raise _WireValidationError("content_outside_wire_root")
+ for sibling in parent:
+ if sibling is current:
+ if (sibling.tail or "").strip():
+ raise _WireValidationError("content_outside_wire_root")
+ continue
+ if isinstance(sibling.tag, str) or (sibling.tail or "").strip():
+ raise _WireValidationError("content_outside_wire_root")
+ current = parent
+
+
def _validate_top_block(wrapper: etree._Element, section_page_idx: int | None) -> MineruHtmlBlockSpec:
"""校验单个顶层 wrapper 的公开类型、元数据与内部机器树。"""
_validate_element_only_content(wrapper)
diff --git a/tests/unittest/test_flash_html.py b/tests/unittest/test_flash_html.py
index 6482810f..2844a9bb 100644
--- a/tests/unittest/test_flash_html.py
+++ b/tests/unittest/test_flash_html.py
@@ -1356,6 +1356,34 @@ def test_html_versioned_wire_visible_structural_text_falls_back_without_loss() -
assert edited_text in markdown
[email protected]("position", ["before", "after"])
+def test_html_versioned_wire_visible_sibling_falls_back_without_loss(position: str) -> None:
+ """验证 wire 根前后的可见兄弟会整体回退,避免精确物化静默丢弃正文。"""
+ source = MiddleJson.model_validate(
+ {
+ "pages": [{"page_idx": 0, "blocks": [{"type": "text", "index": 0, "content": "Original wire text"}]}],
+ "is_full_document": True,
+ "file_suffix": "html",
+ "effort": "flash",
+ "parse_mode": "txt",
+ "mineru_version": "test",
+ }
+ )
+ soup = BeautifulSoup(render_html(source, standalone=False), "html.parser")
+ sibling = soup.new_tag("p")
+ sibling.string = "VISIBLE SIBLING"
+ wire_root = soup.select_one(".mineru-document")
+ if position == "before":
+ wire_root.insert_before(sibling)
+ else:
+ wire_root.insert_after(sibling)
+
+ markdown = render_markdown(doc_analyze(str(soup).encode(), file_suffix="html")[0])
+
+ assert "Original wire text" in markdown
+ assert "VISIBLE SIBLING" in markdown
+
+
def test_html_versioned_wire_markerless_block_child_falls_back_without_crash() -> None:
"""验证行内容器内新增的无 marker 块节点会事务式回退,而不是在物化阶段抛错。"""
source = MiddleJson.model_validate(