Skip to content

Commit

Permalink
Fix corner cases about named pages
Browse files Browse the repository at this point in the history
  • Loading branch information
liZe committed Sep 11, 2017
1 parent 4990ad9 commit 1d1654c
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
13 changes: 9 additions & 4 deletions weasyprint/layout/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,9 @@ def block_container_layout(context, box, max_position_y, skip_stack,
if over_orphans < 0 and not page_is_empty:
# Reached the bottom of the page before we had
# enough lines for orphans, cancel the whole box.
page = child.page_values()[0]
return (
None, None, {'break': 'any', 'page': None}, [],
None, None, {'break': 'any', 'page': page}, [],
False)
# How many lines we need on the next page to satisfy widows
# -1 for the current line.
Expand All @@ -503,8 +504,9 @@ def block_container_layout(context, box, max_position_y, skip_stack,
break
if needed > over_orphans and not page_is_empty:
# Total number of lines < orphans + widows
page = child.page_values()[0]
return (
None, None, {'break': 'any', 'page': None}, [],
None, None, {'break': 'any', 'page': page}, [],
False)
if needed and needed <= over_orphans:
# Remove lines to keep them for the next page
Expand Down Expand Up @@ -548,6 +550,7 @@ def block_container_layout(context, box, max_position_y, skip_stack,
elif page_break not in ('left', 'right', 'recto', 'verso'):
assert page_name
page_break = 'any'
page_name = child.page_values()[0]
next_page = {'break': page_break, 'page': page_name}
resume_at = (index, None)
break
Expand Down Expand Up @@ -661,8 +664,9 @@ def block_container_layout(context, box, max_position_y, skip_stack,
# The page has content *before* this block:
# cancel the block and try to find a break
# in the parent.
page = child.page_values()[0]
return (
None, None, {'break': 'any', 'page': None}, [],
None, None, {'break': 'any', 'page': page}, [],
False)
# else:
# ignore this 'avoid' and break anyway.
Expand All @@ -673,8 +677,9 @@ def block_container_layout(context, box, max_position_y, skip_stack,
else:
# This was the first child of this box, cancel the box
# completly
page = child.page_values()[0]
return (
None, None, {'break': 'any', 'page': None}, [], False)
None, None, {'break': 'any', 'page': page}, [], False)

# Bottom borders may overflow here
# TODO: back-track somehow when all lines fit but not borders
Expand Down
59 changes: 59 additions & 0 deletions weasyprint/tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -1243,6 +1243,65 @@ def test_page_names():
assert p.element_tag == 'p'
assert section.element_tag == 'section'

pages = parse('''
<style>
@page small { size: 100px 100px }
section { page: small }
p { line-height: 80px }
</style>
<section>
<p>small</p>
<p>small</p>
</section>
''')
page1, page2 = pages

assert (page1.width, page1.height) == (100, 100)
html, = page1.children
body, = html.children
section, = body.children
p, = section.children
assert section.element_tag == 'section'
assert p.element_tag == 'p'

assert (page2.width, page2.height) == (100, 100)
html, = page2.children
body, = html.children
section, = body.children
p, = section.children
assert section.element_tag == 'section'
assert p.element_tag == 'p'

pages = parse('''
<style>
@page { size: 200px 200px }
@page small { size: 100px 100px }
section { break-after: page; page: small }
article { page: small }
</style>
<section>
<div>big</div>
<div>big</div>
</section>
<article>
<div>small</div>
<div>small</div>
</article>
''')
page1, page2, = pages

assert (page1.width, page1.height) == (100, 100)
html, = page1.children
body, = html.children
section, = body.children
assert section.element_tag == 'section'

assert (page2.width, page2.height) == (100, 100)
html, = page2.children
body, = html.children
article, = body.children
assert article.element_tag == 'article'


@assert_no_logs
def test_orphans_widows_avoid():
Expand Down

0 comments on commit 1d1654c

Please sign in to comment.