Skip to content

Commit

Permalink
Avoid auto margins in flex layout
Browse files Browse the repository at this point in the history
Fix #800.

The way auto margins are resolved for the parent is currently really stupid. We
have to find exactly when these margins must be set.
  • Loading branch information
liZe committed Mar 19, 2019
1 parent 02f9248 commit fd90af6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
25 changes: 17 additions & 8 deletions weasyprint/layout/flex.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,18 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block,
else:
axis, cross = 'height', 'width'

margin_left = 0 if box.margin_left == 'auto' else box.margin_left
margin_right = 0 if box.margin_right == 'auto' else box.margin_right
margin_top = 0 if box.margin_top == 'auto' else box.margin_top
margin_bottom = 0 if box.margin_bottom == 'auto' else box.margin_bottom

if getattr(box, axis) != 'auto':
available_main_space = getattr(box, axis)
else:
if axis == 'width':
available_main_space = (
containing_block.width -
box.margin_left - box.margin_right -
margin_left - margin_right -
box.padding_left - box.padding_right -
box.border_left_width - box.border_right_width)
else:
Expand All @@ -58,7 +63,7 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block,
main_space = min(main_space, containing_block.height)
available_main_space = (
main_space -
box.margin_top - box.margin_bottom -
margin_top - margin_bottom -
box.padding_top - box.padding_bottom -
box.border_top_width - box.border_bottom_width)

Expand All @@ -75,24 +80,28 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block,
main_space = min(main_space, containing_block.height)
available_cross_space = (
main_space -
box.margin_top - box.margin_bottom -
margin_top - margin_bottom -
box.padding_top - box.padding_bottom -
box.border_top_width - box.border_bottom_width)
else:
available_cross_space = (
containing_block.width -
box.margin_left - box.margin_right -
margin_left - margin_right -
box.padding_left - box.padding_right -
box.border_left_width - box.border_right_width)

# Step 3
children = box.children
resolve_percentages(box, containing_block)
if box.margin_top == 'auto':
parent_box = box.copy_with_children(children)
resolve_percentages(parent_box, containing_block)
if parent_box.margin_top == 'auto':
box.margin_top = 0
if box.margin_bottom == 'auto':
if parent_box.margin_bottom == 'auto':
box.margin_bottom = 0
parent_box = box.copy_with_children(children)
if parent_box.margin_left == 'auto':
box.margin_left = 0
if parent_box.margin_right == 'auto':
box.margin_right = 0
if isinstance(parent_box, boxes.FlexBox):
blocks.block_level_width(parent_box, containing_block)
else:
Expand Down
8 changes: 8 additions & 0 deletions weasyprint/tests/test_layout/test_flex.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,3 +353,11 @@ def test_flex_item_min_height():
div_3.height ==
article.height ==
50)


@assert_no_logs
def test_flex_auto_margin():
# Regression test for https://github.com/Kozea/WeasyPrint/issues/800
page, = render_pages('<div style="display: flex; margin: auto">')
page, = render_pages(
'<div style="display: flex; flex-direction: column; margin: auto">')

0 comments on commit fd90af6

Please sign in to comment.