Skip to content

Commit

Permalink
Take care of ratio when calculating min/max widths of replaced boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Jul 19, 2021
1 parent 071218e commit 56910a8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tests/layout/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -501,3 +501,34 @@ def layout(gradient_css, type_='radial', init=(),
init=(450, 100, 0, 450 * 2 ** 0.5), scale_y=(200 / 450))
layout('radial-gradient(farthest-corner at 40px 210px, blue, lime)',
init=(40, 210, 0, 360 * 2 ** 0.5), scale_y=(210 / 360))


@pytest.mark.parametrize('props, div_width', (
({}, 4),
({'min-width': '10px'}, 10),
({'max-width': '1px'}, 1),
({'width': '10px'}, 10),
({'width': '1px'}, 1),
({'min-height': '10px'}, 10),
({'max-height': '1px'}, 1),
({'height': '10px'}, 10),
({'height': '1px'}, 1),
({'min-width': '10px', 'min-height': '1px'}, 10),
({'min-width': '1px', 'min-height': '10px'}, 10),
({'max-width': '10px', 'max-height': '1px'}, 1),
({'max-width': '1px', 'max-height': '10px'}, 1),
))
def test_image_min_max_width(props, div_width):
default = {
'min-width': 'auto', 'max-width': 'none', 'width': 'auto',
'min-height': 'auto', 'max-height': 'none', 'height': 'auto'}
page, = render_pages('''
<div style="display: inline-block">
<img src="pattern.png" style="display: block; %s">
</div>''' % ';'.join(
f'{key}: {props.get(key, value)}' for key, value in default.items()))
html, = page.children
body, = html.children
line, = body.children
div, = line.children
assert div.width == div_width
11 changes: 11 additions & 0 deletions weasyprint/layout/preferred.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ def min_max(box, width):
max_width = float('inf')
else:
max_width = max_width.value

if isinstance(box, boxes.ReplacedBox):
ratio = box.replacement.intrinsic_ratio
if ratio is not None:
min_height = box.style['min_height']
if min_height != 'auto' and min_height.unit != '%':
min_width = max(min_width, min_height.value * ratio)
max_height = box.style['max_height']
if max_height != 'auto' and max_height.unit != '%':
max_width = min(max_width, max_height.value * ratio)

return max(min_width, min(width, max_width))


Expand Down

0 comments on commit 56910a8

Please sign in to comment.