@mapomodule/form 2.0.0-beta.14 → 2.0.0-beta.16

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 (57) hide show
  1. package/dist/__tests__/MapoFormGroup.component.test.d.ts +2 -0
  2. package/dist/__tests__/MapoFormGroup.component.test.d.ts.map +1 -0
  3. package/dist/__tests__/MapoWygEditor.component.test.d.ts +2 -0
  4. package/dist/__tests__/MapoWygEditor.component.test.d.ts.map +1 -0
  5. package/dist/__tests__/fieldVisibility.component.test.d.ts +2 -0
  6. package/dist/__tests__/fieldVisibility.component.test.d.ts.map +1 -0
  7. package/dist/__tests__/formatHtml.test.d.ts +2 -0
  8. package/dist/__tests__/formatHtml.test.d.ts.map +1 -0
  9. package/dist/__tests__/sanitizeHtml.test.d.ts +2 -0
  10. package/dist/__tests__/sanitizeHtml.test.d.ts.map +1 -0
  11. package/dist/index.d.mts +1 -1
  12. package/dist/index.d.ts +1 -1
  13. package/dist/index.d.ts.map +1 -1
  14. package/dist/module.json +1 -1
  15. package/dist/runtime/components/MapoFormField.vue +5 -10
  16. package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
  17. package/dist/runtime/components/MapoFormFlatSection.vue +21 -3
  18. package/dist/runtime/components/MapoFormFlatSection.vue.d.ts +1 -0
  19. package/dist/runtime/components/MapoFormFlatSection.vue.d.ts.map +1 -1
  20. package/dist/runtime/components/MapoFormGroup.vue +69 -34
  21. package/dist/runtime/components/MapoFormGroup.vue.d.ts +19 -4
  22. package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
  23. package/dist/runtime/components/fields/MapoRepeaterItem.vue +18 -2
  24. package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
  25. package/dist/runtime/components/fields/MapoWygEditor.vue +157 -50
  26. package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
  27. package/dist/runtime/types/descriptor.d.ts +65 -2
  28. package/dist/runtime/types/descriptor.d.ts.map +1 -1
  29. package/dist/runtime/types/index.d.ts +1 -1
  30. package/dist/runtime/types/index.d.ts.map +1 -1
  31. package/dist/runtime/utils/formatHtml.d.ts +16 -0
  32. package/dist/runtime/utils/formatHtml.d.ts.map +1 -0
  33. package/dist/runtime/utils/formatHtml.js +137 -0
  34. package/dist/runtime/utils/sanitizeHtml.d.ts +16 -0
  35. package/dist/runtime/utils/sanitizeHtml.d.ts.map +1 -0
  36. package/dist/runtime/utils/sanitizeHtml.js +37 -0
  37. package/dist/runtime/utils/tabVisibility.d.ts +8 -2
  38. package/dist/runtime/utils/tabVisibility.d.ts.map +1 -1
  39. package/dist/runtime/utils/tabVisibility.js +20 -2
  40. package/package.json +2 -2
  41. package/src/__tests__/MapoFormGroup.component.test.ts +130 -0
  42. package/src/__tests__/MapoWygEditor.component.test.ts +525 -0
  43. package/src/__tests__/fieldVisibility.component.test.ts +546 -0
  44. package/src/__tests__/formatHtml.test.ts +145 -0
  45. package/src/__tests__/sanitizeHtml.test.ts +40 -0
  46. package/src/__tests__/tabVisibility.test.ts +27 -0
  47. package/src/index.ts +2 -0
  48. package/src/runtime/components/MapoFormField.vue +7 -13
  49. package/src/runtime/components/MapoFormFlatSection.vue +38 -5
  50. package/src/runtime/components/MapoFormGroup.vue +116 -43
  51. package/src/runtime/components/fields/MapoRepeaterItem.vue +32 -2
  52. package/src/runtime/components/fields/MapoWygEditor.vue +199 -69
  53. package/src/runtime/types/descriptor.ts +73 -3
  54. package/src/runtime/types/index.ts +2 -0
  55. package/src/runtime/utils/formatHtml.ts +198 -0
  56. package/src/runtime/utils/sanitizeHtml.ts +60 -0
  57. package/src/runtime/utils/tabVisibility.ts +42 -4
@@ -0,0 +1,546 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { createRenderer, h, nextTick, ref } from "vue";
3
+ import type { App, Component, Ref } from "vue";
4
+
5
+ import MapoFormFlatSection from "../runtime/components/MapoFormFlatSection.vue";
6
+ import MapoFormGroup from "../runtime/components/MapoFormGroup.vue";
7
+ import MapoRepeaterItem from "../runtime/components/fields/MapoRepeaterItem.vue";
8
+ import type { AnyFieldDescriptor } from "../runtime/types/index.js";
9
+
10
+ // A field hidden by `visible` or `show` takes its grid cell with it, and a flat
11
+ // section, a group, a sub-tab or a repeater item body with no field to show goes
12
+ // as well.
13
+
14
+ const form = vi.hoisted(() => ({
15
+ context: undefined as
16
+ | { model: Ref<Record<string, unknown>>; currentLang: Ref<string> }
17
+ | undefined,
18
+ }));
19
+
20
+ // The keys of the fields mounted so far, once per mount, and of the ones
21
+ // mounted now: a field that `show` hides stays mounted.
22
+ const fieldMounts = vi.hoisted(() => [] as string[]);
23
+ const mountedFields = vi.hoisted(() => new Set<string>());
24
+
25
+ vi.mock("#app", () => ({
26
+ useRuntimeConfig: () => ({
27
+ public: { mapoForm: { groups: { expanded: true } } },
28
+ }),
29
+ }));
30
+ vi.mock("../runtime/composables/useMapoForm.js", async () => {
31
+ const { ref } = await import("vue");
32
+ return {
33
+ injectMapoForm: () => form.context,
34
+ // A repeater item builds a form of its own; its fields are stubs here.
35
+ useMapoForm: () => ({
36
+ readonly: ref(false),
37
+ ctx: { submitted: ref(false) },
38
+ }),
39
+ };
40
+ });
41
+ vi.mock("../runtime/composables/useFocusMode.js", () => ({
42
+ useFocusMode: () => ({ enter: () => {} }),
43
+ }));
44
+ vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
45
+ vi.mock("../runtime/components/MapoFormField.vue", async () => {
46
+ const { h, onUnmounted } = await import("vue");
47
+ return {
48
+ default: {
49
+ name: "MapoFormField",
50
+ props: { descriptor: { type: Object, required: true } },
51
+ setup(props: { descriptor: { key: string } }) {
52
+ const key = props.descriptor.key;
53
+ fieldMounts.push(key);
54
+ mountedFields.add(key);
55
+ onUnmounted(() => mountedFields.delete(key));
56
+ return () => h("span", { "data-field": key });
57
+ },
58
+ },
59
+ };
60
+ });
61
+
62
+ // Tests run in Node without a DOM. A renderer whose nodes are plain objects
63
+ // keeps each element's tag, props and text; `style` takes what `v-show` writes,
64
+ // and `querySelector` gives the field-expand `Teleport` a target.
65
+ interface HeadlessNode {
66
+ type: string;
67
+ text: string;
68
+ parent: HeadlessNode | null;
69
+ children: HeadlessNode[];
70
+ props: Record<string, unknown>;
71
+ style: Record<string, string>;
72
+ }
73
+
74
+ const headlessNode = (type: string, text = ""): HeadlessNode => ({
75
+ type,
76
+ text,
77
+ parent: null,
78
+ children: [],
79
+ props: {},
80
+ style: {},
81
+ });
82
+ const body = headlessNode("body");
83
+
84
+ const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
85
+ createElement: (type) => headlessNode(type),
86
+ createText: (text) => headlessNode("#text", text),
87
+ createComment: (text) => headlessNode("#comment", text),
88
+ setText: (node, text) => {
89
+ node.text = text;
90
+ },
91
+ setElementText: (node, text) => {
92
+ node.children = [];
93
+ node.text = text;
94
+ },
95
+ patchProp(node, key, _previous, next) {
96
+ node.props[key] = next;
97
+ },
98
+ insert(child, parent, anchor) {
99
+ child.parent = parent;
100
+ const at = anchor ? parent.children.indexOf(anchor) : -1;
101
+ parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
102
+ },
103
+ remove(child) {
104
+ child.parent?.children.splice(child.parent.children.indexOf(child), 1);
105
+ child.parent = null;
106
+ },
107
+ parentNode: (node) => node.parent,
108
+ nextSibling: (node) =>
109
+ node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
110
+ querySelector: () => body,
111
+ });
112
+
113
+ // Nuxt UI components the layouts use, rendered as nothing.
114
+ const nuxtUiStub: Component = { render: () => null };
115
+ const nuxtUiComponents = ["UIcon", "UBadge", "UButton", "UCheckbox", "UInput"];
116
+
117
+ const all = (node: HeadlessNode): HeadlessNode[] => [
118
+ node,
119
+ ...node.children.flatMap(all),
120
+ ];
121
+ const textOf = (node: HeadlessNode): string =>
122
+ node.text + node.children.map(textOf).join("");
123
+ const hasClass = (node: HeadlessNode, name: string) =>
124
+ String(node.props.class ?? "")
125
+ .split(" ")
126
+ .includes(name);
127
+ const isCell = (node: HeadlessNode) =>
128
+ String(node.props.class ?? "").startsWith("col-span-");
129
+
130
+ /** The key of the field a cell holds, or of the `field.<key>` slot that fills it. */
131
+ function keyInCell(cell: HeadlessNode): string | undefined {
132
+ const content = all(cell).find(
133
+ (node) => "data-field" in node.props || "data-slot" in node.props,
134
+ );
135
+ return (content?.props["data-field"] ?? content?.props["data-slot"]) as
136
+ | string
137
+ | undefined;
138
+ }
139
+
140
+ let app: App<HeadlessNode> | undefined;
141
+ let root: HeadlessNode;
142
+ let model: Ref<Record<string, unknown>>;
143
+
144
+ /** The cells of the grids, in order, by the key of what they hold. */
145
+ const cellKeys = () => all(root).filter(isCell).map(keyInCell);
146
+ const cellOf = (key: string) =>
147
+ all(root).find((node) => isCell(node) && keyInCell(node) === key);
148
+ const findByClass = (name: string) =>
149
+ all(root).find((node) => hasClass(node, name));
150
+
151
+ /** Whether a node is on screen: neither it nor an ancestor is hidden by `v-show`. */
152
+ function isDisplayed(node: HeadlessNode | undefined): boolean {
153
+ if (!node) return false;
154
+ for (let at: HeadlessNode | null = node; at; at = at.parent) {
155
+ if (at.style.display === "none") return false;
156
+ }
157
+ return true;
158
+ }
159
+
160
+ /** Mounts a component; props given as a function are read again on each render. */
161
+ function mount(
162
+ component: Component,
163
+ props: Record<string, unknown> | (() => Record<string, unknown>),
164
+ slots: Record<string, () => unknown> = {},
165
+ ) {
166
+ root = headlessNode("root");
167
+ const propsNow = typeof props === "function" ? props : () => props;
168
+ const host: Component = { render: () => h(component, propsNow(), slots) };
169
+ app = createApp(host);
170
+ for (const name of nuxtUiComponents) app.component(name, nuxtUiStub);
171
+ app.mount(root);
172
+ }
173
+
174
+ type Predicate = (ctx: { model: Record<string, unknown> }) => boolean;
175
+
176
+ // Like a page whose content fields depend on the template it uses.
177
+ const isHome: Predicate = ({ model }) => model.template === "home";
178
+ const isLanding: Predicate = ({ model }) => model.template === "landing";
179
+
180
+ const field = (
181
+ key: string,
182
+ options: { cols?: number; visible?: Predicate; show?: Predicate } = {},
183
+ ) => ({ key, type: "text", ...options }) as AnyFieldDescriptor;
184
+
185
+ beforeEach(() => {
186
+ fieldMounts.length = 0;
187
+ mountedFields.clear();
188
+ model = ref<Record<string, unknown>>({ template: "home" });
189
+ form.context = { model, currentLang: ref("") };
190
+ // The field-expand listeners touch `document`, which Node does not have.
191
+ vi.stubGlobal("document", {
192
+ addEventListener: vi.fn(),
193
+ removeEventListener: vi.fn(),
194
+ body: { style: {} },
195
+ });
196
+ });
197
+
198
+ afterEach(() => {
199
+ app?.unmount();
200
+ app = undefined;
201
+ form.context = undefined;
202
+ vi.unstubAllGlobals();
203
+ });
204
+
205
+ describe("MapoFormFlatSection cells", () => {
206
+ it("renders no cell for a field `visible` removes, so the next one takes its place", () => {
207
+ mount(MapoFormFlatSection, {
208
+ fields: [
209
+ field("cta_label", { cols: 6, visible: isLanding }),
210
+ field("hero_title", { cols: 6, visible: isHome }),
211
+ ],
212
+ });
213
+
214
+ expect(cellKeys()).toEqual(["hero_title"]);
215
+ expect([...mountedFields]).toEqual(["hero_title"]);
216
+ });
217
+
218
+ it("keeps the cell of a field `show` hides, hidden, with the field mounted", () => {
219
+ mount(MapoFormFlatSection, {
220
+ fields: [field("title"), field("cta_label", { show: isLanding })],
221
+ });
222
+
223
+ expect(cellKeys()).toEqual(["title", "cta_label"]);
224
+ expect(isDisplayed(cellOf("title"))).toBe(true);
225
+ expect(isDisplayed(cellOf("cta_label"))).toBe(false);
226
+ expect(mountedFields.has("cta_label")).toBe(true);
227
+ });
228
+
229
+ it("brings the cells back when the model changes, without mounting a hidden field again", async () => {
230
+ mount(MapoFormFlatSection, {
231
+ fields: [
232
+ field("title"),
233
+ field("cta_label", { visible: isLanding }),
234
+ field("cta_url", { show: isLanding }),
235
+ ],
236
+ });
237
+ expect(cellKeys()).toEqual(["title", "cta_url"]);
238
+
239
+ model.value.template = "landing";
240
+ await nextTick();
241
+
242
+ expect(cellKeys()).toEqual(["title", "cta_label", "cta_url"]);
243
+ expect(isDisplayed(cellOf("cta_label"))).toBe(true);
244
+ expect(isDisplayed(cellOf("cta_url"))).toBe(true);
245
+ expect(fieldMounts.filter((key) => key === "cta_url")).toHaveLength(1);
246
+
247
+ model.value.template = "home";
248
+ await nextTick();
249
+
250
+ expect(cellKeys()).toEqual(["title", "cta_url"]);
251
+ expect(isDisplayed(cellOf("cta_url"))).toBe(false);
252
+ expect(fieldMounts.filter((key) => key === "cta_url")).toHaveLength(1);
253
+ });
254
+
255
+ it("applies the field's predicates to the cell its `field.<key>` slot fills", async () => {
256
+ mount(
257
+ MapoFormFlatSection,
258
+ {
259
+ fields: [
260
+ field("title"),
261
+ field("cta_label", { visible: isLanding }),
262
+ field("cta_url", { show: isLanding }),
263
+ ],
264
+ },
265
+ {
266
+ "field.cta_label": () => h("em", { "data-slot": "cta_label" }),
267
+ "field.cta_url": () => h("em", { "data-slot": "cta_url" }),
268
+ },
269
+ );
270
+
271
+ expect(cellKeys()).toEqual(["title", "cta_url"]);
272
+ expect(isDisplayed(cellOf("cta_url"))).toBe(false);
273
+
274
+ model.value.template = "landing";
275
+ await nextTick();
276
+
277
+ expect(cellKeys()).toEqual(["title", "cta_label", "cta_url"]);
278
+ expect(isDisplayed(cellOf("cta_url"))).toBe(true);
279
+ });
280
+
281
+ it("keeps the other cells when a field's predicate throws", () => {
282
+ // Reads a nested value the model does not have yet, as on a new record.
283
+ const seoEnabled: Predicate = ({ model }) =>
284
+ (model.seo as { enabled: boolean }).enabled;
285
+ mount(MapoFormFlatSection, {
286
+ fields: [field("title"), field("seo_title", { visible: seoEnabled })],
287
+ });
288
+
289
+ expect(cellKeys()).toEqual(["title"]);
290
+ });
291
+
292
+ it("renders no grid while `visible` removes every field", () => {
293
+ mount(MapoFormFlatSection, {
294
+ fields: [field("cta_label", { visible: isLanding })],
295
+ });
296
+
297
+ expect(findByClass("grid")).toBeUndefined();
298
+ });
299
+
300
+ it("hides the grid while `show` hides every field, and shows it with one of them", async () => {
301
+ mount(MapoFormFlatSection, {
302
+ fields: [
303
+ field("cta_label", { visible: isLanding }),
304
+ field("cta_url", { show: isLanding }),
305
+ ],
306
+ });
307
+ expect(isDisplayed(findByClass("grid"))).toBe(false);
308
+ expect(mountedFields.has("cta_url")).toBe(true);
309
+
310
+ model.value.template = "landing";
311
+ await nextTick();
312
+
313
+ expect(isDisplayed(findByClass("grid"))).toBe(true);
314
+ });
315
+ });
316
+
317
+ describe("MapoFormGroup cells", () => {
318
+ it("renders the cells of the fields to show and hides the cell of one `show` hides", () => {
319
+ mount(MapoFormGroup, {
320
+ name: "content",
321
+ fields: [
322
+ field("cta_label", { cols: 6, visible: isLanding }),
323
+ field("hero_title", { cols: 6, visible: isHome }),
324
+ field("cta_url", { show: isLanding }),
325
+ ],
326
+ });
327
+
328
+ expect(cellKeys()).toEqual(["hero_title", "cta_url"]);
329
+ expect(isDisplayed(cellOf("hero_title"))).toBe(true);
330
+ expect(isDisplayed(cellOf("cta_url"))).toBe(false);
331
+ });
332
+
333
+ it("brings a cell back when the model changes", async () => {
334
+ mount(MapoFormGroup, {
335
+ name: "content",
336
+ fields: [field("hero_title"), field("cta_label", { visible: isLanding })],
337
+ });
338
+ expect(cellKeys()).toEqual(["hero_title"]);
339
+
340
+ model.value.template = "landing";
341
+ await nextTick();
342
+
343
+ expect(cellKeys()).toEqual(["hero_title", "cta_label"]);
344
+ });
345
+
346
+ it("applies the field's predicates to the cells its `field.<key>` slots fill, sub-tabs included", async () => {
347
+ mount(
348
+ MapoFormGroup,
349
+ {
350
+ name: "content",
351
+ fields: [field("title"), field("cta_label", { visible: isLanding })],
352
+ subtabs: new Map([
353
+ ["social", [field("og_title", { show: isLanding })]],
354
+ ["basic", [field("meta_title")]],
355
+ ]),
356
+ },
357
+ {
358
+ "field.cta_label": () => h("em", { "data-slot": "cta_label" }),
359
+ "field.og_title": () => h("em", { "data-slot": "og_title" }),
360
+ },
361
+ );
362
+ expect(cellKeys()).toEqual(["title", "og_title", "meta_title"]);
363
+ expect(isDisplayed(cellOf("og_title"))).toBe(false);
364
+
365
+ model.value.template = "landing";
366
+ await nextTick();
367
+
368
+ expect(cellKeys()).toEqual([
369
+ "title",
370
+ "cta_label",
371
+ "og_title",
372
+ "meta_title",
373
+ ]);
374
+ expect(isDisplayed(cellOf("cta_label"))).toBe(true);
375
+ expect(isDisplayed(cellOf("og_title"))).toBe(true);
376
+ });
377
+
378
+ it("renders no card while `visible` removes every field, and renders it once one is kept", async () => {
379
+ mount(MapoFormGroup, {
380
+ name: "call_to_action",
381
+ fields: [
382
+ field("cta_label", { visible: isLanding }),
383
+ field("cta_url", { visible: isLanding }),
384
+ ],
385
+ });
386
+ expect(findByClass("mapo-form-group")).toBeUndefined();
387
+
388
+ model.value.template = "landing";
389
+ await nextTick();
390
+
391
+ expect(isDisplayed(findByClass("mapo-form-group"))).toBe(true);
392
+ expect(cellKeys()).toEqual(["cta_label", "cta_url"]);
393
+ });
394
+
395
+ it("hides the card while `show` hides every field, keeping the fields mounted", async () => {
396
+ mount(MapoFormGroup, {
397
+ name: "call_to_action",
398
+ fields: [
399
+ field("cta_label", { visible: isLanding }),
400
+ field("cta_url", { show: isLanding }),
401
+ ],
402
+ });
403
+ expect(isDisplayed(findByClass("mapo-form-group"))).toBe(false);
404
+ expect(mountedFields.has("cta_url")).toBe(true);
405
+
406
+ model.value.template = "landing";
407
+ await nextTick();
408
+
409
+ expect(isDisplayed(findByClass("mapo-form-group"))).toBe(true);
410
+ expect(fieldMounts.filter((key) => key === "cta_url")).toHaveLength(1);
411
+ });
412
+ });
413
+
414
+ describe("MapoFormGroup sub-tabs", () => {
415
+ const seoSubtabs = () =>
416
+ new Map([
417
+ ["basic", [field("meta_title")]],
418
+ ["social", [field("og_title", { visible: isLanding })]],
419
+ ["advanced", [field("canonical", { show: isLanding })]],
420
+ ]);
421
+
422
+ const subtabButtons = () =>
423
+ findByClass("mapo-form-subtab-bar")?.children.filter(
424
+ (node) => node.type === "button",
425
+ ) ?? [];
426
+ const subtabLabels = () =>
427
+ subtabButtons().map((button) => textOf(button).trim());
428
+
429
+ async function clickSubtab(label: string) {
430
+ const button = subtabButtons().find(
431
+ (node) => textOf(node).trim() === label,
432
+ );
433
+ (button?.props.onClick as () => void)();
434
+ await nextTick();
435
+ }
436
+
437
+ it("leaves a sub-tab with no field to show out of the bar", () => {
438
+ mount(MapoFormGroup, { name: "seo", fields: [], subtabs: seoSubtabs() });
439
+
440
+ expect(subtabLabels()).toEqual(["basic"]);
441
+ // `visible` removed og_title with its panel; `show` keeps canonical mounted.
442
+ expect(cellOf("og_title")).toBeUndefined();
443
+ expect(isDisplayed(cellOf("canonical"))).toBe(false);
444
+ expect(mountedFields.has("canonical")).toBe(true);
445
+ });
446
+
447
+ it("opens the first shown sub-tab while the picked one is hidden, and the picked one again when it shows", async () => {
448
+ model.value.template = "landing";
449
+ mount(MapoFormGroup, { name: "seo", fields: [], subtabs: seoSubtabs() });
450
+ expect(subtabLabels()).toEqual(["basic", "social", "advanced"]);
451
+
452
+ await clickSubtab("advanced");
453
+ expect(isDisplayed(cellOf("canonical"))).toBe(true);
454
+ expect(isDisplayed(cellOf("meta_title"))).toBe(false);
455
+
456
+ model.value.template = "home";
457
+ await nextTick();
458
+
459
+ expect(subtabLabels()).toEqual(["basic"]);
460
+ expect(isDisplayed(cellOf("meta_title"))).toBe(true);
461
+
462
+ model.value.template = "landing";
463
+ await nextTick();
464
+
465
+ expect(isDisplayed(cellOf("canonical"))).toBe(true);
466
+ expect(isDisplayed(cellOf("meta_title"))).toBe(false);
467
+ });
468
+
469
+ it("renders no card while every sub-tab field is removed", () => {
470
+ mount(MapoFormGroup, {
471
+ name: "seo",
472
+ fields: [],
473
+ subtabs: new Map([
474
+ ["social", [field("og_title", { visible: isLanding })]],
475
+ ]),
476
+ });
477
+
478
+ expect(findByClass("mapo-form-group")).toBeUndefined();
479
+ });
480
+ });
481
+
482
+ describe("MapoRepeaterItem cells", () => {
483
+ // The props a repeater gives each of its items.
484
+ const itemProps = (
485
+ item: Record<string, unknown>,
486
+ fields: AnyFieldDescriptor[],
487
+ ) => ({
488
+ item,
489
+ fields,
490
+ index: 0,
491
+ errorPrefix: "blocks.0",
492
+ parentErrors: {},
493
+ languages: [],
494
+ currentLang: "",
495
+ readonly: false,
496
+ registry: {},
497
+ defaultExpanded: true,
498
+ });
499
+
500
+ beforeEach(() => {
501
+ // An item's predicates read the item's own model, not the root form's.
502
+ form.context = undefined;
503
+ });
504
+
505
+ it("lays out the item's cells like a form section, following the item's model", async () => {
506
+ const item = ref<Record<string, unknown>>({ template: "home" });
507
+ const fields = [
508
+ field("title"),
509
+ field("cta_label", { cols: 6, visible: isLanding }),
510
+ field("hero_title", { cols: 6, visible: isHome }),
511
+ field("cta_url", { show: isLanding }),
512
+ ];
513
+ mount(MapoRepeaterItem, () => itemProps(item.value, fields));
514
+ expect(cellKeys()).toEqual(["title", "hero_title", "cta_url"]);
515
+ expect(isDisplayed(cellOf("cta_url"))).toBe(false);
516
+ expect(mountedFields.has("cta_url")).toBe(true);
517
+
518
+ item.value = { template: "landing" };
519
+ await nextTick();
520
+
521
+ expect(cellKeys()).toEqual(["title", "cta_label", "cta_url"]);
522
+ expect(isDisplayed(cellOf("cta_url"))).toBe(true);
523
+ expect(fieldMounts.filter((key) => key === "cta_url")).toHaveLength(1);
524
+ });
525
+
526
+ it("renders no body while `visible` removes every field", () => {
527
+ mount(
528
+ MapoRepeaterItem,
529
+ itemProps({ template: "home" }, [
530
+ field("cta_label", { visible: isLanding }),
531
+ ]),
532
+ );
533
+
534
+ expect(findByClass("grid")).toBeUndefined();
535
+ });
536
+
537
+ it("hides the body while `show` hides every field, keeping the fields mounted", () => {
538
+ mount(
539
+ MapoRepeaterItem,
540
+ itemProps({ template: "home" }, [field("cta_url", { show: isLanding })]),
541
+ );
542
+
543
+ expect(isDisplayed(findByClass("grid"))).toBe(false);
544
+ expect(mountedFields.has("cta_url")).toBe(true);
545
+ });
546
+ });
@@ -0,0 +1,145 @@
1
+ import { describe, expect, it } from "vitest";
2
+
3
+ import { formatHtml } from "../runtime/utils/formatHtml.js";
4
+
5
+ // The HTML source view lays out the editor's HTML with this formatter. Tiptap
6
+ // gives it as one line; the layout must only add whitespace an HTML parser
7
+ // that collapses whitespace ignores.
8
+
9
+ describe("formatHtml", () => {
10
+ it("puts each block on a line of its own and keeps inline content on its block's line", () => {
11
+ expect(
12
+ formatHtml(
13
+ '<h2>Title</h2><p>Some <strong>bold</strong> and <a href="/x">a link</a>.</p><hr><blockquote><p>Quote</p></blockquote>',
14
+ ),
15
+ ).toBe(
16
+ [
17
+ "<h2>Title</h2>",
18
+ '<p>Some <strong>bold</strong> and <a href="/x">a link</a>.</p>',
19
+ "<hr>",
20
+ "<blockquote>",
21
+ " <p>Quote</p>",
22
+ "</blockquote>",
23
+ ].join("\n"),
24
+ );
25
+ });
26
+
27
+ it("indents nested lists", () => {
28
+ expect(
29
+ formatHtml(
30
+ "<ul><li><p>a</p></li><li><p>b</p><ol><li><p>c</p></li></ol></li></ul>",
31
+ ),
32
+ ).toBe(
33
+ [
34
+ "<ul>",
35
+ " <li>",
36
+ " <p>a</p>",
37
+ " </li>",
38
+ " <li>",
39
+ " <p>b</p>",
40
+ " <ol>",
41
+ " <li>",
42
+ " <p>c</p>",
43
+ " </li>",
44
+ " </ol>",
45
+ " </li>",
46
+ "</ul>",
47
+ ].join("\n"),
48
+ );
49
+ });
50
+
51
+ it("lays out a table as Tiptap gives it", () => {
52
+ expect(
53
+ formatHtml(
54
+ '<table style="min-width: 25px"><colgroup><col style="min-width: 25px"></colgroup><tbody><tr><th colspan="1" rowspan="1"><p>A</p></th></tr><tr><td colspan="1" rowspan="1"><p>1</p></td></tr></tbody></table>',
55
+ ),
56
+ ).toBe(
57
+ [
58
+ '<table style="min-width: 25px">',
59
+ " <colgroup>",
60
+ ' <col style="min-width: 25px">',
61
+ " </colgroup>",
62
+ " <tbody>",
63
+ " <tr>",
64
+ ' <th colspan="1" rowspan="1">',
65
+ " <p>A</p>",
66
+ " </th>",
67
+ " </tr>",
68
+ " <tr>",
69
+ ' <td colspan="1" rowspan="1">',
70
+ " <p>1</p>",
71
+ " </td>",
72
+ " </tr>",
73
+ " </tbody>",
74
+ "</table>",
75
+ ].join("\n"),
76
+ );
77
+ });
78
+
79
+ it("keeps a cell's text on the cell's line", () => {
80
+ expect(formatHtml("<table><tr><td>1</td><td>2</td></tr></table>")).toBe(
81
+ [
82
+ "<table>",
83
+ " <tr>",
84
+ " <td>1</td>",
85
+ " <td>2</td>",
86
+ " </tr>",
87
+ "</table>",
88
+ ].join("\n"),
89
+ );
90
+ });
91
+
92
+ it("copies the content of pre and code as it is", () => {
93
+ const code =
94
+ '<pre><code class="language-js">if (a) {\n <p>not a block</p>\n}</code></pre>';
95
+ expect(formatHtml(`${code}<p>after</p>`)).toBe(`${code}\n<p>after</p>`);
96
+ expect(
97
+ formatHtml("<p>Run <code>a <ul> b</code> twice</p><p>next</p>"),
98
+ ).toBe("<p>Run <code>a <ul> b</code> twice</p>\n<p>next</p>");
99
+ });
100
+
101
+ it("gives a block image a line, and leaves an image inside text where it is", () => {
102
+ expect(
103
+ formatHtml(
104
+ '<p>Before</p><img src="a.png" alt="a > b"><p>Text <img src="b.png"> text</p><p><img src="c.png">caption</p>',
105
+ ),
106
+ ).toBe(
107
+ [
108
+ "<p>Before</p>",
109
+ '<img src="a.png" alt="a > b">',
110
+ '<p>Text <img src="b.png"> text</p>',
111
+ '<p><img src="c.png">caption</p>',
112
+ ].join("\n"),
113
+ );
114
+ });
115
+
116
+ it("gives text between blocks a line", () => {
117
+ expect(formatHtml("<p>a</p>loose <em>text</em><p>b</p>")).toBe(
118
+ "<p>a</p>\nloose <em>text</em>\n<p>b</p>",
119
+ );
120
+ });
121
+
122
+ it("leaves an empty document and plain text as they are", () => {
123
+ expect(formatHtml("")).toBe("");
124
+ expect(formatHtml("<p></p>")).toBe("<p></p>");
125
+ expect(formatHtml("plain <b>text</b>")).toBe("plain <b>text</b>");
126
+ });
127
+
128
+ it("gives the same layout when run on its own output", () => {
129
+ const html =
130
+ "<h2>T</h2><ul><li><p>a</p><ul><li><p>b</p></li></ul></li></ul><table><tbody><tr><td><p>1</p></td></tr></tbody></table><pre><code>x\n y</code></pre><p>a</p>tail<p>b</p>";
131
+ const once = formatHtml(html);
132
+ expect(formatHtml(once)).toBe(once);
133
+ });
134
+
135
+ it("drops only whitespace at the edges of blocks", () => {
136
+ // A no-break space is content, not layout.
137
+ expect(formatHtml("<p> a </p>\n\n<p> </p>")).toBe("<p>a </p>\n<p> </p>");
138
+ });
139
+
140
+ it("keeps a closing tag that has no opening one", () => {
141
+ expect(formatHtml("<p>a</p></div><p>b</p>")).toBe(
142
+ "<p>a</p></div>\n<p>b</p>",
143
+ );
144
+ });
145
+ });