Skip to content

Commit e2b8616

Browse files
cwickhamcderv
andauthored
Fix dashboard URL hash handling (#14820)
* Don't blank a dashboard when the URL hash names no page showPage() removes `active` from every nav tab and every .tab-pane, then restores it only on the pane matching the hash. When the hash names no page, nothing is restored and the dashboard shows an empty content area. Both callers passed the hash through unchecked: the processing on load and the popstate handler, which runs on every hash change. So any in-page anchor -- a footnote link, a cross-reference, a hand-written anchor -- blanked a dashboard with more than one page. Guard both with isPage(). isPage() itself built a CSS selector by concatenating the hash, which throws a SyntaxError for a hash that is not a valid selector (`#`, `#1foo`, `#a:b`). On load that throw would happen before the `hidden` class is removed, leaving the whole dashboard invisible, so switch it to getElementById. #9411 was a narrower instance of this, fixed in #11264 by keeping external links away from showPage(); the load and popstate paths were left unchanged. Closes #14818 * Start dashboard tabbing at the top when the URL names a page A dashboard navbar links to its pages with a URL hash, so `#sales` is the shareable URL for a page. Loading such a URL makes the browser set the sequential focus navigation starting point to the target element, which for a dashboard is the whole .tab-pane. The first Tab press then landed inside the page content, and the navbar -- earlier in DOM order -- could not be reached by tabbing forward at all. A page hash selects a page; it is not a position within one. So when the hash names a page, put the focus starting point back at the top of the document by focusing the body. This has to run after the browser has applied its own fragment behaviour, which is later than DOMContentLoaded, hence the deferral to load. Closes #14819 * Don't assume a fixed tab position for the dashboard navbar The focus-order test asserted that the navbar tab is the second stop. What sits at the top of the body is a format concern and can change -- a skip-to-content link would add a stop before it. Tab forward a bounded number of times and assert the navbar tab is reached, which tests the property the issue is about: the navbar is reachable by tabbing forward. * Guard against malformed percent-encoding in the dashboard URL hash decodeURIComponent throws on a hash like #%, which aborted the DOMContentLoaded handler before the hidden class was removed and left the dashboard blank, the same failure this series otherwise fixes. --------- Co-authored-by: Christophe Dervieux <christophe.dervieux@gmail.com>
1 parent a8b5b2b commit e2b8616

4 files changed

Lines changed: 188 additions & 7 deletions

File tree

‎news/changelog-1.11.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ All changes included in 1.11:
99
- ([#14615](https://github.com/quarto-dev/quarto-cli/issues/14615)): Fix invalid `role="menu"` on the website navbar's collapse toggle button, flagged by axe-core (`aria-allowed-role`) and WAVE (`aria_menu_broken`) when the navbar collapses to the hamburger at narrow viewports.
1010
- ([#12116](https://github.com/quarto-dev/quarto-cli/issues/12116), [#4935](https://github.com/quarto-dev/quarto-cli/issues/4935)): Website sidebar section toggles are now `<button>` elements, so the keyboard can reach them and `Enter` or `Space` can operate them. Each toggle carries `aria-expanded`, and `aria-labelledby` names each disclosed list after its own section. The toggles no longer carry `role="navigation"`, which removed a spurious navigation landmark from every section.
1111

12+
## Formats
13+
14+
### `dashboard`
15+
16+
- ([#14818](https://github.com/quarto-dev/quarto-cli/issues/14818)): Fix a dashboard with more than one page going blank when the URL hash does not name a page, such as a footnote link or a cross-reference. Such a hash is now left alone, so the current page stays visible.
17+
- ([#14819](https://github.com/quarto-dev/quarto-cli/issues/14819)): Fix keyboard focus starting inside the page content when a dashboard opens at a page hash, such as `dashboard.html#sales`. Tabbing forward could not reach the navbar. Focus now starts at the top of the document.
18+
1219
## Engines
1320

1421
### `knitr`

‎src/resources/formats/dashboard/quarto-dashboard.js‎

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,28 @@ window.document.addEventListener("DOMContentLoaded", function (_event) {
106106
}
107107
}
108108

109-
// Try to process the hash and activate a tab
110-
const hash = window.decodeURIComponent(window.location.hash);
111-
if (hash.length > 0) {
109+
// decodeURIComponent throws for malformed percent-encoding (e.g. "#%"); in
110+
// that case the raw hash is kept, which isPage() safely treats as a
111+
// non-page hash rather than leaving the dashboard hidden.
112+
const decodeHash = function (rawHash) {
113+
try {
114+
return window.decodeURIComponent(rawHash);
115+
} catch (_e) {
116+
return rawHash;
117+
}
118+
};
119+
120+
// Try to process the hash and activate a tab. A hash that does not name a
121+
// page (an in-page anchor, a footnote link, a cross-reference) is left
122+
// alone, so the browser can scroll to it and the current page stays visible.
123+
const hash = decodeHash(window.location.hash);
124+
if (QuartoDashboardUtils.isPage(hash)) {
112125
QuartoDashboardUtils.showPage(hash, () => {
126+
// a page hash selects a page, it is not a position within one, so undo
127+
// the browser's fragment behaviour of starting sequential focus inside
128+
// the target. Without this, tabbing from a shared "...#page" URL starts
129+
// inside the page content and never reaches the navbar.
130+
QuartoDashboardUtils.resetFocusToDocumentStart();
113131
window.document.documentElement.classList.remove("hidden");
114132
});
115133
} else {
@@ -118,8 +136,12 @@ window.document.addEventListener("DOMContentLoaded", function (_event) {
118136

119137
// navigate to a tab when the history changes
120138
window.addEventListener("popstate", function (e) {
121-
const hash = window.decodeURIComponent(window.location.hash);
122-
QuartoDashboardUtils.showPage(hash);
139+
const hash = decodeHash(window.location.hash);
140+
// only switch pages for a hash that names one; any other hash change
141+
// must leave the pages alone, otherwise showPage hides all of them
142+
if (hash.length === 0 || QuartoDashboardUtils.isPage(hash)) {
143+
QuartoDashboardUtils.showPage(hash);
144+
}
123145
});
124146

125147
// Hook tabs and use that to update history / active tabs
@@ -226,9 +248,40 @@ window.QuartoDashboardUtils = {
226248
window.scrollTo(0, 0);
227249
}, 10);
228250
},
251+
resetFocusToDocumentStart: function () {
252+
// focusing the body moves the sequential focus navigation starting point
253+
// back to the top of the document; the tabindex only needs to exist for
254+
// the focus() call, so it is removed again immediately.
255+
// this has to run after the browser has applied its own fragment
256+
// behaviour, which happens after DOMContentLoaded, hence the deferral
257+
const reset = function () {
258+
const bodyEl = document.body;
259+
bodyEl.setAttribute("tabindex", "-1");
260+
bodyEl.focus();
261+
bodyEl.removeAttribute("tabindex");
262+
};
263+
if (document.readyState === "complete") {
264+
window.setTimeout(reset, 0);
265+
} else {
266+
window.addEventListener("load", function () {
267+
window.setTimeout(reset, 0);
268+
});
269+
}
270+
},
229271
isPage: function (hash) {
230-
const tabPaneEl = document.querySelector(`.dashboard-page.tab-pane${hash}`);
231-
return tabPaneEl !== null;
272+
// use getElementById rather than building a selector: the hash comes from
273+
// the URL and may not be a valid CSS selector (`#`, `#1foo` and `#a:b` all
274+
// throw a SyntaxError in querySelector)
275+
const id = hash.startsWith("#") ? hash.substring(1) : hash;
276+
if (id === "") {
277+
return false;
278+
}
279+
const tabPaneEl = document.getElementById(id);
280+
return (
281+
tabPaneEl !== null &&
282+
tabPaneEl.classList.contains("dashboard-page") &&
283+
tabPaneEl.classList.contains("tab-pane")
284+
);
232285
},
233286
showPage: function (hash, fnCallback) {
234287
// If the hash is empty, just select the first tab and activate that
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
title: "Hash navigation"
3+
format: dashboard
4+
---
5+
6+
# Heat
7+
8+
## Row
9+
10+
::: {.card title="Heat content"}
11+
Content for the Heat page, with a footnote[^1] whose link is an in-page
12+
anchor that names no page.
13+
14+
[^1]: The footnote target is `#fn1`, not a dashboard page.
15+
16+
```{=html}
17+
<button id="heat-button">Heat button</button>
18+
```
19+
:::
20+
21+
# Cool
22+
23+
## Row
24+
25+
::: {.card title="Cool content"}
26+
Content for the Cool page.
27+
28+
```{=html}
29+
<button id="cool-button">Cool button</button>
30+
```
31+
:::
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import { test, expect } from '@playwright/test';
2+
3+
const dashboard = '/dashboard/hash-navigation.html';
4+
5+
const heatPane = (page) => page.locator('#heat.dashboard-page');
6+
const coolPane = (page) => page.locator('#cool.dashboard-page');
7+
8+
// https://github.com/quarto-dev/quarto-cli/issues/14818
9+
test.describe('a hash that names no page leaves the pages alone', () => {
10+
test('on load', async ({ page }) => {
11+
await page.goto(`${dashboard}#no-such-thing`);
12+
await expect(heatPane(page)).toHaveClass(/active/);
13+
await expect(coolPane(page)).not.toHaveClass(/active/);
14+
await expect(page.locator('html')).not.toHaveClass(/hidden/);
15+
});
16+
17+
test('on a hash change', async ({ page }) => {
18+
await page.goto(dashboard);
19+
await expect(heatPane(page)).toHaveClass(/active/);
20+
// an in-page anchor that exists but is not a page, such as a footnote
21+
await page.evaluate(() => { window.location.hash = '#fn1'; });
22+
await expect(heatPane(page)).toHaveClass(/active/);
23+
});
24+
25+
// isPage used to build a selector from the hash, which throws for a hash
26+
// that is not a valid CSS selector and left the whole dashboard hidden
27+
test('when the hash is not a valid CSS selector', async ({ page }) => {
28+
await page.goto(`${dashboard}#1foo`);
29+
await expect(page.locator('html')).not.toHaveClass(/hidden/);
30+
await expect(heatPane(page)).toHaveClass(/active/);
31+
});
32+
33+
// decodeURIComponent throws for malformed percent-encoding, which aborted
34+
// the DOMContentLoaded handler before the hidden class was removed
35+
test('when the hash is malformed percent-encoding', async ({ page }) => {
36+
await page.goto(`${dashboard}#%`);
37+
await expect(page.locator('html')).not.toHaveClass(/hidden/);
38+
await expect(heatPane(page)).toHaveClass(/active/);
39+
});
40+
});
41+
42+
test('page navigation and history still work', async ({ page }) => {
43+
await page.goto(dashboard);
44+
await expect(heatPane(page)).toHaveClass(/active/);
45+
46+
await page.locator('.navbar .nav-link[data-bs-target="#cool"]').click();
47+
await expect(coolPane(page)).toHaveClass(/active/);
48+
await expect(heatPane(page)).not.toHaveClass(/active/);
49+
50+
await page.goBack();
51+
await expect(heatPane(page)).toHaveClass(/active/);
52+
53+
await page.goForward();
54+
await expect(coolPane(page)).toHaveClass(/active/);
55+
});
56+
57+
// https://github.com/quarto-dev/quarto-cli/issues/14819
58+
test('landing on a page hash starts tabbing at the top of the document', async ({ page, browserName }) => {
59+
// WebKit only tabs to links when the Alt modifier is held, which matches
60+
// Safari's default "Press Tab to highlight each item on a webpage" setting
61+
const tabKey = browserName === 'webkit' ? 'Alt+Tab' : 'Tab';
62+
63+
await page.goto(`${dashboard}#cool`);
64+
await expect(coolPane(page)).toHaveClass(/active/);
65+
66+
// the browser would otherwise start sequential focus inside the target
67+
// pane, which puts the navbar out of reach of a forward Tab
68+
await page.keyboard.press(tabKey);
69+
const firstStop = await page.evaluate(() => ({
70+
isBody: document.activeElement === document.body,
71+
insideAPage: document.activeElement?.closest('.dashboard-page') !== null,
72+
}));
73+
expect(firstStop.isBody).toBe(false);
74+
expect(firstStop.insideAPage).toBe(false);
75+
76+
// the navbar tab for the current page is reachable by tabbing forward.
77+
// the exact number of stops before it depends on what else the format
78+
// puts at the top of the body, so tab forward a bounded number of times
79+
// rather than assuming a fixed position.
80+
let reachedNavbarTab = false;
81+
for (let i = 0; i < 5 && !reachedNavbarTab; i++) {
82+
reachedNavbarTab = await page.evaluate(
83+
() => document.activeElement?.id === 'tab-cool'
84+
);
85+
if (!reachedNavbarTab) {
86+
await page.keyboard.press(tabKey);
87+
}
88+
}
89+
expect(reachedNavbarTab).toBe(true);
90+
});

0 commit comments

Comments
 (0)