@perkyzz/emvb 0.3.0 → 0.5.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 (163) hide show
  1. package/README.md +22 -4
  2. package/THIRD_PARTY_NOTICES.md +1 -1
  3. package/package.json +5 -2
  4. package/src/admin/access/role.ts +9 -3
  5. package/src/admin/agents-api.ts +39 -0
  6. package/src/admin/api.ts +17 -7
  7. package/src/admin/content-api.ts +7 -51
  8. package/src/admin/content-item.ts +22 -0
  9. package/src/admin/design-api.ts +30 -0
  10. package/src/admin/editor/A11yCopilot.tsx +1 -1
  11. package/src/admin/editor/CommandPalette.tsx +43 -2
  12. package/src/admin/editor/Editor.tsx +133 -332
  13. package/src/admin/editor/EditorOverlay.tsx +3 -1
  14. package/src/admin/editor/PerfMeter.tsx +1 -1
  15. package/src/admin/editor/ShortcutsDialog.tsx +4 -1
  16. package/src/admin/editor/Timeline.tsx +124 -9
  17. package/src/admin/editor/canvas/CanvasFrame.tsx +51 -368
  18. package/src/admin/editor/canvas/DeviceBar.tsx +4 -1
  19. package/src/admin/editor/canvas/SelectionOverlay.tsx +86 -46
  20. package/src/admin/editor/canvas/canvas-scale.ts +20 -0
  21. package/src/admin/editor/canvas/drop-line.ts +24 -0
  22. package/src/admin/editor/canvas/use-canvas-dnd.ts +207 -0
  23. package/src/admin/editor/canvas/use-canvas-session.ts +216 -0
  24. package/src/admin/editor/collection-previews.ts +28 -5
  25. package/src/admin/editor/editor-chrome.tsx +346 -0
  26. package/src/admin/editor/editor-commands.ts +360 -0
  27. package/src/admin/editor/editor-css.ts +4 -0
  28. package/src/admin/editor/element-chrome.ts +89 -0
  29. package/src/admin/editor/feature-css.ts +37 -18
  30. package/src/admin/editor/host.ts +33 -0
  31. package/src/admin/editor/icons/IconLibraryDialog.tsx +64 -218
  32. package/src/admin/editor/icons/library.ts +29 -19
  33. package/src/admin/editor/icons/use-icon-library.ts +315 -0
  34. package/src/admin/editor/palette-items.ts +36 -5
  35. package/src/admin/editor/palette.ts +9 -4
  36. package/src/admin/editor/panels/AddPanel.tsx +2 -67
  37. package/src/admin/editor/panels/ColorControl.tsx +21 -18
  38. package/src/admin/editor/panels/ElementPanel.tsx +40 -99
  39. package/src/admin/editor/panels/LayersPanel.tsx +34 -62
  40. package/src/admin/editor/panels/SettingsPanel.tsx +1 -0
  41. package/src/admin/editor/panels/SiteStylesDrawer.tsx +16 -10
  42. package/src/admin/editor/panels/TokenScales.tsx +4 -2
  43. package/src/admin/editor/panels/VariableSection.tsx +1 -10
  44. package/src/admin/editor/panels/color-space.ts +12 -1
  45. package/src/admin/editor/panels/inline-error.tsx +11 -0
  46. package/src/admin/editor/panels/settings/AdvancedFields.tsx +143 -1
  47. package/src/admin/editor/panels/settings/AudienceEditor.tsx +115 -7
  48. package/src/admin/editor/panels/settings/DraftTextField.tsx +42 -9
  49. package/src/admin/editor/panels/settings/FieldControl.tsx +6 -0
  50. package/src/admin/editor/panels/settings/FormBindControl.tsx +8 -4
  51. package/src/admin/editor/panels/settings/IconPicker.tsx +21 -1
  52. package/src/admin/editor/panels/settings/ImageSourcesField.tsx +111 -0
  53. package/src/admin/editor/panels/settings/ItemList.tsx +34 -1
  54. package/src/admin/editor/panels/settings/MediaPicker.tsx +2 -3
  55. package/src/admin/editor/panels/settings/QueryStyleRows.tsx +178 -0
  56. package/src/admin/editor/panels/settings/StyleRow.tsx +14 -2
  57. package/src/admin/editor/panels/settings/advanced-rules.ts +3 -0
  58. package/src/admin/editor/panels/settings/form-names.ts +7 -11
  59. package/src/admin/editor/panels/settings/length-units.ts +15 -4
  60. package/src/admin/editor/panels/settings/link-rules.ts +19 -0
  61. package/src/admin/editor/panels/settings/number-input.ts +11 -0
  62. package/src/admin/editor/panels/settings/query-style.ts +65 -0
  63. package/src/admin/editor/panels/settings/style-sections.ts +5 -1
  64. package/src/admin/editor/panels/style-target.ts +105 -0
  65. package/src/admin/editor/panels/variable-values.ts +4 -2
  66. package/src/admin/editor/remote-change.ts +67 -0
  67. package/src/admin/editor/section-templates.ts +43 -21
  68. package/src/admin/editor/shortcuts.ts +4 -0
  69. package/src/admin/editor/store.ts +10 -1
  70. package/src/admin/editor/useClipboardActions.ts +1 -1
  71. package/src/admin/editor/useEditorCommands.ts +61 -13
  72. package/src/admin/editor/useEditorData.ts +7 -1
  73. package/src/admin/editor/useEditorShortcuts.ts +23 -15
  74. package/src/admin/editor/useNodeActions.ts +6 -1
  75. package/src/admin/forms-api.ts +17 -6
  76. package/src/admin/icon-uploads-api.ts +18 -0
  77. package/src/admin/index.tsx +3 -1
  78. package/src/admin/media-api.ts +20 -0
  79. package/src/admin/pages/AgentsPage.tsx +274 -0
  80. package/src/admin/theme-api.ts +5 -18
  81. package/src/admin/ui.ts +28 -0
  82. package/src/astro/content-port.ts +24 -0
  83. package/src/astro/floats/runtime.ts +10 -0
  84. package/src/astro/forms/EmVBFormsClient.astro +11 -0
  85. package/src/astro/forms/EmVBFormsRuntime.astro +6 -7
  86. package/src/astro/forms/client.ts +1 -0
  87. package/src/astro/forms-definitions.ts +24 -11
  88. package/src/astro/index.ts +1 -0
  89. package/src/astro/popups/runtime.ts +15 -0
  90. package/src/astro/render-session.ts +85 -0
  91. package/src/astro/request-data.ts +20 -8
  92. package/src/astro/resolve-theme.ts +166 -76
  93. package/src/astro/resolve.ts +57 -16
  94. package/src/astro/tabs/runtime.ts +60 -1
  95. package/src/astro/theme-posts.ts +16 -5
  96. package/src/astro/visitor.ts +67 -5
  97. package/src/constants.ts +1 -1
  98. package/src/core/a11y/audit.ts +202 -26
  99. package/src/core/arrange.ts +71 -38
  100. package/src/core/audience/rules.ts +22 -2
  101. package/src/core/canvas-styles.ts +24 -0
  102. package/src/core/clipboard.ts +11 -32
  103. package/src/core/css/generate.ts +18 -1
  104. package/src/core/css/print.ts +12 -0
  105. package/src/core/css/scope.ts +3 -3
  106. package/src/core/css-keywords.ts +83 -0
  107. package/src/core/data/bindings.ts +4 -6
  108. package/src/core/design/classes.ts +13 -15
  109. package/src/core/design/diff.ts +55 -0
  110. package/src/core/design/merge.ts +127 -0
  111. package/src/core/design/variables.ts +25 -21
  112. package/src/core/elements/dynamic-elements.ts +30 -0
  113. package/src/core/elements/form-elements.ts +20 -3
  114. package/src/core/elements/index.ts +211 -7
  115. package/src/core/forms/binding.ts +4 -13
  116. package/src/core/index.ts +10 -0
  117. package/src/core/local-style.ts +27 -0
  118. package/src/core/migrate/index.ts +9 -4
  119. package/src/core/nesting.ts +4 -23
  120. package/src/core/perf/scripts.ts +9 -6
  121. package/src/core/record.ts +3 -0
  122. package/src/core/render/context.ts +2 -0
  123. package/src/core/render/dynamic-post.ts +22 -7
  124. package/src/core/render/form.ts +145 -15
  125. package/src/core/render/index.ts +195 -14
  126. package/src/core/render/pagination.ts +21 -2
  127. package/src/core/render/vnode.ts +12 -2
  128. package/src/core/sanitize/css.ts +90 -77
  129. package/src/core/sanitize/embed-url.ts +7 -1
  130. package/src/core/sanitize/href.ts +13 -1
  131. package/src/core/sanitize/svg.ts +8 -3
  132. package/src/core/sanitize/url.ts +10 -1
  133. package/src/core/schema/descriptors.ts +4 -1
  134. package/src/core/schema/design.ts +5 -7
  135. package/src/core/schema/layout.ts +160 -181
  136. package/src/core/schema/style.ts +53 -63
  137. package/src/core/share/link.ts +72 -29
  138. package/src/core/tabs/presence.ts +7 -17
  139. package/src/core/theme/dynamic.ts +205 -39
  140. package/src/core/theme/float.ts +1 -1
  141. package/src/core/tree-ops.ts +57 -0
  142. package/src/index.ts +14 -2
  143. package/src/mcp/errors.ts +44 -0
  144. package/src/mcp/index.ts +4 -0
  145. package/src/mcp/ports.ts +78 -0
  146. package/src/mcp/prompts.ts +76 -0
  147. package/src/mcp/publish.ts +60 -0
  148. package/src/mcp/queue.ts +19 -0
  149. package/src/mcp/rate-limit.ts +80 -0
  150. package/src/mcp/read.ts +419 -0
  151. package/src/mcp/refs.ts +84 -0
  152. package/src/mcp/settings.ts +18 -0
  153. package/src/mcp/tools.ts +265 -0
  154. package/src/mcp/write.ts +582 -0
  155. package/src/server/agent-settings.ts +134 -0
  156. package/src/server/design-routes.ts +75 -18
  157. package/src/server/icon-routes.ts +72 -16
  158. package/src/server/mcp-routes.ts +265 -0
  159. package/src/server/plugin.ts +40 -4
  160. package/src/admin/editor/icons/sets/fontawesome.json +0 -1
  161. package/src/admin/editor/icons/sets/lucide.json +0 -1
  162. package/src/admin/editor/icons/sets/remix.json +0 -1
  163. package/src/admin/editor/icons/sets/tabler.json +0 -1
package/src/core/index.ts CHANGED
@@ -47,6 +47,7 @@ export {
47
47
  AB_TEST_NAME,
48
48
  Audience,
49
49
  AUDIENCE_DEVICES,
50
+ AUDIENCE_SEGMENT,
50
51
  LOOP_DISPLAYS,
51
52
  LOOP_ORDERS,
52
53
  SectionNode,
@@ -117,6 +118,8 @@ export {
117
118
  type DesignValidation,
118
119
  } from "./validate.ts";
119
120
  export { nestingIssues } from "./nesting.ts";
121
+ export { mergeDesign, type DesignConflict } from "./design/merge.ts";
122
+ export { designChanges, designChangeLabel, type DesignChange } from "./design/diff.ts";
120
123
  export {
121
124
  DESIGN_SCHEMA_VERSION,
122
125
  DEFAULT_STYLE_TAGS,
@@ -258,6 +261,9 @@ export {
258
261
  addSectionNear,
259
262
  canDrop,
260
263
  duplicateNode,
264
+ wrapNode,
265
+ WRAP_TYPES,
266
+ type WrapType,
261
267
  createVariantB,
262
268
  endAbTest,
263
269
  defaultTestName,
@@ -371,6 +377,8 @@ export {
371
377
  collectLoopItemPartIds,
372
378
  collectSectionPartIds,
373
379
  collectCollectionLoops,
380
+ loopPermalink,
381
+ parseLoopFilter,
374
382
  type CollectionLoop,
375
383
  resolvePostForRender,
376
384
  resolvePostsForLoop,
@@ -443,6 +451,7 @@ export {
443
451
  clockIn,
444
452
  deviceFromUserAgent,
445
453
  layoutUsesAudience,
454
+ layoutUsesVisitorRule,
446
455
  type VisitorInfo,
447
456
  } from "./audience/rules.ts";
448
457
  export { changedIds, diffSections, restoreSection, type SectionChange } from "./history/diff.ts";
@@ -489,3 +498,4 @@ export {
489
498
  type SharedPage,
490
499
  type ShareRead,
491
500
  } from "./share/link.ts";
501
+ export { canvasStyleUrls, MAX_CANVAS_STYLES } from "./canvas-styles.ts";
@@ -0,0 +1,27 @@
1
+ import type { LayoutNode } from "./schema/layout.ts";
2
+
3
+ /** Local style keys. Classes and `query` stay on the element (D-033, W-355). */
4
+ const LOCAL_STYLE_KEYS = ["style", "states", "devices", "hiddenOn"] as const;
5
+
6
+ export type LocalStyle = Pick<LayoutNode, (typeof LOCAL_STYLE_KEYS)[number]>;
7
+
8
+ /** The local style of a node; classes stay behind (D-033). */
9
+ export function localStyleOf(node: LayoutNode): LocalStyle {
10
+ const copied: LocalStyle = {};
11
+ for (const key of LOCAL_STYLE_KEYS) {
12
+ const value = node[key];
13
+ if (value !== undefined) (copied as Record<string, unknown>)[key] = structuredClone(value);
14
+ }
15
+ return copied;
16
+ }
17
+
18
+ /** Replaces style, states, devices and hiddenOn. Other fields stay. `query` is not a local style. */
19
+ export function withLocalStyle<T extends LayoutNode>(node: T, copied: LocalStyle): T {
20
+ const next = { ...node } as Record<string, unknown>;
21
+ for (const key of LOCAL_STYLE_KEYS) {
22
+ delete next[key];
23
+ const value = copied[key];
24
+ if (value !== undefined) next[key] = structuredClone(value);
25
+ }
26
+ return next as T;
27
+ }
@@ -1,3 +1,4 @@
1
+ import { isRecord } from "../record.ts";
1
2
  import { LAYOUT_SCHEMA_VERSION } from "../schema/layout.ts";
2
3
 
3
4
  export type Migration = (doc: Record<string, unknown>) => Record<string, unknown>;
@@ -12,13 +13,13 @@ export type Migration = (doc: Record<string, unknown>) => Record<string, unknown
12
13
  * per-corner radii and grid columns per device, and v10 → v11 (D-045) the layout Section and node
13
14
  * labels, and v11 → v12 (W-160) rewrites a gradient `{ angle, from, to }` into
14
15
  * `{ type: "linear", angle, stops }`, and v12 → v13 (W-307 onward) adds optional fields only
15
- * (bindings, and the rest of the W-307 feature batch). Every other value is unchanged.
16
+ * (bindings, and the rest of the W-307 feature batch), and v13 → v14 (W-329 onward) adds
17
+ * optional fields only (audience segments, Loop options, `ch` and negative margins), and
18
+ * v14 → v15 (W-341 onward) adds optional fields only (direction, image link and caption,
19
+ * button icons, table of contents, container queries). Every other value is unchanged.
16
20
  */
17
21
  const unchanged: Migration = (doc) => doc;
18
22
 
19
- const isRecord = (value: unknown): value is Record<string, unknown> =>
20
- typeof value === "object" && value !== null && !Array.isArray(value);
21
-
22
23
  /** v11 two-stop gradient → v12 stops. A gradient that already has `stops` is left as it is. */
23
24
  function convertGradient(value: unknown): unknown {
24
25
  if (!isRecord(value) || Array.isArray(value["stops"])) return value;
@@ -71,6 +72,8 @@ export const LAYOUT_MIGRATIONS: Readonly<Record<number, Migration>> = {
71
72
  10: unchanged,
72
73
  11: gradients,
73
74
  12: unchanged,
75
+ 13: unchanged,
76
+ 14: unchanged,
74
77
  };
75
78
 
76
79
  /** `DESIGN_MIGRATIONS[n]` upgrades a version-n design document to version n + 1. */
@@ -87,6 +90,8 @@ export const DESIGN_MIGRATIONS: Readonly<Record<number, Migration>> = {
87
90
  10: unchanged,
88
91
  11: gradients,
89
92
  12: unchanged,
93
+ 13: unchanged,
94
+ 14: unchanged,
90
95
  };
91
96
 
92
97
  export type UpgradeResult =
@@ -1,12 +1,12 @@
1
- import { REASONS } from "./arrange.ts";
2
- import { isFormFieldType, isFormNode, type Layout, type LayoutNode } from "./schema/layout.ts";
1
+ import { parentChildRefusal } from "./arrange.ts";
2
+ import { isFormNode, type Layout, type LayoutNode } from "./schema/layout.ts";
3
3
  import { nodeChildren } from "./tree-ops.ts";
4
4
  import type { LayoutIssue } from "./validate.ts";
5
5
 
6
6
  /**
7
7
  * Elements in a parent the editor would never let them into: a menu item outside a Menu, a tab
8
8
  * panel outside Tabs, a form field outside a form, a form in a form, and so on (W-225). The
9
- * same rules and messages as drag and drop (arrange.ts DROP_RULES). The editor can't build
9
+ * same rules and messages as drag and drop (`parentChildRefusal`). The editor can't build
10
10
  * these trees, but the content API accepted them and they render broken markup (an orphan
11
11
  * `<li>`, a panel no tab opens). Not part of validateLayout, so stored pages still render and
12
12
  * a copied field can still be pasted; the save hook refuses them instead.
@@ -23,7 +23,7 @@ export function nestingIssues(layout: Layout, max = 5): LayoutIssue[] {
23
23
  for (const [i, child] of kids.entries()) {
24
24
  if (issues.length >= max) return;
25
25
  const at = `${path}.children[${i}]`;
26
- const reason = misplaced(node, child, inForm);
26
+ const reason = parentChildRefusal(node, child, inForm);
27
27
  if (reason) add(at, child, reason);
28
28
  walk(child, at, inForm || isFormNode(child));
29
29
  }
@@ -31,22 +31,3 @@ export function nestingIssues(layout: Layout, max = 5): LayoutIssue[] {
31
31
  walk(layout.root, "root", isFormNode(layout.root));
32
32
  return issues;
33
33
  }
34
-
35
- function misplaced(parent: LayoutNode, child: LayoutNode, inForm: boolean): string | undefined {
36
- if (child.type === "loop-empty" && parent.type !== "loop") return REASONS.emptyOutsideLoop;
37
- if (child.type === "tab-panel" && parent.type !== "tabs") return REASONS.tabOutsideTabs;
38
- if (parent.type === "tabs" && child.type !== "tab-panel") return REASONS.onlyTabPanels;
39
- if (child.type === "accordion-item" && parent.type !== "accordion") {
40
- return REASONS.itemOutsideAccordion;
41
- }
42
- if (parent.type === "accordion" && child.type !== "accordion-item") {
43
- return REASONS.onlyAccordionItems;
44
- }
45
- if (child.type === "menu-item" && parent.type !== "menu" && parent.type !== "menu-item") {
46
- return REASONS.itemOutsideMenu;
47
- }
48
- if (parent.type === "menu" && child.type !== "menu-item") return REASONS.onlyMenuItems;
49
- if (isFormNode(child) && inForm) return REASONS.nestedForm;
50
- if (isFormFieldType(child.type) && !inForm) return REASONS.fieldOutsideForm;
51
- return undefined;
52
- }
@@ -1,4 +1,5 @@
1
1
  import { isParentNode, type Layout, type LayoutNode } from "../schema/layout.ts";
2
+ import { forEachNode, knownChildren } from "../tree-ops.ts";
2
3
 
3
4
  /** The small scripts an EmVB page can load; everything else is plain HTML and CSS (W-311). */
4
5
  export type PageScript = "forms" | "tabs" | "menu";
@@ -48,11 +49,13 @@ export const typeMayAddScript = (type: string): boolean =>
48
49
  /** Every script the layout loads, with the elements that cause it (W-311). */
49
50
  export function layoutScripts(layout: Layout): Map<PageScript, string[]> {
50
51
  const found = new Map<PageScript, string[]>();
51
- const walk = (node: LayoutNode): void => {
52
- const script = nodeScript(node);
53
- if (script) found.set(script, [...(found.get(script) ?? []), node.id]);
54
- if (isParentNode(node)) for (const child of node.children) walk(child);
55
- };
56
- walk(layout.root);
52
+ forEachNode(
53
+ layout.root,
54
+ (node) => {
55
+ const script = nodeScript(node);
56
+ if (script) found.set(script, [...(found.get(script) ?? []), node.id]);
57
+ },
58
+ knownChildren,
59
+ );
57
60
  return found;
58
61
  }
@@ -0,0 +1,3 @@
1
+ /** A plain object. Arrays and null are not records. */
2
+ export const isRecord = (value: unknown): value is Record<string, unknown> =>
3
+ typeof value === "object" && value !== null && !Array.isArray(value);
@@ -18,4 +18,6 @@ export type RenderContext = {
18
18
  * stay with their own copy.
19
19
  */
20
20
  uniqueId: (base: string) => string;
21
+ /** The form whose children are rendering, so each field reads that form's definition. */
22
+ formId?: string;
21
23
  };
@@ -1,3 +1,4 @@
1
+ import { HEADING_TAGS } from "../elements/index.ts";
1
2
  import type { LayoutNode, POST_DATE_FORMATS } from "../schema/layout.ts";
2
3
  import { sanitizeHref } from "../sanitize/href.ts";
3
4
  import { sanitizeMediaUrl } from "../sanitize/media-url.ts";
@@ -28,11 +29,14 @@ type PostField = {
28
29
  /** Whether the post has this field; the placeholder stands in when it doesn't. */
29
30
  has?: (post: ThemePostFields) => boolean;
30
31
  /** The field's markup, or nothing when the post's value is empty or unsafe. */
31
- render: (node: LayoutNode, attrs: Attrs, post: ThemePostFields) => VNode | undefined;
32
+ render: (
33
+ node: LayoutNode,
34
+ attrs: Attrs,
35
+ post: ThemePostFields,
36
+ inLoop: boolean,
37
+ ) => VNode | undefined;
32
38
  };
33
39
 
34
- const HEADING_TAGS = ["h1", "h2", "h3", "h4", "h5", "h6"] as const;
35
-
36
40
  type PostDateFormat = (typeof POST_DATE_FORMATS)[number];
37
41
 
38
42
  /** In UTC, so the date doesn't move with the server's time zone. */
@@ -92,13 +96,16 @@ const POST_FIELDS = new Map<string, PostField>([
92
96
  children: ["Featured image"],
93
97
  }),
94
98
  has: (post) => !!post.featuredImageUrl,
95
- render: (node, attrs, post) => {
99
+ render: (node, attrs, post, inLoop) => {
96
100
  const src = sanitizeMediaUrl(post.featuredImageUrl ?? "");
97
101
  if (!src) return undefined;
98
102
  const alt = props<{ decorative?: boolean }>(node).decorative
99
103
  ? ""
100
104
  : post.featuredImageAlt?.trim() || post.title || "Featured image";
101
- return { tag: "img", attrs: { ...attrs, src, alt }, children: [] };
105
+ // W-440: a Loop's images below the fold loaded all at once; like Image, they wait.
106
+ // A single post's featured image is usually the hero, so it isn't delayed.
107
+ const lazy: Attrs = inLoop ? { loading: "lazy", decoding: "async" } : {};
108
+ return { tag: "img", attrs: { ...attrs, src, alt, ...lazy }, children: [] };
102
109
  },
103
110
  },
104
111
  ],
@@ -119,10 +126,17 @@ const POST_FIELDS = new Map<string, PostField>([
119
126
  linkAttrs.target = "_blank";
120
127
  linkAttrs.rel = "noopener noreferrer";
121
128
  }
129
+ const label = text?.trim();
130
+ const title = post.title.trim();
131
+ // W-439: a Loop repeats the same "Read more" for every post; screen readers listing links
132
+ // heard it N times. The name keeps the visible text first and adds the post's title.
133
+ if (label && title && !label.toLowerCase().includes(title.toLowerCase())) {
134
+ linkAttrs["aria-label"] = `${label}: ${title}`;
135
+ }
122
136
  return {
123
137
  tag: "a",
124
138
  attrs: linkAttrs,
125
- children: [text?.trim() || post.title || post.permalink],
139
+ children: [label || title || post.permalink],
126
140
  };
127
141
  },
128
142
  },
@@ -165,11 +179,12 @@ export function renderDynamicPost(
165
179
  attrs: Attrs,
166
180
  post: ThemePostFields | undefined,
167
181
  mode: RenderMode,
182
+ inLoop = false,
168
183
  ): VNode | undefined {
169
184
  const field = POST_FIELDS.get(node.type);
170
185
  if (!field) return undefined;
171
186
  if (!post || field.has?.(post) === false) {
172
187
  return mode === "editor" ? field.placeholder(attrs) : undefined;
173
188
  }
174
- return field.render(node, attrs, post);
189
+ return field.render(node, attrs, post, inLoop);
175
190
  }
@@ -7,6 +7,9 @@ import type { VNode } from "./vnode.ts";
7
7
 
8
8
  export const FORMS_SUBMIT_PATH = "/_emdash/api/plugins/emdash-forms/submit";
9
9
 
10
+ /** What a paused form shows on the page (W-326). */
11
+ export const FORM_PAUSED_TEXT = "This form isn't accepting responses right now.";
12
+
10
13
  const honeypot = (inputId: string): VNode => ({
11
14
  tag: "div",
12
15
  attrs: {
@@ -37,7 +40,9 @@ const honeypot = (inputId: string): VNode => ({
37
40
  /** The text of the form's first Submit button, as the page renders it. */
38
41
  function firstSubmitLabel(nodes: readonly LayoutNode[]): string | undefined {
39
42
  for (const child of nodes) {
40
- if (child.type === "submit") return (child.props as { label?: string }).label ?? "Submit";
43
+ if (child.type === "submit") {
44
+ return (child.props as { label?: string }).label?.trim() || "Submit";
45
+ }
41
46
  const found = firstSubmitLabel(nodeChildren(child));
42
47
  if (found !== undefined) return found;
43
48
  }
@@ -64,16 +69,34 @@ export function renderForm(
64
69
  children: ["Bind a form in settings to preview it here."],
65
70
  };
66
71
  }
72
+ // W-326: a paused form says so, in place of fields that can't be sent (the forms plugin refuses
73
+ // its submissions), with no form, inputs or Submit button.
74
+ if (ctx.definitions.get(formId)?.status === "paused") {
75
+ return {
76
+ tag: "div",
77
+ attrs: {
78
+ ...attrs,
79
+ class: `${attrs.class} emvb-form-paused`.trim(),
80
+ role: "status",
81
+ "data-emvb-form-paused": "",
82
+ },
83
+ children: [{ tag: "p", attrs: {}, children: [FORM_PAUSED_TEXT] }],
84
+ };
85
+ }
67
86
  // W-203: the forms client puts this label back on the button after each submit, so it must be
68
87
  // the label the page shows, not the form definition's.
69
88
  const submitLabel =
70
89
  firstSubmitLabel(node.children) ??
71
90
  ctx.definitions.get(formId)?.settings.submitLabel ??
72
91
  "Submit";
92
+ const previousForm = ctx.formId;
93
+ ctx.formId = formId;
94
+ const fieldNodes = ctx.children(node.children, dynamic);
95
+ ctx.formId = previousForm;
73
96
  const page: VNode = {
74
97
  tag: "div",
75
98
  attrs: { "data-page": "0" },
76
- children: ctx.children(node.children, dynamic),
99
+ children: fieldNodes,
77
100
  };
78
101
  return {
79
102
  tag: "form",
@@ -131,6 +154,107 @@ const LENGTH_TYPES = new Set(["text", "email", "tel", "url", "textarea"]);
131
154
  const isControl = (node: VNode) =>
132
155
  ["input", "select", "textarea"].includes(node.tag) && node.attrs.type !== "hidden";
133
156
 
157
+ /**
158
+ * A `pattern` the browser can compile. A group that can repeat and that itself contains `|` or
159
+ * another quantifier (`(a+)+`, `(a|aa)+`) is left off: it can hang the visitor's tab while they
160
+ * type. A pattern that does not compile is left off too.
161
+ */
162
+ function hasControlChar(value: string): boolean {
163
+ for (const char of value) {
164
+ const code = char.charCodeAt(0);
165
+ if (code < 32 || code === 127) return true;
166
+ }
167
+ return false;
168
+ }
169
+
170
+ /** How much of `pattern` at `index` is a quantifier, and whether it can repeat enough to hang. */
171
+ function quantifierAt(
172
+ pattern: string,
173
+ index: number,
174
+ ): { consumed: number; dangerous: boolean; repeats: boolean } {
175
+ const none = { consumed: 0, dangerous: false, repeats: false };
176
+ const ch = pattern[index];
177
+ if (ch === "+" || ch === "*") {
178
+ const lazy = pattern[index + 1] === "?" ? 1 : 0;
179
+ return { consumed: 1 + lazy, dangerous: true, repeats: true };
180
+ }
181
+ if (ch === "?") return { consumed: 1, dangerous: false, repeats: false };
182
+ if (ch !== "{") return none;
183
+ const match = /^\{(\d+)(?:\s*,\s*(\d+)?)?\}\??/.exec(pattern.slice(index));
184
+ if (!match) return none;
185
+ const min = Number(match[1]);
186
+ const hasComma = match[0].includes(",");
187
+ const max =
188
+ match[2] === undefined ? (hasComma ? Number.POSITIVE_INFINITY : min) : Number(match[2]);
189
+ if (max < min) return none;
190
+ return { consumed: match[0].length, dangerous: max > 20, repeats: max > 1 };
191
+ }
192
+
193
+ /**
194
+ * True when a group that contains `|` or a quantifier is itself repeated without a small bound.
195
+ * `(abc)+` and `(https?:\/\/)?` stay allowed.
196
+ */
197
+ function repeatsARiskyGroup(pattern: string): boolean {
198
+ const stack: { risky: boolean }[] = [];
199
+ let inClass = false;
200
+ const mark = () => {
201
+ const frame = stack[stack.length - 1];
202
+ if (frame) frame.risky = true;
203
+ };
204
+ for (let i = 0; i < pattern.length; i += 1) {
205
+ const ch = pattern[i];
206
+ if (ch === "\\") {
207
+ i += 1;
208
+ continue;
209
+ }
210
+ if (inClass) {
211
+ if (ch === "]") inClass = false;
212
+ continue;
213
+ }
214
+ if (ch === "[") {
215
+ inClass = true;
216
+ continue;
217
+ }
218
+ if (ch === "(") {
219
+ stack.push({ risky: false });
220
+ continue;
221
+ }
222
+ if (ch === ")") {
223
+ const frame = stack.pop();
224
+ if (!frame) return true;
225
+ const quantifier = quantifierAt(pattern, i + 1);
226
+ if (quantifier.dangerous && frame.risky) return true;
227
+ if (quantifier.repeats || frame.risky) mark();
228
+ i += quantifier.consumed;
229
+ continue;
230
+ }
231
+ if (ch === "|" || ch === "+" || ch === "*" || ch === "?") {
232
+ mark();
233
+ continue;
234
+ }
235
+ if (ch === "{") {
236
+ const quantifier = quantifierAt(pattern, i);
237
+ if (quantifier.consumed === 0) continue;
238
+ mark();
239
+ i += quantifier.consumed - 1;
240
+ }
241
+ }
242
+ return false;
243
+ }
244
+
245
+ function safeInputPattern(pattern: string): string | undefined {
246
+ const value = pattern.trim();
247
+ if (!value || value.length > 200 || hasControlChar(value)) return undefined;
248
+ if (repeatsARiskyGroup(value)) return undefined;
249
+ try {
250
+ const compiled = new RegExp(value);
251
+ compiled.test("");
252
+ } catch {
253
+ return undefined;
254
+ }
255
+ return value;
256
+ }
257
+
134
258
  /**
135
259
  * W-297: what the forms plugin's own embed puts on a control: the input type (so phones show the
136
260
  * right keyboard), `required`, and the length, range and pattern limits, plus a visible mark on
@@ -151,7 +275,8 @@ function withFieldMeta(vnode: VNode, node: LayoutNode, meta: PublicFormField): V
151
275
  if (rules.max !== undefined) limits.max = String(rules.max);
152
276
  }
153
277
  if (node.type === "text-input" && kind !== "number" && kind !== "date" && rules.pattern) {
154
- limits.pattern = rules.pattern;
278
+ const pattern = safeInputPattern(rules.pattern);
279
+ if (pattern) limits.pattern = pattern;
155
280
  }
156
281
  }
157
282
  const mark: VNode = {
@@ -176,30 +301,35 @@ function withFieldMeta(vnode: VNode, node: LayoutNode, meta: PublicFormField): V
176
301
  return { ...child, children: [...child.children, mark] };
177
302
  }
178
303
  // The checkbox label wraps the input and the label text; the mark follows the text.
304
+ // W-441: the same hidden mark as other labels; a plain " *" was read out as "star".
179
305
  if (child.attrs.class === "emvb-form-checkbox-label") {
180
- return {
181
- ...child,
182
- children: child.children.map((part) => (typeof part === "string" ? `${part} *` : part)),
183
- };
306
+ let last = -1;
307
+ child.children.forEach((part, i) => {
308
+ if (typeof part === "string") last = i;
309
+ });
310
+ if (last === -1) return child;
311
+ const children = [...child.children];
312
+ children.splice(last + 1, 0, mark);
313
+ return { ...child, children };
184
314
  }
185
315
  return child;
186
316
  });
187
317
  }
188
318
 
189
- /** Fills a field from the first bound form definition that has it: options, type, limits. */
319
+ /** Fills a field from the form it sits in: options, type, limits. Other forms are left alone. */
190
320
  export function withFieldOptions(
191
321
  vnode: VNode,
192
322
  node: LayoutNode,
193
323
  definitions: FormDefinitions,
324
+ formId?: string,
194
325
  ): VNode {
195
326
  const field = (node.props as { field?: unknown }).field;
196
- if (typeof field !== "string") return vnode;
197
- for (const definition of definitions.values()) {
198
- const meta = fieldByName(definition, field);
199
- if (!meta) continue;
200
- return withFieldMeta(withOptions(vnode, node, meta), node, meta);
201
- }
202
- return vnode;
327
+ if (typeof field !== "string" || !formId) return vnode;
328
+ const definition = definitions.get(formId);
329
+ if (!definition) return vnode;
330
+ const meta = fieldByName(definition, field);
331
+ if (!meta) return vnode;
332
+ return withFieldMeta(withOptions(vnode, node, meta), node, meta);
203
333
  }
204
334
 
205
335
  /** A select or radio field filled with the definition's options. */