Skip to content

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

validate html table wire bodies

myhlolia0625f626476
diff --git a/mineru/model/flash/html/wire.py b/mineru/model/flash/html/wire.py
index 2ef2b3d8..760ce992 100644
--- a/mineru/model/flash/html/wire.py
+++ b/mineru/model/flash/html/wire.py
@@ -347,6 +347,8 @@ def _validate_visual_content(content_root: etree._Element, wrapper: etree._Eleme
raise _WireValidationError("visual_body_index_mismatch")
if parent_type == BlockType.CODE and (wrapper.get("data-block-sub-type") or "").strip() == BlockType.CODE:
_validate_code_body_shape(child)
+ elif parent_type == BlockType.TABLE:
+ _validate_table_body_shape(child)
for marker in _descendant_markers(child):
if marker is child:
continue
@@ -380,6 +382,27 @@ def _validate_code_body_shape(body: etree._Element) -> None:
raise _WireValidationError("invalid_code_body_shape")
+def _validate_table_body_shape(body: etree._Element) -> None:
+ """校验表格 body 只含一种规范载荷,避免精确物化静默丢弃旁路内容。"""
+ _validate_element_only_content(body)
+ children = _element_children(body)
+ if not children:
+ return
+ if len(children) != 1:
+ raise _WireValidationError("invalid_table_body_shape")
+ child = children[0]
+ name = local_name(child)
+ classes = _class_tokens(child)
+ if name == "table":
+ return
+ if name == "pre" and bool(classes & {"mineru-table-text", "mineru-raw-fallback"}):
+ if not _element_children(child):
+ return
+ elif name == "img" and "mineru-table-image" in classes:
+ return
+ raise _WireValidationError("invalid_table_body_shape")
+
+
def _validate_list_container(container: etree._Element, *, top_wrapper: etree._Element | None = None) -> None:
"""递归校验列表容器、叶子类型和嵌套列表 marker。"""
if local_name(container) not in {"ol", "ul"} or (container.get("data-block-type") or "").strip() != BlockType.LIST:
diff --git a/tests/unittest/test_flash_html.py b/tests/unittest/test_flash_html.py
index 20928286..66fca63a 100644
--- a/tests/unittest/test_flash_html.py
+++ b/tests/unittest/test_flash_html.py
@@ -1286,6 +1286,46 @@ def test_html_versioned_wire_edited_code_body_falls_back_without_loss() -> None:
assert "NEW NOTE" in markdown
+def test_html_versioned_wire_edited_table_body_falls_back_without_loss() -> None:
+ """验证表格 body 中新增可见节点会整体回退并保留表格与编辑内容。"""
+ source = MiddleJson.model_validate(
+ {
+ "pages": [
+ {
+ "page_idx": 0,
+ "blocks": [
+ {
+ "type": "table",
+ "index": 0,
+ "content": [
+ {
+ "type": "table_body",
+ "index": 0,
+ "content": "<table><tr><td>A</td></tr></table>",
+ }
+ ],
+ }
+ ],
+ }
+ ],
+ "is_full_document": True,
+ "file_suffix": "html",
+ "effort": "flash",
+ "parse_mode": "txt",
+ "mineru_version": "test",
+ }
+ )
+ soup = BeautifulSoup(render_html(source, standalone=False), "html.parser")
+ paragraph = soup.new_tag("p")
+ paragraph.string = "NEW NOTE"
+ soup.select_one('[data-block-type="table_body"]').append(paragraph)
+
+ markdown = render_markdown(doc_analyze(str(soup).encode(), file_suffix="html")[0])
+
+ assert "| A |" in markdown
+ assert "NEW NOTE" in markdown
+
+
def test_html_marker_fallback_does_not_double_resolve_images(monkeypatch: pytest.MonkeyPatch) -> None:
"""验证结构校验先于资源物化,非法 marker 回退只解析一次图片。"""
source = MiddleJson.model_validate(