@pairdraw/dom-css 0.16.2 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -46,6 +46,19 @@ function mergeClassNames(...values) {
46
46
  const className = values.flatMap((value) => value ? splitWhitespace(value) : []).map((value) => value.trim()).filter((value) => value.length > 0).join(" ");
47
47
  return className.length > 0 ? className : void 0;
48
48
  }
49
+ /**
50
+ * Characters that end an attribute name or quote one. Imported JSX props can have any name, and
51
+ * one holding these would write other attributes, so such a name is left out.
52
+ */
53
+ const UNSAFE_ATTRIBUTE_NAME = /[\s"'/=>]/;
54
+ /**
55
+ * HTML, SVG, and custom element names. Imported JSX can name any tag, and one holding spaces or
56
+ * quotes would write attributes, so another name is written as a `div` with the same content.
57
+ */
58
+ const SAFE_TAG_NAME = /^[a-z][\w.:-]*$/i;
59
+ function safeTagName(tagName) {
60
+ return SAFE_TAG_NAME.test(tagName) ? tagName : "div";
61
+ }
49
62
  function serializeAttrs(node, options) {
50
63
  const style = serializeStyle(node);
51
64
  const tailwindClass = options.style === "tailwind" ? serializeTailwindClasses(node, options.themeVariables) : void 0;
@@ -54,7 +67,7 @@ function serializeAttrs(node, options) {
54
67
  const attrs = { ...options.style === "tailwind" && tailwindClass ? attrsWithoutStyle : node.attrs };
55
68
  if (tailwindClass) attrs.class = mergeClassNames(node.attrs.class, tailwindClass);
56
69
  if (style && options.style !== "tailwind") attrs.style = style;
57
- const serialized = Object.entries(attrs).filter((entry) => typeof entry[1] === "string" && entry[1] !== "").map(([name, value]) => `${name}="${escapeAttribute(value)}"`);
70
+ const serialized = Object.entries(attrs).filter((entry) => typeof entry[1] === "string" && entry[1] !== "").filter(([name]) => name !== "" && !UNSAFE_ATTRIBUTE_NAME.test(name)).map(([name, value]) => `${name}="${escapeAttribute(value)}"`);
58
71
  if (serialized.length === 0) return "";
59
72
  return ` ${serialized.join(" ")}`;
60
73
  }
@@ -65,7 +78,7 @@ function shaderFrame(node) {
65
78
  return frame ? `<img src="${escapeAttribute(frame)}" alt="" style="${SHADER_FRAME_STYLE}">` : "";
66
79
  }
67
80
  function serializeElement(node, options, depth) {
68
- const tagName = node.tagName;
81
+ const tagName = safeTagName(node.tagName);
69
82
  const attrs = serializeAttrs(node, options);
70
83
  if (VOID_ELEMENTS.has(tagName.toLowerCase())) return `<${tagName}${attrs}>`;
71
84
  const children = [shaderFrame(node), ...node.children.map((child) => serializeNode(child, options, depth + 1))].filter((child) => child !== "");
@@ -84,7 +97,7 @@ function serializeNode(node, options = {}, depth = 0) {
84
97
  function serializedElements(document) {
85
98
  const visit = (node) => {
86
99
  if (node.type === "text") return [];
87
- if (VOID_ELEMENTS.has(node.tagName.toLowerCase())) return [node];
100
+ if (VOID_ELEMENTS.has(safeTagName(node.tagName).toLowerCase())) return [node];
88
101
  return [
89
102
  node,
90
103
  ...node.shader?.frame ? [null] : [],
@@ -1 +1 @@
1
- {"version":3,"file":"tailwind.js","names":[],"sources":["../../../../src/export/html.ts","../../../../src/behaviours/states/tailwind.ts"],"sourcesContent":["import { escapeAttribute, escapeText } from 'entities'\nimport { twirl } from 'twirlwind'\n\nimport type { DesignDocument, DesignElement, DesignNode, DesignText } from '../types'\n\nexport interface SerializeHTMLOptions {\n style?: 'inline' | 'tailwind'\n /** Custom properties declared in `@theme`; a `var()` naming one becomes its utility. */\n themeVariables?: readonly string[]\n /**\n * Put each child of an element that holds only elements on its own indented line, for code\n * people read. Text keeps its whitespace; whitespace between elements in a projected layer\n * changes nothing, since its children are placed by flex, grid, or absolute position.\n */\n indent?: boolean\n}\n\nconst VOID_ELEMENTS = new Set([\n 'area',\n 'base',\n 'br',\n 'col',\n 'embed',\n 'hr',\n 'img',\n 'input',\n 'link',\n 'meta',\n 'param',\n 'source',\n 'track',\n 'wbr'\n])\n\nexport function splitWhitespace(value: string): string[] {\n const parts: string[] = []\n let current = ''\n for (const char of value) {\n if (char === ' ' || char === '\\n' || char === '\\t' || char === '\\r' || char === '\\f') {\n if (current.length > 0) parts.push(current)\n current = ''\n } else {\n current += char\n }\n }\n if (current.length > 0) parts.push(current)\n return parts\n}\n\nfunction serializeText(node: DesignText): string {\n return escapeText(node.text)\n}\n\nfunction serializeStyle(node: DesignElement): string | undefined {\n if (!node.inlineStyle || Object.keys(node.inlineStyle).length === 0) return undefined\n return Object.entries(node.inlineStyle)\n .filter(([, value]) => value !== '')\n .map(([property, value]) => `${property}: ${value}`)\n .join('; ')\n}\n\nexport function serializeTailwindClasses(\n node: DesignElement,\n themeVariables: readonly string[] = []\n): string | undefined {\n const style = serializeStyle(node)\n if (!style) return undefined\n const className = twirl(style, { theme: { variables: themeVariables } })\n return className.length > 0 ? className : undefined\n}\n\nexport function mergeClassNames(...values: Array<string | undefined>): string | undefined {\n const className = values\n .flatMap((value) => (value ? splitWhitespace(value) : []))\n .map((value) => value.trim())\n .filter((value) => value.length > 0)\n .join(' ')\n return className.length > 0 ? className : undefined\n}\n\nfunction serializeAttrs(node: DesignElement, options: SerializeHTMLOptions): string {\n const style = serializeStyle(node)\n const tailwindClass =\n options.style === 'tailwind'\n ? serializeTailwindClasses(node, options.themeVariables)\n : undefined\n const attrsWithoutStyle = { ...node.attrs }\n delete attrsWithoutStyle.style\n const sourceAttrs = options.style === 'tailwind' && tailwindClass ? attrsWithoutStyle : node.attrs\n const attrs: Record<string, string | undefined> = { ...sourceAttrs }\n if (tailwindClass) attrs.class = mergeClassNames(node.attrs.class, tailwindClass)\n if (style && options.style !== 'tailwind') attrs.style = style\n const serialized = Object.entries(attrs)\n .filter((entry): entry is [string, string] => typeof entry[1] === 'string' && entry[1] !== '')\n .map(([name, value]) => `${name}=\"${escapeAttribute(value)}\"`)\n\n if (serialized.length === 0) return ''\n return ` ${serialized.join(' ')}`\n}\n\n/** Static HTML cannot play a shader, so its layer shows the still frame behind its content. */\nconst SHADER_FRAME_STYLE =\n 'position:absolute;inset:0;width:100%;height:100%;object-fit:cover;z-index:-1;border-radius:inherit;pointer-events:none'\n\nfunction shaderFrame(node: DesignElement): string {\n const frame = node.shader?.frame\n return frame ? `<img src=\"${escapeAttribute(frame)}\" alt=\"\" style=\"${SHADER_FRAME_STYLE}\">` : ''\n}\n\nfunction serializeElement(\n node: DesignElement,\n options: SerializeHTMLOptions,\n depth: number\n): string {\n // SVG names such as `linearGradient` keep their case, as an SVG parser needs them.\n const tagName = node.tagName\n const attrs = serializeAttrs(node, options)\n if (VOID_ELEMENTS.has(tagName.toLowerCase())) return `<${tagName}${attrs}>`\n const children = [\n shaderFrame(node),\n ...node.children.map((child) => serializeNode(child, options, depth + 1))\n ].filter((child) => child !== '')\n const block =\n options.indent && children.length > 0 && node.children.every((child) => child.type !== 'text')\n if (!block) return `<${tagName}${attrs}>${children.join('')}</${tagName}>`\n const inner = ' '.repeat(depth + 1)\n const outer = ' '.repeat(depth)\n return `<${tagName}${attrs}>\\n${children.map((child) => inner + child).join('\\n')}\\n${outer}</${tagName}>`\n}\n\nexport function serializeNode(\n node: DesignNode,\n options: SerializeHTMLOptions = {},\n depth = 0\n): string {\n return node.type === 'text' ? serializeText(node) : serializeElement(node, options, depth)\n}\n\n/**\n * The elements `serializeHTML` writes, in the order they open: the document's own, and `null`\n * for a shader's still frame, which it adds in front of its layer's content.\n */\nexport function serializedElements(document: DesignDocument): (DesignElement | null)[] {\n const visit = (node: DesignNode): (DesignElement | null)[] => {\n if (node.type === 'text') return []\n if (VOID_ELEMENTS.has(node.tagName.toLowerCase())) return [node]\n return [node, ...(node.shader?.frame ? [null] : []), ...node.children.flatMap(visit)]\n }\n return document.children.flatMap(visit)\n}\n\nexport function serializeHTML(\n document: DesignDocument,\n options: SerializeHTMLOptions = {}\n): string {\n const resolved: SerializeHTMLOptions = {\n ...options,\n themeVariables: options.themeVariables ?? document.tokens?.themeVariables()\n }\n return document.children\n .map((node) => serializeNode(node, resolved))\n .join(options.indent ? '\\n' : '')\n}\n","import { splitWhitespace } from '#dom-css/export/html'\nimport type { DesignDocument, DesignNode, DesignStyleDeclaration } from '#dom-css/types'\nimport { compact } from 'es-toolkit/array'\nimport { twirl } from 'twirlwind'\n\nimport { cssName, propAttribute } from './names'\nimport type { StateCondition, StateElement, StateNode, StateStyles } from './types'\n\nexport interface StateTailwindOptions {\n /** Custom properties declared in `@theme`; a `var()` naming one becomes its utility. */\n themeVariables?: readonly string[]\n}\n\n/** A value inside a variant: bare when it is a plain word, else quoted with spaces as `_`. */\nconst variantValue = (value: string) =>\n /^[\\w-]+$/.test(value) ? value : `\"${value.replace(/\\s/g, '_').replace(/\"/g, '\\\\\"')}\"`\n\n/**\n * The variant a condition adds. On the root it tests the root itself; below it, it tests the\n * root through its named group, so a control nested in another reacts only to its own root.\n */\nfunction variant(condition: StateCondition, group: string | null): string {\n const on = (name: string) => (group ? `group-${name}/${group}:` : `${name}:`)\n if (condition.type === 'state') return on(`data-[state=${variantValue(condition.value)}]`)\n if (condition.type === 'disabled') return on('data-disabled')\n if (condition.type === 'filled') return on('has-[:is(input,textarea):not(:placeholder-shown)]')\n if (condition.type === 'prop') {\n const attribute = propAttribute(condition.name).slice('data-'.length)\n return on(`data-[${attribute}=${variantValue(condition.value)}]`)\n }\n if (condition.state === 'focus') return on(condition.within ? 'focus-within' : 'focus-visible')\n // A disabled control keeps its disabled look under the pointer.\n const enabled = group ? `group-not-data-disabled/${group}:` : 'not-data-disabled:'\n return `${enabled}${on(condition.state === 'hover' ? 'hover' : 'active')}`\n}\n\nfunction utilities(style: DesignStyleDeclaration, options: StateTailwindOptions): string[] {\n const css = Object.entries(style)\n .map(([property, value]) => `${property}: ${value}`)\n .join('; ')\n return css\n ? splitWhitespace(twirl(css, { theme: { variables: options.themeVariables ?? [] } }))\n : []\n}\n\n/** What each element's utilities are written against. */\ninterface Groups {\n /** The control's root group. */\n root: string\n /** A named group for each layer other than the root whose state a rule tests. */\n anchors: Map<StateElement, string>\n}\n\n/** The group a rule's conditions test from `node`, or null when they test `node` itself. */\nfunction testedGroup(\n node: StateElement,\n on: StateElement | undefined,\n groups: Groups,\n isRoot: boolean\n): string | null {\n if (on) return on === node ? null : (groups.anchors.get(on) ?? null)\n return isRoot ? null : groups.root\n}\n\n/** The utilities a layer takes: its group names, its rest style, and each rule behind its variants. */\nfunction elementUtilities(\n node: StateElement,\n groups: Groups,\n isRoot: boolean,\n options: StateTailwindOptions\n): string[] {\n const anchor = groups.anchors.get(node)\n return [\n ...(isRoot ? [`group/${groups.root}`] : []),\n ...(anchor ? [`group/${anchor}`] : []),\n ...utilities(node.base, options),\n ...node.rules.flatMap((rule) => {\n const group = testedGroup(node, rule.on, groups, isRoot)\n const prefix = [\n ...rule.conditions.map((condition) => variant(condition, group)),\n ...(rule.pseudo\n ? [rule.pseudo === 'placeholder' ? 'placeholder:' : `[&::${rule.pseudo}]:`]\n : [])\n ].join('')\n return utilities(rule.style, options).map((utility) => `${prefix}${utility}`)\n })\n ]\n}\n\nfunction designNode(node: StateNode, classes: Map<StateElement, string>): DesignNode {\n if (node.type === 'text') return node\n return {\n type: 'element',\n tagName: node.tagName,\n attrs: {\n ...node.attrs,\n class: compact([\n ...(node.attrs.class ? splitWhitespace(node.attrs.class) : []),\n classes.get(node)\n ]).join(' ')\n },\n children: node.children.map((child) => designNode(child, classes)),\n shader: node.shader\n }\n}\n\n/** The layers rules test other than the root, each named after the root's group. */\nfunction anchorGroups(root: StateElement, group: string): Map<StateElement, string> {\n const anchors = new Map<StateElement, string>()\n const visit = (node: StateNode) => {\n if (node.type === 'text') return\n for (const rule of node.rules)\n if (rule.on && !anchors.has(rule.on)) anchors.set(rule.on, `${group}-${anchors.size + 1}`)\n for (const child of node.children) visit(child)\n }\n visit(root)\n return anchors\n}\n\n/**\n * Each element's Tailwind utilities for the state styles: rest utilities, and each variant's\n * changes behind the variants that show it. More stacked variants make a more specific\n * selector, so a combined variant wins over each of its parts.\n */\nexport function stateTailwindClasses(\n styles: StateStyles,\n options: StateTailwindOptions = {}\n): Map<StateElement, string> {\n const root = cssName(styles.name, 'component')\n const groups = { root, anchors: anchorGroups(styles.root, root) }\n const classes = new Map<StateElement, string>()\n const visit = (node: StateNode, isRoot: boolean) => {\n if (node.type === 'text') return\n classes.set(node, elementUtilities(node, groups, isRoot, options).join(' '))\n for (const child of node.children) visit(child, false)\n }\n visit(styles.root, true)\n return classes\n}\n\n/** The state styles as Tailwind utilities on each element of the merged markup. */\nexport function stateStylesToTailwind(\n styles: StateStyles,\n options: StateTailwindOptions = {}\n): DesignDocument {\n const classes = stateTailwindClasses(styles, options)\n return { type: 'document', children: [designNode(styles.root, classes)] }\n}\n"],"mappings":";;;;;AAiBA,MAAM,gCAAgB,IAAI,IAAI;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;AAED,SAAgB,gBAAgB,OAAyB;CACvD,MAAM,QAAkB,CAAC;CACzB,IAAI,UAAU;CACd,KAAK,MAAM,QAAQ,OACjB,IAAI,SAAS,OAAO,SAAS,QAAQ,SAAS,OAAQ,SAAS,QAAQ,SAAS,MAAM;EACpF,IAAI,QAAQ,SAAS,GAAG,MAAM,KAAK,OAAO;EAC1C,UAAU;CACZ,OACE,WAAW;CAGf,IAAI,QAAQ,SAAS,GAAG,MAAM,KAAK,OAAO;CAC1C,OAAO;AACT;AAEA,SAAS,cAAc,MAA0B;CAC/C,OAAO,WAAW,KAAK,IAAI;AAC7B;AAEA,SAAS,eAAe,MAAyC;CAC/D,IAAI,CAAC,KAAK,eAAe,OAAO,KAAK,KAAK,WAAW,CAAC,CAAC,WAAW,GAAG,OAAO,KAAA;CAC5E,OAAO,OAAO,QAAQ,KAAK,WAAW,CAAC,CACpC,QAAQ,GAAG,WAAW,UAAU,EAAE,CAAC,CACnC,KAAK,CAAC,UAAU,WAAW,GAAG,SAAS,IAAI,OAAO,CAAC,CACnD,KAAK,IAAI;AACd;AAEA,SAAgB,yBACd,MACA,iBAAoC,CAAC,GACjB;CACpB,MAAM,QAAQ,eAAe,IAAI;CACjC,IAAI,CAAC,OAAO,OAAO,KAAA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AM,QAAQ,KAAK,OACtB,IAAI,KAAK,MAAM,CAAC,QAAQ,IAAI,KAAK,EAAE,GAAG,QAAQ,IAAI,KAAK,IAAI,GAAG,MAAM,GAAG,QAAQ,OAAO,GAAG;EAC3F,KAAK,MAAM,SAAS,KAAK,UAAU,MAAM,KAAK;CAChD;CACA,MAAM,IAAI;CACV,OAAO;AACT;;;;;;AAOA,SAAgB,qBACd,QACA,UAAgC,CAAC,GACN;CAC3B,MAAM,OAAO,QAAQ,OAAO,MAAM,WAAW;CAC7C,MAAM,SAAS;EAAE;EAAM,SAAS,aAAa,OAAO,MAAM,IAAI;CAAE;CAChE,MAAM,0BAAU,IAAI,IAA0B;CAC9C,MAAM,SAAS,MAAiB,WAAoB;EAClD,IAAI,KAAK,SAAS,QAAQ;EAC1B,QAAQ,IAAI,MAAM,iBAAiB,MAAM,QAAQ,QAAQ,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC;EAC3E,KAAK,MAAM,SAAS,KAAK,UAAU,MAAM,OAAO,KAAK;CACvD;CACA,MAAM,OAAO,MAAM,IAAI;CACvB,OAAO;AACT;;AAGA,SAAgB,sBACd,QACA,UAAgC,CAAC,GACjB;CAChB,MAAM,UAAU,qBAAqB,QAAQ,OAAO;CACpD,OAAO;EAAE,MAAM;EAAY,UAAU,CAAC,WAAW,OAAO,MAAM,OAAO,CAAC;CAAE;AAC1E"}
1
+ {"version":3,"file":"tailwind.js","names":[],"sources":["../../../../src/export/html.ts","../../../../src/behaviours/states/tailwind.ts"],"sourcesContent":["import { escapeAttribute, escapeText } from 'entities'\nimport { twirl } from 'twirlwind'\n\nimport type { DesignDocument, DesignElement, DesignNode, DesignText } from '../types'\n\nexport interface SerializeHTMLOptions {\n style?: 'inline' | 'tailwind'\n /** Custom properties declared in `@theme`; a `var()` naming one becomes its utility. */\n themeVariables?: readonly string[]\n /**\n * Put each child of an element that holds only elements on its own indented line, for code\n * people read. Text keeps its whitespace; whitespace between elements in a projected layer\n * changes nothing, since its children are placed by flex, grid, or absolute position.\n */\n indent?: boolean\n}\n\nconst VOID_ELEMENTS = new Set([\n 'area',\n 'base',\n 'br',\n 'col',\n 'embed',\n 'hr',\n 'img',\n 'input',\n 'link',\n 'meta',\n 'param',\n 'source',\n 'track',\n 'wbr'\n])\n\nexport function splitWhitespace(value: string): string[] {\n const parts: string[] = []\n let current = ''\n for (const char of value) {\n if (char === ' ' || char === '\\n' || char === '\\t' || char === '\\r' || char === '\\f') {\n if (current.length > 0) parts.push(current)\n current = ''\n } else {\n current += char\n }\n }\n if (current.length > 0) parts.push(current)\n return parts\n}\n\nfunction serializeText(node: DesignText): string {\n return escapeText(node.text)\n}\n\nfunction serializeStyle(node: DesignElement): string | undefined {\n if (!node.inlineStyle || Object.keys(node.inlineStyle).length === 0) return undefined\n return Object.entries(node.inlineStyle)\n .filter(([, value]) => value !== '')\n .map(([property, value]) => `${property}: ${value}`)\n .join('; ')\n}\n\nexport function serializeTailwindClasses(\n node: DesignElement,\n themeVariables: readonly string[] = []\n): string | undefined {\n const style = serializeStyle(node)\n if (!style) return undefined\n const className = twirl(style, { theme: { variables: themeVariables } })\n return className.length > 0 ? className : undefined\n}\n\nexport function mergeClassNames(...values: Array<string | undefined>): string | undefined {\n const className = values\n .flatMap((value) => (value ? splitWhitespace(value) : []))\n .map((value) => value.trim())\n .filter((value) => value.length > 0)\n .join(' ')\n return className.length > 0 ? className : undefined\n}\n\n/**\n * Characters that end an attribute name or quote one. Imported JSX props can have any name, and\n * one holding these would write other attributes, so such a name is left out.\n */\nconst UNSAFE_ATTRIBUTE_NAME = /[\\s\"'/=>]/\n\n/**\n * HTML, SVG, and custom element names. Imported JSX can name any tag, and one holding spaces or\n * quotes would write attributes, so another name is written as a `div` with the same content.\n */\nconst SAFE_TAG_NAME = /^[a-z][\\w.:-]*$/i\n\nfunction safeTagName(tagName: string): string {\n return SAFE_TAG_NAME.test(tagName) ? tagName : 'div'\n}\n\nfunction serializeAttrs(node: DesignElement, options: SerializeHTMLOptions): string {\n const style = serializeStyle(node)\n const tailwindClass =\n options.style === 'tailwind'\n ? serializeTailwindClasses(node, options.themeVariables)\n : undefined\n const attrsWithoutStyle = { ...node.attrs }\n delete attrsWithoutStyle.style\n const sourceAttrs = options.style === 'tailwind' && tailwindClass ? attrsWithoutStyle : node.attrs\n const attrs: Record<string, string | undefined> = { ...sourceAttrs }\n if (tailwindClass) attrs.class = mergeClassNames(node.attrs.class, tailwindClass)\n if (style && options.style !== 'tailwind') attrs.style = style\n const serialized = Object.entries(attrs)\n .filter((entry): entry is [string, string] => typeof entry[1] === 'string' && entry[1] !== '')\n .filter(([name]) => name !== '' && !UNSAFE_ATTRIBUTE_NAME.test(name))\n .map(([name, value]) => `${name}=\"${escapeAttribute(value)}\"`)\n\n if (serialized.length === 0) return ''\n return ` ${serialized.join(' ')}`\n}\n\n/** Static HTML cannot play a shader, so its layer shows the still frame behind its content. */\nconst SHADER_FRAME_STYLE =\n 'position:absolute;inset:0;width:100%;height:100%;object-fit:cover;z-index:-1;border-radius:inherit;pointer-events:none'\n\nfunction shaderFrame(node: DesignElement): string {\n const frame = node.shader?.frame\n return frame ? `<img src=\"${escapeAttribute(frame)}\" alt=\"\" style=\"${SHADER_FRAME_STYLE}\">` : ''\n}\n\nfunction serializeElement(\n node: DesignElement,\n options: SerializeHTMLOptions,\n depth: number\n): string {\n // SVG names such as `linearGradient` keep their case, as an SVG parser needs them.\n const tagName = safeTagName(node.tagName)\n const attrs = serializeAttrs(node, options)\n if (VOID_ELEMENTS.has(tagName.toLowerCase())) return `<${tagName}${attrs}>`\n const children = [\n shaderFrame(node),\n ...node.children.map((child) => serializeNode(child, options, depth + 1))\n ].filter((child) => child !== '')\n const block =\n options.indent && children.length > 0 && node.children.every((child) => child.type !== 'text')\n if (!block) return `<${tagName}${attrs}>${children.join('')}</${tagName}>`\n const inner = ' '.repeat(depth + 1)\n const outer = ' '.repeat(depth)\n return `<${tagName}${attrs}>\\n${children.map((child) => inner + child).join('\\n')}\\n${outer}</${tagName}>`\n}\n\nexport function serializeNode(\n node: DesignNode,\n options: SerializeHTMLOptions = {},\n depth = 0\n): string {\n return node.type === 'text' ? serializeText(node) : serializeElement(node, options, depth)\n}\n\n/**\n * The elements `serializeHTML` writes, in the order they open: the document's own, and `null`\n * for a shader's still frame, which it adds in front of its layer's content.\n */\nexport function serializedElements(document: DesignDocument): (DesignElement | null)[] {\n const visit = (node: DesignNode): (DesignElement | null)[] => {\n if (node.type === 'text') return []\n if (VOID_ELEMENTS.has(safeTagName(node.tagName).toLowerCase())) return [node]\n return [node, ...(node.shader?.frame ? [null] : []), ...node.children.flatMap(visit)]\n }\n return document.children.flatMap(visit)\n}\n\nexport function serializeHTML(\n document: DesignDocument,\n options: SerializeHTMLOptions = {}\n): string {\n const resolved: SerializeHTMLOptions = {\n ...options,\n themeVariables: options.themeVariables ?? document.tokens?.themeVariables()\n }\n return document.children\n .map((node) => serializeNode(node, resolved))\n .join(options.indent ? '\\n' : '')\n}\n","import { splitWhitespace } from '#dom-css/export/html'\nimport type { DesignDocument, DesignNode, DesignStyleDeclaration } from '#dom-css/types'\nimport { compact } from 'es-toolkit/array'\nimport { twirl } from 'twirlwind'\n\nimport { cssName, propAttribute } from './names'\nimport type { StateCondition, StateElement, StateNode, StateStyles } from './types'\n\nexport interface StateTailwindOptions {\n /** Custom properties declared in `@theme`; a `var()` naming one becomes its utility. */\n themeVariables?: readonly string[]\n}\n\n/** A value inside a variant: bare when it is a plain word, else quoted with spaces as `_`. */\nconst variantValue = (value: string) =>\n /^[\\w-]+$/.test(value) ? value : `\"${value.replace(/\\s/g, '_').replace(/\"/g, '\\\\\"')}\"`\n\n/**\n * The variant a condition adds. On the root it tests the root itself; below it, it tests the\n * root through its named group, so a control nested in another reacts only to its own root.\n */\nfunction variant(condition: StateCondition, group: string | null): string {\n const on = (name: string) => (group ? `group-${name}/${group}:` : `${name}:`)\n if (condition.type === 'state') return on(`data-[state=${variantValue(condition.value)}]`)\n if (condition.type === 'disabled') return on('data-disabled')\n if (condition.type === 'filled') return on('has-[:is(input,textarea):not(:placeholder-shown)]')\n if (condition.type === 'prop') {\n const attribute = propAttribute(condition.name).slice('data-'.length)\n return on(`data-[${attribute}=${variantValue(condition.value)}]`)\n }\n if (condition.state === 'focus') return on(condition.within ? 'focus-within' : 'focus-visible')\n // A disabled control keeps its disabled look under the pointer.\n const enabled = group ? `group-not-data-disabled/${group}:` : 'not-data-disabled:'\n return `${enabled}${on(condition.state === 'hover' ? 'hover' : 'active')}`\n}\n\nfunction utilities(style: DesignStyleDeclaration, options: StateTailwindOptions): string[] {\n const css = Object.entries(style)\n .map(([property, value]) => `${property}: ${value}`)\n .join('; ')\n return css\n ? splitWhitespace(twirl(css, { theme: { variables: options.themeVariables ?? [] } }))\n : []\n}\n\n/** What each element's utilities are written against. */\ninterface Groups {\n /** The control's root group. */\n root: string\n /** A named group for each layer other than the root whose state a rule tests. */\n anchors: Map<StateElement, string>\n}\n\n/** The group a rule's conditions test from `node`, or null when they test `node` itself. */\nfunction testedGroup(\n node: StateElement,\n on: StateElement | undefined,\n groups: Groups,\n isRoot: boolean\n): string | null {\n if (on) return on === node ? null : (groups.anchors.get(on) ?? null)\n return isRoot ? null : groups.root\n}\n\n/** The utilities a layer takes: its group names, its rest style, and each rule behind its variants. */\nfunction elementUtilities(\n node: StateElement,\n groups: Groups,\n isRoot: boolean,\n options: StateTailwindOptions\n): string[] {\n const anchor = groups.anchors.get(node)\n return [\n ...(isRoot ? [`group/${groups.root}`] : []),\n ...(anchor ? [`group/${anchor}`] : []),\n ...utilities(node.base, options),\n ...node.rules.flatMap((rule) => {\n const group = testedGroup(node, rule.on, groups, isRoot)\n const prefix = [\n ...rule.conditions.map((condition) => variant(condition, group)),\n ...(rule.pseudo\n ? [rule.pseudo === 'placeholder' ? 'placeholder:' : `[&::${rule.pseudo}]:`]\n : [])\n ].join('')\n return utilities(rule.style, options).map((utility) => `${prefix}${utility}`)\n })\n ]\n}\n\nfunction designNode(node: StateNode, classes: Map<StateElement, string>): DesignNode {\n if (node.type === 'text') return node\n return {\n type: 'element',\n tagName: node.tagName,\n attrs: {\n ...node.attrs,\n class: compact([\n ...(node.attrs.class ? splitWhitespace(node.attrs.class) : []),\n classes.get(node)\n ]).join(' ')\n },\n children: node.children.map((child) => designNode(child, classes)),\n shader: node.shader\n }\n}\n\n/** The layers rules test other than the root, each named after the root's group. */\nfunction anchorGroups(root: StateElement, group: string): Map<StateElement, string> {\n const anchors = new Map<StateElement, string>()\n const visit = (node: StateNode) => {\n if (node.type === 'text') return\n for (const rule of node.rules)\n if (rule.on && !anchors.has(rule.on)) anchors.set(rule.on, `${group}-${anchors.size + 1}`)\n for (const child of node.children) visit(child)\n }\n visit(root)\n return anchors\n}\n\n/**\n * Each element's Tailwind utilities for the state styles: rest utilities, and each variant's\n * changes behind the variants that show it. More stacked variants make a more specific\n * selector, so a combined variant wins over each of its parts.\n */\nexport function stateTailwindClasses(\n styles: StateStyles,\n options: StateTailwindOptions = {}\n): Map<StateElement, string> {\n const root = cssName(styles.name, 'component')\n const groups = { root, anchors: anchorGroups(styles.root, root) }\n const classes = new Map<StateElement, string>()\n const visit = (node: StateNode, isRoot: boolean) => {\n if (node.type === 'text') return\n classes.set(node, elementUtilities(node, groups, isRoot, options).join(' '))\n for (const child of node.children) visit(child, false)\n }\n visit(styles.root, true)\n return classes\n}\n\n/** The state styles as Tailwind utilities on each element of the merged markup. */\nexport function stateStylesToTailwind(\n styles: StateStyles,\n options: StateTailwindOptions = {}\n): DesignDocument {\n const classes = stateTailwindClasses(styles, options)\n return { type: 'document', children: [designNode(styles.root, classes)] }\n}\n"],"mappings":";;;;;AAiBA,MAAM,gCAAgB,IAAI,IAAI;CAC5B;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACF,CAAC;AAED,SAAgB,gBAAgB,OAAyB;CACvD,MAAM,QAAkB,CAAC;CACzB,IAAI,UAAU;CACd,KAAK,MAAM,QAAQ,OACjB,IAAI,S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MAAM,SAAS;EAAE;EAAM,SAAS,aAAa,OAAO,MAAM,IAAI;CAAE;CAChE,MAAM,0BAAU,IAAI,IAA0B;CAC9C,MAAM,SAAS,MAAiB,WAAoB;EAClD,IAAI,KAAK,SAAS,QAAQ;EAC1B,QAAQ,IAAI,MAAM,iBAAiB,MAAM,QAAQ,QAAQ,OAAO,CAAC,CAAC,KAAK,GAAG,CAAC;EAC3E,KAAK,MAAM,SAAS,KAAK,UAAU,MAAM,OAAO,KAAK;CACvD;CACA,MAAM,OAAO,MAAM,IAAI;CACvB,OAAO;AACT;;AAGA,SAAgB,sBACd,QACA,UAAgC,CAAC,GACjB;CAChB,MAAM,UAAU,qBAAqB,QAAQ,OAAO;CACpD,OAAO;EAAE,MAAM;EAAY,UAAU,CAAC,WAAW,OAAO,MAAM,OAAO,CAAC;CAAE;AAC1E"}
@@ -8,6 +8,7 @@ import { isEqual } from "es-toolkit/predicate";
8
8
  import { pick } from "es-toolkit";
9
9
  import { parseCSSAlignItems, parseCSSAlignSelf, parseCSSColor, parseCSSFlexDirection, parseCSSFontFamily, parseCSSJustifyContent, parseCSSLetterSpacing, parseCSSLineHeight, parseCSSNumber, parseCSSShadows } from "@pairdraw/scene-graph/css";
10
10
  import { SceneGraph } from "@pairdraw/scene-graph";
11
+ import { normalizeEffects } from "@pairdraw/scene-graph/effects";
11
12
  //#region src/runtime/browser/runtime.ts
12
13
  const DEFAULT_COMPUTED_PROPERTIES = [
13
14
  "align-items",
@@ -1013,7 +1014,7 @@ function applyElementStyle(graph, node, element, style) {
1013
1014
  setBorderWeights(node, style, strokes[0]);
1014
1015
  }
1015
1016
  const effects = parseCSSShadows(pickStyle(style, "box-shadow"));
1016
- if (effects.length > 0) node.effects = effects;
1017
+ if (effects.length > 0) node.effects = normalizeEffects(effects);
1017
1018
  const opacity = parseCSSNumber(pickStyle(style, "opacity"));
1018
1019
  if (opacity !== null) node.opacity = opacity;
1019
1020
  applyCornerRadii(node, style);
@@ -1045,7 +1046,7 @@ function applyTextStyle(node, style) {
1045
1046
  const opacity = parseCSSNumber(pickStyle(style, "opacity"));
1046
1047
  if (opacity !== null) node.opacity = opacity;
1047
1048
  const effects = parseCSSShadows(pickStyle(style, "text-shadow"));
1048
- if (effects.length > 0) node.effects = effects;
1049
+ if (effects.length > 0) node.effects = normalizeEffects(effects);
1049
1050
  const textAlign = pickStyle(style, "text-align")?.toUpperCase();
1050
1051
  if (textAlign === "CENTER" || textAlign === "RIGHT" || textAlign === "JUSTIFIED") node.textAlignHorizontal = textAlign;
1051
1052
  if (pickStyle(style, "white-space") === "nowrap") node.maxLines = 1;
@@ -1155,7 +1156,7 @@ function createElementNode(graph, parentId, element, parentFlow) {
1155
1156
  }
1156
1157
  if (!hasBoxStyle(style) && holdsRichText(element)) return createRichTextNode(graph, parentId, element, style, display, parentFlow);
1157
1158
  const node = graph.createNode("FRAME", parentId, {
1158
- name: element.attrs.id || element.attrs.class || element.tagName,
1159
+ name: element.attrs["data-name"] || element.attrs.id || element.attrs.class || element.tagName,
1159
1160
  clipsContent: false
1160
1161
  });
1161
1162
  applyElementStyle(graph, node, element, style);
@@ -1208,8 +1209,34 @@ function styleObjectToDeclaration(style) {
1208
1209
  if (entries.length === 0) return void 0;
1209
1210
  return Object.fromEntries(entries);
1210
1211
  }
1212
+ /** Declarations split at `;` outside quotes and parentheses, so `url(data:…;base64,…)` stays whole. */
1213
+ function splitDeclarations(style) {
1214
+ const declarations = [];
1215
+ let depth = 0;
1216
+ let quote = null;
1217
+ let start = 0;
1218
+ for (let index = 0; index < style.length; index++) {
1219
+ const character = style[index];
1220
+ if (quote) {
1221
+ if (character === "\\") index++;
1222
+ else if (character === quote) quote = null;
1223
+ } else if (character === "\"" || character === "'") quote = character;
1224
+ else if (character === "(") depth++;
1225
+ else if (character === ")") depth = Math.max(0, depth - 1);
1226
+ else if (character === ";" && depth === 0) {
1227
+ declarations.push(style.slice(start, index));
1228
+ start = index + 1;
1229
+ }
1230
+ }
1231
+ declarations.push(style.slice(start));
1232
+ return declarations;
1233
+ }
1211
1234
  function styleStringToDeclaration(style) {
1212
- const entries = style.split(";").map((declaration) => declaration.split(":")).filter((parts) => parts.length >= 2).map(([property, ...value]) => [property.trim().toLowerCase(), value.join(":").trim()]).filter(([property, value]) => property.length > 0 && value.length > 0);
1235
+ const entries = splitDeclarations(style).map((declaration) => {
1236
+ const colon = declaration.indexOf(":");
1237
+ if (colon === -1) return ["", ""];
1238
+ return [declaration.slice(0, colon).trim().toLowerCase(), declaration.slice(colon + 1).trim()];
1239
+ }).filter(([property, value]) => property.length > 0 && value.length > 0);
1213
1240
  if (entries.length === 0) return void 0;
1214
1241
  return Object.fromEntries(entries);
1215
1242
  }