@perkyzz/emvb 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (170) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +57 -2
  3. package/package.json +72 -4
  4. package/src/admin/access/RequireEditor.tsx +39 -0
  5. package/src/admin/access/role.ts +36 -0
  6. package/src/admin/access/sidebar-shim.ts +108 -0
  7. package/src/admin/api.ts +48 -0
  8. package/src/admin/content-api.ts +147 -0
  9. package/src/admin/editor/Editor.tsx +457 -0
  10. package/src/admin/editor/EditorOverlay.tsx +54 -0
  11. package/src/admin/editor/ShortcutsDialog.tsx +30 -0
  12. package/src/admin/editor/SmallScreenNotice.tsx +20 -0
  13. package/src/admin/editor/TopBar.tsx +179 -0
  14. package/src/admin/editor/canvas/CanvasFrame.tsx +355 -0
  15. package/src/admin/editor/canvas/CanvasTextEdit.tsx +87 -0
  16. package/src/admin/editor/canvas/DeviceBar.tsx +50 -0
  17. package/src/admin/editor/canvas/EmptyCanvas.tsx +49 -0
  18. package/src/admin/editor/canvas/SelectionOverlay.tsx +185 -0
  19. package/src/admin/editor/canvas/canvas-events.ts +232 -0
  20. package/src/admin/editor/canvas/state-preview.ts +19 -0
  21. package/src/admin/editor/canvas/tab-reveal.ts +35 -0
  22. package/src/admin/editor/canvas/vnode-react.tsx +49 -0
  23. package/src/admin/editor/clipboard-store.ts +44 -0
  24. package/src/admin/editor/dialogs.tsx +125 -0
  25. package/src/admin/editor/dnd/drag-stash.ts +27 -0
  26. package/src/admin/editor/dnd/drop-target.ts +101 -0
  27. package/src/admin/editor/dnd/new-element.ts +32 -0
  28. package/src/admin/editor/editor-css.ts +539 -0
  29. package/src/admin/editor/exit.ts +27 -0
  30. package/src/admin/editor/panels/AddPanel.tsx +142 -0
  31. package/src/admin/editor/panels/ClassesSection.tsx +334 -0
  32. package/src/admin/editor/panels/ColorControl.tsx +230 -0
  33. package/src/admin/editor/panels/ConditionsEditor.tsx +201 -0
  34. package/src/admin/editor/panels/ElementPanel.tsx +629 -0
  35. package/src/admin/editor/panels/LayersPanel.tsx +336 -0
  36. package/src/admin/editor/panels/LeftPanel.tsx +77 -0
  37. package/src/admin/editor/panels/PageSettings.tsx +126 -0
  38. package/src/admin/editor/panels/SettingsPanel.tsx +101 -0
  39. package/src/admin/editor/panels/SiteStylesDrawer.tsx +392 -0
  40. package/src/admin/editor/panels/TriggerSettings.tsx +132 -0
  41. package/src/admin/editor/panels/TriggersEditor.tsx +197 -0
  42. package/src/admin/editor/panels/VariableSection.tsx +492 -0
  43. package/src/admin/editor/panels/settings/BackgroundControls.tsx +320 -0
  44. package/src/admin/editor/panels/settings/ClassChip.tsx +167 -0
  45. package/src/admin/editor/panels/settings/ClassChipInput.tsx +349 -0
  46. package/src/admin/editor/panels/settings/DraftTextField.tsx +201 -0
  47. package/src/admin/editor/panels/settings/EffectsControls.tsx +347 -0
  48. package/src/admin/editor/panels/settings/FieldBindControl.tsx +83 -0
  49. package/src/admin/editor/panels/settings/FieldControl.tsx +150 -0
  50. package/src/admin/editor/panels/settings/FormBindControl.tsx +97 -0
  51. package/src/admin/editor/panels/settings/IconPicker.tsx +109 -0
  52. package/src/admin/editor/panels/settings/LengthRow.tsx +181 -0
  53. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +124 -0
  54. package/src/admin/editor/panels/settings/MediaPicker.tsx +218 -0
  55. package/src/admin/editor/panels/settings/NumberRow.tsx +154 -0
  56. package/src/admin/editor/panels/settings/StateSwitcher.tsx +87 -0
  57. package/src/admin/editor/panels/settings/StyleRow.tsx +480 -0
  58. package/src/admin/editor/panels/settings/VariableBinding.tsx +167 -0
  59. package/src/admin/editor/panels/settings/class-suggest.ts +51 -0
  60. package/src/admin/editor/panels/settings/length-units.ts +83 -0
  61. package/src/admin/editor/panels/settings/node-props.ts +11 -0
  62. package/src/admin/editor/panels/settings/style-sections.ts +291 -0
  63. package/src/admin/editor/panels/site-list.tsx +206 -0
  64. package/src/admin/editor/panels/variable-values.ts +73 -0
  65. package/src/admin/editor/section-templates.ts +50 -0
  66. package/src/admin/editor/shortcuts.ts +20 -0
  67. package/src/admin/editor/store.ts +212 -0
  68. package/src/admin/editor/useClipboardActions.ts +136 -0
  69. package/src/admin/editor/useEditorCommands.ts +142 -0
  70. package/src/admin/editor/useEditorData.ts +167 -0
  71. package/src/admin/editor/useEditorShortcuts.ts +148 -0
  72. package/src/admin/editor/useMediaQuery.ts +20 -0
  73. package/src/admin/editor/useNodeActions.ts +122 -0
  74. package/src/admin/editor/useSave.ts +185 -0
  75. package/src/admin/forms-api.ts +73 -0
  76. package/src/admin/index.tsx +20 -0
  77. package/src/admin/media-api.ts +68 -0
  78. package/src/admin/pages/CreateDialog.tsx +150 -0
  79. package/src/admin/pages/EditorPage.tsx +57 -0
  80. package/src/admin/pages/NewPageDialog.tsx +83 -0
  81. package/src/admin/pages/NewThemePartDialog.tsx +61 -0
  82. package/src/admin/pages/PageList.tsx +71 -0
  83. package/src/admin/pages/PagesPage.tsx +25 -0
  84. package/src/admin/pages/SetupShell.tsx +90 -0
  85. package/src/admin/pages/ThemeBuilderPage.tsx +25 -0
  86. package/src/admin/pages/ThemePartList.tsx +202 -0
  87. package/src/admin/pages/list-kit.tsx +75 -0
  88. package/src/admin/setup/plan.ts +150 -0
  89. package/src/admin/setup/run.ts +61 -0
  90. package/src/admin/setup/spec.ts +100 -0
  91. package/src/admin/setup/useSetupStatus.ts +62 -0
  92. package/src/admin/theme-api.ts +186 -0
  93. package/src/admin/ui.ts +231 -0
  94. package/src/admin/widgets/LayoutField.tsx +67 -0
  95. package/src/astro/EmVBPage.astro +37 -0
  96. package/src/astro/env.d.ts +5 -0
  97. package/src/astro/forms/EmVBFormsRuntime.astro +9 -0
  98. package/src/astro/forms/index.ts +1 -0
  99. package/src/astro/forms-definitions.ts +104 -0
  100. package/src/astro/index.ts +15 -0
  101. package/src/astro/popups/EmVBPopupsRuntime.astro +9 -0
  102. package/src/astro/popups/index.ts +1 -0
  103. package/src/astro/popups/runtime.ts +246 -0
  104. package/src/astro/render.ts +74 -0
  105. package/src/astro/resolve-theme.ts +371 -0
  106. package/src/astro/resolve.ts +65 -0
  107. package/src/astro/tabs/EmVBTabsRuntime.astro +10 -0
  108. package/src/astro/tabs/index.ts +1 -0
  109. package/src/astro/tabs/runtime.ts +111 -0
  110. package/src/astro/theme-context.ts +81 -0
  111. package/src/astro/theme-posts.ts +84 -0
  112. package/src/constants.ts +12 -0
  113. package/src/core/arrange.ts +360 -0
  114. package/src/core/clipboard.ts +422 -0
  115. package/src/core/css/generate.ts +219 -0
  116. package/src/core/css/scope.ts +88 -0
  117. package/src/core/design/cascade.ts +28 -0
  118. package/src/core/design/classes.ts +138 -0
  119. package/src/core/design/devices.ts +56 -0
  120. package/src/core/design/states.ts +49 -0
  121. package/src/core/design/transfer.ts +33 -0
  122. package/src/core/design/variables.ts +319 -0
  123. package/src/core/elements/definition.ts +12 -0
  124. package/src/core/elements/dynamic-elements.ts +173 -0
  125. package/src/core/elements/form-elements.ts +204 -0
  126. package/src/core/elements/index.ts +742 -0
  127. package/src/core/elements/plain-text.ts +32 -0
  128. package/src/core/forms/binding.ts +61 -0
  129. package/src/core/forms/definition.ts +34 -0
  130. package/src/core/forms/success-layout.ts +63 -0
  131. package/src/core/icons/catalog.ts +273 -0
  132. package/src/core/index.ts +316 -0
  133. package/src/core/limits.ts +7 -0
  134. package/src/core/migrate/index.ts +76 -0
  135. package/src/core/render/context.ts +14 -0
  136. package/src/core/render/dynamic-post.ts +147 -0
  137. package/src/core/render/extras.ts +79 -0
  138. package/src/core/render/form.ts +137 -0
  139. package/src/core/render/index.ts +274 -0
  140. package/src/core/render/loop.ts +36 -0
  141. package/src/core/render/section.ts +33 -0
  142. package/src/core/render/tabs.ts +72 -0
  143. package/src/core/render/vnode.ts +87 -0
  144. package/src/core/sanitize/css.ts +591 -0
  145. package/src/core/sanitize/embed-url.ts +103 -0
  146. package/src/core/sanitize/escape.ts +10 -0
  147. package/src/core/sanitize/href.ts +6 -0
  148. package/src/core/sanitize/media-url.ts +7 -0
  149. package/src/core/sanitize/svg.ts +293 -0
  150. package/src/core/sanitize/url.ts +31 -0
  151. package/src/core/schema/descriptors.ts +33 -0
  152. package/src/core/schema/design.ts +88 -0
  153. package/src/core/schema/layout.ts +817 -0
  154. package/src/core/schema/state-names.ts +4 -0
  155. package/src/core/schema/style.ts +257 -0
  156. package/src/core/stats.ts +18 -0
  157. package/src/core/tabs/presence.ts +16 -0
  158. package/src/core/theme/conditions.ts +321 -0
  159. package/src/core/theme/dynamic.ts +133 -0
  160. package/src/core/theme/parse-doc.ts +36 -0
  161. package/src/core/theme/part-types.ts +79 -0
  162. package/src/core/theme/popup-markup.ts +39 -0
  163. package/src/core/theme/popup-rules.ts +61 -0
  164. package/src/core/theme/triggers.ts +104 -0
  165. package/src/core/tree-ops.ts +139 -0
  166. package/src/core/validate.ts +185 -0
  167. package/src/index.ts +18 -0
  168. package/src/server/design-routes.ts +125 -0
  169. package/src/server/hooks.ts +98 -0
  170. package/src/server/plugin.ts +39 -0
@@ -0,0 +1,103 @@
1
+ /**
2
+ * YouTube / Vimeo → privacy-enhanced embed URLs, or media-library video sources (R-013 / R-032).
3
+ * Never passes through autoplay query params.
4
+ */
5
+
6
+ import { sanitizeMediaUrl } from "./media-url.ts";
7
+
8
+ export type EmbedTarget =
9
+ | { kind: "iframe"; src: string; provider: "youtube" | "vimeo" }
10
+ | { kind: "video"; src: string };
11
+
12
+ const YT_HOSTS = new Set([
13
+ "youtube.com",
14
+ "www.youtube.com",
15
+ "m.youtube.com",
16
+ "youtu.be",
17
+ "www.youtu.be",
18
+ "youtube-nocookie.com",
19
+ "www.youtube-nocookie.com",
20
+ ]);
21
+
22
+ const VIMEO_HOSTS = new Set(["vimeo.com", "www.vimeo.com", "player.vimeo.com"]);
23
+
24
+ const matching = (id: string | undefined, pattern: RegExp) =>
25
+ id && pattern.test(id) ? id : undefined;
26
+ const YT_ID = /^[\w-]{6,}$/;
27
+ const VIMEO_ID = /^\d{6,}$/;
28
+ const firstSegment = (path: string) => path.split("/").find(Boolean);
29
+
30
+ function youtubeId(parsed: URL): string | undefined {
31
+ const host = parsed.hostname.toLowerCase();
32
+ const path = parsed.pathname;
33
+ if (host === "youtu.be" || host === "www.youtu.be") return matching(firstSegment(path), YT_ID);
34
+ for (const prefix of ["/embed/", "/shorts/"]) {
35
+ if (path.startsWith(prefix)) return matching(path.slice(prefix.length).split("/")[0], YT_ID);
36
+ }
37
+ return matching(parsed.searchParams.get("v") ?? undefined, YT_ID);
38
+ }
39
+
40
+ function vimeoId(parsed: URL): string | undefined {
41
+ if (parsed.hostname.toLowerCase() !== "player.vimeo.com") {
42
+ return matching(firstSegment(parsed.pathname), VIMEO_ID);
43
+ }
44
+ const parts = parsed.pathname.split("/").filter(Boolean);
45
+ const videoIdx = parts.indexOf("video");
46
+ return matching(videoIdx >= 0 ? parts[videoIdx + 1] : parts[0], VIMEO_ID);
47
+ }
48
+
49
+ /** Embed URLs are built from the id alone, so no query param (autoplay included) carries over. */
50
+ const PROVIDERS = [
51
+ {
52
+ provider: "youtube",
53
+ hosts: YT_HOSTS,
54
+ id: youtubeId,
55
+ embed: "https://www.youtube-nocookie.com/embed/",
56
+ },
57
+ { provider: "vimeo", hosts: VIMEO_HOSTS, id: vimeoId, embed: "https://player.vimeo.com/video/" },
58
+ ] as const;
59
+
60
+ /**
61
+ * Resolves an editor-supplied URL to a safe embed target, or undefined if refused.
62
+ * YouTube always becomes `youtube-nocookie.com`; Vimeo becomes `player.vimeo.com`.
63
+ */
64
+ export function resolveEmbedUrl(value: string): EmbedTarget | undefined {
65
+ const trimmed = value.trim();
66
+ if (trimmed.length === 0 || trimmed.length > 2000) return undefined;
67
+
68
+ const hasScheme = /^[a-z][a-z0-9+.-]*:/i.test(trimmed);
69
+ if (!hasScheme) {
70
+ const media = sanitizeMediaUrl(trimmed);
71
+ if (!media) return undefined;
72
+ return { kind: "video", src: media };
73
+ }
74
+
75
+ let parsed: URL;
76
+ try {
77
+ parsed = new URL(trimmed.replace(/\s+/g, ""));
78
+ } catch {
79
+ return undefined;
80
+ }
81
+ const scheme = parsed.protocol.toLowerCase();
82
+ if (scheme !== "http:" && scheme !== "https:") return undefined;
83
+
84
+ const host = parsed.hostname.toLowerCase();
85
+ const provider = PROVIDERS.find((p) => p.hosts.has(host));
86
+ if (provider) {
87
+ const id = provider.id(parsed);
88
+ return id
89
+ ? { kind: "iframe", src: `${provider.embed}${id}`, provider: provider.provider }
90
+ : undefined;
91
+ }
92
+
93
+ // Allowlisted http(s) media file URLs for <video> (CDN / EmDash media file route).
94
+ const media = sanitizeMediaUrl(trimmed);
95
+ if (!media) return undefined;
96
+ if (
97
+ !/\.(mp4|webm|ogg)(?:$|\?)/i.test(parsed.pathname) &&
98
+ !media.includes("/_emdash/api/media/file/")
99
+ ) {
100
+ return undefined;
101
+ }
102
+ return { kind: "video", src: media };
103
+ }
@@ -0,0 +1,10 @@
1
+ const TEXT_ENTITIES: Record<string, string> = { "&": "&amp;", "<": "&lt;", ">": "&gt;" };
2
+ const ATTR_ENTITIES: Record<string, string> = { ...TEXT_ENTITIES, '"': "&quot;", "'": "&#39;" };
3
+
4
+ export function escapeText(value: string): string {
5
+ return value.replace(/[&<>]/g, (ch) => TEXT_ENTITIES[ch] ?? ch);
6
+ }
7
+
8
+ export function escapeAttr(value: string): string {
9
+ return value.replace(/[&<>"']/g, (ch) => ATTR_ENTITIES[ch] ?? ch);
10
+ }
@@ -0,0 +1,6 @@
1
+ import { sanitizeUrl } from "./url.ts";
2
+
3
+ const HREF_SCHEMES: ReadonlySet<string> = new Set(["http:", "https:", "mailto:", "tel:"]);
4
+
5
+ /** Allowed public hrefs (R-032): http(s), mailto, tel, and relative paths, queries and hashes. */
6
+ export const sanitizeHref = (value: string): string | undefined => sanitizeUrl(value, HREF_SCHEMES);
@@ -0,0 +1,7 @@
1
+ import { sanitizeUrl } from "./url.ts";
2
+
3
+ const MEDIA_SCHEMES: ReadonlySet<string> = new Set(["http:", "https:"]);
4
+
5
+ /** Allowed image and media `src` values (R-032): http(s) and same-origin relative paths. */
6
+ export const sanitizeMediaUrl = (value: string): string | undefined =>
7
+ sanitizeUrl(value, MEDIA_SCHEMES);
@@ -0,0 +1,293 @@
1
+ import { sanitizeMediaUrl } from "./media-url.ts";
2
+ import type { VNode } from "../render/vnode.ts";
3
+
4
+ /**
5
+ * SVG allowlist (R-032 / W-073 / W-079).
6
+ *
7
+ * Expanded carefully for authors: defs/symbol/use (fragment href only),
8
+ * image (http(s)/relative via sanitizeMediaUrl), gradients/stops, text/tspan,
9
+ * clipPath/mask/pattern/marker. Still rejects script, foreignObject, anchors,
10
+ * iframe, style elements/attrs, event handlers, and javascript:/data: urls.
11
+ */
12
+
13
+ const SVG_TAGS = new Set([
14
+ "svg",
15
+ "g",
16
+ "path",
17
+ "circle",
18
+ "ellipse",
19
+ "rect",
20
+ "line",
21
+ "polyline",
22
+ "polygon",
23
+ "title",
24
+ "desc",
25
+ "defs",
26
+ "symbol",
27
+ "use",
28
+ "image",
29
+ "lineargradient",
30
+ "radialgradient",
31
+ "stop",
32
+ "clippath",
33
+ "mask",
34
+ "pattern",
35
+ "marker",
36
+ "text",
37
+ "tspan",
38
+ ]);
39
+
40
+ const ATTR_ALLOW = new Set([
41
+ "viewBox",
42
+ "xmlns",
43
+ "fill",
44
+ "stroke",
45
+ "stroke-width",
46
+ "stroke-linecap",
47
+ "stroke-linejoin",
48
+ "stroke-dasharray",
49
+ "stroke-opacity",
50
+ "fill-opacity",
51
+ "opacity",
52
+ "d",
53
+ "cx",
54
+ "cy",
55
+ "r",
56
+ "rx",
57
+ "ry",
58
+ "x",
59
+ "y",
60
+ "x1",
61
+ "y1",
62
+ "x2",
63
+ "y2",
64
+ "width",
65
+ "height",
66
+ "points",
67
+ "transform",
68
+ "role",
69
+ "aria-hidden",
70
+ "aria-label",
71
+ "focusable",
72
+ "id",
73
+ "href",
74
+ "xlink:href",
75
+ "preserveAspectRatio",
76
+ "gradientUnits",
77
+ "gradientTransform",
78
+ "offset",
79
+ "stop-color",
80
+ "stop-opacity",
81
+ "clipPathUnits",
82
+ "maskUnits",
83
+ "maskContentUnits",
84
+ "patternUnits",
85
+ "patternContentUnits",
86
+ "patternTransform",
87
+ "markerUnits",
88
+ "markerWidth",
89
+ "markerHeight",
90
+ "refX",
91
+ "refY",
92
+ "orient",
93
+ "font-size",
94
+ "font-family",
95
+ "font-weight",
96
+ "text-anchor",
97
+ "dominant-baseline",
98
+ "dx",
99
+ "dy",
100
+ "clip-path",
101
+ "mask",
102
+ ]);
103
+
104
+ /** Hard reject before parse — script/handlers/foreignObject/style/a/iframe and data: urls. */
105
+ const FORBIDDEN =
106
+ /<script\b|<\/script\b|\bon[a-z]+\s*=|javascript:|data:|<\s*foreignObject\b|<\s*style\b|<\s*iframe\b|<\s*a\b/i;
107
+
108
+ /** Transform attributes take only a list of SVG transform functions with the right arity. */
109
+ const TRANSFORM_ATTRS = new Set(["transform", "gradientTransform", "patternTransform"]);
110
+ const TRANSFORM_ARITY: Record<string, readonly number[]> = {
111
+ matrix: [6],
112
+ translate: [1, 2],
113
+ scale: [1, 2],
114
+ rotate: [1, 3],
115
+ skewX: [1],
116
+ skewY: [1],
117
+ };
118
+ const NUMBER = String.raw`[+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?`;
119
+ const TRANSFORM_FN = new RegExp(
120
+ String.raw`\s*([A-Za-z]+)\s*\(\s*(${NUMBER}(?:(?:\s*,\s*|\s+)${NUMBER})*)\s*\)\s*,?`,
121
+ "y",
122
+ );
123
+
124
+ function isTransformList(value: string): boolean {
125
+ if (value.trim() === "") return false;
126
+ TRANSFORM_FN.lastIndex = 0;
127
+ let end = 0;
128
+ for (let match = TRANSFORM_FN.exec(value); match; match = TRANSFORM_FN.exec(value)) {
129
+ const name = match[1] ?? "";
130
+ const arity = Object.hasOwn(TRANSFORM_ARITY, name) ? TRANSFORM_ARITY[name] : undefined;
131
+ const count = (match[2] ?? "").split(/\s*,\s*|\s+/).length;
132
+ if (!arity?.includes(count)) return false;
133
+ end = TRANSFORM_FN.lastIndex;
134
+ }
135
+ return end === value.length && !/,\s*$/.test(value);
136
+ }
137
+ /** The only elements whose text content is kept. */
138
+ const TEXT_TAGS = new Set(["title", "desc", "text", "tspan"]);
139
+
140
+ const FRAGMENT_HREF = /^#[A-Za-z_][\w.-]*$/;
141
+ const URL_HASH_REF = /^url\(\s*#[A-Za-z_][\w.-]*\s*\)$/i;
142
+ const MAX_MARKUP = 32_768;
143
+
144
+ type Tok =
145
+ | { kind: "open"; tag: string; attrs: Record<string, string>; selfClosing: boolean }
146
+ | { kind: "close"; tag: string }
147
+ | { kind: "text"; value: string };
148
+
149
+ function decodeAttr(raw: string): string {
150
+ return raw
151
+ .replace(/&lt;/g, "<")
152
+ .replace(/&gt;/g, ">")
153
+ .replace(/&quot;/g, '"')
154
+ .replace(/&#39;/g, "'")
155
+ .replace(/&amp;/g, "&");
156
+ }
157
+
158
+ function isSafeHref(tag: string, name: string, value: string): boolean {
159
+ if (name !== "href" && name !== "xlink:href") return true;
160
+ if (tag === "use") return FRAGMENT_HREF.test(value);
161
+ if (tag === "image") return sanitizeMediaUrl(value) !== undefined;
162
+ // Other tags should not carry href.
163
+ return false;
164
+ }
165
+
166
+ function isSafeUrlRefAttr(name: string, value: string): boolean {
167
+ if (name !== "clip-path" && name !== "mask") return true;
168
+ if (value === "none") return true;
169
+ return URL_HASH_REF.test(value);
170
+ }
171
+
172
+ function parseAttrs(tag: string, raw: string): Record<string, string> | undefined {
173
+ const attrs: Record<string, string> = {};
174
+ const re = /([A-Za-z_:][-A-Za-z0-9_:]*)\s*=\s*(?:"([^"]*)"|'([^']*)')/g;
175
+ let match: RegExpExecArray | null;
176
+ while ((match = re.exec(raw))) {
177
+ const name = match[1] ?? "";
178
+ if (!ATTR_ALLOW.has(name)) continue;
179
+ const value = decodeAttr(match[2] ?? match[3] ?? "");
180
+ if (/[<>`]|javascript:/i.test(value)) return undefined;
181
+ if (TRANSFORM_ATTRS.has(name) && !isTransformList(value)) return undefined;
182
+ if (!isSafeHref(tag, name, value)) return undefined;
183
+ if (!isSafeUrlRefAttr(name, value)) return undefined;
184
+ // Never allow inline style attribute (XSS vector); presentation attrs only.
185
+ if (name === "style") return undefined;
186
+ attrs[name] = value;
187
+ }
188
+ if (/\bon[a-z]+\s*=/i.test(raw)) return undefined;
189
+ if (/\bstyle\s*=/i.test(raw)) return undefined;
190
+ return attrs;
191
+ }
192
+
193
+ /** One tag's inner text (between `<` and `>`) as a token, or undefined when it isn't allowed. */
194
+ function tagToken(body: string): Tok | undefined {
195
+ if (body.startsWith("/")) {
196
+ const tag = body.slice(1).trim().toLowerCase();
197
+ return SVG_TAGS.has(tag) ? { kind: "close", tag } : undefined;
198
+ }
199
+ const selfClosing = body.endsWith("/");
200
+ const openBody = selfClosing ? body.slice(0, -1).trim() : body;
201
+ const space = openBody.search(/\s/);
202
+ const tag = (space < 0 ? openBody : openBody.slice(0, space)).toLowerCase();
203
+ if (!SVG_TAGS.has(tag)) return undefined;
204
+ const attrs = parseAttrs(tag, space < 0 ? "" : openBody.slice(space));
205
+ return attrs ? { kind: "open", tag, attrs, selfClosing } : undefined;
206
+ }
207
+
208
+ function tokenize(input: string): Tok[] | undefined {
209
+ const tokens: Tok[] = [];
210
+ let i = 0;
211
+ while (i < input.length) {
212
+ if (input[i] !== "<") {
213
+ const next = input.indexOf("<", i);
214
+ const end = next < 0 ? input.length : next;
215
+ const text = input.slice(i, end);
216
+ if (text.trim()) tokens.push({ kind: "text", value: text });
217
+ i = end;
218
+ continue;
219
+ }
220
+ if (input.startsWith("<!--", i)) {
221
+ const end = input.indexOf("-->", i + 4);
222
+ if (end < 0) return undefined;
223
+ i = end + 3;
224
+ continue;
225
+ }
226
+ if (input.startsWith("<!", i) || input.startsWith("<?", i)) return undefined;
227
+ const close = input.indexOf(">", i + 1);
228
+ if (close < 0) return undefined;
229
+ const token = tagToken(input.slice(i + 1, close).trim());
230
+ if (!token) return undefined;
231
+ tokens.push(token);
232
+ i = close + 1;
233
+ }
234
+ return tokens;
235
+ }
236
+
237
+ function build(tokens: Tok[]): VNode | undefined {
238
+ let index = 0;
239
+
240
+ const read = (): VNode | string | undefined => {
241
+ const tok = tokens[index];
242
+ if (!tok) return undefined;
243
+ if (tok.kind === "text") {
244
+ index += 1;
245
+ return tok.value;
246
+ }
247
+ if (tok.kind === "close") return undefined;
248
+ index += 1;
249
+ if (tok.selfClosing) {
250
+ return { tag: tok.tag, attrs: { ...tok.attrs }, children: [] };
251
+ }
252
+ const children: (VNode | string)[] = [];
253
+ while (index < tokens.length) {
254
+ const next = tokens[index];
255
+ if (!next) break;
256
+ if (next.kind === "close") {
257
+ if (next.tag !== tok.tag) return undefined;
258
+ index += 1;
259
+ break;
260
+ }
261
+ const child = read();
262
+ if (child === undefined) return undefined;
263
+ if (typeof child !== "string" || TEXT_TAGS.has(tok.tag)) children.push(child);
264
+ }
265
+ return { tag: tok.tag, attrs: { ...tok.attrs }, children };
266
+ };
267
+
268
+ const root = read();
269
+ if (!root || typeof root === "string" || root.tag !== "svg") return undefined;
270
+ if (index !== tokens.length) return undefined;
271
+ return root;
272
+ }
273
+
274
+ /**
275
+ * Parse pasted SVG into an allowlisted VNode tree (R-032 / W-073 / W-079).
276
+ * Rejects scripts, handlers, foreignObject, style, anchors, and unsafe urls.
277
+ * Allows fragment-only `use` and media-policy `image` hrefs.
278
+ */
279
+ export function sanitizeSvgMarkup(raw: string): VNode | undefined {
280
+ if (typeof raw !== "string") return undefined;
281
+ const trimmed = raw.trim();
282
+ if (!trimmed || trimmed.length > MAX_MARKUP) return undefined;
283
+ if (FORBIDDEN.test(trimmed)) return undefined;
284
+ if (!/<svg[\s>]/i.test(trimmed)) return undefined;
285
+ const tokens = tokenize(trimmed);
286
+ if (!tokens) return undefined;
287
+ return build(tokens);
288
+ }
289
+
290
+ /** True when markup is safe to store (same rules as render). */
291
+ export function isSafeSvgMarkup(raw: string): boolean {
292
+ return sanitizeSvgMarkup(raw) !== undefined;
293
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * The shared URL check behind hrefs and media sources (R-032): a URL with one of `schemes`, or a
3
+ * same-site relative one. Other schemes (javascript:, data:, vbscript:…) are refused, including
4
+ * case and whitespace tricks.
5
+ */
6
+ export function sanitizeUrl(value: string, schemes: ReadonlySet<string>): string | undefined {
7
+ const trimmed = value.trim();
8
+ if (trimmed.length === 0 || trimmed.length > 2000) return undefined;
9
+ // No HTML metacharacters or control chars in any URL.
10
+ for (const ch of trimmed) {
11
+ const code = ch.charCodeAt(0);
12
+ if (code < 32 || code === 127 || "<>\"'`".includes(ch)) return undefined;
13
+ }
14
+
15
+ const hasScheme = /^[a-z][a-z0-9+.-]*:/i.test(trimmed);
16
+ if (!hasScheme) {
17
+ // Browsers resolve `//host` and `/\host` to another site; EmDash 1.0 refuses them for `url` fields too.
18
+ if (/^\/[/\\]/.test(trimmed)) return undefined;
19
+ // Relative: /path, ./path, ../path, ?query, #hash, or bare path segment
20
+ if (/^(?:[/?#.]|[A-Za-z0-9._~-])/.test(trimmed)) return trimmed;
21
+ return undefined;
22
+ }
23
+
24
+ let parsed: URL;
25
+ try {
26
+ parsed = new URL(trimmed.replace(/\s+/g, ""));
27
+ } catch {
28
+ return undefined;
29
+ }
30
+ return schemes.has(parsed.protocol.toLowerCase()) ? trimmed : undefined;
31
+ }
@@ -0,0 +1,33 @@
1
+ /** Field kinds the settings panel knows how to draw (W-021). */
2
+ export type FieldKind =
3
+ | "text"
4
+ | "textarea"
5
+ | "number"
6
+ | "int"
7
+ | "select"
8
+ | "boolean"
9
+ | "href"
10
+ | "media"
11
+ | "icon"
12
+ | "list-items";
13
+
14
+ export type FieldOption = { value: string | number | boolean; label: string };
15
+
16
+ export type FieldDescriptor = {
17
+ key: string;
18
+ kind: FieldKind;
19
+ label: string;
20
+ options?: FieldOption[];
21
+ /** Shown under the control when the value is invalid. */
22
+ message?: string;
23
+ optional?: boolean;
24
+ };
25
+
26
+ export type ElementDescriptor = {
27
+ type: string;
28
+ name: string;
29
+ group: "layout" | "content" | "form" | "dynamic";
30
+ /** Default right-panel tab when this element is selected. */
31
+ defaultTab: "content" | "style";
32
+ fields: FieldDescriptor[];
33
+ };
@@ -0,0 +1,88 @@
1
+ import { z } from "zod";
2
+ import { DeviceStyles, HiddenOn, StyleProps, StyleStates } from "./style.ts";
3
+
4
+ /** 9 since W-104 (D-042): tag default styles. Older documents stay valid. */
5
+ export const DESIGN_SCHEMA_VERSION = 9;
6
+
7
+ /** Tags a site can give a starting style. Classes and local styles still win. */
8
+ export const DEFAULT_STYLE_TAGS = ["h1", "h2", "h3", "h4", "h5", "h6", "p", "a", "button"] as const;
9
+
10
+ export type DefaultStyleTag = (typeof DEFAULT_STYLE_TAGS)[number];
11
+
12
+ const VariableId = z
13
+ .string()
14
+ .regex(/^[a-z0-9-]{1,40}$/, "Ids are 1-40 lowercase letters, digits or -");
15
+
16
+ export const ColorVariable = z.strictObject({
17
+ id: VariableId,
18
+ name: z.string().min(1).max(60),
19
+ value: z
20
+ .string()
21
+ .regex(
22
+ /^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i,
23
+ "Use a hex color such as #1a2b3c",
24
+ ),
25
+ });
26
+
27
+ /** Font stack string — commas and quoted family names allowed; `generateCss` drops an unsafe one. */
28
+ export const FontVariable = z.strictObject({
29
+ id: VariableId,
30
+ name: z.string().min(1).max(60),
31
+ value: z.string().min(1).max(200),
32
+ });
33
+
34
+ export const LengthVariable = z.strictObject({
35
+ id: VariableId,
36
+ name: z.string().min(1).max(60),
37
+ value: z.strictObject({
38
+ value: z.number().finite().min(0).max(10_000),
39
+ unit: z.enum(["px", "rem", "em", "%"]),
40
+ }),
41
+ });
42
+
43
+ /** Shared style class (R-021 / W-030). CSS selector is always `.emvb-k-<id>`. */
44
+ const StyleClass = z.strictObject({
45
+ id: VariableId,
46
+ name: z.string().min(1).max(60),
47
+ style: StyleProps,
48
+ states: StyleStates.optional(),
49
+ devices: DeviceStyles.optional(),
50
+ hiddenOn: HiddenOn.optional(),
51
+ });
52
+
53
+ /** Design system document (D-013 / W-028 / W-030). */
54
+ export const DesignSystem = z.strictObject({
55
+ schemaVersion: z.literal(DESIGN_SCHEMA_VERSION),
56
+ variables: z.strictObject({
57
+ colors: z.array(ColorVariable).max(200),
58
+ fonts: z.array(FontVariable).max(50).optional(),
59
+ fontSizes: z.array(LengthVariable).max(50).optional(),
60
+ spacings: z.array(LengthVariable).max(100).optional(),
61
+ }),
62
+ classes: z.array(StyleClass).max(100).optional(),
63
+ /** Starting styles for HTML tags, emitted under `:where(.emvb-root)` so a class still wins. */
64
+ defaults: z
65
+ .strictObject({
66
+ h1: StyleProps.optional(),
67
+ h2: StyleProps.optional(),
68
+ h3: StyleProps.optional(),
69
+ h4: StyleProps.optional(),
70
+ h5: StyleProps.optional(),
71
+ h6: StyleProps.optional(),
72
+ p: StyleProps.optional(),
73
+ a: StyleProps.optional(),
74
+ button: StyleProps.optional(),
75
+ })
76
+ .optional(),
77
+ });
78
+
79
+ export type ColorVariable = z.infer<typeof ColorVariable>;
80
+ export type FontVariable = z.infer<typeof FontVariable>;
81
+ export type LengthVariable = z.infer<typeof LengthVariable>;
82
+ export type DesignSystem = z.infer<typeof DesignSystem>;
83
+
84
+ export const emptyDesign = (): DesignSystem => ({
85
+ schemaVersion: DESIGN_SCHEMA_VERSION,
86
+ variables: { colors: [], fonts: [], fontSizes: [], spacings: [] },
87
+ classes: [],
88
+ });