Skip to content
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(a11y): Fix some audit false positives #9483

Merged
merged 6 commits into from
Dec 20, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/thirty-panthers-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Fix some false positive in the audit logic of the dev toolbar
30 changes: 20 additions & 10 deletions packages/astro/src/runtime/client/dev-overlay/plugins/audit/a11y.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const a11y_required_attributes = {

const interactiveElements = ['button', 'details', 'embed', 'iframe', 'label', 'select', 'textarea'];

const labellableElements = ['input', 'meter', 'output', 'progress', 'select', 'textarea'];

const aria_non_interactive_roles = [
'alert',
'alertdialog',
Expand Down Expand Up @@ -217,7 +219,7 @@ export const a11y: AuditRuleWithSelector[] = [
code: 'a11y-aria-activedescendant-has-tabindex',
title: 'Elements with attribute `aria-activedescendant` must be tabbable',
message:
'This element must either have an inherent `tabindex` or declare `tabindex` as an attribute.',
'Element with the `aria-activedescendant` attribute must either have an inherent `tabindex` or declare `tabindex` as an attribute.',
selector: '[aria-activedescendant]',
match(element) {
if (!(element as HTMLElement).tabIndex && !element.hasAttribute('tabindex')) return true;
Expand Down Expand Up @@ -280,14 +282,20 @@ export const a11y: AuditRuleWithSelector[] = [
selector: 'a[href]:is([href=""], [href="#"], [href^="javascript:" i])',
},
{
code: 'a11y-label-has-associated-control',
title: '`label` tag should have an associated control and a text content.',
code: 'a11y-invalid-label',
title: '`label` element should have an associated control and a text content.',
message:
'The `label` tag must be associated with a control using either `for` or having a nested input. Additionally, the `label` tag must have text content.',
selector: 'label:not([for])',
match(element) {
const inputChild = element.querySelector('input');
if (!inputChild?.textContent) return true;
'The `label` element must be associated with a control either by using the `for` attribute or by containing a nested form element. Additionally, the `label` element must have text content.',
selector: 'label',
match(element: HTMLLabelElement) {
// Label must be associated with a control, either using `for` or having a nested valid element
const hasFor = element.hasAttribute('for');
Copy link
Member

Choose a reason for hiding this comment

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

Should we also be checking that the for attribute element actually exists? Or does another audit take care of that?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not sure I understand what you mean. The line you highlighted checks for for?

Copy link
Member Author

@Princesseuh Princesseuh Dec 20, 2023

Choose a reason for hiding this comment

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

Oh, I just got it, that the element the for is for exists, that's neat. We could do another rule for that, this rule is already a bit overloaded! I'll add it to the backlog.

const nestedLabellableElement = element.querySelector(`${labellableElements.join(', ')}`);
if (!hasFor && !nestedLabellableElement) return true;

// Label must have text content, using innerText to ignore hidden text
const innerText = element.innerText.trim();
if (innerText === '') return true;
},
},
{
Expand Down Expand Up @@ -347,8 +355,10 @@ export const a11y: AuditRuleWithSelector[] = [
title: 'Missing content on element important for accessibility',
message: 'Headings and anchors must have content to be accessible.',
selector: a11y_required_content.join(','),
match(element) {
if (!element.textContent) return true;
match(element: HTMLElement) {
// innerText is used to ignore hidden text
const innerText = element.innerText.trim();
if (innerText === '') return true;
},
},
{
Expand Down
Loading