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

Unified Diff: test/filterClasses.js

Issue 30025555: Issue 6820 - Move tests to mocha (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rebased. Created April 5, 2019, 4:16 p.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 | « test/elemHide.js ('k') | test/filterListener.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/filterClasses.js
===================================================================
--- a/test/filterClasses.js
+++ b/test/filterClasses.js
@@ -12,16 +12,17 @@
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
+const assert = require("assert");
const {createSandbox} = require("./_common");
let Filter = null;
let InvalidFilter = null;
let CommentFilter = null;
let ActiveFilter = null;
let RegExpFilter = null;
let BlockingFilter = null;
@@ -31,614 +32,588 @@
let ElemHideFilter = null;
let ElemHideException = null;
let ElemHideEmulationFilter = null;
let SnippetFilter = null;
let t = null;
let defaultTypes = null;
-exports.setUp = function(callback)
-{
- let sandboxedRequire = createSandbox();
- (
- {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter,
- BlockingFilter, WhitelistFilter, ContentFilter, ElemHideBase,
- ElemHideFilter, ElemHideException, ElemHideEmulationFilter,
- SnippetFilter} = sandboxedRequire("../lib/filterClasses")
- );
- t = RegExpFilter.typeMap;
- defaultTypes = 0x7FFFFFFF & ~(t.CSP | t.ELEMHIDE | t.DOCUMENT | t.POPUP |
- t.GENERICHIDE | t.GENERICBLOCK);
-
- callback();
-};
-
-function serializeFilter(filter)
+describe("Filters Classes", () =>
{
- // Filter serialization only writes out essential properties, need to do a full serialization here
- let result = [];
- result.push("text=" + filter.text);
- if (filter instanceof InvalidFilter)
+ beforeEach(() =>
{
- result.push("type=invalid");
- result.push("reason=" + filter.reason);
- }
- else if (filter instanceof CommentFilter)
- result.push("type=comment");
- else if (filter instanceof ActiveFilter)
+ let sandboxedRequire = createSandbox();
+ (
+ {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter,
+ BlockingFilter, WhitelistFilter, ContentFilter, ElemHideBase,
+ ElemHideFilter, ElemHideException, ElemHideEmulationFilter,
+ SnippetFilter} = sandboxedRequire("../lib/filterClasses")
+ );
+ t = RegExpFilter.typeMap;
+ defaultTypes = 0x7FFFFFFF & ~(t.CSP | t.ELEMHIDE | t.DOCUMENT | t.POPUP |
+ t.GENERICHIDE | t.GENERICBLOCK);
+ });
+
+ function serializeFilter(filter)
{
- result.push("disabled=" + filter.disabled);
- result.push("lastHit=" + filter.lastHit);
- result.push("hitCount=" + filter.hitCount);
+ // Filter serialization only writes out essential properties, need to do a full serialization here
+ let result = [];
+ result.push("text=" + filter.text);
+ if (filter instanceof InvalidFilter)
+ {
+ result.push("type=invalid");
+ result.push("reason=" + filter.reason);
+ }
+ else if (filter instanceof CommentFilter)
+ result.push("type=comment");
+ else if (filter instanceof ActiveFilter)
+ {
+ result.push("disabled=" + filter.disabled);
+ result.push("lastHit=" + filter.lastHit);
+ result.push("hitCount=" + filter.hitCount);
+
+ let domains = [];
+ if (filter.domains)
+ {
+ for (let [domain, isIncluded] of filter.domains)
+ {
+ if (domain != "")
+ domains.push(isIncluded ? domain : "~" + domain);
+ }
+ }
+ result.push("domains=" + domains.sort().join("|"));
+
+ if (filter instanceof RegExpFilter)
+ {
+ result.push("regexp=" + (filter.regexp ? filter.regexp.source : null));
+ result.push("contentType=" + filter.contentType);
+ result.push("matchCase=" + filter.matchCase);
- let domains = [];
- if (filter.domains)
- {
- for (let [domain, isIncluded] of filter.domains)
+ let sitekeys = filter.sitekeys || [];
+ result.push("sitekeys=" + sitekeys.slice().sort().join("|"));
+
+ result.push("thirdParty=" + filter.thirdParty);
+ if (filter instanceof BlockingFilter)
+ {
+ result.push("type=filterlist");
+ result.push("collapse=" + filter.collapse);
+ result.push("csp=" + filter.csp);
+ result.push("rewrite=" + filter.rewrite);
+ }
+ else if (filter instanceof WhitelistFilter)
+ result.push("type=whitelist");
+ }
+ else if (filter instanceof ElemHideBase)
{
- if (domain != "")
- domains.push(isIncluded ? domain : "~" + domain);
+ if (filter instanceof ElemHideFilter)
+ result.push("type=elemhide");
+ else if (filter instanceof ElemHideException)
+ result.push("type=elemhideexception");
+ else if (filter instanceof ElemHideEmulationFilter)
+ result.push("type=elemhideemulation");
+
+ result.push("selectorDomains=" +
+ [...filter.domains || []]
+ .filter(([domain, isIncluded]) => isIncluded)
+ .map(([domain]) => domain.toLowerCase()));
+ result.push("selector=" + filter.selector);
+ }
+ else if (filter instanceof SnippetFilter)
+ {
+ result.push("type=snippet");
+ result.push("scriptDomains=" +
+ [...filter.domains || []]
+ .filter(([domain, isIncluded]) => isIncluded)
+ .map(([domain]) => domain.toLowerCase()));
+ result.push("script=" + filter.script);
}
}
- result.push("domains=" + domains.sort().join("|"));
-
- if (filter instanceof RegExpFilter)
- {
- result.push("regexp=" + (filter.regexp ? filter.regexp.source : null));
- result.push("contentType=" + filter.contentType);
- result.push("matchCase=" + filter.matchCase);
-
- let sitekeys = filter.sitekeys || [];
- result.push("sitekeys=" + sitekeys.slice().sort().join("|"));
+ return result;
+ }
- result.push("thirdParty=" + filter.thirdParty);
- if (filter instanceof BlockingFilter)
- {
- result.push("type=filterlist");
- result.push("collapse=" + filter.collapse);
- result.push("csp=" + filter.csp);
- result.push("rewrite=" + filter.rewrite);
- }
- else if (filter instanceof WhitelistFilter)
- result.push("type=whitelist");
+ function addDefaults(expected)
+ {
+ let type = null;
+ let hasProperty = {};
+ for (let entry of expected)
+ {
+ if (/^type=(.*)/.test(entry))
+ type = RegExp.$1;
+ else if (/^(\w+)/.test(entry))
+ hasProperty[RegExp.$1] = true;
+ }
+
+ function addProperty(prop, value)
+ {
+ if (!(prop in hasProperty))
+ expected.push(prop + "=" + value);
}
- else if (filter instanceof ElemHideBase)
+
+ if (type == "whitelist" || type == "filterlist" || type == "elemhide" ||
+ type == "elemhideexception" || type == "elemhideemulation" ||
+ type == "snippet")
+ {
+ addProperty("disabled", "false");
+ addProperty("lastHit", "0");
+ addProperty("hitCount", "0");
+ }
+ if (type == "whitelist" || type == "filterlist")
{
- if (filter instanceof ElemHideFilter)
- result.push("type=elemhide");
- else if (filter instanceof ElemHideException)
- result.push("type=elemhideexception");
- else if (filter instanceof ElemHideEmulationFilter)
- result.push("type=elemhideemulation");
-
- result.push("selectorDomains=" +
- [...filter.domains || []]
- .filter(([domain, isIncluded]) => isIncluded)
- .map(([domain]) => domain.toLowerCase()));
- result.push("selector=" + filter.selector);
+ addProperty("contentType", 0x7FFFFFFF & ~(
+ t.CSP | t.DOCUMENT | t.ELEMHIDE | t.POPUP | t.GENERICHIDE | t.GENERICBLOCK
+ ));
+ addProperty("regexp", "null");
+ addProperty("matchCase", "false");
+ addProperty("thirdParty", "null");
+ addProperty("domains", "");
+ addProperty("sitekeys", "");
}
- else if (filter instanceof SnippetFilter)
+ if (type == "filterlist")
{
- result.push("type=snippet");
- result.push("scriptDomains=" +
- [...filter.domains || []]
- .filter(([domain, isIncluded]) => isIncluded)
- .map(([domain]) => domain.toLowerCase()));
- result.push("script=" + filter.script);
+ addProperty("collapse", "null");
+ addProperty("csp", "null");
+ addProperty("rewrite", "null");
+ }
+ if (type == "elemhide" || type == "elemhideexception" ||
+ type == "elemhideemulation")
+ {
+ addProperty("selectorDomains", "");
+ addProperty("domains", "");
+ }
+ if (type == "snippet")
+ {
+ addProperty("scriptDomains", "");
+ addProperty("domains", "");
}
}
- return result;
-}
-function addDefaults(expected)
-{
- let type = null;
- let hasProperty = {};
- for (let entry of expected)
+ function compareFilter(text, expected, postInit)
{
- if (/^type=(.*)/.test(entry))
- type = RegExp.$1;
- else if (/^(\w+)/.test(entry))
- hasProperty[RegExp.$1] = true;
- }
+ addDefaults(expected);
+
+ let filter = Filter.fromText(text);
+ if (postInit)
+ postInit(filter);
+ let result = serializeFilter(filter);
+ assert.equal(result.sort().join("\n"), expected.sort().join("\n"), text);
- function addProperty(prop, value)
- {
- if (!(prop in hasProperty))
- expected.push(prop + "=" + value);
+ // Test round-trip
+ let filter2;
+ let buffer = [...filter.serialize()];
+ if (buffer.length)
+ {
+ let map = Object.create(null);
+ for (let line of buffer.slice(1))
+ {
+ if (/(.*?)=(.*)/.test(line))
+ map[RegExp.$1] = RegExp.$2;
+ }
+ filter2 = Filter.fromObject(map);
+ }
+ else
+ filter2 = Filter.fromText(filter.text);
+
+ assert.equal(serializeFilter(filter).join("\n"), serializeFilter(filter2).join("\n"), text + " deserialization");
}
- if (type == "whitelist" || type == "filterlist" || type == "elemhide" ||
- type == "elemhideexception" || type == "elemhideemulation" ||
- type == "snippet")
- {
- addProperty("disabled", "false");
- addProperty("lastHit", "0");
- addProperty("hitCount", "0");
- }
- if (type == "whitelist" || type == "filterlist")
+ it("Definitions", () =>
{
- addProperty("contentType", 0x7FFFFFFF & ~(
- t.CSP | t.DOCUMENT | t.ELEMHIDE | t.POPUP | t.GENERICHIDE | t.GENERICBLOCK
- ));
- addProperty("regexp", "null");
- addProperty("matchCase", "false");
- addProperty("thirdParty", "null");
- addProperty("domains", "");
- addProperty("sitekeys", "");
- }
- if (type == "filterlist")
- {
- addProperty("collapse", "null");
- addProperty("csp", "null");
- addProperty("rewrite", "null");
- }
- if (type == "elemhide" || type == "elemhideexception" ||
- type == "elemhideemulation")
+ assert.equal(typeof Filter, "function", "typeof Filter");
+ assert.equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
+ assert.equal(typeof CommentFilter, "function", "typeof CommentFilter");
+ assert.equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
+ assert.equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
+ assert.equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
+ assert.equal(typeof ContentFilter, "function", "typeof ContentFilter");
+ assert.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
+ assert.equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
+ assert.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
+ assert.equal(typeof ElemHideException,
+ "function", "typeof ElemHideException");
+ assert.equal(typeof ElemHideEmulationFilter, "function",
+ "typeof ElemHideEmulationFilter");
+ assert.equal(typeof SnippetFilter, "function", "typeof SnippetFilter");
+ });
+
+ it("Comments", () =>
{
- addProperty("selectorDomains", "");
- addProperty("domains", "");
- }
- if (type == "snippet")
+ compareFilter("!asdf", ["type=comment", "text=!asdf"]);
+ compareFilter("!foo#bar", ["type=comment", "text=!foo#bar"]);
+ compareFilter("!foo##bar", ["type=comment", "text=!foo##bar"]);
+ });
+
+ it("Invalid Filters", () =>
{
- addProperty("scriptDomains", "");
- addProperty("domains", "");
- }
-}
-
-function compareFilter(test, text, expected, postInit)
-{
- addDefaults(expected);
+ compareFilter("/??/", ["type=invalid", "text=/??/", "reason=filter_invalid_regexp"]);
+ compareFilter("asd$foobar", ["type=invalid", "text=asd$foobar", "reason=filter_unknown_option"]);
- let filter = Filter.fromText(text);
- if (postInit)
- postInit(filter);
- let result = serializeFilter(filter);
- test.equal(result.sort().join("\n"), expected.sort().join("\n"), text);
+ // No $domain or $~third-party
+ compareFilter("||example.com/ad.js$rewrite=abp-resource:noopjs", ["type=invalid", "text=||example.com/ad.js$rewrite=abp-resource:noopjs", "reason=filter_invalid_rewrite"]);
+ compareFilter("*example.com/ad.js$rewrite=abp-resource:noopjs", ["type=invalid", "text=*example.com/ad.js$rewrite=abp-resource:noopjs", "reason=filter_invalid_rewrite"]);
+ compareFilter("example.com/ad.js$rewrite=abp-resource:noopjs", ["type=invalid", "text=example.com/ad.js$rewrite=abp-resource:noopjs", "reason=filter_invalid_rewrite"]);
+ // Patterns not starting with || or *
+ compareFilter("example.com/ad.js$rewrite=abp-resource:noopjs,domain=foo.com", ["type=invalid", "text=example.com/ad.js$rewrite=abp-resource:noopjs,domain=foo.com", "reason=filter_invalid_rewrite"]);
+ compareFilter("example.com/ad.js$rewrite=abp-resource:noopjs,~third-party", ["type=invalid", "text=example.com/ad.js$rewrite=abp-resource:noopjs,~third-party", "reason=filter_invalid_rewrite"]);
+ // $~third-party requires ||
+ compareFilter("*example.com/ad.js$rewrite=abp-resource:noopjs,~third-party", ["type=invalid", "text=*example.com/ad.js$rewrite=abp-resource:noopjs,~third-party", "reason=filter_invalid_rewrite"]);
- // Test round-trip
- let filter2;
- let buffer = [...filter.serialize()];
- if (buffer.length)
- {
- let map = Object.create(null);
- for (let line of buffer.slice(1))
+ function checkElemHideEmulationFilterInvalid(domains)
{
- if (/(.*?)=(.*)/.test(line))
- map[RegExp.$1] = RegExp.$2;
+ let filterText = domains + "#?#:-abp-properties(abc)";
+ compareFilter(
+ filterText, [
+ "type=invalid", "text=" + filterText,
+ "reason=filter_elemhideemulation_nodomain"
+ ]
+ );
}
- filter2 = Filter.fromObject(map);
- }
- else
- filter2 = Filter.fromText(filter.text);
+ checkElemHideEmulationFilterInvalid("");
+ checkElemHideEmulationFilterInvalid("~foo.com");
+ checkElemHideEmulationFilterInvalid("~foo.com,~bar.com");
+ checkElemHideEmulationFilterInvalid("foo");
+ checkElemHideEmulationFilterInvalid("~foo.com,bar");
+ });
- test.equal(serializeFilter(filter).join("\n"), serializeFilter(filter2).join("\n"), text + " deserialization");
-}
+ it("Filters with State", () =>
+ {
+ compareFilter("blabla", ["type=filterlist", "text=blabla"]);
+ compareFilter(
+ "blabla_default", ["type=filterlist", "text=blabla_default"],
+ filter =>
+ {
+ filter.disabled = false;
+ filter.hitCount = 0;
+ filter.lastHit = 0;
+ }
+ );
+ compareFilter(
+ "blabla_non_default",
+ ["type=filterlist", "text=blabla_non_default", "disabled=true", "hitCount=12", "lastHit=20"],
+ filter =>
+ {
+ filter.disabled = true;
+ filter.hitCount = 12;
+ filter.lastHit = 20;
+ }
+ );
+ });
+
+ it("Special Characters", () =>
+ {
+ compareFilter("/ddd|f?a[s]d/", ["type=filterlist", "text=/ddd|f?a[s]d/", "regexp=ddd|f?a[s]d"]);
+ compareFilter("*asdf*d**dd*", ["type=filterlist", "text=*asdf*d**dd*", "regexp=asdf.*d.*dd"]);
+ compareFilter("|*asd|f*d**dd*|", ["type=filterlist", "text=|*asd|f*d**dd*|", "regexp=^.*asd\\|f.*d.*dd.*$"]);
+ compareFilter("dd[]{}$%<>&()*d", ["type=filterlist", "text=dd[]{}$%<>&()*d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\).*d"]);
+
+ // Leading and trailing wildcards should be left in for rewrite filters (#6868).
+ compareFilter("*asdf*d**dd*$rewrite=", ["type=filterlist", "text=*asdf*d**dd*$rewrite=", "regexp=.*asdf.*d.*dd.*", "rewrite=", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.SUBDOCUMENT | t.OBJECT | t.OBJECT_SUBREQUEST))]);
-exports.testFilterClassDefinitions = function(test)
-{
- test.equal(typeof Filter, "function", "typeof Filter");
- test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
- test.equal(typeof CommentFilter, "function", "typeof CommentFilter");
- test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
- test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
- test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
- test.equal(typeof ContentFilter, "function", "typeof ContentFilter");
- test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
- test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
- test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
- test.equal(typeof ElemHideException, "function", "typeof ElemHideException");
- test.equal(typeof ElemHideEmulationFilter, "function",
- "typeof ElemHideEmulationFilter");
- test.equal(typeof SnippetFilter, "function", "typeof SnippetFilter");
+ compareFilter("@@/ddd|f?a[s]d/", ["type=whitelist", "text=@@/ddd|f?a[s]d/", "regexp=ddd|f?a[s]d", "contentType=" + defaultTypes]);
+ compareFilter("@@*asdf*d**dd*", ["type=whitelist", "text=@@*asdf*d**dd*", "regexp=asdf.*d.*dd", "contentType=" + defaultTypes]);
+ compareFilter("@@|*asd|f*d**dd*|", ["type=whitelist", "text=@@|*asd|f*d**dd*|", "regexp=^.*asd\\|f.*d.*dd.*$", "contentType=" + defaultTypes]);
+ compareFilter("@@dd[]{}$%<>&()*d", ["type=whitelist", "text=@@dd[]{}$%<>&()*d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\).*d", "contentType=" + defaultTypes]);
+ });
+
+ it("Filter Options", () =>
+ {
+ compareFilter("bla$match-case,csp=first csp,script,other,third-party,domain=FOO.cOm,sitekey=foo", ["type=filterlist", "text=bla$match-case,csp=first csp,script,other,third-party,domain=FOO.cOm,sitekey=foo", "matchCase=true", "contentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domains=foo.com", "sitekeys=FOO", "csp=first csp"]);
+ compareFilter("bla$~match-case,~csp=csp,~script,~other,~third-party,domain=~bAr.coM", ["type=filterlist", "text=bla$~match-case,~csp=csp,~script,~other,~third-party,domain=~bAr.coM", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER)), "thirdParty=false", "domains=~bar.com"]);
+ compareFilter("@@bla$match-case,script,other,third-party,domain=foo.com|bar.com|~bAR.foO.Com|~Foo.Bar.com,csp=c s p,sitekey=foo|bar", ["type=whitelist", "text=@@bla$match-case,script,other,third-party,domain=foo.com|bar.com|~bAR.foO.Com|~Foo.Bar.com,csp=c s p,sitekey=foo|bar", "matchCase=true", "contentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domains=bar.com|foo.com|~bar.foo.com|~foo.bar.com", "sitekeys=BAR|FOO"]);
+ compareFilter("@@bla$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo.com|~foo.bar.com,sitekey=foo|bar", ["type=whitelist", "text=@@bla$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo.com|~foo.bar.com,sitekey=foo|bar", "matchCase=true", "contentType=" + (t.SCRIPT | t.OTHER), "thirdParty=true", "domains=bar.com|foo.com|~bar.foo.com|~foo.bar.com", "sitekeys=BAR|FOO"]);
+ compareFilter("||content.server.com/files/*.php$rewrite=$1", ["type=filterlist", "text=||content.server.com/files/*.php$rewrite=$1", "regexp=^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?content\\.server\\.com\\/files\\/.*\\.php", "matchCase=false", "rewrite=$1", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.SUBDOCUMENT | t.OBJECT | t.OBJECT_SUBREQUEST))]);
+
+ compareFilter("||example.com/ad.js$rewrite=abp-resource:noopjs,domain=foo.com|bar.com", ["type=filterlist", "text=||example.com/ad.js$rewrite=abp-resource:noopjs,domain=foo.com|bar.com", "regexp=null", "matchCase=false", "rewrite=abp-resource:noopjs", "contentType=" + (defaultTypes), "domains=bar.com|foo.com"]);
+ compareFilter("*example.com/ad.js$rewrite=abp-resource:noopjs,domain=foo.com|bar.com", ["type=filterlist", "text=*example.com/ad.js$rewrite=abp-resource:noopjs,domain=foo.com|bar.com", "regexp=null", "matchCase=false", "rewrite=abp-resource:noopjs", "contentType=" + (defaultTypes), "domains=bar.com|foo.com"]);
+ compareFilter("||example.com/ad.js$rewrite=abp-resource:noopjs,~third-party", ["type=filterlist", "text=||example.com/ad.js$rewrite=abp-resource:noopjs,~third-party", "regexp=null", "matchCase=false", "rewrite=abp-resource:noopjs", "thirdParty=false", "contentType=" + (defaultTypes)]);
- test.done();
-};
+ // background and image should be the same for backwards compatibility
+ compareFilter("bla$image", ["type=filterlist", "text=bla$image", "contentType=" + (t.IMAGE)]);
+ compareFilter("bla$background", ["type=filterlist", "text=bla$background", "contentType=" + (t.IMAGE)]);
+ compareFilter("bla$~image", ["type=filterlist", "text=bla$~image", "contentType=" + (defaultTypes & ~t.IMAGE)]);
+ compareFilter("bla$~background", ["type=filterlist", "text=bla$~background", "contentType=" + (defaultTypes & ~t.IMAGE)]);
+
+ compareFilter("@@bla$~script,~other", ["type=whitelist", "text=@@bla$~script,~other", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
+ compareFilter("@@http://bla$~script,~other", ["type=whitelist", "text=@@http://bla$~script,~other", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
+ compareFilter("@@|ftp://bla$~script,~other", ["type=whitelist", "text=@@|ftp://bla$~script,~other", "regexp=^ftp\\:\\/\\/bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
+ compareFilter("@@bla$~script,~other,document", ["type=whitelist", "text=@@bla$~script,~other,document", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER) | t.DOCUMENT)]);
+ compareFilter("@@bla$~script,~other,~document", ["type=whitelist", "text=@@bla$~script,~other,~document", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
+ compareFilter("@@bla$document", ["type=whitelist", "text=@@bla$document", "contentType=" + t.DOCUMENT]);
+ compareFilter("@@bla$~script,~other,elemhide", ["type=whitelist", "text=@@bla$~script,~other,elemhide", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER) | t.ELEMHIDE)]);
+ compareFilter("@@bla$~script,~other,~elemhide", ["type=whitelist", "text=@@bla$~script,~other,~elemhide", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
+ compareFilter("@@bla$elemhide", ["type=whitelist", "text=@@bla$elemhide", "contentType=" + t.ELEMHIDE]);
+
+ compareFilter("@@bla$~script,~other,donottrack", ["type=invalid", "text=@@bla$~script,~other,donottrack", "reason=filter_unknown_option"]);
+ compareFilter("@@bla$~script,~other,~donottrack", ["type=invalid", "text=@@bla$~script,~other,~donottrack", "reason=filter_unknown_option"]);
+ compareFilter("@@bla$donottrack", ["type=invalid", "text=@@bla$donottrack", "reason=filter_unknown_option"]);
+ compareFilter("@@bla$foobar", ["type=invalid", "text=@@bla$foobar", "reason=filter_unknown_option"]);
+ compareFilter("@@bla$image,foobar", ["type=invalid", "text=@@bla$image,foobar", "reason=filter_unknown_option"]);
+ compareFilter("@@bla$foobar,image", ["type=invalid", "text=@@bla$foobar,image", "reason=filter_unknown_option"]);
-exports.testComments = function(test)
-{
- compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]);
- compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]);
- compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]);
+ compareFilter("bla$csp", ["type=invalid", "text=bla$csp", "reason=filter_invalid_csp"]);
+ compareFilter("bla$csp=", ["type=invalid", "text=bla$csp=", "reason=filter_invalid_csp"]);
+
+ // Blank CSP values are allowed for whitelist filters.
+ compareFilter("@@bla$csp", ["type=whitelist", "text=@@bla$csp", "contentType=" + t.CSP]);
+ compareFilter("@@bla$csp=", ["type=whitelist", "text=@@bla$csp=", "contentType=" + t.CSP]);
+
+ compareFilter("bla$csp=report-uri", ["type=invalid", "text=bla$csp=report-uri", "reason=filter_invalid_csp"]);
+ compareFilter("bla$csp=foo,csp=report-to", ["type=invalid", "text=bla$csp=foo,csp=report-to", "reason=filter_invalid_csp"]);
+ compareFilter("bla$csp=foo,csp=referrer foo", ["type=invalid", "text=bla$csp=foo,csp=referrer foo", "reason=filter_invalid_csp"]);
+ compareFilter("bla$csp=foo,csp=base-uri", ["type=invalid", "text=bla$csp=foo,csp=base-uri", "reason=filter_invalid_csp"]);
+ compareFilter("bla$csp=foo,csp=upgrade-insecure-requests", ["type=invalid", "text=bla$csp=foo,csp=upgrade-insecure-requests", "reason=filter_invalid_csp"]);
+ compareFilter("bla$csp=foo,csp=ReFeRReR", ["type=invalid", "text=bla$csp=foo,csp=ReFeRReR", "reason=filter_invalid_csp"]);
+ });
- test.done();
-};
+ it("Element Hiding Rules", () =>
+ {
+ compareFilter("##ddd", ["type=elemhide", "text=##ddd", "selector=ddd"]);
+ compareFilter("##body > div:first-child", ["type=elemhide", "text=##body > div:first-child", "selector=body > div:first-child"]);
+ compareFilter("fOO##ddd", ["type=elemhide", "text=fOO##ddd", "selectorDomains=foo", "selector=ddd", "domains=foo"]);
+ compareFilter("Foo,bAr##ddd", ["type=elemhide", "text=Foo,bAr##ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo"]);
+ compareFilter("foo,~baR##ddd", ["type=elemhide", "text=foo,~baR##ddd", "selectorDomains=foo", "selector=ddd", "domains=foo|~bar"]);
+ compareFilter("foo,~baz,bar##ddd", ["type=elemhide", "text=foo,~baz,bar##ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo|~baz"]);
+ });
-exports.testInvalidFilters = function(test)
-{
- compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_invalid_regexp"]);
- compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason=filter_unknown_option"]);
+ it("Element Hiding Exceptions", () =>
+ {
+ compareFilter("#@#ddd", ["type=elemhideexception", "text=#@#ddd", "selector=ddd"]);
+ compareFilter("#@#body > div:first-child", ["type=elemhideexception", "text=#@#body > div:first-child", "selector=body > div:first-child"]);
+ compareFilter("fOO#@#ddd", ["type=elemhideexception", "text=fOO#@#ddd", "selectorDomains=foo", "selector=ddd", "domains=foo"]);
+ compareFilter("Foo,bAr#@#ddd", ["type=elemhideexception", "text=Foo,bAr#@#ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo"]);
+ compareFilter("foo,~baR#@#ddd", ["type=elemhideexception", "text=foo,~baR#@#ddd", "selectorDomains=foo", "selector=ddd", "domains=foo|~bar"]);
+ compareFilter("foo,~baz,bar#@#ddd", ["type=elemhideexception", "text=foo,~baz,bar#@#ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo|~baz"]);
+ });
- // No $domain or $~third-party
- compareFilter(test, "||example.com/ad.js$rewrite=abp-resource:noopjs", ["type=invalid", "text=||example.com/ad.js$rewrite=abp-resource:noopjs", "reason=filter_invalid_rewrite"]);
- compareFilter(test, "*example.com/ad.js$rewrite=abp-resource:noopjs", ["type=invalid", "text=*example.com/ad.js$rewrite=abp-resource:noopjs", "reason=filter_invalid_rewrite"]);
- compareFilter(test, "example.com/ad.js$rewrite=abp-resource:noopjs", ["type=invalid", "text=example.com/ad.js$rewrite=abp-resource:noopjs", "reason=filter_invalid_rewrite"]);
- // Patterns not starting with || or *
- compareFilter(test, "example.com/ad.js$rewrite=abp-resource:noopjs,domain=foo.com", ["type=invalid", "text=example.com/ad.js$rewrite=abp-resource:noopjs,domain=foo.com", "reason=filter_invalid_rewrite"]);
- compareFilter(test, "example.com/ad.js$rewrite=abp-resource:noopjs,~third-party", ["type=invalid", "text=example.com/ad.js$rewrite=abp-resource:noopjs,~third-party", "reason=filter_invalid_rewrite"]);
- // $~third-party requires ||
- compareFilter(test, "*example.com/ad.js$rewrite=abp-resource:noopjs,~third-party", ["type=invalid", "text=*example.com/ad.js$rewrite=abp-resource:noopjs,~third-party", "reason=filter_invalid_rewrite"]);
+ it("Element Hiding Emulation Filters", () =>
+ {
+ // Check valid domain combinations
+ compareFilter("fOO.cOm#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=fOO.cOm#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selector=:-abp-properties(abc)", "domains=foo.com"]);
+ compareFilter("Foo.com,~bAr.com#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=Foo.com,~bAr.com#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selector=:-abp-properties(abc)", "domains=foo.com|~bar.com"]);
+ compareFilter("foo.com,~baR#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=foo.com,~baR#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selector=:-abp-properties(abc)", "domains=foo.com|~bar"]);
+ compareFilter("~foo.com,bar.com#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=~foo.com,bar.com#?#:-abp-properties(abc)", "selectorDomains=bar.com", "selector=:-abp-properties(abc)", "domains=bar.com|~foo.com"]);
+
+ // Check some special cases
+ compareFilter("#?#:-abp-properties(abc)", ["type=invalid", "text=#?#:-abp-properties(abc)", "reason=filter_elemhideemulation_nodomain"]);
+ compareFilter("foo.com#?#abc", ["type=elemhideemulation", "text=foo.com#?#abc", "selectorDomains=foo.com", "selector=abc", "domains=foo.com"]);
+ compareFilter("foo.com#?#:-abp-foobar(abc)", ["type=elemhideemulation", "text=foo.com#?#:-abp-foobar(abc)", "selectorDomains=foo.com", "selector=:-abp-foobar(abc)", "domains=foo.com"]);
+ compareFilter("foo.com#?#aaa :-abp-properties(abc) bbb", ["type=elemhideemulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "selectorDomains=foo.com", "selector=aaa :-abp-properties(abc) bbb", "domains=foo.com"]);
+ compareFilter("foo.com#?#:-abp-properties(|background-image: url(data:*))", ["type=elemhideemulation", "text=foo.com#?#:-abp-properties(|background-image: url(data:*))", "selectorDomains=foo.com", "selector=:-abp-properties(|background-image: url(data:*))", "domains=foo.com"]);
+
+ // Support element hiding emulation filters for localhost (#6931).
+ compareFilter("localhost#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=localhost#?#:-abp-properties(abc)", "selectorDomains=localhost", "selector=:-abp-properties(abc)", "domains=localhost"]);
+ compareFilter("localhost,~www.localhost#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=localhost,~www.localhost#?#:-abp-properties(abc)", "selectorDomains=localhost", "selector=:-abp-properties(abc)", "domains=localhost|~www.localhost"]);
+ compareFilter("~www.localhost,localhost#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=~www.localhost,localhost#?#:-abp-properties(abc)", "selectorDomains=localhost", "selector=:-abp-properties(abc)", "domains=localhost|~www.localhost"]);
+ });
- function checkElemHideEmulationFilterInvalid(domains)
+ it("Empty ElemeHide domains", () =>
{
- let filterText = domains + "#?#:-abp-properties(abc)";
+ let emptyDomainFilters = [
+ ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector",
+ ",foo.com##selector", "foo.com,~##selector",
+ "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector"
+ ];
+
+ for (let filterText of emptyDomainFilters)
+ {
+ let filter = Filter.fromText(filterText);
+ assert.ok(filter instanceof InvalidFilter);
+ assert.equal(filter.reason, "filter_invalid_domain");
+ }
+ });
+
+ it("ElemHide rules with braces", () =>
+ {
compareFilter(
- test, filterText, [
- "type=invalid", "text=" + filterText,
- "reason=filter_elemhideemulation_nodomain"
+ "###foo{color: red}", [
+ "type=elemhide",
+ "text=###foo{color: red}",
+ "selectorDomains=",
+ "selector=#foo{color: red}",
+ "domains="
]
);
- }
- checkElemHideEmulationFilterInvalid("");
- checkElemHideEmulationFilterInvalid("~foo.com");
- checkElemHideEmulationFilterInvalid("~foo.com,~bar.com");
- checkElemHideEmulationFilterInvalid("foo");
- checkElemHideEmulationFilterInvalid("~foo.com,bar");
-
- test.done();
-};
-
-exports.testFiltersWithState = function(test)
-{
- compareFilter(test, "blabla", ["type=filterlist", "text=blabla"]);
- compareFilter(
- test, "blabla_default", ["type=filterlist", "text=blabla_default"],
- filter =>
- {
- filter.disabled = false;
- filter.hitCount = 0;
- filter.lastHit = 0;
- }
- );
- compareFilter(
- test, "blabla_non_default",
- ["type=filterlist", "text=blabla_non_default", "disabled=true", "hitCount=12", "lastHit=20"],
- filter =>
- {
- filter.disabled = true;
- filter.hitCount = 12;
- filter.lastHit = 20;
- }
- );
-
- test.done();
-};
+ compareFilter(
+ "foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", [
+ "type=elemhideemulation",
+ "text=foo.com#?#:-abp-properties(/margin: [3-4]{2}/)",
+ "selectorDomains=foo.com",
+ "selector=:-abp-properties(/margin: [3-4]{2}/)",
+ "domains=foo.com"
+ ]
+ );
+ });
-exports.testSpecialCharacters = function(test)
-{
- compareFilter(test, "/ddd|f?a[s]d/", ["type=filterlist", "text=/ddd|f?a[s]d/", "regexp=ddd|f?a[s]d"]);
- compareFilter(test, "*asdf*d**dd*", ["type=filterlist", "text=*asdf*d**dd*", "regexp=asdf.*d.*dd"]);
- compareFilter(test, "|*asd|f*d**dd*|", ["type=filterlist", "text=|*asd|f*d**dd*|", "regexp=^.*asd\\|f.*d.*dd.*$"]);
- compareFilter(test, "dd[]{}$%<>&()*d", ["type=filterlist", "text=dd[]{}$%<>&()*d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\).*d"]);
-
- // Leading and trailing wildcards should be left in for rewrite filters (#6868).
- compareFilter(test, "*asdf*d**dd*$rewrite=", ["type=filterlist", "text=*asdf*d**dd*$rewrite=", "regexp=.*asdf.*d.*dd.*", "rewrite=", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.SUBDOCUMENT | t.OBJECT | t.OBJECT_SUBREQUEST))]);
-
- compareFilter(test, "@@/ddd|f?a[s]d/", ["type=whitelist", "text=@@/ddd|f?a[s]d/", "regexp=ddd|f?a[s]d", "contentType=" + defaultTypes]);
- compareFilter(test, "@@*asdf*d**dd*", ["type=whitelist", "text=@@*asdf*d**dd*", "regexp=asdf.*d.*dd", "contentType=" + defaultTypes]);
- compareFilter(test, "@@|*asd|f*d**dd*|", ["type=whitelist", "text=@@|*asd|f*d**dd*|", "regexp=^.*asd\\|f.*d.*dd.*$", "contentType=" + defaultTypes]);
- compareFilter(test, "@@dd[]{}$%<>&()*d", ["type=whitelist", "text=@@dd[]{}$%<>&()*d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\).*d", "contentType=" + defaultTypes]);
-
- test.done();
-};
+ it("Snippet Filters", () =>
+ {
+ compareFilter("foo.com#$#abc", ["type=snippet", "text=foo.com#$#abc", "scriptDomains=foo.com", "script=abc", "domains=foo.com"]);
+ compareFilter("foo.com,~bar.com#$#abc", ["type=snippet", "text=foo.com,~bar.com#$#abc", "scriptDomains=foo.com", "script=abc", "domains=foo.com|~bar.com"]);
+ compareFilter("foo.com,~bar#$#abc", ["type=snippet", "text=foo.com,~bar#$#abc", "scriptDomains=foo.com", "script=abc", "domains=foo.com|~bar"]);
+ compareFilter("~foo.com,bar.com#$#abc", ["type=snippet", "text=~foo.com,bar.com#$#abc", "scriptDomains=bar.com", "script=abc", "domains=bar.com|~foo.com"]);
+ });
-exports.testFilterOptions = function(test)
-{
- compareFilter(test, "bla$match-case,csp=first csp,script,other,third-party,domain=FOO.cOm,sitekey=foo", ["type=filterlist", "text=bla$match-case,csp=first csp,script,other,third-party,domain=FOO.cOm,sitekey=foo", "matchCase=true", "contentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domains=foo.com", "sitekeys=FOO", "csp=first csp"]);
- compareFilter(test, "bla$~match-case,~csp=csp,~script,~other,~third-party,domain=~bAr.coM", ["type=filterlist", "text=bla$~match-case,~csp=csp,~script,~other,~third-party,domain=~bAr.coM", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER)), "thirdParty=false", "domains=~bar.com"]);
- compareFilter(test, "@@bla$match-case,script,other,third-party,domain=foo.com|bar.com|~bAR.foO.Com|~Foo.Bar.com,csp=c s p,sitekey=foo|bar", ["type=whitelist", "text=@@bla$match-case,script,other,third-party,domain=foo.com|bar.com|~bAR.foO.Com|~Foo.Bar.com,csp=c s p,sitekey=foo|bar", "matchCase=true", "contentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domains=bar.com|foo.com|~bar.foo.com|~foo.bar.com", "sitekeys=BAR|FOO"]);
- compareFilter(test, "@@bla$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo.com|~foo.bar.com,sitekey=foo|bar", ["type=whitelist", "text=@@bla$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo.com|~foo.bar.com,sitekey=foo|bar", "matchCase=true", "contentType=" + (t.SCRIPT | t.OTHER), "thirdParty=true", "domains=bar.com|foo.com|~bar.foo.com|~foo.bar.com", "sitekeys=BAR|FOO"]);
- compareFilter(test, "||content.server.com/files/*.php$rewrite=$1", ["type=filterlist", "text=||content.server.com/files/*.php$rewrite=$1", "regexp=^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?content\\.server\\.com\\/files\\/.*\\.php", "matchCase=false", "rewrite=$1", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.SUBDOCUMENT | t.OBJECT | t.OBJECT_SUBREQUEST))]);
-
- compareFilter(test, "||example.com/ad.js$rewrite=abp-resource:noopjs,domain=foo.com|bar.com", ["type=filterlist", "text=||example.com/ad.js$rewrite=abp-resource:noopjs,domain=foo.com|bar.com", "regexp=null", "matchCase=false", "rewrite=abp-resource:noopjs", "contentType=" + (defaultTypes), "domains=bar.com|foo.com"]);
- compareFilter(test, "*example.com/ad.js$rewrite=abp-resource:noopjs,domain=foo.com|bar.com", ["type=filterlist", "text=*example.com/ad.js$rewrite=abp-resource:noopjs,domain=foo.com|bar.com", "regexp=null", "matchCase=false", "rewrite=abp-resource:noopjs", "contentType=" + (defaultTypes), "domains=bar.com|foo.com"]);
- compareFilter(test, "||example.com/ad.js$rewrite=abp-resource:noopjs,~third-party", ["type=filterlist", "text=||example.com/ad.js$rewrite=abp-resource:noopjs,~third-party", "regexp=null", "matchCase=false", "rewrite=abp-resource:noopjs", "thirdParty=false", "contentType=" + (defaultTypes)]);
+ it("Filter normalization", () =>
+ {
+ // Line breaks etc
+ assert.equal(Filter.normalize("\n\t\nad\ns"),
+ "ads");
- // background and image should be the same for backwards compatibility
- compareFilter(test, "bla$image", ["type=filterlist", "text=bla$image", "contentType=" + (t.IMAGE)]);
- compareFilter(test, "bla$background", ["type=filterlist", "text=bla$background", "contentType=" + (t.IMAGE)]);
- compareFilter(test, "bla$~image", ["type=filterlist", "text=bla$~image", "contentType=" + (defaultTypes & ~t.IMAGE)]);
- compareFilter(test, "bla$~background", ["type=filterlist", "text=bla$~background", "contentType=" + (defaultTypes & ~t.IMAGE)]);
+ // Comment filters
+ assert.equal(Filter.normalize(" ! fo o## bar "),
+ "! fo o## bar");
- compareFilter(test, "@@bla$~script,~other", ["type=whitelist", "text=@@bla$~script,~other", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
- compareFilter(test, "@@http://bla$~script,~other", ["type=whitelist", "text=@@http://bla$~script,~other", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
- compareFilter(test, "@@|ftp://bla$~script,~other", ["type=whitelist", "text=@@|ftp://bla$~script,~other", "regexp=^ftp\\:\\/\\/bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
- compareFilter(test, "@@bla$~script,~other,document", ["type=whitelist", "text=@@bla$~script,~other,document", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER) | t.DOCUMENT)]);
- compareFilter(test, "@@bla$~script,~other,~document", ["type=whitelist", "text=@@bla$~script,~other,~document", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
- compareFilter(test, "@@bla$document", ["type=whitelist", "text=@@bla$document", "contentType=" + t.DOCUMENT]);
- compareFilter(test, "@@bla$~script,~other,elemhide", ["type=whitelist", "text=@@bla$~script,~other,elemhide", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER) | t.ELEMHIDE)]);
- compareFilter(test, "@@bla$~script,~other,~elemhide", ["type=whitelist", "text=@@bla$~script,~other,~elemhide", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
- compareFilter(test, "@@bla$elemhide", ["type=whitelist", "text=@@bla$elemhide", "contentType=" + t.ELEMHIDE]);
+ // Element hiding filters
+ assert.equal(Filter.normalize(" domain.c om## # sele ctor "),
+ "domain.com### sele ctor");
+
+ // Element hiding emulation filters
+ assert.equal(Filter.normalize(" domain.c om#?# # sele ctor "),
+ "domain.com#?## sele ctor");
- compareFilter(test, "@@bla$~script,~other,donottrack", ["type=invalid", "text=@@bla$~script,~other,donottrack", "reason=filter_unknown_option"]);
- compareFilter(test, "@@bla$~script,~other,~donottrack", ["type=invalid", "text=@@bla$~script,~other,~donottrack", "reason=filter_unknown_option"]);
- compareFilter(test, "@@bla$donottrack", ["type=invalid", "text=@@bla$donottrack", "reason=filter_unknown_option"]);
- compareFilter(test, "@@bla$foobar", ["type=invalid", "text=@@bla$foobar", "reason=filter_unknown_option"]);
- compareFilter(test, "@@bla$image,foobar", ["type=invalid", "text=@@bla$image,foobar", "reason=filter_unknown_option"]);
- compareFilter(test, "@@bla$foobar,image", ["type=invalid", "text=@@bla$foobar,image", "reason=filter_unknown_option"]);
-
- compareFilter(test, "bla$csp", ["type=invalid", "text=bla$csp", "reason=filter_invalid_csp"]);
- compareFilter(test, "bla$csp=", ["type=invalid", "text=bla$csp=", "reason=filter_invalid_csp"]);
-
- // Blank CSP values are allowed for whitelist filters.
- compareFilter(test, "@@bla$csp", ["type=whitelist", "text=@@bla$csp", "contentType=" + t.CSP]);
- compareFilter(test, "@@bla$csp=", ["type=whitelist", "text=@@bla$csp=", "contentType=" + t.CSP]);
+ // Incorrect syntax: the separator "#?#" cannot contain spaces; treated as a
+ // regular filter instead
+ assert.equal(Filter.normalize(" domain.c om# ?#. sele ctor "),
+ "domain.com#?#.selector");
+ // Incorrect syntax: the separator "#?#" cannot contain spaces; treated as an
+ // element hiding filter instead, because the "##" following the "?" is taken
+ // to be the separator instead
+ assert.equal(Filter.normalize(" domain.c om# ?##sele ctor "),
+ "domain.com#?##sele ctor");
- compareFilter(test, "bla$csp=report-uri", ["type=invalid", "text=bla$csp=report-uri", "reason=filter_invalid_csp"]);
- compareFilter(test, "bla$csp=foo,csp=report-to", ["type=invalid", "text=bla$csp=foo,csp=report-to", "reason=filter_invalid_csp"]);
- compareFilter(test, "bla$csp=foo,csp=referrer foo", ["type=invalid", "text=bla$csp=foo,csp=referrer foo", "reason=filter_invalid_csp"]);
- compareFilter(test, "bla$csp=foo,csp=base-uri", ["type=invalid", "text=bla$csp=foo,csp=base-uri", "reason=filter_invalid_csp"]);
- compareFilter(test, "bla$csp=foo,csp=upgrade-insecure-requests", ["type=invalid", "text=bla$csp=foo,csp=upgrade-insecure-requests", "reason=filter_invalid_csp"]);
- compareFilter(test, "bla$csp=foo,csp=ReFeRReR", ["type=invalid", "text=bla$csp=foo,csp=ReFeRReR", "reason=filter_invalid_csp"]);
-
- test.done();
-};
+ // Element hiding exception filters
+ assert.equal(Filter.normalize(" domain.c om#@# # sele ctor "),
+ "domain.com#@## sele ctor");
-exports.testElementHidingRules = function(test)
-{
- compareFilter(test, "##ddd", ["type=elemhide", "text=##ddd", "selector=ddd"]);
- compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body > div:first-child", "selector=body > div:first-child"]);
- compareFilter(test, "fOO##ddd", ["type=elemhide", "text=fOO##ddd", "selectorDomains=foo", "selector=ddd", "domains=foo"]);
- compareFilter(test, "Foo,bAr##ddd", ["type=elemhide", "text=Foo,bAr##ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo"]);
- compareFilter(test, "foo,~baR##ddd", ["type=elemhide", "text=foo,~baR##ddd", "selectorDomains=foo", "selector=ddd", "domains=foo|~bar"]);
- compareFilter(test, "foo,~baz,bar##ddd", ["type=elemhide", "text=foo,~baz,bar##ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo|~baz"]);
-
- test.done();
-};
+ // Incorrect syntax: the separator "#@#" cannot contain spaces; treated as a
+ // regular filter instead (not an element hiding filter either!), because
+ // unlike the case with "# ?##" the "##" following the "@" is not considered
+ // to be a separator
+ assert.equal(Filter.normalize(" domain.c om# @## sele ctor "),
+ "domain.com#@##selector");
-exports.testElementHidingExceptions = function(test)
-{
- compareFilter(test, "#@#ddd", ["type=elemhideexception", "text=#@#ddd", "selector=ddd"]);
- compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "text=#@#body > div:first-child", "selector=body > div:first-child"]);
- compareFilter(test, "fOO#@#ddd", ["type=elemhideexception", "text=fOO#@#ddd", "selectorDomains=foo", "selector=ddd", "domains=foo"]);
- compareFilter(test, "Foo,bAr#@#ddd", ["type=elemhideexception", "text=Foo,bAr#@#ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo"]);
- compareFilter(test, "foo,~baR#@#ddd", ["type=elemhideexception", "text=foo,~baR#@#ddd", "selectorDomains=foo", "selector=ddd", "domains=foo|~bar"]);
- compareFilter(test, "foo,~baz,bar#@#ddd", ["type=elemhideexception", "text=foo,~baz,bar#@#ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo|~baz"]);
-
- test.done();
-};
-
-exports.testElemHideEmulationFilters = function(test)
-{
- // Check valid domain combinations
- compareFilter(test, "fOO.cOm#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=fOO.cOm#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selector=:-abp-properties(abc)", "domains=foo.com"]);
- compareFilter(test, "Foo.com,~bAr.com#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=Foo.com,~bAr.com#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selector=:-abp-properties(abc)", "domains=foo.com|~bar.com"]);
- compareFilter(test, "foo.com,~baR#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=foo.com,~baR#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selector=:-abp-properties(abc)", "domains=foo.com|~bar"]);
- compareFilter(test, "~foo.com,bar.com#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=~foo.com,bar.com#?#:-abp-properties(abc)", "selectorDomains=bar.com", "selector=:-abp-properties(abc)", "domains=bar.com|~foo.com"]);
+ // Snippet filters
+ assert.equal(Filter.normalize(" domain.c om#$# sni pp et "),
+ "domain.com#$#sni pp et");
- // Check some special cases
- compareFilter(test, "#?#:-abp-properties(abc)", ["type=invalid", "text=#?#:-abp-properties(abc)", "reason=filter_elemhideemulation_nodomain"]);
- compareFilter(test, "foo.com#?#abc", ["type=elemhideemulation", "text=foo.com#?#abc", "selectorDomains=foo.com", "selector=abc", "domains=foo.com"]);
- compareFilter(test, "foo.com#?#:-abp-foobar(abc)", ["type=elemhideemulation", "text=foo.com#?#:-abp-foobar(abc)", "selectorDomains=foo.com", "selector=:-abp-foobar(abc)", "domains=foo.com"]);
- compareFilter(test, "foo.com#?#aaa :-abp-properties(abc) bbb", ["type=elemhideemulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "selectorDomains=foo.com", "selector=aaa :-abp-properties(abc) bbb", "domains=foo.com"]);
- compareFilter(test, "foo.com#?#:-abp-properties(|background-image: url(data:*))", ["type=elemhideemulation", "text=foo.com#?#:-abp-properties(|background-image: url(data:*))", "selectorDomains=foo.com", "selector=:-abp-properties(|background-image: url(data:*))", "domains=foo.com"]);
-
- // Support element hiding emulation filters for localhost (#6931).
- compareFilter(test, "localhost#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=localhost#?#:-abp-properties(abc)", "selectorDomains=localhost", "selector=:-abp-properties(abc)", "domains=localhost"]);
- compareFilter(test, "localhost,~www.localhost#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=localhost,~www.localhost#?#:-abp-properties(abc)", "selectorDomains=localhost", "selector=:-abp-properties(abc)", "domains=localhost|~www.localhost"]);
- compareFilter(test, "~www.localhost,localhost#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=~www.localhost,localhost#?#:-abp-properties(abc)", "selectorDomains=localhost", "selector=:-abp-properties(abc)", "domains=localhost|~www.localhost"]);
-
- test.done();
-};
-
-exports.testEmptyElemHideDomains = function(test)
-{
- let emptyDomainFilters = [
- ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector",
- ",foo.com##selector", "foo.com,~##selector",
- "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector"
- ];
-
- for (let filterText of emptyDomainFilters)
- {
- let filter = Filter.fromText(filterText);
- test.ok(filter instanceof InvalidFilter);
- test.equal(filter.reason, "filter_invalid_domain");
- }
-
- test.done();
-};
+ // Regular filters
+ let normalized = Filter.normalize(
+ " b$l a$sitekey= foo ,domain= do main.com |foo .com,c sp= c s p "
+ );
+ assert.equal(
+ normalized,
+ "b$la$sitekey=foo,domain=domain.com|foo.com,csp=c s p"
+ );
+ compareFilter(
+ normalized, [
+ "type=filterlist",
+ "text=" + normalized,
+ "csp=c s p",
+ "domains=domain.com|foo.com",
+ "sitekeys=FOO",
+ "contentType=" + t.CSP
+ ]
+ );
-exports.testElemHideRulesWithBraces = function(test)
-{
- compareFilter(
- test, "###foo{color: red}", [
- "type=elemhide",
- "text=###foo{color: red}",
- "selectorDomains=",
- "selector=#foo{color: red}",
- "domains="
- ]
- );
- compareFilter(
- test, "foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", [
- "type=elemhideemulation",
- "text=foo.com#?#:-abp-properties(/margin: [3-4]{2}/)",
- "selectorDomains=foo.com",
- "selector=:-abp-properties(/margin: [3-4]{2}/)",
- "domains=foo.com"
- ]
- );
- test.done();
-};
-
-exports.testSnippetFilters = function(test)
-{
- compareFilter(test, "foo.com#$#abc", ["type=snippet", "text=foo.com#$#abc", "scriptDomains=foo.com", "script=abc", "domains=foo.com"]);
- compareFilter(test, "foo.com,~bar.com#$#abc", ["type=snippet", "text=foo.com,~bar.com#$#abc", "scriptDomains=foo.com", "script=abc", "domains=foo.com|~bar.com"]);
- compareFilter(test, "foo.com,~bar#$#abc", ["type=snippet", "text=foo.com,~bar#$#abc", "scriptDomains=foo.com", "script=abc", "domains=foo.com|~bar"]);
- compareFilter(test, "~foo.com,bar.com#$#abc", ["type=snippet", "text=~foo.com,bar.com#$#abc", "scriptDomains=bar.com", "script=abc", "domains=bar.com|~foo.com"]);
-
- test.done();
-};
-
-exports.testFilterNormalization = function(test)
-{
- // Line breaks etc
- test.equal(Filter.normalize("\n\t\nad\ns"),
- "ads");
-
- // Comment filters
- test.equal(Filter.normalize(" ! fo o## bar "),
- "! fo o## bar");
-
- // Element hiding filters
- test.equal(Filter.normalize(" domain.c om## # sele ctor "),
- "domain.com### sele ctor");
+ // Some $csp edge cases
+ assert.equal(Filter.normalize("$csp= "),
+ "$csp=");
+ assert.equal(Filter.normalize("$csp= c s p"),
+ "$csp=c s p");
+ assert.equal(Filter.normalize("$$csp= c s p"),
+ "$$csp=c s p");
+ assert.equal(Filter.normalize("$$$csp= c s p"),
+ "$$$csp=c s p");
+ assert.equal(Filter.normalize("foo?csp=b a r$csp=script-src 'self'"),
+ "foo?csp=bar$csp=script-src 'self'");
+ assert.equal(Filter.normalize("foo$bar=c s p = ba z,cs p = script-src 'self'"),
+ "foo$bar=csp=baz,csp=script-src 'self'");
+ assert.equal(Filter.normalize("foo$csp=c s p csp= ba z,cs p = script-src 'self'"),
+ "foo$csp=c s p csp= ba z,csp=script-src 'self'");
+ assert.equal(Filter.normalize("foo$csp=bar,$c sp=c s p"),
+ "foo$csp=bar,$csp=c s p");
+ assert.equal(Filter.normalize(" f o o $ bar $csp=ba r"),
+ "foo$bar$csp=ba r");
+ assert.equal(Filter.normalize("f $ o $ o $ csp=f o o "),
+ "f$o$o$csp=f o o");
+ assert.equal(Filter.normalize("/foo$/$ csp = script-src http://example.com/?$1=1&$2=2&$3=3"),
+ "/foo$/$csp=script-src http://example.com/?$1=1&$2=2&$3=3");
+ assert.equal(Filter.normalize("||content.server.com/files/*.php$rewrite= $1"),
+ "||content.server.com/files/*.php$rewrite=$1");
+ });
- // Element hiding emulation filters
- test.equal(Filter.normalize(" domain.c om#?# # sele ctor "),
- "domain.com#?## sele ctor");
+ it("Filter rewrite option", () =>
+ {
+ let text = "/(content\\.server\\/file\\/.*\\.txt)\\?.*$/$rewrite=$1";
- // Incorrect syntax: the separator "#?#" cannot contain spaces; treated as a
- // regular filter instead
- test.equal(Filter.normalize(" domain.c om# ?#. sele ctor "),
- "domain.com#?#.selector");
- // Incorrect syntax: the separator "#?#" cannot contain spaces; treated as an
- // element hiding filter instead, because the "##" following the "?" is taken
- // to be the separator instead
- test.equal(Filter.normalize(" domain.c om# ?##sele ctor "),
- "domain.com#?##sele ctor");
-
- // Element hiding exception filters
- test.equal(Filter.normalize(" domain.c om#@# # sele ctor "),
- "domain.com#@## sele ctor");
+ let filter = Filter.fromText(text);
- // Incorrect syntax: the separator "#@#" cannot contain spaces; treated as a
- // regular filter instead (not an element hiding filter either!), because
- // unlike the case with "# ?##" the "##" following the "@" is not considered
- // to be a separator
- test.equal(Filter.normalize(" domain.c om# @## sele ctor "),
- "domain.com#@##selector");
-
- // Snippet filters
- test.equal(Filter.normalize(" domain.c om#$# sni pp et "),
- "domain.com#$#sni pp et");
+ assert.equal(filter.rewrite, "$1");
+ // no rewrite occured: didn't match.
+ assert.equal(filter.rewriteUrl("foo"), "foo");
+ // rewrite occured: matched.
+ assert.equal(filter.rewriteUrl("http://content.server/file/foo.txt?bar"),
+ "http://content.server/file/foo.txt");
- // Regular filters
- let normalized = Filter.normalize(
- " b$l a$sitekey= foo ,domain= do main.com |foo .com,c sp= c s p "
- );
- test.equal(
- normalized,
- "b$la$sitekey=foo,domain=domain.com|foo.com,csp=c s p"
- );
- compareFilter(
- test, normalized, [
- "type=filterlist",
- "text=" + normalized,
- "csp=c s p",
- "domains=domain.com|foo.com",
- "sitekeys=FOO",
- "contentType=" + t.CSP
- ]
- );
+ // checking for same origin.
+ let rewriteDiffOrigin =
+ "/content\\.server(\\/file\\/.*\\.txt)\\?.*$/$rewrite=foo.com$1";
+ let filterDiffOrigin = Filter.fromText(rewriteDiffOrigin);
+
+ // no rewrite occured because of a different origin.
+ assert.equal(
+ filterDiffOrigin.rewriteUrl("http://content.server/file/foo.txt?bar"),
+ "http://content.server/file/foo.txt?bar"
+ );
- // Some $csp edge cases
- test.equal(Filter.normalize("$csp= "),
- "$csp=");
- test.equal(Filter.normalize("$csp= c s p"),
- "$csp=c s p");
- test.equal(Filter.normalize("$$csp= c s p"),
- "$$csp=c s p");
- test.equal(Filter.normalize("$$$csp= c s p"),
- "$$$csp=c s p");
- test.equal(Filter.normalize("foo?csp=b a r$csp=script-src 'self'"),
- "foo?csp=bar$csp=script-src 'self'");
- test.equal(Filter.normalize("foo$bar=c s p = ba z,cs p = script-src 'self'"),
- "foo$bar=csp=baz,csp=script-src 'self'");
- test.equal(Filter.normalize("foo$csp=c s p csp= ba z,cs p = script-src 'self'"),
- "foo$csp=c s p csp= ba z,csp=script-src 'self'");
- test.equal(Filter.normalize("foo$csp=bar,$c sp=c s p"),
- "foo$csp=bar,$csp=c s p");
- test.equal(Filter.normalize(" f o o $ bar $csp=ba r"),
- "foo$bar$csp=ba r");
- test.equal(Filter.normalize("f $ o $ o $ csp=f o o "),
- "f$o$o$csp=f o o");
- test.equal(Filter.normalize("/foo$/$ csp = script-src http://example.com/?$1=1&$2=2&$3=3"),
- "/foo$/$csp=script-src http://example.com/?$1=1&$2=2&$3=3");
- test.equal(Filter.normalize("||content.server.com/files/*.php$rewrite= $1"),
- "||content.server.com/files/*.php$rewrite=$1");
- test.done();
-};
+ // relative path.
+ let rewriteRelative = "/(\\/file\\/.*\\.txt)\\?.*$/$rewrite=$1/disable";
+ let filterRelative = Filter.fromText(rewriteRelative);
+
+ assert.equal(
+ filterRelative.rewriteUrl("http://content.server/file/foo.txt?bar"),
+ "http://content.server/file/foo.txt/disable"
+ );
+ assert.equal(
+ filterRelative.rewriteUrl("http://example.com/file/foo.txt?bar"),
+ "http://example.com/file/foo.txt/disable"
+ );
-exports.testFilterRewriteOption = function(test)
-{
- let text = "/(content\\.server\\/file\\/.*\\.txt)\\?.*$/$rewrite=$1";
-
- let filter = Filter.fromText(text);
+ // Example from https://github.com/uBlockOrigin/uBlock-issues/issues/46#issuecomment-391190533
+ // The rewrite shouldn't happen.
+ let rewriteEvil = "/(^https?:\\/\\/[^/])/$script,rewrite=$1.evil.com";
+ let filterEvil = Filter.fromText(rewriteEvil);
- test.equal(filter.rewrite, "$1");
- // no rewrite occured: didn't match.
- test.equal(filter.rewriteUrl("foo"), "foo");
- // rewrite occured: matched.
- test.equal(filter.rewriteUrl("http://content.server/file/foo.txt?bar"),
- "http://content.server/file/foo.txt");
+ assert.equal(
+ filterEvil.rewriteUrl("https://www.adblockplus.org/script.js"),
+ "https://www.adblockplus.org/script.js"
+ );
- // checking for same origin.
- let rewriteDiffOrigin =
- "/content\\.server(\\/file\\/.*\\.txt)\\?.*$/$rewrite=foo.com$1";
- let filterDiffOrigin = Filter.fromText(rewriteDiffOrigin);
-
- // no rewrite occured because of a different origin.
- test.equal(
- filterDiffOrigin.rewriteUrl("http://content.server/file/foo.txt?bar"),
- "http://content.server/file/foo.txt?bar"
- );
+ // Strip.
+ let rewriteStrip = "tag$rewrite=";
+ let filterStrip = Filter.fromText(rewriteStrip);
- // relative path.
- let rewriteRelative = "/(\\/file\\/.*\\.txt)\\?.*$/$rewrite=$1/disable";
- let filterRelative = Filter.fromText(rewriteRelative);
-
- test.equal(
- filterRelative.rewriteUrl("http://content.server/file/foo.txt?bar"),
- "http://content.server/file/foo.txt/disable"
- );
- test.equal(
- filterRelative.rewriteUrl("http://example.com/file/foo.txt?bar"),
- "http://example.com/file/foo.txt/disable"
- );
+ assert.equal(filterStrip.rewrite, "");
+ assert.equal(
+ filterStrip.rewriteUrl("http://example.com/?tag"),
+ "http://example.com/?"
+ );
+ });
- // Example from https://github.com/uBlockOrigin/uBlock-issues/issues/46#issuecomment-391190533
- // The rewrite shouldn't happen.
- let rewriteEvil = "/(^https?:\\/\\/[^/])/$script,rewrite=$1.evil.com";
- let filterEvil = Filter.fromText(rewriteEvil);
-
- test.equal(
- filterEvil.rewriteUrl("https://www.adblockplus.org/script.js"),
- "https://www.adblockplus.org/script.js"
- );
-
- // Strip.
- let rewriteStrip = "tag$rewrite=";
- let filterStrip = Filter.fromText(rewriteStrip);
+ it("Domain map deduplication", () =>
+ {
+ let filter1 = Filter.fromText("foo$domain=blocking.example.com");
+ let filter2 = Filter.fromText("bar$domain=blocking.example.com");
+ let filter3 = Filter.fromText("elemhide.example.com##.foo");
+ let filter4 = Filter.fromText("elemhide.example.com##.bar");
- test.equal(filterStrip.rewrite, "");
- test.equal(
- filterStrip.rewriteUrl("http://example.com/?tag"),
- "http://example.com/?"
- );
+ // This compares the references to make sure that both refer to the same
+ // object (#6815).
- test.done();
-};
+ assert.equal(filter1.domains, filter2.domains);
+ assert.equal(filter3.domains, filter4.domains);
-exports.testDomainMapDeduplication = function(test)
-{
- let filter1 = Filter.fromText("foo$domain=blocking.example.com");
- let filter2 = Filter.fromText("bar$domain=blocking.example.com");
- let filter3 = Filter.fromText("elemhide.example.com##.foo");
- let filter4 = Filter.fromText("elemhide.example.com##.bar");
-
- // This compares the references to make sure that both refer to the same
- // object (#6815).
+ let filter5 = Filter.fromText("bar$domain=www.example.com");
+ let filter6 = Filter.fromText("www.example.com##.bar");
- test.equal(filter1.domains, filter2.domains);
- test.equal(filter3.domains, filter4.domains);
-
- let filter5 = Filter.fromText("bar$domain=www.example.com");
- let filter6 = Filter.fromText("www.example.com##.bar");
-
- test.notEqual(filter2.domains, filter5.domains);
- test.notEqual(filter4.domains, filter6.domains);
-
- test.done();
-};
+ assert.notEqual(filter2.domains, filter5.domains);
+ assert.notEqual(filter4.domains, filter6.domains);
+ });
+});
« no previous file with comments | « test/elemHide.js ('k') | test/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld