Skip to content

feat: ignore element that is children of aria-hidden="true" #944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ test('query can return null', () => {
expect(queryByAltText('LucyRicardo')).toBeNull()
})

test('query by text outside `aria-hidden` tags', () => {
const {queryByText} = render('<div><div>LucyRicardo</div></div>')
expect(queryByText('LucyRicardo')).not.toBeNull()
const {queryByText: queryByTextRoot} = render(
'<div aria-hidden="false">LucyRicardo</div>',
)
expect(queryByTextRoot('LucyRicardo')).not.toBeNull()
})

test('query by text inside `aria-hidden` tags', () => {
const {queryByText} = render(
'<div aria-hidden="true"><div>LucyRicardo</div></div>',
)
expect(queryByText('LucyRicardo')).toBeNull()
const {queryByText: queryByTextRoot} = render(
'<div aria-hidden="true">LucyRicardo</div>',
)
expect(queryByTextRoot('LucyRicardo')).toBeNull()
})

test('get throws a useful error message', () => {
const {
getByLabelText,
Expand Down
30 changes: 29 additions & 1 deletion src/queries/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,31 @@ import {
buildQueries,
} from './all-utils'

function ignoreHiddenElements(container, selector, baseArray) {
const hiddenElements = [
...Array.from(container.querySelectorAll("[aria-hidden='true']")),
]
baseArray.forEach(base => {
if (base.getAttribute('aria-hidden') === 'true') {
hiddenElements.push(base)
}
})
let foundHiddenChildren = [...hiddenElements]
hiddenElements.forEach(hiddenElement => {
foundHiddenChildren = [
...foundHiddenChildren,
...Array.from(hiddenElement.querySelectorAll(selector)),
]
})
const findSameNode = foundElement =>
!foundHiddenChildren.find(foundHiddenElement =>
foundElement.isSameNode(foundHiddenElement),
)
Comment on lines +28 to +31
Copy link

Choose a reason for hiding this comment

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

.find returns the first element from an array that matches the provided callback. In this case you don't particularly care what the element is, just whether it can be found or not. That behaviour is better provided by .some which just returns a boolean saying whether the given callback could be matched anywhere in the array:

Suggested change
const findSameNode = foundElement =>
!foundHiddenChildren.find(foundHiddenElement =>
foundElement.isSameNode(foundHiddenElement),
)
const findSameNode = foundElement =>
!foundHiddenChildren.some(foundHiddenElement =>
foundElement.isSameNode(foundHiddenElement),
)

Additionally the naming of findSameNode doesn't quite feel right to me... I think I would call this something more like "isElementAccessible"

return [...baseArray, ...container.querySelectorAll(selector)].filter(
findSameNode,
)
}

function queryAllByText(
container,
text,
Expand All @@ -28,7 +53,10 @@ function queryAllByText(
if (typeof container.matches === 'function' && container.matches(selector)) {
baseArray = [container]
}
return [...baseArray, ...Array.from(container.querySelectorAll(selector))]

const foundElements = ignoreHiddenElements(container, selector, baseArray)

return foundElements
.filter(node => !ignore || !node.matches(ignore))
.filter(node => matcher(getNodeText(node), node, text, matchNormalizer))
}
Expand Down