Skip to content

Commit ed0fe29

Browse files
authored
Fix TOC highlight for heading ids containing % or : (#15326)
1 parent 9777eda commit ed0fe29

4 files changed

Lines changed: 94 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: 40 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,42 @@ 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 coverage for literal percent signs, percent escapes, and colons (#15297).
18+
test.describe("TOC highlighting with special characters in heading ids", () => {
19+
const sections = [
20+
{ id: "bad%zz", name: "Invalid percent escape" },
21+
{ id: "bad%20ok", name: "Valid percent escape" },
22+
{ id: "with:colon", name: "Colon" },
23+
];
24+
25+
const tocLink = (page: Page, name: string) =>
26+
page.locator("nav#TOC").getByRole("link", { name, exact: true });
27+
28+
test("loading the page raises no error", async ({ page }) => {
29+
const messages: string[] = [];
30+
page.on("pageerror", (error) => {
31+
messages.push(`[${error.name}] ${error.message}`);
32+
});
33+
await page.goto(getUrl("html/toc-scroll-target-ids.html"));
34+
await expect(tocLink(page, "Plain")).toHaveClass(/active/);
35+
expect(messages).toStrictEqual([]);
36+
});
37+
38+
for (const { id, name } of sections) {
39+
test(`scrolling to the "${id}" section highlights its TOC entry`, async ({
40+
page,
41+
}) => {
42+
await page.goto(getUrl("html/toc-scroll-target-ids.html"));
43+
await expect(tocLink(page, "Plain")).toHaveClass(/active/);
44+
45+
// quarto.js compares the scroll position with the section's offsetTop.
46+
await page.evaluate((id) => {
47+
window.scrollTo(0, document.getElementById(id)!.offsetTop);
48+
}, id);
49+
50+
await expect(tocLink(page, name)).toHaveClass(/active/);
51+
await expect(tocLink(page, "Plain")).not.toHaveClass(/active/);
52+
});
53+
}
54+
});

0 commit comments

Comments
 (0)