ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/shtukbase-style/lib/scan.ts

lib/scan.ts

74 líneas, 3.4 KB.

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

  1. // What the style checks look at: rule blocks of the project's CSS files and <style> blocks of HTML files, and style="…"
  2. // attributes of HTML elements. Which files: the settings of design-base (page /design/design-checks.md). A line
  3. // marked `shtukbase-style: allow` is skipped. The working directory is the project root.
  4. import { parseCss, splitTop, stripComments, type Decl } from '../../design-base/lib/css.ts';
  5. import { projectFiles, read, scope, type Scope } from '../../design-base/lib/files.ts';
  6. import { attr, blankComments, elements } from '../../design-base/lib/markup.ts';
  7. export const ALLOW = 'shtukbase-style: allow';
  8. /** One place with declarations: a CSS rule (selectors set), or the style attribute of an element (tag set). */
  9. export type Unit = {
  10. file: string;
  11. line: number;
  12. selectors: string[];
  13. at: string[];
  14. decls: Decl[];
  15. tag?: string;
  16. classes?: string[];
  17. slot?: string;
  18. allowed: (line: number) => boolean;
  19. };
  20. /** Length in px: `120px`, `7.5rem`, `10em` (16 px each); null for anything else. */
  21. export function lenPx(v: string): number | null {
  22. const m = /^(-?\d*\.?\d+)(px|rem|em)?$/.exec(v.trim());
  23. if (!m || (m[2] === undefined && Number(m[1]) !== 0)) return null;
  24. return Number(m[1]) * (m[2] === 'px' || m[2] === undefined ? 1 : 16);
  25. }
  26. const lineOf = (text: string, offset: number) => text.slice(0, offset).split('\n').length;
  27. function allowedFn(text: string): (line: number) => boolean {
  28. const lines = text.split('\n');
  29. return (line) => (lines[line - 1] ?? '').includes(ALLOW) || (lines[line - 2] ?? '').includes(ALLOW);
  30. }
  31. /** Declarations of a style="…" value; all on `line`. */
  32. export function inlineDecls(value: string, line: number): Decl[] {
  33. const out: Decl[] = [];
  34. for (const part of splitTop(value, ';')) {
  35. const i = part.indexOf(':');
  36. if (i <= 0) continue;
  37. const prop = part.slice(0, i).trim();
  38. if (!/^(--)?[a-zA-Z-][\w-]*$/.test(prop)) continue;
  39. out.push({ prop: prop.startsWith('--') ? prop : prop.toLowerCase(), value: part.slice(i + 1).trim().replace(/\s*!important$/i, ''), line });
  40. }
  41. return out;
  42. }
  43. /** The units of one text; `html` tells how to read it. Exposed for the tests. */
  44. export function unitsOf(file: string, text: string, html: boolean): Unit[] {
  45. const allowed = allowedFn(text);
  46. const out: Unit[] = [];
  47. const add = (css: string, baseLine: number) => {
  48. for (const b of parseCss(css)) out.push({ file, line: b.line + baseLine - 1, selectors: b.selectors, at: b.at, decls: b.decls.map((d) => ({ ...d, line: d.line + baseLine - 1 })), allowed });
  49. };
  50. if (!html) { add(text, 1); return out; }
  51. const blank = blankComments(text, true);
  52. for (const m of blank.matchAll(/<style\b[^>]*>([\s\S]*?)<\/style>/gi)) add(stripComments(m[1]), lineOf(blank, (m.index ?? 0) + m[0].indexOf('>') + 1));
  53. for (const el of elements(blank, /[A-Za-z][\w.:-]*/)) {
  54. const st = attr(el, 'style');
  55. if (!st) continue;
  56. const line = lineOf(blank, el.start);
  57. out.push({ file, line, selectors: [], at: [], decls: inlineDecls(st, line), tag: el.name.toLowerCase(), classes: (attr(el, 'class') ?? '').split(/\s+/).filter(Boolean), slot: attr(el, 'data-slot') ?? undefined, allowed });
  58. }
  59. return out;
  60. }
  61. /** Every unit of the project's CSS and HTML files. */
  62. export function allUnits(sc: Scope = scope()): Unit[] {
  63. const out: Unit[] = [];
  64. for (const rel of projectFiles(['css', 'html', 'htm'], sc)) out.push(...unitsOf(rel, read(rel, sc.root), !rel.endsWith('.css')));
  65. return out;
  66. }