Skip to content

Conversation

tf
Copy link

@tf tf commented Sep 23, 2025

What:

Exclude inert elements in role queries by default.

Why:

Align behavior with what is described here 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.

How:

Check presence of inert attribute. Since jsdom does not yet fully support the inert attribute, we use element.hasAttribute('inert') instead of element.inert.

Checklist:

fixes #1364

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 testing-library#1364

[1] jsdom/jsdom#3605
tf added a commit to tf/testing-library-docs that referenced this pull request Sep 23, 2025
Copy link

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 2add312:

Sandbox Source
react-testing-library-examples Configuration

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a bit more reading and seems like it's not safe to bail out on the subtree since nested dialog can escape inertness:

Modal s generated with showModal() escape inertness, meaning that they don't inherit inertness from their ancestors, but can only be made inert by having the inert attribute explicitly set on themselves.
-- https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/inert

We need a bit more involved tests where you have <div inert><dialog /></div> and have showModal() called on the inner <dialog>. That dialog and all of its content needs to be considered accessible.

But then also even more involved tests where you nest two dialogs that have both been opened and only the parent dialog is explicitly inert.

@tf
Copy link
Author

tf commented Sep 23, 2025

In this case, this effort is probably blocked on JSDOM's missing dialog support. Far as I understand, by default, showModal() turns everything outside of the top layer inert without actually applying the inert attribute to any element. So while it is true, that showModal can be used to escape inertness, I'd argue that the 90% case is either using the inert attribute explicitly without showModal or relying on the implicit inertness resulting from showModal. Both of those cases would be compatible with the proposed change.

One other option to make this safe might be to introduce a config option to explicitly opt in to treating inert subtrees as hidden. But this also adds complexity of course.

@eps1lon
Copy link
Member

eps1lon commented Sep 23, 2025

This library isn't just for JSDOM but any DOM implementation so it must be implemented for the DOM not JSDOM.

@tf
Copy link
Author

tf commented Sep 23, 2025

Makes sense. I'll close this PR then.

@tf tf closed this Sep 23, 2025
@tf tf deleted the exclude-inert branch September 23, 2025 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Elements with an inert="" attribute aren't being hidden from the accessibility tree
2 participants