@designos/renderer-dom 0.0.0-stage → 0.1.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/README.md +4 -2
- package/dist/index.d.ts +18 -0
- package/dist/index.js +77 -0
- package/dist/index.js.map +1 -0
- package/package.json +38 -3
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @designos/renderer-dom
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Shared browser-DOM adapter for target renderers whose runtime components are custom elements or native DOM tags. `renderDomPage` converts a canonical DesignOS `PageDocument` to a `DocumentFragment`; `mountDomPage` replaces a host container and returns a cleanup function.
|
|
4
|
+
|
|
5
|
+
The component map selects a tag name for each authored type, with optional `textProp` handling. Named page regions use native `slot` attributes. The adapter filters inline event-handler props and dangerous URL schemes, preserves property values for complex custom-element props, and uses an explicit missing-component marker. This package depends on core and renderer-core, not compiler packages.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ComponentRegistryContract, PageDocument } from '@designos/core';
|
|
2
|
+
|
|
3
|
+
interface DomComponentDefinition {
|
|
4
|
+
tag: string;
|
|
5
|
+
textProp?: string;
|
|
6
|
+
}
|
|
7
|
+
type DomComponentMap = Record<string, string | DomComponentDefinition>;
|
|
8
|
+
interface DomRenderOptions {
|
|
9
|
+
components: DomComponentMap;
|
|
10
|
+
registry?: ComponentRegistryContract;
|
|
11
|
+
document?: Document;
|
|
12
|
+
}
|
|
13
|
+
/** Build a browser DOM fragment from a PageDocument and a component-to-tag map. */
|
|
14
|
+
declare function renderDomPage(data: PageDocument, options: DomRenderOptions): DocumentFragment;
|
|
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;
|
|
17
|
+
|
|
18
|
+
export { type DomComponentDefinition, type DomComponentMap, type DomRenderOptions, mountDomPage, renderDomPage };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { renderPageDocument } from "@designos/renderer-core";
|
|
3
|
+
var validTagName = /^[A-Za-z][A-Za-z0-9._:-]*$/;
|
|
4
|
+
var unsafeUrl = /^\s*(?:javascript|vbscript|data):/i;
|
|
5
|
+
function componentDefinition(type, components) {
|
|
6
|
+
const value = components[type];
|
|
7
|
+
const definition = typeof value === "string" ? { tag: value } : value;
|
|
8
|
+
return definition && validTagName.test(definition.tag) ? definition : { tag: "div" };
|
|
9
|
+
}
|
|
10
|
+
function setDomProps(element, props, textProp) {
|
|
11
|
+
for (const [key, value] of Object.entries(props)) {
|
|
12
|
+
if ([textProp, "children", "slots", "key", "ref"].includes(key) || /^on[a-z]/i.test(key)) continue;
|
|
13
|
+
const name = key === "className" ? "class" : key;
|
|
14
|
+
if (!/^[A-Za-z_:][A-Za-z0-9:._-]*$/.test(name)) continue;
|
|
15
|
+
if (["href", "src", "action", "formaction", "xlink:href"].includes(name.toLowerCase()) && typeof value === "string" && unsafeUrl.test(value)) continue;
|
|
16
|
+
if (name === "style" && value && typeof value === "object" && !Array.isArray(value)) {
|
|
17
|
+
for (const [styleName, styleValue] of Object.entries(value)) {
|
|
18
|
+
if (styleValue === null || styleValue === void 0 || typeof styleValue === "object") continue;
|
|
19
|
+
const cssName = styleName.startsWith("--") ? styleName : styleName.replace(/[A-Z]/g, (c) => `-${c.toLowerCase()}`);
|
|
20
|
+
element.style.setProperty(cssName, String(styleValue));
|
|
21
|
+
}
|
|
22
|
+
continue;
|
|
23
|
+
}
|
|
24
|
+
if (value === null || value === void 0) continue;
|
|
25
|
+
if (typeof value === "string" || typeof value === "number") {
|
|
26
|
+
element.setAttribute(name, String(value));
|
|
27
|
+
if (name in element && !name.startsWith("aria-") && !name.startsWith("data-")) {
|
|
28
|
+
try {
|
|
29
|
+
Reflect.set(element, name, value);
|
|
30
|
+
} catch {
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
} else if (typeof value === "boolean") {
|
|
34
|
+
if (value) element.setAttribute(name, name.startsWith("aria-") || name.startsWith("data-") ? "true" : "");
|
|
35
|
+
else element.removeAttribute(name);
|
|
36
|
+
} else {
|
|
37
|
+
try {
|
|
38
|
+
Reflect.set(element, name, value);
|
|
39
|
+
} catch {
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function renderDomPage(data, options) {
|
|
45
|
+
const doc = options.document ?? globalThis.document;
|
|
46
|
+
if (!doc) throw new Error("renderDomPage requires a browser Document or options.document");
|
|
47
|
+
const root = renderPageDocument(data, {
|
|
48
|
+
renderNode(view) {
|
|
49
|
+
const definition = componentDefinition(view.node.type, options.components);
|
|
50
|
+
const element = doc.createElement(definition.tag);
|
|
51
|
+
setDomProps(element, view.node.props, definition.textProp);
|
|
52
|
+
if (view.slotName) element.setAttribute("slot", view.slotName);
|
|
53
|
+
if (!options.components[view.node.type]) element.setAttribute("data-designos-missing", view.node.type);
|
|
54
|
+
const text = definition.textProp ? view.node.props[definition.textProp] : void 0;
|
|
55
|
+
if (typeof text === "string" || typeof text === "number") element.appendChild(doc.createTextNode(String(text)));
|
|
56
|
+
for (const child of view.children) element.appendChild(child);
|
|
57
|
+
for (const nodes of Object.values(view.slots)) for (const child of nodes) element.appendChild(child);
|
|
58
|
+
return element;
|
|
59
|
+
}
|
|
60
|
+
}, { registry: options.registry });
|
|
61
|
+
const fragment = doc.createDocumentFragment();
|
|
62
|
+
fragment.appendChild(root);
|
|
63
|
+
return fragment;
|
|
64
|
+
}
|
|
65
|
+
function mountDomPage(container, data, options) {
|
|
66
|
+
const fragment = renderDomPage(data, { ...options, document: container.ownerDocument });
|
|
67
|
+
const roots = [...fragment.childNodes];
|
|
68
|
+
container.replaceChildren(fragment);
|
|
69
|
+
return () => roots.forEach((root) => {
|
|
70
|
+
if (root.parentNode === container) container.removeChild(root);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
export {
|
|
74
|
+
mountDomPage,
|
|
75
|
+
renderDomPage
|
|
76
|
+
};
|
|
77
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +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,QA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AAY,IAAI;AACzB,SAAO;AACT;AAGO,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":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@designos/renderer-dom",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"type": "module",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"import": "./dist/index.js"
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@designos/core": "0.1.0",
|
|
14
|
+
"@designos/renderer-core": "0.1.0"
|
|
15
|
+
},
|
|
16
|
+
"devDependencies": {
|
|
17
|
+
"jsdom": "^26.1.0",
|
|
18
|
+
"tsup": "^8.5.0",
|
|
19
|
+
"typescript": "^5.9.2",
|
|
20
|
+
"vitest": "^3.2.4"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"dist",
|
|
24
|
+
"README.md"
|
|
25
|
+
],
|
|
26
|
+
"main": "./dist/index.js",
|
|
27
|
+
"types": "./dist/index.d.ts",
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"repository": {
|
|
32
|
+
"type": "git",
|
|
33
|
+
"url": "git+https://github.com/relacloud/design-os.git",
|
|
34
|
+
"directory": "packages/renderer-dom"
|
|
35
|
+
},
|
|
36
|
+
"scripts": {
|
|
37
|
+
"typecheck": "tsc --noEmit",
|
|
38
|
+
"build": "tsup",
|
|
39
|
+
"test": "vitest"
|
|
40
|
+
}
|
|
6
41
|
}
|