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

Unified Diff: test/filterClasses.js

Issue 29737558: Issue 6538, 6781 - Implement support for snippet filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Fix rebase Created May 7, 2018, 6:57 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 | « lib/snippets.js ('k') | test/snippets.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
@@ -21,32 +21,36 @@
let Filter = null;
let InvalidFilter = null;
let CommentFilter = null;
let ActiveFilter = null;
let RegExpFilter = null;
let BlockingFilter = null;
let WhitelistFilter = null;
+let CodeInjectionFilter = null;
let ElemHideBase = null;
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, ElemHideBase, ElemHideFilter,
+ BlockingFilter, WhitelistFilter, CodeInjectionFilter, ElemHideBase,
+ ElemHideFilter,
ElemHideException,
- ElemHideEmulationFilter} = sandboxedRequire("../lib/filterClasses")
+ ElemHideEmulationFilter,
+ SnippetFilter} = sandboxedRequire("../lib/filterClasses")
);
t = RegExpFilter.typeMap;
defaultTypes = 0x7FFFFFFF & ~(t.CSP | t.ELEMHIDE | t.DOCUMENT | t.POPUP |
t.GENERICHIDE | t.GENERICBLOCK);
callback();
};
@@ -93,27 +97,29 @@
{
result.push("type=filterlist");
result.push("collapse=" + filter.collapse);
result.push("csp=" + filter.csp);
}
else if (filter instanceof WhitelistFilter)
result.push("type=whitelist");
}
- else if (filter instanceof ElemHideBase)
+ else if (filter instanceof CodeInjectionFilter)
{
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");
+ else if (filter instanceof SnippetFilter)
+ result.push("type=snippet");
- result.push("selectorDomains=" + (filter.selectorDomains || ""));
- result.push("selector=" + filter.selector);
+ result.push("injectionDomains=" + (filter.injectionDomains || ""));
+ result.push("code=" + filter.code);
}
}
return result;
}
function addDefaults(expected)
{
let type = null;
@@ -128,17 +134,18 @@
function addProperty(prop, value)
{
if (!(prop in hasProperty))
expected.push(prop + "=" + value);
}
if (type == "whitelist" || type == "filterlist" || type == "elemhide" ||
- type == "elemhideexception" || type == "elemhideemulation")
+ type == "elemhideexception" || type == "elemhideemulation" ||
+ type == "snippet")
{
addProperty("disabled", "false");
addProperty("lastHit", "0");
addProperty("hitCount", "0");
}
if (type == "whitelist" || type == "filterlist")
{
addProperty("contentType", 0x7FFFFFFF & ~(
@@ -150,19 +157,19 @@
addProperty("sitekeys", "");
}
if (type == "filterlist")
{
addProperty("collapse", "null");
addProperty("csp", "null");
}
if (type == "elemhide" || type == "elemhideexception" ||
- type == "elemhideemulation")
+ type == "elemhideemulation" || type == "snippet")
{
- addProperty("selectorDomains", "");
+ addProperty("injectionDomains", "");
addProperty("domains", "");
}
}
function compareFilter(test, text, expected, postInit)
{
addDefaults(expected);
@@ -196,21 +203,24 @@
{
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 WhitelistFilter, "function", "typeof WhitelistFilter");
+ test.equal(typeof CodeInjectionFilter, "function",
+ "typeof CodeInjectionFilter");
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");
test.done();
};
exports.testComments = function(test)
{
compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]);
compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]);
@@ -321,62 +331,62 @@
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();
};
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"]);
+ compareFilter(test, "##ddd", ["type=elemhide", "text=##ddd", "code=ddd"]);
+ compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body > div:first-child", "code=body > div:first-child"]);
+ compareFilter(test, "foo##ddd", ["type=elemhide", "text=foo##ddd", "injectionDomains=foo", "code=ddd", "domains=FOO"]);
+ compareFilter(test, "foo,bar##ddd", ["type=elemhide", "text=foo,bar##ddd", "injectionDomains=foo,bar", "code=ddd", "domains=BAR|FOO"]);
+ compareFilter(test, "foo,~bar##ddd", ["type=elemhide", "text=foo,~bar##ddd", "injectionDomains=foo", "code=ddd", "domains=FOO|~BAR"]);
+ compareFilter(test, "foo,~baz,bar##ddd", ["type=elemhide", "text=foo,~baz,bar##ddd", "injectionDomains=foo,bar", "code=ddd", "domains=BAR|FOO|~BAZ"]);
test.done();
};
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"]);
+ compareFilter(test, "#@#ddd", ["type=elemhideexception", "text=#@#ddd", "code=ddd"]);
+ compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "text=#@#body > div:first-child", "code=body > div:first-child"]);
+ compareFilter(test, "foo#@#ddd", ["type=elemhideexception", "text=foo#@#ddd", "injectionDomains=foo", "code=ddd", "domains=FOO"]);
+ compareFilter(test, "foo,bar#@#ddd", ["type=elemhideexception", "text=foo,bar#@#ddd", "injectionDomains=foo,bar", "code=ddd", "domains=BAR|FOO"]);
+ compareFilter(test, "foo,~bar#@#ddd", ["type=elemhideexception", "text=foo,~bar#@#ddd", "injectionDomains=foo", "code=ddd", "domains=FOO|~BAR"]);
+ compareFilter(test, "foo,~baz,bar#@#ddd", ["type=elemhideexception", "text=foo,~baz,bar#@#ddd", "injectionDomains=foo,bar", "code=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"]);
+ compareFilter(test, "foo.com#?#:-abp-properties(abc)", ["type=elemhideemulation", "text=foo.com#?#:-abp-properties(abc)", "injectionDomains=foo.com", "code=:-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)", "injectionDomains=foo.com", "code=:-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)", "injectionDomains=foo.com", "code=:-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)", "injectionDomains=bar.com", "code=:-abp-properties(abc)", "domains=BAR.COM|~FOO.COM"]);
// 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"]);
+ compareFilter(test, "foo.com#?#abc", ["type=elemhideemulation", "text=foo.com#?#abc", "injectionDomains=foo.com", "code=abc", "domains=FOO.COM"]);
+ compareFilter(test, "foo.com#?#:-abp-foobar(abc)", ["type=elemhideemulation", "text=foo.com#?#:-abp-foobar(abc)", "injectionDomains=foo.com", "code=:-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", "injectionDomains=foo.com", "code=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:*))", "injectionDomains=foo.com", "code=:-abp-properties(|background-image: url(data:*))", "domains=FOO.COM"]);
// Check conversion of legacy filters
- 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##[-abp-properties='abc']", ["type=elemhideemulation", "text=foo.com#?#:-abp-properties(abc)", "injectionDomains=foo.com", "code=:-abp-properties(abc)", "domains=FOO.COM"]);
test.equal(Filter.fromText("foo.com##[-abp-properties='abc']"), Filter.fromText("foo.com#?#:-abp-properties(abc)"));
- compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexception", "text=foo.com#@#[-abp-properties='abc']", "selectorDomains=foo.com", "selector=[-abp-properties='abc']", "domains=FOO.COM"]);
- 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##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='abc']", ["type=elemhideexception", "text=foo.com#@#[-abp-properties='abc']", "injectionDomains=foo.com", "code=[-abp-properties='abc']", "domains=FOO.COM"]);
+ compareFilter(test, "foo.com#?#[-abp-properties='abc']", ["type=elemhideemulation", "text=foo.com#?#[-abp-properties='abc']", "injectionDomains=foo.com", "code=[-abp-properties='abc']", "domains=FOO.COM"]);
+ compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=elemhideemulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "injectionDomains=foo.com", "code=aaa :-abp-properties(abc) bbb", "domains=FOO.COM"]);
// test matching -abp-properties= (https://issues.adblockplus.org/ticket/5037).
- compareFilter(test, "foo.com##[-abp-properties-bogus='abc']", ["type=elemhide", "text=foo.com##[-abp-properties-bogus='abc']", "selectorDomains=foo.com", "selector=[-abp-properties-bogus='abc']", "domains=FOO.COM"]);
+ compareFilter(test, "foo.com##[-abp-properties-bogus='abc']", ["type=elemhide", "text=foo.com##[-abp-properties-bogus='abc']", "injectionDomains=foo.com", "code=[-abp-properties-bogus='abc']", "domains=FOO.COM"]);
test.done();
};
exports.testEmptyElemHideDomains = function(test)
{
let emptyDomainFilters = [
",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector",
@@ -395,33 +405,43 @@
};
exports.testElemHideRulesWithBraces = function(test)
{
compareFilter(
test, "###foo{color: red}", [
"type=elemhide",
"text=###foo{color: red}",
- "selectorDomains=",
- "selector=#foo\\7B color: red\\7D ",
+ "injectionDomains=",
+ "code=#foo\\7B color: red\\7D ",
"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]\\7B 2\\7D /)",
+ "injectionDomains=foo.com",
+ "code=:-abp-properties(/margin: [3-4]\\7B 2\\7D /)",
"domains=FOO.COM"
]
);
test.done();
};
+exports.testSnippetFilters = function(test)
+{
+ compareFilter(test, "foo.com#$#abc", ["type=snippet", "text=foo.com#$#abc", "injectionDomains=foo.com", "code=abc", "domains=FOO.COM"]);
+ compareFilter(test, "foo.com,~bar.com#$#abc", ["type=snippet", "text=foo.com,~bar.com#$#abc", "injectionDomains=foo.com", "code=abc", "domains=FOO.COM|~BAR.COM"]);
+ compareFilter(test, "foo.com,~bar#$#abc", ["type=snippet", "text=foo.com,~bar#$#abc", "injectionDomains=foo.com", "code=abc", "domains=FOO.COM|~BAR"]);
+ compareFilter(test, "~foo.com,bar.com#$#abc", ["type=snippet", "text=~foo.com,bar.com#$#abc", "injectionDomains=bar.com", "code=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 "),
@@ -451,16 +471,20 @@
// 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");
+
// 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"
);
« no previous file with comments | « lib/snippets.js ('k') | test/snippets.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld