From b4a253ef6e3e893552e6f7fa9a40fe8eb233678d Mon Sep 17 00:00:00 2001 From: chenyangsu Date: Mon, 9 Nov 2026 18:20:49 -0800 Subject: [PATCH 1/2] Fix TOC highlight for heading ids containing % or : 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 #15297 --- news/changelog-1.12.md | 1 + src/resources/formats/html/quarto.js | 25 ++++++++++- .../playwright/html/toc-scroll-target-ids.qmd | 30 +++++++++++++ .../playwright/tests/html-toc-js.spec.ts | 45 +++++++++++++++++++ 4 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 tests/docs/playwright/html/toc-scroll-target-ids.qmd diff --git a/news/changelog-1.12.md b/news/changelog-1.12.md index f2550ff7ea6..c2584c9d826 100644 --- a/news/changelog-1.12.md +++ b/news/changelog-1.12.md @@ -19,6 +19,7 @@ All changes included in 1.12: - ([#9606](https://github.com/quarto-dev/quarto-cli/issues/9606)): Fix `format-links` entries written as `{format, text}` adding a second, default link for the same format instead of renaming its link. - ([#15269](https://github.com/quarto-dev/quarto-cli/pull/15269)): Fix `link-external-newwindow` overriding a `target` set explicitly on a link. (author: @mcanouil) +- ([#15297](https://github.com/quarto-dev/quarto-cli/issues/15297)): Fix the table of contents highlight not following the scroll position when a heading id contains a `%` or a `:`. (author: @chenyangsu) ### `pdf` diff --git a/src/resources/formats/html/quarto.js b/src/resources/formats/html/quarto.js index 4b5946e178a..fc851774b30 100644 --- a/src/resources/formats/html/quarto.js +++ b/src/resources/formats/html/quarto.js @@ -142,12 +142,33 @@ window.document.addEventListener("DOMContentLoaded", function (_event) { }); }); + // An id may contain a "%" that does not start a valid escape (#15297), so + // decoding must not throw: that would stop the rest of this handler. + const safeDecodeURI = (value) => { + try { + return decodeURI(value); + } catch { + return value; + } + }; + + // data-scroll-target holds the link's raw href with ":" escaped as "\:". + // Like the browser does for a URL fragment, look the id up as written first, + // then percent-decoded. const sections = tocLinks.map((link) => { const target = link.getAttribute("data-scroll-target"); if (target.startsWith("#")) { - return window.document.getElementById(decodeURI(`${target.slice(1)}`)); + const id = target.slice(1).replace(/\\:/g, ":"); + return ( + window.document.getElementById(id) || + window.document.getElementById(safeDecodeURI(id)) + ); } else { - return window.document.querySelector(decodeURI(`${target}`)); + try { + return window.document.querySelector(safeDecodeURI(target)); + } catch { + return null; + } } }); diff --git a/tests/docs/playwright/html/toc-scroll-target-ids.qmd b/tests/docs/playwright/html/toc-scroll-target-ids.qmd new file mode 100644 index 00000000000..919043ea12d --- /dev/null +++ b/tests/docs/playwright/html/toc-scroll-target-ids.qmd @@ -0,0 +1,30 @@ +--- +title: "TOC highlighting follows headings with special characters in their id (#15297)" +format: + html: + toc: true +--- + +## Plain {#plain} + +{{< lipsum 3 >}} + +## Invalid percent escape {id="bad%zz"} + +`decodeURI` throws on this id. + +{{< lipsum 3 >}} + +## Valid percent escape {id="bad%20ok"} + +Decoding this id gives a different one, `bad ok`. + +{{< lipsum 3 >}} + +## Colon {#with:colon} + +{{< lipsum 3 >}} + +## Last {#last} + +{{< lipsum 20 >}} diff --git a/tests/integration/playwright/tests/html-toc-js.spec.ts b/tests/integration/playwright/tests/html-toc-js.spec.ts index 9c4cbaa3562..a0720423ceb 100644 --- a/tests/integration/playwright/tests/html-toc-js.spec.ts +++ b/tests/integration/playwright/tests/html-toc-js.spec.ts @@ -1,4 +1,5 @@ import { expect, Page, test } from "@playwright/test"; +import { getUrl } from "../src/utils"; const testPages = { 'posts': 'table.html', @@ -12,3 +13,47 @@ test("toc navigation doesn't crash", async ({ page }) => { await page.goto('./blog/simple-blog/_site/posts/post-with-code/index.html'); expect(messages).toStrictEqual([]); }); + +// Regression tests for #15297. quarto.js resolves each TOC link's +// data-scroll-target to its section. An id with a "%" that does not start a +// valid escape made decodeURI throw, which stopped the scroll tracking for +// the whole page. An id that decodes to a different one, or that contains a +// colon (escaped as "\:" in data-scroll-target), was never found, so its TOC +// entry was never highlighted. +test.describe("TOC highlighting with special characters in heading ids", () => { + const sections = [ + { id: "bad%zz", name: "Invalid percent escape" }, + { id: "bad%20ok", name: "Valid percent escape" }, + { id: "with:colon", name: "Colon" }, + ]; + + const tocLink = (page: Page, name: string) => + page.locator("nav#TOC").getByRole("link", { name, exact: true }); + + test("loading the page raises no error", async ({ page }) => { + const messages: string[] = []; + page.on("pageerror", (error) => { + messages.push(`[${error.name}] ${error.message}`); + }); + await page.goto(getUrl("html/toc-scroll-target-ids.html")); + await expect(tocLink(page, "Plain")).toHaveClass(/active/); + expect(messages).toStrictEqual([]); + }); + + for (const { id, name } of sections) { + test(`scrolling to the "${id}" section highlights its TOC entry`, async ({ + page, + }) => { + await page.goto(getUrl("html/toc-scroll-target-ids.html")); + await expect(tocLink(page, "Plain")).toHaveClass(/active/); + + // quarto.js compares the scroll position with the section's offsetTop. + await page.evaluate((id) => { + window.scrollTo(0, document.getElementById(id)!.offsetTop); + }, id); + + await expect(tocLink(page, name)).toHaveClass(/active/); + await expect(tocLink(page, "Plain")).not.toHaveClass(/active/); + }); + } +}); From f0b1c7913ab7f0b2404c0e8882da3a24f82bdb5b Mon Sep 17 00:00:00 2001 From: Christophe Dervieux Date: Tue, 10 Nov 2026 12:54:59 +0100 Subject: [PATCH 2/2] Trim comment --- tests/integration/playwright/tests/html-toc-js.spec.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/tests/integration/playwright/tests/html-toc-js.spec.ts b/tests/integration/playwright/tests/html-toc-js.spec.ts index a0720423ceb..339d90a06e2 100644 --- a/tests/integration/playwright/tests/html-toc-js.spec.ts +++ b/tests/integration/playwright/tests/html-toc-js.spec.ts @@ -14,12 +14,7 @@ test("toc navigation doesn't crash", async ({ page }) => { expect(messages).toStrictEqual([]); }); -// Regression tests for #15297. quarto.js resolves each TOC link's -// data-scroll-target to its section. An id with a "%" that does not start a -// valid escape made decodeURI throw, which stopped the scroll tracking for -// the whole page. An id that decodes to a different one, or that contains a -// colon (escaped as "\:" in data-scroll-target), was never found, so its TOC -// entry was never highlighted. +// Regression coverage for literal percent signs, percent escapes, and colons (#15297). test.describe("TOC highlighting with special characters in heading ids", () => { const sections = [ { id: "bad%zz", name: "Invalid percent escape" },