From 83ab266533e3108bc0ea02f99b61ec13b50a18e9 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 27 Apr 2022 09:43:10 +0100 Subject: [PATCH] Fix editing of non-html replies (#8418) --- src/editor/deserialize.ts | 10 ++++++++-- test/editor/deserialize-test.ts | 20 ++++++++++++++++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/editor/deserialize.ts b/src/editor/deserialize.ts index 57ef52cafd..c6c9fc3520 100644 --- a/src/editor/deserialize.ts +++ b/src/editor/deserialize.ts @@ -16,12 +16,14 @@ limitations under the License. */ import { MatrixEvent } from "matrix-js-sdk/src/models/event"; +import { MsgType } from "matrix-js-sdk/src/@types/event"; import { checkBlockNode } from "../HtmlUtils"; import { getPrimaryPermalinkEntity } from "../utils/permalinks/Permalinks"; import { Part, PartCreator, Type } from "./parts"; import SdkConfig from "../SdkConfig"; import { textToHtmlRainbow } from "../utils/colour"; +import { stripPlainReply } from "../utils/Reply"; const LIST_TYPES = ["UL", "OL", "LI"]; @@ -288,7 +290,7 @@ export function parsePlainTextMessage( export function parseEvent(event: MatrixEvent, pc: PartCreator, opts: IParseOptions = { shouldEscape: true }) { const content = event.getContent(); let parts: Part[]; - const isEmote = content.msgtype === "m.emote"; + const isEmote = content.msgtype === MsgType.Emote; let isRainbow = false; if (content.format === "org.matrix.custom.html") { @@ -297,7 +299,11 @@ export function parseEvent(event: MatrixEvent, pc: PartCreator, opts: IParseOpti isRainbow = true; } } else { - parts = parsePlainTextMessage(content.body || "", pc, opts); + let body = content.body || ""; + if (event.replyEventId) { + body = stripPlainReply(body); + } + parts = parsePlainTextMessage(body, pc, opts); } if (isEmote && isRainbow) { diff --git a/test/editor/deserialize-test.ts b/test/editor/deserialize-test.ts index 47ab6cb2f2..a6713b3139 100644 --- a/test/editor/deserialize-test.ts +++ b/test/editor/deserialize-test.ts @@ -20,7 +20,7 @@ import { createPartCreator } from "./mock"; const FOUR_SPACES = " ".repeat(4); -function htmlMessage(formattedBody, msgtype = "m.text") { +function htmlMessage(formattedBody: string, msgtype = "m.text") { return { getContent() { return { @@ -32,7 +32,7 @@ function htmlMessage(formattedBody, msgtype = "m.text") { } as unknown as MatrixEvent; } -function textMessage(body, msgtype = "m.text") { +function textMessage(body: string, msgtype = "m.text") { return { getContent() { return { @@ -43,6 +43,13 @@ function textMessage(body, msgtype = "m.text") { } as unknown as MatrixEvent; } +function textMessageReply(body: string, msgtype = "m.text") { + return { + ...textMessage(body, msgtype), + replyEventId: "!foo:bar", + } as unknown as MatrixEvent; +} + function mergeAdjacentParts(parts) { let prevPart; for (let i = 0; i < parts.length; ++i) { @@ -404,5 +411,14 @@ describe('editor/deserialize', function() { text: "> no formatting here", }); }); + 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", + }); + }); }); });