@designos/renderer-dom 0.1.0 → 1.0.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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ComponentRegistryContract, PageDocument } from '@designos/core';
1
+ import { ComponentRegistryContract, PageDocument, DesignDocument } from '@designos/core';
2
2
 
3
3
  interface DomComponentDefinition {
4
4
  tag: string;
@@ -11,8 +11,8 @@ interface DomRenderOptions {
11
11
  document?: Document;
12
12
  }
13
13
  /** Build a browser DOM fragment from a PageDocument and a component-to-tag map. */
14
- declare function renderDomPage(data: PageDocument, options: DomRenderOptions): DocumentFragment;
14
+ declare function renderDomPage(page: PageDocument, design: DesignDocument, options: DomRenderOptions): DocumentFragment;
15
15
  /** Replace a container's children with the rendered page; cleanup removes only this render's roots. */
16
- declare function mountDomPage(container: Element, data: PageDocument, options: DomRenderOptions): () => void;
16
+ declare function mountDomPage(container: Element, page: PageDocument, design: DesignDocument, options: DomRenderOptions): () => void;
17
17
 
18
18
  export { type DomComponentDefinition, type DomComponentMap, type DomRenderOptions, mountDomPage, renderDomPage };
package/dist/index.js CHANGED
@@ -41,10 +41,10 @@ function setDomProps(element, props, textProp) {
41
41
  }
42
42
  }
43
43
  }
44
- function renderDomPage(data, options) {
44
+ function renderDomPage(page, design, options) {
45
45
  const doc = options.document ?? globalThis.document;
46
46
  if (!doc) throw new Error("renderDomPage requires a browser Document or options.document");
47
- const root = renderPageDocument(data, {
47
+ const root = renderPageDocument(page, design, {
48
48
  renderNode(view) {
49
49
  const definition = componentDefinition(view.node.type, options.components);
50
50
  const element = doc.createElement(definition.tag);
@@ -62,8 +62,8 @@ function renderDomPage(data, options) {
62
62
  fragment.appendChild(root);
63
63
  return fragment;
64
64
  }
65
- function mountDomPage(container, data, options) {
66
- const fragment = renderDomPage(data, { ...options, document: container.ownerDocument });
65
+ function mountDomPage(container, page, design, options) {
66
+ const fragment = renderDomPage(page, design, { ...options, document: container.ownerDocument });
67
67
  const roots = [...fragment.childNodes];
68
68
  container.replaceChildren(fragment);
69
69
  return () => roots.forEach((root) => {
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { ComponentRegistryContract, PageDocument } from \"@designos/core\";\nimport { renderPageDocument } from \"@designos/renderer-core\";\n\nexport interface DomComponentDefinition {\n tag: string;\n textProp?: string;\n}\nexport type DomComponentMap = Record<string, string | DomComponentDefinition>;\nexport interface DomRenderOptions {\n components: DomComponentMap;\n registry?: ComponentRegistryContract;\n document?: Document;\n}\n\nconst validTagName = /^[A-Za-z][A-Za-z0-9._:-]*$/;\nconst unsafeUrl = /^\\s*(?:javascript|vbscript|data):/i;\n\nfunction componentDefinition(type: string, components: DomComponentMap): DomComponentDefinition {\n const value = components[type];\n const definition = typeof value === \"string\" ? { tag: value } : value;\n return definition && validTagName.test(definition.tag) ? definition : { tag: \"div\" };\n}\n\nfunction setDomProps(element: HTMLElement, props: Record<string, unknown>, textProp?: string): void {\n for (const [key, value] of Object.entries(props)) {\n if ([textProp, \"children\", \"slots\", \"key\", \"ref\"].includes(key) || /^on[a-z]/i.test(key)) continue;\n const name = key === \"className\" ? \"class\" : key;\n if (!/^[A-Za-z_:][A-Za-z0-9:._-]*$/.test(name)) continue;\n if ([\"href\", \"src\", \"action\", \"formaction\", \"xlink:href\"].includes(name.toLowerCase()) && typeof value === \"string\" && unsafeUrl.test(value)) continue;\n if (name === \"style\" && value && typeof value === \"object\" && !Array.isArray(value)) {\n for (const [styleName, styleValue] of Object.entries(value as Record<string, unknown>)) {\n if (styleValue === null || styleValue === undefined || typeof styleValue === \"object\") continue;\n const cssName = styleName.startsWith(\"--\") ? styleName : styleName.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);\n element.style.setProperty(cssName, String(styleValue));\n }\n continue;\n }\n if (value === null || value === undefined) continue;\n if (typeof value === \"string\" || typeof value === \"number\") {\n element.setAttribute(name, String(value));\n if (name in element && !name.startsWith(\"aria-\") && !name.startsWith(\"data-\")) {\n try { Reflect.set(element, name, value); } catch { /* Read-only platform properties remain attributes only. */ }\n }\n } else if (typeof value === \"boolean\") {\n if (value) element.setAttribute(name, name.startsWith(\"aria-\") || name.startsWith(\"data-\") ? \"true\" : \"\");\n else element.removeAttribute(name);\n } else {\n try { Reflect.set(element, name, value); } catch { /* Preserve only values accepted by the native element. */ }\n }\n }\n}\n\n/** Build a browser DOM fragment from a PageDocument and a component-to-tag map. */\nexport function renderDomPage(data: PageDocument, options: DomRenderOptions): DocumentFragment {\n const doc = options.document ?? globalThis.document;\n if (!doc) throw new Error(\"renderDomPage requires a browser Document or options.document\");\n const root = renderPageDocument<HTMLElement>(data, {\n renderNode(view) {\n const definition = componentDefinition(view.node.type, options.components);\n const element = doc.createElement(definition.tag);\n setDomProps(element, view.node.props, definition.textProp);\n if (view.slotName) element.setAttribute(\"slot\", view.slotName);\n if (!options.components[view.node.type]) element.setAttribute(\"data-designos-missing\", view.node.type);\n const text = definition.textProp ? view.node.props[definition.textProp] : undefined;\n if (typeof text === \"string\" || typeof text === \"number\") element.appendChild(doc.createTextNode(String(text)));\n for (const child of view.children) element.appendChild(child);\n for (const nodes of Object.values(view.slots)) for (const child of nodes) element.appendChild(child);\n return element;\n },\n }, { registry: options.registry });\n const fragment = doc.createDocumentFragment();\n fragment.appendChild(root);\n return fragment;\n}\n\n/** Replace a container's children with the rendered page; cleanup removes only this render's roots. */\nexport function mountDomPage(container: Element, data: PageDocument, options: DomRenderOptions): () => void {\n const fragment = renderDomPage(data, { ...options, document: container.ownerDocument });\n const roots = [...fragment.childNodes];\n container.replaceChildren(fragment);\n return () => roots.forEach((root) => { if (root.parentNode === container) container.removeChild(root); });\n}\n"],"mappings":";AACA,SAAS,0BAA0B;AAanC,IAAM,eAAe;AACrB,IAAM,YAAY;AAElB,SAAS,oBAAoB,MAAc,YAAqD;AAC9F,QAAM,QAAQ,WAAW,IAAI;AAC7B,QAAM,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GO,SAAS,aAAa,WAAoB,MAAoB,SAAuC;AAC1G,QAAM,WAAW,cAAc,MAAM,EAAE,GAAG,SAAS,UAAU,UAAU,cAAc,CAAC;AACtF,QAAM,QAAQ,CAAC,GAAG,SAAS,UAAU;AACrC,YAAU,gBAAgB,QAAQ;AAClC,SAAO,MAAM,MAAM,QAAQ,CAAC,SAAS;AAAE,QAAI,KAAK,eAAe,UAAW,WAAU,YAAY,IAAI;AAAA,EAAG,CAAC;AAC1G;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { ComponentRegistryContract, DesignDocument, PageDocument } from \"@designos/core\";\nimport { renderPageDocument } from \"@designos/renderer-core\";\n\nexport interface DomComponentDefinition {\n tag: string;\n textProp?: string;\n}\nexport type DomComponentMap = Record<string, string | DomComponentDefinition>;\nexport interface DomRenderOptions {\n components: DomComponentMap;\n registry?: ComponentRegistryContract;\n document?: Document;\n}\n\nconst validTagName = /^[A-Za-z][A-Za-z0-9._:-]*$/;\nconst unsafeUrl = /^\\s*(?:javascript|vbscript|data):/i;\n\nfunction componentDefinition(type: string, components: DomComponentMap): DomComponentDefinition {\n const value = components[type];\n const definition = typeof value === \"string\" ? { tag: value } : value;\n return definition && validTagName.test(definition.tag) ? definition : { tag: \"div\" };\n}\n\nfunction setDomProps(element: HTMLElement, props: Record<string, unknown>, textProp?: string): void {\n for (const [key, value] of Object.entries(props)) {\n if ([textProp, \"children\", \"slots\", \"key\", \"ref\"].includes(key) || /^on[a-z]/i.test(key)) continue;\n const name = key === \"className\" ? \"class\" : key;\n if (!/^[A-Za-z_:][A-Za-z0-9:._-]*$/.test(name)) continue;\n if ([\"href\", \"src\", \"action\", \"formaction\", \"xlink:href\"].includes(name.toLowerCase()) && typeof value === \"string\" && unsafeUrl.test(value)) continue;\n if (name === \"style\" && value && typeof value === \"object\" && !Array.isArray(value)) {\n for (const [styleName, styleValue] of Object.entries(value as Record<string, unknown>)) {\n if (styleValue === null || styleValue === undefined || typeof styleValue === \"object\") continue;\n const cssName = styleName.startsWith(\"--\") ? styleName : styleName.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);\n element.style.setProperty(cssName, String(styleValue));\n }\n continue;\n }\n if (value === null || value === undefined) continue;\n if (typeof value === \"string\" || typeof value === \"number\") {\n element.setAttribute(name, String(value));\n if (name in element && !name.startsWith(\"aria-\") && !name.startsWith(\"data-\")) {\n try { Reflect.set(element, name, value); } catch { /* Read-only platform properties remain attributes only. */ }\n }\n } else if (typeof value === \"boolean\") {\n if (value) element.setAttribute(name, name.startsWith(\"aria-\") || name.startsWith(\"data-\") ? \"true\" : \"\");\n else element.removeAttribute(name);\n } else {\n try { Reflect.set(element, name, value); } catch { /* Preserve only values accepted by the native element. */ }\n }\n }\n}\n\n/** Build a browser DOM fragment from a PageDocument and a component-to-tag map. */\nexport function renderDomPage(page: PageDocument, design: DesignDocument, options: DomRenderOptions): DocumentFragment {\n const doc = options.document ?? globalThis.document;\n if (!doc) throw new Error(\"renderDomPage requires a browser Document or options.document\");\n const root = renderPageDocument<HTMLElement>(page, design, {\n renderNode(view) {\n const definition = componentDefinition(view.node.type, options.components);\n const element = doc.createElement(definition.tag);\n setDomProps(element, view.node.props, definition.textProp);\n if (view.slotName) element.setAttribute(\"slot\", view.slotName);\n if (!options.components[view.node.type]) element.setAttribute(\"data-designos-missing\", view.node.type);\n const text = definition.textProp ? view.node.props[definition.textProp] : undefined;\n if (typeof text === \"string\" || typeof text === \"number\") element.appendChild(doc.createTextNode(String(text)));\n for (const child of view.children) element.appendChild(child);\n for (const nodes of Object.values(view.slots)) for (const child of nodes) element.appendChild(child);\n return element;\n },\n }, { registry: options.registry });\n const fragment = doc.createDocumentFragment();\n fragment.appendChild(root);\n return fragment;\n}\n\n/** Replace a container's children with the rendered page; cleanup removes only this render's roots. */\nexport function mountDomPage(container: Element, page: PageDocument, design: DesignDocument, options: DomRenderOptions): () => void {\n const fragment = renderDomPage(page, design, { ...options, document: container.ownerDocument });\n const roots = [...fragment.childNodes];\n container.replaceChildren(fragment);\n return () => roots.forEach((root) => { if (root.parentNode === container) container.removeChild(root); });\n}\n"],"mappings":";AACA,SAAS,0BAA0B;AAanC,IAAM,eAAe;AACrB,IAAM,YAAY;AAElB,SAAS,oBAAoB,MAAc,YAAqD;AAC9F,QAAM,QAAQ,WAAW,IAAI;AAC7B,QAAM,aAAa,OAAO,UAAU,WAAW,EAAE,KAAK,MAAM,IAAI;AAChE,SAAO,cAAc,aAAa,KAAK,WAAW,GAAG,IAAI,aAAa,EAAE,KAAK,MAAM;AACrF;AAEA,SAAS,YAAY,SAAsB,OAAgC,UAAyB;AAClG,aAAW,CAAC,KAAK,KAAK,KAAK,OAAO,QAAQ,KAAK,GAAG;AAChD,QAAI,CAAC,UAAU,YAAY,SAAS,OAAO,KAAK,EAAE,SAAS,GAAG,KAAK,YAAY,KAAK,GAAG,EAAG;AAC1F,UAAM,OAAO,QAAQ,cAAc,UAAU;AAC7C,QAAI,CAAC,+BAA+B,KAAK,IAAI,EAAG;AAChD,QAAI,CAAC,QAAQ,OAAO,UAAU,cAAc,YAAY,EAAE,SAAS,KAAK,YAAY,CAAC,KAAK,OAAO,UAAU,YAAY,UAAU,KAAK,KAAK,EAAG;AAC9I,QAAI,SAAS,WAAW,SAAS,OAAO,UAAU,YAAY,CAAC,MAAM,QAAQ,KAAK,GAAG;AACnF,iBAAW,CAAC,WAAW,UAAU,KAAK,OAAO,QAAQ,KAAgC,GAAG;AACtF,YAAI,eAAe,QAAQ,eAAe,UAAa,OAAO,eAAe,SAAU;AACvF,cAAM,UAAU,UAAU,WAAW,IAAI,IAAI,YAAY,UAAU,QAAQ,UAAU,CAAC,MAAM,IAAI,EAAE,YAAY,CAAC,EAAE;AACjH,gBAAQ,MAAM,YAAY,SAAS,OAAO,UAAU,CAAC;AAAA,MACvD;AACA;AAAA,IACF;AACA,QAAI,UAAU,QAAQ,UAAU,OAAW;AAC3C,QAAI,OAAO,UAAU,YAAY,OAAO,UAAU,UAAU;AAC1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5D,iBAAW,SAAS,OAAO,OAAO,KAAK,KAAK,EAAG,YAAW,SAAS,MAAO,SAAQ,YAAY,KAAK;AACnG,aAAO;AAAA,IACT;AAAA,EACF,GAAG,EAAE,UAAU,QAAQ,SAAS,CAAC;AACjC,QAAM,WAAW,IAAI,uBAAuB;AAC5C,WAAS,YAAY,IAAI;AACzB,SAAO;AACT;AAGO,SAAS,aAAa,WAAoB,MAAoB,QAAwB,SAAuC;AAClI,QAAM,WAAW,cAAc,MAAM,QAAQ,EAAE,GAAG,SAAS,UAAU,UAAU,cAAc,CAAC;AAC9F,QAAM,QAAQ,CAAC,GAAG,SAAS,UAAU;AACrC,YAAU,gBAAgB,QAAQ;AAClC,SAAO,MAAM,MAAM,QAAQ,CAAC,SAAS;AAAE,QAAI,KAAK,eAAe,UAAW,WAAU,YAAY,IAAI;AAAA,EAAG,CAAC;AAC1G;","names":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@designos/renderer-dom",
3
- "version": "0.1.0",
3
+ "version": "1.0.0",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "exports": {
@@ -10,8 +10,8 @@
10
10
  }
11
11
  },
12
12
  "dependencies": {
13
- "@designos/core": "0.1.0",
14
- "@designos/renderer-core": "0.1.0"
13
+ "@designos/core": "0.2.0",
14
+ "@designos/renderer-core": "1.0.0"
15
15
  },
16
16
  "devDependencies": {
17
17
  "jsdom": "^26.1.0",