FEATURE: Show an error message if regex is invalid (#13164)

The server cannot always determine when a watched word regular
expression is invalid and this commit implements the check on the client
side.
This commit is contained in:
Bianca Nenciu 2021-05-27 19:42:43 +03:00 committed by GitHub
parent b56e9ad656
commit efd6394cd8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 50 additions and 4 deletions

View File

@ -27,6 +27,17 @@ export default Controller.extend({
return this.findAction(actionName);
},
@discourseComputed("currentAction.words.[]")
regexpError(words) {
for (const { regexp, word } of words) {
try {
RegExp(regexp);
} catch {
return I18n.t("admin.watched_words.invalid_regex", { word });
}
}
},
@discourseComputed("actionNameKey")
actionDescription(actionNameKey) {
return I18n.t("admin.watched_words.action_descriptions." + actionNameKey);

View File

@ -1,3 +1,7 @@
{{#if regexpError}}
<div class="alert alert-error">{{regexpError}}</div>
{{/if}}
<div class="watched-word-controls">
{{d-button
class="btn-default download-link"

View File

@ -12,6 +12,8 @@ acceptance("Admin - Watched Words", function (needs) {
test("list words in groups", async function (assert) {
await visit("/admin/customize/watched_words/action/block");
assert.equal(find(".admin-watched-words .alert-error").length, 0);
assert.ok(
!exists(".watched-words-list"),
"Don't show bad words by default."
@ -100,3 +102,35 @@ acceptance("Admin - Watched Words", function (needs) {
assert.equal(find(".modal-body li .tag").text(), "greeting");
});
});
acceptance("Admin - Watched Words - Bad regular expressions", function (needs) {
needs.user();
needs.pretender((server, helper) => {
server.get("/admin/customize/watched_words.json", () => {
return helper.response({
actions: ["block", "censor", "require_approval", "flag", "replace"],
words: [
{
id: 1,
word: "[.*",
regexp: "[.*",
action: "block",
},
],
regular_expressions: true,
compiled_regular_expressions: {
block: null,
censor: null,
require_approval: null,
flag: null,
replace: null,
},
});
});
});
test("shows an error message if regex is invalid", async function (assert) {
await visit("/admin/customize/watched_words/action/block");
assert.equal(find(".admin-watched-words .alert-error").length, 1);
});
});

View File

@ -7,10 +7,6 @@ class WatchedWordSerializer < ApplicationSerializer
WordWatcher.word_to_regexp(word)
end
def include_regexp?
WatchedWord.has_replacement?(action)
end
def action
WatchedWord.actions[object.action]
end

View File

@ -4701,6 +4701,7 @@ en:
download: Download
clear_all: Clear All
clear_all_confirm: "Are you sure you want to clear all watched words for the %{action} action?"
invalid_regex: 'The watched word "%{word}" is an invalid regular expression.'
actions:
block: "Block"
censor: "Censor"