1
1
mirror of https://github.com/Kozea/WeasyPrint.git synced 2024-10-04 16:07:57 +03:00

Bug fix: properly build anonymous blocks when a block element is in an inline

This commit is contained in:
Simon Sapin 2011-08-17 17:33:19 +02:00
parent 79f2bb856c
commit b74927afa4
3 changed files with 52 additions and 14 deletions

View File

@ -99,7 +99,8 @@ class Box(object):
self.style = self.document.style_for(self.element).copy()
def __repr__(self):
return '<%s %s>' % (type(self).__name__, self.element.tag)
return '<%s %s %i>' % (type(self).__name__, self.element.tag,
self.element.sourceline)
def ancestors(self):
"""Yield parent and recursively yield parent's parents."""
@ -473,4 +474,3 @@ class InlineLevelReplacedBox(ReplacedBox, AtomicInlineLevelBox):
A replaced element with a 'display' value of 'inline', 'inline-table', or
'inline-block' generates an inline-level replaced box.
"""

View File

@ -37,6 +37,7 @@ def build_formatting_structure(document):
box = inline_in_block(box)
box = block_in_inline(box)
box = process_whitespace(box)
sanity_checks(box)
return box
@ -299,7 +300,7 @@ def block_in_inline(box):
if block_level_box is None:
break
_add_anonymous_block(new_box, new_line)
new_box.add_child(block_level_box)
new_box.add_child(block_in_inline(block_level_box))
# Loop with the same child
if new_box.children:
# Some children were already added, this became a block
@ -351,3 +352,30 @@ def _inner_block_in_inline(box):
box.children.appendleft(child)
break
return new_box, block_level_box
def sanity_checks(box):
"""
Check that the rules regarding boxes are met:
* A block container can contain either only block-level boxes or
only line boxes
* Line boxes and inline boxes can only contain inline-level boxes.
"""
if not isinstance(box, boxes.ParentBox):
return
for child in box.children:
sanity_checks(child)
if isinstance(box, boxes.BlockContainerBox):
types = [boxes.BlockLevelBox, boxes.LineBox]
elif isinstance(box, (boxes.LineBox, boxes.InlineBox)):
types = [boxes.InlineLevelBox]
else:
return
assert any(
all(isinstance(child, type_) for child in box.children)
for type_ in types
)

View File

@ -188,12 +188,13 @@ def test_inline_in_block():
@suite.test
def test_block_in_inline():
box = parse('''
<style>
<style>
p { display: inline-block; }
span { display: block; }
</style>
<p>Lorem <em>ipsum <strong>dolor <span>sit</span>
<span>amet,</span></strong><span>consectetur</span></em></p>''')
</style>
<p>Lorem <em>ipsum <strong>dolor <span>sit</span>
<span>amet,</span></strong><span><em>consectetur<div/></em></span></em></p>
''')
box = build.inline_in_block(box)
assert_tree(box, [
('body', 'line', [
@ -214,7 +215,10 @@ def test_block_in_inline():
('span', 'text', 'amet,')])])]),
('span', 'block', [ # This block is "pulled up"
('span', 'line', [
('span', 'text', 'consectetur')])])])])])])])
('em', 'inline', [
('em', 'text', 'consectetur'),
('div', 'block', []),
])])])])])])])])
box = build.block_in_inline(box)
assert_tree(box, [
@ -245,8 +249,14 @@ def test_block_in_inline():
('em', 'inline', [
('strong', 'inline', [])])])]),
('span', 'block', [
('span', 'anon_block', [
('span', 'line', [
('span', 'text', 'consectetur')])]),
('em', 'inline', [
('em', 'text', 'consectetur')])])]),
('div', 'block', []),
('span', 'anon_block', [
('span', 'line', [
('em', 'inline', [])])])]),
('p', 'anon_block', [
('p', 'line', [
('em', 'inline', [])])])])])])