@perkyzz/emvb 1.2.0 → 1.4.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 (150) hide show
  1. package/README.md +2 -2
  2. package/THIRD_PARTY_NOTICES.md +2 -2
  3. package/package.json +4 -4
  4. package/src/admin/bulk-api.ts +5 -1
  5. package/src/admin/editor/A11yCopilot.tsx +3 -1
  6. package/src/admin/editor/Editor.tsx +22 -2
  7. package/src/admin/editor/Timeline.tsx +1 -8
  8. package/src/admin/editor/canvas/DeviceBar.tsx +7 -2
  9. package/src/admin/editor/canvas/EmptyCanvas.tsx +11 -2
  10. package/src/admin/editor/canvas/use-canvas-dnd.ts +1 -20
  11. package/src/admin/editor/canvas/vnode-react.tsx +5 -0
  12. package/src/admin/editor/co-design.ts +36 -1
  13. package/src/admin/editor/dnd/dragged-source.ts +22 -0
  14. package/src/admin/editor/dnd/layer-drop.ts +141 -0
  15. package/src/admin/editor/dnd/new-element.ts +76 -0
  16. package/src/admin/editor/editor-css.ts +9 -1
  17. package/src/admin/editor/element-chrome.ts +34 -0
  18. package/src/admin/editor/feature-css.ts +22 -0
  19. package/src/admin/editor/inert-links.ts +8 -0
  20. package/src/admin/editor/panels/ColorControl.tsx +2 -2
  21. package/src/admin/editor/panels/FontFaces.tsx +342 -0
  22. package/src/admin/editor/panels/LayersPanel.tsx +56 -6
  23. package/src/admin/editor/panels/LeftPanel.tsx +153 -29
  24. package/src/admin/editor/panels/SettingsPanel.tsx +1 -0
  25. package/src/admin/editor/panels/SiteStylesDrawer.tsx +5 -0
  26. package/src/admin/editor/panels/TemplatesPanel.tsx +205 -0
  27. package/src/admin/editor/panels/TokenScales.tsx +2 -0
  28. package/src/admin/editor/panels/TriggersEditor.tsx +203 -9
  29. package/src/admin/editor/panels/VariableSection.tsx +111 -7
  30. package/src/admin/editor/panels/color-space.ts +71 -6
  31. package/src/admin/editor/panels/settings/AudienceEditor.tsx +3 -0
  32. package/src/admin/editor/panels/settings/FieldControl.tsx +18 -0
  33. package/src/admin/editor/panels/settings/ItemList.tsx +33 -0
  34. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +9 -3
  35. package/src/admin/editor/panels/settings/StickyControl.tsx +97 -0
  36. package/src/admin/editor/panels/settings/StyleRow.tsx +14 -0
  37. package/src/admin/editor/panels/settings/style-sections.ts +19 -2
  38. package/src/admin/editor/panels/url-rules.ts +43 -0
  39. package/src/admin/editor/panels/use-layers-drop.ts +232 -0
  40. package/src/admin/editor/store.ts +4 -2
  41. package/src/admin/editor/useExtensions.ts +3 -1
  42. package/src/admin/editor/useNodeActions.ts +74 -1
  43. package/src/admin/editor/useSave.ts +116 -85
  44. package/src/admin/export-api.ts +25 -2
  45. package/src/admin/extension-outputs.ts +179 -0
  46. package/src/admin/font-files-api.ts +38 -0
  47. package/src/admin/pages/ImportButton.tsx +15 -4
  48. package/src/admin/pages/ImportElementorDialog.tsx +22 -188
  49. package/src/admin/pages/ImportHtmlDialog.tsx +209 -0
  50. package/src/admin/pages/ImportImageCopies.tsx +167 -0
  51. package/src/admin/pages/ImportReport.tsx +63 -0
  52. package/src/admin/pages/NewPageDialog.tsx +54 -32
  53. package/src/admin/pages/PageList.tsx +9 -0
  54. package/src/admin/pages/ThemePartList.tsx +9 -0
  55. package/src/astro/EmVBPage.astro +5 -2
  56. package/src/astro/extension-styles.ts +35 -0
  57. package/src/astro/forms/EmVBFormsClient.astro +3 -0
  58. package/src/astro/forms/enhance.ts +107 -0
  59. package/src/astro/popups/runtime.ts +47 -2
  60. package/src/astro/render.ts +19 -4
  61. package/src/astro/request-data.ts +6 -0
  62. package/src/astro/resolve-theme.ts +11 -4
  63. package/src/astro/resolve.ts +9 -0
  64. package/src/astro/widgets/EmVBCountdownRuntime.astro +8 -0
  65. package/src/astro/widgets/EmVBCounterRuntime.astro +8 -0
  66. package/src/astro/widgets/EmVBHeadlineRuntime.astro +9 -0
  67. package/src/astro/widgets/EmVBMapRuntime.astro +8 -0
  68. package/src/astro/widgets/EmVBShareRuntime.astro +9 -0
  69. package/src/astro/widgets/EmVBStickyRuntime.astro +9 -0
  70. package/src/astro/widgets/EmVBThemeRuntime.astro +9 -0
  71. package/src/astro/widgets/EmVBWidgetRuntimes.astro +22 -0
  72. package/src/astro/widgets/alerts.ts +16 -2
  73. package/src/astro/widgets/carousel.ts +65 -24
  74. package/src/astro/widgets/countdown.ts +91 -0
  75. package/src/astro/widgets/counter.ts +98 -0
  76. package/src/astro/widgets/extension-runtimes.ts +27 -0
  77. package/src/astro/widgets/headline.ts +99 -0
  78. package/src/astro/widgets/map.ts +37 -0
  79. package/src/astro/widgets/share.ts +21 -0
  80. package/src/astro/widgets/sticky.ts +87 -0
  81. package/src/astro/widgets/theme.ts +52 -0
  82. package/src/constants.ts +1 -1
  83. package/src/core/a11y/audit.ts +177 -6
  84. package/src/core/arrange.ts +71 -1
  85. package/src/core/canvas-collections.ts +28 -0
  86. package/src/core/css/font-face.ts +72 -0
  87. package/src/core/css/generate.ts +35 -1
  88. package/src/core/elements/counter-format.ts +15 -0
  89. package/src/core/elements/form-elements.ts +119 -9
  90. package/src/core/elements/icon-svg.ts +14 -0
  91. package/src/core/elements/index.ts +54 -4
  92. package/src/core/elements/input-limits.ts +44 -0
  93. package/src/core/elements/marketing.ts +1490 -0
  94. package/src/core/elements/menu.ts +109 -47
  95. package/src/core/elements/site.ts +108 -0
  96. package/src/core/elements/widgets.ts +40 -5
  97. package/src/core/export/runtimes.generated.ts +10 -3
  98. package/src/core/export/site.ts +55 -4
  99. package/src/core/extensions/classes.ts +114 -0
  100. package/src/core/extensions/index.ts +17 -2
  101. package/src/core/extensions/normalize.ts +13 -0
  102. package/src/core/extensions/registry.ts +134 -10
  103. package/src/core/extensions/render-ext.ts +66 -21
  104. package/src/core/extensions/safe-nodes.ts +67 -11
  105. package/src/core/extensions/types.ts +48 -1
  106. package/src/core/fonts/files.ts +74 -0
  107. package/src/core/fonts/google.ts +112 -0
  108. package/src/core/fonts/test-font.ts +9 -0
  109. package/src/core/import/elementor/context.ts +13 -4
  110. package/src/core/import/elementor/index.ts +32 -21
  111. package/src/core/import/elementor/widgets.ts +532 -293
  112. package/src/core/import/html/blocks.ts +602 -0
  113. package/src/core/import/html/index.ts +155 -0
  114. package/src/core/import/html/map.ts +958 -0
  115. package/src/core/import/html/parse.ts +430 -0
  116. package/src/core/index.ts +45 -0
  117. package/src/core/limits.ts +6 -0
  118. package/src/core/migrate/v18.ts +3 -0
  119. package/src/core/public-attributes.ts +17 -0
  120. package/src/core/render/extras.ts +31 -7
  121. package/src/core/render/form.ts +103 -10
  122. package/src/core/render/index.ts +172 -16
  123. package/src/core/render/section.ts +31 -1
  124. package/src/core/render/spacer-basis.ts +102 -0
  125. package/src/core/render/theme-boot.ts +16 -0
  126. package/src/core/render/vnode.ts +1 -1
  127. package/src/core/sanitize/css.ts +68 -3
  128. package/src/core/sanitize/motion.ts +12 -0
  129. package/src/core/schema/design.ts +40 -0
  130. package/src/core/schema/layout.ts +534 -22
  131. package/src/core/schema/style.ts +28 -0
  132. package/src/core/templates/index.ts +218 -0
  133. package/src/core/templates/pages.ts +29 -0
  134. package/src/core/templates/sections.ts +794 -0
  135. package/src/core/templates/types.ts +9 -0
  136. package/src/core/theme/dynamic.ts +11 -6
  137. package/src/core/theme/popup-markup.ts +21 -2
  138. package/src/core/theme/popup-rules.ts +36 -0
  139. package/src/core/theme/triggers.ts +64 -5
  140. package/src/core/translatable.ts +5 -2
  141. package/src/extension-build.ts +331 -0
  142. package/src/index.ts +25 -3
  143. package/src/mcp/brief.ts +191 -0
  144. package/src/mcp/prompts.ts +33 -0
  145. package/src/mcp/read.ts +29 -1
  146. package/src/mcp/tools.ts +40 -1
  147. package/src/mcp/write.ts +41 -1
  148. package/src/server/extension-routes.ts +73 -0
  149. package/src/server/font-routes.ts +301 -0
  150. package/src/server/plugin.ts +11 -1
@@ -0,0 +1,114 @@
1
+ /**
2
+ * W-1086: class allowlists and build-time assets an extension declares, checked at registration.
3
+ * Pure (no file access): the integration reads the files, this checks what it read.
4
+ */
5
+
6
+ import type { ExtensionAssets, ExtensionClasses } from "./types.ts";
7
+
8
+ /** A prefix: lowercase words joined by hyphens, ending in `-` (`d-`, `acme-ui-`). */
9
+ const PREFIX = /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*-$/;
10
+ /** A class name: a lowercase letter, then lowercase letters, digits and hyphens. */
11
+ const CLASS_NAME = /^[a-z][a-z0-9-]*$/;
12
+
13
+ const MAX_CLASS_PREFIX_LENGTH = 16;
14
+ const MAX_CLASS_NAME_LENGTH = 64;
15
+ const MAX_ALLOWED_CLASSES = 512;
16
+ /** Extension classes kept on one output node; the rest are dropped. */
17
+ export const MAX_CLASSES_PER_NODE = 16;
18
+ export const MAX_EXTENSION_CSS_BYTES = 256 * 1024;
19
+ export const MAX_EXTENSION_RUNTIME_BYTES = 64 * 1024;
20
+
21
+ /** Why a class prefix is refused, or undefined when it is fine. */
22
+ export function classPrefixIssue(prefix: unknown): string | undefined {
23
+ if (typeof prefix !== "string") return "the class prefix must be a string";
24
+ if (prefix.length < 2 || prefix.length > MAX_CLASS_PREFIX_LENGTH) {
25
+ return `the class prefix must be 2 to ${MAX_CLASS_PREFIX_LENGTH} characters`;
26
+ }
27
+ if (!PREFIX.test(prefix)) {
28
+ return "the class prefix must be lowercase letters, digits and hyphens, ending in -";
29
+ }
30
+ if (prefix.startsWith("emvb")) return "the class prefix may not start with emvb";
31
+ return undefined;
32
+ }
33
+
34
+ /** Whether `name` is a class an extension with this prefix may declare. */
35
+ function allowedClassName(name: unknown, prefix: string): name is string {
36
+ return (
37
+ typeof name === "string" &&
38
+ name.length > prefix.length &&
39
+ name.length <= MAX_CLASS_NAME_LENGTH &&
40
+ CLASS_NAME.test(name) &&
41
+ name.startsWith(prefix) &&
42
+ !name.startsWith("emvb")
43
+ );
44
+ }
45
+
46
+ /**
47
+ * The checked allowlist: undefined when the prefix is refused or no name survives. Names that
48
+ * break the pattern or miss the prefix are dropped one by one; at most MAX_ALLOWED_CLASSES stay.
49
+ */
50
+ export function normalizeClasses(input: unknown): ExtensionClasses | undefined {
51
+ if (!input || typeof input !== "object" || Array.isArray(input)) return undefined;
52
+ const { prefix, allow } = input as { prefix?: unknown; allow?: unknown };
53
+ if (classPrefixIssue(prefix) || typeof prefix !== "string") return undefined;
54
+ if (!Array.isArray(allow)) return undefined;
55
+ const kept = new Set<string>();
56
+ for (const name of allow) {
57
+ if (kept.size >= MAX_ALLOWED_CLASSES) break;
58
+ if (allowedClassName(name, prefix)) kept.add(name);
59
+ }
60
+ if (kept.size === 0) return undefined;
61
+ return { prefix, allow: [...kept] };
62
+ }
63
+
64
+ /**
65
+ * The extension classes an output node keeps: strings on the allowlist only, deduplicated, at
66
+ * most MAX_CLASSES_PER_NODE. Anything else (free text, spaces, quotes, markup, emvb names) drops.
67
+ */
68
+ export function filterExtensionClasses(
69
+ raw: unknown,
70
+ allow: ReadonlySet<string> | undefined,
71
+ ): string[] {
72
+ if (!allow || allow.size === 0 || !Array.isArray(raw)) return [];
73
+ const out: string[] = [];
74
+ for (const name of raw) {
75
+ if (out.length >= MAX_CLASSES_PER_NODE) break;
76
+ if (typeof name === "string" && allow.has(name) && !out.includes(name)) out.push(name);
77
+ }
78
+ return out;
79
+ }
80
+
81
+ const byteLength = (text: string) => new TextEncoder().encode(text).length;
82
+
83
+ /** Why stylesheet text is refused: too large, or it could close the `<style>` it is written in. */
84
+ export function extensionCssIssue(css: string): string | undefined {
85
+ if (byteLength(css) > MAX_EXTENSION_CSS_BYTES) {
86
+ return `the stylesheet is larger than ${MAX_EXTENSION_CSS_BYTES / 1024} KB`;
87
+ }
88
+ if (/<\/style/i.test(css)) return "the stylesheet contains </style";
89
+ return undefined;
90
+ }
91
+
92
+ /** Why runtime text is refused: too large, a module, or it could break out of its `<script>`. */
93
+ export function extensionRuntimeIssue(code: string): string | undefined {
94
+ if (byteLength(code) > MAX_EXTENSION_RUNTIME_BYTES) {
95
+ return `the runtime is larger than ${MAX_EXTENSION_RUNTIME_BYTES / 1024} KB`;
96
+ }
97
+ if (/<\/script|<script|<!--/i.test(code)) return "the runtime contains <script, </script or <!--";
98
+ if (/^\s*(?:import\s*[{*"'(]|import\s+[\w$]|export\s)/m.test(code)) {
99
+ return "the runtime must be a self-contained script without import or export";
100
+ }
101
+ return undefined;
102
+ }
103
+
104
+ /** Assets that pass the checks above; anything else is dropped. Undefined when none is left. */
105
+ export function cleanExtensionAssets(input: unknown): ExtensionAssets | undefined {
106
+ if (!input || typeof input !== "object" || Array.isArray(input)) return undefined;
107
+ const { css, runtime } = input as { css?: unknown; runtime?: unknown };
108
+ const out: ExtensionAssets = {};
109
+ if (typeof css === "string" && css.trim() && !extensionCssIssue(css)) out.css = css;
110
+ if (typeof runtime === "string" && runtime.trim() && !extensionRuntimeIssue(runtime)) {
111
+ out.runtime = runtime;
112
+ }
113
+ return out.css !== undefined || out.runtime !== undefined ? out : undefined;
114
+ }
@@ -11,7 +11,11 @@ export {
11
11
  type ExtensionPanelManifest,
12
12
  type ExtensionSourceManifest,
13
13
  type ExtensionRenderContext,
14
+ type ExtensionClasses,
15
+ type ExtensionAssets,
16
+ type ExtensionOutputNode,
14
17
  } from "./types.ts";
18
+ export { extensionCssIssue, extensionRuntimeIssue } from "./classes.ts";
15
19
  export {
16
20
  isExtensionType,
17
21
  parseExtensionType,
@@ -35,7 +39,18 @@ export {
35
39
  liveElement,
36
40
  clearExtensions,
37
41
  manifestsFromOptions,
42
+ rememberExtensionAssets,
43
+ rememberExtensionManifests,
44
+ registerBundledExtensions,
45
+ extensionAssets,
46
+ extensionStylesheets,
38
47
  } from "./registry.ts";
39
- export { safeExtensionOutput } from "./safe-nodes.ts";
40
- export { syncSourceValues, renderExtensionNode } from "./render-ext.ts";
48
+ export { safeExtensionOutput, extensionClassesOf, withExtensionClasses } from "./safe-nodes.ts";
49
+ export {
50
+ syncSourceValues,
51
+ renderExtensionNode,
52
+ extensionOutputKey,
53
+ liveExtensionOutput,
54
+ type ExtensionOutputs,
55
+ } from "./render-ext.ts";
41
56
  export { defineExtension } from "./define.ts";
@@ -2,6 +2,7 @@
2
2
  * Validate and turn a live EmVBExtension into a manifest + kept live object (W-927).
3
3
  */
4
4
 
5
+ import { normalizeClasses } from "./classes.ts";
5
6
  import { extensionIdIssue, extensionNameIssue, extensionType } from "./namespace.ts";
6
7
  import { isAllowedExtensionFieldKind, schemaDefaults } from "./schema.ts";
7
8
  import type {
@@ -118,6 +119,15 @@ export function normalizeExtension(input: unknown): NormalizedExtension | undefi
118
119
  const livePanels = panelsIn.filter((p) => panels.some((m) => m.name === p.name));
119
120
  const liveSources = sourcesIn.filter((s) => sources.some((m) => m.name === s.name));
120
121
 
122
+ // W-1086: additive keys. The class allowlist is checked here; `module`, `css` and `runtime` are
123
+ // read by the integration at build time (see extension-build.ts), never at request time.
124
+ const classes = normalizeClasses(ext.classes);
125
+ const strings = Object.fromEntries(
126
+ (["module", "css", "runtime"] as const)
127
+ .filter((key) => typeof ext[key] === "string" && ext[key].length <= 2048)
128
+ .map((key) => [key, ext[key]]),
129
+ );
130
+
121
131
  const live: EmVBExtension = {
122
132
  extensionApi: EXTENSION_API_VERSION,
123
133
  id: ext.id,
@@ -125,6 +135,8 @@ export function normalizeExtension(input: unknown): NormalizedExtension | undefi
125
135
  elements: liveElements,
126
136
  panels: livePanels,
127
137
  sources: liveSources,
138
+ ...strings,
139
+ ...(classes ? { classes } : {}),
128
140
  };
129
141
 
130
142
  return {
@@ -136,6 +148,7 @@ export function normalizeExtension(input: unknown): NormalizedExtension | undefi
136
148
  elements,
137
149
  panels,
138
150
  sources,
151
+ ...(classes ? { classes } : {}),
139
152
  },
140
153
  };
141
154
  }
@@ -7,14 +7,19 @@
7
7
  * and the renderer look them up by id.
8
8
  */
9
9
 
10
+ import { cleanExtensionAssets, normalizeClasses } from "./classes.ts";
10
11
  import { normalizeExtension } from "./normalize.ts";
11
- import type { EmVBExtension, ExtensionManifest } from "./types.ts";
12
+ import type { EmVBExtension, ExtensionAssets, ExtensionManifest } from "./types.ts";
12
13
 
13
14
  const KEY = Symbol.for("emvb.extensions.v1");
14
15
 
15
16
  type Store = {
16
17
  byId: Map<string, EmVBExtension>;
17
18
  manifests: ExtensionManifest[];
19
+ /** W-1086: stylesheet and runtime text read at build time, by extension id. */
20
+ assets?: Map<string, ExtensionAssets>;
21
+ /** W-1086: each extension's class allowlist as a set, by extension id. */
22
+ allow?: Map<string, ReadonlySet<string>>;
18
23
  };
19
24
 
20
25
  function store(): Store {
@@ -23,6 +28,26 @@ function store(): Store {
23
28
  return g[KEY];
24
29
  }
25
30
 
31
+ function assetsMap(): Map<string, ExtensionAssets> {
32
+ const s = store();
33
+ s.assets ??= new Map();
34
+ return s.assets;
35
+ }
36
+
37
+ function allowMap(): Map<string, ReadonlySet<string>> {
38
+ const s = store();
39
+ s.allow ??= new Map();
40
+ return s.allow;
41
+ }
42
+
43
+ function rememberManifest(manifest: ExtensionManifest): void {
44
+ const s = store();
45
+ s.manifests = s.manifests.filter((m) => m.id !== manifest.id);
46
+ s.manifests.push(manifest);
47
+ if (manifest.classes) allowMap().set(manifest.id, new Set(manifest.classes.allow));
48
+ else allowMap().delete(manifest.id);
49
+ }
50
+
26
51
  /** Register live extensions from `emvb({ extensions })`. Returns serializable manifests. */
27
52
  export function rememberExtensions(input: unknown): ExtensionManifest[] {
28
53
  const list = Array.isArray(input) ? input : [];
@@ -33,16 +58,91 @@ export function rememberExtensions(input: unknown): ExtensionManifest[] {
33
58
  if (!normalized) continue;
34
59
  s.byId.set(normalized.live.id, normalized.live);
35
60
  // Replace an existing manifest for the same id (re-import / HMR).
36
- s.manifests = s.manifests.filter((m) => m.id !== normalized.manifest.id);
37
- s.manifests.push(normalized.manifest);
61
+ rememberManifest(normalized.manifest);
38
62
  manifests.push(normalized.manifest);
39
63
  }
40
64
  return manifests;
41
65
  }
42
66
 
43
- /** Manifests currently remembered (for editor/config). */
67
+ /**
68
+ * W-1086: called by the server entry the integration generates (extension-build.ts). Each pair is
69
+ * an extension module's namespace and the extension id it exports; the export whose
70
+ * `extensionApi` and `id` match is registered, so render functions reach Workers and production
71
+ * builds, where astro.config never runs.
72
+ */
73
+ export function registerBundledExtensions(entries: readonly (readonly [unknown, string])[]): void {
74
+ const found: unknown[] = [];
75
+ for (const [namespace, id] of entries) {
76
+ if (!namespace || typeof namespace !== "object") continue;
77
+ const match = Object.values(namespace).find(
78
+ (value) =>
79
+ !!value &&
80
+ typeof value === "object" &&
81
+ (value as { extensionApi?: unknown }).extensionApi === 1 &&
82
+ (value as { id?: unknown }).id === id,
83
+ );
84
+ if (match) found.push(match);
85
+ }
86
+ rememberExtensions(found);
87
+ }
88
+
89
+ /**
90
+ * W-1086: keep the build-time stylesheet and runtime text of extension `id`. Text that fails the
91
+ * checks in classes.ts is dropped.
92
+ */
93
+ export function rememberExtensionAssets(id: string, assets: unknown): void {
94
+ const clean = cleanExtensionAssets(assets);
95
+ if (clean) assetsMap().set(id, clean);
96
+ else assetsMap().delete(id);
97
+ }
98
+
99
+ /** W-1086: the build-time stylesheet and runtime text of extension `id`, if any. */
100
+ export function extensionAssets(id: string): ExtensionAssets | undefined {
101
+ return store().assets?.get(id);
102
+ }
103
+
104
+ /**
105
+ * W-1086: the stylesheets of extensions `ids` (each once, by id), as one sheet. Pages and theme
106
+ * parts that render apart (`renderPage` with `separateExtensionCss`) collect the ids they used
107
+ * and write this once per document.
108
+ */
109
+ export function extensionStylesheets(ids: Iterable<string>): string {
110
+ return [...new Set(ids)]
111
+ .toSorted()
112
+ .flatMap((id) => {
113
+ const css = extensionAssets(id)?.css;
114
+ return css ? [`/* ${id} */\n${css}\n`] : [];
115
+ })
116
+ .join("");
117
+ }
118
+
119
+ /** W-1086: the class allowlist of extension `id` (empty when it declares none). */
120
+ export function extensionClassAllowlist(id: string): ReadonlySet<string> | undefined {
121
+ return store().allow?.get(id);
122
+ }
123
+
124
+ /**
125
+ * W-1086: the admin has manifests (from editor/config) but no live extension code. Remembering
126
+ * them gives the editor canvas and the HTML export the class allowlists and assets.
127
+ */
128
+ export function rememberExtensionManifests(input: readonly unknown[]): void {
129
+ for (const item of input) {
130
+ if (!item || typeof item !== "object") continue;
131
+ const m = item as ExtensionManifest;
132
+ if (m.extensionApi !== 1 || typeof m.id !== "string" || !Array.isArray(m.elements)) continue;
133
+ const classes = normalizeClasses(m.classes);
134
+ const { assets, ...rest } = m;
135
+ rememberManifest({ ...rest, ...(classes ? { classes } : {}) });
136
+ rememberExtensionAssets(m.id, assets);
137
+ }
138
+ }
139
+
140
+ /** Manifests currently remembered (for editor/config), with their build-time assets. */
44
141
  export function listedExtensionManifests(): ExtensionManifest[] {
45
- return store().manifests.slice();
142
+ return store().manifests.map((m) => {
143
+ const assets = extensionAssets(m.id);
144
+ return assets ? Object.assign({}, m, { assets }) : m;
145
+ });
46
146
  }
47
147
 
48
148
  /** Live extension by package id. */
@@ -75,20 +175,44 @@ export function clearExtensions(): void {
75
175
  const s = store();
76
176
  s.byId.clear();
77
177
  s.manifests = [];
178
+ s.assets?.clear();
179
+ s.allow?.clear();
78
180
  }
79
181
 
80
182
  /** Rebuild manifests from options.extensions after createPlugin (ids only or full manifests). */
81
183
  export function manifestsFromOptions(input: unknown): ExtensionManifest[] {
82
184
  if (!Array.isArray(input)) return listedExtensionManifests();
83
- // Prefer live registry order; fall back to whatever was serialized.
84
- const live = listedExtensionManifests();
85
- if (live.length > 0) return live;
185
+ // W-1086: the integration read `css` and `runtime` at build time and passed their text in the
186
+ // serialized options; a Workers or production server has no files to read them from.
187
+ for (const item of input) {
188
+ if (!item || typeof item !== "object") continue;
189
+ const { id, assets } = item as { id?: unknown; assets?: unknown };
190
+ if (typeof id === "string" && assets !== undefined) rememberExtensionAssets(id, assets);
191
+ }
192
+ // W-1086: a Worker or production server has live code only for the extensions the generated
193
+ // entry bundled (those that name their `module`). Every other extension still has its serialized
194
+ // manifest, so merge by id: the live manifest wins, the serialized one fills the gaps.
195
+ const live = new Map(listedExtensionManifests().map((m) => [m.id, m]));
86
196
  const out: ExtensionManifest[] = [];
197
+ const seen = new Set<string>();
87
198
  for (const item of input) {
88
199
  if (!item || typeof item !== "object") continue;
89
200
  const m = item as ExtensionManifest;
90
- if (m.extensionApi !== 1 || typeof m.id !== "string") continue;
91
- out.push(m);
201
+ if (m.extensionApi !== 1 || typeof m.id !== "string" || !Array.isArray(m.elements)) continue;
202
+ if (seen.has(m.id)) continue;
203
+ seen.add(m.id);
204
+ const own = live.get(m.id);
205
+ if (own) {
206
+ out.push(own);
207
+ continue;
208
+ }
209
+ const { assets: _assets, ...rest } = m;
210
+ const classes = normalizeClasses(m.classes);
211
+ const manifest = { ...rest, ...(classes ? { classes } : {}) };
212
+ if (classes) allowMap().set(m.id, new Set(classes.allow));
213
+ const assets = extensionAssets(m.id);
214
+ out.push(assets ? { ...manifest, assets } : manifest);
92
215
  }
216
+ for (const [id, manifest] of live) if (!seen.has(id)) out.push(manifest);
93
217
  return out;
94
218
  }
@@ -5,7 +5,7 @@
5
5
 
6
6
  import type { LayoutNode } from "../schema/layout.ts";
7
7
  import type { VNode } from "../render/vnode.ts";
8
- import { liveElement, liveExtension } from "./registry.ts";
8
+ import { extensionClassAllowlist, liveElement, liveExtension } from "./registry.ts";
9
9
  import { validateExtensionProps } from "./schema.ts";
10
10
  import { safeExtensionOutput } from "./safe-nodes.ts";
11
11
  import { extensionPlaceholderVNode } from "./placeholder.ts";
@@ -65,6 +65,57 @@ export function syncSourceValues(pkg: string): Record<string, Record<string, str
65
65
 
66
66
  export type VisitChildren = (nodes: LayoutNode[]) => VNode[];
67
67
 
68
+ const propsOf = (node: { props?: unknown }): Record<string, unknown> =>
69
+ node.props && typeof node.props === "object" && !Array.isArray(node.props)
70
+ ? (node.props as Record<string, unknown>)
71
+ : {};
72
+
73
+ /** Stable JSON: object keys sorted, so equal props give equal keys. */
74
+ function stableJson(value: unknown): string {
75
+ if (Array.isArray(value)) return `[${value.map(stableJson).join(",")}]`;
76
+ if (value && typeof value === "object") {
77
+ const entries = Object.entries(value as Record<string, unknown>)
78
+ .filter(([, v]) => v !== undefined)
79
+ .toSorted(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
80
+ return `{${entries.map(([k, v]) => `${JSON.stringify(k)}:${stableJson(v)}`).join(",")}}`;
81
+ }
82
+ return JSON.stringify(value) ?? "null";
83
+ }
84
+
85
+ /**
86
+ * W-1086: the key of one extension element's output: its type and props. The admin has no
87
+ * extension code, so the editor canvas and the HTML export ask the server for outputs by key.
88
+ */
89
+ export function extensionOutputKey(node: { type: string; props?: unknown }): string {
90
+ return `${node.type}\n${stableJson(propsOf(node))}`;
91
+ }
92
+
93
+ /** Raw render output per `extensionOutputKey`, rendered by the server for the admin. */
94
+ export type ExtensionOutputs = ReadonlyMap<string, unknown>;
95
+
96
+ /**
97
+ * Run the live render() of a stored extension element and check its output. Undefined when the
98
+ * extension is missing here, render throws, or nothing legal is left.
99
+ */
100
+ export function liveExtensionOutput(node: {
101
+ type: string;
102
+ props?: unknown;
103
+ }): LayoutNode[] | undefined {
104
+ const parsed = parseExtensionType(node.type);
105
+ const found = parsed ? liveElement(node.type) : undefined;
106
+ if (!parsed || !found) return undefined;
107
+ const checked = validateExtensionProps(found.manifest.schema, propsOf(node));
108
+ const ctx: ExtensionRenderContext = { sources: syncSourceValues(parsed.pkg) };
109
+ let raw: unknown;
110
+ try {
111
+ raw = found.element.render(checked.props, ctx);
112
+ } catch {
113
+ return undefined;
114
+ }
115
+ const safe = safeExtensionOutput(raw, Math.random, extensionClassAllowlist(parsed.pkg));
116
+ return safe.length > 0 ? safe : undefined;
117
+ }
118
+
68
119
  /**
69
120
  * Build the VNode for a stored extension element. Always returns a node (placeholder
70
121
  * on failure) so siblings keep rendering.
@@ -73,6 +124,10 @@ export function renderExtensionNode(
73
124
  node: LayoutNode,
74
125
  mode: ExtensionRenderMode,
75
126
  visitChildren: VisitChildren,
127
+ /** W-1086: server-rendered outputs (the admin's canvas and export), by `extensionOutputKey`. */
128
+ outputs?: ExtensionOutputs,
129
+ /** W-1086: told the package id whenever one of its elements renders. */
130
+ rendered?: (pkg: string) => void,
76
131
  ): VNode {
77
132
  const id = typeof node.id === "string" ? node.id : undefined;
78
133
  const type = node.type;
@@ -81,29 +136,19 @@ export function renderExtensionNode(
81
136
  return extensionPlaceholderVNode(type, id, mode);
82
137
  }
83
138
 
84
- const found = liveElement(type);
85
- if (!found) {
86
- return extensionPlaceholderVNode(type, id, mode);
139
+ let safe = liveExtensionOutput(node);
140
+ if (!safe && !liveElement(type) && outputs?.has(extensionOutputKey(node))) {
141
+ // The server's output is checked again here, with the allowlist the admin was sent.
142
+ safe = safeExtensionOutput(
143
+ outputs.get(extensionOutputKey(node)),
144
+ Math.random,
145
+ extensionClassAllowlist(parsed.pkg),
146
+ );
87
147
  }
88
-
89
- const storedProps =
90
- node.props && typeof node.props === "object" && !Array.isArray(node.props)
91
- ? (node.props as Record<string, unknown>)
92
- : {};
93
- const checked = validateExtensionProps(found.manifest.schema, storedProps);
94
- const ctx: ExtensionRenderContext = { sources: syncSourceValues(parsed.pkg) };
95
-
96
- let raw: unknown;
97
- try {
98
- raw = found.element.render(checked.props, ctx);
99
- } catch {
100
- return extensionPlaceholderVNode(type, id, mode);
101
- }
102
-
103
- const safe = safeExtensionOutput(raw);
104
- if (safe.length === 0) {
148
+ if (!safe || safe.length === 0) {
105
149
  return extensionPlaceholderVNode(type, id, mode);
106
150
  }
151
+ rendered?.(parsed.pkg);
107
152
 
108
153
  const children = visitChildren(safe);
109
154
  return {
@@ -10,6 +10,20 @@ import { KNOWN_ELEMENT_TYPES } from "../schema/layout.ts";
10
10
  import { validateLayout } from "../validate.ts";
11
11
  import { newNodeId } from "../tree-ops.ts";
12
12
  import { isExtensionType } from "./namespace.ts";
13
+ import { filterExtensionClasses } from "./classes.ts";
14
+
15
+ /**
16
+ * W-1086: the extension classes each validated output node keeps. Render output only: they ride
17
+ * beside the node, never in it, so the stored layout schema and its validation are unchanged.
18
+ */
19
+ const EXT_CLASSES = new WeakMap<object, readonly string[]>();
20
+
21
+ /** The allowlisted extension classes of an output node (none for every other node). */
22
+ export function extensionClassesOf(node: object): readonly string[] | undefined {
23
+ return EXT_CLASSES.get(node);
24
+ }
25
+
26
+ type Coerced = { node: Record<string, unknown>; classes: string[]; children?: Coerced[] };
13
27
 
14
28
  const KNOWN = new Set<string>(KNOWN_ELEMENT_TYPES);
15
29
  const MAX_NODES = 32;
@@ -20,7 +34,8 @@ function coerce(
20
34
  depth: number,
21
35
  budget: { left: number },
22
36
  random: () => number,
23
- ): Record<string, unknown> | undefined {
37
+ allow: ReadonlySet<string> | undefined,
38
+ ): Coerced | undefined {
24
39
  if (budget.left <= 0 || depth > MAX_DEPTH) return undefined;
25
40
  if (typeof value === "string") return undefined; // raw HTML / text — refused
26
41
  if (!value || typeof value !== "object" || Array.isArray(value)) return undefined;
@@ -39,24 +54,56 @@ function coerce(
39
54
  typeof node.id === "string" && /^[A-Za-z0-9_-]{4,24}$/.test(node.id)
40
55
  ? node.id
41
56
  : newNodeId(random);
42
- let children: Record<string, unknown>[] | undefined;
57
+ let children: Coerced[] | undefined;
43
58
  if (Array.isArray(node.children)) {
44
59
  children = [];
45
60
  for (const child of node.children) {
46
- const ok = coerce(child, depth + 1, budget, random);
61
+ const ok = coerce(child, depth + 1, budget, random, allow);
47
62
  if (ok) children.push(ok);
48
63
  }
49
64
  }
50
65
  // Drop style/attributes from extension output: only props + children of built-ins.
51
66
  // (Extensions style through the host element's own style on the wrapper.)
52
67
  return {
53
- id,
54
- type: node.type,
55
- props: { ...(node.props as Record<string, unknown>) },
68
+ node: {
69
+ id,
70
+ type: node.type,
71
+ props: { ...(node.props as Record<string, unknown>) },
72
+ ...(children ? { children: children.map((c) => c.node) } : {}),
73
+ },
74
+ classes: filterExtensionClasses(node.extClasses, allow),
56
75
  ...(children ? { children } : {}),
57
76
  };
58
77
  }
59
78
 
79
+ /** Pair each validated node with the classes its coerced twin kept (same tree shape). */
80
+ function attachClasses(validated: LayoutNode, coerced: Coerced): void {
81
+ if (coerced.classes.length > 0) EXT_CLASSES.set(validated, coerced.classes);
82
+ const kids = (validated as { children?: LayoutNode[] }).children;
83
+ if (!Array.isArray(kids) || !coerced.children || kids.length !== coerced.children.length) return;
84
+ kids.forEach((kid, i) => {
85
+ const twin = coerced.children?.[i];
86
+ if (twin) attachClasses(kid, twin);
87
+ });
88
+ }
89
+
90
+ /**
91
+ * W-1086: output nodes as plain JSON with their extension classes back in `extClasses`, for the
92
+ * editor route; the admin runs them through `safeExtensionOutput` again with its allowlist.
93
+ */
94
+ export function withExtensionClasses(nodes: readonly LayoutNode[]): unknown[] {
95
+ const copy = (node: LayoutNode): Record<string, unknown> => {
96
+ const classes = EXT_CLASSES.get(node);
97
+ const kids = (node as { children?: LayoutNode[] }).children;
98
+ return {
99
+ ...node,
100
+ ...(Array.isArray(kids) ? { children: kids.map(copy) } : {}),
101
+ ...(classes ? { extClasses: [...classes] } : {}),
102
+ };
103
+ };
104
+ return nodes.map(copy);
105
+ }
106
+
60
107
  /**
61
108
  * Normalize a render() return value to validated built-in nodes.
62
109
  * Illegal shapes, schema failures and empty output yield `[]` (caller shows the placeholder).
@@ -64,16 +111,18 @@ function coerce(
64
111
  export function safeExtensionOutput(
65
112
  raw: unknown,
66
113
  random: () => number = Math.random,
114
+ /** W-1086: the extension's class allowlist; without one every `extClasses` entry drops. */
115
+ allow?: ReadonlySet<string>,
67
116
  ): LayoutNode[] {
68
117
  const budget = { left: MAX_NODES };
69
- const coerced: Record<string, unknown>[] = [];
118
+ const coerced: Coerced[] = [];
70
119
  if (Array.isArray(raw)) {
71
120
  for (const item of raw) {
72
- const node = coerce(item, 0, budget, random);
121
+ const node = coerce(item, 0, budget, random, allow);
73
122
  if (node) coerced.push(node);
74
123
  }
75
124
  } else {
76
- const one = coerce(raw, 0, budget, random);
125
+ const one = coerce(raw, 0, budget, random, allow);
77
126
  if (one) coerced.push(one);
78
127
  }
79
128
  if (coerced.length === 0) return [];
@@ -84,10 +133,17 @@ export function safeExtensionOutput(
84
133
  id: "root0001",
85
134
  type: "container",
86
135
  props: {},
87
- children: coerced,
136
+ children: coerced.map((c) => c.node),
88
137
  },
89
138
  };
90
139
  const result = validateLayout(page);
91
140
  if (!result.ok) return [];
92
- return result.layout.root.children.slice();
141
+ const out = result.layout.root.children.slice();
142
+ if (out.length === coerced.length) {
143
+ out.forEach((node, i) => {
144
+ const twin = coerced[i];
145
+ if (twin) attachClasses(node, twin);
146
+ });
147
+ }
148
+ return out;
93
149
  }