Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/changelog-1.12.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
25 changes: 23 additions & 2 deletions src/resources/formats/html/quarto.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
});

Expand Down
30 changes: 30 additions & 0 deletions tests/docs/playwright/html/toc-scroll-target-ids.qmd
Original file line number Diff line number Diff line change
@@ -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 >}}
40 changes: 40 additions & 0 deletions tests/integration/playwright/tests/html-toc-js.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { expect, Page, test } from "@playwright/test";
import { getUrl } from "../src/utils";

const testPages = {
'posts': 'table.html',
Expand All @@ -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/);
});
}
});
Loading