@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.
- package/dist/__tests__/MapoFormGroup.component.test.d.ts +2 -0
- package/dist/__tests__/MapoFormGroup.component.test.d.ts.map +1 -0
- package/dist/__tests__/MapoWygEditor.component.test.d.ts +2 -0
- package/dist/__tests__/MapoWygEditor.component.test.d.ts.map +1 -0
- package/dist/__tests__/fieldVisibility.component.test.d.ts +2 -0
- package/dist/__tests__/fieldVisibility.component.test.d.ts.map +1 -0
- package/dist/__tests__/formatHtml.test.d.ts +2 -0
- package/dist/__tests__/formatHtml.test.d.ts.map +1 -0
- package/dist/__tests__/sanitizeHtml.test.d.ts +2 -0
- package/dist/__tests__/sanitizeHtml.test.d.ts.map +1 -0
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/runtime/components/MapoFormField.vue +5 -10
- package/dist/runtime/components/MapoFormField.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormFlatSection.vue +21 -3
- package/dist/runtime/components/MapoFormFlatSection.vue.d.ts +1 -0
- package/dist/runtime/components/MapoFormFlatSection.vue.d.ts.map +1 -1
- package/dist/runtime/components/MapoFormGroup.vue +69 -34
- package/dist/runtime/components/MapoFormGroup.vue.d.ts +19 -4
- package/dist/runtime/components/MapoFormGroup.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoRepeaterItem.vue +18 -2
- package/dist/runtime/components/fields/MapoRepeaterItem.vue.d.ts.map +1 -1
- package/dist/runtime/components/fields/MapoWygEditor.vue +157 -50
- package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
- package/dist/runtime/types/descriptor.d.ts +65 -2
- package/dist/runtime/types/descriptor.d.ts.map +1 -1
- package/dist/runtime/types/index.d.ts +1 -1
- package/dist/runtime/types/index.d.ts.map +1 -1
- package/dist/runtime/utils/formatHtml.d.ts +16 -0
- package/dist/runtime/utils/formatHtml.d.ts.map +1 -0
- package/dist/runtime/utils/formatHtml.js +137 -0
- package/dist/runtime/utils/sanitizeHtml.d.ts +16 -0
- package/dist/runtime/utils/sanitizeHtml.d.ts.map +1 -0
- package/dist/runtime/utils/sanitizeHtml.js +37 -0
- package/dist/runtime/utils/tabVisibility.d.ts +8 -2
- package/dist/runtime/utils/tabVisibility.d.ts.map +1 -1
- package/dist/runtime/utils/tabVisibility.js +20 -2
- package/package.json +2 -2
- package/src/__tests__/MapoFormGroup.component.test.ts +130 -0
- package/src/__tests__/MapoWygEditor.component.test.ts +525 -0
- package/src/__tests__/fieldVisibility.component.test.ts +546 -0
- package/src/__tests__/formatHtml.test.ts +145 -0
- package/src/__tests__/sanitizeHtml.test.ts +40 -0
- package/src/__tests__/tabVisibility.test.ts +27 -0
- package/src/index.ts +2 -0
- package/src/runtime/components/MapoFormField.vue +7 -13
- package/src/runtime/components/MapoFormFlatSection.vue +38 -5
- package/src/runtime/components/MapoFormGroup.vue +116 -43
- package/src/runtime/components/fields/MapoRepeaterItem.vue +32 -2
- package/src/runtime/components/fields/MapoWygEditor.vue +199 -69
- package/src/runtime/types/descriptor.ts +73 -3
- package/src/runtime/types/index.ts +2 -0
- package/src/runtime/utils/formatHtml.ts +198 -0
- package/src/runtime/utils/sanitizeHtml.ts +60 -0
- package/src/runtime/utils/tabVisibility.ts +42 -4
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Protocol allowlist for user-entered links. Without this,
|
|
2
|
+
// `<a href="javascript:...">` would be accepted, leading to XSS when content is
|
|
3
|
+
// rendered elsewhere (preview, server-side, other editors).
|
|
4
|
+
export const ALLOWED_LINK_PROTOCOLS = ["http", "https", "mailto", "tel"];
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Whether a link or image URL is safe to keep: relative, or with a protocol in
|
|
8
|
+
* {@link ALLOWED_LINK_PROTOCOLS}.
|
|
9
|
+
*/
|
|
10
|
+
export function isSafeUrl(url: string): boolean {
|
|
11
|
+
try {
|
|
12
|
+
// URL relativi (es. "/page") non hanno protocol — li accettiamo.
|
|
13
|
+
if (!/^[a-z][a-z0-9+.-]*:/i.test(url)) return true;
|
|
14
|
+
const proto = new URL(url, "https://placeholder.local").protocol
|
|
15
|
+
.replace(":", "")
|
|
16
|
+
.toLowerCase();
|
|
17
|
+
return ALLOWED_LINK_PROTOCOLS.includes(proto);
|
|
18
|
+
} catch {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Sanitise incoming HTML from external sources (initial set, server sync, the
|
|
25
|
+
* source view) by removing <script>, inline handlers (on*), and href:
|
|
26
|
+
* javascript:. This is intentionally minimal (no DOMPurify) to avoid a heavy
|
|
27
|
+
* dependency: it covers the most common vectors; for truly untrusted HTML the
|
|
28
|
+
* consumer should use DOMPurify at the app level. Without a DOM (SSR) the HTML
|
|
29
|
+
* is returned as it is.
|
|
30
|
+
*/
|
|
31
|
+
export function sanitizeHtml(html: string): string {
|
|
32
|
+
if (typeof window === "undefined" || !html) return html;
|
|
33
|
+
const tpl = document.createElement("template");
|
|
34
|
+
tpl.innerHTML = html;
|
|
35
|
+
const walker = document.createTreeWalker(
|
|
36
|
+
tpl.content,
|
|
37
|
+
NodeFilter.SHOW_ELEMENT,
|
|
38
|
+
);
|
|
39
|
+
const toRemove: Element[] = [];
|
|
40
|
+
let node = walker.nextNode() as Element | null;
|
|
41
|
+
while (node) {
|
|
42
|
+
if (node.tagName === "SCRIPT" || node.tagName === "STYLE") {
|
|
43
|
+
toRemove.push(node);
|
|
44
|
+
} else {
|
|
45
|
+
for (const attr of Array.from(node.attributes)) {
|
|
46
|
+
const name = attr.name.toLowerCase();
|
|
47
|
+
if (name.startsWith("on")) node.removeAttribute(attr.name);
|
|
48
|
+
if (
|
|
49
|
+
(name === "href" || name === "src" || name === "xlink:href") &&
|
|
50
|
+
!isSafeUrl(attr.value)
|
|
51
|
+
) {
|
|
52
|
+
node.removeAttribute(attr.name);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
node = walker.nextNode() as Element | null;
|
|
57
|
+
}
|
|
58
|
+
toRemove.forEach((el) => el.remove());
|
|
59
|
+
return tpl.innerHTML;
|
|
60
|
+
}
|
|
@@ -1,6 +1,43 @@
|
|
|
1
1
|
import type { AnyFieldDescriptor } from "../types/index.js";
|
|
2
2
|
import type { TabEntry } from "../types/layout.js";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Runs the field's `visible` or `show` predicate; a field without it passes.
|
|
6
|
+
* A predicate that throws, say one reading `model.seo.enabled` while `seo` is
|
|
7
|
+
* null, counts as `false`: it hides its own field, and the error does not take
|
|
8
|
+
* the field's section, group card or tab down with it. Development builds warn.
|
|
9
|
+
*/
|
|
10
|
+
function passesPredicate(
|
|
11
|
+
field: AnyFieldDescriptor,
|
|
12
|
+
name: "visible" | "show",
|
|
13
|
+
model: Record<string, unknown>,
|
|
14
|
+
): boolean {
|
|
15
|
+
const predicate = field[name];
|
|
16
|
+
if (!predicate) return true;
|
|
17
|
+
try {
|
|
18
|
+
return Boolean(predicate({ model }));
|
|
19
|
+
} catch (error) {
|
|
20
|
+
if (import.meta.dev) {
|
|
21
|
+
console.warn(
|
|
22
|
+
`[MapoForm] The \`${name}\` predicate of field "${String(field.key)}" threw, so the field is hidden.`,
|
|
23
|
+
error,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Whether the field's `visible` predicate removes it for this model. A removed
|
|
32
|
+
* field is not rendered, and neither is its grid cell.
|
|
33
|
+
*/
|
|
34
|
+
export function isFieldRemoved(
|
|
35
|
+
field: AnyFieldDescriptor,
|
|
36
|
+
model: Record<string, unknown>,
|
|
37
|
+
): boolean {
|
|
38
|
+
return !passesPredicate(field, "visible", model);
|
|
39
|
+
}
|
|
40
|
+
|
|
4
41
|
/**
|
|
5
42
|
* Whether a field shows for this model: neither its `visible` predicate, which
|
|
6
43
|
* removes it, nor its `show` predicate, which hides it, keeps it out.
|
|
@@ -9,8 +46,8 @@ export function isFieldShown(
|
|
|
9
46
|
field: AnyFieldDescriptor,
|
|
10
47
|
model: Record<string, unknown>,
|
|
11
48
|
): boolean {
|
|
12
|
-
const removed = field
|
|
13
|
-
const hidden = field
|
|
49
|
+
const removed = isFieldRemoved(field, model);
|
|
50
|
+
const hidden = !passesPredicate(field, "show", model);
|
|
14
51
|
return !removed && !hidden;
|
|
15
52
|
}
|
|
16
53
|
|
|
@@ -33,10 +70,11 @@ export function tabHasShownFields(
|
|
|
33
70
|
|
|
34
71
|
/**
|
|
35
72
|
* The tab to open among the ones shown: the tab the user picked, while it is
|
|
36
|
-
* shown; otherwise the first one. Empty when no tab is shown.
|
|
73
|
+
* shown; otherwise the first one. Empty when no tab is shown. A group's
|
|
74
|
+
* sub-tabs are picked the same way.
|
|
37
75
|
*/
|
|
38
76
|
export function pickActiveTab(
|
|
39
|
-
shownTabs: TabEntry[],
|
|
77
|
+
shownTabs: Pick<TabEntry, "name">[],
|
|
40
78
|
picked: string | null,
|
|
41
79
|
): string {
|
|
42
80
|
const pickedTab = shownTabs.find((tab) => tab.name === picked);
|