|
1 | 1 | import type { AriaRole } from "@playwright/test"; |
2 | 2 | import { roles as ariaRolesMap } from "aria-query"; |
3 | 3 |
|
4 | | -// Set of all valid ARIA roles sourced from aria-query (for diagnostics) |
5 | | -export const validAriaRoles = new Set<string>([...ariaRolesMap.keys()]); |
6 | | - |
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" |
| 4 | +// Custom phrases for ARIA roles (when the phrase differs from the role name) |
| 5 | +const customPhrases: Record<string, AriaRole> = { |
11 | 6 | "menu item": "menuitem", |
12 | 7 | }; |
13 | 8 |
|
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> => { |
| 9 | +function buildAllowedRolePhrases(): Record<string, AriaRole> { |
20 | 10 | const mapping: Record<string, AriaRole> = {}; |
21 | 11 |
|
22 | | - // Seed with custom phrases (lowercased keys) |
23 | | - for (const [phrase, role] of Object.entries(customizedRoleNames)) { |
| 12 | + for (const [phrase, role] of Object.entries(customPhrases)) { |
24 | 13 | mapping[phrase.trim().toLowerCase()] = role; |
25 | 14 | } |
26 | 15 |
|
27 | | - const customizedRoles = new Set(Object.values(customizedRoleNames)); |
| 16 | + const customRoles = new Set(Object.values(customPhrases)); |
28 | 17 | for (const roleName of ariaRolesMap.keys()) { |
29 | 18 | const role = String(roleName) as AriaRole; |
30 | | - if (!customizedRoles.has(role)) { |
31 | | - mapping[role] = role; // default: phrase equals role name |
| 19 | + if (!customRoles.has(role)) { |
| 20 | + mapping[role] = role; |
32 | 21 | } |
33 | 22 | } |
34 | 23 | return mapping; |
35 | | -}; |
| 24 | +} |
36 | 25 |
|
37 | 26 | export const allowedRolePhrases = buildAllowedRolePhrases(); |
38 | | - |
39 | | -// Inverse lookup for recommendations (ARIA role → preferred phrase) |
40 | | -// Note: We intentionally omit an inverse mapping (role -> preferred phrase) |
41 | | -// to keep this surface minimal. Add it here if you want suggestion messages. |
0 commit comments