discourse/app/assets/javascripts/pretty-text/censored-words.js.es6

58 lines
1.5 KiB
Plaintext
Raw Normal View History

2017-01-30 21:39:45 -05:00
function escapeRegexp(text) {
2017-01-30 21:45:04 -05:00
return text.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
2017-01-30 21:39:45 -05:00
}
export function censorFn(censoredWords, censoredPattern, replacementLetter) {
let patterns = [];
replacementLetter = replacementLetter || "■";
if (censoredWords && censoredWords.length) {
2017-01-30 21:39:45 -05:00
patterns = censoredWords.split("|").map(t => `(${escapeRegexp(t)})`);
}
if (censoredPattern && censoredPattern.length > 0) {
2016-11-08 17:59:51 -05:00
patterns.push("(" + censoredPattern + ")");
}
if (patterns.length) {
let censorRegexp;
try {
censorRegexp = new RegExp("(\\b(?:" + patterns.join("|") + ")\\b)(?![^\\(]*\\))", "ig");
if (censorRegexp) {
return function(text) {
let original = text;
try {
let m = censorRegexp.exec(text);
while (m && m[0]) {
if (m[0].length > original.length) { return original; } // regex is dangerous
const replacement = new Array(m[0].length+1).join(replacementLetter);
text = text.replace(new RegExp(`(\\b${escapeRegexp(m[0])}\\b)(?![^\\(]*\\))`, "ig"), replacement);
m = censorRegexp.exec(text);
}
return text;
} catch (e) {
return original;
}
};
}
} catch(e) {
// fall through
}
}
return function(t){ return t;};
}
export function censor(text, censoredWords, censoredPattern, replacementLetter) {
return censorFn(censoredWords, censoredPattern, replacementLetter)(text);
}