docling icon indicating copy to clipboard operation
docling copied to clipboard

HTML: an empty <tr> covered by rowspans drops the cells of the next row

Open chaitanya0305 opened this issue 1 week ago • 3 comments

Summary

In the HTML backend, if every cell in a table row has rowspan="2" or more, the next <tr> has no cells of its own. That is valid HTML and common in generated tables. Docling treats the empty row like a header row and does not count it. The rowspans then cover the next real row instead, and its cells are dropped from TableData and from the Markdown and HTML exports. With partial rowspans, cells land in the wrong column instead. The table content is lost without any warning.

Reproduction

Run on current main (0521b433):

from io import BytesIO
from docling.backend.html_backend import HTMLDocumentBackend
from docling.datamodel.base_models import InputFormat
from docling.datamodel.document import InputDocument

CASES = {
    "rowspan covers empty tr": '<table><tr><td rowspan="2">a</td><td rowspan="2">b</td></tr><tr></tr><tr><td>c</td><td>d</td></tr></table>',
    "partial rowspan + empty tr": '<table><tr><td rowspan="2">a</td><td>b</td></tr><tr></tr><tr><td>c</td><td>d</td></tr></table>',
    "th rowspan header + empty tr": '<table><tr><th rowspan="2">A</th><th rowspan="2">B</th></tr><tr></tr><tr><td>c</td><td>d</td></tr></table>',
    "uncovered empty tr (control)": "<table><tr><td>a</td><td>b</td></tr><tr></tr><tr><td>c</td><td>d</td></tr></table>",
}
for name, html in CASES.items():
    src = html.encode()
    ind = InputDocument(path_or_stream=BytesIO(src), format=InputFormat.HTML,
                        backend=HTMLDocumentBackend, filename="t.html")
    doc = HTMLDocumentBackend(in_doc=ind, path_or_stream=BytesIO(src)).convert()
    print(name, [[c.text for c in row] for row in doc.tables[0].data.grid])

Actual

rowspan covers empty tr [['a', 'b'], ['a', 'b']]
partial rowspan + empty tr [['a', 'b'], ['a', 'c']]
th rowspan header + empty tr [['A', 'B']]
uncovered empty tr (control) [['a', 'b'], ['c', 'd']]

Expected

rowspan covers empty tr [['a', 'b'], ['a', 'b'], ['c', 'd']]
partial rowspan + empty tr [['a', 'b'], ['a', ''], ['c', 'd']]
th rowspan header + empty tr [['A', 'B'], ['c', 'd']]   (data row kept)
uncovered empty tr (control) [['a', 'b'], ['c', 'd']]              (unchanged)

Cause

In docling/backend/html_backend.py, both parse_table_data and get_html_table_row_col start each row with row_header = True and set it to False only when they see a non-th cell. A <tr> with no cells keeps row_header = True, so it is handled like an all-th rowspan header row and is not counted. The following row is then placed on the row that the rowspans already fill.

Proposed fix

Count a cell-less <tr> as a table row only when an earlier rowspan covers it, as the HTML table model does. An empty row that nothing covers is still skipped, so no blank rows are added (leading, middle and trailing empty rows give the same output as now). There are two regression tests in tests/test_backend_html.py: one for the covered case and one that checks that uncovered empty rows add nothing. No groundtruth file in tests/data has an empty <tr>.

I used an AI coding assistant (Claude) to find and analyse this; I reproduced it and checked the fix myself.

I've tested a fix with a regression test: https://github.com/chaitanya0305/docling/compare/main...chaitanya0305:docling:fix/html-empty-table-row. If this is accepted, I'll open the PR straight away.

chaitanya0305 avatar Nov 25 '26 14:11 chaitanya0305

Classification labels added: bug, priority:high, html.

These labels are advisory. Maintainers can correct them. The workflow preserves these corrections.

github-actions[bot] avatar Nov 25 '26 14:11 github-actions[bot]

Thanks @chaitanya0305 for spotting this issue. I've checked and I was able to reproduce it. All four HTML cases are valid HTML5 and therefore they should be properly handled by Docling. Please, feel free to open a PR. The direction you suggested sounds reasonable.

ceberam avatar Nov 25 '26 15:11 ceberam

Thanks @ceberam for confirming. I'll open the PR shortly from the linked branch: it counts a cell-less <tr> as a row only when an earlier rowspan covers it, so uncovered empty rows still add nothing, with regression tests for both cases.

chaitanya0305 avatar Nov 25 '26 15:11 chaitanya0305