Skip to content

Commit

Permalink
Remove absolute placeholders from tables with break-inside: avoid
Browse files Browse the repository at this point in the history
Related to #2134.
  • Loading branch information
liZe committed May 11, 2024
1 parent ed249da commit 22b35bb
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 5 deletions.
94 changes: 94 additions & 0 deletions tests/layout/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -2800,6 +2800,100 @@ def test_table_empty_body(rows_expected, thead, tfoot, content):
assert rows == rows_expected[i]


def test_table_group_break_inside_avoid_absolute():
# Test regression: https://github.com/Kozea/WeasyPrint/issues/2134
html = '''
<style>
@page { size: 5cm }
tbody { break-inside: avoid; line-height: 2cm }
div { position: absolute }
</style>
<table>
<tbody><tr><td>a</td></tr></tbody>
<tbody><tr>
<td><div>a<br>b</div></td>
<td>a<br>b</td>
</tr></tbody>
</table>
'''
page1, page2 = render_pages(html)

html, = page1.children
body, = html.children
table_wrapper, = body.children
table, = table_wrapper.children
group, = table.children # Only first group

html, = page2.children
body, = html.children # No absolute div here
table_wrapper, = body.children
table, = table_wrapper.children
group, = table.children # Only second group


def test_table_row_break_inside_avoid_absolute():
# Test regression: https://github.com/Kozea/WeasyPrint/issues/2134
html = '''
<style>
@page { size: 5cm }
tr { break-inside: avoid; line-height: 2cm }
div { position: absolute }
</style>
<table>
<tr><td>a</td></tr>
<tr>
<td><div>a<br>b</div></td>
<td>a<br>b</td>
</tr>
</table>
'''
page1, page2 = render_pages(html)

html, = page1.children
body, = html.children
table_wrapper, = body.children
table, = table_wrapper.children
group, = table.children
row, = group.children # Only first row

html, = page2.children
body, = html.children # No absolute div here
table_wrapper, = body.children
table, = table_wrapper.children
group, = table.children
row, = group.children # Only second row


def test_table_break_inside_avoid_absolute():
# Test regression: https://github.com/Kozea/WeasyPrint/issues/2134
html = '''
<style>
@page { size: 5cm }
body { line-height: 2cm }
table { break-inside: avoid }
div { position: absolute }
</style>
<p>text</p>
<table>
<tr>
<td><div>a<br>b</div></td>
<td>a<br>b</td>
</tr>
</table>
'''
page1, page2 = render_pages(html)

html, = page1.children
body, = html.children
p, = body.children
line, = p.children
text, = line.children
assert text.text == 'text'

html, = page2.children
body, = html.children # No absolute div here


def test_table_break_children_margin():
# Test regression: https://github.com/Kozea/WeasyPrint/issues/1254
html = '''
Expand Down
1 change: 0 additions & 1 deletion weasyprint/css/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@
'bottom',
'break_after',
'break_before',
'break_inside',
'clear',
'counter_increment',
'counter_reset',
Expand Down
10 changes: 6 additions & 4 deletions weasyprint/layout/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def table_layout(context, table, bottom_space, skip_stack, containing_block,
"""Layout for a table box."""
from .block import ( # isort:skip
avoid_page_break, block_container_layout, block_level_page_break,
find_earlier_page_break, force_page_break)
find_earlier_page_break, force_page_break, remove_placeholders)

has_header = table.children and table.children[0].is_header
has_footer = table.children and table.children[-1].is_footer
Expand Down Expand Up @@ -209,6 +209,8 @@ def group_layout(group, position_y, bottom_space, page_is_empty,
if resume_at and not page_is_empty:
if avoid_page_break(row.style['break_inside'], context):
resume_at = {index_row: {}}
remove_placeholders(
context, new_row_children, absolute_boxes, fixed_boxes)
break

row = row.copy_with_children(new_row_children)
Expand Down Expand Up @@ -338,9 +340,8 @@ def group_layout(group, position_y, bottom_space, page_is_empty,
if resume_at and not original_page_is_empty and (
avoid_page_break(group.style['break_inside'], context) or
not new_group_children):
for descendant in group.descendants():
if descendant.footnote is not None:
context.unlayout_footnote(descendant.footnote)
remove_placeholders(
context, new_group_children, absolute_boxes, fixed_boxes)
return None, None, next_page

group = group.copy_with_children(new_group_children)
Expand Down Expand Up @@ -599,6 +600,7 @@ def get_column_cells(table, column):

avoid_break = avoid_page_break(table.style['break_inside'], context)
if resume_at and not page_is_empty and avoid_break:
remove_placeholders(context, [table], absolute_boxes, fixed_boxes)
table = None
resume_at = None
adjoining_margins = []
Expand Down

0 comments on commit 22b35bb

Please sign in to comment.