Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(parser): place a table's footer rows after its body (#21283)

Lukas4969b7b41a59
diff --git a/internal/parser/tableutil/table_html.go b/internal/parser/tableutil/table_html.go
index c618ef3e57..6908139a6b 100644
--- a/internal/parser/tableutil/table_html.go
+++ b/internal/parser/tableutil/table_html.go
@@ -52,6 +52,12 @@ func ParseTableHTML(s string) (*entity.TableData, error) {
var rows [][]CellInput
var headerFlags []bool
var caption string
+ // A <tfoot> shows below the body wherever the markup puts it, and HTML 4
+ // required it before <tbody>, so its rows are collected apart and placed
+ // last; read in source order, a "Total" row came before the data it sums.
+ var footRows [][]CellInput
+ var footFlags []bool
+ inFoot := false
var walk func(*html.Node)
walk = func(n *html.Node) {
if n.Type == html.ElementNode && isInertElement(n.Data) {
@@ -61,6 +67,25 @@ func ParseTableHTML(s string) (*entity.TableData, error) {
caption = cellText(n)
return
}
+ // A table's footer goes below that table's own body, not below every table
+ // the fragment holds
+ if isHTMLElement(n, "table") && !inFoot {
+ for c := n.FirstChild; c != nil; c = c.NextSibling {
+ walk(c)
+ }
+ rows = append(rows, footRows...)
+ headerFlags = append(headerFlags, footFlags...)
+ footRows, footFlags = nil, nil
+ return
+ }
+ if isHTMLElement(n, "tfoot") && !inFoot {
+ inFoot = true
+ for c := n.FirstChild; c != nil; c = c.NextSibling {
+ walk(c)
+ }
+ inFoot = false
+ return
+ }
if isHTMLElement(n, "tr") {
var cells []CellInput
anyTh := false
@@ -76,6 +101,11 @@ func ParseTableHTML(s string) (*entity.TableData, error) {
})
}
}
+ if inFoot {
+ footRows = append(footRows, cells)
+ footFlags = append(footFlags, anyTh)
+ return
+ }
rows = append(rows, cells)
headerFlags = append(headerFlags, anyTh)
// Do not recurse into the row's cells: a nested <table> inside a
@@ -88,6 +118,8 @@ func ParseTableHTML(s string) (*entity.TableData, error) {
}
}
walk(doc)
+ rows = append(rows, footRows...)
+ headerFlags = append(headerFlags, footFlags...)
return BuildTableData(rows, headerFlags, caption), nil
}
diff --git a/internal/parser/tableutil/table_html_test.go b/internal/parser/tableutil/table_html_test.go
index 80b365043a..40d0269de5 100644
--- a/internal/parser/tableutil/table_html_test.go
+++ b/internal/parser/tableutil/table_html_test.go
@@ -47,6 +47,39 @@ func TestParseTableHTMLHappyPath(t *testing.T) {
assertRowsEqual(t, td.CellTexts(), want)
}
+// HTML 4 required <tfoot> before <tbody>, and a browser still draws it
+// below the body, so its rows read last; a footer of <th> cells is not part
+// of the header either.
+func TestParseTableHTMLFooterRowsComeLast(t *testing.T) {
+ html := `<table><thead><tr><th>Item</th><th>Qty</th></tr></thead>` +
+ `<tfoot><tr><th>Total</th><th>3</th></tr></tfoot>` +
+ `<tbody><tr><td>pen</td><td>1</td></tr><tr><td>ink</td><td>2</td></tr></tbody></table>`
+ td := mustParse(t, html)
+ if td.HeaderRows != 1 {
+ t.Fatalf("HeaderRows = %d, want 1", td.HeaderRows)
+ }
+ want := [][]string{
+ {"Item", "Qty"},
+ {"pen", "1"},
+ {"ink", "2"},
+ {"Total", "3"},
+ }
+ assertRowsEqual(t, td.CellTexts(), want)
+}
+
+func TestParseTableHTMLFooterRowsStayWithTheirTable(t *testing.T) {
+ html := `<table><tfoot><tr><td>Total</td><td>1</td></tr></tfoot>` +
+ `<tbody><tr><td>A</td><td>1</td></tr></tbody></table>` +
+ `<table><tr><td>B</td><td>2</td></tr></table>`
+ td := mustParse(t, html)
+ want := [][]string{
+ {"A", "1"},
+ {"Total", "1"},
+ {"B", "2"},
+ }
+ assertRowsEqual(t, td.CellTexts(), want)
+}
+
func TestParseTableHTMLMultipleHeaderRows(t *testing.T) {
html := `<table><tr><th>H1</th></tr><tr><th>H2</th></tr>` +
`<tr><td>a</td></tr></table>`