Skip to content

Commit

Permalink
Handle more cases of margin-break use cases
Browse files Browse the repository at this point in the history
This solution is not perfect, but at least it works for table cells (whose
top margins never collapse) and avoids the related workaround.

Related to #1547.
  • Loading branch information
liZe committed Jan 25, 2022
1 parent 4f43434 commit 8b0f147
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
21 changes: 13 additions & 8 deletions weasyprint/layout/block.py
Expand Up @@ -30,15 +30,20 @@ def block_level_layout(context, box, bottom_space, skip_stack,
if box.margin_bottom == 'auto':
box.margin_bottom = 0

if (context.current_page > 1 and page_is_empty):
# TODO: we should take care of cases when this box doesn't have
# collapsing margins with the first child of the page, see
# test_margin_break_clearance.
if box.style['margin_break'] == 'discard':
box.margin_top = 0
elif box.style['margin_break'] == 'auto':
if not context.forced_break:
if context.current_page > 1 and page_is_empty:
# TODO: this condition is wrong, it only works for blocks whose
# parent breaks collapsing margins. It should work for blocks whose
# one of the ancestors breaks collapsing margins.
# See test_margin_break_clearance.
collapse_with_page = (
containing_block.is_for_root_element or
adjoining_margins)
if collapse_with_page:
if box.style['margin_break'] == 'discard':
box.margin_top = 0
elif box.style['margin_break'] == 'auto':
if not context.forced_break:
box.margin_top = 0

collapsed_margin = collapse_margin(
adjoining_margins + [box.margin_top])
Expand Down
23 changes: 8 additions & 15 deletions weasyprint/layout/table.py
Expand Up @@ -165,24 +165,17 @@ def group_layout(group, position_y, bottom_space, page_is_empty,
# 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,
page_is_empty=page_is_empty, absolute_boxes=absolute_boxes,
fixed_boxes=fixed_boxes, adjoining_margins=None,
discard=False)
if new_cell is 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}
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

Expand Down

0 comments on commit 8b0f147

Please sign in to comment.