файл пакета→ .shtukbase/packages/shtukbase-style/checks/background.ts
checks/background.ts
37 строк, 2.3 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // SBS-R01. The document is white: the background of `html`, `body` and `:root` in the light theme is white or the
- // token --background; no grey paper, dots, grid or gradient behind the text of a page. Patterns belong to canvases
- // and areas (.sb-canvas, .sb-bg-*), not to the document. The dark theme (a selector with `dark`, or
- // prefers-color-scheme: dark) is not looked at: there the ground is graphite.
- import { report } from '../../design-base/lib/files.ts';
- import { type Unit, allUnits } from '../lib/scan.ts';
- export const RULE = { id: 'sbs-r01', title: 'The document is white' };
- const DOCUMENT = /^(html|body|html\s+body|:root)$/;
- 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;
- /** The colour part of a `background` value: without images and positions it is the value itself. */
- const isImage = (v: string) => /gradient\(|url\(/i.test(v);
- export function checkUnits(units: Unit[]): string[] {
- const out: string[] = [];
- for (const u of units) {
- const dark = u.at.some((a) => /prefers-color-scheme\s*:\s*dark/.test(a)) || u.selectors.some((s) => /dark/i.test(s));
- const doc = u.tag === 'body' || u.tag === 'html' || u.selectors.some((s) => DOCUMENT.test(s.trim().replace(/\s+/g, ' ')));
- if (dark || !doc) continue;
- for (const d of u.decls) {
- if (d.prop !== 'background' && d.prop !== 'background-color' && d.prop !== 'background-image') continue;
- if (u.allowed(d.line)) continue;
- const v = d.value.trim().replace(/\s+/g, ' ');
- 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; }
- 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`);
- else if (!WHITE.test(v)) out.push(`${u.file}:${d.line}: the document background is ${v}, not white — use var(--background)`);
- }
- }
- return out;
- }
- export function check(): string[] {
- return checkUnits(allUnits());
- }
- if (import.meta.main) report(check());