diff --git a/docling/backend/html_backend.py b/docling/backend/html_backend.py index 42162b429d..35177511bc 100644 --- a/docling/backend/html_backend.py +++ b/docling/backend/html_backend.py @@ -1522,6 +1522,7 @@ def parse_table_data( row_span -= 1 while ( col_idx < num_cols + and row_idx + start_row_span < num_rows and grid[row_idx + start_row_span][col_idx] is not None ): col_idx += 1 diff --git a/tests/test_backend_html.py b/tests/test_backend_html.py index d51ba88d29..fc0141cd15 100644 --- a/tests/test_backend_html.py +++ b/tests/test_backend_html.py @@ -168,6 +168,24 @@ def test_heading_levels(): assert found_lvl_1 and found_lvl_2 +def test_table_header_rowspan_without_body_does_not_crash(): + # A table whose only row is a `th` with rowspan (no body rows to span into) + # used to raise IndexError: get_html_table_row_col counts no rows for an + # all-header-rowspan row, so the grid was empty and the cell-placement read + # went out of bounds. It should not crash and should keep the cell. + src = b"
h
" + in_doc = InputDocument( + path_or_stream=BytesIO(src), + format=InputFormat.HTML, + backend=HTMLDocumentBackend, + filename="t.html", + ) + doc = HTMLDocumentBackend(in_doc=in_doc, path_or_stream=BytesIO(src)).convert() + + assert len(doc.tables) == 1 + assert [cell.text for cell in doc.tables[0].data.table_cells] == ["h"] + + def test_ordered_lists(): test_set: list[tuple[bytes, str]] = []