@mapomodule/form 2.0.0-beta.15 → 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__/MapoWygEditor.component.test.d.ts +2 -0
- package/dist/__tests__/MapoWygEditor.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/fields/MapoWygEditor.vue +157 -50
- package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
- package/dist/runtime/types/descriptor.d.ts +55 -0
- 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/package.json +2 -2
- package/src/__tests__/MapoWygEditor.component.test.ts +525 -0
- package/src/__tests__/formatHtml.test.ts +145 -0
- package/src/__tests__/sanitizeHtml.test.ts +40 -0
- package/src/index.ts +2 -0
- package/src/runtime/components/fields/MapoWygEditor.vue +199 -69
- package/src/runtime/types/descriptor.ts +63 -1
- package/src/runtime/types/index.ts +2 -0
- package/src/runtime/utils/formatHtml.ts +198 -0
- package/src/runtime/utils/sanitizeHtml.ts +60 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
const BLOCK_TAGS = /* @__PURE__ */ new Set([
|
|
2
|
+
"p",
|
|
3
|
+
"h1",
|
|
4
|
+
"h2",
|
|
5
|
+
"h3",
|
|
6
|
+
"h4",
|
|
7
|
+
"h5",
|
|
8
|
+
"h6",
|
|
9
|
+
"ul",
|
|
10
|
+
"ol",
|
|
11
|
+
"li",
|
|
12
|
+
"table",
|
|
13
|
+
"colgroup",
|
|
14
|
+
"col",
|
|
15
|
+
"thead",
|
|
16
|
+
"tbody",
|
|
17
|
+
"tfoot",
|
|
18
|
+
"tr",
|
|
19
|
+
"th",
|
|
20
|
+
"td",
|
|
21
|
+
"blockquote",
|
|
22
|
+
"pre",
|
|
23
|
+
"hr",
|
|
24
|
+
"img",
|
|
25
|
+
"div",
|
|
26
|
+
"figure",
|
|
27
|
+
"figcaption"
|
|
28
|
+
]);
|
|
29
|
+
const TEXTBLOCK_TAGS = /* @__PURE__ */ new Set([
|
|
30
|
+
"p",
|
|
31
|
+
"h1",
|
|
32
|
+
"h2",
|
|
33
|
+
"h3",
|
|
34
|
+
"h4",
|
|
35
|
+
"h5",
|
|
36
|
+
"h6",
|
|
37
|
+
"figcaption"
|
|
38
|
+
]);
|
|
39
|
+
const VOID_TAGS = /* @__PURE__ */ new Set([
|
|
40
|
+
"area",
|
|
41
|
+
"base",
|
|
42
|
+
"br",
|
|
43
|
+
"col",
|
|
44
|
+
"embed",
|
|
45
|
+
"hr",
|
|
46
|
+
"img",
|
|
47
|
+
"input",
|
|
48
|
+
"link",
|
|
49
|
+
"meta",
|
|
50
|
+
"source",
|
|
51
|
+
"track",
|
|
52
|
+
"wbr"
|
|
53
|
+
]);
|
|
54
|
+
const VERBATIM_TAGS = /* @__PURE__ */ new Set(["pre", "code"]);
|
|
55
|
+
const TOKEN = /<!--[\s\S]*?-->|<\/?[a-zA-Z][^\s/>]*(?:"[^"]*"|'[^']*'|[^'">])*>|[^<]+|</g;
|
|
56
|
+
const TAG_NAME = /^<\/?([a-zA-Z][a-zA-Z0-9-]*)/;
|
|
57
|
+
const BLANK = /^[ \t\n\f\r]*$/;
|
|
58
|
+
const LEADING_BLANK = /^[ \t\n\f\r]+/;
|
|
59
|
+
const TRAILING_BLANK = /[ \t\n\f\r]+$/;
|
|
60
|
+
export function formatHtml(html, indent = " ") {
|
|
61
|
+
const lines = [];
|
|
62
|
+
const open = [];
|
|
63
|
+
let line = "";
|
|
64
|
+
let last = "block-open";
|
|
65
|
+
let verbatim = null;
|
|
66
|
+
const startLine = () => {
|
|
67
|
+
const isBlankLine = BLANK.test(line);
|
|
68
|
+
if (!isBlankLine) lines.push(line.replace(TRAILING_BLANK, ""));
|
|
69
|
+
line = indent.repeat(open.length);
|
|
70
|
+
};
|
|
71
|
+
const closeBlock = (name, token) => {
|
|
72
|
+
let at = open.length - 1;
|
|
73
|
+
while (at >= 0 && open[at].name !== name) at -= 1;
|
|
74
|
+
const isStray = at < 0;
|
|
75
|
+
if (isStray) {
|
|
76
|
+
line += token;
|
|
77
|
+
last = "block-end";
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
const holdsBlocks = open[at].holdsBlocks;
|
|
81
|
+
open.length = at;
|
|
82
|
+
if (holdsBlocks) startLine();
|
|
83
|
+
line += token;
|
|
84
|
+
last = "block-end";
|
|
85
|
+
};
|
|
86
|
+
for (const [token] of html.matchAll(TOKEN)) {
|
|
87
|
+
const name = TAG_NAME.exec(token)?.[1]?.toLowerCase();
|
|
88
|
+
const isClosing = token.startsWith("</");
|
|
89
|
+
if (verbatim) {
|
|
90
|
+
const isVerbatimTag = name === verbatim.name;
|
|
91
|
+
if (isVerbatimTag && isClosing) verbatim.depth -= 1;
|
|
92
|
+
else if (isVerbatimTag) verbatim.depth += 1;
|
|
93
|
+
const verbatimEnds = verbatim.depth === 0;
|
|
94
|
+
if (!verbatimEnds) {
|
|
95
|
+
line += token;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
verbatim = null;
|
|
99
|
+
if (name === "pre") closeBlock(name, token);
|
|
100
|
+
else line += token;
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
const isBlock = name !== void 0 && BLOCK_TAGS.has(name);
|
|
104
|
+
const parent = open.at(-1);
|
|
105
|
+
const isInsideText = last === "inline" || parent !== void 0 && TEXTBLOCK_TAGS.has(parent.name);
|
|
106
|
+
const isInlineImage = name === "img" && isInsideText;
|
|
107
|
+
if (!isBlock || isInlineImage) {
|
|
108
|
+
const isText = name === void 0;
|
|
109
|
+
const isBlankBetweenBlocks = isText && last !== "inline" && BLANK.test(token);
|
|
110
|
+
if (isBlankBetweenBlocks) continue;
|
|
111
|
+
if (last === "block-end") startLine();
|
|
112
|
+
const startsText = isText && last !== "inline";
|
|
113
|
+
line += startsText ? token.replace(LEADING_BLANK, "") : token;
|
|
114
|
+
last = "inline";
|
|
115
|
+
const opensVerbatim = name !== void 0 && VERBATIM_TAGS.has(name) && !isClosing;
|
|
116
|
+
if (opensVerbatim) verbatim = { name, depth: 1 };
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
if (isClosing) {
|
|
120
|
+
closeBlock(name, token);
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
123
|
+
if (parent) parent.holdsBlocks = true;
|
|
124
|
+
startLine();
|
|
125
|
+
line += token;
|
|
126
|
+
const isVoid = VOID_TAGS.has(name) || token.endsWith("/>");
|
|
127
|
+
if (isVoid) {
|
|
128
|
+
last = "block-end";
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
open.push({ name, holdsBlocks: false });
|
|
132
|
+
last = "block-open";
|
|
133
|
+
if (name === "pre") verbatim = { name, depth: 1 };
|
|
134
|
+
}
|
|
135
|
+
startLine();
|
|
136
|
+
return lines.join("\n");
|
|
137
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const ALLOWED_LINK_PROTOCOLS: string[];
|
|
2
|
+
/**
|
|
3
|
+
* Whether a link or image URL is safe to keep: relative, or with a protocol in
|
|
4
|
+
* {@link ALLOWED_LINK_PROTOCOLS}.
|
|
5
|
+
*/
|
|
6
|
+
export declare function isSafeUrl(url: string): boolean;
|
|
7
|
+
/**
|
|
8
|
+
* Sanitise incoming HTML from external sources (initial set, server sync, the
|
|
9
|
+
* source view) by removing <script>, inline handlers (on*), and href:
|
|
10
|
+
* javascript:. This is intentionally minimal (no DOMPurify) to avoid a heavy
|
|
11
|
+
* dependency: it covers the most common vectors; for truly untrusted HTML the
|
|
12
|
+
* consumer should use DOMPurify at the app level. Without a DOM (SSR) the HTML
|
|
13
|
+
* is returned as it is.
|
|
14
|
+
*/
|
|
15
|
+
export declare function sanitizeHtml(html: string): string;
|
|
16
|
+
//# sourceMappingURL=sanitizeHtml.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sanitizeHtml.d.ts","sourceRoot":"","sources":["../../../src/runtime/utils/sanitizeHtml.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,sBAAsB,UAAqC,CAAC;AAEzE;;;GAGG;AACH,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAW9C;AAED;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CA6BjD"}
|
|
@@ -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
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mapomodule/form",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
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.
|
|
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",
|