Skip to content

refactor(parser): preserve HTML <table> structure (inline + structured item) - #18129

Merged
yuzhichang merged 3 commits into
infiniflow:mainfrom
xugangqiang:fix/html-table-structure
Aug 11, 2026
Merged

yuzhichang merged 3 commits into
infiniflow:mainfrom
xugangqiang:fix/html-table-structure

Conversation

@xugangqiang

Copy link
Copy Markdown
Contributor

Summary

Stop flattening <table> into a single text blob. A <table> now emits:

  1. an inlined doc_type_kwd:"text" item keeping the <table>…</table> markup
    (row/column structure survives for embedding/retrieval/LLM rendering), and
  2. a structured doc_type_kwd:"table" / ck_type:"table" item appended after
    the walk, consumed by the downstream chunker.

Covers both top-level tables (walkHTMLBlocks) and tables nested in
div/section/article (via walkHTMLLeaf). Mirrors markdown_parser.go's
two-item table handling (inline text copy + appended doc_type_kwd:"table"
item). It matches Python's RAGFlowHtmlParser only in keeping the raw
<table>…</table> markup: Python keeps the table once, outside the inline
flow, and adds doc_type_kwd/ck_type downstream — this change emits both
items at the parser layer.

Test plan

  • TestHTMLParser_TableProducesStructuredItems
  • TestHTMLParser_NestedTableProducesStructuredItems
  • TestHTMLParser_NestedListItemsPreserved
  • TestHTMLParser_PrePreservesVerbatim
  • TestHTMLParser_AlignmentGolden (compares vs html.python.golden.json,
    ignores "table" via meta.accepted_divergences)

Dependency

Note

This change was not described in the original combined PR's title; called out
here for explicit review.

Related

…d item)

Stop flattening <table> into a single text blob. A <table> now emits:
1. an inlined doc_type_kwd:"text" item keeping the <table>...</table> markup
   (row/column structure survives for embedding/retrieval/LLM rendering), and
2. a structured doc_type_kwd:"table" / ck_type:"table" item appended after
   the walk, consumed by the downstream chunker.

Covers both top-level tables (walkHTMLBlocks) and tables nested in
div/section/article (via walkHTMLLeaf). Mirrors markdown_parser.go table
handling and Python's RAGFlowHtmlParser.

Tests:
- TestHTMLParser_TableProducesStructuredItems
- TestHTMLParser_NestedTableProducesStructuredItems
- TestHTMLParser_NestedListItemsPreserved
- TestHTMLParser_PrePreservesVerbatim
- TestHTMLParser_AlignmentGolden (compares vs html.python.golden.json, ignores
  "table" via meta.accepted_divergences)

Depends on infiniflow#18014 and the foundation helpers PR (align_test.go).

Files:
  internal/parser/parser/html_parser.go
  internal/parser/parser/html_parser_align_test.go (new)
  internal/parser/parser/testdata/html.python.golden.json (new)
  internal/parser/parser/testdata/html.sample.html (new)
renderTableHTML only fails on unsupported node kinds, which a parsed
<table> never triggers, so the degenerate flatten-children branch in
walkHTMLLeaf is unreachable. Remove it so the nested-table path matches
the top-level walkHTMLBlocks case (both skip silently when the markup is
empty).

Also tighten the inaccurate 'mirrors Python's HtmlParser' inline comment
in the same block (the two-item split is the markdown_parser.go model;
Python keeps the table once, outside the inline flow).
@dosubot dosubot Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Aug 11, 2026
@coderabbitai

coderabbitai Bot commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The HTML parser now preserves tables as inline HTML and separate structured items. Nested tables retain their markup and content. Tests and English/Chinese golden fixtures validate table ordering, nested lists, verbatim text, and parser alignment.

Changes

HTML table preservation

Layer / File(s) Summary
Table serialization and structured emission
internal/parser/parser/html_parser.go
Top-level tables are emitted as inline HTML text and recorded as structured table items appended after traversal.
Nested table traversal
internal/parser/parser/html_parser.go
Leaf traversal propagates table collection state. Nested tables remain serialized in the containing text stream and structured output.
Parser alignment validation
internal/parser/parser/html_parser_align_test.go, internal/parser/parser/testdata/*
Tests and fixtures validate table output, nested content order, list handling, verbatim content, and English/Chinese parser alignment.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Sequence Diagram(s)

sequenceDiagram
  participant HTMLDocument
  participant walkHTMLBlocks
  participant walkHTMLLeaf
  participant renderHTMLTable
  participant ParserItems
  HTMLDocument->>walkHTMLBlocks: traverse document blocks
  walkHTMLBlocks->>walkHTMLLeaf: traverse leaf content
  walkHTMLLeaf->>renderHTMLTable: serialize encountered table
  renderHTMLTable->>ParserItems: emit inline table markup
  renderHTMLTable->>ParserItems: collect structured table item
  walkHTMLBlocks->>ParserItems: append structured tables after traversal
Loading

Possibly related issues

Possibly related PRs

Suggested labels: ☯️ refactor, 🧪 test, ci

Suggested reviewers: yuzhichang

Poem

A rabbit hops through markup bright,
Tables keep their shape and light.
Text stays ordered, nested too,
Golden tests confirm what’s true.
Carrots cheer the parser’s flight!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: preserving HTML table structure as inline and structured items.
Description check ✅ Passed The description includes a clear summary, background, implementation scope, tests, dependencies, and related context.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
internal/parser/parser/html_parser_align_test.go (1)

21-23: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Remove migration-history comments from the tests.

The current assertions already define the required table behavior. The descriptions of the pre-fix implementation will become stale.

  • internal/parser/parser/html_parser_align_test.go#L21-L23: remove the obsolete flattened-output description.
  • internal/parser/parser/html_parser_align_test.go#L92-L100: remove the obsolete nested-table traversal description.

As per coding guidelines, remove stale documentation and keep Go comments aligned with the current runtime path rather than migration history.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/parser/parser/html_parser_align_test.go` around lines 21 - 23,
Remove the obsolete migration-history comments from
internal/parser/parser/html_parser_align_test.go at lines 21-23 and 92-100;
retain the existing assertions and test logic, including the current table and
nested-table behavior coverage.

Source: Coding guidelines

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/parser/parser/html_parser.go`:
- Around line 136-157: Update both table-handling paths in
internal/parser/parser/html_parser.go (lines 136-157 and 323-341) to traverse
descendant tables before returning, ensuring nested tables produce structured
items with doc_type_kwd:"table" in addition to the parent table. Collapse the
duplicate collection logic into a shared helper or reusable path where
appropriate, and add a test covering a table nested inside a table cell.

---

Nitpick comments:
In `@internal/parser/parser/html_parser_align_test.go`:
- Around line 21-23: Remove the obsolete migration-history comments from
internal/parser/parser/html_parser_align_test.go at lines 21-23 and 92-100;
retain the existing assertions and test logic, including the current table and
nested-table behavior coverage.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 8602a36d-917d-4a9f-93ae-ae90e2c5ada9

📥 Commits

Reviewing files that changed from the base of the PR and between 3ef6e45 and f065e77.

📒 Files selected for processing (4)
  • internal/parser/parser/html_parser.go
  • internal/parser/parser/html_parser_align_test.go
  • internal/parser/parser/testdata/html.python.golden.json
  • internal/parser/parser/testdata/html.sample.html

Comment on lines +136 to +157
case "table":
// Keep the table as its full HTML markup (NOT flattened) so
// row/column structure survives into embedding, retrieval, and
// LLM rendering. This mirrors the markdown table handling
// (markdown_parser.go:305-328) and Python's HtmlParser, which
// keeps the <table>…</table> string as the section text.
markup := renderTableHTML(child)
if strings.TrimSpace(markup) != "" {
// Inlined copy in document order (matches Python's <table>
// section text and markdown's text-flow copy).
*out = append(*out, map[string]any{
"text": markup,
"doc_type_kwd": "text",
})
// Structured table item, appended after the walk.
tableItems = append(tableItems, map[string]any{
"text": markup,
"doc_type_kwd": "table",
"ck_type": "table",
})
}
continue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🗄️ Data Integrity & Integration | 🟠 Major | ⚡ Quick win

Collect structured items for tables nested inside table cells.

Both paths serialize the current table and return. If a <table> is inside a <td>, its markup is included in the parent table, but the child table is never visited as a table node. The child table therefore has no structured doc_type_kwd:"table" item.

Collect descendant table nodes before returning, and add a test with a table nested in a table cell.

  • internal/parser/parser/html_parser.go#L136-L157: collect structured items for child tables in the top-level table path.
  • internal/parser/parser/html_parser.go#L323-L341: collect structured items for child tables in the leaf traversal path.

As per coding guidelines, collapse duplicate paths in actively refactored parser code.

📍 Affects 1 file
  • internal/parser/parser/html_parser.go#L136-L157 (this comment)
  • internal/parser/parser/html_parser.go#L323-L341
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/parser/parser/html_parser.go` around lines 136 - 157, Update both
table-handling paths in internal/parser/parser/html_parser.go (lines 136-157 and
323-341) to traverse descendant tables before returning, ensuring nested tables
produce structured items with doc_type_kwd:"table" in addition to the parent
table. Collapse the duplicate collection logic into a shared helper or reusable
path where appropriate, and add a test covering a table nested inside a table
cell.

Source: Coding guidelines

Mirror the markdown/textcode alignment pattern: rename the English html
sample to _en, add a focused _zh sample (full-width delimiters, Chinese
table cells), and make TestHTMLParser_AlignmentGolden table-driven over both
cases. No generator script is committed; baselines stay reproducible from
each golden's meta block.

Co-Authored-By: CodeBuddy <[email protected]>
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. and removed size:M This PR changes 30-99 lines, ignoring generated files. labels Aug 11, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@internal/parser/parser/testdata/html.sample.en.html`:
- Line 1: Add <!doctype html> before the opening <html> element in both
internal/parser/parser/testdata/html.sample.en.html lines 1-1 and
internal/parser/parser/testdata/html.sample.zh.html lines 1-1, ensuring each
complete HTML fixture begins with the doctype declaration.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 36ea06fe-f925-4f27-89c5-9338c3809090

📥 Commits

Reviewing files that changed from the base of the PR and between f065e77 and f37a409.

📒 Files selected for processing (5)
  • internal/parser/parser/html_parser_align_test.go
  • internal/parser/parser/testdata/html.python.en.golden.json
  • internal/parser/parser/testdata/html.python.zh.golden.json
  • internal/parser/parser/testdata/html.sample.en.html
  • internal/parser/parser/testdata/html.sample.zh.html
🚧 Files skipped from review as they are similar to previous changes (1)
  • internal/parser/parser/html_parser_align_test.go

@@ -0,0 +1,11 @@
<html><body>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Add <!doctype html> to both HTML fixtures.

Both complete documents start with <html> and trigger the same doctype-first lint error.

  • internal/parser/parser/testdata/html.sample.en.html#L1-L1: add <!doctype html> before <html>.
  • internal/parser/parser/testdata/html.sample.zh.html#L1-L1: add <!doctype html> before <html>.
🧰 Tools
🪛 HTMLHint (1.9.2)

[error] 1-1: Doctype must be declared before any non-comment content.

(doctype-first)

📍 Affects 2 files
  • internal/parser/parser/testdata/html.sample.en.html#L1-L1 (this comment)
  • internal/parser/parser/testdata/html.sample.zh.html#L1-L1
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@internal/parser/parser/testdata/html.sample.en.html` at line 1, Add <!doctype
html> before the opening <html> element in both
internal/parser/parser/testdata/html.sample.en.html lines 1-1 and
internal/parser/parser/testdata/html.sample.zh.html lines 1-1, ensuring each
complete HTML fixture begins with the doctype declaration.

Source: Linters/SAST tools

@xugangqiang xugangqiang added the ci Continue Integration label Aug 11, 2026
@xugangqiang
xugangqiang requested a review from yuzhichang August 11, 2026 12:24
@yuzhichang
yuzhichang merged commit 875ca96 into infiniflow:main Aug 11, 2026
9 checks passed
@xugangqiang
xugangqiang deleted the fix/html-table-structure branch August 12, 2026 10:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants