Document defined counter not available in margin-box
When rendering the following document, I was expecting the paragraph counter to be available in the top-right margin-box. As you can see in the rendered version, it's always set to zero.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<style>
@page {
size: A5;
@top-right {
content: "Paragraph counter: " counter(paragraph);
}
}
p {
counter-increment: paragraph;
break-before: page;
}
p:after {
break-before: avoid;
content: "Paragraph counter: " counter(paragraph);
display: block;
}
</style>
</head>
<body>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
<p>Paragraph 3</p>
<p>Paragraph 4</p>
<p>Paragraph 5</p>
</body>
</html>

I checked the spec, and if I understand it correctly it says this should be working:
If a counter that has not been reset or incremented within the margin context or the page context is used by counter() or counters() in the margin context, then the resultant value is exactly as if the page-margin box were an element within the document at the start of the page, inside the deepest element in the normal flow that spans the page break. Use of the counter in this way does not affect the calculation of the counter’s value.
CSS Paged Media Module Level 3 6.1. Page-based counters
I also checked this same document in prince and the counter seems to behave like the spec mentions.
Thanks a lot for the bug request (and the specification quote 😉).
@peregrinogris the spec you quote is about page based counters.
It's been a while since I dealt with css counters and the spec, but as far as I remember the only way to transfer counter values from document context into page/margin context was via string-set() in document context and string() in page context.
Possibly running heads and footers can do the trick, too.