Skip to content

Commit 3a4d5ec

Browse files
OliverSpeirematipicosarah11918
authoredJan 17, 2024
Update a11y.ts (#9567)
* improve error message and allow for aria-label, aria-labelledby, img alt and svg title * add checks for input * Update packages/astro/src/runtime/client/dev-overlay/plugins/audit/a11y.ts Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> * Update .changeset/orange-trainers-learn.md Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> --------- Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
1 parent 1539e04 commit 3a4d5ec

File tree

2 files changed

+67
-3
lines changed

2 files changed

+67
-3
lines changed
 

‎.changeset/orange-trainers-learn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": minor
3+
---
4+
5+
Improves the a11y-missing-content rule and error message for audit feature of dev-overlay. This also fixes an error where this check was falsely reporting accessibility errors.

‎packages/astro/src/runtime/client/dev-toolbar/apps/audit/a11y.ts

+62-3
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,72 @@ export const a11y: AuditRuleWithSelector[] = [
358358
},
359359
{
360360
code: 'a11y-missing-content',
361-
title: 'Missing content on element important for accessibility',
362-
message: 'Headings and anchors must have content to be accessible.',
361+
title: 'Missing content',
362+
message:
363+
'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>.',
363364
selector: a11y_required_content.join(','),
364365
match(element: HTMLElement) {
365366
// innerText is used to ignore hidden text
366367
const innerText = element.innerText.trim();
367-
if (innerText === '') return true;
368+
if (innerText !== '') return false;
369+
370+
// Check for aria-label
371+
const ariaLabel = element.getAttribute('aria-label')?.trim();
372+
if (ariaLabel && ariaLabel !== '') return false;
373+
374+
// Check for valid aria-labelledby
375+
const ariaLabelledby = element.getAttribute('aria-labelledby')?.trim();
376+
if (ariaLabelledby) {
377+
const ids = ariaLabelledby.split(' ');
378+
for (const id of ids) {
379+
const referencedElement = document.getElementById(id);
380+
if (referencedElement && referencedElement.innerText.trim() !== '') return false;
381+
}
382+
}
383+
384+
// Check for <img> with valid alt attribute
385+
const imgElements = element.querySelectorAll('img');
386+
for (const img of imgElements) {
387+
const altAttribute = img.getAttribute('alt');
388+
if (altAttribute && altAttribute.trim() !== '') return false;
389+
}
390+
391+
// Check for <svg> with valid title
392+
const svgElements = element.querySelectorAll('svg');
393+
for (const svg of svgElements) {
394+
const titleText = svg.querySelector('title');
395+
if (titleText && titleText.textContent && titleText.textContent.trim() !== '') return false;
396+
}
397+
398+
const inputElements = element.querySelectorAll('input');
399+
for (const input of inputElements) {
400+
// Check for alt attribute if input type is image
401+
if (input.type === 'image') {
402+
const altAttribute = input.getAttribute('alt');
403+
if (altAttribute && altAttribute.trim() !== '') return false;
404+
}
405+
406+
// Check for aria-label
407+
const inputAriaLabel = input.getAttribute('aria-label')?.trim();
408+
if (inputAriaLabel && inputAriaLabel !== '') return false;
409+
410+
// Check for aria-labelledby
411+
const inputAriaLabelledby = input.getAttribute('aria-labelledby')?.trim();
412+
if (inputAriaLabelledby) {
413+
const ids = inputAriaLabelledby.split(' ');
414+
for (const id of ids) {
415+
const referencedElement = document.getElementById(id);
416+
if (referencedElement && referencedElement.innerText.trim() !== '') return false;
417+
}
418+
}
419+
420+
// Check for title
421+
const title = input.getAttribute('title')?.trim();
422+
if (title && title !== '') return false;
423+
}
424+
425+
// If all checks fail, return true indicating missing content
426+
return true;
368427
},
369428
},
370429
{

0 commit comments

Comments
 (0)
Please sign in to comment.