archivo del paquete→ .shtukbase/packages/design-a11y/checks/names.ts
checks/names.ts
34 líneas, 2.0 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // A11Y-R04. Every button and link has a name: text inside (visible or sr-only), aria-label, aria-labelledby or title.
- // An icon alone is not a name. Looked at: <button>, <a>, and the components Button and Link.
- // Skipped: elements with {...props} (the name may come from outside) and inner text built by an expression ({…}).
- import { attr, elements, innerText } from '../../design-base/lib/markup.ts';
- import { lineAt, report } from '../../design-base/lib/files.ts';
- import { markupFiles } from './a11ylib.ts';
- export const RULE = { id: 'a11y-r04', title: 'A button and a link have a name' };
- export function check(root = process.cwd()): string[] {
- const out: string[] = [];
- for (const f of markupFiles(root)) {
- for (const el of elements(f.text, /button|a|Button|Link/)) {
- if (el.spread || el.inAttr) continue;
- if (['aria-label', 'aria-labelledby', 'title'].some((a) => { const v = attr(el, a); return v !== null && v.trim() !== ''; })) continue;
- if (el.name === 'a' && attr(el, 'href') === null && attr(el, 'role') === null) continue; // an anchor, not a link
- // A component that returns only an empty <Link />/<Button /> is a template for a render prop.
- if (el.selfClosing && /(=>|\breturn)\s*\(?\s*$/.test(f.text.slice(Math.max(0, el.start - 40), el.start))) continue;
- if (el.selfClosing) {
- out.push(`${f.rel}:${lineAt(f.text, el.start)}: <${el.name} /> without text and aria-label`);
- continue;
- }
- if (el.inner === null) continue;
- const text = innerText(el.inner);
- if (text === null || text) continue;
- if (/\balt\s*=\s*["'][^"']+["']|\baria-label\s*=\s*["'{]/.test(el.inner)) continue; // image with alt, labelled child
- const tag = el.name === 'a' || el.name === 'Link' ? 'link' : 'button';
- out.push(`${f.rel}:${lineAt(f.text, el.start)}: ${tag} <${el.name}> without a name (only an icon?) — add text, <span className="sr-only">…</span> or aria-label`);
- }
- }
- return out;
- }
- if (import.meta.main) report(check());