Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 83ab266

Browse files
authored
Fix editing of non-html replies (#8418)
1 parent e718242 commit 83ab266

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

src/editor/deserialize.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ limitations under the License.
1616
*/
1717

1818
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
19+
import { MsgType } from "matrix-js-sdk/src/@types/event";
1920

2021
import { checkBlockNode } from "../HtmlUtils";
2122
import { getPrimaryPermalinkEntity } from "../utils/permalinks/Permalinks";
2223
import { Part, PartCreator, Type } from "./parts";
2324
import SdkConfig from "../SdkConfig";
2425
import { textToHtmlRainbow } from "../utils/colour";
26+
import { stripPlainReply } from "../utils/Reply";
2527

2628
const LIST_TYPES = ["UL", "OL", "LI"];
2729

@@ -288,7 +290,7 @@ export function parsePlainTextMessage(
288290
export function parseEvent(event: MatrixEvent, pc: PartCreator, opts: IParseOptions = { shouldEscape: true }) {
289291
const content = event.getContent();
290292
let parts: Part[];
291-
const isEmote = content.msgtype === "m.emote";
293+
const isEmote = content.msgtype === MsgType.Emote;
292294
let isRainbow = false;
293295

294296
if (content.format === "org.matrix.custom.html") {
@@ -297,7 +299,11 @@ export function parseEvent(event: MatrixEvent, pc: PartCreator, opts: IParseOpti
297299
isRainbow = true;
298300
}
299301
} else {
300-
parts = parsePlainTextMessage(content.body || "", pc, opts);
302+
let body = content.body || "";
303+
if (event.replyEventId) {
304+
body = stripPlainReply(body);
305+
}
306+
parts = parsePlainTextMessage(body, pc, opts);
301307
}
302308

303309
if (isEmote && isRainbow) {

test/editor/deserialize-test.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { createPartCreator } from "./mock";
2020

2121
const FOUR_SPACES = " ".repeat(4);
2222

23-
function htmlMessage(formattedBody, msgtype = "m.text") {
23+
function htmlMessage(formattedBody: string, msgtype = "m.text") {
2424
return {
2525
getContent() {
2626
return {
@@ -32,7 +32,7 @@ function htmlMessage(formattedBody, msgtype = "m.text") {
3232
} as unknown as MatrixEvent;
3333
}
3434

35-
function textMessage(body, msgtype = "m.text") {
35+
function textMessage(body: string, msgtype = "m.text") {
3636
return {
3737
getContent() {
3838
return {
@@ -43,6 +43,13 @@ function textMessage(body, msgtype = "m.text") {
4343
} as unknown as MatrixEvent;
4444
}
4545

46+
function textMessageReply(body: string, msgtype = "m.text") {
47+
return {
48+
...textMessage(body, msgtype),
49+
replyEventId: "!foo:bar",
50+
} as unknown as MatrixEvent;
51+
}
52+
4653
function mergeAdjacentParts(parts) {
4754
let prevPart;
4855
for (let i = 0; i < parts.length; ++i) {
@@ -404,5 +411,14 @@ describe('editor/deserialize', function() {
404411
text: "> <del>no formatting here</del>",
405412
});
406413
});
414+
it("it strips plaintext replies", () => {
415+
const body = "> Sender: foo\n\nMessage";
416+
const parts = normalize(parseEvent(textMessageReply(body), createPartCreator(), { shouldEscape: false }));
417+
expect(parts.length).toBe(1);
418+
expect(parts[0]).toStrictEqual({
419+
type: "plain",
420+
text: "Message",
421+
});
422+
});
407423
});
408424
});

0 commit comments

Comments
 (0)