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..339d90a06e2 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,42 @@ 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 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" }, + { 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/); + }); + } +});