Skip to content

Commit b4a253e

Browse files
author
chenyangsu
committed
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
1 parent fa74261 commit b4a253e

4 files changed

Lines changed: 99 additions & 2 deletions

File tree

‎news/changelog-1.12.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ All changes included in 1.12:
1919

2020
- ([#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.
2121
- ([#15269](https://github.com/quarto-dev/quarto-cli/pull/15269)): Fix `link-external-newwindow` overriding a `target` set explicitly on a link. (author: @mcanouil)
22+
- ([#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)
2223

2324
### `pdf`
2425

‎src/resources/formats/html/quarto.js‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,33 @@ window.document.addEventListener("DOMContentLoaded", function (_event) {
142142
});
143143
});
144144

145+
// An id may contain a "%" that does not start a valid escape (#15297), so
146+
// decoding must not throw: that would stop the rest of this handler.
147+
const safeDecodeURI = (value) => {
148+
try {
149+
return decodeURI(value);
150+
} catch {
151+
return value;
152+
}
153+
};
154+
155+
// data-scroll-target holds the link's raw href with ":" escaped as "\:".
156+
// Like the browser does for a URL fragment, look the id up as written first,
157+
// then percent-decoded.
145158
const sections = tocLinks.map((link) => {
146159
const target = link.getAttribute("data-scroll-target");
147160
if (target.startsWith("#")) {
148-
return window.document.getElementById(decodeURI(`${target.slice(1)}`));
161+
const id = target.slice(1).replace(/\\:/g, ":");
162+
return (
163+
window.document.getElementById(id) ||
164+
window.document.getElementById(safeDecodeURI(id))
165+
);
149166
} else {
150-
return window.document.querySelector(decodeURI(`${target}`));
167+
try {
168+
return window.document.querySelector(safeDecodeURI(target));
169+
} catch {
170+
return null;
171+
}
151172
}
152173
});
153174

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: "TOC highlighting follows headings with special characters in their id (#15297)"
3+
format:
4+
html:
5+
toc: true
6+
---
7+
8+
## Plain {#plain}
9+
10+
{{< lipsum 3 >}}
11+
12+
## Invalid percent escape {id="bad%zz"}
13+
14+
`decodeURI` throws on this id.
15+
16+
{{< lipsum 3 >}}
17+
18+
## Valid percent escape {id="bad%20ok"}
19+
20+
Decoding this id gives a different one, `bad ok`.
21+
22+
{{< lipsum 3 >}}
23+
24+
## Colon {#with:colon}
25+
26+
{{< lipsum 3 >}}
27+
28+
## Last {#last}
29+
30+
{{< lipsum 20 >}}

‎tests/integration/playwright/tests/html-toc-js.spec.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, Page, test } from "@playwright/test";
2+
import { getUrl } from "../src/utils";
23

34
const testPages = {
45
'posts': 'table.html',
@@ -12,3 +13,47 @@ test("toc navigation doesn't crash", async ({ page }) => {
1213
await page.goto('./blog/simple-blog/_site/posts/post-with-code/index.html');
1314
expect(messages).toStrictEqual([]);
1415
});
16+
17+
// Regression tests for #15297. quarto.js resolves each TOC link's
18+
// data-scroll-target to its section. An id with a "%" that does not start a
19+
// valid escape made decodeURI throw, which stopped the scroll tracking for
20+
// the whole page. An id that decodes to a different one, or that contains a
21+
// colon (escaped as "\:" in data-scroll-target), was never found, so its TOC
22+
// entry was never highlighted.
23+
test.describe("TOC highlighting with special characters in heading ids", () => {
24+
const sections = [
25+
{ id: "bad%zz", name: "Invalid percent escape" },
26+
{ id: "bad%20ok", name: "Valid percent escape" },
27+
{ id: "with:colon", name: "Colon" },
28+
];
29+
30+
const tocLink = (page: Page, name: string) =>
31+
page.locator("nav#TOC").getByRole("link", { name, exact: true });
32+
33+
test("loading the page raises no error", async ({ page }) => {
34+
const messages: string[] = [];
35+
page.on("pageerror", (error) => {
36+
messages.push(`[${error.name}] ${error.message}`);
37+
});
38+
await page.goto(getUrl("html/toc-scroll-target-ids.html"));
39+
await expect(tocLink(page, "Plain")).toHaveClass(/active/);
40+
expect(messages).toStrictEqual([]);
41+
});
42+
43+
for (const { id, name } of sections) {
44+
test(`scrolling to the "${id}" section highlights its TOC entry`, async ({
45+
page,
46+
}) => {
47+
await page.goto(getUrl("html/toc-scroll-target-ids.html"));
48+
await expect(tocLink(page, "Plain")).toHaveClass(/active/);
49+
50+
// quarto.js compares the scroll position with the section's offsetTop.
51+
await page.evaluate((id) => {
52+
window.scrollTo(0, document.getElementById(id)!.offsetTop);
53+
}, id);
54+
55+
await expect(tocLink(page, name)).toHaveClass(/active/);
56+
await expect(tocLink(page, "Plain")).not.toHaveClass(/active/);
57+
});
58+
}
59+
});

0 commit comments

Comments
 (0)