@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,37 @@
1
+ export const ALLOWED_LINK_PROTOCOLS = ["http", "https", "mailto", "tel"];
2
+ export function isSafeUrl(url) {
3
+ try {
4
+ if (!/^[a-z][a-z0-9+.-]*:/i.test(url)) return true;
5
+ const proto = new URL(url, "https://placeholder.local").protocol.replace(":", "").toLowerCase();
6
+ return ALLOWED_LINK_PROTOCOLS.includes(proto);
7
+ } catch {
8
+ return false;
9
+ }
10
+ }
11
+ export function sanitizeHtml(html) {
12
+ if (typeof window === "undefined" || !html) return html;
13
+ const tpl = document.createElement("template");
14
+ tpl.innerHTML = html;
15
+ const walker = document.createTreeWalker(
16
+ tpl.content,
17
+ NodeFilter.SHOW_ELEMENT
18
+ );
19
+ const toRemove = [];
20
+ let node = walker.nextNode();
21
+ while (node) {
22
+ if (node.tagName === "SCRIPT" || node.tagName === "STYLE") {
23
+ toRemove.push(node);
24
+ } else {
25
+ for (const attr of Array.from(node.attributes)) {
26
+ const name = attr.name.toLowerCase();
27
+ if (name.startsWith("on")) node.removeAttribute(attr.name);
28
+ if ((name === "href" || name === "src" || name === "xlink:href") && !isSafeUrl(attr.value)) {
29
+ node.removeAttribute(attr.name);
30
+ }
31
+ }
32
+ }
33
+ node = walker.nextNode();
34
+ }
35
+ toRemove.forEach((el) => el.remove());
36
+ return tpl.innerHTML;
37
+ }
@@ -1,5 +1,10 @@
1
1
  import type { AnyFieldDescriptor } from "../types/index.js";
2
2
  import type { TabEntry } from "../types/layout.js";
3
+ /**
4
+ * Whether the field's `visible` predicate removes it for this model. A removed
5
+ * field is not rendered, and neither is its grid cell.
6
+ */
7
+ export declare function isFieldRemoved(field: AnyFieldDescriptor, model: Record<string, unknown>): boolean;
3
8
  /**
4
9
  * Whether a field shows for this model: neither its `visible` predicate, which
5
10
  * removes it, nor its `show` predicate, which hides it, keeps it out.
@@ -12,7 +17,8 @@ export declare function isFieldShown(field: AnyFieldDescriptor, model: Record<st
12
17
  export declare function tabHasShownFields(tab: TabEntry, model: Record<string, unknown>): boolean;
13
18
  /**
14
19
  * The tab to open among the ones shown: the tab the user picked, while it is
15
- * shown; otherwise the first one. Empty when no tab is shown.
20
+ * shown; otherwise the first one. Empty when no tab is shown. A group's
21
+ * sub-tabs are picked the same way.
16
22
  */
17
- export declare function pickActiveTab(shownTabs: TabEntry[], picked: string | null): string;
23
+ export declare function pickActiveTab(shownTabs: Pick<TabEntry, "name">[], picked: string | null): string;
18
24
  //# sourceMappingURL=tabVisibility.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"tabVisibility.d.ts","sourceRoot":"","sources":["../../../src/runtime/utils/tabVisibility.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAEnD;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,kBAAkB,EACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAIT;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,QAAQ,EACb,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAQT;AAED;;;GAGG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,QAAQ,EAAE,EACrB,MAAM,EAAE,MAAM,GAAG,IAAI,GACpB,MAAM,CAGR"}
1
+ {"version":3,"file":"tabVisibility.d.ts","sourceRoot":"","sources":["../../../src/runtime/utils/tabVisibility.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AA4BnD;;;GAGG;AACH,wBAAgB,cAAc,CAC5B,KAAK,EAAE,kBAAkB,EACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAET;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,KAAK,EAAE,kBAAkB,EACzB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAIT;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAC/B,GAAG,EAAE,QAAQ,EACb,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAQT;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,IAAI,CAAC,QAAQ,EAAE,MAAM,CAAC,EAAE,EACnC,MAAM,EAAE,MAAM,GAAG,IAAI,GACpB,MAAM,CAGR"}
@@ -1,6 +1,24 @@
1
+ function passesPredicate(field, name, model) {
2
+ const predicate = field[name];
3
+ if (!predicate) return true;
4
+ try {
5
+ return Boolean(predicate({ model }));
6
+ } catch (error) {
7
+ if (import.meta.dev) {
8
+ console.warn(
9
+ `[MapoForm] The \`${name}\` predicate of field "${String(field.key)}" threw, so the field is hidden.`,
10
+ error
11
+ );
12
+ }
13
+ return false;
14
+ }
15
+ }
16
+ export function isFieldRemoved(field, model) {
17
+ return !passesPredicate(field, "visible", model);
18
+ }
1
19
  export function isFieldShown(field, model) {
2
- const removed = field.visible ? !field.visible({ model }) : false;
3
- const hidden = field.show ? !field.show({ model }) : false;
20
+ const removed = isFieldRemoved(field, model);
21
+ const hidden = !passesPredicate(field, "show", model);
4
22
  return !removed && !hidden;
5
23
  }
6
24
  export function tabHasShownFields(tab, model) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mapomodule/form",
3
- "version": "2.0.0-beta.14",
3
+ "version": "2.0.0-beta.16",
4
4
  "description": "Mapo declarative typed form engine and field components",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -47,7 +47,7 @@
47
47
  },
48
48
  "dependencies": {
49
49
  "@internationalized/date": "^3.7.0",
50
- "@mapomodule/i18n": "2.0.0-beta.5",
50
+ "@mapomodule/i18n": "2.0.0-beta.7",
51
51
  "@mapomodule/store": "2.0.0-beta.6",
52
52
  "@mapomodule/utils": "2.0.0-beta.7",
53
53
  "@tiptap/extension-image": "^2.11.5",
@@ -0,0 +1,130 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2
+ import { createRenderer, h } from "vue";
3
+ import type { App, Component } from "vue";
4
+
5
+ import MapoFormGroup from "../runtime/components/MapoFormGroup.vue";
6
+
7
+ const runtimeConfig = vi.hoisted(() => ({
8
+ public: { mapoForm: { groups: { expanded: true } } },
9
+ }));
10
+
11
+ // Keys of the fields the group renders: its body renders them only while the
12
+ // group is expanded.
13
+ const renderedFields = vi.hoisted(() => [] as string[]);
14
+
15
+ vi.mock("#app", () => ({ useRuntimeConfig: () => runtimeConfig }));
16
+ vi.mock("../runtime/composables/useMapoForm.js", () => ({
17
+ injectMapoForm: () => undefined,
18
+ }));
19
+ vi.mock("../runtime/components/MapoFormField.vue", () => ({
20
+ default: {
21
+ name: "MapoFormField",
22
+ props: { descriptor: { type: Object, required: true } },
23
+ setup(props: { descriptor: { key: string } }) {
24
+ renderedFields.push(props.descriptor.key);
25
+ return () => null;
26
+ },
27
+ },
28
+ }));
29
+
30
+ // Tests run in Node without a DOM. A renderer whose nodes are plain objects is
31
+ // enough to mount the component; `style` takes what `v-show` writes, and
32
+ // `querySelector` gives the field-expand `Teleport` a target.
33
+ interface HeadlessNode {
34
+ parent: HeadlessNode | null;
35
+ children: HeadlessNode[];
36
+ style: Record<string, string>;
37
+ }
38
+
39
+ const headlessNode = (): HeadlessNode => ({
40
+ parent: null,
41
+ children: [],
42
+ style: {},
43
+ });
44
+ const body = headlessNode();
45
+
46
+ const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
47
+ createElement: headlessNode,
48
+ createText: headlessNode,
49
+ createComment: headlessNode,
50
+ setText: () => {},
51
+ setElementText: () => {},
52
+ patchProp: () => {},
53
+ insert(child, parent, anchor) {
54
+ child.parent = parent;
55
+ const at = anchor ? parent.children.indexOf(anchor) : -1;
56
+ parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
57
+ },
58
+ remove(child) {
59
+ child.parent?.children.splice(child.parent.children.indexOf(child), 1);
60
+ child.parent = null;
61
+ },
62
+ parentNode: (node) => node.parent,
63
+ nextSibling: (node) =>
64
+ node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
65
+ querySelector: () => body,
66
+ });
67
+
68
+ const UIcon: Component = { name: "UIcon", render: () => null };
69
+
70
+ let app: App<HeadlessNode> | undefined;
71
+
72
+ /** Mounts a group with one field; `initialExpanded` is passed only when given. */
73
+ function mountGroup(props: { initialExpanded?: boolean } = {}) {
74
+ const root: Component = {
75
+ render: () =>
76
+ h(MapoFormGroup as Component, {
77
+ name: "seo",
78
+ fields: [{ key: "meta_title", type: "text" }],
79
+ ...props,
80
+ }),
81
+ };
82
+ app = createApp(root);
83
+ app.component("UIcon", UIcon);
84
+ app.mount(headlessNode());
85
+ }
86
+
87
+ describe("MapoFormGroup initial state", () => {
88
+ beforeEach(() => {
89
+ renderedFields.length = 0;
90
+ // The field-expand listeners touch `document`, which Node does not have.
91
+ vi.stubGlobal("document", {
92
+ addEventListener: vi.fn(),
93
+ removeEventListener: vi.fn(),
94
+ body: { style: {} },
95
+ });
96
+ });
97
+
98
+ afterEach(() => {
99
+ app?.unmount();
100
+ app = undefined;
101
+ vi.unstubAllGlobals();
102
+ });
103
+
104
+ it("starts expanded when the prop is absent and groups.expanded is true", () => {
105
+ runtimeConfig.public.mapoForm.groups.expanded = true;
106
+ mountGroup();
107
+
108
+ expect(renderedFields).toEqual(["meta_title"]);
109
+ });
110
+
111
+ it("starts collapsed when the prop is absent and groups.expanded is false", () => {
112
+ runtimeConfig.public.mapoForm.groups.expanded = false;
113
+ mountGroup();
114
+
115
+ expect(renderedFields).toEqual([]);
116
+ });
117
+
118
+ it.each([
119
+ { initialExpanded: false, configExpanded: true, fields: [] },
120
+ { initialExpanded: true, configExpanded: false, fields: ["meta_title"] },
121
+ ])(
122
+ "an explicit initialExpanded $initialExpanded wins over groups.expanded $configExpanded",
123
+ ({ initialExpanded, configExpanded, fields }) => {
124
+ runtimeConfig.public.mapoForm.groups.expanded = configExpanded;
125
+ mountGroup({ initialExpanded });
126
+
127
+ expect(renderedFields).toEqual(fields);
128
+ },
129
+ );
130
+ });