Skip to content

Commit

Permalink
Avoid infinite loops with td with break-inside: avoid
Browse files Browse the repository at this point in the history
Related to #1547.
  • Loading branch information
liZe committed Jan 25, 2022
1 parent 0ae65b8 commit 4f43434
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
18 changes: 18 additions & 0 deletions tests/layout/test_table.py
Expand Up @@ -2753,3 +2753,21 @@ def test_table_break_children_margin():
</table>
'''
assert len(render_pages(html)) == 3


def test_table_td_break_inside_avoid():
# Test regression: https://github.com/Kozea/WeasyPrint/issues/1547
html = '''
<style>
@page { size: 4cm }
td { break-inside: avoid; line-height: 3cm }
</style>
<table>
<tr>
<td>
a<br>a
</td>
</tr>
</table>
'''
assert len(render_pages(html)) == 2
27 changes: 21 additions & 6 deletions weasyprint/layout/table.py
Expand Up @@ -157,20 +157,35 @@ def group_layout(group, position_y, bottom_space, page_is_empty,
cell.remove_decoration(start=True, end=False)
if table.style['border_collapse'] == 'collapse':
table.skip_cell_border_top = True

# First try to render content as if there was already something
# on the page to avoid hitting block_level_layout’s TODO. Then
# force to render something if the page is actually empty, or
# just draw an empty cell otherwise. See
# test_table_break_children_margin.
new_cell, cell_resume_at, _, _, _ = block_container_layout(
context, cell, bottom_space, cell_skip_stack,
page_is_empty=False, absolute_boxes=absolute_boxes,
fixed_boxes=fixed_boxes, adjoining_margins=None,
discard=False)
if new_cell is None:
cell = cell.copy_with_children([])
cell, _, _, _, _ = block_container_layout(
context, cell, bottom_space, cell_skip_stack,
page_is_empty=True, absolute_boxes=[], fixed_boxes=[],
adjoining_margins=None, discard=False)
cell_resume_at = {0: None}
if page_is_empty:
cell, cell_resume_at, _, _, _ = block_container_layout(
context, cell, bottom_space, cell_skip_stack,
page_is_empty=True, absolute_boxes=[],
fixed_boxes=[], adjoining_margins=None,
discard=False)
else:
cell = cell.copy_with_children([])
cell, _, _, _, _ = block_container_layout(
context, cell, bottom_space, cell_skip_stack,
page_is_empty=True, absolute_boxes=[],
fixed_boxes=[], adjoining_margins=None,
discard=False)
cell_resume_at = {0: None}
else:
cell = new_cell

cell.remove_decoration(
start=cell_skip_stack is not None,
end=cell_resume_at is not None)
Expand Down

0 comments on commit 4f43434

Please sign in to comment.