@perkyzz/emvb 1.3.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.
- package/README.md +2 -2
- package/package.json +2 -2
- package/src/admin/bulk-api.ts +5 -1
- package/src/admin/editor/A11yCopilot.tsx +3 -1
- package/src/admin/editor/Editor.tsx +22 -2
- package/src/admin/editor/Timeline.tsx +1 -8
- package/src/admin/editor/canvas/DeviceBar.tsx +7 -2
- package/src/admin/editor/canvas/EmptyCanvas.tsx +11 -2
- package/src/admin/editor/canvas/use-canvas-dnd.ts +1 -20
- package/src/admin/editor/canvas/vnode-react.tsx +2 -0
- package/src/admin/editor/co-design.ts +36 -1
- package/src/admin/editor/dnd/dragged-source.ts +22 -0
- package/src/admin/editor/dnd/layer-drop.ts +141 -0
- package/src/admin/editor/dnd/new-element.ts +16 -0
- package/src/admin/editor/editor-css.ts +9 -1
- package/src/admin/editor/element-chrome.ts +6 -0
- package/src/admin/editor/feature-css.ts +22 -0
- package/src/admin/editor/inert-links.ts +8 -0
- package/src/admin/editor/panels/ColorControl.tsx +2 -2
- package/src/admin/editor/panels/FontFaces.tsx +342 -0
- package/src/admin/editor/panels/LayersPanel.tsx +56 -6
- package/src/admin/editor/panels/LeftPanel.tsx +153 -29
- package/src/admin/editor/panels/SettingsPanel.tsx +1 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +5 -0
- package/src/admin/editor/panels/TemplatesPanel.tsx +205 -0
- package/src/admin/editor/panels/TokenScales.tsx +2 -0
- package/src/admin/editor/panels/TriggersEditor.tsx +203 -9
- package/src/admin/editor/panels/VariableSection.tsx +111 -7
- package/src/admin/editor/panels/color-space.ts +71 -6
- package/src/admin/editor/panels/settings/AudienceEditor.tsx +3 -0
- package/src/admin/editor/panels/settings/FieldControl.tsx +18 -0
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +9 -3
- package/src/admin/editor/panels/settings/StickyControl.tsx +97 -0
- package/src/admin/editor/panels/settings/StyleRow.tsx +14 -0
- package/src/admin/editor/panels/settings/style-sections.ts +19 -2
- package/src/admin/editor/panels/url-rules.ts +43 -0
- package/src/admin/editor/panels/use-layers-drop.ts +232 -0
- package/src/admin/editor/store.ts +4 -2
- package/src/admin/editor/useExtensions.ts +3 -1
- package/src/admin/editor/useNodeActions.ts +74 -1
- package/src/admin/editor/useSave.ts +116 -85
- package/src/admin/export-api.ts +25 -2
- package/src/admin/extension-outputs.ts +179 -0
- package/src/admin/font-files-api.ts +38 -0
- package/src/admin/pages/ImportButton.tsx +15 -4
- package/src/admin/pages/ImportElementorDialog.tsx +22 -188
- package/src/admin/pages/ImportHtmlDialog.tsx +209 -0
- package/src/admin/pages/ImportImageCopies.tsx +167 -0
- package/src/admin/pages/ImportReport.tsx +63 -0
- package/src/admin/pages/NewPageDialog.tsx +54 -32
- package/src/admin/pages/PageList.tsx +9 -0
- package/src/admin/pages/ThemePartList.tsx +9 -0
- package/src/astro/EmVBPage.astro +5 -2
- package/src/astro/extension-styles.ts +35 -0
- package/src/astro/forms/EmVBFormsClient.astro +3 -0
- package/src/astro/forms/enhance.ts +107 -0
- package/src/astro/popups/runtime.ts +47 -2
- package/src/astro/render.ts +19 -4
- package/src/astro/resolve-theme.ts +11 -4
- package/src/astro/widgets/EmVBStickyRuntime.astro +9 -0
- package/src/astro/widgets/EmVBThemeRuntime.astro +9 -0
- package/src/astro/widgets/EmVBWidgetRuntimes.astro +12 -0
- package/src/astro/widgets/alerts.ts +16 -2
- package/src/astro/widgets/carousel.ts +65 -24
- package/src/astro/widgets/countdown.ts +18 -7
- package/src/astro/widgets/extension-runtimes.ts +27 -0
- package/src/astro/widgets/headline.ts +14 -5
- package/src/astro/widgets/sticky.ts +87 -0
- package/src/astro/widgets/theme.ts +52 -0
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +150 -2
- package/src/core/arrange.ts +19 -1
- package/src/core/css/font-face.ts +72 -0
- package/src/core/css/generate.ts +35 -1
- package/src/core/elements/form-elements.ts +119 -9
- package/src/core/elements/icon-svg.ts +14 -0
- package/src/core/elements/index.ts +24 -4
- package/src/core/elements/input-limits.ts +44 -0
- package/src/core/elements/menu.ts +109 -47
- package/src/core/elements/site.ts +108 -0
- package/src/core/elements/widgets.ts +13 -3
- package/src/core/export/runtimes.generated.ts +7 -5
- package/src/core/export/site.ts +50 -4
- package/src/core/extensions/classes.ts +114 -0
- package/src/core/extensions/index.ts +17 -2
- package/src/core/extensions/normalize.ts +13 -0
- package/src/core/extensions/registry.ts +134 -10
- package/src/core/extensions/render-ext.ts +66 -21
- package/src/core/extensions/safe-nodes.ts +67 -11
- package/src/core/extensions/types.ts +48 -1
- package/src/core/fonts/files.ts +74 -0
- package/src/core/fonts/google.ts +112 -0
- package/src/core/fonts/test-font.ts +9 -0
- package/src/core/import/elementor/context.ts +13 -4
- package/src/core/import/elementor/index.ts +32 -21
- package/src/core/import/elementor/widgets.ts +114 -12
- package/src/core/import/html/blocks.ts +602 -0
- package/src/core/import/html/index.ts +155 -0
- package/src/core/import/html/map.ts +958 -0
- package/src/core/import/html/parse.ts +430 -0
- package/src/core/index.ts +45 -0
- package/src/core/migrate/v18.ts +3 -0
- package/src/core/public-attributes.ts +17 -0
- package/src/core/render/extras.ts +31 -7
- package/src/core/render/form.ts +103 -10
- package/src/core/render/index.ts +151 -15
- package/src/core/render/section.ts +31 -1
- package/src/core/render/spacer-basis.ts +102 -0
- package/src/core/render/theme-boot.ts +16 -0
- package/src/core/render/vnode.ts +1 -1
- package/src/core/sanitize/css.ts +68 -3
- package/src/core/sanitize/motion.ts +12 -0
- package/src/core/schema/design.ts +40 -0
- package/src/core/schema/layout.ts +114 -5
- package/src/core/schema/style.ts +28 -0
- package/src/core/templates/index.ts +218 -0
- package/src/core/templates/pages.ts +29 -0
- package/src/core/templates/sections.ts +794 -0
- package/src/core/templates/types.ts +9 -0
- package/src/core/theme/dynamic.ts +9 -6
- package/src/core/theme/popup-markup.ts +21 -2
- package/src/core/theme/popup-rules.ts +36 -0
- package/src/core/theme/triggers.ts +64 -5
- package/src/core/translatable.ts +5 -2
- package/src/extension-build.ts +331 -0
- package/src/index.ts +25 -3
- package/src/mcp/brief.ts +191 -0
- package/src/mcp/prompts.ts +33 -0
- package/src/mcp/read.ts +29 -1
- package/src/mcp/tools.ts +40 -1
- package/src/mcp/write.ts +41 -1
- package/src/server/extension-routes.ts +73 -0
- package/src/server/font-routes.ts +301 -0
- package/src/server/plugin.ts +9 -0
package/src/core/export/site.ts
CHANGED
|
@@ -37,6 +37,11 @@ import {
|
|
|
37
37
|
} from "../index.ts";
|
|
38
38
|
import { someNode } from "../tree-ops.ts";
|
|
39
39
|
import { EXPORT_RUNTIMES } from "./runtimes.generated.ts";
|
|
40
|
+
import {
|
|
41
|
+
extensionAssets,
|
|
42
|
+
extensionStylesheets,
|
|
43
|
+
type ExtensionOutputs,
|
|
44
|
+
} from "../extensions/index.ts";
|
|
40
45
|
import type { ZipEntry } from "./zip.ts";
|
|
41
46
|
|
|
42
47
|
/** A published page as the content API returns it. */
|
|
@@ -216,6 +221,14 @@ const SRCSET = /\ssrcset="([^"]+)"/g;
|
|
|
216
221
|
const CSS_URL = /url\(\s*(?:"([^"]+)"|'([^']+)'|([^)'"\s]+))\s*\)/g;
|
|
217
222
|
const MEDIA_EXT = /\.(?:avif|webp|png|jpe?g|gif|svg|ico|mp4|webm|ogg|mp3|pdf|woff2?)$/i;
|
|
218
223
|
|
|
224
|
+
/** W-1072: EmVB's own font files (`/_emdash/api/plugins/emvb/font?id=<16 hex>`), copied as media. */
|
|
225
|
+
const FONT_ROUTE = /^\/_emdash\/api\/plugins\/emvb\/font\?id=([0-9a-f]{16})$/;
|
|
226
|
+
|
|
227
|
+
/** The font id of an EmVB font route URL, or null. */
|
|
228
|
+
function fontRouteId(url: string): string | null {
|
|
229
|
+
return FONT_ROUTE.exec(url)?.[1] ?? null;
|
|
230
|
+
}
|
|
231
|
+
|
|
219
232
|
/** S-06: the URL's path alone, so `?x=.png` can't make an API URL look like a media file. */
|
|
220
233
|
function pathOf(url: string): string {
|
|
221
234
|
try {
|
|
@@ -241,7 +254,8 @@ export function mediaUrls(html: string, css: string): string[] {
|
|
|
241
254
|
if (!url || url.startsWith("#") || url.startsWith("data:")) return;
|
|
242
255
|
if (/^(?:mailto|tel|javascript):/i.test(url)) return;
|
|
243
256
|
const path = pathOf(url);
|
|
244
|
-
const isMediaPath =
|
|
257
|
+
const isMediaPath =
|
|
258
|
+
path.startsWith("/_emdash/api/media/") || MEDIA_EXT.test(path) || fontRouteId(url) !== null;
|
|
245
259
|
if (isMediaPath) found.add(url);
|
|
246
260
|
};
|
|
247
261
|
for (const m of html.matchAll(ATTR_URL)) add(unescapeAttr(m[1] ?? ""));
|
|
@@ -265,6 +279,8 @@ const RUNTIME_FILES = {
|
|
|
265
279
|
map: "assets/emvb-map.js",
|
|
266
280
|
headline: "assets/emvb-headline.js",
|
|
267
281
|
share: "assets/emvb-share.js",
|
|
282
|
+
sticky: "assets/emvb-sticky.js",
|
|
283
|
+
theme: "assets/emvb-theme.js",
|
|
268
284
|
} as const;
|
|
269
285
|
|
|
270
286
|
function documentHtml(opts: {
|
|
@@ -309,6 +325,8 @@ export function buildSiteExport(input: {
|
|
|
309
325
|
lang?: string;
|
|
310
326
|
/** Exported on (ISO date), for the report. */
|
|
311
327
|
date?: string;
|
|
328
|
+
/** W-1086: extension outputs the server rendered (the admin has no extension code). */
|
|
329
|
+
extensionOutputs?: ExtensionOutputs;
|
|
312
330
|
}): SiteExport {
|
|
313
331
|
const lang = input.lang ?? "en";
|
|
314
332
|
const parts = new Map<string, Part>();
|
|
@@ -326,6 +344,13 @@ export function buildSiteExport(input: {
|
|
|
326
344
|
const report: ExportReport = { pages: [], skipped: [], snapshots: [], media: [] };
|
|
327
345
|
const media = new Set<string>();
|
|
328
346
|
const runtimes = new Set<keyof typeof RUNTIME_FILES>();
|
|
347
|
+
// W-1086: extension runtimes the pages use, copied as `assets/ext/<package>.js`.
|
|
348
|
+
const extensionRuntimes = new Map<string, string>();
|
|
349
|
+
// W-1086: each page's style.css writes every extension stylesheet once (see below).
|
|
350
|
+
const extensionOutputs = {
|
|
351
|
+
...(input.extensionOutputs ? { extensionOutputs: input.extensionOutputs } : {}),
|
|
352
|
+
separateExtensionCss: true,
|
|
353
|
+
};
|
|
329
354
|
const taken = new Set<string>();
|
|
330
355
|
|
|
331
356
|
const renderPart = (part: Part, scopeOnly = false) => {
|
|
@@ -334,6 +359,7 @@ export function buildSiteExport(input: {
|
|
|
334
359
|
scope: part.id,
|
|
335
360
|
partIds: true,
|
|
336
361
|
lang,
|
|
362
|
+
...extensionOutputs,
|
|
337
363
|
});
|
|
338
364
|
if (scopeOnly) return result;
|
|
339
365
|
const ownLandmark = /<(?:header|footer)[\s>]/.test(result.html);
|
|
@@ -375,6 +401,7 @@ export function buildSiteExport(input: {
|
|
|
375
401
|
dynamic: templatesFor(layout, parts),
|
|
376
402
|
scope: page.id,
|
|
377
403
|
lang,
|
|
404
|
+
...extensionOutputs,
|
|
378
405
|
});
|
|
379
406
|
const headerPart = partOf(header);
|
|
380
407
|
const footerPart = partOf(footer);
|
|
@@ -403,6 +430,7 @@ export function buildSiteExport(input: {
|
|
|
403
430
|
);
|
|
404
431
|
const css = [
|
|
405
432
|
...all.map((r) => r.css),
|
|
433
|
+
extensionStylesheets(all.flatMap((r) => r.extensions)),
|
|
406
434
|
...(popupRenders.length > 0 ? [POPUP_CHROME_CSS] : []),
|
|
407
435
|
...(floatRenders.length > 0 ? [FLOAT_CHROME_CSS] : []),
|
|
408
436
|
]
|
|
@@ -418,12 +446,23 @@ export function buildSiteExport(input: {
|
|
|
418
446
|
.filter(Boolean)
|
|
419
447
|
.join("\n");
|
|
420
448
|
const used: (keyof typeof RUNTIME_FILES)[] = [];
|
|
449
|
+
const usedExtensions: string[] = [];
|
|
421
450
|
if (popupRenders.length > 0) used.push("popups");
|
|
422
451
|
if (floatRenders.length > 0) used.push("floats");
|
|
423
452
|
if (all.some((r) => r.needsTabsRuntime)) used.push("tabs");
|
|
424
453
|
if (all.some((r) => r.needsMenuRuntime)) used.push("menu");
|
|
425
454
|
// W-1025: the widget runtimes a page asked for.
|
|
426
|
-
for (const name of new Set(all.flatMap((r) => r.runtimes)))
|
|
455
|
+
for (const name of new Set(all.flatMap((r) => r.runtimes))) {
|
|
456
|
+
if (!name.startsWith("ext:")) {
|
|
457
|
+
used.push(name as keyof typeof RUNTIME_FILES);
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
const pkg = name.slice(4);
|
|
461
|
+
const code = extensionAssets(pkg)?.runtime;
|
|
462
|
+
if (!code) continue;
|
|
463
|
+
extensionRuntimes.set(pkg, code);
|
|
464
|
+
usedExtensions.push(`assets/ext/${pkg}.js`);
|
|
465
|
+
}
|
|
427
466
|
for (const name of used) runtimes.add(name);
|
|
428
467
|
|
|
429
468
|
files.push(
|
|
@@ -436,7 +475,9 @@ export function buildSiteExport(input: {
|
|
|
436
475
|
...(input.design.direction ? { dir: input.design.direction } : {}),
|
|
437
476
|
css: "style.css",
|
|
438
477
|
body: html,
|
|
439
|
-
scripts: used.map((name) =>
|
|
478
|
+
scripts: [...used.map((name) => RUNTIME_FILES[name]), ...usedExtensions].map(
|
|
479
|
+
(file) => `../${file}`,
|
|
480
|
+
),
|
|
440
481
|
}),
|
|
441
482
|
},
|
|
442
483
|
{ path: `${folder}/style.css`, data: `${css}\n` },
|
|
@@ -454,6 +495,8 @@ export function buildSiteExport(input: {
|
|
|
454
495
|
|
|
455
496
|
for (const name of runtimes)
|
|
456
497
|
files.push({ path: RUNTIME_FILES[name], data: EXPORT_RUNTIMES[name] });
|
|
498
|
+
for (const [pkg, code] of extensionRuntimes)
|
|
499
|
+
files.push({ path: `assets/ext/${pkg}.js`, data: code });
|
|
457
500
|
report.media = [...media].toSorted();
|
|
458
501
|
files.push(
|
|
459
502
|
{
|
|
@@ -529,7 +572,10 @@ function reportMarkdown(report: ExportReport, date?: string): string {
|
|
|
529
572
|
/** A file name for a copied media URL: its last path segment, made safe and unique. */
|
|
530
573
|
export function mediaFileName(url: string, taken: Set<string>): string {
|
|
531
574
|
const path = url.split(/[?#]/)[0] ?? "";
|
|
532
|
-
const
|
|
575
|
+
const fontId = fontRouteId(url);
|
|
576
|
+
const last = fontId
|
|
577
|
+
? `font-${fontId}.woff2`
|
|
578
|
+
: decodeURIComponentSafe(path.split("/").pop() ?? "");
|
|
533
579
|
const clean =
|
|
534
580
|
last
|
|
535
581
|
.replace(/[^A-Za-z0-9._-]+/g, "-")
|
|
@@ -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 {
|
|
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
|
-
|
|
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
|
-
/**
|
|
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.
|
|
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
|
-
//
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
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
|
-
|
|
85
|
-
if (!
|
|
86
|
-
|
|
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 {
|