@perkyzz/emvb 0.6.0 → 0.8.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 (208) hide show
  1. package/README.md +2 -2
  2. package/THIRD_PARTY_NOTICES.md +1 -1
  3. package/package.json +7 -7
  4. package/src/admin/access/RequireEditor.tsx +7 -7
  5. package/src/admin/access/role.ts +5 -2
  6. package/src/admin/content-api.ts +10 -3
  7. package/src/admin/content-item.ts +1 -1
  8. package/src/admin/editor/A11yCopilot.tsx +104 -56
  9. package/src/admin/editor/CommandPalette.tsx +29 -13
  10. package/src/admin/editor/DialogCloseButton.tsx +16 -0
  11. package/src/admin/editor/Editor.tsx +115 -20
  12. package/src/admin/editor/EditorToasts.tsx +26 -0
  13. package/src/admin/editor/PerfMeter.tsx +92 -73
  14. package/src/admin/editor/SavedSectionDialogs.tsx +169 -0
  15. package/src/admin/editor/ShareLink.tsx +102 -72
  16. package/src/admin/editor/ShortcutsDialog.tsx +37 -18
  17. package/src/admin/editor/Timeline.tsx +250 -160
  18. package/src/admin/editor/TopBar.tsx +71 -14
  19. package/src/admin/editor/canvas/CanvasFrame.tsx +10 -1
  20. package/src/admin/editor/canvas/CanvasTextEdit.tsx +8 -2
  21. package/src/admin/editor/canvas/drop-line.ts +11 -3
  22. package/src/admin/editor/canvas/frame-focus-ring.ts +37 -0
  23. package/src/admin/editor/canvas/use-canvas-dnd.ts +32 -3
  24. package/src/admin/editor/canvas/vnode-react.tsx +7 -0
  25. package/src/admin/editor/collection-previews.ts +10 -0
  26. package/src/admin/editor/dialogs.tsx +41 -28
  27. package/src/admin/editor/dnd/drop-target.ts +26 -1
  28. package/src/admin/editor/edited-noun.ts +15 -0
  29. package/src/admin/editor/editor-chrome.tsx +43 -7
  30. package/src/admin/editor/editor-commands.ts +17 -2
  31. package/src/admin/editor/editor-css.ts +75 -24
  32. package/src/admin/editor/element-chrome.ts +21 -11
  33. package/src/admin/editor/feature-css.ts +56 -14
  34. package/src/admin/editor/focus-when-lost.ts +23 -0
  35. package/src/admin/editor/icons/IconLibraryDialog.tsx +54 -18
  36. package/src/admin/editor/icons/library.ts +1 -1
  37. package/src/admin/editor/icons/use-icon-library.ts +5 -2
  38. package/src/admin/editor/ime.ts +29 -0
  39. package/src/admin/editor/palette-items.ts +27 -3
  40. package/src/admin/editor/panels/AddPanel.tsx +44 -11
  41. package/src/admin/editor/panels/ClassesSection.tsx +18 -7
  42. package/src/admin/editor/panels/ColorControl.tsx +130 -23
  43. package/src/admin/editor/panels/ConditionsEditor.tsx +52 -26
  44. package/src/admin/editor/panels/ElementPanel.tsx +134 -24
  45. package/src/admin/editor/panels/LayersPanel.tsx +61 -20
  46. package/src/admin/editor/panels/LeftPanel.tsx +9 -1
  47. package/src/admin/editor/panels/PageSettings.tsx +66 -35
  48. package/src/admin/editor/panels/SavedSectionsGroup.tsx +246 -0
  49. package/src/admin/editor/panels/SiteStylesDrawer.tsx +224 -42
  50. package/src/admin/editor/panels/TokenScales.tsx +88 -48
  51. package/src/admin/editor/panels/TriggerSettings.tsx +50 -12
  52. package/src/admin/editor/panels/TriggersEditor.tsx +3 -2
  53. package/src/admin/editor/panels/VariableSection.tsx +48 -18
  54. package/src/admin/editor/panels/color-space.ts +23 -29
  55. package/src/admin/editor/panels/settings/AdvancedFields.tsx +12 -2
  56. package/src/admin/editor/panels/settings/AudienceEditor.tsx +87 -74
  57. package/src/admin/editor/panels/settings/BackgroundControls.tsx +55 -11
  58. package/src/admin/editor/panels/settings/BackgroundFill.tsx +3 -2
  59. package/src/admin/editor/panels/settings/BindingsEditor.tsx +15 -18
  60. package/src/admin/editor/panels/settings/BoxControl.tsx +2 -1
  61. package/src/admin/editor/panels/settings/ClassChip.tsx +21 -9
  62. package/src/admin/editor/panels/settings/ClassChipInput.tsx +24 -16
  63. package/src/admin/editor/panels/settings/DraftTextField.tsx +41 -3
  64. package/src/admin/editor/panels/settings/EffectsControls.tsx +38 -14
  65. package/src/admin/editor/panels/settings/FieldBindControl.tsx +46 -28
  66. package/src/admin/editor/panels/settings/FieldControl.tsx +20 -8
  67. package/src/admin/editor/panels/settings/FormBindControl.tsx +2 -2
  68. package/src/admin/editor/panels/settings/HintedSwitch.tsx +33 -0
  69. package/src/admin/editor/panels/settings/IconControls.tsx +33 -13
  70. package/src/admin/editor/panels/settings/IconPicker.tsx +2 -0
  71. package/src/admin/editor/panels/settings/ImageSourcesField.tsx +3 -1
  72. package/src/admin/editor/panels/settings/ItemList.tsx +15 -2
  73. package/src/admin/editor/panels/settings/LengthRow.tsx +8 -6
  74. package/src/admin/editor/panels/settings/LoopItemBindControl.tsx +1 -1
  75. package/src/admin/editor/panels/settings/MediaPicker.tsx +127 -23
  76. package/src/admin/editor/panels/settings/MotionControl.tsx +28 -15
  77. package/src/admin/editor/panels/settings/NumberRow.tsx +9 -3
  78. package/src/admin/editor/panels/settings/PanelSelect.tsx +50 -0
  79. package/src/admin/editor/panels/settings/QueryStyleRows.tsx +25 -18
  80. package/src/admin/editor/panels/settings/RequiredFieldsNotice.tsx +66 -1
  81. package/src/admin/editor/panels/settings/StyleRow.tsx +10 -3
  82. package/src/admin/editor/panels/settings/StyleSources.tsx +4 -1
  83. package/src/admin/editor/panels/settings/VariantEditor.tsx +10 -1
  84. package/src/admin/editor/panels/settings/box-link-name.ts +6 -2
  85. package/src/admin/editor/panels/settings/class-suggest.ts +7 -7
  86. package/src/admin/editor/panels/settings/query-style.ts +38 -9
  87. package/src/admin/editor/panels/settings/svg-images.ts +13 -0
  88. package/src/admin/editor/panels/site-list.tsx +20 -6
  89. package/src/admin/editor/panels/variable-values.ts +11 -4
  90. package/src/admin/editor/restore-deleted.ts +21 -2
  91. package/src/admin/editor/section-templates.ts +81 -12
  92. package/src/admin/editor/shortcuts.ts +31 -2
  93. package/src/admin/editor/store.ts +7 -0
  94. package/src/admin/editor/useEditorCommands.ts +61 -15
  95. package/src/admin/editor/useEditorData.ts +4 -2
  96. package/src/admin/editor/useEditorShortcuts.ts +17 -7
  97. package/src/admin/editor/useNodeActions.ts +61 -6
  98. package/src/admin/editor/useSave.ts +22 -7
  99. package/src/admin/editor/useSavedSections.ts +197 -0
  100. package/src/admin/forms-api.ts +2 -0
  101. package/src/admin/image-editor/Canvas.tsx +61 -17
  102. package/src/admin/image-editor/CurvesPanel.tsx +2 -1
  103. package/src/admin/image-editor/ExportDialog.tsx +48 -18
  104. package/src/admin/image-editor/ImageEditorDialog.tsx +179 -8
  105. package/src/admin/image-editor/MetadataPanel.tsx +15 -4
  106. package/src/admin/image-editor/crop-geometry.ts +3 -2
  107. package/src/admin/image-editor/image-editor-css.ts +15 -10
  108. package/src/admin/image-editor/load.ts +9 -2
  109. package/src/admin/image-editor/parts.tsx +69 -19
  110. package/src/admin/image-editor/save.ts +54 -8
  111. package/src/admin/media-api.ts +2 -1
  112. package/src/admin/pages/AgentsPage.tsx +138 -27
  113. package/src/admin/pages/CreateDialog.tsx +97 -30
  114. package/src/admin/pages/NewPageDialog.tsx +21 -3
  115. package/src/admin/pages/NewThemePartDialog.tsx +11 -3
  116. package/src/admin/pages/PageList.tsx +14 -6
  117. package/src/admin/pages/SetupShell.tsx +28 -12
  118. package/src/admin/pages/ThemePartList.tsx +59 -11
  119. package/src/admin/pages/list-kit.tsx +106 -24
  120. package/src/admin/saved-sections-api.ts +51 -0
  121. package/src/admin/search.ts +15 -0
  122. package/src/admin/setup/useSetupStatus.ts +1 -1
  123. package/src/admin/theme-api.ts +16 -5
  124. package/src/admin/ui.ts +82 -22
  125. package/src/astro/floats/runtime.ts +21 -0
  126. package/src/astro/menu/runtime.ts +34 -1
  127. package/src/astro/popups/runtime.ts +41 -9
  128. package/src/astro/render.ts +3 -0
  129. package/src/astro/resolve-theme.ts +53 -11
  130. package/src/astro/resolve.ts +9 -1
  131. package/src/astro/tabs/runtime.ts +12 -0
  132. package/src/astro/theme-posts.ts +2 -2
  133. package/src/astro/visitor.ts +2 -0
  134. package/src/constants.ts +1 -1
  135. package/src/core/a11y/audit.ts +196 -24
  136. package/src/core/arrange.ts +48 -12
  137. package/src/core/audience/rules.ts +5 -1
  138. package/src/core/audience/variants.ts +29 -3
  139. package/src/core/clipboard.ts +7 -0
  140. package/src/core/css/generate.ts +31 -6
  141. package/src/core/css/print.ts +5 -0
  142. package/src/core/data/bindings.ts +8 -1
  143. package/src/core/design/classes.ts +39 -16
  144. package/src/core/design/devices.ts +7 -13
  145. package/src/core/design/diff.ts +6 -0
  146. package/src/core/design/merge.ts +90 -2
  147. package/src/core/design/states.ts +7 -13
  148. package/src/core/design/tokens.ts +16 -2
  149. package/src/core/design/transfer.ts +33 -6
  150. package/src/core/design/variables.ts +30 -9
  151. package/src/core/elements/dynamic-elements.ts +53 -14
  152. package/src/core/elements/form-elements.ts +55 -16
  153. package/src/core/elements/index.ts +84 -39
  154. package/src/core/elements/menu.ts +40 -19
  155. package/src/core/elements/select-prompt.ts +3 -0
  156. package/src/core/elements/svg-ids.ts +39 -0
  157. package/src/core/forms/binding.ts +57 -16
  158. package/src/core/forms/definition.ts +72 -0
  159. package/src/core/history/diff.ts +18 -1
  160. package/src/core/icons/animation-runs.ts +11 -0
  161. package/src/core/icons/catalog.ts +3 -2
  162. package/src/core/image-edit/schema.ts +11 -1
  163. package/src/core/image-edit/summary.ts +6 -3
  164. package/src/core/index.ts +35 -1
  165. package/src/core/limits.ts +12 -1
  166. package/src/core/migrate/index.ts +3 -0
  167. package/src/core/perf/meter.ts +42 -7
  168. package/src/core/recipes/index.ts +5 -4
  169. package/src/core/render/context.ts +8 -0
  170. package/src/core/render/dynamic-post.ts +69 -12
  171. package/src/core/render/extras.ts +77 -4
  172. package/src/core/render/form.ts +140 -6
  173. package/src/core/render/index.ts +156 -20
  174. package/src/core/render/loop.ts +44 -8
  175. package/src/core/render/pagination.ts +18 -4
  176. package/src/core/render/section.ts +27 -0
  177. package/src/core/render/tabs.ts +12 -3
  178. package/src/core/render/vnode.ts +1 -1
  179. package/src/core/sanitize/css.ts +29 -8
  180. package/src/core/sanitize/embed-url.ts +42 -5
  181. package/src/core/sanitize/url.ts +2 -1
  182. package/src/core/saved-sections.ts +318 -0
  183. package/src/core/schema/design.ts +1 -1
  184. package/src/core/schema/layout.ts +16 -7
  185. package/src/core/schema/style.ts +4 -2
  186. package/src/core/stats.ts +2 -1
  187. package/src/core/theme/conditions.ts +3 -1
  188. package/src/core/theme/dynamic.ts +36 -5
  189. package/src/core/theme/float.ts +12 -2
  190. package/src/core/theme/pagination.ts +50 -29
  191. package/src/core/theme/part-types.ts +4 -4
  192. package/src/core/theme/popup-markup.ts +23 -5
  193. package/src/core/theme/popup-rules.ts +1 -0
  194. package/src/core/theme/triggers.ts +3 -2
  195. package/src/core/tree-ops.ts +27 -0
  196. package/src/mcp/ports.ts +22 -1
  197. package/src/mcp/rate-limit.ts +2 -1
  198. package/src/mcp/read.ts +15 -3
  199. package/src/mcp/refs.ts +18 -0
  200. package/src/mcp/sections.ts +183 -0
  201. package/src/mcp/tools.ts +44 -0
  202. package/src/mcp/write.ts +178 -14
  203. package/src/server/agent-settings.ts +1 -1
  204. package/src/server/hooks.ts +5 -3
  205. package/src/server/icon-routes.ts +30 -9
  206. package/src/server/mcp-routes.ts +8 -1
  207. package/src/server/plugin.ts +14 -0
  208. package/src/server/saved-sections.ts +163 -0
@@ -51,25 +51,55 @@ function imageBytes(node: LayoutNode): number {
51
51
  return w > 0 && h > 0 ? Math.max(5_000, Math.round(w * h * 0.25)) : IMAGE_GUESS;
52
52
  }
53
53
 
54
- const hasBackgroundImage = (node: LayoutNode): boolean => {
55
- const sheets = [node.style, ...Object.values(node.devices ?? {})];
54
+ type Sheets = { style?: unknown; devices?: Record<string, unknown>; query?: { style?: unknown } };
55
+ const sheetsHaveBackground = (owner: Sheets): boolean => {
56
+ // W-645: a container-query override (W-346) can set the background too.
57
+ const sheets = [owner.style, ...Object.values(owner.devices ?? {}), owner.query?.style];
56
58
  return sheets.some((style) => {
57
59
  const value = (style as { backgroundImage?: unknown } | undefined)?.backgroundImage;
58
60
  return typeof value === "string" ? value.length > 0 : Boolean(value);
59
61
  });
60
62
  };
61
63
 
64
+ /** Ids of global classes that set a background image (W-635). */
65
+ const backgroundClasses = (design: DesignSystem): Set<string> =>
66
+ new Set((design.classes ?? []).filter((c) => sheetsHaveBackground(c)).map((c) => c.id));
67
+
68
+ /** Its own style or one of its classes sets a background image (W-635). */
69
+ const hasBackgroundImage = (node: LayoutNode, bgClasses: Set<string>): boolean =>
70
+ sheetsHaveBackground(node as Sheets) || (node.classes ?? []).some((id) => bgClasses.has(id));
71
+
62
72
  const isPlayerEmbed = (node: LayoutNode): boolean => {
63
73
  const url = (node.props as { url?: unknown }).url;
64
74
  return typeof url === "string" && resolveEmbedUrl(url)?.kind === "iframe";
65
75
  };
66
76
 
67
77
  /** Images and embeds under a node, with their estimated weight. */
68
- function media(node: LayoutNode): { images: number; bytes: number; embeds: number } {
78
+ function media(
79
+ node: LayoutNode,
80
+ bgClasses: Set<string>,
81
+ dynamic?: ThemeDynamicData,
82
+ ): { images: number; bytes: number; embeds: number } {
69
83
  let images = 0;
70
84
  let bytes = 0;
71
85
  let embeds = 0;
86
+ const seen = new Set<Layout>();
87
+ /** W-671: a synced section or Loop Item part the canvas loaded is weighed with the page. */
88
+ const part = (n: LayoutNode): Layout | undefined => {
89
+ const props = n.props as { partId?: unknown; itemPartId?: unknown };
90
+ const found =
91
+ n.type === "section" && typeof props.partId === "string"
92
+ ? dynamic?.sectionTemplates?.[props.partId.trim()]
93
+ : n.type === "loop" && typeof props.itemPartId === "string"
94
+ ? dynamic?.loopTemplates?.[props.itemPartId.trim()]
95
+ : undefined;
96
+ if (!found || seen.has(found)) return undefined;
97
+ seen.add(found);
98
+ return found;
99
+ };
72
100
  const walk = (n: LayoutNode): void => {
101
+ const template = part(n);
102
+ if (template) walk(template.root);
73
103
  if (n.type === "image" || n.type === "post-image") {
74
104
  const src = (n.props as { src?: unknown }).src;
75
105
  if (n.type === "post-image" || (typeof src === "string" && src.trim())) {
@@ -77,7 +107,7 @@ function media(node: LayoutNode): { images: number; bytes: number; embeds: numbe
77
107
  bytes += imageBytes(n);
78
108
  }
79
109
  }
80
- if (hasBackgroundImage(n)) {
110
+ if (hasBackgroundImage(n, bgClasses)) {
81
111
  images += 1;
82
112
  bytes += IMAGE_GUESS;
83
113
  }
@@ -110,7 +140,8 @@ export function measurePage(
110
140
  bytes: SCRIPT_BYTES[script],
111
141
  nodeIds,
112
142
  }));
113
- const all = media(layout.root);
143
+ const bgClasses = backgroundClasses(design);
144
+ const all = media(layout.root, bgClasses, dynamic);
114
145
  const bytes: Record<PerfKind, number> = {
115
146
  html: Math.round(utf8(rendered.html) * GZIP),
116
147
  css: Math.round(utf8(rendered.css) * GZIP),
@@ -126,7 +157,7 @@ export function measurePage(
126
157
  root: Object.assign({}, layout.root, { children: [child] }),
127
158
  };
128
159
  const one = renderPage(alone, design, opts);
129
- const m = media(child);
160
+ const m = media(child, bgClasses, dynamic);
130
161
  sections.push({
131
162
  id: child.id,
132
163
  type: child.type,
@@ -157,8 +188,12 @@ export function measurePage(
157
188
  return { bytes, over, scripts, imageCount: all.images, embeds: all.embeds, sections, warnings };
158
189
  }
159
190
 
191
+ /** W-574: the first image that renders; an Image with no picture yet shows nothing to load. */
160
192
  function findFirstImage(node: LayoutNode): LayoutNode | undefined {
161
- if (node.type === "image") return node;
193
+ if (node.type === "image") {
194
+ const src = (node.props as { src?: unknown }).src;
195
+ if (typeof src === "string" && src.trim()) return node;
196
+ }
162
197
  if (!isParentNode(node)) return undefined;
163
198
  for (const child of node.children) {
164
199
  const found = findFirstImage(child);
@@ -103,12 +103,13 @@ export function siteTokens(design: DesignSystem): RecipeTokens {
103
103
  if (surface) tokens.surface = surface;
104
104
  const onAccent = color(pick(others, /on[- ]?(primary|brand|accent)|inverse|white/));
105
105
  if (onAccent) tokens.onAccent = onAccent;
106
- else if (accentVar && /^#[0-9a-f]{6}$/i.test(accentVar.value)) {
106
+ else if (accentVar && /^#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i.test(accentVar.value)) {
107
107
  // No "on accent" colour: white or near-black, whichever reads better on the accent.
108
+ // W-642: a short (#fd0) or alpha (#ffdd00cc) accent is judged as its six-digit colour.
109
+ const h = accentVar.value.slice(1);
110
+ const hex = `#${(h.length <= 4 ? [...h].map((c) => c + c).join("") : h).slice(0, 6)}`;
108
111
  tokens.onAccent =
109
- contrastRatio(accentVar.value, "#ffffff") >= contrastRatio(accentVar.value, "#111111")
110
- ? "#ffffff"
111
- : "#111111";
112
+ contrastRatio(hex, "#ffffff") >= contrastRatio(hex, "#111111") ? "#ffffff" : "#111111";
112
113
  }
113
114
  const buttonCls = pick(design.classes, /\b(button|btn)\b/, /cta/);
114
115
  if (buttonCls) {
@@ -18,6 +18,14 @@ export type RenderContext = {
18
18
  * stay with their own copy.
19
19
  */
20
20
  uniqueId: (base: string) => string;
21
+ /**
22
+ * W-653: claims `nodes` elements from this render's synced-section budget; false once spent.
23
+ * Sections nested in sections multiply, so a few small parts could expand to millions of
24
+ * elements and stall the page.
25
+ */
26
+ takeSectionBudget?: (nodes: number) => boolean;
27
+ /** The page's language, e.g. "fr-CA", for dates (W-688). */
28
+ lang?: string;
21
29
  /** The form whose children are rendering, so each field reads that form's definition. */
22
30
  formId?: string;
23
31
  };
@@ -7,6 +7,39 @@ import { portableTextToVNodes, type ThemePostFields } from "../theme/dynamic.ts"
7
7
  import type { RenderMode } from "./context.ts";
8
8
  import type { VNode } from "./vnode.ts";
9
9
 
10
+ /** Scripts written without spaces between words; a space-separated chunk holds many words. */
11
+ const UNSPACED =
12
+ /[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Thai}\p{Script=Lao}\p{Script=Khmer}\p{Script=Myanmar}]/u;
13
+ let wordSegmenter: Intl.Segmenter | undefined;
14
+
15
+ /**
16
+ * The first `max` words, then "…". W-660: Japanese, Chinese or Thai text has no spaces between
17
+ * words, so splitting on spaces counted a whole excerpt as one word and never shortened it.
18
+ * Those chunks are split into words; spaced text is counted by spaces as before.
19
+ */
20
+ function limitWords(text: string, max: number | undefined): string {
21
+ if (!max || max < 1) return text;
22
+ let count = 0;
23
+ for (const chunk of text.matchAll(/\S+/g)) {
24
+ const start = chunk.index ?? 0;
25
+ if (!UNSPACED.test(chunk[0])) {
26
+ count++;
27
+ if (count === max) return finish(text, start + chunk[0].length);
28
+ continue;
29
+ }
30
+ wordSegmenter ??= new Intl.Segmenter(undefined, { granularity: "word" });
31
+ for (const piece of wordSegmenter.segment(chunk[0])) {
32
+ if (!piece.isWordLike) continue;
33
+ count++;
34
+ if (count === max) return finish(text, start + piece.index + piece.segment.length);
35
+ }
36
+ }
37
+ return text;
38
+ }
39
+
40
+ const finish = (text: string, end: number) =>
41
+ text.slice(end).trim() === "" ? text : `${text.slice(0, end).trim().replace(/\s+/g, " ")}…`;
42
+
10
43
  /**
11
44
  * W-196: only an ISO date (`2026-10-06`, optionally with a time) is read as a date. `Date.parse`
12
45
  * also takes text like "Spring 2026" (as Jan 1) or "2026-02-30", which would show a wrong date.
@@ -17,8 +50,10 @@ const parseIsoDate = (raw: string): number => {
17
50
  const parsed = Date.parse(raw.replace(" ", "T"));
18
51
  if (Number.isNaN(parsed)) return Number.NaN;
19
52
  const day = new Date(Date.UTC(Number(match[1]), Number(match[2]) - 1, Number(match[3])));
53
+ // W-599: the day as written. A post at 22:00-04:00 was published that day, not the next
54
+ // one in UTC, and a time with no zone isn't moved by the server's zone.
20
55
  return day.getUTCDate() === Number(match[3]) && day.getUTCMonth() === Number(match[2]) - 1
21
- ? parsed
56
+ ? day.getTime()
22
57
  : Number.NaN;
23
58
  };
24
59
 
@@ -35,16 +70,32 @@ type PostField = {
35
70
  attrs: Attrs,
36
71
  post: ThemePostFields,
37
72
  inLoop: boolean,
73
+ lang?: string,
38
74
  ) => VNode | undefined;
39
75
  };
40
76
 
41
77
  type PostDateFormat = (typeof POST_DATE_FORMATS)[number];
42
78
 
43
- /** In UTC, so the date doesn't move with the server's time zone. */
44
- function formatPostDate(time: number, format: PostDateFormat): string {
79
+ /** A BCP 47-shaped language tag Intl can use, else English (W-688). */
80
+ function dateLocale(lang: string | undefined): string {
81
+ if (!lang || !/^[A-Za-z]{2,8}(?:-[A-Za-z0-9]{1,8})*$/.test(lang)) return "en";
82
+ try {
83
+ return Intl.DateTimeFormat.supportedLocalesOf([lang])[0] ?? "en";
84
+ } catch {
85
+ return "en";
86
+ }
87
+ }
88
+
89
+ /**
90
+ * In UTC on the written day, so the date doesn't move with the server's time zone.
91
+ * W-688: in the page's language ("6 oct. 2026" on a French site); it was always English.
92
+ */
93
+ function formatPostDate(time: number, format: PostDateFormat, lang?: string): string {
45
94
  const date = new Date(time);
46
95
  if (format === "numeric") return date.toISOString().slice(0, 10);
47
- return new Intl.DateTimeFormat("en", { dateStyle: format, timeZone: "UTC" }).format(date);
96
+ return new Intl.DateTimeFormat(dateLocale(lang), { dateStyle: format, timeZone: "UTC" }).format(
97
+ date,
98
+ );
48
99
  }
49
100
 
50
101
  const props = <T>(node: LayoutNode) => node.props as T;
@@ -54,6 +105,9 @@ const POST_FIELDS = new Map<string, PostField>([
54
105
  "post-title",
55
106
  {
56
107
  placeholder: (attrs) => ({ tag: "h1", attrs, children: ["Post Title"] }),
108
+ // W-661: an untitled post left an empty heading on the page (a blank entry for screen
109
+ // reader heading lists).
110
+ has: (post) => !!post.title?.trim(),
57
111
  render: (node, attrs, post) => {
58
112
  const level = props<{ level?: number }>(node).level ?? 1;
59
113
  const tag = HEADING_TAGS[Math.min(6, Math.max(1, level)) - 1] ?? "h1";
@@ -66,10 +120,8 @@ const POST_FIELDS = new Map<string, PostField>([
66
120
  {
67
121
  placeholder: (attrs) => ({ tag: "p", attrs, children: ["Post excerpt…"] }),
68
122
  render: (node, attrs, post) => {
69
- if (!post.excerpt) return undefined;
70
- const max = props<{ maxWords?: number }>(node).maxWords;
71
- const words = post.excerpt.trim().split(/\s+/);
72
- const text = max && words.length > max ? `${words.slice(0, max).join(" ")}…` : post.excerpt;
123
+ if (!post.excerpt?.trim()) return undefined;
124
+ const text = limitWords(post.excerpt, props<{ maxWords?: number }>(node).maxWords);
73
125
  return { tag: "p", attrs, children: [text] };
74
126
  },
75
127
  },
@@ -102,7 +154,7 @@ const POST_FIELDS = new Map<string, PostField>([
102
154
  if (!src) return undefined;
103
155
  const alt = props<{ decorative?: boolean }>(node).decorative
104
156
  ? ""
105
- : post.featuredImageAlt?.trim() || post.title || "Featured image";
157
+ : post.featuredImageAlt?.trim() || post.title?.trim() || "Featured image";
106
158
  // W-440: a Loop's images below the fold loaded all at once; like Image, they wait.
107
159
  // A single post's featured image is usually the hero, so it isn't delayed.
108
160
  const lazy: Attrs = inLoop ? { loading: "lazy", decoding: "async" } : {};
@@ -147,12 +199,15 @@ const POST_FIELDS = new Map<string, PostField>([
147
199
  {
148
200
  placeholder: (attrs) => ({ tag: "time", attrs, children: ["Post date"] }),
149
201
  has: (post) => !!post.publishedAt?.trim(),
150
- render: (node, attrs, post) => {
202
+ render: (node, attrs, post, _inLoop, lang) => {
151
203
  const raw = post.publishedAt?.trim();
152
204
  if (!raw) return undefined;
153
205
  const parsed = parseIsoDate(raw);
154
206
  const format = props<{ format?: PostDateFormat }>(node).format ?? "medium";
155
- const text = Number.isNaN(parsed) ? raw.slice(0, 80) : formatPostDate(parsed, format);
207
+ // W-700: whole characters, so a long free-text date doesn't end in half an emoji.
208
+ const text = Number.isNaN(parsed)
209
+ ? clipText(raw, 80)
210
+ : formatPostDate(parsed, format, lang);
156
211
  // W-196: `datetime` must be machine-readable, so a date we can't read only shows as text.
157
212
  const timeAttrs = Number.isNaN(parsed) ? attrs : { ...attrs, datetime: raw.slice(0, 80) };
158
213
  return { tag: "time", attrs: timeAttrs, children: [text] };
@@ -181,11 +236,13 @@ export function renderDynamicPost(
181
236
  post: ThemePostFields | undefined,
182
237
  mode: RenderMode,
183
238
  inLoop = false,
239
+ /** The page's language, e.g. "fr-CA", for dates (W-688). */
240
+ lang?: string,
184
241
  ): VNode | undefined {
185
242
  const field = POST_FIELDS.get(node.type);
186
243
  if (!field) return undefined;
187
244
  if (!post || field.has?.(post) === false) {
188
245
  return mode === "editor" ? field.placeholder(attrs) : undefined;
189
246
  }
190
- return field.render(node, attrs, post, inLoop);
247
+ return field.render(node, attrs, post, inLoop, lang);
191
248
  }
@@ -46,9 +46,49 @@ function childrenOf(node: LayoutNode): LayoutNode[] {
46
46
  }
47
47
 
48
48
  /** A link, a button, or a box that is already a link. */
49
+ /**
50
+ * W-668: elements that always render a link, control or embed. A linked box around one of these
51
+ * nested an <a> in an <a> (the browser then splits the box apart) or put a menu, form field,
52
+ * accordion toggle or video player inside a link.
53
+ */
54
+ const ALWAYS_INTERACTIVE = new Set([
55
+ "link",
56
+ "post-link",
57
+ "button",
58
+ "menu",
59
+ "menu-item",
60
+ "accordion",
61
+ "accordion-item",
62
+ "tabs",
63
+ "video",
64
+ "form",
65
+ "text-input",
66
+ "textarea",
67
+ "select",
68
+ "checkbox",
69
+ "radio",
70
+ "submit",
71
+ "pagination",
72
+ "toc",
73
+ // Post content can hold links of its own.
74
+ "post-content",
75
+ ]);
76
+
49
77
  function containsInteractive(node: LayoutNode): boolean {
50
- if (node.type === "link" || node.type === "post-link" || node.type === "button") return true;
51
- if (BOX.has(node.type) || node.type === "heading" || node.type === "icon") {
78
+ if (ALWAYS_INTERACTIVE.has(node.type)) return true;
79
+ const props = node.props as { partId?: unknown; itemPartId?: unknown };
80
+ // A synced section or Loop Item part is another layout this check can't see into.
81
+ if (node.type === "section" && typeof props.partId === "string" && props.partId.trim())
82
+ return true;
83
+ if (node.type === "loop" && typeof props.itemPartId === "string" && props.itemPartId.trim()) {
84
+ return true;
85
+ }
86
+ if (
87
+ BOX.has(node.type) ||
88
+ node.type === "heading" ||
89
+ node.type === "icon" ||
90
+ node.type === "image"
91
+ ) {
52
92
  const href = (node.props as { href?: unknown }).href;
53
93
  if (typeof href === "string" && href.trim()) return true;
54
94
  }
@@ -59,6 +99,26 @@ function containsInteractive(node: LayoutNode): boolean {
59
99
  * A div box becomes the link. A landmark tag stays a landmark. A link already inside the box
60
100
  * keeps the box from becoming a link, so the page never nests anchors.
61
101
  */
102
+ /**
103
+ * W-669: a box with a working Link that still renders as a box because something inside it is a
104
+ * link, control or embed. The editor says so; the Link otherwise did nothing without a word.
105
+ */
106
+ export function boxLinkHeldBack(node: LayoutNode): boolean {
107
+ if (!BOX.has(node.type)) return false;
108
+ const href = (node.props as { href?: unknown }).href;
109
+ if (typeof href !== "string" || !href.trim() || !sanitizeHref(href)) return false;
110
+ return childrenOf(node).some(containsInteractive);
111
+ }
112
+
113
+ /** W-798: the same test applyBoxLink makes, for checks that see only the layout (the audit). */
114
+ export function boxBecomesLink(node: LayoutNode): boolean {
115
+ if (!BOX.has(node.type)) return false;
116
+ const { href, tag } = node.props as { href?: unknown; tag?: unknown };
117
+ if (tag !== undefined && tag !== "div") return false;
118
+ if (typeof href !== "string" || !href.trim() || !sanitizeHref(href)) return false;
119
+ return !childrenOf(node).some(containsInteractive);
120
+ }
121
+
62
122
  export function applyBoxLink(node: LayoutNode, vnode: VNode): VNode {
63
123
  if (!BOX.has(node.type) || vnode.tag !== "div") return vnode;
64
124
  const { href, newTab } = node.props as { href?: string; newTab?: boolean };
@@ -66,7 +126,11 @@ export function applyBoxLink(node: LayoutNode, vnode: VNode): VNode {
66
126
  if (childrenOf(node).some(containsInteractive)) return vnode;
67
127
  const safe = sanitizeHref(href);
68
128
  if (!safe) return vnode;
69
- const attrs: Record<string, string> = { ...vnode.attrs, href: safe };
129
+ const attrs: Record<string, string> = {
130
+ ...vnode.attrs,
131
+ class: `${vnode.attrs.class ?? ""} emvb-box-link`.trim(),
132
+ href: safe,
133
+ };
70
134
  if (newTab) {
71
135
  attrs.target = "_blank";
72
136
  attrs.rel = "noopener noreferrer";
@@ -89,7 +153,16 @@ export function applyBackgroundVideo(vnode: VNode, src: string | undefined): VNo
89
153
  children: [
90
154
  {
91
155
  tag: "video",
92
- attrs: { class: "emvb-bg-video", src, muted: "", loop: "", playsinline: "", autoplay: "" },
156
+ // W-801: decoration, not content, so screen readers skip it.
157
+ attrs: {
158
+ class: "emvb-bg-video",
159
+ src,
160
+ muted: "",
161
+ loop: "",
162
+ playsinline: "",
163
+ autoplay: "",
164
+ "aria-hidden": "true",
165
+ },
93
166
  children: [],
94
167
  },
95
168
  ...vnode.children,
@@ -1,5 +1,12 @@
1
- import { fieldByName, type FormDefinitions, type PublicFormField } from "../forms/definition.ts";
1
+ import {
2
+ fieldByName,
3
+ fieldsOf,
4
+ type FormDefinitions,
5
+ type PublicFormDefinition,
6
+ type PublicFormField,
7
+ } from "../forms/definition.ts";
2
8
  import type { FormNode, LayoutNode } from "../schema/layout.ts";
9
+ import { selectPrompt } from "../elements/select-prompt.ts";
3
10
  import type { ThemeDynamicData } from "../theme/dynamic.ts";
4
11
  import { nodeChildren } from "../tree-ops.ts";
5
12
  import type { RenderContext } from "./context.ts";
@@ -115,6 +122,7 @@ export function renderForm(
115
122
  children: [
116
123
  page,
117
124
  { tag: "input", attrs: { type: "hidden", name: "formId", value: formId }, children: [] },
125
+ ...hiddenFields(ctx.definitions.get(formId)),
118
126
  // One id per form element, so the same form placed twice keeps unique ids (W-187), also
119
127
  // when one form element renders twice, from a synced section or a loop item (W-249).
120
128
  honeypot(ctx.uniqueId(`emvb-hp-${node.id}`)),
@@ -127,6 +135,24 @@ export function renderForm(
127
135
  };
128
136
  }
129
137
 
138
+ /**
139
+ * W-595: the definition's hidden fields (a source tag, a campaign id) have no control to place,
140
+ * so the form carries them with their default value, as the forms plugin's own embed does.
141
+ */
142
+ function hiddenFields(definition: PublicFormDefinition | undefined): VNode[] {
143
+ if (!definition) return [];
144
+ return fieldsOf(definition)
145
+ .filter((field) => field.type === "hidden" && field.name)
146
+ .map((field) => {
147
+ const value = (field as { defaultValue?: unknown }).defaultValue;
148
+ return {
149
+ tag: "input",
150
+ attrs: { type: "hidden", name: field.name, value: typeof value === "string" ? value : "" },
151
+ children: [],
152
+ };
153
+ });
154
+ }
155
+
130
156
  /** Replaces the children of every node matching `match`, anywhere in the tree. */
131
157
  function replaceChildren(vnode: VNode, match: (node: VNode) => boolean, children: VNode[]): VNode {
132
158
  if (match(vnode)) return { ...vnode, children };
@@ -217,6 +243,9 @@ function repeatsARiskyGroup(pattern: string): boolean {
217
243
  }
218
244
  if (ch === "(") {
219
245
  stack.push({ risky: false });
246
+ // W-594: the `?` of `(?:`, `(?=`, `(?!`, `(?<=`, `(?<!` or `(?<name>` is not a quantifier.
247
+ const prefix = /^\?(?::|=|!|<=|<!|<[A-Za-z_$][\w$]*>)/.exec(pattern.slice(i + 1));
248
+ if (prefix) i += prefix[0].length;
220
249
  continue;
221
250
  }
222
251
  if (ch === ")") {
@@ -252,7 +281,69 @@ function safeInputPattern(pattern: string): string | undefined {
252
281
  } catch {
253
282
  return undefined;
254
283
  }
255
- return value;
284
+ if (compilesAsPattern(value)) return value;
285
+ const repaired = escapeClassLiterals(value);
286
+ return compilesAsPattern(repaired) ? repaired : undefined;
287
+ }
288
+
289
+ /**
290
+ * W-538: browsers compile `pattern` as `^(?:…)$` with the `v` flag, which refuses a common
291
+ * pattern such as `[A-Za-z0-9_-]+` (a `-` or `(` left bare in a class). The browser then drops
292
+ * the check with a console error, so the field went unchecked.
293
+ */
294
+ function compilesAsPattern(value: string): boolean {
295
+ try {
296
+ return new RegExp(`^(?:${value})$`, "v").source.length > 0;
297
+ } catch {
298
+ return false;
299
+ }
300
+ }
301
+
302
+ /** Escapes, inside character classes, the literals `v` mode wants escaped: a loose `-`, ()[]{}/|. */
303
+ function escapeClassLiterals(value: string): string {
304
+ let out = "";
305
+ let inClass = false;
306
+ let classStart = 0;
307
+ for (let i = 0; i < value.length; i += 1) {
308
+ const ch = value[i] as string;
309
+ if (ch === "\\") {
310
+ out += ch + (value[i + 1] ?? "");
311
+ i += 1;
312
+ continue;
313
+ }
314
+ if (!inClass) {
315
+ if (ch === "[") {
316
+ inClass = true;
317
+ out += ch;
318
+ if (value[i + 1] === "^") {
319
+ out += "^";
320
+ i += 1;
321
+ }
322
+ classStart = i + 1;
323
+ continue;
324
+ }
325
+ out += ch;
326
+ continue;
327
+ }
328
+ if (ch === "]") {
329
+ inClass = false;
330
+ out += ch;
331
+ continue;
332
+ }
333
+ if (ch === "-") {
334
+ const loose = i === classStart || value[i + 1] === "]";
335
+ out += loose ? "\\-" : ch;
336
+ continue;
337
+ }
338
+ out += "()[{}/|".includes(ch) ? `\\${ch}` : ch;
339
+ }
340
+ return out;
341
+ }
342
+
343
+ function lengthLimit(value: unknown, round: (n: number) => number): string | undefined {
344
+ if (typeof value !== "number" || !Number.isFinite(value)) return undefined;
345
+ const whole = round(value);
346
+ return whole >= 0 ? String(whole) : undefined;
256
347
  }
257
348
 
258
349
  /**
@@ -267,12 +358,19 @@ function withFieldMeta(vnode: VNode, node: LayoutNode, meta: PublicFormField): V
267
358
  const limits: Record<string, string> = {};
268
359
  if (node.type === "text-input" || node.type === "textarea") {
269
360
  if (LENGTH_TYPES.has(kind)) {
270
- if (rules.minLength !== undefined) limits.minlength = String(rules.minLength);
271
- if (rules.maxLength !== undefined) limits.maxlength = String(rules.maxLength);
361
+ // W-588: the browser drops a minlength/maxlength that isn't a whole number >= 0, so a
362
+ // fractional limit is rounded inward and a negative or non-finite one is left off.
363
+ const lo = lengthLimit(rules.minLength, Math.ceil);
364
+ const hi = lengthLimit(rules.maxLength, Math.floor);
365
+ if (lo !== undefined) limits.minlength = lo;
366
+ if (hi !== undefined) limits.maxlength = hi;
272
367
  }
273
368
  if (kind === "number") {
274
369
  if (rules.min !== undefined) limits.min = String(rules.min);
275
370
  if (rules.max !== undefined) limits.max = String(rules.max);
371
+ // W-578: the forms plugin accepts any number; the browser's default step of 1 refused
372
+ // decimals (and, counted from a fractional min, every whole number).
373
+ limits.step = "any";
276
374
  }
277
375
  if (node.type === "text-input" && kind !== "number" && kind !== "date" && rules.pattern) {
278
376
  const pattern = safeInputPattern(rules.pattern);
@@ -317,7 +415,7 @@ function withFieldMeta(vnode: VNode, node: LayoutNode, meta: PublicFormField): V
317
415
  }
318
416
 
319
417
  /** Fills a field from the form it sits in: options, type, limits. Other forms are left alone. */
320
- export function withFieldOptions(
418
+ function withDefinedField(
321
419
  vnode: VNode,
322
420
  node: LayoutNode,
323
421
  definitions: FormDefinitions,
@@ -332,6 +430,38 @@ export function withFieldOptions(
332
430
  return withFieldMeta(withOptions(vnode, node, meta), node, meta);
333
431
  }
334
432
 
433
+ /** Fills a field from the form it sits in, and carries its show condition (W-596). */
434
+ export function withFieldOptions(
435
+ vnode: VNode,
436
+ node: LayoutNode,
437
+ definitions: FormDefinitions,
438
+ formId?: string,
439
+ ): VNode {
440
+ const built = withDefinedField(vnode, node, definitions, formId);
441
+ const field = (node.props as { field?: unknown }).field;
442
+ if (built === vnode || typeof field !== "string" || !formId) return built;
443
+ const meta = fieldByName(definitions.get(formId), field);
444
+ return meta ? withCondition(built, meta) : built;
445
+ }
446
+
447
+ const CONDITION_OPS = new Set(["eq", "neq", "filled", "empty"]);
448
+
449
+ /**
450
+ * W-596: a field the definition shows only on a condition (`other` when Topic is Other) carries
451
+ * it as the plugin's embed does; the forms client then hides and disables it, so its `required`
452
+ * no longer blocks the form while it can't be filled.
453
+ */
454
+ function withCondition(vnode: VNode, meta: PublicFormField): VNode {
455
+ const raw = (meta as { condition?: unknown }).condition;
456
+ if (!raw || typeof raw !== "object") return vnode;
457
+ const { field, op, value } = raw as Record<string, unknown>;
458
+ if (typeof field !== "string" || !field || typeof op !== "string" || !CONDITION_OPS.has(op)) {
459
+ return vnode;
460
+ }
461
+ const condition = typeof value === "string" ? { field, op, value } : { field, op };
462
+ return { ...vnode, attrs: { ...vnode.attrs, "data-condition": JSON.stringify(condition) } };
463
+ }
464
+
335
465
  /** The fieldset that holds a radio group's options. */
336
466
  const radioGroup = (child: VNode) =>
337
467
  child.tag === "fieldset" && (child.attrs.class ?? "").includes("emvb-radio-group");
@@ -342,7 +472,11 @@ function withOptions(vnode: VNode, node: LayoutNode, meta: PublicFormField): VNo
342
472
  if ((node.type !== "select" && node.type !== "radio") || !meta.options?.length) return vnode;
343
473
  if (node.type === "select") {
344
474
  return replaceChildren(vnode, (child) => child.tag === "select", [
345
- { tag: "option", attrs: { value: "" }, children: ["Choose…"] },
475
+ {
476
+ tag: "option",
477
+ attrs: { value: "" },
478
+ children: [selectPrompt(node.props as { placeholder?: string })],
479
+ },
346
480
  ...meta.options.map((option): VNode => ({
347
481
  tag: "option",
348
482
  attrs: { value: option.value },