|
| 1 | +/** |
| 2 | + * Hand-rolled flat-CSS rule editor. |
| 3 | + * |
| 4 | + * Handles only simple flat rules (`selector { declarations }`) — no nesting, |
| 5 | + * no @-rules, no CSS comments. Sufficient for composition <style> blocks |
| 6 | + * generated by hyperframes, which never contain those constructs. |
| 7 | + */ |
| 8 | + |
| 9 | +// ─── Types ──────────────────────────────────────────────────────────────────── |
| 10 | + |
| 11 | +interface CssRule { |
| 12 | + selector: string; |
| 13 | + body: string; |
| 14 | + /** Byte offset of the first char of the selector. */ |
| 15 | + start: number; |
| 16 | + /** Byte offset after the closing `}`. */ |
| 17 | + end: number; |
| 18 | +} |
| 19 | + |
| 20 | +// ─── Parsing ────────────────────────────────────────────────────────────────── |
| 21 | + |
| 22 | +function parseCssRules(css: string): CssRule[] { |
| 23 | + const rules: CssRule[] = []; |
| 24 | + const re = /([^{}]+?)\s*\{([^}]*)\}/g; |
| 25 | + let m: RegExpExecArray | null; |
| 26 | + while ((m = re.exec(css)) !== null) { |
| 27 | + rules.push({ |
| 28 | + selector: m[1]!.trim(), |
| 29 | + body: m[2]!, |
| 30 | + start: m.index, |
| 31 | + end: m.index + m[0].length, |
| 32 | + }); |
| 33 | + } |
| 34 | + return rules; |
| 35 | +} |
| 36 | + |
| 37 | +function parseDeclarations(body: string): Record<string, string> { |
| 38 | + const decls: Record<string, string> = {}; |
| 39 | + for (const part of body.split(";")) { |
| 40 | + const colon = part.indexOf(":"); |
| 41 | + if (colon === -1) continue; |
| 42 | + const prop = part.slice(0, colon).trim(); |
| 43 | + const value = part.slice(colon + 1).trim(); |
| 44 | + if (prop && value) decls[prop] = value; |
| 45 | + } |
| 46 | + return decls; |
| 47 | +} |
| 48 | + |
| 49 | +function serializeDeclarations(decls: Record<string, string>): string { |
| 50 | + const entries = Object.entries(decls); |
| 51 | + if (!entries.length) return ""; |
| 52 | + return " " + entries.map(([k, v]) => `${k}: ${v}`).join("; ") + "; "; |
| 53 | +} |
| 54 | + |
| 55 | +function normalizeSelector(sel: string): string { |
| 56 | + return sel.trim().replace(/\s+/g, " "); |
| 57 | +} |
| 58 | + |
| 59 | +// ─── Public API ─────────────────────────────────────────────────────────────── |
| 60 | + |
| 61 | +/** |
| 62 | + * Update or insert a CSS rule. |
| 63 | + * |
| 64 | + * Finds the first rule whose selector matches (after whitespace normalization) |
| 65 | + * and merges the given declarations into it. A null value removes the property. |
| 66 | + * If no matching rule exists, appends a new one. |
| 67 | + * |
| 68 | + * Returns the modified CSS string. Returns `css` unchanged when nothing changed. |
| 69 | + */ |
| 70 | +export function upsertCssRule( |
| 71 | + css: string, |
| 72 | + selector: string, |
| 73 | + styles: Record<string, string | null>, |
| 74 | +): string { |
| 75 | + const normalized = normalizeSelector(selector); |
| 76 | + const rules = parseCssRules(css); |
| 77 | + const idx = rules.findIndex((r) => normalizeSelector(r.selector) === normalized); |
| 78 | + |
| 79 | + if (idx !== -1) { |
| 80 | + const rule = rules[idx]!; |
| 81 | + const decls = parseDeclarations(rule.body); |
| 82 | + for (const [prop, value] of Object.entries(styles)) { |
| 83 | + if (value === null) { |
| 84 | + delete decls[prop]; |
| 85 | + } else { |
| 86 | + decls[prop] = value; |
| 87 | + } |
| 88 | + } |
| 89 | + const newRuleText = `${rule.selector} {${serializeDeclarations(decls)}}`; |
| 90 | + return css.slice(0, rule.start) + newRuleText + css.slice(rule.end); |
| 91 | + } |
| 92 | + |
| 93 | + // Append new rule — skip if all values are null (nothing to write). |
| 94 | + const newDecls: Record<string, string> = {}; |
| 95 | + for (const [prop, value] of Object.entries(styles)) { |
| 96 | + if (value !== null) newDecls[prop] = value; |
| 97 | + } |
| 98 | + if (!Object.keys(newDecls).length) return css; |
| 99 | + |
| 100 | + const newRuleText = `${selector} {${serializeDeclarations(newDecls)}}`; |
| 101 | + const sep = css.length > 0 && !css.endsWith("\n") ? "\n" : ""; |
| 102 | + return css + sep + newRuleText + "\n"; |
| 103 | +} |
0 commit comments