-
Notifications
You must be signed in to change notification settings - Fork 470
fix: use the first label for LabelText query suggestions #672
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
fix: use the first label for LabelText query suggestions #672
Conversation
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 70a3048:
|
Codecov Report
@@ Coverage Diff @@
## master #672 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 24 24
Lines 595 596 +1
Branches 148 148
=========================================
+ Hits 595 596 +1
Continue to review full report at Codecov.
|
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.
But the html spec doesn't accept spaces in id attributes. The compound labels, are meant to do something different.
That would mean that a fix to silence the error, should just only use the first id (which are separated by spaces).
const firstLabelId = ariaLabelledBy.split(' ')[0];
document.querySelector(`[id="${firstLabelId}"]`)
yes about spec you're right. I don't know which solution is better then the other, both solution are not correct because of #545 |
True, both fixes are just hacks to stop the query from throwing errors, while making suggestions. The difference is that by wrapping the query in quotes, the query won't find a matching element, and thereby won't make a suggestion. It feels wrong to me to build a selector that we know is incorrect and won't return any elements. By taking only the first id, the query still finds a matching element and will make a valid suggestion. It won't give the best suggestion possible, but at least it will suggest something close that does return an element. The best fix would be to contact the labels, but as I understand, that isn't currently supported? If the partial match doesn't result in a valid query either (I haven't tested it), I propose to just return const ariaLabelledBy = element.getAttribute('aria-labelledby')
if (ariaLabelledBy) {
// compound labels are currently unsupported, fall back to different query options
label = ariaLabelledBy.includes(' ') ? undefined : document.querySelector(`[id="${ariaLabelledBy}"]`)
} concat labelsfunction getLabelTextFor(element) {
let label =
element.labels &&
Array.from(element.labels).find(el => Boolean(normalize(el.textContent)))
if (label) {
return label;
}
// non form elements that are using aria-labelledby won't be included in `element.labels`
const ariaLabelledBy = element.getAttribute('aria-labelledby');
if (ariaLabelledBy) {
const selector = ariaLabelledBy.split(' ').map(id => `[id="${id}"]`).join(', ');
const labels = document.querySelectorAll(selector);
return Array.from(labels).map(label => label.textContent).join(' ')
}
return undefined
} |
Yes, I see. So my proposal, to just only take the first label and use that instead. Sounds like a good fix to me. Let's take the markup: <div id="a">One</div>
<div id="b">Two</div>
<input
type="text"
aria-labelledby="a b"
/> The full label is I see 3 options:
As 1 isn't currently possible, that's not a real option. So only 2 and 3 are left. |
Yes I can implement the third option. |
08383be
to
70a3048
Compare
🎉 This PR is included in version 7.18.1 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
What: Fix a little bug
Why: As reported here when
getSuggestedQuery
tried to get label text througharia-labelledby
it threw an exception when the id had spaces or special charsHow: Surrounding the selector with
" "
Checklist:
docs site