2019-07-25 15:27:47 +00:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2022-12-12 11:24:14 +00:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/matrix";
|
2019-07-25 15:27:47 +00:00
|
|
|
|
2021-06-29 12:11:58 +00:00
|
|
|
import { parseEvent } from "../../src/editor/deserialize";
|
2023-02-13 11:39:16 +00:00
|
|
|
import { Part } from "../../src/editor/parts";
|
2021-06-29 12:11:58 +00:00
|
|
|
import { createPartCreator } from "./mock";
|
2019-07-25 15:27:47 +00:00
|
|
|
|
2021-12-07 09:46:04 +00:00
|
|
|
const FOUR_SPACES = " ".repeat(4);
|
|
|
|
|
2022-04-27 08:43:10 +00:00
|
|
|
function htmlMessage(formattedBody: string, msgtype = "m.text") {
|
2019-07-25 15:27:47 +00:00
|
|
|
return {
|
|
|
|
getContent() {
|
|
|
|
return {
|
|
|
|
msgtype,
|
|
|
|
format: "org.matrix.custom.html",
|
|
|
|
formatted_body: formattedBody,
|
|
|
|
};
|
|
|
|
},
|
2022-04-06 11:40:13 +00:00
|
|
|
} as unknown as MatrixEvent;
|
2019-07-25 15:27:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 08:43:10 +00:00
|
|
|
function textMessage(body: string, msgtype = "m.text") {
|
2019-07-25 15:27:47 +00:00
|
|
|
return {
|
|
|
|
getContent() {
|
|
|
|
return {
|
|
|
|
msgtype,
|
|
|
|
body,
|
|
|
|
};
|
|
|
|
},
|
2022-04-06 11:40:13 +00:00
|
|
|
} as unknown as MatrixEvent;
|
2019-07-25 15:27:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 08:43:10 +00:00
|
|
|
function textMessageReply(body: string, msgtype = "m.text") {
|
|
|
|
return {
|
|
|
|
...textMessage(body, msgtype),
|
|
|
|
replyEventId: "!foo:bar",
|
|
|
|
} as unknown as MatrixEvent;
|
|
|
|
}
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
function mergeAdjacentParts(parts: Part[]) {
|
|
|
|
let prevPart: Part | undefined;
|
2019-07-25 15:27:47 +00:00
|
|
|
for (let i = 0; i < parts.length; ++i) {
|
|
|
|
let part = parts[i];
|
|
|
|
const isEmpty = !part.text.length;
|
2023-02-13 11:39:16 +00:00
|
|
|
const isMerged = !isEmpty && prevPart && prevPart.merge?.(part);
|
2019-07-25 15:27:47 +00:00
|
|
|
if (isEmpty || isMerged) {
|
|
|
|
// remove empty or merged part
|
|
|
|
part = prevPart;
|
|
|
|
parts.splice(i, 1);
|
|
|
|
//repeat this index, as it's removed now
|
|
|
|
--i;
|
|
|
|
}
|
|
|
|
prevPart = part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
function normalize(parts: Part[]) {
|
2019-07-25 15:27:47 +00:00
|
|
|
// merge adjacent parts as this will happen
|
|
|
|
// in the model anyway, and whether 1 or multiple
|
|
|
|
// plain parts are returned is an implementation detail
|
|
|
|
mergeAdjacentParts(parts);
|
|
|
|
// convert to data objects for easier asserting
|
2022-12-12 11:24:14 +00:00
|
|
|
return parts.map((p) => p.serialize());
|
2019-07-25 15:27:47 +00:00
|
|
|
}
|
|
|
|
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("editor/deserialize", function () {
|
|
|
|
describe("text messages", function () {
|
|
|
|
it("test with newlines", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const parts = normalize(parseEvent(textMessage("hello\nworld"), createPartCreator()));
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "hello" });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "world" });
|
2019-08-22 13:41:40 +00:00
|
|
|
expect(parts.length).toBe(3);
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("@room pill", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const parts = normalize(parseEvent(textMessage("text message for @room"), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(2);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "text message for " });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "at-room-pill", text: "@room" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("emote", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const text = "says DON'T SHOUT!";
|
|
|
|
const parts = normalize(parseEvent(textMessage(text, "m.emote"), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(1);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "/me says DON'T SHOUT!" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("html messages", function () {
|
|
|
|
it("inline styling", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const html = "<strong>bold</strong> and <em>emphasized</em> text";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(1);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "**bold** and _emphasized_ text" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("hyperlink", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const html = 'click <a href="http://example.com/">this</a>!';
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(1);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "click [this](http://example.com/)!" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("multiple lines with paragraphs", function () {
|
|
|
|
const html = "<p>hello</p><p>world</p>";
|
2019-07-25 15:27:47 +00:00
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
2019-09-23 13:34:01 +00:00
|
|
|
expect(parts.length).toBe(4);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "hello" });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[3]).toStrictEqual({ type: "plain", text: "world" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("multiple lines with line breaks", function () {
|
|
|
|
const html = "hello<br>world";
|
2019-07-25 15:27:47 +00:00
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(3);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "hello" });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "world" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("multiple lines mixing paragraphs and line breaks", function () {
|
|
|
|
const html = "<p>hello<br>warm</p><p>world</p>";
|
2019-07-25 15:27:47 +00:00
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
2019-09-23 13:34:01 +00:00
|
|
|
expect(parts.length).toBe(6);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "hello" });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "warm" });
|
|
|
|
expect(parts[3]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[4]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[5]).toStrictEqual({ type: "plain", text: "world" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("quote", function () {
|
|
|
|
const html = "<blockquote><p><em>wise</em><br><strong>words</strong></p></blockquote><p>indeed</p>";
|
2019-07-25 15:27:47 +00:00
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(6);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "> _wise_" });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "> **words**" });
|
|
|
|
expect(parts[3]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[4]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[5]).toStrictEqual({ type: "plain", text: "indeed" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("user pill", function () {
|
|
|
|
const html = 'Hi <a href="https://matrix.to/#/@alice:hs.tld">Alice</a>!';
|
2019-07-25 15:27:47 +00:00
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(3);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "Hi " });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "user-pill", text: "Alice", resourceId: "@alice:hs.tld" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "!" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("user pill with displayname containing backslash", function () {
|
|
|
|
const html = 'Hi <a href="https://matrix.to/#/@alice:hs.tld">Alice\\</a>!';
|
2020-04-14 23:40:38 +00:00
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(3);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "Hi " });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "user-pill", text: "Alice\\", resourceId: "@alice:hs.tld" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "!" });
|
2020-04-14 23:40:38 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("user pill with displayname containing opening square bracket", function () {
|
|
|
|
const html = 'Hi <a href="https://matrix.to/#/@alice:hs.tld">Alice[[</a>!';
|
2020-04-15 00:02:08 +00:00
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(3);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "Hi " });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "user-pill", text: "Alice[[", resourceId: "@alice:hs.tld" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "!" });
|
2020-04-15 00:02:08 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("user pill with displayname containing closing square bracket", function () {
|
|
|
|
const html = 'Hi <a href="https://matrix.to/#/@alice:hs.tld">Alice]</a>!';
|
2020-04-14 23:40:38 +00:00
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(3);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "Hi " });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "user-pill", text: "Alice]", resourceId: "@alice:hs.tld" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "!" });
|
2020-04-14 23:40:38 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("room pill", function () {
|
|
|
|
const html = 'Try <a href="https://matrix.to/#/#room:hs.tld">#room:hs.tld</a>?';
|
2019-07-25 15:27:47 +00:00
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(3);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "Try " });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "room-pill", text: "#room:hs.tld", resourceId: "#room:hs.tld" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "?" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("@room pill", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const html = "<em>formatted</em> message for @room";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(2);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "_formatted_ message for " });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "at-room-pill", text: "@room" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("inline code", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const html = "there is no place like <code>127.0.0.1</code>!";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(1);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "there is no place like `127.0.0.1`!" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("code block with no trailing text", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const html = "<pre><code>0xDEADBEEF\n</code></pre>\n";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(5);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "```" });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "0xDEADBEEF" });
|
|
|
|
expect(parts[3]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[4]).toStrictEqual({ type: "plain", text: "```" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2020-08-03 15:02:26 +00:00
|
|
|
// failing likely because of https://github.com/vector-im/element-web/issues/10316
|
2022-12-12 11:24:14 +00:00
|
|
|
xit("code block with no trailing text and no newlines", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const html = "<pre><code>0xDEADBEEF</code></pre>";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(5);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "```" });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "0xDEADBEEF" });
|
|
|
|
expect(parts[3]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[4]).toStrictEqual({ type: "plain", text: "```" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("unordered lists", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const html = "<ul><li>Oak</li><li>Spruce</li><li>Birch</li></ul>";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(5);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "- Oak" });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "- Spruce" });
|
|
|
|
expect(parts[3]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[4]).toStrictEqual({ type: "plain", text: "- Birch" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("ordered lists", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const html = "<ol><li>Start</li><li>Continue</li><li>Finish</li></ol>";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(5);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "1. Start" });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: "2. Continue" });
|
|
|
|
expect(parts[3]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[4]).toStrictEqual({ type: "plain", text: "3. Finish" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("nested unordered lists", () => {
|
2021-12-07 09:46:04 +00:00
|
|
|
const html = "<ul><li>Oak<ul><li>Spruce<ul><li>Birch</li></ul></li></ul></li></ul>";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(5);
|
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "- Oak" });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: `${FOUR_SPACES}- Spruce` });
|
|
|
|
expect(parts[3]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[4]).toStrictEqual({ type: "plain", text: `${FOUR_SPACES.repeat(2)}- Birch` });
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("nested ordered lists", () => {
|
2021-12-07 09:46:04 +00:00
|
|
|
const html = "<ol><li>Oak<ol><li>Spruce<ol><li>Birch</li></ol></li></ol></li></ol>";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(5);
|
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "1. Oak" });
|
|
|
|
expect(parts[1]).toStrictEqual({ type: "newline", text: "\n" });
|
2021-12-21 10:07:44 +00:00
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: `${FOUR_SPACES}1. Spruce` });
|
|
|
|
expect(parts[3]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[4]).toStrictEqual({ type: "plain", text: `${FOUR_SPACES.repeat(2)}1. Birch` });
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("nested lists", () => {
|
2021-12-21 10:07:44 +00:00
|
|
|
const html = "<ol><li>Oak\n<ol><li>Spruce\n<ol><li>Birch</li></ol></li></ol></li></ol>";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(5);
|
2022-03-09 12:43:05 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "1. Oak\n" });
|
2021-12-21 10:07:44 +00:00
|
|
|
expect(parts[1]).toStrictEqual({ type: "newline", text: "\n" });
|
2022-03-09 12:43:05 +00:00
|
|
|
expect(parts[2]).toStrictEqual({ type: "plain", text: `${FOUR_SPACES}1. Spruce\n` });
|
2021-12-07 09:46:04 +00:00
|
|
|
expect(parts[3]).toStrictEqual({ type: "newline", text: "\n" });
|
|
|
|
expect(parts[4]).toStrictEqual({ type: "plain", text: `${FOUR_SPACES.repeat(2)}1. Birch` });
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("mx-reply is stripped", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const html = "<mx-reply>foo</mx-reply>bar";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(1);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "bar" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("emote", function () {
|
2019-07-25 15:27:47 +00:00
|
|
|
const html = "says <em>DON'T SHOUT</em>!";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html, "m.emote"), createPartCreator()));
|
|
|
|
expect(parts.length).toBe(1);
|
2021-06-29 12:11:58 +00:00
|
|
|
expect(parts[0]).toStrictEqual({ type: "plain", text: "/me says _DON'T SHOUT_!" });
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("preserves nested quotes", () => {
|
2022-03-09 12:43:05 +00:00
|
|
|
const html = "<blockquote>foo<blockquote>bar</blockquote></blockquote>";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts).toMatchSnapshot();
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("surrounds lists with newlines", () => {
|
2022-03-09 12:43:05 +00:00
|
|
|
const html = "foo<ul><li>bar</li></ul>baz";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts).toMatchSnapshot();
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("preserves nested formatting", () => {
|
2022-03-09 12:43:05 +00:00
|
|
|
const html = "a<sub>b<em>c<strong>d<u>e</u></strong></em></sub>";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts).toMatchSnapshot();
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("escapes backticks in code blocks", () => {
|
|
|
|
const html =
|
|
|
|
"<p><code>this → ` is a backtick</code></p>" + "<pre><code>and here are 3 of them:\n```</code></pre>";
|
2022-03-09 12:43:05 +00:00
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts).toMatchSnapshot();
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("escapes backticks outside of code blocks", () => {
|
2022-03-09 12:43:05 +00:00
|
|
|
const html = "some `backticks`";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts).toMatchSnapshot();
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("escapes backslashes", () => {
|
2022-03-09 12:43:05 +00:00
|
|
|
const html = "C:\\My Documents";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts).toMatchSnapshot();
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("escapes asterisks", () => {
|
2022-03-09 12:43:05 +00:00
|
|
|
const html = "*hello*";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts).toMatchSnapshot();
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("escapes underscores", () => {
|
2022-03-09 12:43:05 +00:00
|
|
|
const html = "__emphasis__";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts).toMatchSnapshot();
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("escapes square brackets", () => {
|
2022-03-09 12:43:05 +00:00
|
|
|
const html = "[not an actual link](https://example.org)";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts).toMatchSnapshot();
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("escapes angle brackets", () => {
|
2022-03-09 12:43:05 +00:00
|
|
|
const html = "> \\<del>no formatting here\\</del>";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
|
|
|
|
expect(parts).toMatchSnapshot();
|
|
|
|
});
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
describe("plaintext messages", function () {
|
|
|
|
it("turns html tags back into markdown", function () {
|
|
|
|
const html = '<strong>bold</strong> and <em>emphasized</em> text <a href="http://example.com/">this</a>!';
|
2022-04-19 13:53:59 +00:00
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
|
|
|
|
expect(parts.length).toBe(1);
|
|
|
|
expect(parts[0]).toStrictEqual({
|
|
|
|
type: "plain",
|
|
|
|
text: "**bold** and _emphasized_ text [this](http://example.com/)!",
|
|
|
|
});
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("keeps backticks unescaped", () => {
|
2022-04-19 13:53:59 +00:00
|
|
|
const html = "this → ` is a backtick and here are 3 of them:\n```";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
|
|
|
|
expect(parts.length).toBe(1);
|
|
|
|
expect(parts[0]).toStrictEqual({
|
|
|
|
type: "plain",
|
|
|
|
text: "this → ` is a backtick and here are 3 of them:\n```",
|
|
|
|
});
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("keeps backticks outside of code blocks", () => {
|
2022-04-19 13:53:59 +00:00
|
|
|
const html = "some `backticks`";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
|
|
|
|
expect(parts.length).toBe(1);
|
|
|
|
expect(parts[0]).toStrictEqual({
|
|
|
|
type: "plain",
|
|
|
|
text: "some `backticks`",
|
|
|
|
});
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("keeps backslashes", () => {
|
2022-04-19 13:53:59 +00:00
|
|
|
const html = "C:\\My Documents";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
|
|
|
|
expect(parts.length).toBe(1);
|
|
|
|
expect(parts[0]).toStrictEqual({
|
|
|
|
type: "plain",
|
|
|
|
text: "C:\\My Documents",
|
|
|
|
});
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("keeps asterisks", () => {
|
2022-04-19 13:53:59 +00:00
|
|
|
const html = "*hello*";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
|
|
|
|
expect(parts.length).toBe(1);
|
|
|
|
expect(parts[0]).toStrictEqual({
|
|
|
|
type: "plain",
|
|
|
|
text: "*hello*",
|
|
|
|
});
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("keeps underscores", () => {
|
2022-04-19 13:53:59 +00:00
|
|
|
const html = "__emphasis__";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
|
|
|
|
expect(parts.length).toBe(1);
|
|
|
|
expect(parts[0]).toStrictEqual({
|
|
|
|
type: "plain",
|
|
|
|
text: "__emphasis__",
|
|
|
|
});
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("keeps square brackets", () => {
|
2022-04-19 13:53:59 +00:00
|
|
|
const html = "[not an actual link](https://example.org)";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
|
|
|
|
expect(parts.length).toBe(1);
|
|
|
|
expect(parts[0]).toStrictEqual({
|
|
|
|
type: "plain",
|
|
|
|
text: "[not an actual link](https://example.org)",
|
|
|
|
});
|
|
|
|
});
|
2022-12-12 11:24:14 +00:00
|
|
|
it("escapes angle brackets", () => {
|
2022-04-19 13:53:59 +00:00
|
|
|
const html = "> <del>no formatting here</del>";
|
|
|
|
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator(), { shouldEscape: false }));
|
|
|
|
expect(parts.length).toBe(1);
|
|
|
|
expect(parts[0]).toStrictEqual({
|
|
|
|
type: "plain",
|
|
|
|
text: "> <del>no formatting here</del>",
|
|
|
|
});
|
|
|
|
});
|
2022-04-27 08:43:10 +00:00
|
|
|
it("it strips plaintext replies", () => {
|
|
|
|
const body = "> Sender: foo\n\nMessage";
|
|
|
|
const parts = normalize(parseEvent(textMessageReply(body), createPartCreator(), { shouldEscape: false }));
|
|
|
|
expect(parts.length).toBe(1);
|
|
|
|
expect(parts[0]).toStrictEqual({
|
|
|
|
type: "plain",
|
|
|
|
text: "Message",
|
|
|
|
});
|
|
|
|
});
|
2022-04-19 13:53:59 +00:00
|
|
|
});
|
2019-07-25 15:27:47 +00:00
|
|
|
});
|