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

Fixes false positives in the dev overlay audit when multiple role values exist. #9857

Merged
merged 4 commits into from
Feb 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eight-flowers-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Fixes false positives in the dev overlay audit when multiple `role` values exist.
44 changes: 27 additions & 17 deletions packages/astro/src/runtime/client/dev-toolbar/apps/audit/a11y.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import { aria, roles } from 'aria-query';
import { AXObjectRoles, elementAXObjects } from 'axobject-query';
import type { AuditRuleWithSelector } from './index.js';

const WHITESPACE_REGEX = /\s+/;

const a11y_required_attributes = {
a: ['href'],
area: ['alt', 'aria-label', 'aria-labelledby'],
Expand Down Expand Up @@ -496,13 +498,17 @@ export const a11y: AuditRuleWithSelector[] = [
if (is_semantic_role_element(role, element.localName, getAttributeObject(element))) {
return;
}
const { requiredProps } = roles.get(role)!;
const required_role_props = Object.keys(requiredProps);
const missingProps = required_role_props.filter((prop) => !element.hasAttribute(prop));
if (missingProps.length > 0) {
(element as any).__astro_role = role;
(element as any).__astro_missing_attributes = missingProps;
return true;

const elementRoles = role.split(WHITESPACE_REGEX) as ARIARoleDefinitionKey[];
for(const elementRole of elementRoles) {
const { requiredProps } = roles.get(elementRole)!;
iamyunsin marked this conversation as resolved.
Show resolved Hide resolved
const required_role_props = Object.keys(requiredProps);
const missingProps = required_role_props.filter((prop) => !element.hasAttribute(prop));
if (missingProps.length > 0) {
(element as any).__astro_role = elementRole;
(element as any).__astro_missing_attributes = missingProps;
return true;
}
}
},
},
Expand All @@ -522,16 +528,20 @@ export const a11y: AuditRuleWithSelector[] = [
match(element) {
const role = getRole(element);
if (!role) return false;
const { props } = roles.get(role)!;
const attributes = getAttributeObject(element);
const unsupportedAttributes = aria.keys().filter((attribute) => !(attribute in props));
const invalidAttributes: string[] = Object.keys(attributes).filter(
(key) => key.startsWith('aria-') && unsupportedAttributes.includes(key as any)
);
if (invalidAttributes.length > 0) {
(element as any).__astro_role = role;
(element as any).__astro_unsupported_attributes = invalidAttributes;
return true;

const elementRoles = role.split(WHITESPACE_REGEX) as ARIARoleDefinitionKey[];
for(const elementRole of elementRoles) {
const { props } = roles.get(elementRole)!;
const attributes = getAttributeObject(element);
const unsupportedAttributes = aria.keys().filter((attribute) => !(attribute in props));
const invalidAttributes: string[] = Object.keys(attributes).filter(
(key) => key.startsWith('aria-') && unsupportedAttributes.includes(key as any)
);
if (invalidAttributes.length > 0) {
(element as any).__astro_role = elementRole;
(element as any).__astro_unsupported_attributes = invalidAttributes;
return true;
}
}
},
},
Expand Down
Loading