Skip to content

Commit

Permalink
Accommodate tables with no rows
Browse files Browse the repository at this point in the history
Tables with no rows are not semantically correct, but they shouldn't
crash the app.

Now they don't crash the app, amazing. 🙌
  • Loading branch information
pcraig3 committed Nov 20, 2024
1 parent cf3da50 commit cb24f4d
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 12 additions & 9 deletions bloom_nofos/nofos/templatetags/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions bloom_nofos/nofos/tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down Expand Up @@ -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 = """
<table>
<tr><th>Header</th></tr>
<tr><td></td></tr>
</table>
"""
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 = """
<table>
<tr><th>Header 1</th><th>Header 2</th></tr>
<tr><td></td></tr>
</table>
"""
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 = """
<table>
<tr><th>Header</th></tr>
<tr><td></td></tr>
<tr><td></td></tr>
</table>
"""
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 = """
<table>
<tr><th>Header</th></tr>
<tr><td>Non-empty</td></tr>
</table>
"""
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 <td> cells
html = """
<table>
<tr><th>Header</th></tr>
</table>
"""
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 = "<table>Hello</table>"
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 = ""
Expand Down

0 comments on commit cb24f4d

Please sign in to comment.