Skip to content

Commit fd90af6

Browse files
committed
Avoid auto margins in flex layout
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.
1 parent 02f9248 commit fd90af6

File tree

2 files changed

+25
-8
lines changed

2 files changed

+25
-8
lines changed

weasyprint/layout/flex.py

+17-8
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block,
3939
else:
4040
axis, cross = 'height', 'width'
4141

42+
margin_left = 0 if box.margin_left == 'auto' else box.margin_left
43+
margin_right = 0 if box.margin_right == 'auto' else box.margin_right
44+
margin_top = 0 if box.margin_top == 'auto' else box.margin_top
45+
margin_bottom = 0 if box.margin_bottom == 'auto' else box.margin_bottom
46+
4247
if getattr(box, axis) != 'auto':
4348
available_main_space = getattr(box, axis)
4449
else:
4550
if axis == 'width':
4651
available_main_space = (
4752
containing_block.width -
48-
box.margin_left - box.margin_right -
53+
margin_left - margin_right -
4954
box.padding_left - box.padding_right -
5055
box.border_left_width - box.border_right_width)
5156
else:
@@ -58,7 +63,7 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block,
5863
main_space = min(main_space, containing_block.height)
5964
available_main_space = (
6065
main_space -
61-
box.margin_top - box.margin_bottom -
66+
margin_top - margin_bottom -
6267
box.padding_top - box.padding_bottom -
6368
box.border_top_width - box.border_bottom_width)
6469

@@ -75,24 +80,28 @@ def flex_layout(context, box, max_position_y, skip_stack, containing_block,
7580
main_space = min(main_space, containing_block.height)
7681
available_cross_space = (
7782
main_space -
78-
box.margin_top - box.margin_bottom -
83+
margin_top - margin_bottom -
7984
box.padding_top - box.padding_bottom -
8085
box.border_top_width - box.border_bottom_width)
8186
else:
8287
available_cross_space = (
8388
containing_block.width -
84-
box.margin_left - box.margin_right -
89+
margin_left - margin_right -
8590
box.padding_left - box.padding_right -
8691
box.border_left_width - box.border_right_width)
8792

8893
# Step 3
8994
children = box.children
90-
resolve_percentages(box, containing_block)
91-
if box.margin_top == 'auto':
95+
parent_box = box.copy_with_children(children)
96+
resolve_percentages(parent_box, containing_block)
97+
if parent_box.margin_top == 'auto':
9298
box.margin_top = 0
93-
if box.margin_bottom == 'auto':
99+
if parent_box.margin_bottom == 'auto':
94100
box.margin_bottom = 0
95-
parent_box = box.copy_with_children(children)
101+
if parent_box.margin_left == 'auto':
102+
box.margin_left = 0
103+
if parent_box.margin_right == 'auto':
104+
box.margin_right = 0
96105
if isinstance(parent_box, boxes.FlexBox):
97106
blocks.block_level_width(parent_box, containing_block)
98107
else:

weasyprint/tests/test_layout/test_flex.py

+8
Original file line numberDiff line numberDiff line change
@@ -353,3 +353,11 @@ def test_flex_item_min_height():
353353
div_3.height ==
354354
article.height ==
355355
50)
356+
357+
358+
@assert_no_logs
359+
def test_flex_auto_margin():
360+
# Regression test for https://github.com/Kozea/WeasyPrint/issues/800
361+
page, = render_pages('<div style="display: flex; margin: auto">')
362+
page, = render_pages(
363+
'<div style="display: flex; flex-direction: column; margin: auto">')

0 commit comments

Comments
 (0)