Skip to content

Commit

Permalink
Fix flex collapsing margins
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Feb 19, 2025
1 parent 7024f63 commit 37f91ff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
18 changes: 18 additions & 0 deletions tests/layout/test_flex.py
Original file line number Diff line number Diff line change
Expand Up @@ -1735,3 +1735,21 @@ def test_flex_grow_in_flex_column():
assert body.width == div.width == 10
assert body.margin_width() == 14
assert body.margin_height() == 9


@assert_no_logs
def test_flex_collapsing_margin():
page, = render_pages('''
<p style="margin-bottom: 20px; height: 100px">ABC</p>
<article style="display: flex; margin-top: 10px">
<div>A</div>
</article>
''')
html, = page.children
body, = html.children
p, article = body.children
div, = article.children
assert p.position_y == 0
assert p.height == 100
assert article.position_y == 110
assert div.position_y == 120
13 changes: 9 additions & 4 deletions weasyprint/layout/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .grid import grid_layout
from .inline import iter_line_boxes
from .min_max import handle_min_max_width
from .percent import resolve_percentages, resolve_position_percentages
from .percent import percentage, resolve_percentages, resolve_position_percentages
from .replaced import block_replaced_box_layout
from .table import table_layout, table_wrapper_width

Expand Down Expand Up @@ -501,9 +501,14 @@ def _in_flow_layout(context, box, index, child, new_children, page_is_empty,
no_margin_collapse = box.is_table_wrapper or isinstance(
child, (boxes.BlockReplacedBox, boxes.FlexBox, boxes.GridBox))
if no_margin_collapse:
collapsed_margin = collapse_margin(adjoining_margins)
child.position_y += collapsed_margin
position_y += collapsed_margin
if child.style['margin_top'] == 'auto':
margin_top = 0
else:
margin_top = percentage(child.style['margin_top'], box.width)
adjoining_margins.append(margin_top)
offset_y = collapse_margin(adjoining_margins) - margin_top
child.position_y += offset_y
position_y += offset_y
adjoining_margins = []

page_is_empty_with_no_children = page_is_empty and not any(
Expand Down

0 comments on commit 37f91ff

Please sign in to comment.