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

Delta Between Two Patch Sets: test/filterClasses.js

Issue 29737558: Issue 6538, 6781 - Implement support for snippet filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Rebase Created May 7, 2018, 6:50 p.m.
Right Patch Set: Improve comment formatting Created July 11, 2018, 1:02 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-present eyeo GmbH 3 * Copyright (C) 2006-present eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 "use strict"; 18 "use strict";
19 19
20 const {createSandbox} = require("./_common"); 20 const {createSandbox} = require("./_common");
21 21
22 let Filter = null; 22 let Filter = null;
23 let InvalidFilter = null; 23 let InvalidFilter = null;
24 let CommentFilter = null; 24 let CommentFilter = null;
25 let ActiveFilter = null; 25 let ActiveFilter = null;
26 let RegExpFilter = null; 26 let RegExpFilter = null;
27 let BlockingFilter = null; 27 let BlockingFilter = null;
28 let ContentFilter = null;
28 let WhitelistFilter = null; 29 let WhitelistFilter = null;
29 let CodeInjectionFilter = null;
30 let ElemHideBase = null; 30 let ElemHideBase = null;
31 let ElemHideFilter = null; 31 let ElemHideFilter = null;
32 let ElemHideException = null; 32 let ElemHideException = null;
33 let ElemHideEmulationFilter = null; 33 let ElemHideEmulationFilter = null;
34 let SnippetFilter = null; 34 let SnippetFilter = null;
35 35
36 let t = null; 36 let t = null;
37 let defaultTypes = null; 37 let defaultTypes = null;
38 38
39 exports.setUp = function(callback) 39 exports.setUp = function(callback)
40 { 40 {
41 let sandboxedRequire = createSandbox(); 41 let sandboxedRequire = createSandbox();
42 ( 42 (
43 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, 43 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter,
44 BlockingFilter, WhitelistFilter, CodeInjectionFilter, ElemHideBase, 44 BlockingFilter, WhitelistFilter, ContentFilter, ElemHideBase,
45 ElemHideFilter, 45 ElemHideFilter, ElemHideException, ElemHideEmulationFilter,
46 ElemHideException,
47 ElemHideEmulationFilter,
48 SnippetFilter} = sandboxedRequire("../lib/filterClasses") 46 SnippetFilter} = sandboxedRequire("../lib/filterClasses")
49 ); 47 );
50 t = RegExpFilter.typeMap; 48 t = RegExpFilter.typeMap;
51 defaultTypes = 0x7FFFFFFF & ~(t.CSP | t.ELEMHIDE | t.DOCUMENT | t.POPUP | 49 defaultTypes = 0x7FFFFFFF & ~(t.CSP | t.ELEMHIDE | t.DOCUMENT | t.POPUP |
52 t.GENERICHIDE | t.GENERICBLOCK); 50 t.GENERICHIDE | t.GENERICBLOCK);
53 51
54 callback(); 52 callback();
55 }; 53 };
56 54
57 function serializeFilter(filter) 55 function serializeFilter(filter)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 89
92 let sitekeys = filter.sitekeys || []; 90 let sitekeys = filter.sitekeys || [];
93 result.push("sitekeys=" + sitekeys.slice().sort().join("|")); 91 result.push("sitekeys=" + sitekeys.slice().sort().join("|"));
94 92
95 result.push("thirdParty=" + filter.thirdParty); 93 result.push("thirdParty=" + filter.thirdParty);
96 if (filter instanceof BlockingFilter) 94 if (filter instanceof BlockingFilter)
97 { 95 {
98 result.push("type=filterlist"); 96 result.push("type=filterlist");
99 result.push("collapse=" + filter.collapse); 97 result.push("collapse=" + filter.collapse);
100 result.push("csp=" + filter.csp); 98 result.push("csp=" + filter.csp);
99 result.push("rewrite=" + filter.rewrite);
101 } 100 }
102 else if (filter instanceof WhitelistFilter) 101 else if (filter instanceof WhitelistFilter)
103 result.push("type=whitelist"); 102 result.push("type=whitelist");
104 } 103 }
105 else if (filter instanceof CodeInjectionFilter) 104 else if (filter instanceof ElemHideBase)
106 { 105 {
107 if (filter instanceof ElemHideFilter) 106 if (filter instanceof ElemHideFilter)
108 result.push("type=elemhide"); 107 result.push("type=elemhide");
109 else if (filter instanceof ElemHideException) 108 else if (filter instanceof ElemHideException)
110 result.push("type=elemhideexception"); 109 result.push("type=elemhideexception");
111 else if (filter instanceof ElemHideEmulationFilter) 110 else if (filter instanceof ElemHideEmulationFilter)
112 result.push("type=elemhideemulation"); 111 result.push("type=elemhideemulation");
113 else if (filter instanceof SnippetFilter) 112
114 result.push("type=snippet"); 113 result.push("selectorDomains=" +
115 114 [...filter.domains || []]
116 result.push("injectionDomains=" + (filter.injectionDomains || "")); 115 .filter(([domain, isIncluded]) => isIncluded)
117 result.push("code=" + filter.code); 116 .map(([domain]) => domain.toLowerCase()));
117 result.push("selector=" + filter.selector);
118 }
119 else if (filter instanceof SnippetFilter)
120 {
121 result.push("type=snippet");
122 result.push("scriptDomains=" +
123 [...filter.domains || []]
124 .filter(([domain, isIncluded]) => isIncluded)
125 .map(([domain]) => domain.toLowerCase()));
126 result.push("script=" + filter.script);
118 } 127 }
119 } 128 }
120 return result; 129 return result;
121 } 130 }
122 131
123 function addDefaults(expected) 132 function addDefaults(expected)
124 { 133 {
125 let type = null; 134 let type = null;
126 let hasProperty = {}; 135 let hasProperty = {};
127 for (let entry of expected) 136 for (let entry of expected)
(...skipping 25 matching lines...) Expand all
153 )); 162 ));
154 addProperty("matchCase", "false"); 163 addProperty("matchCase", "false");
155 addProperty("thirdParty", "null"); 164 addProperty("thirdParty", "null");
156 addProperty("domains", ""); 165 addProperty("domains", "");
157 addProperty("sitekeys", ""); 166 addProperty("sitekeys", "");
158 } 167 }
159 if (type == "filterlist") 168 if (type == "filterlist")
160 { 169 {
161 addProperty("collapse", "null"); 170 addProperty("collapse", "null");
162 addProperty("csp", "null"); 171 addProperty("csp", "null");
172 addProperty("rewrite", "null");
163 } 173 }
164 if (type == "elemhide" || type == "elemhideexception" || 174 if (type == "elemhide" || type == "elemhideexception" ||
165 type == "elemhideemulation" || type == "snippet") 175 type == "elemhideemulation")
166 { 176 {
167 addProperty("injectionDomains", ""); 177 addProperty("selectorDomains", "");
178 addProperty("domains", "");
179 }
180 if (type == "snippet")
181 {
182 addProperty("scriptDomains", "");
168 addProperty("domains", ""); 183 addProperty("domains", "");
169 } 184 }
170 } 185 }
171 186
172 function compareFilter(test, text, expected, postInit) 187 function compareFilter(test, text, expected, postInit)
173 { 188 {
174 addDefaults(expected); 189 addDefaults(expected);
175 190
176 let filter = Filter.fromText(text); 191 let filter = Filter.fromText(text);
177 if (postInit) 192 if (postInit)
(...skipping 22 matching lines...) Expand all
200 } 215 }
201 216
202 exports.testFilterClassDefinitions = function(test) 217 exports.testFilterClassDefinitions = function(test)
203 { 218 {
204 test.equal(typeof Filter, "function", "typeof Filter"); 219 test.equal(typeof Filter, "function", "typeof Filter");
205 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); 220 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
206 test.equal(typeof CommentFilter, "function", "typeof CommentFilter"); 221 test.equal(typeof CommentFilter, "function", "typeof CommentFilter");
207 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); 222 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
208 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); 223 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
209 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); 224 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
225 test.equal(typeof ContentFilter, "function", "typeof ContentFilter");
210 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); 226 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
211 test.equal(typeof CodeInjectionFilter, "function",
212 "typeof CodeInjectionFilter");
213 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); 227 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
214 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); 228 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
215 test.equal(typeof ElemHideException, "function", "typeof ElemHideException"); 229 test.equal(typeof ElemHideException, "function", "typeof ElemHideException");
216 test.equal(typeof ElemHideEmulationFilter, "function", 230 test.equal(typeof ElemHideEmulationFilter, "function",
217 "typeof ElemHideEmulationFilter"); 231 "typeof ElemHideEmulationFilter");
218 test.equal(typeof SnippetFilter, "function", "typeof SnippetFilter"); 232 test.equal(typeof SnippetFilter, "function", "typeof SnippetFilter");
219 233
220 test.done(); 234 test.done();
221 }; 235 };
222 236
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 compareFilter(test, "@@/ddd|f?a[s]d/", ["type=whitelist", "text=@@/ddd|f?a[s]d /", "regexp=ddd|f?a[s]d", "contentType=" + defaultTypes]); 303 compareFilter(test, "@@/ddd|f?a[s]d/", ["type=whitelist", "text=@@/ddd|f?a[s]d /", "regexp=ddd|f?a[s]d", "contentType=" + defaultTypes]);
290 compareFilter(test, "@@*asdf*d**dd*", ["type=whitelist", "text=@@*asdf*d**dd*" , "regexp=asdf.*d.*dd", "contentType=" + defaultTypes]); 304 compareFilter(test, "@@*asdf*d**dd*", ["type=whitelist", "text=@@*asdf*d**dd*" , "regexp=asdf.*d.*dd", "contentType=" + defaultTypes]);
291 compareFilter(test, "@@|*asd|f*d**dd*|", ["type=whitelist", "text=@@|*asd|f*d* *dd*|", "regexp=^.*asd\\|f.*d.*dd.*$", "contentType=" + defaultTypes]); 305 compareFilter(test, "@@|*asd|f*d**dd*|", ["type=whitelist", "text=@@|*asd|f*d* *dd*|", "regexp=^.*asd\\|f.*d.*dd.*$", "contentType=" + defaultTypes]);
292 compareFilter(test, "@@dd[]{}$%<>&()d", ["type=whitelist", "text=@@dd[]{}$%<>& ()d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d", "contentType=" + defaultTyp es]); 306 compareFilter(test, "@@dd[]{}$%<>&()d", ["type=whitelist", "text=@@dd[]{}$%<>& ()d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d", "contentType=" + defaultTyp es]);
293 307
294 test.done(); 308 test.done();
295 }; 309 };
296 310
297 exports.testFilterOptions = function(test) 311 exports.testFilterOptions = function(test)
298 { 312 {
299 compareFilter(test, "bla$match-case,csp=first csp,script,other,third-party,dom ain=foo.com,sitekey=foo", ["type=filterlist", "text=bla$match-case,csp=first csp ,script,other,third-party,domain=foo.com,sitekey=foo", "regexp=bla", "matchCase= true", "contentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domain s=FOO.COM", "sitekeys=FOO", "csp=first csp"]); 313 compareFilter(test, "bla$match-case,csp=first csp,script,other,third-party,dom ain=FOO.cOm,sitekey=foo", ["type=filterlist", "text=bla$match-case,csp=first csp ,script,other,third-party,domain=FOO.cOm,sitekey=foo", "regexp=bla", "matchCase= true", "contentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domain s=foo.com", "sitekeys=FOO", "csp=first csp"]);
300 compareFilter(test, "bla$~match-case,~csp=csp,~script,~other,~third-party,doma in=~bar.com", ["type=filterlist", "text=bla$~match-case,~csp=csp,~script,~other, ~third-party,domain=~bar.com", "regexp=bla", "contentType=" + (defaultTypes & ~( t.SCRIPT | t.OTHER)), "thirdParty=false", "domains=~BAR.COM"]); 314 compareFilter(test, "bla$~match-case,~csp=csp,~script,~other,~third-party,doma in=~bAr.coM", ["type=filterlist", "text=bla$~match-case,~csp=csp,~script,~other, ~third-party,domain=~bAr.coM", "regexp=bla", "contentType=" + (defaultTypes & ~( t.SCRIPT | t.OTHER)), "thirdParty=false", "domains=~bar.com"]);
301 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", "regexp=bla", "matchCase=true", "c ontentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domains=BAR.COM |FOO.COM|~BAR.FOO.COM|~FOO.BAR.COM", "sitekeys=BAR|FOO"]); 315 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", "regexp=bla", "matchCase=true", "c ontentType=" + (t.SCRIPT | t.OTHER | t.CSP), "thirdParty=true", "domains=bar.com |foo.com|~bar.foo.com|~foo.bar.com", "sitekeys=BAR|FOO"]);
302 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=@@b la$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo.com|~foo. bar.com,sitekey=foo|bar", "regexp=bla", "matchCase=true", "contentType=" + (t.SC RIPT | t.OTHER), "thirdParty=true", "domains=BAR.COM|FOO.COM|~BAR.FOO.COM|~FOO.B AR.COM", "sitekeys=BAR|FOO"]); 316 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=@@b la$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo.com|~foo. bar.com,sitekey=foo|bar", "regexp=bla", "matchCase=true", "contentType=" + (t.SC RIPT | t.OTHER), "thirdParty=true", "domains=bar.com|foo.com|~bar.foo.com|~foo.b ar.com", "sitekeys=BAR|FOO"]);
317 compareFilter(test, "||content.server.com/files/*.php$rewrite=$1", ["type=filt erlist", "text=||content.server.com/files/*.php$rewrite=$1", "regexp=^[\\w\\-]+: \\/+(?!\\/)(?:[^\\/]+\\.)?content\\.server\\.com\\/files\\/.*\\.php", "matchCase =false", "rewrite=$1", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.SUBDOCUME NT | t.OBJECT | t.OBJECT_SUBREQUEST))]);
303 318
304 // background and image should be the same for backwards compatibility 319 // background and image should be the same for backwards compatibility
305 compareFilter(test, "bla$image", ["type=filterlist", "text=bla$image", "regexp =bla", "contentType=" + (t.IMAGE)]); 320 compareFilter(test, "bla$image", ["type=filterlist", "text=bla$image", "regexp =bla", "contentType=" + (t.IMAGE)]);
306 compareFilter(test, "bla$background", ["type=filterlist", "text=bla$background ", "regexp=bla", "contentType=" + (t.IMAGE)]); 321 compareFilter(test, "bla$background", ["type=filterlist", "text=bla$background ", "regexp=bla", "contentType=" + (t.IMAGE)]);
307 compareFilter(test, "bla$~image", ["type=filterlist", "text=bla$~image", "rege xp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]); 322 compareFilter(test, "bla$~image", ["type=filterlist", "text=bla$~image", "rege xp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]);
308 compareFilter(test, "bla$~background", ["type=filterlist", "text=bla$~backgrou nd", "regexp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]); 323 compareFilter(test, "bla$~background", ["type=filterlist", "text=bla$~backgrou nd", "regexp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]);
309 324
310 compareFilter(test, "@@bla$~script,~other", ["type=whitelist", "text=@@bla$~sc ript,~other", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHE R))]); 325 compareFilter(test, "@@bla$~script,~other", ["type=whitelist", "text=@@bla$~sc ript,~other", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHE R))]);
311 compareFilter(test, "@@http://bla$~script,~other", ["type=whitelist", "text=@@ http://bla$~script,~other", "regexp=http\\:\\/\\/bla", "contentType=" + (default Types & ~(t.SCRIPT | t.OTHER))]); 326 compareFilter(test, "@@http://bla$~script,~other", ["type=whitelist", "text=@@ http://bla$~script,~other", "regexp=http\\:\\/\\/bla", "contentType=" + (default Types & ~(t.SCRIPT | t.OTHER))]);
312 compareFilter(test, "@@|ftp://bla$~script,~other", ["type=whitelist", "text=@@ |ftp://bla$~script,~other", "regexp=^ftp\\:\\/\\/bla", "contentType=" + (default Types & ~(t.SCRIPT | t.OTHER))]); 327 compareFilter(test, "@@|ftp://bla$~script,~other", ["type=whitelist", "text=@@ |ftp://bla$~script,~other", "regexp=^ftp\\:\\/\\/bla", "contentType=" + (default Types & ~(t.SCRIPT | t.OTHER))]);
(...skipping 16 matching lines...) Expand all
329 compareFilter(test, "bla$csp=foo,csp=referrer foo", ["type=invalid", "text=bla $csp=foo,csp=referrer foo", "reason=filter_invalid_csp"]); 344 compareFilter(test, "bla$csp=foo,csp=referrer foo", ["type=invalid", "text=bla $csp=foo,csp=referrer foo", "reason=filter_invalid_csp"]);
330 compareFilter(test, "bla$csp=foo,csp=base-uri", ["type=invalid", "text=bla$csp =foo,csp=base-uri", "reason=filter_invalid_csp"]); 345 compareFilter(test, "bla$csp=foo,csp=base-uri", ["type=invalid", "text=bla$csp =foo,csp=base-uri", "reason=filter_invalid_csp"]);
331 compareFilter(test, "bla$csp=foo,csp=upgrade-insecure-requests", ["type=invali d", "text=bla$csp=foo,csp=upgrade-insecure-requests", "reason=filter_invalid_csp "]); 346 compareFilter(test, "bla$csp=foo,csp=upgrade-insecure-requests", ["type=invali d", "text=bla$csp=foo,csp=upgrade-insecure-requests", "reason=filter_invalid_csp "]);
332 compareFilter(test, "bla$csp=foo,csp=ReFeRReR", ["type=invalid", "text=bla$csp =foo,csp=ReFeRReR", "reason=filter_invalid_csp"]); 347 compareFilter(test, "bla$csp=foo,csp=ReFeRReR", ["type=invalid", "text=bla$csp =foo,csp=ReFeRReR", "reason=filter_invalid_csp"]);
333 348
334 test.done(); 349 test.done();
335 }; 350 };
336 351
337 exports.testElementHidingRules = function(test) 352 exports.testElementHidingRules = function(test)
338 { 353 {
339 compareFilter(test, "##ddd", ["type=elemhide", "text=##ddd", "code=ddd"]); 354 compareFilter(test, "##ddd", ["type=elemhide", "text=##ddd", "selector=ddd"]);
340 compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body > div:first-child", "code=body > div:first-child"]); 355 compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body > div:first-child", "selector=body > div:first-child"]);
341 compareFilter(test, "foo##ddd", ["type=elemhide", "text=foo##ddd", "injectionD omains=foo", "code=ddd", "domains=FOO"]); 356 compareFilter(test, "fOO##ddd", ["type=elemhide", "text=fOO##ddd", "selectorDo mains=foo", "selector=ddd", "domains=foo"]);
342 compareFilter(test, "foo,bar##ddd", ["type=elemhide", "text=foo,bar##ddd", "in jectionDomains=foo,bar", "code=ddd", "domains=BAR|FOO"]); 357 compareFilter(test, "Foo,bAr##ddd", ["type=elemhide", "text=Foo,bAr##ddd", "se lectorDomains=foo,bar", "selector=ddd", "domains=bar|foo"]);
343 compareFilter(test, "foo,~bar##ddd", ["type=elemhide", "text=foo,~bar##ddd", " injectionDomains=foo", "code=ddd", "domains=FOO|~BAR"]); 358 compareFilter(test, "foo,~baR##ddd", ["type=elemhide", "text=foo,~baR##ddd", " selectorDomains=foo", "selector=ddd", "domains=foo|~bar"]);
344 compareFilter(test, "foo,~baz,bar##ddd", ["type=elemhide", "text=foo,~baz,bar# #ddd", "injectionDomains=foo,bar", "code=ddd", "domains=BAR|FOO|~BAZ"]); 359 compareFilter(test, "foo,~baz,bar##ddd", ["type=elemhide", "text=foo,~baz,bar# #ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo|~baz"]);
345 360
346 test.done(); 361 test.done();
347 }; 362 };
348 363
349 exports.testElementHidingExceptions = function(test) 364 exports.testElementHidingExceptions = function(test)
350 { 365 {
351 compareFilter(test, "#@#ddd", ["type=elemhideexception", "text=#@#ddd", "code= ddd"]); 366 compareFilter(test, "#@#ddd", ["type=elemhideexception", "text=#@#ddd", "selec tor=ddd"]);
352 compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "t ext=#@#body > div:first-child", "code=body > div:first-child"]); 367 compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "t ext=#@#body > div:first-child", "selector=body > div:first-child"]);
353 compareFilter(test, "foo#@#ddd", ["type=elemhideexception", "text=foo#@#ddd", "injectionDomains=foo", "code=ddd", "domains=FOO"]); 368 compareFilter(test, "fOO#@#ddd", ["type=elemhideexception", "text=fOO#@#ddd", "selectorDomains=foo", "selector=ddd", "domains=foo"]);
354 compareFilter(test, "foo,bar#@#ddd", ["type=elemhideexception", "text=foo,bar# @#ddd", "injectionDomains=foo,bar", "code=ddd", "domains=BAR|FOO"]); 369 compareFilter(test, "Foo,bAr#@#ddd", ["type=elemhideexception", "text=Foo,bAr# @#ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo"]);
355 compareFilter(test, "foo,~bar#@#ddd", ["type=elemhideexception", "text=foo,~ba r#@#ddd", "injectionDomains=foo", "code=ddd", "domains=FOO|~BAR"]); 370 compareFilter(test, "foo,~baR#@#ddd", ["type=elemhideexception", "text=foo,~ba R#@#ddd", "selectorDomains=foo", "selector=ddd", "domains=foo|~bar"]);
356 compareFilter(test, "foo,~baz,bar#@#ddd", ["type=elemhideexception", "text=foo ,~baz,bar#@#ddd", "injectionDomains=foo,bar", "code=ddd", "domains=BAR|FOO|~BAZ" ]); 371 compareFilter(test, "foo,~baz,bar#@#ddd", ["type=elemhideexception", "text=foo ,~baz,bar#@#ddd", "selectorDomains=foo,bar", "selector=ddd", "domains=bar|foo|~b az"]);
357 372
358 test.done(); 373 test.done();
359 }; 374 };
360 375
361 exports.testElemHideEmulationFilters = function(test) 376 exports.testElemHideEmulationFilters = function(test)
362 { 377 {
363 // Check valid domain combinations 378 // Check valid domain combinations
364 compareFilter(test, "foo.com#?#:-abp-properties(abc)", ["type=elemhideemulatio n", "text=foo.com#?#:-abp-properties(abc)", "injectionDomains=foo.com", "code=:- abp-properties(abc)", "domains=FOO.COM"]); 379 compareFilter(test, "fOO.cOm#?#:-abp-properties(abc)", ["type=elemhideemulatio n", "text=fOO.cOm#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selector =:-abp-properties(abc)", "domains=foo.com"]);
365 compareFilter(test, "foo.com,~bar.com#?#:-abp-properties(abc)", ["type=elemhid eemulation", "text=foo.com,~bar.com#?#:-abp-properties(abc)", "injectionDomains= foo.com", "code=:-abp-properties(abc)", "domains=FOO.COM|~BAR.COM"]); 380 compareFilter(test, "Foo.com,~bAr.com#?#:-abp-properties(abc)", ["type=elemhid eemulation", "text=Foo.com,~bAr.com#?#:-abp-properties(abc)", "selectorDomains=f oo.com", "selector=:-abp-properties(abc)", "domains=foo.com|~bar.com"]);
366 compareFilter(test, "foo.com,~bar#?#:-abp-properties(abc)", ["type=elemhideemu lation", "text=foo.com,~bar#?#:-abp-properties(abc)", "injectionDomains=foo.com" , "code=:-abp-properties(abc)", "domains=FOO.COM|~BAR"]); 381 compareFilter(test, "foo.com,~baR#?#:-abp-properties(abc)", ["type=elemhideemu lation", "text=foo.com,~baR#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selector=:-abp-properties(abc)", "domains=foo.com|~bar"]);
367 compareFilter(test, "~foo.com,bar.com#?#:-abp-properties(abc)", ["type=elemhid eemulation", "text=~foo.com,bar.com#?#:-abp-properties(abc)", "injectionDomains= bar.com", "code=:-abp-properties(abc)", "domains=BAR.COM|~FOO.COM"]); 382 compareFilter(test, "~foo.com,bar.com#?#:-abp-properties(abc)", ["type=elemhid eemulation", "text=~foo.com,bar.com#?#:-abp-properties(abc)", "selectorDomains=b ar.com", "selector=:-abp-properties(abc)", "domains=bar.com|~foo.com"]);
368 383
369 // Check some special cases 384 // Check some special cases
370 compareFilter(test, "#?#:-abp-properties(abc)", ["type=invalid", "text=#?#:-ab p-properties(abc)", "reason=filter_elemhideemulation_nodomain"]); 385 compareFilter(test, "#?#:-abp-properties(abc)", ["type=invalid", "text=#?#:-ab p-properties(abc)", "reason=filter_elemhideemulation_nodomain"]);
371 compareFilter(test, "foo.com#?#abc", ["type=elemhideemulation", "text=foo.com# ?#abc", "injectionDomains=foo.com", "code=abc", "domains=FOO.COM"]); 386 compareFilter(test, "foo.com#?#abc", ["type=elemhideemulation", "text=foo.com# ?#abc", "selectorDomains=foo.com", "selector=abc", "domains=foo.com"]);
372 compareFilter(test, "foo.com#?#:-abp-foobar(abc)", ["type=elemhideemulation", "text=foo.com#?#:-abp-foobar(abc)", "injectionDomains=foo.com", "code=:-abp-foob ar(abc)", "domains=FOO.COM"]); 387 compareFilter(test, "foo.com#?#:-abp-foobar(abc)", ["type=elemhideemulation", "text=foo.com#?#:-abp-foobar(abc)", "selectorDomains=foo.com", "selector=:-abp-f oobar(abc)", "domains=foo.com"]);
373 compareFilter(test, "foo.com#?#aaa :-abp-properties(abc) bbb", ["type=elemhide emulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "injectionDomains=fo o.com", "code=aaa :-abp-properties(abc) bbb", "domains=FOO.COM"]); 388 compareFilter(test, "foo.com#?#aaa :-abp-properties(abc) bbb", ["type=elemhide emulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "selectorDomains=foo .com", "selector=aaa :-abp-properties(abc) bbb", "domains=foo.com"]);
374 compareFilter(test, "foo.com#?#:-abp-properties(|background-image: url(data:*) )", ["type=elemhideemulation", "text=foo.com#?#:-abp-properties(|background-imag e: url(data:*))", "injectionDomains=foo.com", "code=:-abp-properties(|background -image: url(data:*))", "domains=FOO.COM"]); 389 compareFilter(test, "foo.com#?#:-abp-properties(|background-image: url(data:*) )", ["type=elemhideemulation", "text=foo.com#?#:-abp-properties(|background-imag e: url(data:*))", "selectorDomains=foo.com", "selector=:-abp-properties(|backgro und-image: url(data:*))", "domains=foo.com"]);
375 390
376 // Check conversion of legacy filters 391 // Check conversion of legacy filters
377 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati on", "text=foo.com#?#:-abp-properties(abc)", "injectionDomains=foo.com", "code=: -abp-properties(abc)", "domains=FOO.COM"]); 392 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati on", "text=foo.com#?#:-abp-properties(abc)", "selectorDomains=foo.com", "selecto r=:-abp-properties(abc)", "domains=foo.com"]);
378 test.equal(Filter.fromText("foo.com##[-abp-properties='abc']"), Filter.fromTex t("foo.com#?#:-abp-properties(abc)")); 393 test.equal(Filter.fromText("foo.com##[-abp-properties='abc']"), Filter.fromTex t("foo.com#?#:-abp-properties(abc)"));
379 compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexcept ion", "text=foo.com#@#[-abp-properties='abc']", "injectionDomains=foo.com", "cod e=[-abp-properties='abc']", "domains=FOO.COM"]); 394 compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexcept ion", "text=foo.com#@#[-abp-properties='abc']", "selectorDomains=foo.com", "sele ctor=[-abp-properties='abc']", "domains=foo.com"]);
380 compareFilter(test, "foo.com#?#[-abp-properties='abc']", ["type=elemhideemulat ion", "text=foo.com#?#[-abp-properties='abc']", "injectionDomains=foo.com", "cod e=[-abp-properties='abc']", "domains=FOO.COM"]); 395 compareFilter(test, "foo.com#?#[-abp-properties='abc']", ["type=elemhideemulat ion", "text=foo.com#?#[-abp-properties='abc']", "selectorDomains=foo.com", "sele ctor=[-abp-properties='abc']", "domains=foo.com"]);
381 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=elemhid eemulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "injectionDomains=f oo.com", "code=aaa :-abp-properties(abc) bbb", "domains=FOO.COM"]); 396 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=elemhid eemulation", "text=foo.com#?#aaa :-abp-properties(abc) bbb", "selectorDomains=fo o.com", "selector=aaa :-abp-properties(abc) bbb", "domains=foo.com"]);
382 397
383 // test matching -abp-properties= (https://issues.adblockplus.org/ticket/5037) . 398 // test matching -abp-properties= (https://issues.adblockplus.org/ticket/5037) .
384 compareFilter(test, "foo.com##[-abp-properties-bogus='abc']", ["type=elemhide" , "text=foo.com##[-abp-properties-bogus='abc']", "injectionDomains=foo.com", "co de=[-abp-properties-bogus='abc']", "domains=FOO.COM"]); 399 compareFilter(test, "foo.com##[-abp-properties-bogus='abc']", ["type=elemhide" , "text=foo.com##[-abp-properties-bogus='abc']", "selectorDomains=foo.com", "sel ector=[-abp-properties-bogus='abc']", "domains=foo.com"]);
385 400
386 test.done(); 401 test.done();
387 }; 402 };
388 403
389 exports.testEmptyElemHideDomains = function(test) 404 exports.testEmptyElemHideDomains = function(test)
390 { 405 {
391 let emptyDomainFilters = [ 406 let emptyDomainFilters = [
392 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector", 407 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector",
393 ",foo.com##selector", "foo.com,~##selector", 408 ",foo.com##selector", "foo.com,~##selector",
394 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector" 409 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector"
395 ]; 410 ];
396 411
397 for (let filterText of emptyDomainFilters) 412 for (let filterText of emptyDomainFilters)
398 { 413 {
399 let filter = Filter.fromText(filterText); 414 let filter = Filter.fromText(filterText);
400 test.ok(filter instanceof InvalidFilter); 415 test.ok(filter instanceof InvalidFilter);
401 test.equal(filter.reason, "filter_invalid_domain"); 416 test.equal(filter.reason, "filter_invalid_domain");
402 } 417 }
403 418
404 test.done(); 419 test.done();
405 }; 420 };
406 421
407 exports.testElemHideRulesWithBraces = function(test) 422 exports.testElemHideRulesWithBraces = function(test)
408 { 423 {
409 compareFilter( 424 compareFilter(
410 test, "###foo{color: red}", [ 425 test, "###foo{color: red}", [
411 "type=elemhide", 426 "type=elemhide",
412 "text=###foo{color: red}", 427 "text=###foo{color: red}",
413 "injectionDomains=", 428 "selectorDomains=",
414 "code=#foo\\7B color: red\\7D ", 429 "selector=#foo\\7B color: red\\7D ",
415 "domains=" 430 "domains="
416 ] 431 ]
417 ); 432 );
418 compareFilter( 433 compareFilter(
419 test, "foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", [ 434 test, "foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", [
420 "type=elemhideemulation", 435 "type=elemhideemulation",
421 "text=foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", 436 "text=foo.com#?#:-abp-properties(/margin: [3-4]{2}/)",
422 "injectionDomains=foo.com", 437 "selectorDomains=foo.com",
423 "code=:-abp-properties(/margin: [3-4]\\7B 2\\7D /)", 438 "selector=:-abp-properties(/margin: [3-4]\\7B 2\\7D /)",
424 "domains=FOO.COM" 439 "domains=foo.com"
425 ] 440 ]
426 ); 441 );
427 test.done(); 442 test.done();
428 }; 443 };
429 444
430 exports.testSnippetFilters = function(test) 445 exports.testSnippetFilters = function(test)
431 { 446 {
432 compareFilter(test, "foo.com#$#abc", ["type=snippet", "text=foo.com#$#abc", "i njectionDomains=foo.com", "code=abc", "domains=FOO.COM"]); 447 compareFilter(test, "foo.com#$#abc", ["type=snippet", "text=foo.com#$#abc", "s criptDomains=foo.com", "script=abc", "domains=foo.com"]);
433 compareFilter(test, "foo.com,~bar.com#$#abc", ["type=snippet", "text=foo.com,~ bar.com#$#abc", "injectionDomains=foo.com", "code=abc", "domains=FOO.COM|~BAR.CO M"]); 448 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 "]);
434 compareFilter(test, "foo.com,~bar#$#abc", ["type=snippet", "text=foo.com,~bar# $#abc", "injectionDomains=foo.com", "code=abc", "domains=FOO.COM|~BAR"]); 449 compareFilter(test, "foo.com,~bar#$#abc", ["type=snippet", "text=foo.com,~bar# $#abc", "scriptDomains=foo.com", "script=abc", "domains=foo.com|~bar"]);
435 compareFilter(test, "~foo.com,bar.com#$#abc", ["type=snippet", "text=~foo.com, bar.com#$#abc", "injectionDomains=bar.com", "code=abc", "domains=BAR.COM|~FOO.CO M"]); 450 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 "]);
436 451
437 test.done(); 452 test.done();
438 }; 453 };
439 454
440 exports.testFilterNormalization = function(test) 455 exports.testFilterNormalization = function(test)
441 { 456 {
442 // Line breaks etc 457 // Line breaks etc
443 test.equal(Filter.normalize("\n\t\nad\ns"), 458 test.equal(Filter.normalize("\n\t\nad\ns"),
444 "ads"); 459 "ads");
445 460
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 ); 501 );
487 test.equal( 502 test.equal(
488 normalized, 503 normalized,
489 "b$la$sitekey=foo,domain=domain.com|foo.com,csp=c s p" 504 "b$la$sitekey=foo,domain=domain.com|foo.com,csp=c s p"
490 ); 505 );
491 compareFilter( 506 compareFilter(
492 test, normalized, [ 507 test, normalized, [
493 "type=filterlist", 508 "type=filterlist",
494 "text=" + normalized, 509 "text=" + normalized,
495 "csp=c s p", 510 "csp=c s p",
496 "domains=DOMAIN.COM|FOO.COM", 511 "domains=domain.com|foo.com",
497 "sitekeys=FOO", 512 "sitekeys=FOO",
498 "regexp=b\\$la", 513 "regexp=b\\$la",
499 "contentType=" + t.CSP 514 "contentType=" + t.CSP
500 ] 515 ]
501 ); 516 );
502 517
503 // Some $csp edge cases 518 // Some $csp edge cases
504 test.equal(Filter.normalize("$csp= c s p"), 519 test.equal(Filter.normalize("$csp= c s p"),
505 "$csp=c s p"); 520 "$csp=c s p");
506 test.equal(Filter.normalize("$$csp= c s p"), 521 test.equal(Filter.normalize("$$csp= c s p"),
507 "$$csp=c s p"); 522 "$$csp=c s p");
508 test.equal(Filter.normalize("$$$csp= c s p"), 523 test.equal(Filter.normalize("$$$csp= c s p"),
509 "$$$csp=c s p"); 524 "$$$csp=c s p");
510 test.equal(Filter.normalize("foo?csp=b a r$csp=script-src 'self'"), 525 test.equal(Filter.normalize("foo?csp=b a r$csp=script-src 'self'"),
511 "foo?csp=bar$csp=script-src 'self'"); 526 "foo?csp=bar$csp=script-src 'self'");
512 test.equal(Filter.normalize("foo$bar=c s p = ba z,cs p = script-src 'self'"), 527 test.equal(Filter.normalize("foo$bar=c s p = ba z,cs p = script-src 'self'"),
513 "foo$bar=csp=baz,csp=script-src 'self'"); 528 "foo$bar=csp=baz,csp=script-src 'self'");
514 test.equal(Filter.normalize("foo$csp=c s p csp= ba z,cs p = script-src 'self '"), 529 test.equal(Filter.normalize("foo$csp=c s p csp= ba z,cs p = script-src 'self '"),
515 "foo$csp=c s p csp= ba z,csp=script-src 'self'"); 530 "foo$csp=c s p csp= ba z,csp=script-src 'self'");
516 test.equal(Filter.normalize("foo$csp=bar,$c sp=c s p"), 531 test.equal(Filter.normalize("foo$csp=bar,$c sp=c s p"),
517 "foo$csp=bar,$csp=c s p"); 532 "foo$csp=bar,$csp=c s p");
518 test.equal(Filter.normalize(" f o o $ bar $csp=ba r"), 533 test.equal(Filter.normalize(" f o o $ bar $csp=ba r"),
519 "foo$bar$csp=ba r"); 534 "foo$bar$csp=ba r");
520 test.equal(Filter.normalize("f $ o $ o $ csp=f o o "), 535 test.equal(Filter.normalize("f $ o $ o $ csp=f o o "),
521 "f$o$o$csp=f o o"); 536 "f$o$o$csp=f o o");
522 test.equal(Filter.normalize("/foo$/$ csp = script-src http://example.com/?$1= 1&$2=2&$3=3"), 537 test.equal(Filter.normalize("/foo$/$ csp = script-src http://example.com/?$1= 1&$2=2&$3=3"),
523 "/foo$/$csp=script-src http://example.com/?$1=1&$2=2&$3=3"); 538 "/foo$/$csp=script-src http://example.com/?$1=1&$2=2&$3=3");
524 539 test.equal(Filter.normalize("||content.server.com/files/*.php$rewrite= $1"),
525 test.done(); 540 "||content.server.com/files/*.php$rewrite=$1");
526 }; 541 test.done();
542 };
543
544
545 exports.testFilterRewriteOption = function(test)
546 {
547 let text = "/(content\\.server\\/file\\/.*\\.txt)\\?.*$/$rewrite=$1";
548
549 let filter = Filter.fromText(text);
550
551 test.equal(filter.rewrite, "$1");
552 // no rewrite occured: didn't match.
553 test.equal(filter.rewriteUrl("foo"), "foo");
554 // rewrite occured: matched.
555 test.equal(filter.rewriteUrl("http://content.server/file/foo.txt?bar"),
556 "http://content.server/file/foo.txt");
557
558 // checking for same origin.
559 let rewriteDiffOrigin =
560 "/content\\.server(\\/file\\/.*\\.txt)\\?.*$/$rewrite=foo.com$1";
561 let filterDiffOrigin = Filter.fromText(rewriteDiffOrigin);
562
563 // no rewrite occured because of a different origin.
564 test.equal(
565 filterDiffOrigin.rewriteUrl("http://content.server/file/foo.txt?bar"),
566 "http://content.server/file/foo.txt?bar"
567 );
568
569 // relative path.
570 let rewriteRelative = "/(\\/file\\/.*\\.txt)\\?.*$/$rewrite=$1/disable";
571 let filterRelative = Filter.fromText(rewriteRelative);
572
573 test.equal(
574 filterRelative.rewriteUrl("http://content.server/file/foo.txt?bar"),
575 "http://content.server/file/foo.txt/disable"
576 );
577 test.equal(
578 filterRelative.rewriteUrl("http://example.com/file/foo.txt?bar"),
579 "http://example.com/file/foo.txt/disable"
580 );
581
582 // Example from https://github.com/uBlockOrigin/uBlock-issues/issues/46#issuec omment-391190533
583 // The rewrite shouldn't happen.
584 let rewriteEvil = "/(^https?:\\/\\/[^/])/$script,rewrite=$1.evil.com";
585 let filterEvil = Filter.fromText(rewriteEvil);
586
587 test.equal(
588 filterEvil.rewriteUrl("https://www.adblockplus.org/script.js"),
589 "https://www.adblockplus.org/script.js"
590 );
591
592 test.done();
593 };
LEFTRIGHT

Powered by Google App Engine
This is Rietveld