Repository navigation
Fix TOC highlight for heading ids containing % or : - #15326
Merged
Merged
Conversation
Collaborator
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
quarto.js passed each TOC link's data-scroll-target through decodeURI.
An id with a "%" that does not start a valid escape (id="bad%zz") made it
throw, which aborted the rest of the DOMContentLoaded handler, so the TOC
stopped following the scroll position on the whole page.
Two related cases never threw but never matched either: an id with a valid
escape (id="bad%20ok") was only looked up decoded ("bad ok"), and an id
with a colon was looked up with the "\:" CSS escape that
format-html-bootstrap.ts adds to data-scroll-target.
Look the id up as written first (with "\:" unescaped), then decoded, like
the browser resolves a URL fragment, and never let decoding or an invalid
selector throw.
Fixes quarto-dev#15297
chenyangsu
force-pushed
the
fix/toc-highlight-percent-id
branch
from
November 10, 2026 03:43
0b9a049 to
b4a253e
Compare
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.
Description
Fixes #15297
quarto.jspasses each table of contentsdata-scroll-targetthroughdecodeURI. A heading id with a%that does not start a valid escape ({id="bad%zz"}) makes it throwURIError: URI malformed. Nothing catches the error, so the rest of theDOMContentLoadedhandler is skipped and the TOC stops following the scroll position on the whole page.While fixing this I found two related cases that never threw but were never highlighted either:
{id="bad%20ok"}was looked up only after decoding (bad ok), so it matched nothing.{#with:colon}:format-html-bootstrap.tswritesdata-scroll-targetwith:escaped as\:for CSS. Since the lookup moved togetElementById, that escape is kept in the id, so colon ids matched nothing.The lookup now tries the id as written first (with
\:unescaped), then percent-decoded, which is how browsers resolve a URL fragment. Decoding falls back to the raw value instead of throwing, and an invalid selector in the non-#branch returnsnull. Ids that resolved before still resolve to the same element, unless a page also has a second element whose id is the encoded form of that id. Percent-encoded non-ASCII hrefs still work.Tests: a new Playwright fixture
tests/docs/playwright/html/toc-scroll-target-ids.qmdand tests inhtml-toc-js.spec.ts. They check that the page loads with no errors and that scrolling to each special-id section highlights its TOC entry.Checklist
I have (if applicable):
Testing note: I could not finish a full dev build on my Windows machine (
configure.cmdhit path-length issues). I ran the new Playwright tests in Chrome and Edge against a page rendered with Pandoc 3.10 and post-processed the wayformat-html-bootstrap.tsdoes, loading this branch'squarto.js. All 4 pass in Chrome and Edge, and all 4 fail withmain'squarto.js. I have not run them throughquarto renderor the CI wrapper, and not in Firefox or WebKit.AI-assisted PR
quarto.js/format-html-bootstrap.ts