Skip to content

Commit a188a5e

Browse files
committed
.
1 parent 9da119c commit a188a5e

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

tests/bdd/support/roles.ts

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,38 @@ import { roles as ariaRolesMap } from "aria-query";
44
// Set of all valid ARIA roles sourced from aria-query (for diagnostics)
55
export const validAriaRoles = new Set<string>([...ariaRolesMap.keys()]);
66

7-
// Canonical phrases allowed in features → mapped to ARIA role
8-
// Keep exactly one human-facing variant per role to minimize duplication
9-
export const allowedRolePhrases: Record<string, AriaRole> = {
10-
// common interactive roles
11-
button: "button",
12-
link: "link",
13-
checkbox: "checkbox",
7+
// Customized human-friendly phrases → ARIA roles.
8+
// Only add entries here when the preferred phrase differs from the raw ARIA role name.
9+
const customizedRoleNames: Record<string, AriaRole> = {
10+
// Prefer "menu item" over the raw ARIA role string "menuitem"
1411
"menu item": "menuitem",
15-
"radio button": "radio",
16-
combobox: "combobox",
17-
listbox: "listbox",
18-
// useful extras (read-only)
19-
heading: "heading",
20-
textbox: "textbox",
2112
};
2213

14+
// Build the canonical mapping of phrases allowed in features → ARIA roles.
15+
// - Start from customized phrases
16+
// - Then add every remaining ARIA role mapping to itself (phrase === role)
17+
// - Do NOT add a self-mapping for roles already covered by a custom phrase,
18+
// so there is exactly one canonical phrase per role.
19+
const buildAllowedRolePhrases = (): Record<string, AriaRole> => {
20+
const mapping: Record<string, AriaRole> = {};
21+
22+
// Seed with custom phrases (lowercased keys)
23+
for (const [phrase, role] of Object.entries(customizedRoleNames)) {
24+
mapping[phrase.trim().toLowerCase()] = role;
25+
}
26+
27+
const customizedRoles = new Set(Object.values(customizedRoleNames));
28+
for (const roleName of ariaRolesMap.keys()) {
29+
const role = String(roleName) as AriaRole;
30+
if (!customizedRoles.has(role)) {
31+
mapping[role] = role; // default: phrase equals role name
32+
}
33+
}
34+
return mapping;
35+
};
36+
37+
export const allowedRolePhrases = buildAllowedRolePhrases();
38+
2339
// Inverse lookup for recommendations (ARIA role → preferred phrase)
2440
export const preferredPhraseByRole: Record<AriaRole, string> = Object.entries(
2541
allowedRolePhrases,

0 commit comments

Comments
 (0)