FIX: Add word boundaries to replace and tag watched words (#13405)

The generated regular expressions did not contain \b which matched
every text that contained the word, even if it was only a substring of
a word.

For example, if "art" was a watched word a post containing word
"artist" matched.
This commit is contained in:
Bianca Nenciu
2021-06-18 18:54:06 +03:00
committed by GitHub
parent 4afd8f9bdf
commit 74f7295631
8 changed files with 38 additions and 18 deletions
@@ -118,7 +118,6 @@ acceptance("Admin - Watched Words - Bad regular expressions", function (needs) {
action: "block",
},
],
regular_expressions: true,
compiled_regular_expressions: {
block: null,
censor: null,
@@ -11,14 +11,14 @@ export default {
{
id: 7,
word: "hi",
regexp: "hi",
regexp: "(hi)",
replacement: "hello",
action: "replace",
},
{
id: 8,
word: "hello",
regexp: "hello",
regexp: "(hello)",
replacement: "greeting",
action: "tag",
},
@@ -1675,21 +1675,21 @@ var bar = 'bar';
test("watched words replace", function (assert) {
const opts = {
watchedWordsReplace: { fun: "times" },
watchedWordsReplace: { "(?:\\W|^)(fun)(?=\\W|$)": "times" },
};
assert.cookedOptions("test fun", opts, "<p>test times</p>");
assert.cookedOptions("test fun funny", opts, "<p>test times funny</p>");
});
test("watched words link", function (assert) {
const opts = {
watchedWordsLink: { fun: "https://discourse.org" },
watchedWordsLink: { "(?:\\W|^)(fun)(?=\\W|$)": "https://discourse.org" },
};
assert.cookedOptions(
"test fun",
"test fun funny",
opts,
'<p>test <a href="https://discourse.org">fun</a></p>'
'<p>test <a href="https://discourse.org">fun</a> funny</p>'
);
});
@@ -1697,7 +1697,7 @@ var bar = 'bar';
const maxMatches = 100; // same limit as MD watched-words-replace plugin
const opts = {
siteSettings: { watched_words_regular_expressions: true },
watchedWordsReplace: { "\\bu?\\b": "you" },
watchedWordsReplace: { "(\\bu?\\b)": "you" },
};
assert.cookedOptions(
@@ -20,8 +20,8 @@ function findAllMatches(text, matchers) {
count++ < MAX_MATCHES
) {
matches.push({
index: match.index,
text: match[0],
index: match.index + match[0].indexOf(match[1]),
text: match[1],
replacement: matcher.replacement,
link: matcher.link,
});