@perkyzz/emvb 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.
Files changed (170) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +57 -2
  3. package/package.json +72 -4
  4. package/src/admin/access/RequireEditor.tsx +39 -0
  5. package/src/admin/access/role.ts +36 -0
  6. package/src/admin/access/sidebar-shim.ts +108 -0
  7. package/src/admin/api.ts +48 -0
  8. package/src/admin/content-api.ts +147 -0
  9. package/src/admin/editor/Editor.tsx +457 -0
  10. package/src/admin/editor/EditorOverlay.tsx +54 -0
  11. package/src/admin/editor/ShortcutsDialog.tsx +30 -0
  12. package/src/admin/editor/SmallScreenNotice.tsx +20 -0
  13. package/src/admin/editor/TopBar.tsx +179 -0
  14. package/src/admin/editor/canvas/CanvasFrame.tsx +355 -0
  15. package/src/admin/editor/canvas/CanvasTextEdit.tsx +87 -0
  16. package/src/admin/editor/canvas/DeviceBar.tsx +50 -0
  17. package/src/admin/editor/canvas/EmptyCanvas.tsx +49 -0
  18. package/src/admin/editor/canvas/SelectionOverlay.tsx +185 -0
  19. package/src/admin/editor/canvas/canvas-events.ts +232 -0
  20. package/src/admin/editor/canvas/state-preview.ts +19 -0
  21. package/src/admin/editor/canvas/tab-reveal.ts +35 -0
  22. package/src/admin/editor/canvas/vnode-react.tsx +49 -0
  23. package/src/admin/editor/clipboard-store.ts +44 -0
  24. package/src/admin/editor/dialogs.tsx +125 -0
  25. package/src/admin/editor/dnd/drag-stash.ts +27 -0
  26. package/src/admin/editor/dnd/drop-target.ts +101 -0
  27. package/src/admin/editor/dnd/new-element.ts +32 -0
  28. package/src/admin/editor/editor-css.ts +539 -0
  29. package/src/admin/editor/exit.ts +27 -0
  30. package/src/admin/editor/panels/AddPanel.tsx +142 -0
  31. package/src/admin/editor/panels/ClassesSection.tsx +334 -0
  32. package/src/admin/editor/panels/ColorControl.tsx +230 -0
  33. package/src/admin/editor/panels/ConditionsEditor.tsx +201 -0
  34. package/src/admin/editor/panels/ElementPanel.tsx +629 -0
  35. package/src/admin/editor/panels/LayersPanel.tsx +336 -0
  36. package/src/admin/editor/panels/LeftPanel.tsx +77 -0
  37. package/src/admin/editor/panels/PageSettings.tsx +126 -0
  38. package/src/admin/editor/panels/SettingsPanel.tsx +101 -0
  39. package/src/admin/editor/panels/SiteStylesDrawer.tsx +392 -0
  40. package/src/admin/editor/panels/TriggerSettings.tsx +132 -0
  41. package/src/admin/editor/panels/TriggersEditor.tsx +197 -0
  42. package/src/admin/editor/panels/VariableSection.tsx +492 -0
  43. package/src/admin/editor/panels/settings/BackgroundControls.tsx +320 -0
  44. package/src/admin/editor/panels/settings/ClassChip.tsx +167 -0
  45. package/src/admin/editor/panels/settings/ClassChipInput.tsx +349 -0
  46. package/src/admin/editor/panels/settings/DraftTextField.tsx +201 -0
  47. package/src/admin/editor/panels/settings/EffectsControls.tsx +347 -0
  48. package/src/admin/editor/panels/settings/FieldBindControl.tsx +83 -0
  49. package/src/admin/editor/panels/settings/FieldControl.tsx +150 -0
  50. package/src/admin/editor/panels/settings/FormBindControl.tsx +97 -0
  51. package/src/admin/editor/panels/settings/IconPicker.tsx +109 -0
  52. package/src/admin/editor/panels/settings/LengthRow.tsx +181 -0
  53. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +124 -0
  54. package/src/admin/editor/panels/settings/MediaPicker.tsx +218 -0
  55. package/src/admin/editor/panels/settings/NumberRow.tsx +154 -0
  56. package/src/admin/editor/panels/settings/StateSwitcher.tsx +87 -0
  57. package/src/admin/editor/panels/settings/StyleRow.tsx +480 -0
  58. package/src/admin/editor/panels/settings/VariableBinding.tsx +167 -0
  59. package/src/admin/editor/panels/settings/class-suggest.ts +51 -0
  60. package/src/admin/editor/panels/settings/length-units.ts +83 -0
  61. package/src/admin/editor/panels/settings/node-props.ts +11 -0
  62. package/src/admin/editor/panels/settings/style-sections.ts +291 -0
  63. package/src/admin/editor/panels/site-list.tsx +206 -0
  64. package/src/admin/editor/panels/variable-values.ts +73 -0
  65. package/src/admin/editor/section-templates.ts +50 -0
  66. package/src/admin/editor/shortcuts.ts +20 -0
  67. package/src/admin/editor/store.ts +212 -0
  68. package/src/admin/editor/useClipboardActions.ts +136 -0
  69. package/src/admin/editor/useEditorCommands.ts +142 -0
  70. package/src/admin/editor/useEditorData.ts +167 -0
  71. package/src/admin/editor/useEditorShortcuts.ts +148 -0
  72. package/src/admin/editor/useMediaQuery.ts +20 -0
  73. package/src/admin/editor/useNodeActions.ts +122 -0
  74. package/src/admin/editor/useSave.ts +185 -0
  75. package/src/admin/forms-api.ts +73 -0
  76. package/src/admin/index.tsx +20 -0
  77. package/src/admin/media-api.ts +68 -0
  78. package/src/admin/pages/CreateDialog.tsx +150 -0
  79. package/src/admin/pages/EditorPage.tsx +57 -0
  80. package/src/admin/pages/NewPageDialog.tsx +83 -0
  81. package/src/admin/pages/NewThemePartDialog.tsx +61 -0
  82. package/src/admin/pages/PageList.tsx +71 -0
  83. package/src/admin/pages/PagesPage.tsx +25 -0
  84. package/src/admin/pages/SetupShell.tsx +90 -0
  85. package/src/admin/pages/ThemeBuilderPage.tsx +25 -0
  86. package/src/admin/pages/ThemePartList.tsx +202 -0
  87. package/src/admin/pages/list-kit.tsx +75 -0
  88. package/src/admin/setup/plan.ts +150 -0
  89. package/src/admin/setup/run.ts +61 -0
  90. package/src/admin/setup/spec.ts +100 -0
  91. package/src/admin/setup/useSetupStatus.ts +62 -0
  92. package/src/admin/theme-api.ts +186 -0
  93. package/src/admin/ui.ts +231 -0
  94. package/src/admin/widgets/LayoutField.tsx +67 -0
  95. package/src/astro/EmVBPage.astro +37 -0
  96. package/src/astro/env.d.ts +5 -0
  97. package/src/astro/forms/EmVBFormsRuntime.astro +9 -0
  98. package/src/astro/forms/index.ts +1 -0
  99. package/src/astro/forms-definitions.ts +104 -0
  100. package/src/astro/index.ts +15 -0
  101. package/src/astro/popups/EmVBPopupsRuntime.astro +9 -0
  102. package/src/astro/popups/index.ts +1 -0
  103. package/src/astro/popups/runtime.ts +246 -0
  104. package/src/astro/render.ts +74 -0
  105. package/src/astro/resolve-theme.ts +371 -0
  106. package/src/astro/resolve.ts +65 -0
  107. package/src/astro/tabs/EmVBTabsRuntime.astro +10 -0
  108. package/src/astro/tabs/index.ts +1 -0
  109. package/src/astro/tabs/runtime.ts +111 -0
  110. package/src/astro/theme-context.ts +81 -0
  111. package/src/astro/theme-posts.ts +84 -0
  112. package/src/constants.ts +12 -0
  113. package/src/core/arrange.ts +360 -0
  114. package/src/core/clipboard.ts +422 -0
  115. package/src/core/css/generate.ts +219 -0
  116. package/src/core/css/scope.ts +88 -0
  117. package/src/core/design/cascade.ts +28 -0
  118. package/src/core/design/classes.ts +138 -0
  119. package/src/core/design/devices.ts +56 -0
  120. package/src/core/design/states.ts +49 -0
  121. package/src/core/design/transfer.ts +33 -0
  122. package/src/core/design/variables.ts +319 -0
  123. package/src/core/elements/definition.ts +12 -0
  124. package/src/core/elements/dynamic-elements.ts +173 -0
  125. package/src/core/elements/form-elements.ts +204 -0
  126. package/src/core/elements/index.ts +742 -0
  127. package/src/core/elements/plain-text.ts +32 -0
  128. package/src/core/forms/binding.ts +61 -0
  129. package/src/core/forms/definition.ts +34 -0
  130. package/src/core/forms/success-layout.ts +63 -0
  131. package/src/core/icons/catalog.ts +273 -0
  132. package/src/core/index.ts +316 -0
  133. package/src/core/limits.ts +7 -0
  134. package/src/core/migrate/index.ts +76 -0
  135. package/src/core/render/context.ts +14 -0
  136. package/src/core/render/dynamic-post.ts +147 -0
  137. package/src/core/render/extras.ts +79 -0
  138. package/src/core/render/form.ts +137 -0
  139. package/src/core/render/index.ts +274 -0
  140. package/src/core/render/loop.ts +36 -0
  141. package/src/core/render/section.ts +33 -0
  142. package/src/core/render/tabs.ts +72 -0
  143. package/src/core/render/vnode.ts +87 -0
  144. package/src/core/sanitize/css.ts +591 -0
  145. package/src/core/sanitize/embed-url.ts +103 -0
  146. package/src/core/sanitize/escape.ts +10 -0
  147. package/src/core/sanitize/href.ts +6 -0
  148. package/src/core/sanitize/media-url.ts +7 -0
  149. package/src/core/sanitize/svg.ts +293 -0
  150. package/src/core/sanitize/url.ts +31 -0
  151. package/src/core/schema/descriptors.ts +33 -0
  152. package/src/core/schema/design.ts +88 -0
  153. package/src/core/schema/layout.ts +817 -0
  154. package/src/core/schema/state-names.ts +4 -0
  155. package/src/core/schema/style.ts +257 -0
  156. package/src/core/stats.ts +18 -0
  157. package/src/core/tabs/presence.ts +16 -0
  158. package/src/core/theme/conditions.ts +321 -0
  159. package/src/core/theme/dynamic.ts +133 -0
  160. package/src/core/theme/parse-doc.ts +36 -0
  161. package/src/core/theme/part-types.ts +79 -0
  162. package/src/core/theme/popup-markup.ts +39 -0
  163. package/src/core/theme/popup-rules.ts +61 -0
  164. package/src/core/theme/triggers.ts +104 -0
  165. package/src/core/tree-ops.ts +139 -0
  166. package/src/core/validate.ts +185 -0
  167. package/src/index.ts +18 -0
  168. package/src/server/design-routes.ts +125 -0
  169. package/src/server/hooks.ts +98 -0
  170. package/src/server/plugin.ts +39 -0
@@ -0,0 +1,137 @@
1
+ import { fieldByName, type FormDefinitions } from "../forms/definition.ts";
2
+ import type { FormNode, LayoutNode } from "../schema/layout.ts";
3
+ import type { ThemeDynamicData } from "../theme/dynamic.ts";
4
+ import type { RenderContext } from "./context.ts";
5
+ import type { VNode } from "./vnode.ts";
6
+
7
+ export const FORMS_SUBMIT_PATH = "/_emdash/api/plugins/emdash-forms/submit";
8
+
9
+ const honeypot = (formId: string): VNode => ({
10
+ tag: "div",
11
+ attrs: {
12
+ class: "ec-form-hp",
13
+ "aria-hidden": "true",
14
+ },
15
+ children: [
16
+ {
17
+ tag: "label",
18
+ attrs: { for: `${formId}-_hp` },
19
+ children: ["Leave blank"],
20
+ },
21
+ {
22
+ tag: "input",
23
+ attrs: {
24
+ type: "text",
25
+ id: `${formId}-_hp`,
26
+ name: "_hp",
27
+ tabindex: "-1",
28
+ autocomplete: "off",
29
+ },
30
+ children: [],
31
+ },
32
+ ],
33
+ });
34
+
35
+ /** An EmDash Forms markup shell (D-006) around the form's fields; unbound forms only preview in the editor. */
36
+ export function renderForm(
37
+ node: FormNode,
38
+ attrs: Record<string, string>,
39
+ ctx: RenderContext,
40
+ dynamic: ThemeDynamicData | undefined,
41
+ ): VNode | undefined {
42
+ const formId = node.props.formId.trim();
43
+ // Unbound forms: no live <form>/ec-form shell (avoids broken preview; public stays zero-JS).
44
+ if (!formId) {
45
+ if (ctx.mode === "public") return undefined;
46
+ return {
47
+ tag: "div",
48
+ attrs: {
49
+ ...attrs,
50
+ class: `${attrs.class} emvb-form-unbound`.trim(),
51
+ "data-emvb-form-unbound": "",
52
+ },
53
+ children: ["Bind a form in settings to preview it here."],
54
+ };
55
+ }
56
+ const submitLabel = ctx.definitions.get(formId)?.settings.submitLabel ?? "Submit";
57
+ const page: VNode = {
58
+ tag: "div",
59
+ attrs: { "data-page": "0" },
60
+ children: ctx.children(node.children, dynamic),
61
+ };
62
+ return {
63
+ tag: "form",
64
+ attrs: {
65
+ ...attrs,
66
+ class: `${attrs.class} ec-form`.trim(),
67
+ method: "POST",
68
+ action: FORMS_SUBMIT_PATH,
69
+ "data-ec-form": "",
70
+ "data-form-id": formId,
71
+ "data-submit-label": submitLabel,
72
+ },
73
+ children: [
74
+ page,
75
+ { tag: "input", attrs: { type: "hidden", name: "formId", value: formId }, children: [] },
76
+ honeypot(formId),
77
+ {
78
+ tag: "div",
79
+ attrs: { class: "ec-form-status", "data-form-status": "", "aria-live": "polite" },
80
+ children: [],
81
+ },
82
+ ],
83
+ };
84
+ }
85
+
86
+ /** Replaces the children of every node matching `match`, anywhere in the tree. */
87
+ function replaceChildren(vnode: VNode, match: (node: VNode) => boolean, children: VNode[]): VNode {
88
+ if (match(vnode)) return { ...vnode, children };
89
+ return {
90
+ ...vnode,
91
+ children: vnode.children.map((child) =>
92
+ typeof child === "string" ? child : replaceChildren(child, match, children),
93
+ ),
94
+ };
95
+ }
96
+
97
+ /** Fills a select or radio field with the options of the first bound form definition that has it. */
98
+ export function withFieldOptions(
99
+ vnode: VNode,
100
+ node: LayoutNode,
101
+ definitions: FormDefinitions,
102
+ ): VNode {
103
+ if (node.type !== "select" && node.type !== "radio") return vnode;
104
+ const field = (node.props as { field: string }).field;
105
+ for (const definition of definitions.values()) {
106
+ const meta = fieldByName(definition, field);
107
+ if (!meta) continue;
108
+ if (!meta.options?.length) return vnode;
109
+ if (node.type === "select") {
110
+ return replaceChildren(vnode, (child) => child.tag === "select", [
111
+ { tag: "option", attrs: { value: "" }, children: ["Choose…"] },
112
+ ...meta.options.map((option): VNode => ({
113
+ tag: "option",
114
+ attrs: { value: option.value },
115
+ children: [option.label],
116
+ })),
117
+ ]);
118
+ }
119
+ return replaceChildren(
120
+ vnode,
121
+ (child) => child.tag === "fieldset" && (child.attrs.class ?? "").includes("emvb-radio-group"),
122
+ meta.options.map((option) => ({
123
+ tag: "label",
124
+ attrs: { class: "emvb-form-radio-label" },
125
+ children: [
126
+ {
127
+ tag: "input",
128
+ attrs: { type: "radio", name: field, value: option.value },
129
+ children: [],
130
+ },
131
+ ` ${option.label}`,
132
+ ],
133
+ })),
134
+ );
135
+ }
136
+ return vnode;
137
+ }
@@ -0,0 +1,274 @@
1
+ import { generateCss } from "../css/generate.ts";
2
+ import { cssScopeToken, scopeAttribute, scopeCss, scopeWrapperCss } from "../css/scope.ts";
3
+ import { ELEMENTS } from "../elements/index.ts";
4
+ import { layoutHasForm } from "../forms/binding.ts";
5
+ import { layoutHasTabs } from "../tabs/presence.ts";
6
+ import type { DesignSystem } from "../schema/design.ts";
7
+ import {
8
+ isFormNode,
9
+ isLoopNode,
10
+ isParentNode,
11
+ isSectionNode,
12
+ isTabsNode,
13
+ type Layout,
14
+ type LayoutNode,
15
+ } from "../schema/layout.ts";
16
+ import {
17
+ cssLength,
18
+ stateDeclarations,
19
+ styleClassName,
20
+ styleDeclarations,
21
+ type Declaration,
22
+ type StateDeclarations,
23
+ } from "../sanitize/css.ts";
24
+ import { STYLE_STATES } from "../schema/state-names.ts";
25
+ import { resolveEmbedUrl } from "../sanitize/embed-url.ts";
26
+ import { resolvePostForRender, type ThemeDynamicData } from "../theme/dynamic.ts";
27
+ import type { FormDefinitions } from "../forms/definition.ts";
28
+ import type { PopupDevice } from "../theme/popup-rules.ts";
29
+ import type { RenderContext, RenderMode } from "./context.ts";
30
+ import { isDynamicPostNode, renderDynamicPost } from "./dynamic-post.ts";
31
+ import { renderForm, withFieldOptions } from "./form.ts";
32
+ import { renderLoop } from "./loop.ts";
33
+ import { renderSection } from "./section.ts";
34
+ import { renderTabs } from "./tabs.ts";
35
+ import { serialize, type VNode } from "./vnode.ts";
36
+ import { classStylesInListOrder } from "../design/cascade.ts";
37
+ import {
38
+ applyBackgroundVideo,
39
+ applyBoxLink,
40
+ customAttributes,
41
+ safeBackgroundVideo,
42
+ } from "./extras.ts";
43
+
44
+ export type { RenderMode } from "./context.ts";
45
+ export { FORMS_SUBMIT_PATH } from "./form.ts";
46
+
47
+ export type RenderWarning = {
48
+ nodeId: string;
49
+ code: "unknown-type" | "rejected-style" | "unknown-variable";
50
+ detail: string;
51
+ };
52
+ export type RenderResult = {
53
+ vnode: VNode;
54
+ html: string;
55
+ css: string;
56
+ needsFormsRuntime: boolean;
57
+ needsTabsRuntime: boolean;
58
+ warnings: RenderWarning[];
59
+ };
60
+
61
+ const ID = /^[A-Za-z0-9_-]{4,24}$/;
62
+ const HTML_ID = /^[A-Za-z][A-Za-z0-9_-]{0,63}$/;
63
+
64
+ // Canvas iframe has no allow-scripts (D-011): live YouTube/Vimeo players fail there.
65
+ function videoPreview(node: LayoutNode, attrs: Record<string, string>): VNode | undefined {
66
+ const target = resolveEmbedUrl((node.props as { url: string }).url);
67
+ if (target?.kind !== "iframe") return undefined;
68
+ const label =
69
+ (node.props as { title?: string }).title?.trim() ||
70
+ (target.provider === "youtube" ? "YouTube video" : "Vimeo video");
71
+ return {
72
+ tag: "div",
73
+ attrs: {
74
+ ...attrs,
75
+ class: `${attrs.class} emvb-video-preview`.trim(),
76
+ "data-emvb-video-preview": target.provider,
77
+ },
78
+ children: [`${label} — plays on the published page`],
79
+ };
80
+ }
81
+
82
+ /**
83
+ * Renders a validated layout to lean HTML and CSS (R-031, R-032). Walks defensively: unknown types
84
+ * render nothing publicly and a placeholder in the editor (R-033); unsafe style values are dropped.
85
+ */
86
+ export function renderPage(
87
+ layout: Layout,
88
+ design: DesignSystem,
89
+ opts: {
90
+ mode?: RenderMode;
91
+ formDefinitions?: FormDefinitions;
92
+ dynamic?: ThemeDynamicData;
93
+ /** Public renders: wrap the markup and scope the CSS to it, so sheets can't override each other (W-112). */
94
+ scope?: string;
95
+ /** Editor canvas: device rules follow this device, not the frame width (W-116). */
96
+ previewDevice?: PopupDevice;
97
+ } = {},
98
+ ): RenderResult {
99
+ const mode = opts.mode ?? "public";
100
+ const baseDynamic = opts.dynamic;
101
+ const warnings: RenderWarning[] = [];
102
+ const usedTypes = new Set<string>();
103
+ const localRules: {
104
+ id: string;
105
+ declarations: Declaration[];
106
+ states: StateDeclarations;
107
+ tablet?: Declaration[];
108
+ mobile?: Declaration[];
109
+ hiddenOn?: LayoutNode["hiddenOn"];
110
+ }[] = [];
111
+ const knownVariables = new Set(design.variables.colors.map((c) => c.id));
112
+ let backgroundVideo = false;
113
+
114
+ const ctx: RenderContext = {
115
+ mode,
116
+ definitions: opts.formDefinitions ?? new Map(),
117
+ children: (nodes, dynamic) =>
118
+ nodes
119
+ .map((child) => visit(child, false, dynamic))
120
+ .filter((child): child is VNode => child !== undefined),
121
+ };
122
+
123
+ const nodeAttrs = (
124
+ node: LayoutNode,
125
+ isRoot: boolean,
126
+ id: string | undefined,
127
+ nodeId: string,
128
+ ): Record<string, string> => {
129
+ const classes = [...(isRoot ? ["emvb-root"] : []), `emvb-${node.type}`];
130
+ for (const classId of node.classes ?? []) {
131
+ const name = styleClassName(classId);
132
+ if (name) classes.push(name);
133
+ }
134
+ const { declarations, rejected } = styleDeclarations(node.style);
135
+ const tablet = styleDeclarations(node.devices?.tablet);
136
+ const mobile = styleDeclarations(node.devices?.mobile);
137
+ for (const key of [...rejected, ...tablet.rejected, ...mobile.rejected]) {
138
+ warnings.push({ nodeId, code: "rejected-style", detail: key });
139
+ }
140
+ if (node.type === "spacer") {
141
+ const height = cssLength((node as { props: { height: unknown } }).props.height);
142
+ if (height) declarations.push({ property: "height", value: height });
143
+ }
144
+ if (node.type === "grid") {
145
+ const columns = (node as { props: { columns: unknown } }).props.columns;
146
+ if (
147
+ typeof columns === "number" &&
148
+ Number.isInteger(columns) &&
149
+ columns >= 1 &&
150
+ columns <= 12
151
+ ) {
152
+ declarations.push({
153
+ property: "grid-template-columns",
154
+ value: `repeat(${columns}, minmax(0, 1fr))`,
155
+ });
156
+ }
157
+ }
158
+ const states = stateDeclarations(node.states);
159
+ for (const key of states.rejected) {
160
+ warnings.push({ nodeId, code: "rejected-style", detail: key });
161
+ }
162
+ for (const color of [node.style?.color, ...STYLE_STATES.map((s) => node.states?.[s]?.color)]) {
163
+ if (typeof color === "object" && !knownVariables.has(color.var)) {
164
+ warnings.push({ nodeId, code: "unknown-variable", detail: color.var });
165
+ }
166
+ }
167
+ const hiddenOn = node.hiddenOn;
168
+ const responsive =
169
+ tablet.declarations.length > 0 ||
170
+ mobile.declarations.length > 0 ||
171
+ (hiddenOn?.length ?? 0) > 0;
172
+ if (id && (declarations.length > 0 || Object.keys(states.states).length > 0 || responsive)) {
173
+ classes.push(`emvb-e-${id}`);
174
+ localRules.push({
175
+ id,
176
+ declarations,
177
+ states: states.states,
178
+ tablet: tablet.declarations,
179
+ mobile: mobile.declarations,
180
+ hiddenOn,
181
+ });
182
+ }
183
+ const attrs: Record<string, string> = { class: classes.join(" "), ...customAttributes(node) };
184
+ if (mode === "editor" && id) attrs["data-emvb-id"] = id;
185
+ if (node.htmlId && HTML_ID.test(node.htmlId)) attrs.id = node.htmlId;
186
+ return attrs;
187
+ };
188
+
189
+ function visit(
190
+ node: LayoutNode,
191
+ isRoot: boolean,
192
+ dynamic: ThemeDynamicData | undefined = baseDynamic,
193
+ ): VNode | undefined {
194
+ const id = ID.test(node.id) ? node.id : undefined;
195
+ const nodeId = id ?? "(invalid id)";
196
+ const type: string = node.type;
197
+ if (!Object.hasOwn(ELEMENTS, type)) {
198
+ warnings.push({ nodeId, code: "unknown-type", detail: type });
199
+ if (mode === "public") return undefined;
200
+ return {
201
+ tag: "div",
202
+ attrs: { class: "emvb-unknown", ...(id ? { "data-emvb-id": id } : {}) },
203
+ children: [`Unknown element "${type}"`],
204
+ };
205
+ }
206
+ usedTypes.add(type);
207
+ const attrs = nodeAttrs(node, isRoot, id, nodeId);
208
+ const def = ELEMENTS[type as keyof typeof ELEMENTS];
209
+ if (isFormNode(node)) return finish(node, renderForm(node, attrs, ctx, dynamic));
210
+ if (isLoopNode(node)) return finish(node, renderLoop(node, attrs, ctx, dynamic));
211
+ if (isSectionNode(node)) return finish(node, renderSection(node, attrs, ctx, dynamic));
212
+ if (isDynamicPostNode(node)) {
213
+ return finish(
214
+ node,
215
+ renderDynamicPost(node, attrs, resolvePostForRender(dynamic, mode), mode),
216
+ );
217
+ }
218
+ if (isTabsNode(node)) return finish(node, renderTabs(node, id, attrs, ctx, dynamic));
219
+ if (isParentNode(node)) {
220
+ return finish(node, def.build(node as never, attrs, ctx.children(node.children, dynamic)));
221
+ }
222
+ if (mode === "editor" && node.type === "video") {
223
+ const preview = videoPreview(node, attrs);
224
+ if (preview) return finish(node, preview);
225
+ }
226
+ return finish(
227
+ node,
228
+ withFieldOptions(def.build(node as never, attrs, []), node, ctx.definitions),
229
+ );
230
+ }
231
+
232
+ function videoOf(node: LayoutNode): string | undefined {
233
+ let found: string | undefined;
234
+ for (const style of classStylesInListOrder(design.classes, node.classes ?? [])) {
235
+ const safe = safeBackgroundVideo(style?.backgroundVideo);
236
+ if (safe) found = safe;
237
+ }
238
+ const own = safeBackgroundVideo(node.style?.backgroundVideo);
239
+ return own ?? found;
240
+ }
241
+
242
+ function finish(node: LayoutNode, vnode: VNode | undefined): VNode | undefined {
243
+ if (!vnode) return undefined;
244
+ const src = videoOf(node);
245
+ if (src) backgroundVideo = true;
246
+ return applyBackgroundVideo(applyBoxLink(node, vnode), src);
247
+ }
248
+
249
+ const vnode = visit(layout.root, true) ?? {
250
+ tag: "div",
251
+ attrs: { class: "emvb-root" },
252
+ children: [],
253
+ };
254
+ const baseCss = new Map(Object.entries(ELEMENTS).map(([type, def]) => [type, def.baseCss]));
255
+ const html = serialize(vnode);
256
+ const css = generateCss({
257
+ design,
258
+ usedTypes,
259
+ baseCss,
260
+ localRules,
261
+ previewStates: mode === "editor",
262
+ backgroundVideo,
263
+ ...(mode === "editor" && opts.previewDevice ? { previewDevice: opts.previewDevice } : {}),
264
+ });
265
+ const scope = opts.scope === undefined ? undefined : cssScopeToken(opts.scope);
266
+ return {
267
+ vnode,
268
+ html: scope ? `<div ${scopeAttribute(scope)}>${html}</div>` : html,
269
+ css: scope ? scopeWrapperCss() + scopeCss(css, scope) : css,
270
+ needsFormsRuntime: layoutHasForm(layout),
271
+ needsTabsRuntime: layoutHasTabs(layout),
272
+ warnings,
273
+ };
274
+ }
@@ -0,0 +1,36 @@
1
+ import { ELEMENTS } from "../elements/index.ts";
2
+ import type { LayoutNode, LoopNode } from "../schema/layout.ts";
3
+ import { resolvePostsForLoop, type ThemeDynamicData } from "../theme/dynamic.ts";
4
+ import type { RenderContext } from "./context.ts";
5
+ import type { VNode } from "./vnode.ts";
6
+
7
+ /** One item per post, from the chosen loop-item part or the loop's own children. */
8
+ export function renderLoop(
9
+ node: LoopNode,
10
+ attrs: Record<string, string>,
11
+ ctx: RenderContext,
12
+ dynamic: ThemeDynamicData | undefined,
13
+ ): VNode | undefined {
14
+ const posts = resolvePostsForLoop(dynamic, ctx.mode);
15
+ const itemPartId = node.props.itemPartId?.trim() ?? "";
16
+ const templateLayout =
17
+ itemPartId && dynamic?.loopTemplates ? dynamic.loopTemplates[itemPartId] : undefined;
18
+ const templateNodes: LayoutNode[] = templateLayout ? templateLayout.root.children : node.children;
19
+ if (posts.length === 0) {
20
+ if (ctx.mode !== "editor") return undefined;
21
+ return {
22
+ tag: "div",
23
+ attrs: { ...attrs, "data-emvb-loop-empty": "" },
24
+ children: ["Loop — add posts on the public archive to see items."],
25
+ };
26
+ }
27
+ const children: VNode[] = [];
28
+ for (const post of posts) {
29
+ children.push({
30
+ tag: "div",
31
+ attrs: { class: "emvb-loop-item", "data-emvb-loop-item": post.id },
32
+ children: ctx.children(templateNodes, { ...dynamic, post }),
33
+ });
34
+ }
35
+ return ELEMENTS.loop.build(node as never, attrs, children);
36
+ }
@@ -0,0 +1,33 @@
1
+ import { ELEMENTS } from "../elements/index.ts";
2
+ import type { SectionNode } from "../schema/layout.ts";
3
+ import type { ThemeDynamicData } from "../theme/dynamic.ts";
4
+ import type { RenderContext } from "./context.ts";
5
+ import type { VNode } from "./vnode.ts";
6
+
7
+ const MAX_SECTION_DEPTH = 8;
8
+
9
+ /**
10
+ * A section with `partId` renders that theme part's children.
11
+ * A missing part stays empty on the public site (local children would drift from the source).
12
+ */
13
+ export function renderSection(
14
+ node: SectionNode,
15
+ attrs: Record<string, string>,
16
+ ctx: RenderContext,
17
+ dynamic: ThemeDynamicData | undefined,
18
+ ): VNode {
19
+ const partId = node.props.partId?.trim() ?? "";
20
+ const stack = dynamic?.sectionStack ?? [];
21
+ const template =
22
+ partId && dynamic?.sectionTemplates ? dynamic.sectionTemplates[partId] : undefined;
23
+ if (!partId) {
24
+ return ELEMENTS.section.build(node as never, attrs, ctx.children(node.children, dynamic));
25
+ }
26
+ if (!template || stack.includes(partId) || stack.length >= MAX_SECTION_DEPTH) {
27
+ if (ctx.mode !== "editor") return ELEMENTS.section.build(node as never, attrs, []);
28
+ return { tag: "div", attrs, children: ["Synced section"] };
29
+ }
30
+ const templateNodes = template.root.children;
31
+ const next: ThemeDynamicData = { ...dynamic, sectionStack: [...stack, partId] };
32
+ return ELEMENTS.section.build(node as never, attrs, ctx.children(templateNodes, next));
33
+ }
@@ -0,0 +1,72 @@
1
+ import { isTabPanelNode, type TabsNode } from "../schema/layout.ts";
2
+ import type { ThemeDynamicData } from "../theme/dynamic.ts";
3
+ import type { RenderContext } from "./context.ts";
4
+ import type { VNode } from "./vnode.ts";
5
+
6
+ const MAX_TABS = 12;
7
+
8
+ /** CSS-only tabs (radio inputs and labels) that the optional tabs script enhances. */
9
+ export function renderTabs(
10
+ node: TabsNode,
11
+ id: string | undefined,
12
+ attrs: Record<string, string>,
13
+ ctx: RenderContext,
14
+ dynamic: ThemeDynamicData | undefined,
15
+ ): VNode {
16
+ const panels = node.children.filter(isTabPanelNode).slice(0, MAX_TABS);
17
+ const group = `emvb-tabs-${id ?? "x"}`;
18
+ const inputs: VNode[] = [];
19
+ const labels: VNode[] = [];
20
+ const panelNodes: VNode[] = [];
21
+ panels.forEach((panel, i) => {
22
+ const inputId = `${group}-${i}`;
23
+ const inputAttrs: Record<string, string> = {
24
+ type: "radio",
25
+ name: group,
26
+ id: inputId,
27
+ class: "emvb-tab-input",
28
+ };
29
+ if (i === 0) inputAttrs.checked = "checked";
30
+ inputs.push({ tag: "input", attrs: inputAttrs, children: [] });
31
+ const panelDomId = `${group}-panel-${i}`;
32
+ const tabDomId = `${group}-tab-${i}`;
33
+ labels.push({
34
+ tag: "label",
35
+ attrs: {
36
+ class: "emvb-tab-label",
37
+ for: inputId,
38
+ id: tabDomId,
39
+ role: "tab",
40
+ "aria-selected": i === 0 ? "true" : "false",
41
+ "aria-controls": panelDomId,
42
+ tabindex: i === 0 ? "0" : "-1",
43
+ },
44
+ children: [panel.props.label],
45
+ });
46
+ const panelAttrs: Record<string, string> = {
47
+ class: "emvb-tab-panel",
48
+ id: panelDomId,
49
+ role: "tabpanel",
50
+ "aria-labelledby": tabDomId,
51
+ };
52
+ if (ctx.mode === "editor") panelAttrs["data-emvb-id"] = panel.id;
53
+ panelNodes.push({
54
+ tag: "div",
55
+ attrs: panelAttrs,
56
+ children: ctx.children(panel.children, dynamic),
57
+ });
58
+ });
59
+ return {
60
+ tag: "div",
61
+ attrs,
62
+ children: [
63
+ ...inputs,
64
+ {
65
+ tag: "div",
66
+ attrs: { class: "emvb-tab-list", role: "tablist", "aria-orientation": "horizontal" },
67
+ children: labels,
68
+ },
69
+ { tag: "div", attrs: { class: "emvb-tab-panels" }, children: panelNodes },
70
+ ],
71
+ };
72
+ }
@@ -0,0 +1,87 @@
1
+ import { escapeAttr, escapeText } from "../sanitize/escape.ts";
2
+
3
+ export type VNode = { tag: string; attrs: Record<string, string>; children: (VNode | string)[] };
4
+
5
+ const TAGS = new Set([
6
+ "div",
7
+ "section",
8
+ "header",
9
+ "footer",
10
+ "main",
11
+ "article",
12
+ "aside",
13
+ "nav",
14
+ "h1",
15
+ "h2",
16
+ "h3",
17
+ "h4",
18
+ "h5",
19
+ "h6",
20
+ "p",
21
+ "span",
22
+ "a",
23
+ "button",
24
+ "ul",
25
+ "ol",
26
+ "li",
27
+ "hr",
28
+ "img",
29
+ "iframe",
30
+ "video",
31
+ "form",
32
+ "input",
33
+ "textarea",
34
+ "select",
35
+ "option",
36
+ "label",
37
+ "fieldset",
38
+ "legend",
39
+ "details",
40
+ "summary",
41
+ "time",
42
+ // Lucide icon primitives (W-025 / A-04)
43
+ "svg",
44
+ "path",
45
+ "circle",
46
+ "ellipse",
47
+ "line",
48
+ "polyline",
49
+ "polygon",
50
+ "rect",
51
+ "g",
52
+ "title",
53
+ "desc",
54
+ "defs",
55
+ "symbol",
56
+ "use",
57
+ "image",
58
+ "lineargradient",
59
+ "radialgradient",
60
+ "stop",
61
+ "clippath",
62
+ "mask",
63
+ "pattern",
64
+ "marker",
65
+ "text",
66
+ "tspan",
67
+ ]);
68
+ const VOID = new Set(["hr", "img", "input"]);
69
+ // iframe/video are not void — they need closing tags.
70
+ const ATTR_NAME =
71
+ /^(?:class|id|href|src|alt|width|height|loading|decoding|role|type|checked|target|rel|aria-[a-z][a-z0-9-]*|title|allow|referrerpolicy|preload|controls|allowfullscreen|xmlns|viewBox|fill|stroke|stroke-width|stroke-linecap|stroke-linejoin|stroke-dasharray|stroke-opacity|fill-opacity|opacity|transform|focusable|d|cx|cy|r|x|y|x1|y1|x2|y2|points|rx|ry|xlink:href|preserveAspectRatio|gradientUnits|gradientTransform|offset|stop-color|stop-opacity|clipPathUnits|maskUnits|maskContentUnits|patternUnits|patternContentUnits|patternTransform|markerUnits|markerWidth|markerHeight|refX|refY|orient|font-size|font-family|font-weight|text-anchor|dominant-baseline|dx|dy|clip-path|method|action|name|value|placeholder|for|tabindex|autocomplete|open|datetime|muted|loop|playsinline|autoplay|data-[a-z][a-z0-9-]*)$/;
72
+
73
+ export const isAllowedTag = (tag: string) => TAGS.has(tag);
74
+ export const isAllowedAttr = (name: string) => ATTR_NAME.test(name);
75
+
76
+ /** Serializes a VNode tree. Text and attribute values are always escaped; tags and attribute names are allowlisted. */
77
+ export function serialize(node: VNode | string): string {
78
+ if (typeof node === "string") return escapeText(node);
79
+ if (!TAGS.has(node.tag)) throw new Error(`EmVB: tag "${node.tag}" is not allowed`);
80
+ let attrs = "";
81
+ for (const [name, value] of Object.entries(node.attrs)) {
82
+ if (!ATTR_NAME.test(name)) throw new Error(`EmVB: attribute "${name}" is not allowed`);
83
+ attrs += ` ${name}="${escapeAttr(value)}"`;
84
+ }
85
+ if (VOID.has(node.tag)) return `<${node.tag}${attrs}>`;
86
+ return `<${node.tag}${attrs}>${node.children.map(serialize).join("")}</${node.tag}>`;
87
+ }