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

Update a11y.ts #9567

Merged
merged 5 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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/orange-trainers-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": minor
---

Improve a11y-missing-content rule for audit feature of dev-overlay
OliverSpeir marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,72 @@ export const a11y: AuditRuleWithSelector[] = [
},
{
code: 'a11y-missing-content',
title: 'Missing content on element important for accessibility',
message: 'Headings and anchors must have content to be accessible.',
title: 'Missing content',
message:
'Headings and anchors must have an accessible name, which can come from: inner text, aria-label, aria-labelledby, an img with alt property, or an svg with a tag <title></title>.',
Copy link
Member

Choose a reason for hiding this comment

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

Just checking because I don't think these error messages currently get pulled into docs, so this should be fine. But for docs, we would be using back ticks for inline code formatting on all values, but especially any components like <title> or else docs will break.

@Princesseuh - tangentially, do we want these dev tool error messages available in docs somewhere? Eventually? We don't really have any "using dev toolbar" docs at all. Is this something we want/care about?

Copy link
Member

@Princesseuh Princesseuh Jan 12, 2024

Choose a reason for hiding this comment

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

Sorry, missed this message (blame GitHub notifications not working properly since a few weeks 😠). Yes, eventually I would like to have the audits in the docs, similar to the error reference.

It would most likely use JSDoc comments like the error reference do, so the messages would get sanitized etc. (or we'd duplicate every error in the docs, that's ok too)

selector: a11y_required_content.join(','),
match(element: HTMLElement) {
// innerText is used to ignore hidden text
const innerText = element.innerText.trim();
if (innerText === '') return true;
if (innerText !== '') return false;

// Check for aria-label
const ariaLabel = element.getAttribute('aria-label')?.trim();
if (ariaLabel && ariaLabel !== '') return false;

// Check for valid aria-labelledby
const ariaLabelledby = element.getAttribute('aria-labelledby')?.trim();
if (ariaLabelledby) {
const ids = ariaLabelledby.split(' ');
for (const id of ids) {
const referencedElement = document.getElementById(id);
if (referencedElement && referencedElement.innerText.trim() !== '') return false;
}
}

// Check for <img> with valid alt attribute
const imgElements = element.querySelectorAll('img');
OliverSpeir marked this conversation as resolved.
Show resolved Hide resolved
for (const img of imgElements) {
const altAttribute = img.getAttribute('alt');
if (altAttribute && altAttribute.trim() !== '') return false;
}

// Check for <svg> with valid title
const svgElements = element.querySelectorAll('svg');
OliverSpeir marked this conversation as resolved.
Show resolved Hide resolved
for (const svg of svgElements) {
const titleText = svg.querySelector('title');
if (titleText && titleText.textContent && titleText.textContent.trim() !== '') return false;
}

const inputElements = element.querySelectorAll('input');
OliverSpeir marked this conversation as resolved.
Show resolved Hide resolved
for (const input of inputElements) {
OliverSpeir marked this conversation as resolved.
Show resolved Hide resolved
// Check for alt attribute if input type is image
if (input.type === 'image') {
const altAttribute = input.getAttribute('alt');
if (altAttribute && altAttribute.trim() !== '') return false;
}

// Check for aria-label
const inputAriaLabel = input.getAttribute('aria-label')?.trim();
if (inputAriaLabel && inputAriaLabel !== '') return false;

// Check for aria-labelledby
const inputAriaLabelledby = input.getAttribute('aria-labelledby')?.trim();
if (inputAriaLabelledby) {
const ids = inputAriaLabelledby.split(' ');
for (const id of ids) {
const referencedElement = document.getElementById(id);
if (referencedElement && referencedElement.innerText.trim() !== '') return false;
}
}

// Check for title
const title = input.getAttribute('title')?.trim();
if (title && title !== '') return false;
}

// If all checks fail, return true indicating missing content
return true;
},
},
{
Expand Down
Loading