diff --git a/src/editor/deserialize.ts b/src/editor/deserialize.ts
index 6680029130..5322f09f11 100644
--- a/src/editor/deserialize.ts
+++ b/src/editor/deserialize.ts
@@ -53,7 +53,7 @@ function parseLink(a: HTMLAnchorElement, partCreator: PartCreator) {
if (href === a.textContent) {
return partCreator.plain(a.textContent);
} else {
- return partCreator.plain(`[${a.textContent.replace(/[\\\]]/, c => "\\" + c)}](${href})`);
+ return partCreator.plain(`[${a.textContent.replace(/[[\\\]]/, c => "\\" + c)}](${href})`);
}
}
}
diff --git a/src/editor/serialize.ts b/src/editor/serialize.ts
index 9ff1cfbd80..d501bdd47e 100644
--- a/src/editor/serialize.ts
+++ b/src/editor/serialize.ts
@@ -31,7 +31,7 @@ export function mdSerialize(model: EditorModel) {
return html + part.text;
case "room-pill":
case "user-pill":
- return html + `[${part.text.replace(/[\\\]]/, c => "\\" + c)}](${makeGenericPermalink(part.resourceId)})`;
+ return html + `[${part.text.replace(/[[\\\]]/, c => "\\" + c)}](${makeGenericPermalink(part.resourceId)})`;
}
}, "");
}
diff --git a/test/editor/deserialize-test.js b/test/editor/deserialize-test.js
index 4184552559..fb97d75752 100644
--- a/test/editor/deserialize-test.js
+++ b/test/editor/deserialize-test.js
@@ -156,6 +156,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 opening square bracket', function() {
+ const html = "Hi Alice[!";
+ 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: "Alice[", resourceId: "@alice:hs.tld"});
+ expect(parts[2]).toStrictEqual({type: "plain", text: "!"});
+ });
it('user pill with displayname containing closing square bracket', function() {
const html = "Hi Alice]!";
const parts = normalize(parseEvent(htmlMessage(html), createPartCreator()));
diff --git a/test/editor/serialize-test.js b/test/editor/serialize-test.js
index a69e3598e3..a114f89de2 100644
--- a/test/editor/serialize-test.js
+++ b/test/editor/serialize-test.js
@@ -49,6 +49,12 @@ describe('editor/serialize', function() {
const html = htmlSerializeIfNeeded(model, {});
expect(html).toBe("Displayname\\");
});
+ it('displaynames containing an opening square bracket work', function() {
+ const pc = createPartCreator();
+ const model = new EditorModel([pc.userPill("Displayname[", "@user:server")]);
+ const html = htmlSerializeIfNeeded(model, {});
+ expect(html).toBe("Displayname[");
+ });
it('displaynames containing a closing square bracket work', function() {
const pc = createPartCreator();
const model = new EditorModel([pc.userPill("Displayname]", "@user:server")]);