diff --git a/src/HtmlUtils.tsx b/src/HtmlUtils.tsx
index b518c43973..e7d6ce2890 100644
--- a/src/HtmlUtils.tsx
+++ b/src/HtmlUtils.tsx
@@ -348,7 +348,7 @@ export function bodyToHtml(content: IContent, highlights: Optional, op
isHtmlMessage = !isPlainText;
if (isHtmlMessage && SettingsStore.getValue("feature_latex_maths")) {
- [...phtml.querySelectorAll("div, span[data-mx-maths]")].forEach((e) => {
+ [...phtml.querySelectorAll("div[data-mx-maths], span[data-mx-maths]")].forEach((e) => {
e.outerHTML = katex.renderToString(decode(e.getAttribute("data-mx-maths")), {
throwOnError: false,
displayMode: e.tagName == "DIV",
diff --git a/test/HtmlUtils-test.tsx b/test/HtmlUtils-test.tsx
index f177fc1b47..d9e75faaa9 100644
--- a/test/HtmlUtils-test.tsx
+++ b/test/HtmlUtils-test.tsx
@@ -166,6 +166,16 @@ describe("bodyToHtml", () => {
});
expect(html).toMatchSnapshot();
});
+
+ it("should not mangle divs", () => {
+ const html = getHtml({
+ body: "hello world",
+ msgtype: "m.text",
+ formatted_body: "hello
world
",
+ format: "org.matrix.custom.html",
+ });
+ expect(html).toMatchSnapshot();
+ });
});
});
diff --git a/test/__snapshots__/HtmlUtils-test.tsx.snap b/test/__snapshots__/HtmlUtils-test.tsx.snap
index c4d91467c0..c33cc46433 100644
--- a/test/__snapshots__/HtmlUtils-test.tsx.snap
+++ b/test/__snapshots__/HtmlUtils-test.tsx.snap
@@ -2,6 +2,8 @@
exports[`bodyToHtml feature_latex_maths should not mangle code blocks 1`] = `"hello
$\\xi$
world
"`;
+exports[`bodyToHtml feature_latex_maths should not mangle divs 1`] = `"hello
world
"`;
+
exports[`bodyToHtml feature_latex_maths should render block katex 1`] = `"hello
ξworld
"`;
exports[`bodyToHtml feature_latex_maths should render inline katex 1`] = `"hello ξ world"`;