-
Notifications
You must be signed in to change notification settings - Fork 472
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
Add more robust selector handling for getByLabelText #373
Changes from 6 commits
019a1c3
2a832cd
7b41148
581c451
696f309
d7a9c2a
ad90d4e
a7ad2fb
dc48a12
8eb8c9f
d5ca2f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,9 +40,10 @@ function queryAllByLabelText( | |
normalizer: matchNormalizer, | ||
}) | ||
const labelledElements = labels | ||
.map(label => { | ||
.reduce((matchedElements, label) => { | ||
const elementsForLabel = [] | ||
if (label.control) { | ||
return label.control | ||
elementsForLabel.push(label.control) | ||
} | ||
/* istanbul ignore if */ | ||
if (label.getAttribute('for')) { | ||
|
@@ -51,21 +52,25 @@ function queryAllByLabelText( | |
// <label for="someId">text</label><input id="someId" /> | ||
|
||
// .control support has landed in jsdom (https://github.com/jsdom/jsdom/issues/2175) | ||
return container.querySelector(`[id="${label.getAttribute('for')}"]`) | ||
elementsForLabel.push( | ||
container.querySelector(`[id="${label.getAttribute('for')}"]`), | ||
) | ||
} | ||
if (label.getAttribute('id')) { | ||
// <label id="someId">text</label><input aria-labelledby="someId" /> | ||
return container.querySelector( | ||
`[aria-labelledby~="${label.getAttribute('id')}"]`, | ||
) | ||
container | ||
.querySelectorAll(`[aria-labelledby~="${label.getAttribute('id')}"]`) | ||
.forEach(element => elementsForLabel.push(element)) | ||
} | ||
if (label.childNodes.length) { | ||
// <label>text: <input /></label> | ||
return label.querySelector(selector) | ||
label | ||
.querySelectorAll('input, textarea') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is insufficient. We need to select every form control element type. Unfortunately there's not a common class that all form control elements inherit from, but I think we'd be safe if we found every element that supports the I'm not sure of the best way to do this, but here's a list of all the element types: https://developer.mozilla.org/en-US/docs/Web/API (Everything named something like HTML*Element). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll look into that and see if I can find all of them. 👍 |
||
.forEach(element => elementsForLabel.push(element)) | ||
} | ||
return null | ||
}) | ||
.filter(label => label !== null) | ||
return matchedElements.concat(elementsForLabel) | ||
}, []) | ||
.filter(element => element !== null) | ||
.concat(queryAllByAttribute('aria-label', container, text, {exact})) | ||
|
||
const possibleAriaLabelElements = queryAllByText(container, text, { | ||
|
@@ -89,7 +94,9 @@ function queryAllByLabelText( | |
[], | ||
) | ||
|
||
return Array.from(new Set([...labelledElements, ...ariaLabelledElements])) | ||
return Array.from( | ||
new Set([...labelledElements, ...ariaLabelledElements]), | ||
).filter(element => element.matches(selector)) | ||
} | ||
|
||
// the getAll* query would normally look like this: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add another test for the situation I found myself in where we had two elements that had the same label text?
Thanks!