@perkyzz/emvb 0.5.0 → 0.7.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 (212) hide show
  1. package/README.md +4 -0
  2. package/THIRD_PARTY_NOTICES.md +23 -1
  3. package/licenses/exif-js.LICENSE +21 -0
  4. package/licenses/fflate.LICENSE +21 -0
  5. package/licenses/glfx.js.LICENSE +19 -0
  6. package/licenses/mini-photo-editor.LICENSE +21 -0
  7. package/licenses/photocn.LICENSE +21 -0
  8. package/package.json +2 -1
  9. package/src/admin/access/RequireEditor.tsx +7 -7
  10. package/src/admin/access/role.ts +5 -2
  11. package/src/admin/content-api.ts +10 -3
  12. package/src/admin/content-item.ts +1 -1
  13. package/src/admin/editor/A11yCopilot.tsx +104 -56
  14. package/src/admin/editor/CommandPalette.tsx +29 -13
  15. package/src/admin/editor/Editor.tsx +88 -20
  16. package/src/admin/editor/EditorToasts.tsx +26 -0
  17. package/src/admin/editor/PerfMeter.tsx +97 -72
  18. package/src/admin/editor/ShareLink.tsx +107 -72
  19. package/src/admin/editor/ShortcutsDialog.tsx +27 -16
  20. package/src/admin/editor/Timeline.tsx +255 -160
  21. package/src/admin/editor/TopBar.tsx +71 -14
  22. package/src/admin/editor/canvas/CanvasFrame.tsx +10 -1
  23. package/src/admin/editor/canvas/CanvasTextEdit.tsx +8 -2
  24. package/src/admin/editor/canvas/drop-line.ts +11 -3
  25. package/src/admin/editor/canvas/frame-focus-ring.ts +37 -0
  26. package/src/admin/editor/canvas/use-canvas-dnd.ts +32 -3
  27. package/src/admin/editor/canvas/vnode-react.tsx +7 -0
  28. package/src/admin/editor/collection-previews.ts +16 -1
  29. package/src/admin/editor/dialogs.tsx +38 -14
  30. package/src/admin/editor/dnd/drop-target.ts +26 -1
  31. package/src/admin/editor/edited-noun.ts +15 -0
  32. package/src/admin/editor/editor-chrome.tsx +108 -7
  33. package/src/admin/editor/editor-commands.ts +15 -1
  34. package/src/admin/editor/editor-css.ts +75 -22
  35. package/src/admin/editor/element-chrome.ts +21 -11
  36. package/src/admin/editor/feature-css.ts +45 -13
  37. package/src/admin/editor/icons/IconLibraryDialog.tsx +52 -11
  38. package/src/admin/editor/icons/library.ts +1 -1
  39. package/src/admin/editor/icons/use-icon-library.ts +5 -2
  40. package/src/admin/editor/ime.ts +29 -0
  41. package/src/admin/editor/palette-items.ts +10 -3
  42. package/src/admin/editor/panels/AddPanel.tsx +35 -10
  43. package/src/admin/editor/panels/ClassesSection.tsx +18 -7
  44. package/src/admin/editor/panels/ColorControl.tsx +135 -27
  45. package/src/admin/editor/panels/ConditionsEditor.tsx +52 -26
  46. package/src/admin/editor/panels/ElementPanel.tsx +134 -24
  47. package/src/admin/editor/panels/LayersPanel.tsx +68 -22
  48. package/src/admin/editor/panels/PageSettings.tsx +66 -35
  49. package/src/admin/editor/panels/SiteStylesDrawer.tsx +170 -32
  50. package/src/admin/editor/panels/TokenScales.tsx +76 -36
  51. package/src/admin/editor/panels/TriggerSettings.tsx +50 -12
  52. package/src/admin/editor/panels/TriggersEditor.tsx +2 -1
  53. package/src/admin/editor/panels/VariableSection.tsx +53 -21
  54. package/src/admin/editor/panels/color-space.ts +9 -3
  55. package/src/admin/editor/panels/settings/AdvancedFields.tsx +12 -2
  56. package/src/admin/editor/panels/settings/AudienceEditor.tsx +51 -35
  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 +38 -15
  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 +12 -8
  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/MediaPicker.tsx +274 -20
  75. package/src/admin/editor/panels/settings/MotionControl.tsx +28 -15
  76. package/src/admin/editor/panels/settings/NumberRow.tsx +9 -3
  77. package/src/admin/editor/panels/settings/PanelSelect.tsx +50 -0
  78. package/src/admin/editor/panels/settings/QueryStyleRows.tsx +25 -18
  79. package/src/admin/editor/panels/settings/RequiredFieldsNotice.tsx +141 -0
  80. package/src/admin/editor/panels/settings/StyleRow.tsx +10 -3
  81. package/src/admin/editor/panels/settings/StyleSources.tsx +4 -1
  82. package/src/admin/editor/panels/settings/VariantEditor.tsx +10 -1
  83. package/src/admin/editor/panels/settings/box-link-name.ts +6 -2
  84. package/src/admin/editor/panels/settings/class-suggest.ts +7 -7
  85. package/src/admin/editor/panels/settings/query-style.ts +38 -9
  86. package/src/admin/editor/panels/settings/svg-images.ts +13 -0
  87. package/src/admin/editor/panels/site-list.tsx +20 -6
  88. package/src/admin/editor/panels/variable-values.ts +25 -4
  89. package/src/admin/editor/restore-deleted.ts +21 -2
  90. package/src/admin/editor/section-templates.ts +81 -12
  91. package/src/admin/editor/shortcuts.ts +17 -2
  92. package/src/admin/editor/store.ts +14 -3
  93. package/src/admin/editor/useEditorCommands.ts +61 -15
  94. package/src/admin/editor/useEditorData.ts +4 -2
  95. package/src/admin/editor/useEditorShortcuts.ts +17 -7
  96. package/src/admin/editor/useNodeActions.ts +61 -6
  97. package/src/admin/editor/useSave.ts +22 -7
  98. package/src/admin/forms-api.ts +2 -0
  99. package/src/admin/image-editor/BlurFocus.tsx +112 -0
  100. package/src/admin/image-editor/Canvas.tsx +261 -0
  101. package/src/admin/image-editor/CropOverlay.tsx +390 -0
  102. package/src/admin/image-editor/CurvesPanel.tsx +395 -0
  103. package/src/admin/image-editor/ExportDialog.tsx +384 -0
  104. package/src/admin/image-editor/ImageEditButton.tsx +83 -0
  105. package/src/admin/image-editor/ImageEditorDialog.tsx +828 -0
  106. package/src/admin/image-editor/MetadataPanel.tsx +194 -0
  107. package/src/admin/image-editor/announce.tsx +65 -0
  108. package/src/admin/image-editor/crop-geometry.ts +162 -0
  109. package/src/admin/image-editor/curve-math.ts +177 -0
  110. package/src/admin/image-editor/data-url-fetch.ts +59 -0
  111. package/src/admin/image-editor/engine.ts +55 -0
  112. package/src/admin/image-editor/exif.ts +82 -0
  113. package/src/admin/image-editor/flatten.ts +54 -0
  114. package/src/admin/image-editor/gpu-fit.ts +81 -0
  115. package/src/admin/image-editor/image-editor-css.ts +400 -0
  116. package/src/admin/image-editor/load.ts +116 -0
  117. package/src/admin/image-editor/panels.tsx +287 -0
  118. package/src/admin/image-editor/parts.tsx +614 -0
  119. package/src/admin/image-editor/save.ts +141 -0
  120. package/src/admin/image-editor/tone-panels.tsx +338 -0
  121. package/src/admin/image-editor/tools.ts +59 -0
  122. package/src/admin/image-editor/worker.ts +111 -0
  123. package/src/admin/media-api.ts +114 -3
  124. package/src/admin/pages/AgentsPage.tsx +138 -27
  125. package/src/admin/pages/CreateDialog.tsx +97 -30
  126. package/src/admin/pages/NewPageDialog.tsx +21 -3
  127. package/src/admin/pages/NewThemePartDialog.tsx +11 -3
  128. package/src/admin/pages/PageList.tsx +14 -6
  129. package/src/admin/pages/SetupShell.tsx +28 -12
  130. package/src/admin/pages/ThemePartList.tsx +27 -7
  131. package/src/admin/pages/list-kit.tsx +106 -24
  132. package/src/admin/search.ts +15 -0
  133. package/src/admin/setup/useSetupStatus.ts +1 -1
  134. package/src/admin/theme-api.ts +13 -3
  135. package/src/admin/ui.ts +78 -22
  136. package/src/astro/EmVBPage.astro +11 -2
  137. package/src/astro/floats/runtime.ts +21 -0
  138. package/src/astro/menu/runtime.ts +34 -1
  139. package/src/astro/popups/runtime.ts +33 -3
  140. package/src/astro/render.ts +6 -0
  141. package/src/astro/resolve-theme.ts +67 -14
  142. package/src/astro/resolve.ts +9 -1
  143. package/src/astro/tabs/runtime.ts +12 -0
  144. package/src/astro/theme-posts.ts +2 -2
  145. package/src/astro/visitor.ts +6 -3
  146. package/src/constants.ts +1 -1
  147. package/src/core/a11y/audit.ts +171 -24
  148. package/src/core/arrange.ts +29 -6
  149. package/src/core/audience/rules.ts +5 -1
  150. package/src/core/audience/variants.ts +29 -3
  151. package/src/core/clipboard.ts +7 -0
  152. package/src/core/css/generate.ts +31 -6
  153. package/src/core/data/bindings.ts +19 -6
  154. package/src/core/design/classes.ts +20 -9
  155. package/src/core/design/diff.ts +9 -1
  156. package/src/core/design/merge.ts +93 -3
  157. package/src/core/design/tokens.ts +16 -2
  158. package/src/core/design/transfer.ts +46 -8
  159. package/src/core/design/variables.ts +32 -10
  160. package/src/core/elements/dynamic-elements.ts +53 -6
  161. package/src/core/elements/form-elements.ts +68 -30
  162. package/src/core/elements/index.ts +83 -38
  163. package/src/core/elements/menu.ts +37 -19
  164. package/src/core/elements/plain-text.ts +2 -2
  165. package/src/core/elements/select-prompt.ts +3 -0
  166. package/src/core/elements/svg-ids.ts +39 -0
  167. package/src/core/forms/binding.ts +190 -2
  168. package/src/core/forms/definition.ts +72 -0
  169. package/src/core/history/diff.ts +49 -4
  170. package/src/core/icons/catalog.ts +3 -2
  171. package/src/core/image-edit/schema.ts +155 -0
  172. package/src/core/image-edit/summary.ts +61 -0
  173. package/src/core/index.ts +29 -1
  174. package/src/core/limits.ts +21 -0
  175. package/src/core/migrate/index.ts +3 -1
  176. package/src/core/perf/meter.ts +51 -8
  177. package/src/core/recipes/index.ts +5 -4
  178. package/src/core/render/context.ts +8 -0
  179. package/src/core/render/dynamic-post.ts +71 -13
  180. package/src/core/render/extras.ts +77 -4
  181. package/src/core/render/form.ts +164 -12
  182. package/src/core/render/index.ts +179 -22
  183. package/src/core/render/loop.ts +43 -7
  184. package/src/core/render/pagination.ts +18 -4
  185. package/src/core/render/section.ts +27 -0
  186. package/src/core/render/tabs.ts +12 -3
  187. package/src/core/render/vnode.ts +1 -1
  188. package/src/core/sanitize/css.ts +15 -2
  189. package/src/core/sanitize/embed-url.ts +42 -5
  190. package/src/core/sanitize/url.ts +2 -1
  191. package/src/core/schema/layout.ts +26 -7
  192. package/src/core/share/link.ts +5 -2
  193. package/src/core/stable-json.ts +15 -0
  194. package/src/core/stats.ts +2 -1
  195. package/src/core/theme/conditions.ts +3 -1
  196. package/src/core/theme/dynamic.ts +57 -23
  197. package/src/core/theme/float.ts +9 -2
  198. package/src/core/theme/pagination.ts +57 -10
  199. package/src/core/theme/popup-markup.ts +23 -5
  200. package/src/core/theme/popup-rules.ts +11 -5
  201. package/src/core/theme/triggers.ts +3 -2
  202. package/src/core/tree-ops.ts +27 -0
  203. package/src/mcp/ports.ts +8 -1
  204. package/src/mcp/rate-limit.ts +2 -1
  205. package/src/mcp/read.ts +82 -3
  206. package/src/mcp/refs.ts +18 -0
  207. package/src/mcp/tools.ts +1 -1
  208. package/src/mcp/write.ts +271 -25
  209. package/src/server/agent-settings.ts +29 -4
  210. package/src/server/hooks.ts +5 -3
  211. package/src/server/icon-routes.ts +33 -9
  212. package/src/server/mcp-routes.ts +74 -11
@@ -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);
@@ -296,7 +394,7 @@ function withFieldMeta(vnode: VNode, node: LayoutNode, meta: PublicFormField): V
296
394
  },
297
395
  };
298
396
  }
299
- if (!meta.required || child.tag !== "label") return child;
397
+ if (!meta.required || (child.tag !== "label" && child.tag !== "legend")) return child;
300
398
  if (child.attrs.class === "emvb-form-label") {
301
399
  return { ...child, children: [...child.children, mark] };
302
400
  }
@@ -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,13 +430,53 @@ 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
+
465
+ /** The fieldset that holds a radio group's options. */
466
+ const radioGroup = (child: VNode) =>
467
+ child.tag === "fieldset" && (child.attrs.class ?? "").includes("emvb-radio-group");
468
+
335
469
  /** A select or radio field filled with the definition's options. */
336
470
  function withOptions(vnode: VNode, node: LayoutNode, meta: PublicFormField): VNode {
337
471
  const field = meta.name;
338
472
  if ((node.type !== "select" && node.type !== "radio") || !meta.options?.length) return vnode;
339
473
  if (node.type === "select") {
340
474
  return replaceChildren(vnode, (child) => child.tag === "select", [
341
- { tag: "option", attrs: { value: "" }, children: ["Choose…"] },
475
+ {
476
+ tag: "option",
477
+ attrs: { value: "" },
478
+ children: [selectPrompt(node.props as { placeholder?: string })],
479
+ },
342
480
  ...meta.options.map((option): VNode => ({
343
481
  tag: "option",
344
482
  attrs: { value: option.value },
@@ -346,10 +484,13 @@ function withOptions(vnode: VNode, node: LayoutNode, meta: PublicFormField): VNo
346
484
  })),
347
485
  ]);
348
486
  }
349
- return replaceChildren(
350
- vnode,
351
- (child) => child.tag === "fieldset" && (child.attrs.class ?? "").includes("emvb-radio-group"),
352
- meta.options.map((option) => ({
487
+ // W-524: the group's <legend> stays; only the placeholder option is replaced.
488
+ const legend = findNode(vnode, radioGroup)?.children.filter(
489
+ (child): child is VNode => typeof child !== "string" && child.tag === "legend",
490
+ );
491
+ return replaceChildren(vnode, radioGroup, [
492
+ ...(legend ?? []),
493
+ ...meta.options.map((option): VNode => ({
353
494
  tag: "label",
354
495
  attrs: { class: "emvb-form-radio-label" },
355
496
  children: [
@@ -361,5 +502,16 @@ function withOptions(vnode: VNode, node: LayoutNode, meta: PublicFormField): VNo
361
502
  ` ${option.label}`,
362
503
  ],
363
504
  })),
364
- );
505
+ ]);
506
+ }
507
+
508
+ /** The first element node `match` accepts, depth first. */
509
+ function findNode(vnode: VNode, match: (node: VNode) => boolean): VNode | undefined {
510
+ if (match(vnode)) return vnode;
511
+ for (const child of vnode.children) {
512
+ if (typeof child === "string") continue;
513
+ const found = findNode(child, match);
514
+ if (found) return found;
515
+ }
516
+ return undefined;
365
517
  }
@@ -1,4 +1,5 @@
1
- import { variantShows } from "../audience/variants.ts";
1
+ import { MAX_NODES } from "../limits.ts";
2
+ import { loneAbTests, variantShows } from "../audience/variants.ts";
2
3
  import { audienceShows, audienceSummary } from "../audience/rules.ts";
3
4
  import { generateCss } from "../css/generate.ts";
4
5
  import { printCss } from "../css/print.ts";
@@ -6,7 +7,7 @@ import { cssScopeToken, scopeClassName, scopeCss, scopeWrapperCss } from "../css
6
7
  import { ELEMENTS } from "../elements/index.ts";
7
8
  import { controlId } from "../elements/field-id.ts";
8
9
  import { layoutHasForm } from "../forms/binding.ts";
9
- import { forEachNode, someNode } from "../tree-ops.ts";
10
+ import { nodeChildren, someNode } from "../tree-ops.ts";
10
11
  import { layoutHasMenuDropdown, layoutHasTabs } from "../tabs/presence.ts";
11
12
  import type { DesignSystem } from "../schema/design.ts";
12
13
  import {
@@ -25,7 +26,6 @@ import {
25
26
  stateDeclarations,
26
27
  styleClassName,
27
28
  styleDeclarations,
28
- withInheritedAnimation,
29
29
  withInheritedLayers,
30
30
  type Declaration,
31
31
  type StateDeclarations,
@@ -190,6 +190,9 @@ function renameIds(vnode: VNode, base: string, next: string): VNode {
190
190
  };
191
191
  }
192
192
 
193
+ /** Elements synced sections may add to one render, all copies together (W-653): 20 full pages. */
194
+ const MAX_SECTION_ELEMENTS = 20 * MAX_NODES;
195
+
193
196
  export function renderPage(
194
197
  layout: Layout,
195
198
  design: DesignSystem,
@@ -201,6 +204,14 @@ export function renderPage(
201
204
  scope?: string;
202
205
  /** Editor canvas: device rules follow this device, not the frame width (W-116). */
203
206
  previewDevice?: PopupDevice;
207
+ /**
208
+ * Theme parts (header, footer, popup, float) render apart from the page, so a synced section
209
+ * on both gave form fields, honeypots and tab groups the same ids: a label in a popup focused
210
+ * the page's field. Their generated ids get a suffix from `scope` (W-527).
211
+ */
212
+ partIds?: boolean;
213
+ /** The page's language, e.g. "fr-CA": Post Dates are written in it (W-688). */
214
+ lang?: string;
204
215
  } = {},
205
216
  ): RenderResult {
206
217
  const mode = opts.mode ?? "public";
@@ -218,22 +229,40 @@ export function renderPage(
218
229
  queryMin?: number;
219
230
  }[] = [];
220
231
  const layoutHasToc = treeHasType(layout.root, "toc");
232
+ // W-799: each grid child's grid, so a span or start line that doesn't fit a device's columns
233
+ // can be clamped there.
234
+ const gridOf = new Map<string, LayoutNode>();
235
+ someNode(layout.root, (current) => {
236
+ if (current.type === "grid")
237
+ for (const child of nodeChildren(current)) gridOf.set(child.id, current);
238
+ return false;
239
+ });
240
+ const loneTests = loneAbTests(layout);
221
241
  // W-384: a table of contents lists the page's headings that actually render, with their
222
242
  // rendered text, so a hidden A/B arm or audience heading is not a dead link.
223
243
  const pageHeadings = layoutHasToc
224
- ? new Set(headingAnchors(layout.root).map((h) => h.node))
244
+ ? new Map(headingAnchors(layout.root).map((h) => [h.node, h.hiddenOn]))
225
245
  : undefined;
226
- const renderedHeadings: { id: string; text: string }[] = [];
246
+ const renderedHeadings: { id: string; text: string; tocClass?: string }[] = [];
247
+ const emittedIds = new Set<string>();
227
248
  const tocNodes: { node: LayoutNode; vnode: VNode }[] = [];
228
249
  const knownVariables = new Set(design.variables.colors.map((c) => c.id));
229
250
  let backgroundVideo = false;
230
251
 
231
252
  const domIds = new Map<string, number>();
253
+ const idSuffix = opts.partIds && opts.scope ? `-t${originHash(opts.scope)}` : "";
232
254
  // W-253: runtimes follow what renders, including synced section and loop item contents,
233
255
  // which the page's own layout doesn't hold.
234
256
  const rendered = { form: false, tabs: false, menuDropdown: false };
257
+ let sectionBudget = MAX_SECTION_ELEMENTS;
235
258
  const ctx: RenderContext = {
259
+ takeSectionBudget: (nodes) => {
260
+ if (nodes > sectionBudget) return false;
261
+ sectionBudget -= nodes;
262
+ return true;
263
+ },
236
264
  mode,
265
+ ...(opts.lang ? { lang: opts.lang } : {}),
237
266
  definitions: opts.formDefinitions ?? new Map(),
238
267
  children: (nodes, dynamic) =>
239
268
  nodes
@@ -242,7 +271,7 @@ export function renderPage(
242
271
  uniqueId: (base) => {
243
272
  const count = (domIds.get(base) ?? 0) + 1;
244
273
  domIds.set(base, count);
245
- return count === 1 ? base : `${base}-r${count}`;
274
+ return `${base}${count === 1 ? "" : `-r${count}`}${idSuffix}`;
246
275
  },
247
276
  };
248
277
 
@@ -273,12 +302,31 @@ export function renderPage(
273
302
  ...(fromClasses.entrance ? { entrance: fromClasses.entrance } : {}),
274
303
  ...(fromClasses.scrollMotion ? { scrollMotion: fromClasses.scrollMotion } : {}),
275
304
  };
305
+ // W-569: and an own image, gradient or overlay keeps the layers its classes draw, as they
306
+ // share one background-image stack and the local rule would otherwise replace them.
307
+ const classLayers = {
308
+ ...classAnimation,
309
+ ...Object.fromEntries(
310
+ (
311
+ [
312
+ "backgroundImage",
313
+ "gradient",
314
+ "overlay",
315
+ "backgroundSize",
316
+ "backgroundPosition",
317
+ "backgroundRepeat",
318
+ ] as const
319
+ )
320
+ .filter((key) => fromClasses[key] !== undefined)
321
+ .map((key) => [key, fromClasses[key]]),
322
+ ),
323
+ };
276
324
  const own = node.style
277
- ? (withInheritedAnimation(node.style, classAnimation) as typeof node.style)
325
+ ? (withInheritedLayers(node.style, classLayers) as typeof node.style)
278
326
  : undefined;
279
327
  const { declarations, rejected } = styleDeclarations(own);
280
328
  // W-210: a device that sets one background layer keeps the layers it inherits.
281
- const base = { ...classAnimation, ...own };
329
+ const base = { ...classLayers, ...own };
282
330
  const tabletStyle = node.devices?.tablet;
283
331
  const tablet = styleDeclarations(withInheritedLayers(tabletStyle, base));
284
332
  const mobile = styleDeclarations(
@@ -310,6 +358,11 @@ export function renderPage(
310
358
  }
311
359
  }
312
360
  if (node.container) declarations.push({ property: "container-type", value: "inline-size" });
361
+ // W-523: a query container can't size to its content, so in a row (or with align-items other
362
+ // than stretch) it collapsed to 0 px and its content vanished. Full width unless a width is set.
363
+ if (node.container && node.style?.width === undefined && !node.classes?.length) {
364
+ declarations.push({ property: "width", value: "100%" });
365
+ }
313
366
  if (node.type === "image") {
314
367
  const focal = node.props as { focalX?: unknown; focalY?: unknown };
315
368
  const position = objectPosition(focal.focalX, focal.focalY);
@@ -341,6 +394,26 @@ export function renderPage(
341
394
  mobile.declarations.unshift(...mobileColumns);
342
395
  }
343
396
  }
397
+ const grid = gridOf.get(node.id);
398
+ if (grid) {
399
+ const counts = gridColumnCounts(grid);
400
+ const tabletPlacement = { ...node.style, ...node.devices?.tablet };
401
+ const levels = [
402
+ node.style ?? {},
403
+ tabletPlacement,
404
+ { ...tabletPlacement, ...node.devices?.mobile },
405
+ ];
406
+ const fitted = levels.map((style, i) =>
407
+ fitGridColumn(style.gridColumnStart, style.gridColumnSpan, counts[i] ?? 1),
408
+ );
409
+ if (fitted.some((f) => f.changed)) {
410
+ const at = (i: number) => ({ property: "grid-column", value: fitted[i]?.value ?? "auto" });
411
+ const kept = declarations.filter((d) => d.property !== "grid-column");
412
+ declarations.splice(0, declarations.length, ...kept, at(0));
413
+ tablet.declarations.push(at(1));
414
+ mobile.declarations.push(at(2));
415
+ }
416
+ }
344
417
  const states = stateDeclarations(node.states);
345
418
  for (const key of states.rejected) {
346
419
  warnings.push({ nodeId, code: "rejected-style", detail: key });
@@ -350,7 +423,8 @@ export function renderPage(
350
423
  warnings.push({ nodeId, code: "unknown-variable", detail: color.var });
351
424
  }
352
425
  }
353
- const hiddenOn = node.hiddenOn;
426
+ // W-567: the root is the page itself; hiding it on a device blanked the whole page there.
427
+ const hiddenOn = isRoot ? undefined : node.hiddenOn;
354
428
  const responsive =
355
429
  tablet.declarations.length > 0 ||
356
430
  mobile.declarations.length > 0 ||
@@ -383,8 +457,18 @@ export function renderPage(
383
457
  }
384
458
  const attrs: Record<string, string> = { class: classes.join(" "), ...customAttributes(node) };
385
459
  if (mode === "editor" && id) attrs["data-emvb-id"] = id;
386
- if (node.htmlId && HTML_ID.test(node.htmlId)) attrs.id = node.htmlId;
387
- else if (node.type === "heading" && id && layoutHasToc) attrs.id = `emvb-h-${id}`;
460
+ const htmlId =
461
+ node.htmlId && HTML_ID.test(node.htmlId)
462
+ ? node.htmlId
463
+ : node.type === "heading" && id && layoutHasToc
464
+ ? `emvb-h-${id}`
465
+ : undefined;
466
+ // W-649: a Loop renders its item design once per entry; an id repeated on the page is invalid
467
+ // and every Table of contents link or label would point at the first copy only.
468
+ if (htmlId && !emittedIds.has(htmlId)) {
469
+ emittedIds.add(htmlId);
470
+ attrs.id = htmlId;
471
+ }
388
472
  // W-230: the site's text direction on the root, also a fallback when the host's <html> has none.
389
473
  if (node.dir === "ltr" || node.dir === "rtl" || node.dir === "auto") attrs.dir = node.dir;
390
474
  else if (isRoot && design.direction) attrs.dir = design.direction;
@@ -399,7 +483,7 @@ export function renderPage(
399
483
  // W-307: bound fields read their live value; the typed value is the fallback.
400
484
  const node = stored.bind ? applyBindings(stored, bindingData(dynamic)) : stored;
401
485
  // W-312: only the visitor's arm of an A/B test renders; the editor shows every arm.
402
- if (mode === "public" && !variantShows(stored, dynamic?.abArms)) return undefined;
486
+ if (mode === "public" && !variantShows(stored, dynamic?.abArms, loneTests)) return undefined;
403
487
  // W-313: visitor-aware elements render only for their audience; the editor shows them all.
404
488
  if (mode === "public" && !audienceShows(stored, dynamic?.visitor)) return undefined;
405
489
  const id = ID.test(node.id) ? node.id : undefined;
@@ -443,6 +527,7 @@ export function renderPage(
443
527
  resolvePostForRender(dynamic, mode),
444
528
  mode,
445
529
  dynamic?.inLoopItem === true,
530
+ ctx.lang,
446
531
  ),
447
532
  );
448
533
  }
@@ -460,7 +545,18 @@ export function renderPage(
460
545
  node.hiddenOn?.includes(device),
461
546
  );
462
547
  if (text && !hiddenEverywhere && !renderedHeadings.some((h) => h.id === attrs.id)) {
463
- renderedHeadings.push({ id: attrs.id, text });
548
+ // W-793: a heading hidden on some devices (itself or an ancestor) hides its contents
549
+ // entry there too, or the entry was a dead link on those devices.
550
+ const hiddenOn = pageHeadings.get(stored) ?? [];
551
+ const tocId = `toc-${node.id}`;
552
+ if (hiddenOn.length > 0) {
553
+ localRules.push({ id: tocId, declarations: [], states: {}, hiddenOn: [...hiddenOn] });
554
+ }
555
+ renderedHeadings.push({
556
+ id: attrs.id,
557
+ text,
558
+ ...(hiddenOn.length > 0 ? { tocClass: `emvb-e-${tocId}` } : {}),
559
+ });
464
560
  }
465
561
  }
466
562
  if (isParentNode(node)) {
@@ -557,18 +653,79 @@ function treeHasType(node: LayoutNode, type: string): boolean {
557
653
  return someNode(node, (current) => current.type === type);
558
654
  }
559
655
 
560
- /** The page's own heading nodes (not synced section or loop item contents). */
561
- function headingAnchors(root: LayoutNode): { node: LayoutNode }[] {
562
- const out: { node: LayoutNode }[] = [];
563
- forEachNode(root, (node) => {
564
- if (node.type === "heading") out.push({ node });
565
- });
656
+ /**
657
+ * The page's own heading nodes (not synced section or loop item contents). W-512: not those
658
+ * inside an element hidden on every device, which never show, so a contents link has nothing
659
+ * to jump to (W-409 covered only the heading's own visibility).
660
+ */
661
+ function headingAnchors(root: LayoutNode): { node: LayoutNode; hiddenOn: PopupDevice[] }[] {
662
+ const out: { node: LayoutNode; hiddenOn: PopupDevice[] }[] = [];
663
+ const walk = (node: LayoutNode, inherited: PopupDevice[]) => {
664
+ if (node !== root && HIDDEN_DEVICES.every((device) => node.hiddenOn?.includes(device))) return;
665
+ const own = node === root ? [] : (node.hiddenOn ?? []);
666
+ const hiddenOn = HIDDEN_DEVICES.filter((d) => inherited.includes(d) || own.includes(d));
667
+ if (hiddenOn.length === HIDDEN_DEVICES.length) return;
668
+ if (node.type === "heading") out.push({ node, hiddenOn });
669
+ for (const child of nodeChildren(node)) walk(child, hiddenOn);
670
+ };
671
+ walk(root, []);
566
672
  return out;
567
673
  }
568
674
 
675
+ const HIDDEN_DEVICES = ["desktop", "tablet", "mobile"] as const;
676
+
677
+ /** Columns set by custom tracks or a count, or undefined when neither is set. */
678
+ const columnCount = (tracks: unknown, columns: unknown): number | undefined =>
679
+ Array.isArray(tracks) && tracks.length > 0
680
+ ? tracks.length
681
+ : Number.isInteger(columns) && (columns as number) >= 1
682
+ ? (columns as number)
683
+ : undefined;
684
+
685
+ /** A grid's column count on desktop, tablet and mobile; unset follows the next wider device. */
686
+ function gridColumnCounts(grid: LayoutNode): [number, number, number] {
687
+ const props = grid.props as Record<string, unknown>;
688
+ const desktop = columnCount(props["columnTracks"], props["columns"]) ?? 1;
689
+ const tablet = columnCount(props["columnTracksTablet"], props["columnsTablet"]) ?? desktop;
690
+ const mobile = columnCount(props["columnTracksMobile"], props["columnsMobile"]) ?? tablet;
691
+ return [desktop, tablet, mobile];
692
+ }
693
+
694
+ /**
695
+ * W-799: a span wider than the grid, or a start line past it, made the browser add columns, so on
696
+ * a phone grid of 1 column a "span 3" child squeezed its siblings to a few pixels. The span is
697
+ * capped at the count and a start line that no longer fits is dropped.
698
+ */
699
+ function fitGridColumn(
700
+ start: unknown,
701
+ span: unknown,
702
+ count: number,
703
+ ): { value?: string; changed: boolean } {
704
+ const across =
705
+ Number.isInteger(span) && (span as number) >= 1 && (span as number) <= 12
706
+ ? (span as number)
707
+ : undefined;
708
+ const line =
709
+ Number.isInteger(start) && (start as number) >= 1 && (start as number) <= 13
710
+ ? (start as number)
711
+ : undefined;
712
+ const width = Math.min(across ?? 1, count);
713
+ const fits = line !== undefined && line + width - 1 <= count ? line : undefined;
714
+ const changed = width !== (across ?? 1) || fits !== line;
715
+ const value =
716
+ fits !== undefined
717
+ ? across !== undefined
718
+ ? `${fits} / span ${width}`
719
+ : String(fits)
720
+ : across !== undefined
721
+ ? `span ${width}`
722
+ : undefined;
723
+ return { value, changed };
724
+ }
725
+
569
726
  function tocChildren(
570
727
  node: LayoutNode,
571
- items: { id: string; text: string }[],
728
+ items: { id: string; text: string; tocClass?: string }[],
572
729
  mode: RenderMode,
573
730
  ): (VNode | string)[] {
574
731
  const title =
@@ -590,9 +747,9 @@ function tocChildren(
590
747
  children.push({
591
748
  tag: "ol",
592
749
  attrs: {},
593
- children: items.map((item) => ({
750
+ children: items.map((item): VNode => ({
594
751
  tag: "li",
595
- attrs: {},
752
+ attrs: item.tocClass ? { class: item.tocClass } : {},
596
753
  children: [{ tag: "a", attrs: { href: `#${item.id}` }, children: [item.text] }],
597
754
  })),
598
755
  });
@@ -41,6 +41,21 @@ function entriesFor(
41
41
  * state child (W-308) is left out of the item design and shows when there are no entries; the
42
42
  * editor shows it under the items too, so it can be designed.
43
43
  */
44
+ /** Loops inside loop items, at most this deep (W-654). */
45
+ const MAX_LOOP_DEPTH = 4;
46
+
47
+ function countNodes(nodes: readonly LayoutNode[]): number {
48
+ let count = 0;
49
+ const stack: LayoutNode[] = [...nodes];
50
+ while (stack.length > 0) {
51
+ const node = stack.pop() as LayoutNode;
52
+ count++;
53
+ const kids = (node as { children?: LayoutNode[] }).children;
54
+ if (Array.isArray(kids)) for (const child of kids) stack.push(child);
55
+ }
56
+ return count;
57
+ }
58
+
44
59
  export function renderLoop(
45
60
  node: LoopNode,
46
61
  attrs: Record<string, string>,
@@ -49,6 +64,14 @@ export function renderLoop(
49
64
  ): VNode | undefined {
50
65
  const posts = entriesFor(node, dynamic, ctx.mode);
51
66
  const itemPartId = node.props.itemPartId?.trim() ?? "";
67
+ const loopStack = dynamic?.loopStack ?? [];
68
+ // W-654: a loop item part holding a Loop that uses the same part (or one that leads back to it)
69
+ // recursed until the stack overflowed and the page failed to render.
70
+ const cycle = itemPartId !== "" && loopStack.includes(itemPartId);
71
+ if (cycle || loopStack.length >= MAX_LOOP_DEPTH) {
72
+ if (ctx.mode === "public") return undefined;
73
+ return { tag: "div", attrs: { ...attrs, "data-emvb-loop-empty": "" }, children: ["Loop"] };
74
+ }
52
75
  const templateLayout =
53
76
  itemPartId && dynamic?.loopTemplates ? dynamic.loopTemplates[itemPartId] : undefined;
54
77
  const own = node.children.filter((child) => child.type !== "loop-empty");
@@ -62,7 +85,9 @@ export function renderLoop(
62
85
  if (designed.length > 0) return ELEMENTS.loop.build(node as never, attrs, designed);
63
86
  }
64
87
  // Editor mode always has a sample post, so an empty item template would paint a zero-height box.
65
- if (posts.length === 0 || (ctx.mode === "editor" && templateNodes.length === 0)) {
88
+ // W-602: on the page, no item design means nothing to list, not a row of blank items (empty
89
+ // bordered boxes with Cards).
90
+ if (posts.length === 0 || templateNodes.length === 0) {
66
91
  if (ctx.mode !== "editor") return undefined;
67
92
  return {
68
93
  tag: "div",
@@ -75,16 +100,27 @@ export function renderLoop(
75
100
  }
76
101
  const children: VNode[] = [];
77
102
  // A loop item part is another layout, so its ids may match the page's (W-250).
78
- const itemDynamic = templateLayout
79
- ? { ...rest, idOrigin: `${rest.idOrigin ?? ""}/l:${itemPartId}` }
80
- : rest;
103
+ const itemDynamic = {
104
+ ...(templateLayout ? { ...rest, idOrigin: `${rest.idOrigin ?? ""}/l:${itemPartId}` } : rest),
105
+ loopStack: [...loopStack, itemPartId || node.id],
106
+ };
107
+ const itemSize = countNodes(templateNodes);
81
108
  for (const post of posts) {
109
+ // W-654: nested Loops multiply like nested sections; they share the render's budget.
110
+ if (ctx.takeSectionBudget && !ctx.takeSectionBudget(itemSize)) break;
82
111
  children.push({
83
112
  tag: "div",
84
- attrs: { class: "emvb-loop-item", "data-emvb-loop-item": post.id },
113
+ attrs: { class: "emvb-loop-item", "data-emvb-loop-item": post.id, role: "listitem" },
85
114
  children: ctx.children(templateNodes, { ...itemDynamic, post, inLoopItem: true }),
86
115
  });
87
116
  }
88
- if (ctx.mode === "editor") children.push(...empty());
89
- return ELEMENTS.loop.build(node as never, attrs, children);
117
+ // W-789: entries were bare divs, so screen readers didn't hear "list, 4 items" and a repeated
118
+ // "Read more" had no list item to take its purpose from (WCAG 1.3.1, 2.4.4). The editor adds the
119
+ // Empty state under the items; a list may hold only its items, so the roles go only then.
120
+ const extras = ctx.mode === "editor" ? empty() : [];
121
+ if (extras.length > 0) {
122
+ for (const item of children) delete item.attrs.role;
123
+ return ELEMENTS.loop.build(node as never, attrs, [...children, ...extras]);
124
+ }
125
+ return ELEMENTS.loop.build(node as never, { ...attrs, role: "list" }, children);
90
126
  }