/*
Copyright 2024 New Vector Ltd.
Copyright 2019 The Matrix.org Foundation C.I.C.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
Please see LICENSE files in the repository root for full details.
*/
import { mocked } from "jest-mock";
import EditorModel from "../../../src/editor/model";
import { htmlSerializeFromMdIfNeeded, htmlSerializeIfNeeded } from "../../../src/editor/serialize";
import { createPartCreator } from "./mock";
import { IConfigOptions } from "../../../src/IConfigOptions";
import SettingsStore from "../../../src/settings/SettingsStore";
import SdkConfig from "../../../src/SdkConfig";
describe("editor/serialize", function () {
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('Alice');
});
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('#room:hs.tld');
});
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("hello 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('Displayname\\');
});
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('Displayname[[');
});
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('Displayname]');
});
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('Display
name');
});
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 not retain backslashes around other markdown", 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!");
});
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('
\\xi
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(`
"hello
\\xi
world
" `); }); 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(` "hello
$\\xi$
world
" `); }); }); });