Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #420 - Fix for empty table body crash #422

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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