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}; |
} |
}; |