ShtukbaseStandard
package file→ .shtukbase/packages/design-a11y/checks/lang.ts

checks/lang.ts

25 lines, 1.1 KB.

  1. // A11Y-R06. The page says its language: every <html> (in HTML files, and in TSX/JSX layouts such as Next.js
  2. // app/layout.tsx) has a non-empty lang. An HTML page without an <html> tag at all is a page too: it lacks lang.
  3. import { attr, elements } 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-r06', title: 'The page has a language' };
  7. export function check(root = process.cwd()): string[] {
  8. const out: string[] = [];
  9. for (const f of markupFiles(root)) {
  10. const html = elements(f.text, /html/);
  11. if (!html.length) {
  12. if (f.html && /<body[\s>]/i.test(f.text)) out.push(`${f.rel}:1: no <html lang="…"> — set the page language`);
  13. continue;
  14. }
  15. for (const el of html) {
  16. const lang = attr(el, 'lang');
  17. if (lang === null || lang.trim() === '') out.push(`${f.rel}:${lineAt(f.text, el.start)}: <html> without lang — set the page language (lang="en")`);
  18. }
  19. }
  20. return out;
  21. }
  22. if (import.meta.main) report(check());