From cb24f4d819a3c818e93d62140ce55d65e400179e Mon Sep 17 00:00:00 2001 From: Paul Craig Date: Wed, 20 Nov 2024 15:46:25 -0500 Subject: [PATCH] Accommodate tables with no rows MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tables with no rows are not semantically correct, but they shouldn't crash the app. Now they don't crash the app, amazing. 🙌 --- CHANGELOG.md | 1 + .../nofos/templatetags/utils/__init__.py | 21 +++--- bloom_nofos/nofos/tests/test_templatetags.py | 70 +++++++++++++++++++ 3 files changed, 83 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e35e347b..07ac7e4a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve - Do not bold paragraph in th that follows a strong - Add 33% and 66% width classes +- Tables with no rows no longer crash the app ## [1.36.0] - 2023-11-19 diff --git a/bloom_nofos/nofos/templatetags/utils/__init__.py b/bloom_nofos/nofos/templatetags/utils/__init__.py index 607255ef..b1a70889 100644 --- a/bloom_nofos/nofos/templatetags/utils/__init__.py +++ b/bloom_nofos/nofos/templatetags/utils/__init__.py @@ -226,16 +226,19 @@ def get_parent_td(element): def is_callout_box_table_markdown(table): rows = table.find_all("tr") - cols = rows[0].find_all("th") + rows[0].find_all("td") - tds = table.find_all("td") + if rows: + cols = rows[0].find_all("th") + rows[0].find_all("td") + tds = table.find_all("td") + + return ( + len(cols) == 1 # 1 column + and len(rows) == 2 # 2 rows (thead and tbody generated automatically) + and len(tds) == 1 # 1 cell + and tds[0] + and tds[0].get_text().strip() == "" # the cell is empty + ) - return ( - len(cols) == 1 # 1 column - and len(rows) == 2 # 2 rows (thead and tbody generated automatically) - and len(tds) == 1 # 1 cell - and tds[0] - and tds[0].get_text().strip() == "" # the cell is empty - ) + return False # Footnotes diff --git a/bloom_nofos/nofos/tests/test_templatetags.py b/bloom_nofos/nofos/tests/test_templatetags.py index 36eae6a3..361d1b26 100644 --- a/bloom_nofos/nofos/tests/test_templatetags.py +++ b/bloom_nofos/nofos/tests/test_templatetags.py @@ -17,6 +17,7 @@ format_footnote_ref_html, get_footnote_type, get_parent_td, + is_callout_box_table_markdown, is_floating_callout_box, is_footnote_ref, ) @@ -267,6 +268,75 @@ def test_edge_case_just_over_225(self): self.assertEqual(result, "nofo--cover-page--title--h1--very-very-smol") +class IsCalloutBoxTableMarkdownTest(TestCase): + def test_valid_callout_box_table(self): + # A table with 1 column, 2 rows, and 1 empty cell + html = """ + + + +
Header
+ """ + soup = BeautifulSoup(html, "html.parser") + table = soup.find("table") + self.assertTrue(is_callout_box_table_markdown(table)) + + def test_invalid_multiple_columns(self): + # A table with more than 1 column + html = """ + + + +
Header 1Header 2
+ """ + soup = BeautifulSoup(html, "html.parser") + table = soup.find("table") + self.assertFalse(is_callout_box_table_markdown(table)) + + def test_invalid_multiple_rows(self): + # A table with more than 2 rows + html = """ + + + + +
Header
+ """ + soup = BeautifulSoup(html, "html.parser") + table = soup.find("table") + self.assertFalse(is_callout_box_table_markdown(table)) + + def test_invalid_non_empty_cell(self): + # A table with a non-empty cell + html = """ + + + +
Header
Non-empty
+ """ + soup = BeautifulSoup(html, "html.parser") + table = soup.find("table") + self.assertFalse(is_callout_box_table_markdown(table)) + + def test_invalid_no_cell(self): + # A table with no cells + html = """ + + +
Header
+ """ + soup = BeautifulSoup(html, "html.parser") + table = soup.find("table") + self.assertFalse(is_callout_box_table_markdown(table)) + + def test_invalid_no_rows(self): + # An empty table with no rows + html = "Hello
" + soup = BeautifulSoup(html, "html.parser") + table = soup.find("table") + self.assertFalse(is_callout_box_table_markdown(table)) + + class HTMLTableClassTests(TestCase): def _generate_table(self, num_cols, num_rows=1, cell="td", table_empty=False): rows = ""