From 8e674609e0252529139c70cff540e54c8613c538 Mon Sep 17 00:00:00 2001 From: eeshsaxena Date: Mon, 17 Aug 2026 10:47:06 +0530 Subject: [PATCH] Do not crash padding an empty table pad_tables_in_text reformats each table block when it hits the closing marker. Two markers with nothing between them (an empty table) leave the buffer empty, and reformat_table then read lines[0] and raised IndexError. Return early when there are no lines. --- html2text/utils.py | 2 ++ test/test_html2text.py | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/html2text/utils.py b/html2text/utils.py index 8f77668..8ecd2ce 100644 --- a/html2text/utils.py +++ b/html2text/utils.py @@ -256,6 +256,8 @@ def reformat_table(lines: List[str], right_margin: int) -> List[str]: Given the lines of a table padds the cells and returns the new lines """ + if not lines: + return lines # find the maximum width of the columns max_width = [len(x.rstrip()) + right_margin for x in lines[0].split("|")] max_cols = len(max_width) diff --git a/test/test_html2text.py b/test/test_html2text.py index 1f16b4f..8949a17 100644 --- a/test/test_html2text.py +++ b/test/test_html2text.py @@ -254,3 +254,14 @@ def test_strong_emptied() -> None: h.strong_mark = "" string = "A B C." assert h.handle(string) == "A B _C_.\n\n" + + +def test_pad_tables_empty_table() -> None: + """An empty table block should not crash table padding.""" + from html2text import config + from html2text.utils import pad_tables_in_text + + # Two consecutive markers leave the table buffer empty, which used to + # raise IndexError on lines[0]. + marker = config.TABLE_MARKER_FOR_PAD + pad_tables_in_text(f"{marker}\n{marker}\n")