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

Commit

Permalink
Fix editing of non-html replies (#8418)
Browse files Browse the repository at this point in the history
  • Loading branch information
t3chguy authored Apr 27, 2022
1 parent e718242 commit 83ab266
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/editor/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"];

Expand Down Expand Up @@ -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") {
Expand All @@ -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) {
Expand Down
20 changes: 18 additions & 2 deletions test/editor/deserialize-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -404,5 +411,14 @@ describe('editor/deserialize', function() {
text: "> <del>no formatting here</del>",
});
});
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",
});
});
});
});

0 comments on commit 83ab266

Please sign in to comment.