fix: resolve CSS counters in document order with proper scoping - #673
Merged
Merged
Conversation
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #480
With
inlinePseudoElements, counters used incontentwere computed rule by rule instead of in document order, and a counter was only visible to the element's descendants (via a per-element prototype chain), not its following siblings. So any interleaved numbering came out wrong, like headings where eachh1resets theh2counter:Part1, 1.1, 1.2, Part2, 2.1, 2.2Part1, 0.counter(counterh2), 0.counter(counterh2), Part2, 0.counter(counterh2), 0.counter(counterh2)Part1, 1.1, 1.2, Part2, 2.1, 2.2How it works now
Counters are resolved in a single pass over the document after all rules are applied, following CSS Lists 3:
counter-reset,counter-incrementandcounter-setare applied, using the declarations that win the cascade (before, every matching rule'scounter-*was applied, even overridden ones)::beforeand::aftercount as the element's first and last childcounter(x)was left as literal text)The pass only runs with
inlinePseudoElementson and when the CSS uses counters.Tests
Expected values come from what Chrome renders for the same CSS and HTML: the issue's headings, the reporter's exact CSS (where
counterh1is only incremented onh1:before, so it's scoped to that pseudo-element and theh2s see 0, same as in Chrome), a parent reset shadowing sibling resets, nested lists, a counter that's never reset,counter-set, and the cascade winner. The existing counter tests and thepseudo-elementsfixture pass unchanged.Performance
No difference for documents without counters. For a counter-heavy document (50 chapters × 6 numbered headings + paragraphs, ~650 elements),
4.23ms→4.83ms, since the pass copies the active counters at each element.🤖 Generated with Claude Code