1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-05 00:21:15 +03:00

Ignore display: table-* values for inline elements

They end up as inline-level replaced boxes.
This commit is contained in:
Simon Sapin 2012-03-14 13:34:08 +01:00
parent ae3c73db68
commit 869aac58c7
2 changed files with 28 additions and 38 deletions

View File

@ -55,23 +55,6 @@ def handler(tag):
return decorator
def is_block_level(box):
"""Tell wether ``box`` is supposed to be block level.
Return ``True`` if the element is block-level, ``False`` if it is
inline-level, and raise ValueError if it is neither.
"""
display = box.style.display
if display in ('block', 'list-item', 'table'):
return True
elif display in ('inline', 'inline-table', 'inline-block'):
return False
else:
raise ValueError('Unsupported display: ' + display)
def make_replaced_box(element, box, image):
"""Wrap an image in a replaced box.
@ -79,29 +62,14 @@ def make_replaced_box(element, box, image):
element should be.
"""
if is_block_level(box):
if box.style.display in ('block', 'list-item', 'table'):
type_ = boxes.BlockReplacedBox
else:
# TODO: support images with 'display: table-cell'?
type_ = boxes.InlineReplacedBox
return type_(element.tag, element.sourceline, box.style, image)
def make_text_box(element, box, text):
"""Make a text box.
If the element should be block-level, wrap it in a block box.
"""
text_box = boxes.TextBox(element.tag, element.sourceline,
box.style.inherit_from(), text)
if is_block_level(box):
type_ = boxes.BlockBox
else:
type_ = boxes.InlineBox
return type_(element.tag, element.sourceline,
box.style, [text_box])
@handler('img')
def handle_img(document, element, box):
"""Handle ``<img>`` elements, return either an image or the alt-text.
@ -118,7 +86,8 @@ def handle_img(document, element, box):
else:
# Invalid image, use the alt-text.
if alt:
return [make_text_box(element, box, alt)]
return [box.copy_with_children(
[boxes.TextBox.anonymous_from(box, alt)])]
elif alt == '':
# The element represents nothing
return []
@ -129,7 +98,8 @@ def handle_img(document, element, box):
return []
else:
if alt:
return [make_text_box(element, box, alt)]
return [box.copy_with_children(
[boxes.TextBox.anonymous_from(box, alt)])]
else:
return []

View File

@ -839,7 +839,7 @@ def test_whitespace_processing():
@assert_no_logs
def test_with_images():
def test_images():
"""Test that width, height and ratio of images are respected."""
# Try a few image formats
for html in [
@ -918,9 +918,10 @@ def test_with_images():
assert img.width == 40
assert img.height == 40
# display: table-cell is ignored
page, = parse('''
<img src="pattern.png" style="width: 40px">
<img src="pattern.png" style="width: 60px">
<img src="pattern.png" style="width: 60px; display: table-cell">
''')
html, = page.children
body, = html.children
@ -934,6 +935,25 @@ def test_with_images():
assert img_1.position_y == 20
assert img_2.position_y == 0
# Block-level image:
page, = parse('''
<style>
@page { -weasy-size: 100px }
img { width: 40px; margin: 10px auto; display: block }
</style>
<body>
<img src="pattern.png">
''')
html, = page.children
body, = html.children
img, = body.children
assert img.element_tag == 'img'
assert img.position_x == 0
assert img.position_y == 0
assert img.content_box_x() == 30 # (100 - 40) / 2 == 30px for margin-left
assert img.content_box_y() == 10
@assert_no_logs
def test_vertical_align():