ShtukbaseStandard
package file→ .shtukbase/packages/shtukbase-style/checks/background.ts

checks/background.ts

37 lines, 2.3 KB.

  1. // SBS-R01. The document is white: the background of `html`, `body` and `:root` in the light theme is white or the
  2. // token --background; no grey paper, dots, grid or gradient behind the text of a page. Patterns belong to canvases
  3. // and areas (.sb-canvas, .sb-bg-*), not to the document. The dark theme (a selector with `dark`, or
  4. // prefers-color-scheme: dark) is not looked at: there the ground is graphite.
  5. import { report } from '../../design-base/lib/files.ts';
  6. import { type Unit, allUnits } from '../lib/scan.ts';
  7. export const RULE = { id: 'sbs-r01', title: 'The document is white' };
  8. const DOCUMENT = /^(html|body|html\s+body|:root)$/;
  9. const WHITE = /^(#fff(?:fff)?|white|var\(--background(?:\s*,[^)]*)?\)|none|transparent|initial|inherit|unset|revert|rgb\(\s*255[\s,]+255[\s,]+255\s*\)|hsl\(\s*0[\s,]+0%[\s,]+100%\s*\))$/i;
  10. /** The colour part of a `background` value: without images and positions it is the value itself. */
  11. const isImage = (v: string) => /gradient\(|url\(/i.test(v);
  12. export function checkUnits(units: Unit[]): string[] {
  13. const out: string[] = [];
  14. for (const u of units) {
  15. const dark = u.at.some((a) => /prefers-color-scheme\s*:\s*dark/.test(a)) || u.selectors.some((s) => /dark/i.test(s));
  16. const doc = u.tag === 'body' || u.tag === 'html' || u.selectors.some((s) => DOCUMENT.test(s.trim().replace(/\s+/g, ' ')));
  17. if (dark || !doc) continue;
  18. for (const d of u.decls) {
  19. if (d.prop !== 'background' && d.prop !== 'background-color' && d.prop !== 'background-image') continue;
  20. if (u.allowed(d.line)) continue;
  21. const v = d.value.trim().replace(/\s+/g, ' ');
  22. if (d.prop === 'background-image') { if (!/^(none|initial|inherit|unset)$/i.test(v)) out.push(`${u.file}:${d.line}: the document has a background image (${v.slice(0, 60)}) — the document is white; patterns are for canvases and areas (.sb-canvas, .sb-bg-*)`); continue; }
  23. if (isImage(v)) out.push(`${u.file}:${d.line}: the document has a pattern or gradient background (${v.slice(0, 60)}) — the document is white; use .sb-canvas on a canvas element instead`);
  24. else if (!WHITE.test(v)) out.push(`${u.file}:${d.line}: the document background is ${v}, not white — use var(--background)`);
  25. }
  26. }
  27. return out;
  28. }
  29. export function check(): string[] {
  30. return checkUnits(allUnits());
  31. }
  32. if (import.meta.main) report(check());