From 9c1939b75679980f14f3ee550af00e65346c1fd0 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 15 Apr 2020 02:31:30 +0100 Subject: [PATCH] match all, not just first instance of tokens to escape Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- src/editor/deserialize.ts | 2 +- src/editor/serialize.ts | 2 +- test/editor/deserialize-test.js | 4 ++-- test/editor/serialize-test.js | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/editor/deserialize.ts b/src/editor/deserialize.ts index 5322f09f11..48d1d98ae4 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(/[[\\\]]/g, c => "\\" + c)}](${href})`); } } } diff --git a/src/editor/serialize.ts b/src/editor/serialize.ts index d501bdd47e..4d0b8cd03a 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(/[[\\\]]/g, c => "\\" + c)}](${makeGenericPermalink(part.resourceId)})`; } }, ""); } diff --git a/test/editor/deserialize-test.js b/test/editor/deserialize-test.js index fb97d75752..2bd5d7e4c6 100644 --- a/test/editor/deserialize-test.js +++ b/test/editor/deserialize-test.js @@ -157,11 +157,11 @@ describe('editor/deserialize', function() { expect(parts[2]).toStrictEqual({type: "plain", text: "!"}); }); it('user pill with displayname containing opening square bracket', function() { - const html = "Hi Alice[!"; + 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[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() { diff --git a/test/editor/serialize-test.js b/test/editor/serialize-test.js index a114f89de2..bd26ae91bb 100644 --- a/test/editor/serialize-test.js +++ b/test/editor/serialize-test.js @@ -51,9 +51,9 @@ describe('editor/serialize', function() { }); it('displaynames containing an opening square bracket work', function() { const pc = createPartCreator(); - const model = new EditorModel([pc.userPill("Displayname[", "@user:server")]); + const model = new EditorModel([pc.userPill("Displayname[[", "@user:server")]); const html = htmlSerializeIfNeeded(model, {}); - expect(html).toBe("Displayname["); + expect(html).toBe("Displayname[["); }); it('displaynames containing a closing square bracket work', function() { const pc = createPartCreator();