файл пакета→ .shtukbase/packages/shtukbase-style/lib/scan.ts
lib/scan.ts
74 строки, 3.4 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // What the style checks look at: rule blocks of the project's CSS files and <style> blocks of HTML files, and style="…"
- // attributes of HTML elements. Which files: the settings of design-base (page /design/design-checks.md). A line
- // marked `shtukbase-style: allow` is skipped. The working directory is the project root.
- import { parseCss, splitTop, stripComments, type Decl } from '../../design-base/lib/css.ts';
- import { projectFiles, read, scope, type Scope } from '../../design-base/lib/files.ts';
- import { attr, blankComments, elements } from '../../design-base/lib/markup.ts';
- export const ALLOW = 'shtukbase-style: allow';
- /** One place with declarations: a CSS rule (selectors set), or the style attribute of an element (tag set). */
- export type Unit = {
- file: string;
- line: number;
- selectors: string[];
- at: string[];
- decls: Decl[];
- tag?: string;
- classes?: string[];
- slot?: string;
- allowed: (line: number) => boolean;
- };
- /** Length in px: `120px`, `7.5rem`, `10em` (16 px each); null for anything else. */
- export function lenPx(v: string): number | null {
- const m = /^(-?\d*\.?\d+)(px|rem|em)?$/.exec(v.trim());
- if (!m || (m[2] === undefined && Number(m[1]) !== 0)) return null;
- return Number(m[1]) * (m[2] === 'px' || m[2] === undefined ? 1 : 16);
- }
- const lineOf = (text: string, offset: number) => text.slice(0, offset).split('\n').length;
- function allowedFn(text: string): (line: number) => boolean {
- const lines = text.split('\n');
- return (line) => (lines[line - 1] ?? '').includes(ALLOW) || (lines[line - 2] ?? '').includes(ALLOW);
- }
- /** Declarations of a style="…" value; all on `line`. */
- export function inlineDecls(value: string, line: number): Decl[] {
- const out: Decl[] = [];
- for (const part of splitTop(value, ';')) {
- const i = part.indexOf(':');
- if (i <= 0) continue;
- const prop = part.slice(0, i).trim();
- if (!/^(--)?[a-zA-Z-][\w-]*$/.test(prop)) continue;
- out.push({ prop: prop.startsWith('--') ? prop : prop.toLowerCase(), value: part.slice(i + 1).trim().replace(/\s*!important$/i, ''), line });
- }
- return out;
- }
- /** The units of one text; `html` tells how to read it. Exposed for the tests. */
- export function unitsOf(file: string, text: string, html: boolean): Unit[] {
- const allowed = allowedFn(text);
- const out: Unit[] = [];
- const add = (css: string, baseLine: number) => {
- 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 });
- };
- if (!html) { add(text, 1); return out; }
- const blank = blankComments(text, true);
- 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));
- for (const el of elements(blank, /[A-Za-z][\w.:-]*/)) {
- const st = attr(el, 'style');
- if (!st) continue;
- const line = lineOf(blank, el.start);
- 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 });
- }
- return out;
- }
- /** Every unit of the project's CSS and HTML files. */
- export function allUnits(sc: Scope = scope()): Unit[] {
- const out: Unit[] = [];
- for (const rel of projectFiles(['css', 'html', 'htm'], sc)) out.push(...unitsOf(rel, read(rel, sc.root), !rel.endsWith('.css')));
- return out;
- }