fix: Make heading IDs CSS-safe while preserving Unicode text#92
Merged
dereuromark merged 2 commits intomasterfrom Mar 10, 2026
Merged
fix: Make heading IDs CSS-safe while preserving Unicode text#92dereuromark merged 2 commits intomasterfrom
dereuromark merged 2 commits intomasterfrom
Conversation
172aec2 to
a01b086
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #92 +/- ##
=========================================
Coverage 93.54% 93.55%
- Complexity 2312 2315 +3
=========================================
Files 79 79
Lines 6107 6113 +6
=========================================
+ Hits 5713 5719 +6
Misses 394 394 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR updates heading ID normalization to produce CSS-selector-safe IDs while preserving non-ASCII/Unicode heading text, and aligns related TOC expectations in tests.
Changes:
- Harden
HeadingIdTracker::normalizeId()by trimming, whitespace-to-dash normalization, Unicode-aware character filtering, dash collapsing, and empty-result fallback. - Add/adjust PHPUnit coverage for punctuation-heavy headings, dash collapsing, Unicode headings, and empty-result fallback.
- Update TOC test expectation for inline-math-derived heading IDs.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/Renderer/HeadingIdTracker.php |
Implements stricter, Unicode-aware heading ID normalization intended to be safe for CSS selectors / querySelector. |
tests/TestCase/Renderer/HeadingIdTrackerTest.php |
Adds new normalization assertions (special chars, dash collapsing, Unicode preservation, empty fallback). |
tests/TestCase/Extension/TableOfContentsExtensionTest.php |
Updates TOC ID expectation for inline math to match new normalization rules. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
This PR hardens heading ID normalization to avoid invalid CSS selectors, while preserving Unicode heading text (for example Japanese/Cyrillic headings).
Changes
HeadingIdTracker::normalizeId()in HeadingIdTracker.php#--using Unicode-aware regex:/[^\p{L}\p{N}_-]+/uheadingwhen result is empty日本語の見出し)###->heading)Equation-E=mc^2->Equation-E-mc-2Why
Some consumers (for example HTMX scroll restoration via
querySelector) require selector-safe IDs. The previous logic could emit unsafe IDs for punctuation-heavy headings, and the first revision over-sanitized non-ASCII headings. This change keeps IDs safe and internationalization-friendly.