From 3c2608d41c3e19c3037571b9102f73b743053fbc Mon Sep 17 00:00:00 2001 From: David Taylor Date: Thu, 4 Oct 2018 15:15:10 +0100 Subject: [PATCH] FIX: Correctly censor strings starting or ending with non-word characters (#6445) --- .../pretty-text/censored-words.js.es6 | 14 ++++++------- test/javascripts/lib/pretty-text-test.js.es6 | 21 ++++++++++++++++++- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/assets/javascripts/pretty-text/censored-words.js.es6 b/app/assets/javascripts/pretty-text/censored-words.js.es6 index badaeb322cc..32c21dc39b6 100644 --- a/app/assets/javascripts/pretty-text/censored-words.js.es6 +++ b/app/assets/javascripts/pretty-text/censored-words.js.es6 @@ -2,6 +2,10 @@ function escapeRegexp(text) { return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&").replace(/\*/g, "S*"); } +function createCensorRegexp(patterns) { + return new RegExp(`((? url }; @@ -959,6 +959,25 @@ QUnit.test("censoring", assert => { "

I have a pen, I have an ■■■■■

", "it escapes regexp chars" ); + + assert.cooked( + "Aw shuck$, I can't fix the problem with money", + "

Aw ■■■■■■, I can't fix the problem with money

", + "it works for words ending in non-word characters" + ); + + assert.cooked( + "Let's go to a café today", + "

Let's go to a ■■■■ today

", + "it works for words ending in accented characters" + ); + + assert.cooked( + "Discourse is $uper amazing", + "

Discourse is ■■■■■ amazing

", + "it works for words starting with non-word characters" + ); + assert.cooked( "No badword or apple here plz.", "

No ■■■■■■■ or ■■■■■ here plz.

",