2019-07-26 00:47:29 +08:00
|
|
|
/*
|
2024-09-09 21:57:16 +08:00
|
|
|
Copyright 2024 New Vector Ltd.
|
2019-07-26 00:47:29 +08:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
2024-09-09 21:57:16 +08:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2019-07-26 00:47:29 +08:00
|
|
|
*/
|
|
|
|
|
2024-08-01 19:17:44 +08:00
|
|
|
import { mocked } from "jest-mock";
|
|
|
|
|
2019-07-26 00:47:29 +08:00
|
|
|
import EditorModel from "../../src/editor/model";
|
2021-06-29 20:11:58 +08:00
|
|
|
import { htmlSerializeIfNeeded } from "../../src/editor/serialize";
|
|
|
|
import { createPartCreator } from "./mock";
|
2024-08-01 19:17:44 +08:00
|
|
|
import { IConfigOptions } from "../../src/IConfigOptions";
|
2023-05-23 21:31:05 +08:00
|
|
|
import SettingsStore from "../../src/settings/SettingsStore";
|
2024-08-01 19:17:44 +08:00
|
|
|
import SdkConfig from "../../src/SdkConfig";
|
2019-07-26 00:47:29 +08:00
|
|
|
|
|
|
|
describe("editor/serialize", function () {
|
2022-04-19 21:53:59 +08:00
|
|
|
describe("with markdown", function () {
|
|
|
|
it("user pill turns message into html", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.userPill("Alice", "@alice:hs.tld")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBe('<a href="https://matrix.to/#/@alice:hs.tld">Alice</a>');
|
|
|
|
});
|
|
|
|
it("room pill turns message into html", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.roomPill("#room:hs.tld")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBe('<a href="https://matrix.to/#/#room:hs.tld">#room:hs.tld</a>');
|
|
|
|
});
|
|
|
|
it("@room pill turns message into html", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.atRoomPill("@room")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBeFalsy();
|
|
|
|
});
|
|
|
|
it("any markdown turns message into html", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("*hello* world")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBe("<em>hello</em> world");
|
|
|
|
});
|
|
|
|
it("displaynames ending in a backslash work", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.userPill("Displayname\\", "@user:server")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBe('<a href="https://matrix.to/#/@user:server">Displayname\\</a>');
|
|
|
|
});
|
|
|
|
it("displaynames containing an opening square bracket work", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.userPill("Displayname[[", "@user:server")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBe('<a href="https://matrix.to/#/@user:server">Displayname[[</a>');
|
|
|
|
});
|
|
|
|
it("displaynames containing a closing square bracket work", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.userPill("Displayname]", "@user:server")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBe('<a href="https://matrix.to/#/@user:server">Displayname]</a>');
|
|
|
|
});
|
2023-07-06 06:00:27 +08:00
|
|
|
it("displaynames containing a newline work", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.userPill("Display\nname", "@user:server")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBe('<a href="https://matrix.to/#/@user:server">Display<br>name</a>');
|
|
|
|
});
|
2022-04-19 21:53:59 +08:00
|
|
|
it("escaped markdown should not retain backslashes", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("\\*hello\\* world")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBe("*hello* world");
|
|
|
|
});
|
|
|
|
it("escaped markdown should convert HTML entities", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("\\*hello\\* world < hey world!")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBe("*hello* world < hey world!");
|
|
|
|
});
|
2023-08-18 00:37:19 +08:00
|
|
|
|
|
|
|
it("lists with a single empty item are not considered markdown", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
|
|
|
|
const model1 = new EditorModel([pc.plain("-")], pc);
|
|
|
|
const html1 = htmlSerializeIfNeeded(model1, {});
|
|
|
|
expect(html1).toBe(undefined);
|
|
|
|
|
|
|
|
const model2 = new EditorModel([pc.plain("* ")], pc);
|
|
|
|
const html2 = htmlSerializeIfNeeded(model2, {});
|
|
|
|
expect(html2).toBe(undefined);
|
|
|
|
|
|
|
|
const model3 = new EditorModel([pc.plain("2021.")], pc);
|
|
|
|
const html3 = htmlSerializeIfNeeded(model3, {});
|
|
|
|
expect(html3).toBe(undefined);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("lists with a single non-empty item are still markdown", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("2021. foo")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBe('<ol start="2021">\n<li>foo</li>\n</ol>\n');
|
|
|
|
});
|
2024-08-01 19:17:44 +08:00
|
|
|
describe("with permalink_prefix set", function () {
|
|
|
|
const sdkConfigGet = SdkConfig.get;
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SdkConfig, "get").mockImplementation((key: keyof IConfigOptions, altCaseName?: string) => {
|
|
|
|
if (key === "permalink_prefix") {
|
|
|
|
return "https://element.fs.tld";
|
|
|
|
} else return sdkConfigGet(key, altCaseName);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("user pill uses matrix.to", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.userPill("Alice", "@alice:hs.tld")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBe('<a href="https://matrix.to/#/@alice:hs.tld">Alice</a>');
|
|
|
|
});
|
|
|
|
it("room pill uses matrix.to", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.roomPill("#room:hs.tld")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toBe('<a href="https://matrix.to/#/#room:hs.tld">#room:hs.tld</a>');
|
|
|
|
});
|
|
|
|
afterEach(() => {
|
|
|
|
mocked(SdkConfig.get).mockRestore();
|
|
|
|
});
|
|
|
|
});
|
2019-07-26 00:47:29 +08:00
|
|
|
});
|
2023-05-23 21:31:05 +08:00
|
|
|
|
2022-04-19 21:53:59 +08:00
|
|
|
describe("with plaintext", function () {
|
|
|
|
it("markdown remains plaintext", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("*hello* world")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, { useMarkdown: false });
|
|
|
|
expect(html).toBe("*hello* world");
|
|
|
|
});
|
|
|
|
it("markdown should retain backslashes", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("\\*hello\\* world")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, { useMarkdown: false });
|
|
|
|
expect(html).toBe("\\*hello\\* world");
|
|
|
|
});
|
|
|
|
it("markdown should convert HTML entities", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("\\*hello\\* world < hey world!")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, { useMarkdown: false });
|
|
|
|
expect(html).toBe("\\*hello\\* world < hey world!");
|
|
|
|
});
|
2022-06-08 04:20:32 +08:00
|
|
|
it("plaintext remains plaintext even when forcing html", function () {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("hello world")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, { forceHTML: true, useMarkdown: false });
|
|
|
|
expect(html).toBe("hello world");
|
|
|
|
});
|
2020-06-04 05:36:48 +08:00
|
|
|
});
|
2023-05-23 21:31:05 +08:00
|
|
|
|
|
|
|
describe("feature_latex_maths", () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
jest.spyOn(SettingsStore, "getValue").mockImplementation((feature) => feature === "feature_latex_maths");
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should support inline katex", () => {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("hello $\\xi$ world")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toMatchInlineSnapshot(`"hello <span data-mx-maths="\\xi"><code>\\xi</code></span> world"`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should support block katex", () => {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("hello \n$$\\xi$$\n world")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toMatchInlineSnapshot(`
|
|
|
|
"<p>hello</p>
|
|
|
|
<div data-mx-maths="\\xi"><code>\\xi</code></div>
|
|
|
|
<p>world</p>
|
|
|
|
"
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("should not mangle code blocks", () => {
|
|
|
|
const pc = createPartCreator();
|
|
|
|
const model = new EditorModel([pc.plain("hello\n```\n$\\xi$\n```\nworld")], pc);
|
|
|
|
const html = htmlSerializeIfNeeded(model, {});
|
|
|
|
expect(html).toMatchInlineSnapshot(`
|
|
|
|
"<p>hello</p>
|
|
|
|
<pre><code>$\\xi$
|
|
|
|
</code></pre>
|
|
|
|
<p>world</p>
|
|
|
|
"
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
});
|
2019-07-26 00:47:29 +08:00
|
|
|
});
|