Skip to content

Commit

Permalink
Include witdh/height when calculating auto margins of absolute boxes
Browse files Browse the repository at this point in the history
Fix #1264.
  • Loading branch information
liZe committed Jan 11, 2021
1 parent 59b4e5a commit b2c7463
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
18 changes: 18 additions & 0 deletions tests/test_layout/test_position.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,24 @@ def test_absolute_positioning_7():
assert (p7.position_x, p7.position_y) == (0, 91)


@assert_no_logs
def test_absolute_positioning_8():
# Regression test for https://github.com/Kozea/WeasyPrint/issues/1264
page, = parse('''
<style>@page{ width: 50px; height: 50px }</style>
<body style="font-size: 0">
<div style="position: absolute; margin: auto;
left: 0; right: 10px;
top: 0; bottom: 10px;
width: 10px; height: 20px">
''')
html, = page.children
body, = html.children
div, = body.children
assert (div.content_box_x(), div.content_box_y()) == (15, 10)
assert (div.width, div.height) == (10, 20)


@assert_no_logs
def test_absolute_images():
page, = parse('''
Expand Down
4 changes: 2 additions & 2 deletions weasyprint/layout/absolute.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def absolute_width(box, context, containing_block):
box.width = shrink_to_fit(context, box, available_width)
elif left != 'auto' and right != 'auto' and width != 'auto':
width_for_margins = cb_width - (
right + left + padding_plus_borders_x)
right + left + width + padding_plus_borders_x)
if margin_l == margin_r == 'auto':
if width + padding_plus_borders_x + right + left <= cb_width:
box.margin_left = box.margin_right = width_for_margins / 2
Expand Down Expand Up @@ -151,7 +151,7 @@ def absolute_height(box, context, containing_block):
box.margin_bottom = 0
elif top != 'auto' and bottom != 'auto' and height != 'auto':
height_for_margins = cb_height - (
top + bottom + paddings_plus_borders_y)
top + bottom + height + paddings_plus_borders_y)
if margin_t == margin_b == 'auto':
box.margin_top = box.margin_bottom = height_for_margins / 2
elif margin_t == 'auto':
Expand Down

0 comments on commit b2c7463

Please sign in to comment.