|
| 1 | +/** |
| 2 | + * Browser-safe parser for the `data-composition-variables` schema attribute. |
| 3 | + * Lives outside htmlParser.ts so browser consumers (SDK, Studio, lint) can |
| 4 | + * import it via `@hyperframes/parsers/composition` without pulling the |
| 5 | + * linkedom/Node HTML-parser machinery from the main entry. |
| 6 | + */ |
| 7 | + |
| 8 | +import type { CompositionVariable, CompositionVariableType } from "./types.js"; |
| 9 | + |
| 10 | +/** |
| 11 | + * Required typeof for each variable type's `default`. For font the default is |
| 12 | + * the font-family name string; for image it is the fallback URL string — |
| 13 | + * extra metadata fields on both are optional and not validated here. |
| 14 | + */ |
| 15 | +const DEFAULT_TYPEOF: Record<CompositionVariableType, "string" | "number" | "boolean"> = { |
| 16 | + string: "string", |
| 17 | + number: "number", |
| 18 | + color: "string", |
| 19 | + boolean: "boolean", |
| 20 | + enum: "string", |
| 21 | + font: "string", |
| 22 | + image: "string", |
| 23 | +}; |
| 24 | + |
| 25 | +/** |
| 26 | + * Scalar variable values (string/number/boolean) are the ones that flow into |
| 27 | + * CSS custom props and text bindings; font/image values are object-shaped. |
| 28 | + * Shared so the SDK's CSS-compat writes, the runtime bindings, and Studio's |
| 29 | + * display logic can never disagree on what "scalar" means. |
| 30 | + */ |
| 31 | +export function isScalarVariableValue(value: unknown): value is string | number | boolean { |
| 32 | + return typeof value === "string" || typeof value === "number" || typeof value === "boolean"; |
| 33 | +} |
| 34 | + |
| 35 | +function isRecord(v: unknown): v is Record<string, unknown> { |
| 36 | + return typeof v === "object" && v !== null; |
| 37 | +} |
| 38 | + |
| 39 | +function isVariableType(t: unknown): t is CompositionVariableType { |
| 40 | + return typeof t === "string" && t in DEFAULT_TYPEOF; |
| 41 | +} |
| 42 | + |
| 43 | +function isCompositionVariable(v: unknown): v is CompositionVariable { |
| 44 | + if (!isRecord(v)) return false; |
| 45 | + if (typeof v.id !== "string" || typeof v.label !== "string") return false; |
| 46 | + if (!isVariableType(v.type)) return false; |
| 47 | + if (typeof v.default !== DEFAULT_TYPEOF[v.type]) return false; |
| 48 | + if (v.type === "enum" && !Array.isArray(v.options)) return false; |
| 49 | + return true; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Parse the typed variable declarations from an element's |
| 54 | + * `data-composition-variables` attribute. Malformed entries (wrong shape, |
| 55 | + * unknown type, default not matching the declared type) are dropped; an |
| 56 | + * absent attribute, invalid JSON, or a non-array payload yields `[]`. |
| 57 | + */ |
| 58 | +export function parseCompositionVariables(htmlEl: Element): CompositionVariable[] { |
| 59 | + const variablesAttr = htmlEl.getAttribute("data-composition-variables"); |
| 60 | + if (!variablesAttr) { |
| 61 | + return []; |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + const parsed = JSON.parse(variablesAttr); |
| 66 | + if (!Array.isArray(parsed)) { |
| 67 | + return []; |
| 68 | + } |
| 69 | + return parsed.filter(isCompositionVariable); |
| 70 | + } catch { |
| 71 | + return []; |
| 72 | + } |
| 73 | +} |
0 commit comments