@designos/renderer-dom 0.1.0 → 1.0.1
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/LICENSE +21 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 relacloud
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
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(
|
|
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,
|
|
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(
|
|
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(
|
|
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,
|
|
66
|
-
const fragment = renderDomPage(
|
|
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(
|
|
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.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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,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@designos/renderer-dom",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"license": "MIT",
|
|
4
5
|
"private": false,
|
|
5
6
|
"type": "module",
|
|
6
7
|
"exports": {
|
|
@@ -10,8 +11,8 @@
|
|
|
10
11
|
}
|
|
11
12
|
},
|
|
12
13
|
"dependencies": {
|
|
13
|
-
"@designos/core": "0.1
|
|
14
|
-
"@designos/renderer-core": "0.1
|
|
14
|
+
"@designos/core": "0.2.1",
|
|
15
|
+
"@designos/renderer-core": "1.0.1"
|
|
15
16
|
},
|
|
16
17
|
"devDependencies": {
|
|
17
18
|
"jsdom": "^26.1.0",
|