From fde30577e4b370cf952338f058085ef65ef54972 Mon Sep 17 00:00:00 2001 From: BobVul Date: Fri, 31 Jan 2020 12:33:10 +1100 Subject: [PATCH 1/3] Fix escaped markdown passing backslashes through Fixes https://github.com/vector-im/riot-web/issues/11230 Signed-off-by: Bob Rao --- src/editor/serialize.js | 4 ++++ test/editor/serialize-test.js | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/src/editor/serialize.js b/src/editor/serialize.js index ba380f2809..075c462b37 100644 --- a/src/editor/serialize.js +++ b/src/editor/serialize.js @@ -41,6 +41,10 @@ export function htmlSerializeIfNeeded(model, {forceHTML = false} = {}) { if (!parser.isPlainText() || forceHTML) { return parser.toHTML(); } + // Format "plain" text to ensure removal of backslash escapes + // https://github.com/vector-im/riot-web/issues/11230 + // https://github.com/vector-im/riot-web/issues/2870 + return parser.toPlaintext(); } export function textSerialize(model) { diff --git a/test/editor/serialize-test.js b/test/editor/serialize-test.js index 7517e46437..f846e0d79e 100644 --- a/test/editor/serialize-test.js +++ b/test/editor/serialize-test.js @@ -43,4 +43,10 @@ describe('editor/serialize', function() { const html = htmlSerializeIfNeeded(model, {}); expect(html).toBe("hello world"); }); + it('escaped markdown should not retain backslashes', function() { + const pc = createPartCreator(); + const model = new EditorModel([pc.plain('\\*hello\\* world')]); + const html = htmlSerializeIfNeeded(model, {}); + expect(html).toBe('*hello* world'); + }) }); From 9cf53aa9d89986139c5ea4b7a9447281b882d981 Mon Sep 17 00:00:00 2001 From: Bob Date: Sun, 2 Feb 2020 16:34:19 +1100 Subject: [PATCH 2/3] Only return formatted text when necessary (for escaped chars) Signed-off-by: Bob Rao --- src/editor/serialize.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/editor/serialize.js b/src/editor/serialize.js index 075c462b37..03f3759ea0 100644 --- a/src/editor/serialize.js +++ b/src/editor/serialize.js @@ -44,7 +44,11 @@ export function htmlSerializeIfNeeded(model, {forceHTML = false} = {}) { // Format "plain" text to ensure removal of backslash escapes // https://github.com/vector-im/riot-web/issues/11230 // https://github.com/vector-im/riot-web/issues/2870 - return parser.toPlaintext(); + const postParsePlaintext = parser.toPlaintext(); + if (postParsePlaintext !== md) { + // only return "formatted" text if it differs from the source text + return postParsePlaintext; + } } export function textSerialize(model) { From 1df6837649e31dcde0f72458e4568f2331ea126e Mon Sep 17 00:00:00 2001 From: BobVul Date: Tue, 4 Feb 2020 10:38:43 +1100 Subject: [PATCH 3/3] Missing semicolon --- test/editor/serialize-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/editor/serialize-test.js b/test/editor/serialize-test.js index f846e0d79e..9d116cdc4b 100644 --- a/test/editor/serialize-test.js +++ b/test/editor/serialize-test.js @@ -48,5 +48,5 @@ describe('editor/serialize', function() { const model = new EditorModel([pc.plain('\\*hello\\* world')]); const html = htmlSerializeIfNeeded(model, {}); expect(html).toBe('*hello* world'); - }) + }); });