Skip to content

Commit 04b027c

Browse files
authored
fix: Consider explicit role when pretty printing roles (#568)
Closes #553
1 parent 217b2a8 commit 04b027c

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/__tests__/role.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,35 @@ Here are the accessible roles:
343343
`)
344344
})
345345

346+
test('explicit role is most specific', () => {
347+
const {getByRole} = renderIntoDocument(
348+
`<button role="tab" aria-label="my-tab" />`,
349+
)
350+
351+
expect(() => getByRole('button')).toThrowErrorMatchingInlineSnapshot(`
352+
"Unable to find an accessible element with the role "button"
353+
354+
Here are the accessible roles:
355+
356+
tab:
357+
358+
Name "my-tab":
359+
<button
360+
aria-label="my-tab"
361+
role="tab"
362+
/>
363+
364+
--------------------------------------------------
365+
366+
<body>
367+
<button
368+
aria-label="my-tab"
369+
role="tab"
370+
/>
371+
</body>"
372+
`)
373+
})
374+
346375
describe('configuration', () => {
347376
let originalConfig
348377
beforeEach(() => {

src/role-helpers.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,13 @@ function getRoles(container, {hidden = false} = {}) {
134134
return hidden === false ? isInaccessible(element) === false : true
135135
})
136136
.reduce((acc, node) => {
137-
const roles = getImplicitAriaRoles(node)
137+
let roles = []
138+
// TODO: This violates html-aria which does not allow any role on every element
139+
if (node.hasAttribute('role')) {
140+
roles = node.getAttribute('role').split(' ').slice(0, 1)
141+
} else {
142+
roles = getImplicitAriaRoles(node)
143+
}
138144

139145
return roles.reduce(
140146
(rolesAcc, role) =>

0 commit comments

Comments
 (0)