(htmlEl: Element)
| 792 | } |
| 793 | |
| 794 | function parseCompositionVariables(htmlEl: Element): CompositionVariable[] { |
| 795 | const variablesAttr = htmlEl.getAttribute("data-composition-variables"); |
| 796 | if (!variablesAttr) { |
| 797 | return []; |
| 798 | } |
| 799 | |
| 800 | try { |
| 801 | const parsed = JSON.parse(variablesAttr); |
| 802 | if (!Array.isArray(parsed)) { |
| 803 | return []; |
| 804 | } |
| 805 | |
| 806 | return parsed.filter((v): v is CompositionVariable => { |
| 807 | if (typeof v !== "object" || v === null) return false; |
| 808 | if (typeof v.id !== "string" || typeof v.label !== "string") return false; |
| 809 | if (!["string", "number", "color", "boolean", "enum", "font", "image"].includes(v.type)) |
| 810 | return false; |
| 811 | |
| 812 | switch (v.type) { |
| 813 | case "string": |
| 814 | return typeof v.default === "string"; |
| 815 | case "number": |
| 816 | return typeof v.default === "number"; |
| 817 | case "color": |
| 818 | return typeof v.default === "string"; |
| 819 | case "boolean": |
| 820 | return typeof v.default === "boolean"; |
| 821 | case "enum": |
| 822 | return typeof v.default === "string" && Array.isArray(v.options); |
| 823 | case "font": |
| 824 | // default is the font-family name string; extra metadata fields are optional |
| 825 | return typeof v.default === "string"; |
| 826 | case "image": |
| 827 | // default is the fallback image URL string; extra metadata fields are optional |
| 828 | return typeof v.default === "string"; |
| 829 | default: |
| 830 | return false; |
| 831 | } |
| 832 | }); |
| 833 | } catch { |
| 834 | return []; |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | export function validateCompositionHtml(html: string): ValidationResult { |
| 839 | const errors: string[] = []; |
no test coverage detected