@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,525 @@
|
|
|
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 MapoWygEditor from "../runtime/components/fields/MapoWygEditor.vue";
|
|
6
|
+
import type { EditorDescriptor } from "../runtime/types/index.js";
|
|
7
|
+
|
|
8
|
+
// The editor field with and without the HTML source view (`attrs.source`).
|
|
9
|
+
// Tiptap needs a DOM, which these tests do not have: `@tiptap/vue-3` is
|
|
10
|
+
// replaced by an editor that keeps its document as an HTML string and
|
|
11
|
+
// normalizes what it is given, the way a schema would.
|
|
12
|
+
|
|
13
|
+
interface FakeEditor {
|
|
14
|
+
getHTML: () => string;
|
|
15
|
+
commands: { setContent: ReturnType<typeof vi.fn> };
|
|
16
|
+
setEditable: ReturnType<typeof vi.fn>;
|
|
17
|
+
isActive: () => boolean;
|
|
18
|
+
chain: () => unknown;
|
|
19
|
+
can: () => unknown;
|
|
20
|
+
destroy: ReturnType<typeof vi.fn>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const tiptap = vi.hoisted(() => ({ editor: undefined as unknown }));
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What the fake schema does to HTML: whitespace between tags goes, a `<span>`
|
|
27
|
+
* (no extension supports it) is unwrapped, an empty document is one empty
|
|
28
|
+
* paragraph.
|
|
29
|
+
*/
|
|
30
|
+
const normalize = (html: string) =>
|
|
31
|
+
html
|
|
32
|
+
.replace(/>[ \t\n\f\r]+</g, "><")
|
|
33
|
+
.replace(/<\/?span[^>]*>/g, "")
|
|
34
|
+
.trim() || "<p></p>";
|
|
35
|
+
|
|
36
|
+
function createFakeEditor(content: string): FakeEditor {
|
|
37
|
+
let html = normalize(content);
|
|
38
|
+
// A command chain: any command returns the chain, `run()` applies nothing.
|
|
39
|
+
const chain: unknown = new Proxy(() => chain, {
|
|
40
|
+
get: () => () => chain,
|
|
41
|
+
});
|
|
42
|
+
return {
|
|
43
|
+
getHTML: () => html,
|
|
44
|
+
commands: {
|
|
45
|
+
setContent: vi.fn((value: string) => {
|
|
46
|
+
html = normalize(value);
|
|
47
|
+
}),
|
|
48
|
+
},
|
|
49
|
+
setEditable: vi.fn(),
|
|
50
|
+
isActive: () => false,
|
|
51
|
+
chain: () => chain,
|
|
52
|
+
can: () => chain,
|
|
53
|
+
destroy: vi.fn(),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
vi.mock("@tiptap/vue-3", async () => {
|
|
58
|
+
const { h, shallowRef } = await import("vue");
|
|
59
|
+
return {
|
|
60
|
+
useEditor: (options: { content: string }) => {
|
|
61
|
+
tiptap.editor = createFakeEditor(options.content);
|
|
62
|
+
return shallowRef(tiptap.editor);
|
|
63
|
+
},
|
|
64
|
+
EditorContent: {
|
|
65
|
+
name: "EditorContent",
|
|
66
|
+
props: { editor: { type: Object, default: undefined } },
|
|
67
|
+
render: () => h("editor-content"),
|
|
68
|
+
},
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// The DOM sanitizer returns its input without a DOM; this stand-in removes the
|
|
73
|
+
// vectors it targets, so the tests see whether the source goes through it.
|
|
74
|
+
vi.mock("../runtime/utils/sanitizeHtml.js", async (importOriginal) => ({
|
|
75
|
+
...(await importOriginal<Record<string, unknown>>()),
|
|
76
|
+
sanitizeHtml: vi.fn((html: string) =>
|
|
77
|
+
html
|
|
78
|
+
.replace(/<script[\s\S]*?<\/script>/gi, "")
|
|
79
|
+
.replace(/\son\w+="[^"]*"/gi, "")
|
|
80
|
+
.replace(/\shref="javascript:[^"]*"/gi, ""),
|
|
81
|
+
),
|
|
82
|
+
}));
|
|
83
|
+
|
|
84
|
+
vi.mock("vue-i18n", () => ({ useI18n: () => ({ t: (key: string) => key }) }));
|
|
85
|
+
|
|
86
|
+
// Nodes are plain objects: each keeps its tag, its props (attributes and
|
|
87
|
+
// listeners included) and its text; `style` takes what `v-show` writes.
|
|
88
|
+
interface HeadlessNode {
|
|
89
|
+
type: string;
|
|
90
|
+
text: string;
|
|
91
|
+
parent: HeadlessNode | null;
|
|
92
|
+
children: HeadlessNode[];
|
|
93
|
+
props: Record<string, unknown>;
|
|
94
|
+
style: Record<string, string>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const headlessNode = (type: string, text = ""): HeadlessNode => ({
|
|
98
|
+
type,
|
|
99
|
+
text,
|
|
100
|
+
parent: null,
|
|
101
|
+
children: [],
|
|
102
|
+
props: {},
|
|
103
|
+
style: {},
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const { createApp } = createRenderer<HeadlessNode, HeadlessNode>({
|
|
107
|
+
createElement: (type) => headlessNode(type),
|
|
108
|
+
createText: (text) => headlessNode("#text", text),
|
|
109
|
+
createComment: (text) => headlessNode("#comment", text),
|
|
110
|
+
setText: (node, text) => {
|
|
111
|
+
node.text = text;
|
|
112
|
+
},
|
|
113
|
+
setElementText: (node, text) => {
|
|
114
|
+
node.children = [];
|
|
115
|
+
node.text = text;
|
|
116
|
+
},
|
|
117
|
+
patchProp(node, key, _previous, next) {
|
|
118
|
+
node.props[key] = next;
|
|
119
|
+
},
|
|
120
|
+
insert(child, parent, anchor) {
|
|
121
|
+
child.parent = parent;
|
|
122
|
+
const at = anchor ? parent.children.indexOf(anchor) : -1;
|
|
123
|
+
parent.children.splice(at < 0 ? parent.children.length : at, 0, child);
|
|
124
|
+
},
|
|
125
|
+
remove(child) {
|
|
126
|
+
child.parent?.children.splice(child.parent.children.indexOf(child), 1);
|
|
127
|
+
child.parent = null;
|
|
128
|
+
},
|
|
129
|
+
parentNode: (node) => node.parent,
|
|
130
|
+
nextSibling: (node) =>
|
|
131
|
+
node.parent?.children[node.parent.children.indexOf(node) + 1] ?? null,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// Nuxt UI stand-ins: the element each one renders keeps the attributes and
|
|
135
|
+
// listeners the editor passes.
|
|
136
|
+
const stubs: Record<string, Component> = {
|
|
137
|
+
UButton: {
|
|
138
|
+
setup:
|
|
139
|
+
(_, { slots }) =>
|
|
140
|
+
() =>
|
|
141
|
+
h("button", slots.default?.()),
|
|
142
|
+
},
|
|
143
|
+
UTooltip: {
|
|
144
|
+
props: { text: { type: String, default: "" } },
|
|
145
|
+
setup:
|
|
146
|
+
(props, { slots }) =>
|
|
147
|
+
() =>
|
|
148
|
+
h("tooltip", { text: props.text }, slots.default?.()),
|
|
149
|
+
},
|
|
150
|
+
USeparator: { render: () => h("separator") },
|
|
151
|
+
UTextarea: {
|
|
152
|
+
props: { modelValue: { type: String, default: "" } },
|
|
153
|
+
emits: ["update:modelValue"],
|
|
154
|
+
setup:
|
|
155
|
+
(props, { emit }) =>
|
|
156
|
+
() =>
|
|
157
|
+
h("textarea", {
|
|
158
|
+
value: props.modelValue,
|
|
159
|
+
onInput: (value: string) => emit("update:modelValue", value),
|
|
160
|
+
}),
|
|
161
|
+
},
|
|
162
|
+
MapoMediaManagerDialog: { render: () => null },
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
const all = (node: HeadlessNode): HeadlessNode[] => [
|
|
166
|
+
node,
|
|
167
|
+
...node.children.flatMap(all),
|
|
168
|
+
];
|
|
169
|
+
|
|
170
|
+
let app: App<HeadlessNode> | undefined;
|
|
171
|
+
let root: HeadlessNode;
|
|
172
|
+
let modelValue: Ref<string>;
|
|
173
|
+
let emitted: string[];
|
|
174
|
+
|
|
175
|
+
const fakeEditor = () => tiptap.editor as FakeEditor;
|
|
176
|
+
const buttons = () => all(root).filter((node) => node.type === "button");
|
|
177
|
+
const sourceToggle = () =>
|
|
178
|
+
buttons().find((node) => node.props.icon === "i-lucide-code-xml");
|
|
179
|
+
const textarea = () => all(root).find((node) => node.type === "textarea");
|
|
180
|
+
const editorContent = () =>
|
|
181
|
+
all(root).find((node) => node.type === "editor-content")!;
|
|
182
|
+
const isHidden = (node: HeadlessNode) => node.style.display === "none";
|
|
183
|
+
|
|
184
|
+
function click(node: HeadlessNode | undefined) {
|
|
185
|
+
(node!.props.onClick as () => void)();
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Types into the source textarea: the stub reports it as `update:modelValue`. */
|
|
189
|
+
function typeSource(html: string) {
|
|
190
|
+
(textarea()!.props.onInput as (value: string) => void)(html);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function mount(
|
|
194
|
+
attrs: EditorDescriptor["attrs"] = {},
|
|
195
|
+
state: { readonly?: boolean } = {},
|
|
196
|
+
) {
|
|
197
|
+
root = headlessNode("root");
|
|
198
|
+
const descriptor: EditorDescriptor = { key: "body", type: "editor", attrs };
|
|
199
|
+
const host: Component = {
|
|
200
|
+
render: () =>
|
|
201
|
+
h(MapoWygEditor, {
|
|
202
|
+
modelValue: modelValue.value,
|
|
203
|
+
descriptor,
|
|
204
|
+
readonly: state.readonly,
|
|
205
|
+
disabled: state.readonly,
|
|
206
|
+
"onUpdate:modelValue": (value: string) => emitted.push(value),
|
|
207
|
+
}),
|
|
208
|
+
};
|
|
209
|
+
app = createApp(host);
|
|
210
|
+
for (const [name, stub] of Object.entries(stubs)) app.component(name, stub);
|
|
211
|
+
app.mount(root);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
beforeEach(() => {
|
|
215
|
+
vi.useFakeTimers();
|
|
216
|
+
modelValue = ref("<p>Hello <strong>world</strong></p><h2>Title</h2>");
|
|
217
|
+
emitted = [];
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
afterEach(() => {
|
|
221
|
+
app?.unmount();
|
|
222
|
+
app = undefined;
|
|
223
|
+
vi.useRealTimers();
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
describe("MapoWygEditor without the new attrs", () => {
|
|
227
|
+
it("renders the built-in toolbar only, enabled, and the editor", () => {
|
|
228
|
+
mount();
|
|
229
|
+
|
|
230
|
+
expect(buttons()).toHaveLength(10);
|
|
231
|
+
expect(buttons().every((node) => !node.props.disabled)).toBe(true);
|
|
232
|
+
expect(sourceToggle()).toBeUndefined();
|
|
233
|
+
expect(textarea()).toBeUndefined();
|
|
234
|
+
expect(isHidden(editorContent())).toBe(false);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it("disables the built-in toolbar when read-only", () => {
|
|
238
|
+
mount({}, { readonly: true });
|
|
239
|
+
|
|
240
|
+
expect(buttons().every((node) => node.props.disabled)).toBe(true);
|
|
241
|
+
});
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
describe("MapoWygEditor source view", () => {
|
|
245
|
+
it("shows the editor's HTML laid out in a textarea, keeping the editor mounted", async () => {
|
|
246
|
+
mount({ source: true });
|
|
247
|
+
|
|
248
|
+
click(sourceToggle());
|
|
249
|
+
await nextTick();
|
|
250
|
+
|
|
251
|
+
expect(textarea()!.props.value).toBe(
|
|
252
|
+
"<p>Hello <strong>world</strong></p>\n<h2>Title</h2>",
|
|
253
|
+
);
|
|
254
|
+
expect(isHidden(editorContent())).toBe(true);
|
|
255
|
+
expect(sourceToggle()!.props["aria-pressed"]).toBe(true);
|
|
256
|
+
expect(fakeEditor().destroy).not.toHaveBeenCalled();
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
it("disables every other toolbar button and keeps the toggle active", async () => {
|
|
260
|
+
mount({ source: true });
|
|
261
|
+
|
|
262
|
+
click(sourceToggle());
|
|
263
|
+
await nextTick();
|
|
264
|
+
|
|
265
|
+
const others = buttons().filter((node) => node !== sourceToggle());
|
|
266
|
+
expect(others.every((node) => node.props.disabled)).toBe(true);
|
|
267
|
+
expect(sourceToggle()!.props.disabled).toBeFalsy();
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
it("writes the typing into the editor after a pause and emits the editor's HTML", async () => {
|
|
271
|
+
mount({ source: true });
|
|
272
|
+
click(sourceToggle());
|
|
273
|
+
await nextTick();
|
|
274
|
+
|
|
275
|
+
typeSource("<p>One</p>\n<p>Two <span>words</span></p>");
|
|
276
|
+
expect(fakeEditor().commands.setContent).not.toHaveBeenCalled();
|
|
277
|
+
vi.advanceTimersByTime(250);
|
|
278
|
+
|
|
279
|
+
expect(fakeEditor().commands.setContent).toHaveBeenCalledWith(
|
|
280
|
+
"<p>One</p>\n<p>Two <span>words</span></p>",
|
|
281
|
+
false,
|
|
282
|
+
);
|
|
283
|
+
// Normalized by the editor: the layout whitespace and the unsupported tag are gone.
|
|
284
|
+
expect(emitted).toEqual(["<p>One</p><p>Two words</p>"]);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
it("passes the source through the sanitizer, so script, handlers and javascript: links never reach the value", async () => {
|
|
288
|
+
mount({ source: true });
|
|
289
|
+
click(sourceToggle());
|
|
290
|
+
await nextTick();
|
|
291
|
+
|
|
292
|
+
typeSource(
|
|
293
|
+
'<p onclick="steal()">Hi</p><script>alert(1)</script><p><a href="javascript:alert(1)">link</a></p>',
|
|
294
|
+
);
|
|
295
|
+
vi.advanceTimersByTime(250);
|
|
296
|
+
|
|
297
|
+
expect(fakeEditor().commands.setContent).toHaveBeenCalledWith(
|
|
298
|
+
"<p>Hi</p><p><a>link</a></p>",
|
|
299
|
+
false,
|
|
300
|
+
);
|
|
301
|
+
expect(emitted).toEqual(["<p>Hi</p><p><a>link</a></p>"]);
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
it("emits nothing when the typing leaves the document as it was", async () => {
|
|
305
|
+
mount({ source: true });
|
|
306
|
+
click(sourceToggle());
|
|
307
|
+
await nextTick();
|
|
308
|
+
|
|
309
|
+
typeSource("<p>Hello <strong>world</strong></p>\n\n<h2>Title</h2> ");
|
|
310
|
+
vi.advanceTimersByTime(250);
|
|
311
|
+
|
|
312
|
+
expect(emitted).toEqual([]);
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
it("goes back to the editor, already up to date, and round-trips again", async () => {
|
|
316
|
+
mount({ source: true });
|
|
317
|
+
click(sourceToggle());
|
|
318
|
+
await nextTick();
|
|
319
|
+
typeSource("<ul><li><p>Item</p></li></ul>");
|
|
320
|
+
|
|
321
|
+
// Leaving before the pause still applies the typing.
|
|
322
|
+
click(sourceToggle());
|
|
323
|
+
await nextTick();
|
|
324
|
+
|
|
325
|
+
expect(emitted).toEqual(["<ul><li><p>Item</p></li></ul>"]);
|
|
326
|
+
expect(textarea()).toBeUndefined();
|
|
327
|
+
expect(isHidden(editorContent())).toBe(false);
|
|
328
|
+
expect(fakeEditor().getHTML()).toBe("<ul><li><p>Item</p></li></ul>");
|
|
329
|
+
|
|
330
|
+
click(sourceToggle());
|
|
331
|
+
await nextTick();
|
|
332
|
+
|
|
333
|
+
expect(textarea()!.props.value).toBe(
|
|
334
|
+
"<ul>\n <li>\n <p>Item</p>\n </li>\n</ul>",
|
|
335
|
+
);
|
|
336
|
+
});
|
|
337
|
+
|
|
338
|
+
it("does not rewrite the textarea when the field's own value comes back", async () => {
|
|
339
|
+
mount({ source: true });
|
|
340
|
+
click(sourceToggle());
|
|
341
|
+
await nextTick();
|
|
342
|
+
|
|
343
|
+
typeSource("<p>Typed</p>\n");
|
|
344
|
+
vi.advanceTimersByTime(250);
|
|
345
|
+
modelValue.value = emitted[0]!;
|
|
346
|
+
await nextTick();
|
|
347
|
+
|
|
348
|
+
expect(textarea()!.props.value).toBe("<p>Typed</p>\n");
|
|
349
|
+
expect(fakeEditor().commands.setContent).toHaveBeenCalledTimes(1);
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
it("shows a value set from outside, dropping the typing it replaces", async () => {
|
|
353
|
+
mount({ source: true });
|
|
354
|
+
click(sourceToggle());
|
|
355
|
+
await nextTick();
|
|
356
|
+
typeSource("<p>Not saved</p>");
|
|
357
|
+
|
|
358
|
+
modelValue.value = "<p>Draft</p><p>restored</p>";
|
|
359
|
+
await nextTick();
|
|
360
|
+
vi.advanceTimersByTime(250);
|
|
361
|
+
|
|
362
|
+
expect(textarea()!.props.value).toBe("<p>Draft</p>\n<p>restored</p>");
|
|
363
|
+
expect(fakeEditor().getHTML()).toBe("<p>Draft</p><p>restored</p>");
|
|
364
|
+
expect(emitted).toEqual([]);
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it("applies the typing at once when the textarea loses the focus", async () => {
|
|
368
|
+
mount({ source: true });
|
|
369
|
+
click(sourceToggle());
|
|
370
|
+
await nextTick();
|
|
371
|
+
|
|
372
|
+
typeSource("<p>Saved right away</p>");
|
|
373
|
+
(textarea()!.props.onBlur as () => void)();
|
|
374
|
+
|
|
375
|
+
expect(emitted).toEqual(["<p>Saved right away</p>"]);
|
|
376
|
+
vi.advanceTimersByTime(250);
|
|
377
|
+
expect(emitted).toHaveLength(1);
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
it("applies pending typing when the field unmounts", async () => {
|
|
381
|
+
mount({ source: true });
|
|
382
|
+
click(sourceToggle());
|
|
383
|
+
await nextTick();
|
|
384
|
+
|
|
385
|
+
typeSource("<p>Last words</p>");
|
|
386
|
+
app!.unmount();
|
|
387
|
+
app = undefined;
|
|
388
|
+
|
|
389
|
+
expect(emitted).toEqual(["<p>Last words</p>"]);
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
it("shows the source of a read-only field without letting it change", async () => {
|
|
393
|
+
mount({ source: true }, { readonly: true });
|
|
394
|
+
|
|
395
|
+
expect(sourceToggle()!.props.disabled).toBeFalsy();
|
|
396
|
+
click(sourceToggle());
|
|
397
|
+
await nextTick();
|
|
398
|
+
|
|
399
|
+
expect(textarea()!.props.readonly).toBe(true);
|
|
400
|
+
typeSource("<p>Changed</p>");
|
|
401
|
+
vi.advanceTimersByTime(250);
|
|
402
|
+
|
|
403
|
+
expect(textarea()!.props.value).toBe(
|
|
404
|
+
"<p>Hello <strong>world</strong></p>\n<h2>Title</h2>",
|
|
405
|
+
);
|
|
406
|
+
expect(fakeEditor().commands.setContent).not.toHaveBeenCalled();
|
|
407
|
+
expect(emitted).toEqual([]);
|
|
408
|
+
});
|
|
409
|
+
});
|
|
410
|
+
|
|
411
|
+
describe("MapoWygEditor custom toolbar buttons", () => {
|
|
412
|
+
/** The toolbar's elements in order, without the anchors of fragments. */
|
|
413
|
+
const toolbarRow = () =>
|
|
414
|
+
all(root)
|
|
415
|
+
.find((node) => node.children.some((child) => child.type === "tooltip"))!
|
|
416
|
+
.children.filter((node) => !node.type.startsWith("#"));
|
|
417
|
+
const tooltipOf = (label: string) =>
|
|
418
|
+
all(root).find(
|
|
419
|
+
(node) => node.type === "tooltip" && node.props.text === label,
|
|
420
|
+
);
|
|
421
|
+
const buttonOf = (label: string) =>
|
|
422
|
+
buttons().find((node) => node.props["aria-label"] === label);
|
|
423
|
+
const hasActiveClass = (node: HeadlessNode | undefined) =>
|
|
424
|
+
String(node!.props.class ?? "").includes("bg-gray-200");
|
|
425
|
+
|
|
426
|
+
it("renders the buttons after the built-in ones, past a separator", () => {
|
|
427
|
+
mount({
|
|
428
|
+
toolbar: [
|
|
429
|
+
{
|
|
430
|
+
key: "table",
|
|
431
|
+
icon: "i-lucide-table",
|
|
432
|
+
label: "Insert table",
|
|
433
|
+
action: () => {},
|
|
434
|
+
},
|
|
435
|
+
{ key: "quote", label: "Quote", action: () => {} },
|
|
436
|
+
],
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
const row = toolbarRow();
|
|
440
|
+
const table = row.indexOf(tooltipOf("Insert table")!);
|
|
441
|
+
expect(row[table - 1]!.type).toBe("separator");
|
|
442
|
+
expect(row[table - 2]).toBe(tooltipOf("mapo.wygEditor.insertImage"));
|
|
443
|
+
expect(row[table + 1]).toBe(tooltipOf("Quote"));
|
|
444
|
+
expect(buttons()).toHaveLength(12);
|
|
445
|
+
|
|
446
|
+
expect(buttonOf("Insert table")!.props.icon).toBe("i-lucide-table");
|
|
447
|
+
// A button with no icon shows its label.
|
|
448
|
+
expect(buttonOf("Quote")!.props.label).toBe("Quote");
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it("runs the action with the editor on click", () => {
|
|
452
|
+
const action = vi.fn();
|
|
453
|
+
mount({ toolbar: [{ key: "x", label: "X", action }] });
|
|
454
|
+
|
|
455
|
+
click(buttonOf("X"));
|
|
456
|
+
|
|
457
|
+
expect(action).toHaveBeenCalledWith(fakeEditor());
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
it("shows the active state and disables the button as its predicates say", () => {
|
|
461
|
+
mount({
|
|
462
|
+
toolbar: [
|
|
463
|
+
{
|
|
464
|
+
key: "on",
|
|
465
|
+
label: "On",
|
|
466
|
+
action: () => {},
|
|
467
|
+
isActive: () => true,
|
|
468
|
+
isDisabled: () => false,
|
|
469
|
+
},
|
|
470
|
+
{ key: "off", label: "Off", action: () => {}, isDisabled: () => true },
|
|
471
|
+
],
|
|
472
|
+
});
|
|
473
|
+
|
|
474
|
+
expect(hasActiveClass(buttonOf("On"))).toBe(true);
|
|
475
|
+
expect(buttonOf("On")!.props.disabled).toBe(false);
|
|
476
|
+
expect(hasActiveClass(buttonOf("Off"))).toBe(false);
|
|
477
|
+
expect(buttonOf("Off")!.props.disabled).toBe(true);
|
|
478
|
+
});
|
|
479
|
+
|
|
480
|
+
it("disables the buttons when read-only and in the source view", async () => {
|
|
481
|
+
const toolbar = [{ key: "x", label: "X", action: () => {} }];
|
|
482
|
+
mount({ toolbar }, { readonly: true });
|
|
483
|
+
expect(buttonOf("X")!.props.disabled).toBe(true);
|
|
484
|
+
app!.unmount();
|
|
485
|
+
|
|
486
|
+
mount({ toolbar, source: true });
|
|
487
|
+
expect(buttonOf("X")!.props.disabled).toBe(false);
|
|
488
|
+
click(sourceToggle());
|
|
489
|
+
await nextTick();
|
|
490
|
+
expect(buttonOf("X")!.props.disabled).toBe(true);
|
|
491
|
+
});
|
|
492
|
+
|
|
493
|
+
it("keeps the source toggle last, after the custom buttons", () => {
|
|
494
|
+
mount({
|
|
495
|
+
toolbar: [{ key: "x", label: "X", action: () => {} }],
|
|
496
|
+
source: true,
|
|
497
|
+
});
|
|
498
|
+
|
|
499
|
+
const row = toolbarRow();
|
|
500
|
+
expect(row.at(-1)).toBe(tooltipOf("mapo.wygEditor.source"));
|
|
501
|
+
expect(row.at(-2)!.type).toBe("separator");
|
|
502
|
+
expect(row.at(-3)).toBe(tooltipOf("X"));
|
|
503
|
+
});
|
|
504
|
+
|
|
505
|
+
it("treats a predicate that throws as inactive and disabled, without breaking the toolbar", () => {
|
|
506
|
+
const missingCommand = () => {
|
|
507
|
+
throw new TypeError("addRowAfter is not a function");
|
|
508
|
+
};
|
|
509
|
+
mount({
|
|
510
|
+
toolbar: [
|
|
511
|
+
{
|
|
512
|
+
key: "row",
|
|
513
|
+
label: "Add row",
|
|
514
|
+
action: () => {},
|
|
515
|
+
isActive: missingCommand,
|
|
516
|
+
isDisabled: missingCommand,
|
|
517
|
+
},
|
|
518
|
+
],
|
|
519
|
+
});
|
|
520
|
+
|
|
521
|
+
expect(hasActiveClass(buttonOf("Add row"))).toBe(false);
|
|
522
|
+
expect(buttonOf("Add row")!.props.disabled).toBe(true);
|
|
523
|
+
expect(buttons()).toHaveLength(11);
|
|
524
|
+
});
|
|
525
|
+
});
|
|
@@ -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
|
+
});
|