Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: background.js

Issue 29674584: Issue 5549 - Implement missing error handlings for custom filter lists (Closed)
Patch Set: changed custom-filters-edit-area to custom-filters-control as suggested Created Feb. 5, 2018, 9:05 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « README.md ('k') | desktop-options.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: background.js
===================================================================
--- a/background.js
+++ b/background.js
@@ -71,7 +71,6 @@
blockedURLs: "",
filterlistsReinitialized: false,
addSubscription: false,
- filterError: false,
downloadStatus: "synchronize_ok",
showNotificationUI: false,
showPageOptions: false
@@ -313,7 +312,10 @@
{
this.text = text;
this.disabled = false;
+ if (Filter.elemhideRegExp.test(text))
+ this.selector = RegExp.$3;
}
+ Filter.elemhideRegExp = /^([^/*|@"!]*?)#([@?])?#(.+)$/;
Filter.fromText = (text) => new Filter(text);
function BlockingFilter()
@@ -331,24 +333,65 @@
RegExpFilter
};
+ const isValidCSSSelector = selector =>
+ {
+ if (!selector)
+ return true;
+ try
+ {
+ document.documentElement.matches(selector);
+ return true;
+ }
+ catch (error)
+ {
+ return false;
+ }
+ };
+
modules.filterValidation =
{
+ // to test failing filters
+ // use one or more bad CSS selectors
+ // or start the line with a [
parseFilter(text)
{
- if (params.filterError)
- return {error: "Invalid filter"};
- return {filter: modules.filterClasses.Filter.fromText(text)};
+ let filter = null;
+ if (text)
+ {
+ if (text[0] == "[")
+ return {error: {reason: "Unexpected filter list header"}};
+
+ filter = modules.filterClasses.Filter.fromText(text);
+
+ if (!isValidCSSSelector(filter.selector))
+ {
+ return {error: {reason: "Invalid CSS selector"}};
+ }
+ }
+
+ return {filter};
},
parseFilters(text)
{
- if (params.filterError)
- return {errors: ["Invalid filter"]};
- return {
- filters: text.split("\n")
- .filter((filter) => !!filter)
- .map(modules.filterClasses.Filter.fromText),
- errors: []
- };
+ let lines = text.split("\n");
+ let filters = [];
+ let errors = [];
+
+ for (let i = 0; i < lines.length; i++)
+ {
+ let {filter, error} = this.parseFilter(lines[i]);
+
+ if (filter)
+ filters.push(filter);
+
+ if (error)
+ {
+ error.lineno = i + 1;
+ errors.push(error);
+ }
+ }
+
+ return {filters, errors};
}
};
« no previous file with comments | « README.md ('k') | desktop-options.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld