Skip to content

Commit 7ab7608

Browse files
committed
fix: dynamically calculate th aria role
1 parent 27845ff commit 7ab7608

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

.changeset/healthy-jobs-act.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"dom-accessibility-api": patch
3+
---
4+
5+
Dynamically calculates the aria role for <th> elements instead of always returning 'columnheader'

sources/__tests__/getRole.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,13 @@ const cases = [
168168
["title", null, createElementFactory("title", {})],
169169
// WARNING: Only in certain contexts
170170
["td", "cell", createElementFactory("td", {})],
171-
["th", "columnheader", createElementFactory("th", {})],
171+
// default scope=auto
172+
["th missing scope", "columnheader", createElementFactory("th", {})],
173+
["th scope explicitly set to `auto`", "columnheader", createElementFactory("th", {scope:"auto"})],
174+
["th scope=col", "columnheader", createElementFactory("th", {scope:"col"})],
175+
["th scope=colgroup", "columnheader", createElementFactory("th", {scope:"colgroup"})],
176+
["th scope=row", "rowheader", createElementFactory("th", {scope:"row"})],
177+
["th scope=rowgroup", "rowheader", createElementFactory("th", {scope:"rowgroup"})],
172178
["tr", "row", createElementFactory("tr", {})],
173179
["track", null, createElementFactory("track", {})],
174180
["ul", "list", createElementFactory("ul", {})],
@@ -182,7 +188,7 @@ const cases = [
182188
["presentational <div /> with prohibited aria attributes", null, createElementFactory("div", {'aria-label': "hello", role: "none"})],
183189
];
184190

185-
it.each(cases)("%s has the role %s", (name, role, elementFactory) => {
191+
it.each(cases)("%s has the role %s", (_name, role, elementFactory) => {
186192
const element = elementFactory();
187193

188194
expect(getRole(element)).toEqual(role);

sources/getRole.ts

+14-3
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ const localNameToRoleMappings: Record<string, string | undefined> = {
5959
tfoot: "rowgroup",
6060
// WARNING: Only in certain context
6161
td: "cell",
62-
th: "columnheader",
6362
thead: "rowgroup",
6463
tr: "row",
6564
ul: "list",
@@ -143,12 +142,13 @@ export default function getRole(element: Element): string | null {
143142
}
144143

145144
function getImplicitRole(element: Element): string | null {
146-
const mappedByTag = localNameToRoleMappings[getLocalName(element)];
145+
const elName = getLocalName(element);
146+
const mappedByTag = localNameToRoleMappings[elName];
147147
if (mappedByTag !== undefined) {
148148
return mappedByTag;
149149
}
150150

151-
switch (getLocalName(element)) {
151+
switch (elName) {
152152
case "a":
153153
case "area":
154154
case "link":
@@ -205,6 +205,17 @@ function getImplicitRole(element: Element): string | null {
205205
return "listbox";
206206
}
207207
return "combobox";
208+
case "th":
209+
switch (element.getAttribute("scope")) {
210+
case "row":
211+
case "rowgroup":
212+
return "rowheader";
213+
case "col":
214+
case "colgroup":
215+
case "auto":
216+
default:
217+
return "columnheader";
218+
}
208219
}
209220
return null;
210221
}

0 commit comments

Comments
 (0)