zjquery: Tell user why using tags as selector is invalid.

Like using $('input') is too broad a selector and shouldn't
be used in the codebase. With this error messages, contributors
can easily understand that now.
This commit is contained in:
Aman Agrawal
2021-02-22 13:37:49 +00:00
committed by Steve Howell
parent 269457c58b
commit 2b8921bf67

View File

@@ -29,7 +29,14 @@ function verify_selector_for_zulip(selector) {
(selector.includes("[") && selector.indexOf("]") >= selector.indexOf("["));
if (!is_valid) {
throw new Error("Invalid selector: " + selector + " Use $.create() maybe?");
// Check if selector has only english alphabets and space.
// Then, the user is probably trying to use a tag as a selector
// like $('div a').
if (/^[ A-Za-z]+$/.test(selector)) {
throw new Error("Selector too broad! Use id, class or attributes of target instead.");
} else {
throw new Error("Invalid selector: " + selector + " Use $.create() maybe?");
}
}
}