diff --git a/CHANGELOG.md b/CHANGELOG.md
index 48740ebe5..896fd6692 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,7 @@
### ✨ New features/enhancements
- Made the graph's zoom, reset view and sidebar buttons keyboard and screen reader accessible (accessible names, visible focus outlines, and activation with Enter/Space)
+- Added an accessible name and description to the graph SVG, labelled its course areas and courses groups, labelled each course with its text, and hid decorative parts (arrows, and/or nodes, region shapes, and shadow and blur effects) from screen readers
- Changed the colour of hybrid nodes to avoid a visual clash with the darkened background when selecting a focus
- Updated section codes on the grid page to match the official timetable builder (listed as LEC/TUT/PRA instead of L/T/P)
- Updated text on the course panel of the grid page to better communicate the panel's behaviour
diff --git a/js/components/graph/Graph.js b/js/components/graph/Graph.js
index 022a7ef03..3a1e2db2c 100644
--- a/js/components/graph/Graph.js
+++ b/js/components/graph/Graph.js
@@ -8,6 +8,7 @@ import Edge from "./Edge"
import Node from "./Node"
import Button from "./Button"
import InfoBox from "./InfoBox"
+import { GRAPH_A11Y_TEXT, getGraphA11yDescription, getGraphA11yName } from "./a11y"
import GraphCanvas, { NODE_DROPSHADOW_FILTER_ID } from "./GraphCanvas"
import Sidebar from "./Sidebar"
import { numCredits, transformPoint, formatTransform } from "./util"
@@ -921,7 +922,13 @@ export class Graph extends React.Component {
)
}
- return {Object.values(boolsJSON).map(generateBool)}
+ // Hidden from screen readers: an "and"/"or" read on its own, away from the
+ // courses it connects, is meaningless
+ return (
+
+ {Object.values(boolsJSON).map(generateBool)}
+
+ )
}
/**
@@ -970,7 +977,12 @@ export class Graph extends React.Component {
}
})
- return {edgesCopy.map(generateEdge)}
+ // Hidden from screen readers: the arrows are purely visual
+ return (
+
+ {edgesCopy.map(generateEdge)}
+
+ )
}
/**
@@ -995,7 +1007,7 @@ export class Graph extends React.Component {
nodeDropshadowFilter
) => {
return (
-
+
{Object.values(hybridsJSON).map(entry => {
return (
+ return (
+
+ )
})
}
@@ -1084,7 +1104,7 @@ export class Graph extends React.Component {
renderRegionsLabels(regionsJSON, labelsJSON) {
return (
-
+
{this.renderRegions(regionsJSON)}
{this.renderLabels(labelsJSON)}
@@ -1204,6 +1224,10 @@ export class Graph extends React.Component {
zoomFactor={this.state.zoomFactor}
horizontalPanFactor={this.state.horizontalPanFactor}
verticalPanFactor={this.state.verticalPanFactor}
+ a11yName={getGraphA11yName(this.state.graphName)}
+ a11yDescription={getGraphA11yDescription(
+ Object.values(this.state.labelsJSON).map(label => label.text)
+ )}
{...svgMouseEvents}
>
{this.renderRegionsLabels(this.state.regionsJSON, this.state.labelsJSON)}
diff --git a/js/components/graph/GraphCanvas.tsx b/js/components/graph/GraphCanvas.tsx
index de2abdf07..06c37057a 100644
--- a/js/components/graph/GraphCanvas.tsx
+++ b/js/components/graph/GraphCanvas.tsx
@@ -21,6 +21,10 @@ interface GraphCanvasProps {
zoomFactor: number
horizontalPanFactor: number
verticalPanFactor: number
+ /** Accessible name of the graph, announced by screen readers */
+ a11yName?: string
+ /** Longer accessible description of the graph, rendered in a `` */
+ a11yDescription?: string
onMouseDown?: React.MouseEventHandler
onMouseUp?: React.MouseEventHandler
onMouseMove?: React.MouseEventHandler
@@ -38,6 +42,8 @@ export default function GraphCanvas({
zoomFactor,
horizontalPanFactor,
verticalPanFactor,
+ a11yName,
+ a11yDescription,
onMouseDown,
onMouseUp,
onMouseMove,
@@ -77,12 +83,15 @@ export default function GraphCanvas({
height="100%"
viewBox={`${viewboxX} ${viewboxY} ${newViewboxWidth} ${newViewboxHeight}`}
preserveAspectRatio="xMidYMid"
+ role="graphics-document"
+ aria-label={a11yName}
onMouseDown={onMouseDown}
onMouseUp={onMouseUp}
onMouseMove={onMouseMove}
onTouchStart={onTouchStart}
>
-
+ {a11yDescription && {a11yDescription}}
+
diff --git a/js/components/graph/Node.tsx b/js/components/graph/Node.tsx
index fdb8d8e32..a9d72c4de 100644
--- a/js/components/graph/Node.tsx
+++ b/js/components/graph/Node.tsx
@@ -1,5 +1,6 @@
import React from "react"
import type { GraphNodeJSON } from "./types"
+import { getNodeA11yLabel } from "./a11y"
interface NodeProps {
className: string
@@ -113,6 +114,8 @@ export default function Node(props: NodeProps) {
className={newClassName}
data-testid={getDataTestId()}
transform={props.transform}
+ role="img"
+ aria-label={getNodeA11yLabel(props.JSON.text.map(textTag => textTag.text))}
>
{ellipse}
{
+ it("names the graph after the displayed graph and describes it", async () => {
+ await TestGraph.build()
+ const svg = document.querySelector("#main-graph")
+ expect(svg.getAttribute("aria-label")).toBe("Computer Science prerequisite graph")
+ // The test graph has no area labels, so the description has no examples
+ expect(svg.querySelector(":scope > desc").textContent).toBe(
+ getGraphA11yDescription([])
+ )
+ })
+
+ it("gives the graph's own area labels as examples in its description", async () => {
+ const labelledData = {
+ ...testData,
+ texts: [
+ ...testData.texts,
+ { graph: 1, rId: "tspan1", text: "Theory of Computing", pos: [0, 0], fill: "" },
+ { graph: 1, rId: "tspan2", text: "Computer Systems", pos: [0, 20], fill: "" },
+ ],
+ }
+ fetchMock.get(
+ "http://localhost/get-json-data?graphName=Labelled+Graph",
+ labelledData
+ )
+
+ render()
+ await screen.findByText("Theory of Computing")
+
+ const svg = document.querySelector("#main-graph")
+ expect(svg.getAttribute("aria-label")).toBe("Labelled Graph prerequisite graph")
+ expect(svg.querySelector(":scope > desc").textContent).toContain(
+ "such as Theory of Computing, and Computer Systems."
+ )
+ })
+
+ it("labels the course areas and courses groups", async () => {
+ await TestGraph.build()
+ expect(screen.getByRole("group", { name: "Course areas" }).id).toBe("regions")
+ expect(screen.getByRole("group", { name: "Courses" }).id).toBe("nodes")
+ })
+
+ it("hides the and/or nodes and arrows from screen readers", async () => {
+ await TestGraph.build()
+ expect(document.querySelector("#bools").getAttribute("aria-hidden")).toBe("true")
+ expect(document.querySelector("#edges").getAttribute("aria-hidden")).toBe("true")
+ })
+
+ it("hides the region shapes but keeps the region labels readable", () => {
+ // The test graph data has no regions or labels, so render them directly
+ const graph = new Graph(Graph.defaultProps)
+ const regionsJSON = {
+ region1: {
+ id_: "region1",
+ points: [
+ [0, 0],
+ [10, 0],
+ [10, 10],
+ ],
+ fill: "#fff",
+ },
+ }
+ const labelsJSON = {
+ tspan1: { rId: "tspan1", pos: [5, 5], fill: "#000", text: "Theory of Computing" },
+ }
+ const { container } = render(
+
+ )
+
+ expect(container.querySelector(".region").getAttribute("aria-hidden")).toBe("true")
+ const label = screen.getByText("Theory of Computing")
+ expect(label.hasAttribute("aria-hidden")).toBe(false)
+ })
+
+ it("labels each course and hybrid node with its text, as a single image", async () => {
+ await TestGraph.build()
+ const courseNode = document.querySelector("#nodes .node")
+ const hybridNode = document.querySelector("#nodes .hybrid")
+ expect(hybridNode).not.toBeNull()
+ for (const node of [courseNode, hybridNode]) {
+ // The img role stops screen readers from navigating into the node's contents
+ expect(node.getAttribute("role")).toBe("img")
+ expect(node.getAttribute("aria-label")).toBe(node.textContent)
+ }
+ expect(screen.getByRole("img", { name: "AAA100" }).id).toBe("aaa100")
+ })
+
+ it("keeps the spotlight around hovered courses inside the labelled node", async () => {
+ const user = userEvent.setup()
+ await TestGraph.build()
+ await user.hover(screen.getByText("AAA303"))
+ const spotlights = document.querySelectorAll(".spotlight, .spotlight-focus")
+ expect(spotlights.length).toBeGreaterThan(0)
+ spotlights.forEach(spotlight =>
+ expect(spotlight.closest('[role="img"]')).not.toBeNull()
+ )
+ })
+})
+
const getViewBox = () => document.querySelector("#main-graph").getAttribute("viewBox")
describe("Graph controls", () => {
diff --git a/js/components/graph/__tests__/GraphCanvas.test.js b/js/components/graph/__tests__/GraphCanvas.test.js
index 52f155458..1967e1123 100644
--- a/js/components/graph/__tests__/GraphCanvas.test.js
+++ b/js/components/graph/__tests__/GraphCanvas.test.js
@@ -70,4 +70,47 @@ describe("GraphCanvas", () => {
expect(document.querySelector("#main-graph > #test-child")).not.toBeNull()
})
+
+ describe("accessibility", () => {
+ it("labels the graph and describes it with a ", () => {
+ createContainer(1000, 500, false)
+ const { container } = render(
+
+ )
+ const svg = container.querySelector("#main-graph")
+ expect(svg.getAttribute("role")).toBe("graphics-document")
+ expect(svg.getAttribute("aria-label")).toBe("Computer Science prerequisite graph")
+ // A direct child is automatically the