ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/design-a11y/checks/alt.ts

checks/alt.ts

21 líneas, 997 B.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // A11Y-R02. Every <img> in HTML, TSX and JSX has an alt attribute (alt="" for a decorative picture).
  2. // Skipped: an <img> with {...props} (alt may come from outside) and one hidden from assistive tech (aria-hidden="true").
  3. import { elements, attr } from '../../design-base/lib/markup.ts';
  4. import { lineAt, report } from '../../design-base/lib/files.ts';
  5. import { markupFiles } from './a11ylib.ts';
  6. export const RULE = { id: 'a11y-r02', title: 'An image has alt' };
  7. export function check(root = process.cwd()): string[] {
  8. const out: string[] = [];
  9. for (const f of markupFiles(root)) {
  10. for (const el of elements(f.text, /img/)) {
  11. if (attr(el, 'alt') !== null || el.spread) continue;
  12. if (attr(el, 'aria-hidden') === 'true' || attr(el, 'role') === 'presentation') continue;
  13. out.push(`${f.rel}:${lineAt(f.text, el.start)}: <img> without alt — describe the picture in words or use alt="" for decoration`);
  14. }
  15. }
  16. return out;
  17. }
  18. if (import.meta.main) report(check());