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(toolbar): add anchor element to interactive elements #10343

Merged
merged 3 commits into from
Mar 7, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tough-moose-prove.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes some false positive in the dev toolbar a11y audits, by adding the `a` element to the list of interactive elements.
14 changes: 14 additions & 0 deletions packages/astro/e2e/dev-toolbar-audits.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,18 @@ test.describe('Dev Toolbar - Audits', () => {
// Toggle app off
await appButton.click();
});

test('does not warn for non-interactive element', async ({ page, astro }) => {
await page.goto(astro.resolveUrl('/a11y-exceptions'));

const toolbar = page.locator('astro-dev-toolbar');
const appButton = toolbar.locator('button[data-app-id="astro:audit"]');
await appButton.click();

const auditCanvas = toolbar.locator('astro-dev-toolbar-app-canvas[data-app-id="astro:audit"]');
const auditHighlights = auditCanvas.locator('astro-dev-toolbar-highlight');

const count = await auditHighlights.count();
expect(count).toEqual(0);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---

---


<input role="button" aria-label="Label"/>
33 changes: 32 additions & 1 deletion packages/astro/src/runtime/client/dev-toolbar/apps/audit/a11y.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,25 @@ const a11y_required_attributes = {
object: ['title', 'aria-label', 'aria-labelledby'],
};

const interactiveElements = ['button', 'details', 'embed', 'iframe', 'label', 'select', 'textarea'];
const MAYBE_INTERACTIVE = new Map([
['a', 'href'],
['input', 'type'],
['audio', 'controls'],
['img', 'usemap'],
['object', 'usemap'],
['video', 'controls'],
]);

const interactiveElements = [
'button',
'details',
'embed',
'iframe',
'label',
'select',
'textarea',
...MAYBE_INTERACTIVE.keys(),
];

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

Expand Down Expand Up @@ -187,6 +205,15 @@ const ariaRoles = new Set(
)
);

function isInteractive(element: Element): boolean {
const attribute = MAYBE_INTERACTIVE.get(element.localName);
if (attribute) {
return element.hasAttribute(attribute);
}

return true;
}

export const a11y: AuditRuleWithSelector[] = [
{
code: 'a11y-accesskey',
Expand Down Expand Up @@ -434,6 +461,7 @@ export const a11y: AuditRuleWithSelector[] = [
'Interactive HTML elements like `<a>` and `<button>` cannot use non-interactive roles like `heading`, `list`, `menu`, and `toolbar`.',
selector: `[role]:is(${interactiveElements.join(',')})`,
match(element) {
if (!isInteractive(element)) return false;
const role = element.getAttribute('role');
if (!role) return false;
if (!ariaRoles.has(role)) return false;
Expand All @@ -448,6 +476,7 @@ export const a11y: AuditRuleWithSelector[] = [
'Interactive roles should not be used to convert a non-interactive element to an interactive element',
selector: `[role]:not(${interactiveElements.join(',')})`,
match(element) {
if (!isInteractive(element)) return false;
const role = element.getAttribute('role');
if (!role) return false;
if (!ariaRoles.has(role)) return false;
Expand All @@ -472,6 +501,8 @@ export const a11y: AuditRuleWithSelector[] = [
element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth;
if (isScrollable) return false;

if (!isInteractive(element)) return false;

if (!interactiveElements.includes(element.localName)) return true;
},
},
Expand Down