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

Fix textual message stripping new line #7239

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions src/HtmlUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,9 @@ export interface IOptsReturnString extends IOpts {
export function bodyToHtml(content: IContent, highlights: string[], opts: IOptsReturnString): string;
export function bodyToHtml(content: IContent, highlights: string[], opts: IOptsReturnNode): ReactNode;
export function bodyToHtml(content: IContent, highlights: string[], opts: IOpts = {}) {
const isHtmlMessage = content.format === "org.matrix.custom.html" && content.formatted_body;
const isFormattedBody = content.format === "org.matrix.custom.html" && content.formatted_body;
let bodyHasEmoji = false;
let isHtmlMessage = false;

let sanitizeParams = sanitizeHtmlParams;
if (opts.forComposerQuote) {
Expand Down Expand Up @@ -449,20 +450,23 @@ export function bodyToHtml(content: IContent, highlights: string[], opts: IOpts
if (opts.stripReplyFallback && formattedBody) formattedBody = ReplyChain.stripHTMLReply(formattedBody);
strippedBody = opts.stripReplyFallback ? ReplyChain.stripPlainReply(plainBody) : plainBody;

bodyHasEmoji = mightContainEmoji(isHtmlMessage ? formattedBody : plainBody);
bodyHasEmoji = mightContainEmoji(isFormattedBody ? formattedBody : plainBody);

// Only generate safeBody if the message was sent as org.matrix.custom.html
if (isHtmlMessage) {
if (isFormattedBody) {
isDisplayedWithHtml = true;

safeBody = sanitizeHtml(formattedBody, sanitizeParams);
const phtml = cheerio.load(safeBody, {
// @ts-ignore: The `_useHtmlParser2` internal option is the
// simplest way to both parse and render using `htmlparser2`.
_useHtmlParser2: true,
decodeEntities: false,
});
const isPlainText = phtml.html() === phtml.root().text();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know that there are some situations where the content body from a message can be plain text while the formatted body doesn't(e.g. mentions). Trying to rely only on the formatted body now.

isHtmlMessage = isFormattedBody && !isPlainText;

if (SettingsStore.getValue("feature_latex_maths")) {
const phtml = cheerio.load(safeBody, {
// @ts-ignore: The `_useHtmlParser2` internal option is the
// simplest way to both parse and render using `htmlparser2`.
_useHtmlParser2: true,
decodeEntities: false,
});
if (isHtmlMessage && SettingsStore.getValue("feature_latex_maths")) {
// @ts-ignore - The types for `replaceWith` wrongly expect
// Cheerio instance to be returned.
phtml('div, span[data-mx-maths!=""]').replaceWith(function(i, e) {
Expand Down
24 changes: 24 additions & 0 deletions test/components/views/messages/TextualBody-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,30 @@ describe("<TextualBody />", () => {
'!ZxbRYPQXDXKGmDnJNg:example.com</a></span> with vias</span>',
);
});

it('renders formatted body without html corretly', () => {
const ev = mkEvent({
type: "m.room.message",
room: "room_id",
user: "sender",
content: {
body: "escaped \\*markdown\\*",
msgtype: "m.text",
format: "org.matrix.custom.html",
formatted_body: "escaped *markdown*",
},
event: true,
});

const wrapper = mount(<TextualBody mxEvent={ev} />);

const content = wrapper.find(".mx_EventTile_body");
expect(content.html()).toBe(
'<span class="mx_EventTile_body" dir="auto">' +
'escaped *markdown*' +
'</span>',
);
});
});

it("renders url previews correctly", () => {
Expand Down