Skip to content

Commit

Permalink
fix for empty table body crash
Browse files Browse the repository at this point in the history
  • Loading branch information
th0mr authored and brechtm committed Dec 14, 2023
1 parent 0550a0d commit 3c87c45
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/rinoh/frontend/rst/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,10 @@ def build_flowable(self):
head = tgroup.thead.get_table_section()
except AttributeError:
head = None
body = tgroup.tbody.get_table_section()
try:
body = tgroup.tbody.get_table_section()
except AttributeError:
body = None
align = self.get('align')
width_string = self.get('width')
table = rt.Table(body, head=head,
Expand Down
31 changes: 19 additions & 12 deletions src/rinoh/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,16 @@ def __init__(self, body, head=None, align=None, width=None,
if head:
head.parent = self
self.body = body
body.parent = self
if body:
body.parent = self
self.column_widths = column_widths

def prepare(self, flowable_target):
super().prepare(flowable_target)
if self.head:
self.head.prepare(flowable_target)
self.body.prepare(flowable_target)
if self.body:
self.body.prepare(flowable_target)

def initial_state(self, container):
return TableState(self)
Expand Down Expand Up @@ -173,13 +175,14 @@ def render_rows(section, next_row_index=0):
if render_rows(self.head) != len(self.head):
raise EndOfContainer(state)
# body rows
next_row_index = render_rows(self.body, state.body_row_index)
rows_left = len(self.body) - next_row_index
if rows_left > 0:
split_minimum_rows = get_style('split_minimum_rows')
if min(next_row_index, rows_left) >= split_minimum_rows:
state.body_row_index = next_row_index
raise EndOfContainer(state)
if self.body:
next_row_index = render_rows(self.body, state.body_row_index)
rows_left = len(self.body) - next_row_index
if rows_left > 0:
split_minimum_rows = get_style('split_minimum_rows')
if min(next_row_index, rows_left) >= split_minimum_rows:
state.body_row_index = next_row_index
raise EndOfContainer(state)
return sum(state.column_widths), 0, 0

def _size_columns(self, container):
Expand All @@ -190,7 +193,11 @@ def _size_columns(self, container):
- cell contents
"""
num_cols = self.body.num_columns
if self.body is not None:
num_cols = self.body.num_columns
else:
# Table is a head-only table
num_cols = self.head.num_columns
width = self._width(container)
if width == FlowableWidth.FILL:
width = 100 * PERCENT
Expand Down Expand Up @@ -307,15 +314,15 @@ def cell_content_width(cell):

# find the maximum content width for all non-column-spanning cells for
# each non-fixed-width column
for row in chain(self.head or [], self.body):
for row in chain(self.head or [], self.body or []):
for cell in (cell for cell in row if cell.colspan == 1):
col = int(cell.column_index)
if col not in fixed_width_cols:
widths[col] = max(widths[col], cell_content_width(cell))

# divide the extra space needed for column-spanning cells equally over
# the spanned columns (skipping fixed-width columns)
for row in chain(self.head or [], self.body):
for row in chain(self.head or [], self.body or []):
for cell in (cell for cell in row if cell.colspan > 1):
c = int(cell.column_index)
c_end = c + cell.colspan
Expand Down

0 comments on commit 3c87c45

Please sign in to comment.