mirror of
https://github.com/discourse/discourse.git
synced 2025-04-03 05:39:41 +08:00
FIX: censored regex words were replacing other text that shouldn't be censored
This commit is contained in:
parent
594efa37b6
commit
dc97239040
@ -36,13 +36,14 @@ export function censorFn(censoredWords, censoredPattern, replacementLetter, watc
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
let m = censorRegexp.exec(text);
|
let m = censorRegexp.exec(text);
|
||||||
|
const fourCharReplacement = new Array(5).join(replacementLetter);
|
||||||
|
|
||||||
while (m && m[0]) {
|
while (m && m[0]) {
|
||||||
if (m[0].length > original.length) { return original; } // regex is dangerous
|
if (m[0].length > original.length) { return original; } // regex is dangerous
|
||||||
const replacement = new Array(m[0].length+1).join(replacementLetter);
|
|
||||||
if (watchedWordsRegularExpressions) {
|
if (watchedWordsRegularExpressions) {
|
||||||
text = text.replace(new RegExp(`(${escapeRegexp(m[0])})(?![^\\(]*\\))`, "ig"), replacement);
|
text = text.replace(censorRegexp, fourCharReplacement);
|
||||||
} else {
|
} else {
|
||||||
|
const replacement = new Array(m[0].length+1).join(replacementLetter);
|
||||||
text = text.replace(new RegExp(`(\\b${escapeRegexp(m[0])}\\b)(?![^\\(]*\\))`, "ig"), replacement);
|
text = text.replace(new RegExp(`(\\b${escapeRegexp(m[0])}\\b)(?![^\\(]*\\))`, "ig"), replacement);
|
||||||
}
|
}
|
||||||
m = censorRegexp.exec(text);
|
m = censorRegexp.exec(text);
|
||||||
|
@ -617,7 +617,18 @@ QUnit.test("censoring", assert => {
|
|||||||
},
|
},
|
||||||
censoredWords: 'xyz*|plee+ase'
|
censoredWords: 'xyz*|plee+ase'
|
||||||
},
|
},
|
||||||
"<p>Pleased to meet you, but ■■■■■■■■■ call me later, ■■■123</p>",
|
"<p>Pleased to meet you, but ■■■■ call me later, ■■■■123</p>",
|
||||||
|
"supports words as regular expressions");
|
||||||
|
|
||||||
|
assert.cookedOptions(
|
||||||
|
"Meet downtown in your town at the townhouse on Main St.",
|
||||||
|
{ siteSettings: {
|
||||||
|
watched_words_regular_expressions: true,
|
||||||
|
censored_pattern: null
|
||||||
|
},
|
||||||
|
censoredWords: '\\btown\\b'
|
||||||
|
},
|
||||||
|
"<p>Meet downtown in your ■■■■ at the townhouse on Main St.</p>",
|
||||||
"supports words as regular expressions");
|
"supports words as regular expressions");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user