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

Commit

Permalink
Handle newlines in user pills
Browse files Browse the repository at this point in the history
  • Loading branch information
Johennes committed Jun 29, 2023
1 parent f62fe26 commit b9f5f49
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { logger } from "matrix-js-sdk/src/logger";

import { linkify } from "./linkify-matrix";

const ALLOWED_HTML_TAGS = ["sub", "sup", "del", "u"];
const ALLOWED_HTML_TAGS = ["sub", "sup", "del", "u", "br", "br/"];

// These types of node are definitely text
const TEXT_NODES = ["text", "softbreak", "linebreak", "paragraph", "document"];
Expand All @@ -36,8 +36,8 @@ function isAllowedHtmlTag(node: commonmark.Node): boolean {
return true;
}

// Regex won't work for tags with attrs, but we only
// allow <del> anyway.
// Regex won't work for tags with attrs, but the oens we allow
// shouldn't really have any anyway.
const matches = /^<\/?(.*)>$/.exec(node.literal);
if (matches && matches.length == 2) {
const tag = matches[1];
Expand Down
4 changes: 3 additions & 1 deletion src/editor/serialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ export function mdSerialize(model: EditorModel): string {
case Type.UserPill:
return (
html +
`[${part.text.replace(/[[\\\]]/g, (c) => "\\" + c)}](${makeGenericPermalink(part.resourceId)})`
`[${part.text.replace(/[[\\\]]/g, (c) => "\\" + c).replace(/\n/g, "<br/>")}](${makeGenericPermalink(
part.resourceId,
)})`
);
}
}, "");
Expand Down
8 changes: 8 additions & 0 deletions test/editor/deserialize-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,14 @@ describe("editor/deserialize", function () {
expect(parts[1]).toStrictEqual({ type: "user-pill", text: "Alice]", resourceId: "@alice:hs.tld" });
expect(parts[2]).toStrictEqual({ type: "plain", text: "!" });
});
it("user pill with displayname containing linebreak square bracket", function () {
const html = 'Hi <a href="https://matrix.to/#/@alice:hs.tld">Alice<br/>123</a>!';
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
expect(parts.length).toBe(3);
expect(parts[0]).toStrictEqual({ type: "plain", text: "Hi " });
expect(parts[1]).toStrictEqual({ type: "user-pill", text: "Alice123", resourceId: "@alice:hs.tld" });
expect(parts[2]).toStrictEqual({ type: "plain", text: "!" });
});
it("room pill", function () {
const html = 'Try <a href="https://matrix.to/#/#room:hs.tld">#room:hs.tld</a>?';
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
Expand Down
7 changes: 6 additions & 1 deletion test/editor/serialize-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ describe("editor/serialize", function () {
const html = htmlSerializeIfNeeded(model, {});
expect(html).toBe('<a href="https://matrix.to/#/@user:server">Displayname]</a>');
});
it("displaynames containing a newline work", function () {
const pc = createPartCreator();
const model = new EditorModel([pc.userPill("Display\nname", "@user:server")], pc);
const html = htmlSerializeIfNeeded(model, {});
expect(html).toBe('<a href="https://matrix.to/#/@user:server">Display<br>name</a>');
});
it("escaped markdown should not retain backslashes", function () {
const pc = createPartCreator();
const model = new EditorModel([pc.plain("\\*hello\\* world")], pc);
Expand Down Expand Up @@ -96,7 +102,6 @@ describe("editor/serialize", function () {
const html = htmlSerializeIfNeeded(model, { useMarkdown: false });
expect(html).toBe("\\*hello\\* world &lt; hey world!");
});

it("plaintext remains plaintext even when forcing html", function () {
const pc = createPartCreator();
const model = new EditorModel([pc.plain("hello world")], pc);
Expand Down

0 comments on commit b9f5f49

Please sign in to comment.