WeasyPrint
WeasyPrint copied to clipboard
Empty pages from page-break-before/after use wrong page selector
Problem description: Using page-break-before/after: right/left
can create empty pages which is of course intended. However, these pages use the wrong css selector in my case. In particular, they use a different selector than the pages before and after.
Example: In the following example, I would expect all pages to be white, because all the content is within the main
element. However, the empty page, which is created due to page-break-before: right
is not white, but blue.
<html>
<head>
<style>
main { page: main_page; }
h1 { page-break-before: right; }
@page { background-color: blue; }
@page main_page { background-color: white; }
</style>
</head>
<body>
<main>
<h1>First heading</h1>
<h1>Second heading</h1>
</main>
</body>
</html>
Version: Weasyprint 51
This is by purpose:
https://github.com/Kozea/WeasyPrint/blob/27c600f18f025d8a7bffb4e5e0f861014881198e/weasyprint/layout/pages.py#L725-L726
...and possibly according to the spec... according to the spec, a blank page is content empty. Contemplating this expression, we must confess, that there is no <main>
box to match the CSS rule main { page: main_page; }
. And the blue page, despite being a left page, is right.
BTW: According to this nitpicking the CSS selector @page main_page:blank
should be invalid.
Thanks for the update! I cannot judge whether this is according to the spec. It just doesn't feel intuitive to me as the page is before the opening and after the closing tag of main
. But I understand that there is no content on the page and thus it's considered blank. Using @page main_page:blank
could be a nice workaround, but as you correctly point out, it isn't.
I didn't find anything in the spec mentioning (or forbidding) the :blank
pseudo selector in context of named pages. My comment was supposed to be a bit sarcastically.
The blue page feels wrong.
In fact: @liZe, who knows the spec better than I do, added the bug label.
In fact: @liZe, who knows the spec better than I do, added the bug label.
There’s a bug because we have to keep the current page name on empty pages created by break
rules.
The (current/last used) page name is present (I debugged that), but somebody explicitly decided to remove it:
https://github.com/Kozea/WeasyPrint/blob/c443b9c9a61d82bd581c6f6aac7df746368c0073/weasyprint/layout/pages.py#L725-L726
Nevertheless I'm wondering whether a blank page, happening between two different named pages should belong to the previous or to the next page's namespace? Should that depend on the break-rule -- break-after
vs. break-before
?
The (current/last used) page name is present (I debugged that), but somebody explicitly decided to remove it:
Yes, this code is (probably) useful for example when we break after an element with a named page.
Nevertheless I'm wondering whether a blank page, happening between two different named pages should belong to the previous or to the next page's namespace? Should that depend on the break-rule --
break-after
vs.break-before
?
I’m not sure of anything anymore.
With the previous example, shouldn’t the main
box be displayed on the empty page? That would make sense, and then the answer would be : "it belongs to the parent namespace". I have to read the spec again.
I have to read the spec again.
Good luck!
Hi, is there any way to work round this issue? I am putting together a book using numbered sections, and having the next section start on a right page means I lose control of the formatting of headers and footers on the blank pages before.
Hi @tokyovigilante, I do not have a good workaround, other than making sure the pages before and after the inserted blank page use the default page
style so that the style on all three pages is the same.
In my case, I needed two different styles: one for the part before the first main section and one for the main section (in which this problem occurred). I ensured the main section uses the default page
style and I styled all pages before the main page with explicit @page :nth(...)
selectors. Inelegant and brittle, but worked for me.
Of course this won't work for more complicated styles.
@timtroendle Thanks for the comments. I'm trying to make my front matter section (between cover and contents page) use roman numerals for page numbers, and then my default page
style use Section-Page numbering.
Unfortunately the last page in the front matter section reverts to the default as my style is tied to each page keeping its section name. I can use hacks as you suggest to compensate but it is inelegant as you suggest.