ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/shtukbase-style/checks/card-ratio.ts

checks/card-ratio.ts

47 líneas, 2.5 KB.

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

  1. // SBS-R03. A card is at most 3:1: a card that is longer than that is a row of a list. The check reads CSS rules whose
  2. // selector names a card (a class or a data-slot containing "card") and style="…" of such elements; it sees
  3. // `aspect-ratio` and a fixed `width` with a fixed `height` in px, rem or em. A size with var(), %, calc() or auto
  4. // cannot be judged by text and is skipped (a person sees it in the catalog).
  5. import { report } from '../../design-base/lib/files.ts';
  6. import { type Unit, allUnits, lenPx } from '../lib/scan.ts';
  7. export const RULE = { id: 'sbs-r03', title: 'A card is at most 3:1' };
  8. export const MAX = 3;
  9. // A card itself, not its parts: `.card`, `.pkg-card`, `[data-slot=card]`, but not `.card-header`.
  10. const CARD = /(?:\.|#|\b)(?:[\w-]*-)?card(?![\w-])|\[data-slot[~|^$*]?=["']?(?:[\w-]*-)?card["'\]\s]/i;
  11. /** The last compound of a selector is the element the rule is about: `.card .icon` is an icon, not a card. */
  12. const subject = (s: string) => s.trim().split(/\s*[>+~]\s*|\s+/).pop() ?? '';
  13. const isCard = (u: Unit) =>
  14. u.selectors.some((s) => CARD.test(subject(s))) ||
  15. (u.tag !== undefined && (u.classes ?? []).some((c) => /(^|[-_])card$/i.test(c))) ||
  16. /(^|-)card$/.test(u.slot ?? '');
  17. function ratioOf(v: string): number | null {
  18. const m = /^(\d*\.?\d+)\s*(?:\/\s*(\d*\.?\d+))?$/.exec(v.trim().replace(/^auto\s+/, ''));
  19. if (!m) return null;
  20. const a = Number(m[1]), b = m[2] === undefined ? 1 : Number(m[2]);
  21. return a > 0 && b > 0 ? a / b : null;
  22. }
  23. export function checkUnits(units: Unit[]): string[] {
  24. const out: string[] = [];
  25. for (const u of units) {
  26. if (!isCard(u)) continue;
  27. const get = (p: string) => u.decls.find((d) => d.prop === p);
  28. const say = (line: number, r: number, how: string) => { if (!u.allowed(line)) out.push(`${u.file}:${line}: card ${how} is ${r >= 1 ? `${r.toFixed(1)}:1` : `1:${(1 / r).toFixed(1)}`} — a card is at most ${MAX}:1 (a longer one is a row of a list)`); };
  29. const ar = get('aspect-ratio');
  30. const r = ar ? ratioOf(ar.value) : null;
  31. if (ar && r !== null && (r > MAX || r < 1 / MAX)) say(ar.line, r, `aspect-ratio ${ar.value.trim()}`);
  32. const w = get('width'), h = get('height');
  33. const pw = w ? lenPx(w.value) : null, ph = h ? lenPx(h.value) : null;
  34. if (w && h && pw && ph && (pw / ph > MAX || ph / pw > MAX)) say(Math.min(w.line, h.line), pw / ph, `size ${w.value.trim()} × ${h.value.trim()}`);
  35. }
  36. return out;
  37. }
  38. export function check(): string[] {
  39. return checkUnits(allUnits());
  40. }
  41. if (import.meta.main) report(check());