Skip to content

Commit 2add312

Browse files
committed
feat(ByRole): Treat inert subtrees as hidden in role queries
https://html.spec.whatwg.org/multipage/interaction.html#inert-subtrees: Inert nodes generally cannot be focused, and user agents do not expose the inert nodes to accessibility APIs or assistive technologies. Since jsdom does not yet fully support the `inert` attribute [1], we use `element.hasAttribute('inert')` instead of `element.inert`. fixes #1364 [1] jsdom/jsdom#3605
1 parent 225a3e4 commit 2add312

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

src/__tests__/role-helpers.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ test.each([
205205
['<div style="display: none;"/>', true],
206206
['<div style="visibility: hidden;"/>', true],
207207
['<div aria-hidden="true" />', true],
208+
['<div inert />', true],
208209
])('shouldExcludeFromA11yTree for %s returns %p', (html, expected) => {
209210
const {container} = render(html)
210211
container.firstChild.appendChild(document.createElement('button'))

src/__tests__/role.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,25 @@ test('by default excludes elements which have aria-hidden="true" or any of their
175175
`)
176176
})
177177

178+
test('by default excludes elements which have inert attribute or any of their parents', () => {
179+
const {getByRole} = render('<div inert><ul /></div>')
180+
181+
expect(() => getByRole('list')).toThrowErrorMatchingInlineSnapshot(`
182+
Unable to find an accessible element with the role "list"
183+
184+
There are no accessible roles. But there might be some inaccessible roles. If you wish to access them, then set the \`hidden\` option to \`true\`. Learn more about this here: https://testing-library.com/docs/dom-testing-library/api-queries#byrole
185+
186+
Ignored nodes: comments, script, style
187+
<div>
188+
<div
189+
inert=""
190+
>
191+
<ul />
192+
</div>
193+
</div>
194+
`)
195+
})
196+
178197
test('considers the computed visibility style not the parent', () => {
179198
// this behavior deviates from the spec which includes "any descendant"
180199
// if visibility is hidden. However, chrome a11y tree and nvda will include

src/role-helpers.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ function isSubtreeInaccessible(element) {
2121
return true
2222
}
2323

24+
if (element.hasAttribute('inert')) {
25+
return true
26+
}
27+
2428
const window = element.ownerDocument.defaultView
2529
if (window.getComputedStyle(element).display === 'none') {
2630
return true

0 commit comments

Comments
 (0)