@mapomodule/form 2.0.0-beta.15 → 2.0.0-beta.17
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.d.mts +13 -0
- package/dist/module.d.ts +13 -0
- package/dist/module.d.ts.map +1 -1
- package/dist/module.json +1 -1
- package/dist/module.mjs +6 -1
- package/dist/runtime/components/fields/MapoWygEditor.vue +351 -51
- package/dist/runtime/components/fields/MapoWygEditor.vue.d.ts.map +1 -1
- package/dist/runtime/devtools/DevtoolsFormsPage.vue +22 -0
- package/dist/runtime/devtools/DevtoolsFormsPage.vue.d.ts.map +1 -1
- package/dist/runtime/types/descriptor.d.ts +64 -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/types/registry.d.ts +6 -1
- package/dist/runtime/types/registry.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 +3 -2
- package/src/__tests__/MapoWygEditor.component.test.ts +886 -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/module.ts +20 -2
- package/src/runtime/components/fields/MapoWygEditor.vue +532 -69
- package/src/runtime/devtools/DevtoolsFormsPage.vue +23 -0
- package/src/runtime/types/descriptor.ts +72 -1
- package/src/runtime/types/index.ts +2 -0
- package/src/runtime/types/registry.ts +3 -1
- package/src/runtime/utils/formatHtml.ts +198 -0
- package/src/runtime/utils/sanitizeHtml.ts +60 -0
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { isSafeUrl, sanitizeHtml } from "../runtime/utils/sanitizeHtml.js";
|
|
4
|
+
|
|
5
|
+
// The URL check behind the editor's sanitizer and its Link extension. The
|
|
6
|
+
// sanitizer itself walks a DOM, which these tests do not have.
|
|
7
|
+
|
|
8
|
+
describe("isSafeUrl", () => {
|
|
9
|
+
it("accepts relative URLs and the allowed protocols", () => {
|
|
10
|
+
for (const url of [
|
|
11
|
+
"/page",
|
|
12
|
+
"page#anchor",
|
|
13
|
+
"?q=1",
|
|
14
|
+
"https://example.com",
|
|
15
|
+
"http://example.com",
|
|
16
|
+
"mailto:a@example.com",
|
|
17
|
+
"tel:+39000",
|
|
18
|
+
]) {
|
|
19
|
+
expect(isSafeUrl(url), url).toBe(true);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("rejects script and data protocols, whatever their case", () => {
|
|
24
|
+
for (const url of [
|
|
25
|
+
"javascript:alert(1)",
|
|
26
|
+
"JavaScript:alert(1)",
|
|
27
|
+
"vbscript:msgbox(1)",
|
|
28
|
+
"data:text/html,<script>alert(1)</script>",
|
|
29
|
+
]) {
|
|
30
|
+
expect(isSafeUrl(url), url).toBe(false);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe("sanitizeHtml", () => {
|
|
36
|
+
it("returns the HTML as it is without a DOM (server side)", () => {
|
|
37
|
+
expect(sanitizeHtml("<p>a</p>")).toBe("<p>a</p>");
|
|
38
|
+
expect(sanitizeHtml("")).toBe("");
|
|
39
|
+
});
|
|
40
|
+
});
|
package/src/index.ts
CHANGED
package/src/module.ts
CHANGED
|
@@ -16,6 +16,19 @@ export interface MapoFormOptions {
|
|
|
16
16
|
fields?: PartialFieldRegistry;
|
|
17
17
|
groups?: { expanded?: boolean };
|
|
18
18
|
debounce?: number;
|
|
19
|
+
/** Defaults for every `editor` field; a field's own `attrs` win. */
|
|
20
|
+
editor?: {
|
|
21
|
+
/**
|
|
22
|
+
* Shows the HTML source toggle in the toolbar of every editor field. A
|
|
23
|
+
* field sets `attrs.source` to differ from it. Default: `true`.
|
|
24
|
+
*/
|
|
25
|
+
source?: boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Shows the alignment buttons (left, centre, right) in every editor
|
|
28
|
+
* field. A field sets `attrs.textAlign` to differ from it. Default: `false`.
|
|
29
|
+
*/
|
|
30
|
+
textAlign?: boolean;
|
|
31
|
+
};
|
|
19
32
|
}
|
|
20
33
|
|
|
21
34
|
export default defineNuxtModule<MapoFormOptions>({
|
|
@@ -27,6 +40,7 @@ export default defineNuxtModule<MapoFormOptions>({
|
|
|
27
40
|
defaults: {
|
|
28
41
|
debounce: 300,
|
|
29
42
|
groups: { expanded: true },
|
|
43
|
+
editor: { source: true, textAlign: false },
|
|
30
44
|
},
|
|
31
45
|
|
|
32
46
|
async setup(options, nuxt) {
|
|
@@ -55,11 +69,15 @@ export default defineNuxtModule<MapoFormOptions>({
|
|
|
55
69
|
// export default defineNuxtPlugin(({ $mapoFormRegistry }) => {
|
|
56
70
|
// $mapoFormRegistry.mapping['my-type'] = () => import('~/components/MyField.vue')
|
|
57
71
|
// })
|
|
58
|
-
// `defaults` guarantees groups/debounce at runtime; fall back
|
|
59
|
-
// so the assignment type is fully defined.
|
|
72
|
+
// `defaults` guarantees groups/debounce/editor at runtime; fall back
|
|
73
|
+
// explicitly so the assignment type is fully defined.
|
|
60
74
|
nuxt.options.runtimeConfig.public.mapoForm = {
|
|
61
75
|
groups: { expanded: options.groups?.expanded ?? true },
|
|
62
76
|
debounce: options.debounce ?? 300,
|
|
77
|
+
editor: {
|
|
78
|
+
source: options.editor?.source ?? true,
|
|
79
|
+
textAlign: options.editor?.textAlign ?? false,
|
|
80
|
+
},
|
|
63
81
|
// attrs is JSON-serialisable (plain objects) — merged in the plugin
|
|
64
82
|
fields: { attrs: options.fields?.attrs ?? {} },
|
|
65
83
|
};
|