Skip to content

Commit 22e8518

Browse files
authored
Sort axe report violations by severity, then standard (#14676)
The axe document reporter listed violations in axe-core's raw scan order. Sort them most-important-first instead, matching axe DevTools' impact-first convention, so the overlay, RevealJS report slide, and dashboard offcanvas all surface the worst problems first. - Sort key: impact (critical > serious > moderate > minor > unknown), then conformance standard (WCAG A > AA > AAA > best practice > obsolete > untagged), then rule id alphabetically for determinism - Sorting happens on a copy in createReportElement, so json and console output modes are unchanged - Exports impactRank, standardRank, and compareViolations for unit testing, sharing a new parseConformanceTags helper with the existing axeConformanceLevel instead of duplicating tag-parsing regexes Part of the axe accessibility Quality of Life (QoL) series.
1 parent af16571 commit 22e8518

5 files changed

Lines changed: 249 additions & 20 deletions

File tree

‎news/changelog-1.10.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ All changes included in 1.10:
1919
- ([#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)
2020
- ([#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.
2121
- ([#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.
22+
- ([#14676](https://github.com/quarto-dev/quarto-cli/pull/14676)): The `axe` accessibility report UI now lists violations most-important-first: by impact (critical first), then WCAG conformance level (A before AA, current criteria before Best Practice and obsolete ones), then rule id. `output: json` still emits axe-core's raw, unsorted result.
2223
- ([#14677](https://github.com/quarto-dev/quarto-cli/pull/14677)): The `axe` option now uses a copy of axe-core bundled with Quarto instead of loading it from the Skypack CDN in the reader's browser. Accessibility checking now works offline, and viewing a rendered document no longer triggers a request to `cdn.skypack.dev`. The axe-core version is unchanged (4.10.3), so scan results are identical.
2324

2425
## Formats

‎src/resources/formats/html/axe/axe-check.js‎

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
1-
// Derive a human-readable WCAG conformance label from axe-core's `tags` array.
2-
// Tags encode the version+level (`wcag2a`, `wcag21aa`), the specific success
3-
// criteria (`wcag111` → 1.1.1), and `best-practice` for axe's own
4-
// recommendations that aren't tied to any WCAG success criterion. Returns "" when
5-
// no conformance tags are present so callers can fall back to the impact alone.
6-
export function axeConformanceLevel(tags) {
7-
if (tags.includes("best-practice")) return "Best Practice";
8-
9-
// Version+level: wcag2a, wcag2aa, wcag21aa, wcag22aa, ... An `-obsolete`
10-
// suffix (e.g. `wcag2a-obsolete` on the deprecated `duplicate-id` rule) marks
11-
// a criterion that was withdrawn from later WCAG versions, e.g. SC 4.1.1,
12-
// removed in WCAG 2.2. We surface the original level but flag it as obsolete
13-
// so a withdrawn criterion isn't mistaken for a current conformance failure.
1+
// Parse the conformance-related tags from axe-core's `tags` array. Tags encode
2+
// the version+level (`wcag2a`, `wcag21aa`), and `best-practice` for axe's own
3+
// recommendations that aren't tied to any WCAG success criterion. An
4+
// `-obsolete` suffix (e.g. `wcag2a-obsolete` on the deprecated `duplicate-id`
5+
// rule) marks a criterion that was withdrawn from later WCAG versions, e.g.
6+
// SC 4.1.1, removed in WCAG 2.2. Returns:
7+
// { bestPractice: true } for best-practice rules
8+
// { major, minor, level, obsolete } for WCAG version+level tags (level is
9+
// the raw "a"/"aa"/"aaa" string)
10+
// null when no conformance tags are present
11+
function parseConformanceTags(tags) {
12+
if (tags.includes("best-practice")) return { bestPractice: true };
1413
const versionTag = tags.find((t) => /^wcag\d+a+(-obsolete)?$/.test(t));
15-
// Without a version+level tag there's no conformance level to report, so fall
16-
// back to the impact alone rather than emitting a bare, level-less criterion.
17-
if (!versionTag) return "";
18-
14+
if (!versionTag) return null;
1915
const [, major, minor, level, obsolete] =
2016
versionTag.match(/^wcag(\d)(\d?)(a+)(-obsolete)?$/);
21-
let label = `WCAG ${major}.${minor || "0"} ${level.toUpperCase()}`;
17+
return { major, minor, level, obsolete: !!obsolete };
18+
}
19+
20+
// Derive a human-readable WCAG conformance label from axe-core's `tags` array,
21+
// including the specific success criteria (`wcag111` → 1.1.1). Returns "" when
22+
// no conformance tags are present so callers can fall back to the impact alone.
23+
export function axeConformanceLevel(tags) {
24+
const parsed = parseConformanceTags(tags);
25+
// Without conformance tags there's no level to report, so fall back to the
26+
// impact alone rather than emitting a bare, level-less criterion.
27+
if (!parsed) return "";
28+
if (parsed.bestPractice) return "Best Practice";
29+
30+
// We surface an obsolete criterion's original level but flag it as obsolete
31+
// so it isn't mistaken for a current conformance failure.
32+
let label = `WCAG ${parsed.major}.${parsed.minor || "0"} ${parsed.level.toUpperCase()}`;
2233

2334
// Success criteria: wcag111 → 1.1.1, wcag1410 → 1.4.10. Principle and
2435
// guideline are always single digits; the remainder is the criterion number.
@@ -32,7 +43,34 @@ export function axeConformanceLevel(tags) {
3243
if (criteria.length) {
3344
label += ` (${criteria.join(", ")})`;
3445
}
35-
return obsolete ? `Obsolete ${label}` : label;
46+
return parsed.obsolete ? `Obsolete ${label}` : label;
47+
}
48+
49+
// Rank a violation's axe-core `impact` for sorting: critical=0, serious=1,
50+
// moderate=2, minor=3. A null or unrecognized impact sorts last.
51+
export function impactRank(impact) {
52+
return { critical: 0, serious: 1, moderate: 2, minor: 3 }[impact] ?? 4;
53+
}
54+
55+
// Rank a violation's conformance standard for sorting: WCAG A=0, AA=1, AAA=2,
56+
// Best Practice=3, obsolete criteria=4, no conformance tags=5. Violations of a
57+
// current WCAG requirement outrank recommendations and withdrawn criteria.
58+
export function standardRank(tags) {
59+
const parsed = parseConformanceTags(tags);
60+
if (!parsed) return 5;
61+
if (parsed.bestPractice) return 3;
62+
if (parsed.obsolete) return 4;
63+
return parsed.level.length - 1;
64+
}
65+
66+
// Order report violations most-important-first: by impact, then by
67+
// conformance standard, then by rule id for deterministic output.
68+
export function compareViolations(a, b) {
69+
return (
70+
impactRank(a.impact) - impactRank(b.impact) ||
71+
standardRank(a.tags) - standardRank(b.tags) ||
72+
a.id.localeCompare(b.id)
73+
);
3674
}
3775

3876
class QuartoAxeReporter {
@@ -151,7 +189,9 @@ class QuartoAxeDocumentReporter extends QuartoAxeReporter {
151189
}
152190

153191
createReportElement() {
154-
const violations = this.axeResult.violations;
192+
// Sort a copy so this.axeResult stays untouched: the json reporter (and
193+
// anything else reading axeResult) must see raw axe-core output.
194+
const violations = [...this.axeResult.violations].sort(compareViolations);
155195
const reportElement = document.createElement("div");
156196
reportElement.className = "quarto-axe-report";
157197
if (violations.length === 0) {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
format:
3+
html:
4+
axe:
5+
output: document
6+
---
7+
8+
## Axe report sort order
9+
10+
Three violations with distinct impacts, chosen so axe-core's own order
11+
(roughly alphabetical by rule id: color-contrast, heading-order, image-alt)
12+
differs from the report's severity order (image-alt, color-contrast,
13+
heading-order).
14+
15+
This text violates contrast rules: [insufficient contrast.]{style="color: #eee; background: #fff;"} (serious, WCAG 2.0 AA)
16+
17+
<img src="placeholder.png" class="axe-test-img">
18+
19+
#### This heading skips from h2 to h4 (moderate, Best Practice)

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,32 @@ test.describe('Dashboard axe — re-scan on visibility change', () => {
487487
});
488488
});
489489

490+
test.describe('Axe report — violations sorted by severity (#14676)', () => {
491+
test('html — report lists critical before serious before moderate', async ({ page }) => {
492+
// Fixture triggers image-alt (critical), color-contrast (serious), and
493+
// heading-order (moderate). axe-core emits these roughly alphabetically by
494+
// rule id (color-contrast, heading-order, image-alt), which is not
495+
// severity order, so this fails if the report stops sorting.
496+
await page.goto('/html/axe-sort-order.html', { waitUntil: 'networkidle' });
497+
498+
const axeReport = page.locator('.quarto-axe-report');
499+
await expect(axeReport).toBeVisible({ timeout: 10000 });
500+
501+
const descriptions = await axeReport
502+
.locator('.quarto-axe-violation-description').allTextContents();
503+
const indexOf = (impact: string) =>
504+
descriptions.findIndex(d => d.startsWith(impact));
505+
506+
for (const impact of ['Critical', 'Serious', 'Moderate']) {
507+
expect(indexOf(impact),
508+
`Expected a ${impact} violation in: ${descriptions.join(' | ')}`)
509+
.toBeGreaterThanOrEqual(0);
510+
}
511+
expect(indexOf('Critical')).toBeLessThan(indexOf('Serious'));
512+
expect(indexOf('Serious')).toBeLessThan(indexOf('Moderate'));
513+
});
514+
});
515+
490516
test.describe('Axe — code line-number anchors have accessible names (#14655)', () => {
491517
test('html — numbered code block has no axe violations', async ({ page }) => {
492518
await page.goto('/html/axe-code-line-numbers.html', { waitUntil: 'networkidle' });

‎tests/unit/axe-sort.test.ts‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
* axe-sort.test.ts
3+
*
4+
* Tests the sort order our axe document reporter applies to violations:
5+
* impact (critical → serious → moderate → minor → null), then conformance
6+
* standard (WCAG A → AA → AAA → Best Practice → obsolete → none), then rule
7+
* id for deterministic output.
8+
*
9+
* These exercise our own ranking and comparison logic, not axe-core's rule
10+
* data: every input is a hand-written violation fixture (the contract axe
11+
* hands us). An axe-core upgrade that re-tags or re-ranks a rule therefore
12+
* cannot break these tests.
13+
*
14+
* Copyright (C) 2020-2025 Posit Software, PBC
15+
*/
16+
17+
import { unitTest } from "../test.ts";
18+
import { assertEquals } from "testing/asserts";
19+
import {
20+
compareViolations,
21+
impactRank,
22+
standardRank,
23+
} from "../../src/resources/formats/html/axe/axe-check.js";
24+
25+
unitTest(
26+
"impactRank - orders critical → serious → moderate → minor → null",
27+
// deno-lint-ignore require-await
28+
async () => {
29+
assertEquals(impactRank("critical"), 0);
30+
assertEquals(impactRank("serious"), 1);
31+
assertEquals(impactRank("moderate"), 2);
32+
assertEquals(impactRank("minor"), 3);
33+
assertEquals(impactRank(null), 4);
34+
assertEquals(impactRank("unheard-of"), 4);
35+
},
36+
);
37+
38+
unitTest(
39+
"standardRank - orders A → AA → AAA → Best Practice → obsolete → none",
40+
// deno-lint-ignore require-await
41+
async () => {
42+
assertEquals(standardRank(["wcag2a", "wcag111"]), 0);
43+
assertEquals(standardRank(["wcag21aa", "wcag143"]), 1);
44+
assertEquals(standardRank(["wcag2aaa"]), 2);
45+
assertEquals(standardRank(["best-practice", "cat.color"]), 3);
46+
assertEquals(standardRank(["wcag2a-obsolete", "wcag411"]), 4);
47+
assertEquals(standardRank(["cat.color"]), 5);
48+
},
49+
);
50+
51+
unitTest(
52+
"standardRank - best-practice takes precedence over any WCAG tags",
53+
// deno-lint-ignore require-await
54+
async () => {
55+
// Mirrors axeConformanceLevel: axe tags best-practice rules with related
56+
// WCAG criteria, but they're still recommendations, not requirements.
57+
assertEquals(standardRank(["best-practice", "wcag2a", "wcag111"]), 3);
58+
},
59+
);
60+
61+
unitTest(
62+
"compareViolations - impact ordering, null impact last",
63+
// deno-lint-ignore require-await
64+
async () => {
65+
const violations = [
66+
{ id: "r-minor", impact: "minor", tags: ["wcag2a"] },
67+
{ id: "r-null", impact: null, tags: ["wcag2a"] },
68+
{ id: "r-critical", impact: "critical", tags: ["wcag2a"] },
69+
{ id: "r-moderate", impact: "moderate", tags: ["wcag2a"] },
70+
{ id: "r-serious", impact: "serious", tags: ["wcag2a"] },
71+
];
72+
assertEquals(
73+
violations.sort(compareViolations).map((v) => v.id),
74+
["r-critical", "r-serious", "r-moderate", "r-minor", "r-null"],
75+
);
76+
},
77+
);
78+
79+
unitTest(
80+
"compareViolations - standard breaks ties within an impact",
81+
// deno-lint-ignore require-await
82+
async () => {
83+
const violations = [
84+
{ id: "r-none", impact: "serious", tags: ["cat.color"] },
85+
{ id: "r-obsolete", impact: "serious", tags: ["wcag2a-obsolete"] },
86+
{ id: "r-bp", impact: "serious", tags: ["best-practice"] },
87+
{ id: "r-aa", impact: "serious", tags: ["wcag21aa", "wcag143"] },
88+
{ id: "r-a", impact: "serious", tags: ["wcag2a", "wcag111"] },
89+
];
90+
assertEquals(
91+
violations.sort(compareViolations).map((v) => v.id),
92+
["r-a", "r-aa", "r-bp", "r-obsolete", "r-none"],
93+
);
94+
},
95+
);
96+
97+
unitTest(
98+
"compareViolations - impact outranks standard",
99+
// deno-lint-ignore require-await
100+
async () => {
101+
// A critical best-practice violation precedes a minor WCAG A one.
102+
const violations = [
103+
{ id: "r-minor-a", impact: "minor", tags: ["wcag2a"] },
104+
{ id: "r-critical-bp", impact: "critical", tags: ["best-practice"] },
105+
];
106+
assertEquals(
107+
violations.sort(compareViolations).map((v) => v.id),
108+
["r-critical-bp", "r-minor-a"],
109+
);
110+
},
111+
);
112+
113+
unitTest(
114+
"compareViolations - rule id breaks full ties deterministically",
115+
// deno-lint-ignore require-await
116+
async () => {
117+
const violations = [
118+
{ id: "image-alt", impact: "critical", tags: ["wcag2a"] },
119+
{ id: "area-alt", impact: "critical", tags: ["wcag2a"] },
120+
{ id: "button-name", impact: "critical", tags: ["wcag2a"] },
121+
];
122+
assertEquals(
123+
violations.sort(compareViolations).map((v) => v.id),
124+
["area-alt", "button-name", "image-alt"],
125+
);
126+
},
127+
);
128+
129+
unitTest(
130+
"compareViolations - sorting a copy leaves the input array untouched",
131+
// deno-lint-ignore require-await
132+
async () => {
133+
// The document reporter sorts a copy so axeResult keeps axe-core's raw
134+
// order for the json reporter.
135+
const violations = [
136+
{ id: "r-minor", impact: "minor", tags: ["wcag2a"] },
137+
{ id: "r-critical", impact: "critical", tags: ["wcag2a"] },
138+
];
139+
const sorted = [...violations].sort(compareViolations);
140+
assertEquals(sorted.map((v) => v.id), ["r-critical", "r-minor"]);
141+
assertEquals(violations.map((v) => v.id), ["r-minor", "r-critical"]);
142+
},
143+
);

0 commit comments

Comments
 (0)