Skip to content

Fix TOC highlight for heading ids containing % or : - #15326

Merged
cderv merged 2 commits into
quarto-dev:mainfrom
chenyangsu:fix/toc-highlight-percent-id
Nov 10, 2026
Merged

cderv merged 2 commits into
quarto-dev:mainfrom
chenyangsu:fix/toc-highlight-percent-id

Conversation

@chenyangsu

@chenyangsu chenyangsu commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #15297

quarto.js passes each table of contents data-scroll-target through decodeURI. A heading id with a % that does not start a valid escape ({id="bad%zz"}) makes it throw URIError: URI malformed. Nothing catches the error, so the rest of the DOMContentLoaded handler 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.ts writes data-scroll-target with : escaped as \: for CSS. Since the lookup moved to getElementById, 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 returns null. 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.qmd and tests in html-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):

  • referenced the GitHub issue this PR closes
  • updated the appropriate changelog in the PR
  • ensured the present test suite passes
  • added new tests
  • created a separate documentation PR in Quarto's website repo and linked it to this PR (not needed)

Testing note: I could not finish a full dev build on my Windows machine (configure.cmd hit 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 way format-html-bootstrap.ts does, loading this branch's quarto.js. All 4 pass in Chrome and Edge, and all 4 fail with main's quarto.js. I have not run them through quarto render or the CI wrapper, and not in Firefox or WebKit.

AI-assisted PR
  • AI tool used: AI coding assistant
  • Codebase grounding: local clone, git history of quarto.js / format-html-bootstrap.ts
  • Human review: I have reviewed, tested, and verified the AI-generated content before submitting.

@posit-snyk-bot

posit-snyk-bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues

💻 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
chenyangsu force-pushed the fix/toc-highlight-percent-id branch from 0b9a049 to b4a253e Compare November 10, 2026 03:43

@cderv cderv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you - this looks good.

@cderv
cderv merged commit ed0fe29 into quarto-dev:main Nov 10, 2026
46 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Table of contents highlight stops working when a heading id contains a percent sign

3 participants