@perkyzz/emvb 0.9.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +10 -1
- package/package.json +2 -2
- package/src/admin/agents-api.ts +45 -0
- package/src/admin/bulk-api.ts +67 -0
- package/src/admin/content-api.ts +10 -6
- package/src/admin/copy-rich-text.ts +119 -0
- package/src/admin/dialog-tab-wrap.ts +78 -0
- package/src/admin/editor/A11yCopilot.tsx +3 -1
- package/src/admin/editor/Editor.tsx +311 -250
- package/src/admin/editor/Timeline.tsx +8 -3
- package/src/admin/editor/TopBar.tsx +29 -7
- package/src/admin/editor/canvas/CanvasFrame.tsx +7 -2
- package/src/admin/editor/canvas/DeviceBar.tsx +31 -10
- package/src/admin/editor/canvas/canvas-scale.ts +3 -3
- package/src/admin/editor/canvas/use-canvas-session.ts +2 -2
- package/src/admin/editor/co-design.ts +190 -0
- package/src/admin/editor/dnd/new-element.ts +48 -4
- package/src/admin/editor/editor-chrome.tsx +8 -6
- package/src/admin/editor/editor-commands.ts +15 -4
- package/src/admin/editor/editor-css.ts +2 -1
- package/src/admin/editor/element-chrome.ts +3 -1
- package/src/admin/editor/extension-manifests.ts +20 -0
- package/src/admin/editor/extensions/ExtensionPanels.tsx +26 -0
- package/src/admin/editor/extensions-context.tsx +27 -0
- package/src/admin/editor/feature-css.ts +17 -0
- package/src/admin/editor/palette-items.ts +2 -2
- package/src/admin/editor/panels/AddPanel.tsx +31 -1
- package/src/admin/editor/panels/AgentRequestBox.tsx +116 -0
- package/src/admin/editor/panels/ClassesSection.tsx +9 -6
- package/src/admin/editor/panels/CoDesignPanel.tsx +170 -0
- package/src/admin/editor/panels/ColorControl.tsx +2 -2
- package/src/admin/editor/panels/ElementPanel.tsx +68 -20
- package/src/admin/editor/panels/LanguagesSection.tsx +152 -0
- package/src/admin/editor/panels/LayersPanel.tsx +10 -6
- package/src/admin/editor/panels/LeftPanel.tsx +33 -5
- package/src/admin/editor/panels/PageSettings.tsx +4 -0
- package/src/admin/editor/panels/SettingsPanel.tsx +21 -2
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +7 -7
- package/src/admin/editor/panels/settings/BackgroundControls.tsx +72 -15
- package/src/admin/editor/panels/settings/BackgroundFill.tsx +8 -6
- package/src/admin/editor/panels/settings/BoxControl.tsx +5 -5
- package/src/admin/editor/panels/settings/EffectsControls.tsx +183 -10
- package/src/admin/editor/panels/settings/FieldControl.tsx +9 -3
- package/src/admin/editor/panels/settings/IconControls.tsx +25 -11
- package/src/admin/editor/panels/settings/LengthRow.tsx +63 -19
- package/src/admin/editor/panels/settings/LoopControl.tsx +269 -0
- package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +19 -1
- package/src/admin/editor/panels/settings/MotionControl.tsx +12 -11
- package/src/admin/editor/panels/settings/QueryStyleRows.tsx +7 -4
- package/src/admin/editor/panels/settings/StyleRow.tsx +120 -13
- package/src/admin/editor/panels/settings/TextMarksControl.tsx +247 -0
- package/src/admin/editor/panels/settings/VariableBinding.tsx +2 -2
- package/src/admin/editor/panels/settings/background-mode.ts +14 -11
- package/src/admin/editor/panels/settings/box-link-name.ts +1 -1
- package/src/admin/editor/panels/settings/box-sides.ts +22 -14
- package/src/admin/editor/panels/settings/heading-warning.ts +5 -1
- package/src/admin/editor/panels/settings/length-units.ts +68 -4
- package/src/admin/editor/panels/settings/query-style.ts +3 -3
- package/src/admin/editor/panels/settings/style-sections.ts +52 -10
- package/src/admin/editor/panels/style-target.ts +22 -7
- package/src/admin/editor/presence.ts +60 -0
- package/src/admin/editor/section-templates.ts +26 -4
- package/src/admin/editor/store.ts +14 -4
- package/src/admin/editor/useCoDesign.ts +177 -0
- package/src/admin/editor/useEditorCommands.ts +7 -5
- package/src/admin/editor/useExtensions.ts +45 -0
- package/src/admin/editor/useNodeActions.ts +18 -7
- package/src/admin/editor/useProposals.ts +81 -0
- package/src/admin/export-api.ts +221 -0
- package/src/admin/index.tsx +6 -1
- package/src/admin/languages-api.ts +61 -0
- package/src/admin/pages/AgentSetup.tsx +148 -0
- package/src/admin/pages/AgentsPage.tsx +9 -26
- package/src/admin/pages/CreateDialog.tsx +15 -0
- package/src/admin/pages/ExportDialog.tsx +395 -0
- package/src/admin/pages/ImportButton.tsx +18 -0
- package/src/admin/pages/ImportElementorDialog.tsx +669 -0
- package/src/admin/pages/ImportPageLinks.tsx +86 -0
- package/src/admin/pages/PageList.tsx +82 -3
- package/src/admin/pages/ThemePartList.tsx +52 -2
- package/src/admin/pages/bulk.tsx +303 -0
- package/src/admin/pages/section-head.tsx +27 -0
- package/src/admin/proposals-api.ts +91 -0
- package/src/admin/search.ts +20 -0
- package/src/admin/theme-api.ts +4 -0
- package/src/admin/ui.ts +50 -4
- package/src/admin/widgets/CanvasField.tsx +8 -0
- package/src/admin/widgets/ConvertToRichText.tsx +136 -0
- package/src/admin/widgets/ExportEntryHtml.tsx +92 -0
- package/src/admin/widgets/convert-body.ts +73 -2
- package/src/astro/content-port.ts +14 -1
- package/src/astro/render.ts +6 -6
- package/src/astro/resolve-theme.ts +25 -8
- package/src/astro/resolve.ts +89 -2
- package/src/astro/tabs/runtime.ts +16 -6
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +45 -14
- package/src/core/arrange.ts +5 -1
- package/src/core/bounded-array.ts +31 -0
- package/src/core/clipboard.ts +9 -0
- package/src/core/contract.ts +305 -0
- package/src/core/css/generate.ts +151 -20
- package/src/core/css/scope.ts +10 -2
- package/src/core/css/trace.ts +47 -13
- package/src/core/css-keywords.ts +51 -1
- package/src/core/design/cascade.ts +12 -9
- package/src/core/design/classes.ts +28 -9
- package/src/core/design/devices.ts +89 -13
- package/src/core/design/states.ts +5 -3
- package/src/core/design/transfer.ts +5 -0
- package/src/core/design/variables.ts +31 -5
- package/src/core/elements/dynamic-elements.ts +3 -3
- package/src/core/elements/index.ts +73 -9
- package/src/core/export/runtimes.generated.ts +9 -0
- package/src/core/export/site.ts +562 -0
- package/src/core/export/zip.ts +95 -0
- package/src/core/extensions/define.ts +13 -0
- package/src/core/extensions/index.ts +41 -0
- package/src/core/extensions/namespace.ts +82 -0
- package/src/core/extensions/normalize.ts +141 -0
- package/src/core/extensions/placeholder.ts +41 -0
- package/src/core/extensions/registry.ts +94 -0
- package/src/core/extensions/render-ext.ts +118 -0
- package/src/core/extensions/safe-nodes.ts +93 -0
- package/src/core/extensions/schema.ts +112 -0
- package/src/core/extensions/types.ts +159 -0
- package/src/core/forms/binding.ts +1 -1
- package/src/core/history/diff.ts +26 -1
- package/src/core/import/elementor/atomic.ts +1608 -0
- package/src/core/import/elementor/classes.ts +127 -0
- package/src/core/import/elementor/context.ts +429 -0
- package/src/core/import/elementor/counter-steps.ts +72 -0
- package/src/core/import/elementor/css.ts +445 -0
- package/src/core/import/elementor/html.ts +500 -0
- package/src/core/import/elementor/index.ts +967 -0
- package/src/core/import/elementor/inline-marks.ts +81 -0
- package/src/core/import/elementor/snapshots.ts +48 -0
- package/src/core/import/elementor/style.ts +429 -0
- package/src/core/import/elementor/values.ts +241 -0
- package/src/core/import/elementor/widgets.ts +1897 -0
- package/src/core/index.ts +96 -0
- package/src/core/json-schema.ts +114 -0
- package/src/core/limits.ts +19 -0
- package/src/core/migrate/index.ts +6 -1
- package/src/core/migrate/v18.ts +222 -0
- package/src/core/perf/meter.ts +42 -24
- package/src/core/portable-text-export.ts +243 -0
- package/src/core/proposals-safety.ts +102 -0
- package/src/core/proposals.ts +357 -0
- package/src/core/public-attributes.ts +30 -0
- package/src/core/recipes/index.ts +2 -2
- package/src/core/render/extras.ts +3 -3
- package/src/core/render/index.ts +222 -23
- package/src/core/render/loop.ts +1 -1
- package/src/core/render/section.ts +4 -4
- package/src/core/render/tabs.ts +18 -11
- package/src/core/render/vnode.ts +1 -1
- package/src/core/sanitize/css.ts +151 -10
- package/src/core/sanitize/motion.ts +116 -0
- package/src/core/saved-sections.ts +6 -0
- package/src/core/schema/descriptors.ts +3 -1
- package/src/core/schema/design.ts +4 -3
- package/src/core/schema/layout.ts +92 -16
- package/src/core/schema/style.ts +296 -112
- package/src/core/search-text.ts +70 -0
- package/src/core/text-marks.ts +227 -0
- package/src/core/theme/dynamic.ts +9 -4
- package/src/core/theme/float.ts +2 -0
- package/src/core/theme/popup-rules.ts +33 -1
- package/src/core/translatable.ts +34 -0
- package/src/core/validate.ts +13 -1
- package/src/index.ts +24 -0
- package/src/mcp/errors.ts +3 -1
- package/src/mcp/ports.ts +67 -0
- package/src/mcp/prompts.ts +1 -11
- package/src/mcp/proposals.ts +281 -0
- package/src/mcp/read.ts +10 -10
- package/src/mcp/tools.ts +71 -3
- package/src/mcp/write.ts +66 -11
- package/src/server/agent-settings.ts +5 -1
- package/src/server/icon-routes.ts +52 -12
- package/src/server/mcp-routes.ts +15 -0
- package/src/server/plugin.ts +35 -2
- package/src/server/presence.ts +78 -0
- package/src/server/proposals.ts +261 -0
- package/src/server/requests.ts +196 -0
- package/src/server/tools-served.ts +45 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// oxlint-disable no-underscore-dangle -- Portable Text names its fields _type, _key and _ref.
|
|
2
|
+
import type { Layout, LayoutNode, TextMark } from "./schema/layout.ts";
|
|
3
|
+
import { nodeChildren } from "./tree-ops.ts";
|
|
4
|
+
import { sanitizeUrl } from "./sanitize/url.ts";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* W-963: the reverse of Convert to EmVB. A layout becomes EmDash rich text (Portable Text): the
|
|
8
|
+
* words, headings, lists, links and images, in reading order, without the design. What can't be
|
|
9
|
+
* rich text (forms, embeds, icons, dividers, loops) is counted by type, never guessed at.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
type Span = { _type: "span"; _key: string; text: string; marks: string[] };
|
|
13
|
+
type LinkDef = { _type: "link"; _key: string; href: string; blank?: true };
|
|
14
|
+
export type PortableTextBlock =
|
|
15
|
+
| {
|
|
16
|
+
_type: "block";
|
|
17
|
+
_key: string;
|
|
18
|
+
style: string;
|
|
19
|
+
children: Span[];
|
|
20
|
+
markDefs: LinkDef[];
|
|
21
|
+
listItem?: "bullet" | "number";
|
|
22
|
+
level?: number;
|
|
23
|
+
}
|
|
24
|
+
| {
|
|
25
|
+
_type: "image";
|
|
26
|
+
_key: string;
|
|
27
|
+
asset: { url: string; _ref?: string };
|
|
28
|
+
alt?: string;
|
|
29
|
+
width?: number;
|
|
30
|
+
height?: number;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type PortableTextExport = {
|
|
34
|
+
body: PortableTextBlock[];
|
|
35
|
+
/** Elements that became blocks. */
|
|
36
|
+
converted: number;
|
|
37
|
+
/** Elements left out, by element type (`form: 2`). */
|
|
38
|
+
skipped: Record<string, number>;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const LINK_SCHEMES: ReadonlySet<string> = new Set(["https:", "http:", "mailto:", "tel:"]);
|
|
42
|
+
const IMAGE_SCHEMES: ReadonlySet<string> = new Set(["https:", "http:"]);
|
|
43
|
+
|
|
44
|
+
/** Boxes whose children are read in order; they add nothing of their own. */
|
|
45
|
+
const BOXES = new Set([
|
|
46
|
+
"container",
|
|
47
|
+
"flexbox",
|
|
48
|
+
"grid",
|
|
49
|
+
"div",
|
|
50
|
+
"div-block",
|
|
51
|
+
"layout-section",
|
|
52
|
+
"accordion",
|
|
53
|
+
"tabs",
|
|
54
|
+
]);
|
|
55
|
+
|
|
56
|
+
/** Leaves with no words (or nothing that rich text can hold), left out without a count. */
|
|
57
|
+
const QUIET = new Set(["spacer"]);
|
|
58
|
+
|
|
59
|
+
/** A relative site path is kept as is; anything else must be a safe absolute URL. */
|
|
60
|
+
function safeHref(raw: string | undefined, schemes = LINK_SCHEMES): string | undefined {
|
|
61
|
+
const href = raw?.trim();
|
|
62
|
+
if (!href) return undefined;
|
|
63
|
+
if (href.startsWith("/") && !href.startsWith("//")) return href;
|
|
64
|
+
if (href.startsWith("#")) return href;
|
|
65
|
+
return sanitizeUrl(href, schemes);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function layoutToPortableText(
|
|
69
|
+
layout: Layout,
|
|
70
|
+
options: {
|
|
71
|
+
/** Synced section layouts by part id, so their words come across too. */
|
|
72
|
+
sectionTemplates?: Readonly<Record<string, Layout>>;
|
|
73
|
+
/** Stable keys for tests. */
|
|
74
|
+
key?: () => string;
|
|
75
|
+
} = {},
|
|
76
|
+
): PortableTextExport {
|
|
77
|
+
let counter = 0;
|
|
78
|
+
const key = options.key ?? (() => `k${(counter++).toString(36)}`);
|
|
79
|
+
const body: PortableTextBlock[] = [];
|
|
80
|
+
const skipped: Record<string, number> = {};
|
|
81
|
+
let converted = 0;
|
|
82
|
+
const skip = (type: string) => {
|
|
83
|
+
skipped[type] = (skipped[type] ?? 0) + 1;
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const block = (
|
|
87
|
+
text: string,
|
|
88
|
+
style = "normal",
|
|
89
|
+
marks: readonly TextMark[] = [],
|
|
90
|
+
whole?: { href: string; newTab?: boolean },
|
|
91
|
+
) => {
|
|
92
|
+
const markDefs: LinkDef[] = [];
|
|
93
|
+
const linkKey = (href: string, newTab?: boolean) => {
|
|
94
|
+
const def: LinkDef = { _type: "link", _key: key(), href, ...(newTab ? { blank: true } : {}) };
|
|
95
|
+
markDefs.push(def);
|
|
96
|
+
return def._key;
|
|
97
|
+
};
|
|
98
|
+
const wholeKey = whole ? linkKey(whole.href, whole.newTab) : undefined;
|
|
99
|
+
const usable = marks.filter((m) => m.end > m.start && m.start < text.length);
|
|
100
|
+
const cuts = new Set([0, text.length]);
|
|
101
|
+
for (const m of usable) {
|
|
102
|
+
cuts.add(Math.min(m.start, text.length));
|
|
103
|
+
cuts.add(Math.min(m.end, text.length));
|
|
104
|
+
}
|
|
105
|
+
const points = [...cuts].toSorted((a, b) => a - b);
|
|
106
|
+
const markKeys = new Map<TextMark, string>();
|
|
107
|
+
const children: Span[] = [];
|
|
108
|
+
for (let i = 0; i < points.length - 1; i += 1) {
|
|
109
|
+
const from = points[i] ?? 0;
|
|
110
|
+
const to = points[i + 1] ?? 0;
|
|
111
|
+
if (to <= from) continue;
|
|
112
|
+
const names: string[] = wholeKey ? [wholeKey] : [];
|
|
113
|
+
for (const m of usable) {
|
|
114
|
+
if (m.start > from || m.end < to) continue;
|
|
115
|
+
if (m.bold) names.push("strong");
|
|
116
|
+
if (m.italic) names.push("em");
|
|
117
|
+
const href = wholeKey ? undefined : safeHref(m.href);
|
|
118
|
+
if (href) {
|
|
119
|
+
let k = markKeys.get(m);
|
|
120
|
+
if (!k) {
|
|
121
|
+
k = linkKey(href, m.newTab);
|
|
122
|
+
markKeys.set(m, k);
|
|
123
|
+
}
|
|
124
|
+
names.push(k);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
children.push({ _type: "span", _key: key(), text: text.slice(from, to), marks: names });
|
|
128
|
+
}
|
|
129
|
+
if (children.length === 0) children.push({ _type: "span", _key: key(), text: "", marks: [] });
|
|
130
|
+
body.push({ _type: "block", _key: key(), style, children, markDefs });
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
const visit = (node: LayoutNode, seen: ReadonlySet<string>) => {
|
|
134
|
+
// An A/B test's B element repeats what A says; A is kept.
|
|
135
|
+
if (node.variant?.arm === "b") {
|
|
136
|
+
skip("A/B test variant");
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
const props = (node.props ?? {}) as Record<string, unknown>;
|
|
140
|
+
const text = typeof props["text"] === "string" ? props["text"].trim() : "";
|
|
141
|
+
const marks = Array.isArray(props["marks"]) ? (props["marks"] as TextMark[]) : [];
|
|
142
|
+
switch (node.type) {
|
|
143
|
+
case "heading": {
|
|
144
|
+
if (!text) return;
|
|
145
|
+
const href = safeHref(props["href"] as string | undefined);
|
|
146
|
+
block(
|
|
147
|
+
text,
|
|
148
|
+
`h${Number(props["level"]) || 2}`,
|
|
149
|
+
href ? [] : marks,
|
|
150
|
+
href ? { href, newTab: props["newTab"] === true } : undefined,
|
|
151
|
+
);
|
|
152
|
+
converted++;
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
case "text":
|
|
156
|
+
case "label":
|
|
157
|
+
case "span": {
|
|
158
|
+
if (!text) return;
|
|
159
|
+
block(text, "normal", marks);
|
|
160
|
+
converted++;
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
case "link":
|
|
164
|
+
case "button": {
|
|
165
|
+
if (!text) return;
|
|
166
|
+
const href = safeHref(props["href"] as string | undefined);
|
|
167
|
+
block(text, "normal", [], href ? { href, newTab: props["newTab"] === true } : undefined);
|
|
168
|
+
converted++;
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
case "list": {
|
|
172
|
+
const items = Array.isArray(props["items"]) ? props["items"] : [];
|
|
173
|
+
const ordered =
|
|
174
|
+
props["ordered"] === true ||
|
|
175
|
+
(typeof props["marker"] === "string" &&
|
|
176
|
+
!["disc", "circle", "square", "none"].includes(props["marker"]));
|
|
177
|
+
let any = false;
|
|
178
|
+
for (const item of items) {
|
|
179
|
+
if (typeof item !== "string" || !item.trim()) continue;
|
|
180
|
+
block(item.trim());
|
|
181
|
+
const last = body.at(-1);
|
|
182
|
+
if (last?._type === "block") {
|
|
183
|
+
last.listItem = ordered ? "number" : "bullet";
|
|
184
|
+
last.level = 1;
|
|
185
|
+
}
|
|
186
|
+
any = true;
|
|
187
|
+
}
|
|
188
|
+
if (any) converted++;
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
case "image": {
|
|
192
|
+
const src = safeHref(props["src"] as string | undefined, IMAGE_SCHEMES);
|
|
193
|
+
if (!src) {
|
|
194
|
+
skip("image");
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const alt = typeof props["alt"] === "string" ? props["alt"] : "";
|
|
198
|
+
const mediaId = typeof props["mediaId"] === "string" ? props["mediaId"] : undefined;
|
|
199
|
+
body.push({
|
|
200
|
+
_type: "image",
|
|
201
|
+
_key: key(),
|
|
202
|
+
asset: { url: src, ...(mediaId ? { _ref: mediaId } : {}) },
|
|
203
|
+
...(alt && props["decorative"] !== true ? { alt } : {}),
|
|
204
|
+
...(typeof props["width"] === "number" ? { width: props["width"] } : {}),
|
|
205
|
+
...(typeof props["height"] === "number" ? { height: props["height"] } : {}),
|
|
206
|
+
});
|
|
207
|
+
converted++;
|
|
208
|
+
if (typeof props["caption"] === "string" && props["caption"].trim())
|
|
209
|
+
block(props["caption"].trim());
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
case "accordion-item":
|
|
213
|
+
case "tab-panel": {
|
|
214
|
+
const title = node.type === "accordion-item" ? props["summary"] : props["label"];
|
|
215
|
+
if (typeof title === "string" && title.trim()) {
|
|
216
|
+
block(title.trim(), "h3");
|
|
217
|
+
converted++;
|
|
218
|
+
}
|
|
219
|
+
for (const child of nodeChildren(node)) visit(child, seen);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
case "part-ref": {
|
|
223
|
+
const partId = typeof props["partId"] === "string" ? props["partId"] : "";
|
|
224
|
+
const synced = options.sectionTemplates?.[partId];
|
|
225
|
+
if (synced && !seen.has(partId)) {
|
|
226
|
+
visit(synced.root, new Set([...seen, partId]));
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
for (const child of nodeChildren(node)) visit(child, seen);
|
|
230
|
+
return;
|
|
231
|
+
}
|
|
232
|
+
default: {
|
|
233
|
+
if (BOXES.has(node.type)) {
|
|
234
|
+
for (const child of nodeChildren(node)) visit(child, seen);
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (!QUIET.has(node.type)) skip(node.type);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
};
|
|
241
|
+
visit(layout.root, new Set());
|
|
242
|
+
return { body, converted, skipped };
|
|
243
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { ELEMENT_DESCRIPTORS } from "./elements/index.ts";
|
|
2
|
+
import { isFlatMotionKey } from "./migrate/v18.ts";
|
|
3
|
+
import { isRecord } from "./record.ts";
|
|
4
|
+
import { isSafeCssValue, isSafeFontStack } from "./sanitize/css.ts";
|
|
5
|
+
import { sanitizeHref } from "./sanitize/href.ts";
|
|
6
|
+
import { sanitizeMediaUrl } from "./sanitize/media-url.ts";
|
|
7
|
+
import { isSafeSvgMarkup } from "./sanitize/svg.ts";
|
|
8
|
+
import type { DesignSystem } from "./schema/design.ts";
|
|
9
|
+
import type { Layout } from "./schema/layout.ts";
|
|
10
|
+
import { stableJson } from "./stable-json.ts";
|
|
11
|
+
import { findNode, forEachNode } from "./tree-ops.ts";
|
|
12
|
+
|
|
13
|
+
const same = (a: unknown, b: unknown) => stableJson(a) === stableJson(b);
|
|
14
|
+
|
|
15
|
+
function styleRefusal(raw: unknown, key = ""): string | undefined {
|
|
16
|
+
if (typeof raw === "string" && raw !== "") {
|
|
17
|
+
const safe = key === "fontFamily" ? isSafeFontStack(raw) : isSafeCssValue(raw);
|
|
18
|
+
if (!safe) return `Unsafe style value at ${key}.`;
|
|
19
|
+
}
|
|
20
|
+
if (Array.isArray(raw)) {
|
|
21
|
+
for (const value of raw) {
|
|
22
|
+
const reason = styleRefusal(value, key);
|
|
23
|
+
if (reason) return reason;
|
|
24
|
+
}
|
|
25
|
+
} else if (isRecord(raw)) {
|
|
26
|
+
for (const [name, value] of Object.entries(raw)) {
|
|
27
|
+
if (key !== "motion" && isFlatMotionKey(name))
|
|
28
|
+
return "Use frozen style.motion keys, not legacy motion fields.";
|
|
29
|
+
if ((name === "src" || name === "url") && typeof value === "string") {
|
|
30
|
+
if (value.trim() && !sanitizeMediaUrl(value)) return "Unsafe background media URL.";
|
|
31
|
+
} else {
|
|
32
|
+
const reason = styleRefusal(value, name);
|
|
33
|
+
if (reason) return reason;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Reuse the renderer's allowlists. Old, unchanged unsafe fields remain readable like MCP writes. */
|
|
41
|
+
export function proposalSafetyRefusal(
|
|
42
|
+
before: Layout,
|
|
43
|
+
after: Layout,
|
|
44
|
+
oldDesign: DesignSystem,
|
|
45
|
+
design: DesignSystem,
|
|
46
|
+
): string | undefined {
|
|
47
|
+
let reason: string | undefined;
|
|
48
|
+
forEachNode(after.root, (node) => {
|
|
49
|
+
if (reason) return;
|
|
50
|
+
const old = findNode(before, node.id);
|
|
51
|
+
const props = node.props as Record<string, unknown>;
|
|
52
|
+
const oldProps = old?.props as Record<string, unknown> | undefined;
|
|
53
|
+
for (const field of ELEMENT_DESCRIPTORS.find((d) => d.type === node.type)?.fields ?? []) {
|
|
54
|
+
const value = props[field.key];
|
|
55
|
+
if (
|
|
56
|
+
field.kind === "href" &&
|
|
57
|
+
typeof value === "string" &&
|
|
58
|
+
value.trim() &&
|
|
59
|
+
value !== oldProps?.[field.key] &&
|
|
60
|
+
sanitizeHref(value) === undefined
|
|
61
|
+
)
|
|
62
|
+
reason = `Unsafe link on ${node.id}.${field.key}.`;
|
|
63
|
+
}
|
|
64
|
+
if (!same(node.props, old?.props)) {
|
|
65
|
+
const sources = node.type === "image" && Array.isArray(props.sources) ? props.sources : [];
|
|
66
|
+
const urls = [
|
|
67
|
+
node.type === "image" || node.type === "video" ? props.src : undefined,
|
|
68
|
+
...sources.map((s: { src?: unknown }) => s.src),
|
|
69
|
+
];
|
|
70
|
+
if (urls.some((url) => typeof url === "string" && url.trim() && !sanitizeMediaUrl(url)))
|
|
71
|
+
reason = `Unsafe media URL on ${node.id}.`;
|
|
72
|
+
for (const markup of [node.type === "svg" ? props.markup : undefined, props.iconSvg]) {
|
|
73
|
+
if (typeof markup === "string" && markup.trim() && !isSafeSvgMarkup(markup))
|
|
74
|
+
reason = `Unsafe SVG on ${node.id}.`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (const key of ["style", "states", "devices", "query"] as const) {
|
|
78
|
+
if (!same(node[key], old?.[key])) reason ??= styleRefusal(node[key]);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
for (const cls of design.classes ?? []) {
|
|
82
|
+
if (
|
|
83
|
+
!same(
|
|
84
|
+
cls,
|
|
85
|
+
oldDesign.classes?.find((c) => c.id === cls.id),
|
|
86
|
+
)
|
|
87
|
+
) {
|
|
88
|
+
for (const raw of [cls.style, cls.states, cls.devices]) reason ??= styleRefusal(raw);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
for (const font of design.variables.fonts ?? []) {
|
|
92
|
+
if (
|
|
93
|
+
!same(
|
|
94
|
+
font,
|
|
95
|
+
oldDesign.variables.fonts?.find((v) => v.id === font.id),
|
|
96
|
+
) &&
|
|
97
|
+
!isSafeFontStack(font.value)
|
|
98
|
+
)
|
|
99
|
+
reason ??= "Unsafe font stack.";
|
|
100
|
+
}
|
|
101
|
+
return reason;
|
|
102
|
+
}
|