Skip to content

Commit 8577ed1

Browse files
authored
Fix code-line-numbers links having no accessible name (#14662)
* Add aria-label to code line-number anchors (#14655) Skylighting's numberSource line numbering emits an empty <a> per line, with the visible number drawn purely via a CSS counter on ::before. Screen readers and axe-core see a focusable link with no discernible text (WCAG 2.4.4/4.1.2). aria-hidden isn't an option since these anchors are real, focusable fragment links, and aria-hidden on a focusable element is itself an axe violation. Instead, set aria-label to the visible line number, computed from any counter-reset offset so custom startFrom values stay correct. Implemented as a postprocessor pass in htmlFormatPostprocessor, which revealjs's htmlFormatExtras also inherits, so one pass covers both. * Add playwright regression coverage for code line-number aria-label (#14655) Verified against real axe-core 4.10 in a browser: the html fixture (numbered code, no other violation source) now reports zero axe-core violations, and the revealjs fixture's report has no violation targeting a code line-number anchor (only the pre-existing, unrelated slide-menu-button/viewport-meta violations remain, tracked separately).
1 parent effe145 commit 8577ed1

6 files changed

Lines changed: 166 additions & 0 deletions

File tree

‎news/changelog-1.10.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ All changes included in 1.10:
1717
- ([#14468](https://github.com/quarto-dev/quarto-cli/issues/14468)): The `axe` accessibility report UI (HTML overlay, revealjs report slide, dashboard offcanvas) now uses its own theme-independent colors instead of inheriting from `brand` or theme. Keeps the report readable regardless of page styling, and stops `axe` from clobbering brand colors set via `_brand.yml`.
1818
- ([#14602](https://github.com/quarto-dev/quarto-cli/pull/14602), [#14632](https://github.com/quarto-dev/quarto-cli/pull/14632)): Fix ORCID profile link having no accessible name for screen readers in HTML, Reveal.js, and ipynb title-block author metadata. (author: @mcanouil for #14602)
1919
- ([#14604](https://github.com/quarto-dev/quarto-cli/issues/14604)): The `axe` accessibility report UI now shows each violation's WCAG conformance level (e.g. `WCAG 2.0 AA (1.4.3)`) or `Best Practice`, derived from the violation's axe-core tags.
20+
- ([#14655](https://github.com/quarto-dev/quarto-cli/issues/14655)): Add accessible names to code line-number links so screen readers and accessibility audits no longer report them as empty links.
2021

2122
## Formats
2223

‎src/format/html/format-html.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,40 @@ function htmlFormatPostprocessor(
732732
for (let i = 0; i < codeBlocks.length; i++) {
733733
const code = codeBlocks[i] as Element;
734734

735+
// Give each line-number anchor an accessible name (#14655). Skylighting
736+
// emits an empty <a href="#cbN-n"></a> as the first child of every line
737+
// span and draws the visible number purely via a CSS counter on
738+
// ::before, so axe-core reports these focusable links as having no
739+
// discernible text (WCAG 2.4.4/4.1.2). Set aria-label to the visible
740+
// line number. aria-hidden is not an option here: these anchors are
741+
// focusable fragment links, and aria-hidden on a focusable element is
742+
// itself an accessibility violation.
743+
if (code.classList.contains("numberSource")) {
744+
const codeEl = code.querySelector("code");
745+
// Custom start numbers surface as `counter-reset: source-line N` on
746+
// the <code>; the first visible line is then N + 1. Default is 0.
747+
let startZero = 0;
748+
// Signed digits: startFrom="0" emits `counter-reset: source-line -1`,
749+
// and negative starts are possible — a bare \d+ would drop the sign and
750+
// mislabel. skylighting emits exactly one well-formed declaration, so
751+
// multiple/malformed counter-reset is not a real input.
752+
const counterMatch = codeEl?.getAttribute("style")?.match(
753+
/counter-reset:\s*source-line\s+(-?\d+)/,
754+
);
755+
if (counterMatch) {
756+
startZero = parseInt(counterMatch[1], 10);
757+
}
758+
const lineAnchors = code.querySelectorAll(
759+
"code > span > a:first-child",
760+
);
761+
for (let j = 0; j < lineAnchors.length; j++) {
762+
const anchor = lineAnchors[j] as Element;
763+
if (!anchor.hasAttribute("aria-label")) {
764+
anchor.setAttribute("aria-label", String(startZero + j + 1));
765+
}
766+
}
767+
}
768+
735769
// hoist hidden and cell-code to parent div
736770
const parentHoist = (clz: string) => {
737771
if (code.classList.contains(clz)) {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
format:
3+
html:
4+
axe:
5+
output: document
6+
---
7+
8+
# Numbered code
9+
10+
```{.r .numberLines}
11+
x <- 1
12+
y <- 2
13+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
format:
3+
revealjs:
4+
axe:
5+
output: document
6+
---
7+
8+
## Numbered code
9+
10+
```{.r .numberLines}
11+
x <- 1
12+
y <- 2
13+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: "code-line-numbers anchors have accessible names (#14655)"
3+
format:
4+
html: default
5+
revealjs: default
6+
_quarto:
7+
tests:
8+
html:
9+
ensureHtmlElements:
10+
-
11+
- 'pre.numberSource a[aria-label="1"]'
12+
- 'pre.numberSource a[aria-label="2"]'
13+
- 'pre.sourceCode:not(.numberSource) a[aria-hidden="true"]'
14+
- '#startfrom a[aria-label="100"]'
15+
- '#startfrom a[aria-label="101"]'
16+
- '#badstart a[aria-label="1"]'
17+
- '#badstart a[aria-label="2"]'
18+
-
19+
- 'pre.numberSource a[href]:not([aria-label])'
20+
- 'pre.numberSource a[href][aria-label=""]'
21+
- 'pre.sourceCode:not(.numberSource) a[aria-label]'
22+
revealjs:
23+
ensureHtmlElements:
24+
-
25+
- 'pre.numberSource a[aria-label="1"]'
26+
- 'pre.numberSource a[aria-label="2"]'
27+
-
28+
- 'pre.numberSource a[href]:not([aria-label])'
29+
---
30+
31+
## Canonical numbered (row 2)
32+
33+
```{.r .numberLines}
34+
x <- 1
35+
y <- 2
36+
```
37+
38+
## Hyphen-synonym numbered (row 3)
39+
40+
```{.python .number-lines}
41+
a = 1
42+
b = 2
43+
```
44+
45+
## No-language numbered (row 8)
46+
47+
```{.numberLines}
48+
plain line one
49+
plain line two
50+
```
51+
52+
## lineAnchors only — must stay non-numbered (row 10)
53+
54+
```{.r .lineAnchors}
55+
z <- 3
56+
```
57+
58+
## Second numbered block, no id (row 11)
59+
60+
```{.python .numberLines}
61+
p = 1
62+
q = 2
63+
```
64+
65+
## Custom start (row 5)
66+
67+
```{#startfrom .python .numberLines startFrom="100"}
68+
a = 1
69+
b = 2
70+
```
71+
72+
## Invalid start falls back to 1 (row 6)
73+
74+
```{#badstart .python .numberLines startFrom="notanumber"}
75+
c = 3
76+
d = 4
77+
```

‎tests/integration/playwright/tests/axe-accessibility.spec.ts‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,31 @@ test.describe('Dashboard axe — re-scan on visibility change', () => {
486486
'Tab A restored: #tab-a-contrast should be detected again').toBe(true);
487487
});
488488
});
489+
490+
test.describe('Axe — code line-number anchors have accessible names (#14655)', () => {
491+
test('html — numbered code block has no axe violations', async ({ page }) => {
492+
await page.goto('/html/axe-code-line-numbers.html', { waitUntil: 'networkidle' });
493+
await waitForAxeCompletion(page);
494+
495+
const axeReport = page.locator('.quarto-axe-report');
496+
await expect(axeReport).toBeVisible({ timeout: 10000 });
497+
await expect(axeReport.locator('.quarto-axe-no-violations'))
498+
.toHaveText('No axe-core violations found.');
499+
});
500+
501+
test('revealjs — code line-number anchors are not reported for missing link text', async ({ page }) => {
502+
await page.goto('/revealjs/axe-code-line-numbers.html', { waitUntil: 'networkidle' });
503+
await waitForAxeCompletion(page);
504+
505+
const reportSlide = page.locator('section.quarto-axe-report-slide');
506+
await expect(reportSlide).toBeAttached({ timeout: 10000 });
507+
508+
// RevealJS chrome (.slide-menu-button) and its default viewport meta trip
509+
// their own pre-existing, unrelated violations — out of scope for #14655.
510+
// The regression guard here is narrower: no violation should target a
511+
// code line-number anchor (id `cbN-n`).
512+
const targets = await reportSlide.locator('.quarto-axe-violation-target').allTextContents();
513+
const codeLineTargets = targets.filter((t) => /#cb\d+-\d+/.test(t));
514+
expect(codeLineTargets, `Unexpected code-line-number targets: ${targets.join(', ')}`).toEqual([]);
515+
});
516+
});

0 commit comments

Comments
 (0)