Fix editing of non-html replies (#8418)

This commit is contained in:
Michael Telatynski 2022-04-27 09:43:10 +01:00 committed by GitHub
parent e718242912
commit 83ab266533
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 4 deletions

View file

@ -16,12 +16,14 @@ limitations under the License.
*/ */
import { MatrixEvent } from "matrix-js-sdk/src/models/event"; import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { MsgType } from "matrix-js-sdk/src/@types/event";
import { checkBlockNode } from "../HtmlUtils"; import { checkBlockNode } from "../HtmlUtils";
import { getPrimaryPermalinkEntity } from "../utils/permalinks/Permalinks"; import { getPrimaryPermalinkEntity } from "../utils/permalinks/Permalinks";
import { Part, PartCreator, Type } from "./parts"; import { Part, PartCreator, Type } from "./parts";
import SdkConfig from "../SdkConfig"; import SdkConfig from "../SdkConfig";
import { textToHtmlRainbow } from "../utils/colour"; import { textToHtmlRainbow } from "../utils/colour";
import { stripPlainReply } from "../utils/Reply";
const LIST_TYPES = ["UL", "OL", "LI"]; const LIST_TYPES = ["UL", "OL", "LI"];
@ -288,7 +290,7 @@ export function parsePlainTextMessage(
export function parseEvent(event: MatrixEvent, pc: PartCreator, opts: IParseOptions = { shouldEscape: true }) { export function parseEvent(event: MatrixEvent, pc: PartCreator, opts: IParseOptions = { shouldEscape: true }) {
const content = event.getContent(); const content = event.getContent();
let parts: Part[]; let parts: Part[];
const isEmote = content.msgtype === "m.emote"; const isEmote = content.msgtype === MsgType.Emote;
let isRainbow = false; let isRainbow = false;
if (content.format === "org.matrix.custom.html") { if (content.format === "org.matrix.custom.html") {
@ -297,7 +299,11 @@ export function parseEvent(event: MatrixEvent, pc: PartCreator, opts: IParseOpti
isRainbow = true; isRainbow = true;
} }
} else { } 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) { if (isEmote && isRainbow) {

View file

@ -20,7 +20,7 @@ import { createPartCreator } from "./mock";
const FOUR_SPACES = " ".repeat(4); const FOUR_SPACES = " ".repeat(4);
function htmlMessage(formattedBody, msgtype = "m.text") { function htmlMessage(formattedBody: string, msgtype = "m.text") {
return { return {
getContent() { getContent() {
return { return {
@ -32,7 +32,7 @@ function htmlMessage(formattedBody, msgtype = "m.text") {
} as unknown as MatrixEvent; } as unknown as MatrixEvent;
} }
function textMessage(body, msgtype = "m.text") { function textMessage(body: string, msgtype = "m.text") {
return { return {
getContent() { getContent() {
return { return {
@ -43,6 +43,13 @@ function textMessage(body, msgtype = "m.text") {
} as unknown as MatrixEvent; } 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) { function mergeAdjacentParts(parts) {
let prevPart; let prevPart;
for (let i = 0; i < parts.length; ++i) { for (let i = 0; i < parts.length; ++i) {
@ -404,5 +411,14 @@ describe('editor/deserialize', function() {
text: "> <del>no formatting here</del>", 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",
});
});
}); });
}); });