Skip to content

Commit 1d1654c

Browse files
committed
Fix corner cases about named pages
1 parent 4990ad9 commit 1d1654c

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

weasyprint/layout/blocks.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,9 @@ def block_container_layout(context, box, max_position_y, skip_stack,
490490
if over_orphans < 0 and not page_is_empty:
491491
# Reached the bottom of the page before we had
492492
# enough lines for orphans, cancel the whole box.
493+
page = child.page_values()[0]
493494
return (
494-
None, None, {'break': 'any', 'page': None}, [],
495+
None, None, {'break': 'any', 'page': page}, [],
495496
False)
496497
# How many lines we need on the next page to satisfy widows
497498
# -1 for the current line.
@@ -503,8 +504,9 @@ def block_container_layout(context, box, max_position_y, skip_stack,
503504
break
504505
if needed > over_orphans and not page_is_empty:
505506
# Total number of lines < orphans + widows
507+
page = child.page_values()[0]
506508
return (
507-
None, None, {'break': 'any', 'page': None}, [],
509+
None, None, {'break': 'any', 'page': page}, [],
508510
False)
509511
if needed and needed <= over_orphans:
510512
# Remove lines to keep them for the next page
@@ -548,6 +550,7 @@ def block_container_layout(context, box, max_position_y, skip_stack,
548550
elif page_break not in ('left', 'right', 'recto', 'verso'):
549551
assert page_name
550552
page_break = 'any'
553+
page_name = child.page_values()[0]
551554
next_page = {'break': page_break, 'page': page_name}
552555
resume_at = (index, None)
553556
break
@@ -661,8 +664,9 @@ def block_container_layout(context, box, max_position_y, skip_stack,
661664
# The page has content *before* this block:
662665
# cancel the block and try to find a break
663666
# in the parent.
667+
page = child.page_values()[0]
664668
return (
665-
None, None, {'break': 'any', 'page': None}, [],
669+
None, None, {'break': 'any', 'page': page}, [],
666670
False)
667671
# else:
668672
# ignore this 'avoid' and break anyway.
@@ -673,8 +677,9 @@ def block_container_layout(context, box, max_position_y, skip_stack,
673677
else:
674678
# This was the first child of this box, cancel the box
675679
# completly
680+
page = child.page_values()[0]
676681
return (
677-
None, None, {'break': 'any', 'page': None}, [], False)
682+
None, None, {'break': 'any', 'page': page}, [], False)
678683

679684
# Bottom borders may overflow here
680685
# TODO: back-track somehow when all lines fit but not borders

weasyprint/tests/test_layout.py

+59
Original file line numberDiff line numberDiff line change
@@ -1243,6 +1243,65 @@ def test_page_names():
12431243
assert p.element_tag == 'p'
12441244
assert section.element_tag == 'section'
12451245

1246+
pages = parse('''
1247+
<style>
1248+
@page small { size: 100px 100px }
1249+
section { page: small }
1250+
p { line-height: 80px }
1251+
</style>
1252+
<section>
1253+
<p>small</p>
1254+
<p>small</p>
1255+
</section>
1256+
''')
1257+
page1, page2 = pages
1258+
1259+
assert (page1.width, page1.height) == (100, 100)
1260+
html, = page1.children
1261+
body, = html.children
1262+
section, = body.children
1263+
p, = section.children
1264+
assert section.element_tag == 'section'
1265+
assert p.element_tag == 'p'
1266+
1267+
assert (page2.width, page2.height) == (100, 100)
1268+
html, = page2.children
1269+
body, = html.children
1270+
section, = body.children
1271+
p, = section.children
1272+
assert section.element_tag == 'section'
1273+
assert p.element_tag == 'p'
1274+
1275+
pages = parse('''
1276+
<style>
1277+
@page { size: 200px 200px }
1278+
@page small { size: 100px 100px }
1279+
section { break-after: page; page: small }
1280+
article { page: small }
1281+
</style>
1282+
<section>
1283+
<div>big</div>
1284+
<div>big</div>
1285+
</section>
1286+
<article>
1287+
<div>small</div>
1288+
<div>small</div>
1289+
</article>
1290+
''')
1291+
page1, page2, = pages
1292+
1293+
assert (page1.width, page1.height) == (100, 100)
1294+
html, = page1.children
1295+
body, = html.children
1296+
section, = body.children
1297+
assert section.element_tag == 'section'
1298+
1299+
assert (page2.width, page2.height) == (100, 100)
1300+
html, = page2.children
1301+
body, = html.children
1302+
article, = body.children
1303+
assert article.element_tag == 'article'
1304+
12461305

12471306
@assert_no_logs
12481307
def test_orphans_widows_avoid():

0 commit comments

Comments
 (0)