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

Side by Side Diff: test/filterClasses.js

Issue 29737558: Issue 6538, 6781 - Implement support for snippet filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rebase, rename to ScriptFilter, ignore element hiding exceptions Created May 23, 2018, 3:54 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « lib/snippets.js ('k') | test/snippets.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 ScriptFilter = null;
28 let WhitelistFilter = null; 29 let WhitelistFilter = null;
29 let ElemHideBase = null; 30 let ElemHideBase = null;
30 let ElemHideFilter = null; 31 let ElemHideFilter = null;
31 let ElemHideException = null; 32 let ElemHideException = null;
32 let ElemHideEmulationFilter = null; 33 let ElemHideEmulationFilter = null;
34 let SnippetFilter = null;
33 35
34 let t = null; 36 let t = null;
35 let defaultTypes = null; 37 let defaultTypes = null;
36 38
37 exports.setUp = function(callback) 39 exports.setUp = function(callback)
38 { 40 {
39 let sandboxedRequire = createSandbox(); 41 let sandboxedRequire = createSandbox();
40 ( 42 (
41 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, 43 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter,
42 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, 44 BlockingFilter, WhitelistFilter, ScriptFilter, ElemHideBase,
43 ElemHideException, 45 ElemHideFilter, ElemHideException, ElemHideEmulationFilter,
44 ElemHideEmulationFilter} = sandboxedRequire("../lib/filterClasses") 46 SnippetFilter} = sandboxedRequire("../lib/filterClasses")
45 ); 47 );
46 t = RegExpFilter.typeMap; 48 t = RegExpFilter.typeMap;
47 defaultTypes = 0x7FFFFFFF & ~(t.CSP | t.ELEMHIDE | t.DOCUMENT | t.POPUP | 49 defaultTypes = 0x7FFFFFFF & ~(t.CSP | t.ELEMHIDE | t.DOCUMENT | t.POPUP |
48 t.GENERICHIDE | t.GENERICBLOCK); 50 t.GENERICHIDE | t.GENERICBLOCK);
49 51
50 callback(); 52 callback();
51 }; 53 };
52 54
53 function serializeFilter(filter) 55 function serializeFilter(filter)
54 { 56 {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 if (filter instanceof ElemHideFilter) 106 if (filter instanceof ElemHideFilter)
105 result.push("type=elemhide"); 107 result.push("type=elemhide");
106 else if (filter instanceof ElemHideException) 108 else if (filter instanceof ElemHideException)
107 result.push("type=elemhideexception"); 109 result.push("type=elemhideexception");
108 else if (filter instanceof ElemHideEmulationFilter) 110 else if (filter instanceof ElemHideEmulationFilter)
109 result.push("type=elemhideemulation"); 111 result.push("type=elemhideemulation");
110 112
111 result.push("selectorDomains=" + (filter.selectorDomains || "")); 113 result.push("selectorDomains=" + (filter.selectorDomains || ""));
112 result.push("selector=" + filter.selector); 114 result.push("selector=" + filter.selector);
113 } 115 }
116 else if (filter instanceof SnippetFilter)
117 {
118 result.push("type=snippet");
119 result.push("scriptDomains=" + (filter.scriptDomains || ""));
120 result.push("script=" + filter.script);
121 }
114 } 122 }
115 return result; 123 return result;
116 } 124 }
117 125
118 function addDefaults(expected) 126 function addDefaults(expected)
119 { 127 {
120 let type = null; 128 let type = null;
121 let hasProperty = {}; 129 let hasProperty = {};
122 for (let entry of expected) 130 for (let entry of expected)
123 { 131 {
124 if (/^type=(.*)/.test(entry)) 132 if (/^type=(.*)/.test(entry))
125 type = RegExp.$1; 133 type = RegExp.$1;
126 else if (/^(\w+)/.test(entry)) 134 else if (/^(\w+)/.test(entry))
127 hasProperty[RegExp.$1] = true; 135 hasProperty[RegExp.$1] = true;
128 } 136 }
129 137
130 function addProperty(prop, value) 138 function addProperty(prop, value)
131 { 139 {
132 if (!(prop in hasProperty)) 140 if (!(prop in hasProperty))
133 expected.push(prop + "=" + value); 141 expected.push(prop + "=" + value);
134 } 142 }
135 143
136 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || 144 if (type == "whitelist" || type == "filterlist" || type == "elemhide" ||
137 type == "elemhideexception" || type == "elemhideemulation") 145 type == "elemhideexception" || type == "elemhideemulation" ||
146 type == "snippet")
138 { 147 {
139 addProperty("disabled", "false"); 148 addProperty("disabled", "false");
140 addProperty("lastHit", "0"); 149 addProperty("lastHit", "0");
141 addProperty("hitCount", "0"); 150 addProperty("hitCount", "0");
142 } 151 }
143 if (type == "whitelist" || type == "filterlist") 152 if (type == "whitelist" || type == "filterlist")
144 { 153 {
145 addProperty("contentType", 0x7FFFFFFF & ~( 154 addProperty("contentType", 0x7FFFFFFF & ~(
146 t.CSP | t.DOCUMENT | t.ELEMHIDE | t.POPUP | t.GENERICHIDE | t.GENERICBLOCK 155 t.CSP | t.DOCUMENT | t.ELEMHIDE | t.POPUP | t.GENERICHIDE | t.GENERICBLOCK
147 )); 156 ));
148 addProperty("matchCase", "false"); 157 addProperty("matchCase", "false");
149 addProperty("thirdParty", "null"); 158 addProperty("thirdParty", "null");
150 addProperty("domains", ""); 159 addProperty("domains", "");
151 addProperty("sitekeys", ""); 160 addProperty("sitekeys", "");
152 } 161 }
153 if (type == "filterlist") 162 if (type == "filterlist")
154 { 163 {
155 addProperty("collapse", "null"); 164 addProperty("collapse", "null");
156 addProperty("csp", "null"); 165 addProperty("csp", "null");
157 addProperty("rewrite", "null"); 166 addProperty("rewrite", "null");
158 } 167 }
159 if (type == "elemhide" || type == "elemhideexception" || 168 if (type == "elemhide" || type == "elemhideexception" ||
160 type == "elemhideemulation") 169 type == "elemhideemulation")
161 { 170 {
162 addProperty("selectorDomains", ""); 171 addProperty("selectorDomains", "");
163 addProperty("domains", ""); 172 addProperty("domains", "");
164 } 173 }
174 if (type == "snippet")
175 {
176 addProperty("scriptDomains", "");
177 addProperty("domains", "");
178 }
165 } 179 }
166 180
167 function compareFilter(test, text, expected, postInit) 181 function compareFilter(test, text, expected, postInit)
168 { 182 {
169 addDefaults(expected); 183 addDefaults(expected);
170 184
171 let filter = Filter.fromText(text); 185 let filter = Filter.fromText(text);
172 if (postInit) 186 if (postInit)
173 postInit(filter); 187 postInit(filter);
174 let result = serializeFilter(filter); 188 let result = serializeFilter(filter);
(...skipping 20 matching lines...) Expand all
195 } 209 }
196 210
197 exports.testFilterClassDefinitions = function(test) 211 exports.testFilterClassDefinitions = function(test)
198 { 212 {
199 test.equal(typeof Filter, "function", "typeof Filter"); 213 test.equal(typeof Filter, "function", "typeof Filter");
200 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); 214 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
201 test.equal(typeof CommentFilter, "function", "typeof CommentFilter"); 215 test.equal(typeof CommentFilter, "function", "typeof CommentFilter");
202 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); 216 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
203 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); 217 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
204 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); 218 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
219 test.equal(typeof ScriptFilter, "function", "typeof ScriptFilter");
205 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); 220 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
206 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); 221 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
207 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); 222 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
208 test.equal(typeof ElemHideException, "function", "typeof ElemHideException"); 223 test.equal(typeof ElemHideException, "function", "typeof ElemHideException");
209 test.equal(typeof ElemHideEmulationFilter, "function", 224 test.equal(typeof ElemHideEmulationFilter, "function",
210 "typeof ElemHideEmulationFilter"); 225 "typeof ElemHideEmulationFilter");
226 test.equal(typeof SnippetFilter, "function", "typeof SnippetFilter");
211 227
212 test.done(); 228 test.done();
213 }; 229 };
214 230
215 exports.testComments = function(test) 231 exports.testComments = function(test)
216 { 232 {
217 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]); 233 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]);
218 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]); 234 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]);
219 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]); 235 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]);
220 236
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 "type=elemhideemulation", 429 "type=elemhideemulation",
414 "text=foo.com#?#:-abp-properties(/margin: [3-4]{2}/)", 430 "text=foo.com#?#:-abp-properties(/margin: [3-4]{2}/)",
415 "selectorDomains=foo.com", 431 "selectorDomains=foo.com",
416 "selector=:-abp-properties(/margin: [3-4]\\7B 2\\7D /)", 432 "selector=:-abp-properties(/margin: [3-4]\\7B 2\\7D /)",
417 "domains=FOO.COM" 433 "domains=FOO.COM"
418 ] 434 ]
419 ); 435 );
420 test.done(); 436 test.done();
421 }; 437 };
422 438
439 exports.testSnippetFilters = function(test)
440 {
441 compareFilter(test, "foo.com#$#abc", ["type=snippet", "text=foo.com#$#abc", "s criptDomains=foo.com", "script=abc", "domains=FOO.COM"]);
442 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 "]);
443 compareFilter(test, "foo.com,~bar#$#abc", ["type=snippet", "text=foo.com,~bar# $#abc", "scriptDomains=foo.com", "script=abc", "domains=FOO.COM|~BAR"]);
444 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 "]);
445
446 test.done();
447 };
448
423 exports.testFilterNormalization = function(test) 449 exports.testFilterNormalization = function(test)
424 { 450 {
425 // Line breaks etc 451 // Line breaks etc
426 test.equal(Filter.normalize("\n\t\nad\ns"), 452 test.equal(Filter.normalize("\n\t\nad\ns"),
427 "ads"); 453 "ads");
428 454
429 // Comment filters 455 // Comment filters
430 test.equal(Filter.normalize(" ! fo o## bar "), 456 test.equal(Filter.normalize(" ! fo o## bar "),
431 "! fo o## bar"); 457 "! fo o## bar");
432 458
(...skipping 19 matching lines...) Expand all
452 test.equal(Filter.normalize(" domain.c om#@# # sele ctor "), 478 test.equal(Filter.normalize(" domain.c om#@# # sele ctor "),
453 "domain.com#@## sele ctor"); 479 "domain.com#@## sele ctor");
454 480
455 // Incorrect syntax: the separator "#@#" cannot contain spaces; treated as a 481 // Incorrect syntax: the separator "#@#" cannot contain spaces; treated as a
456 // regular filter instead (not an element hiding filter either!), because 482 // regular filter instead (not an element hiding filter either!), because
457 // unlike the case with "# ?##" the "##" following the "@" is not considered 483 // unlike the case with "# ?##" the "##" following the "@" is not considered
458 // to be a separator 484 // to be a separator
459 test.equal(Filter.normalize(" domain.c om# @## sele ctor "), 485 test.equal(Filter.normalize(" domain.c om# @## sele ctor "),
460 "domain.com#@##selector"); 486 "domain.com#@##selector");
461 487
488 // Snippet filters
489 test.equal(Filter.normalize(" domain.c om#$# sni pp et "),
490 "domain.com#$#sni pp et");
491
462 // Regular filters 492 // Regular filters
463 let normalized = Filter.normalize( 493 let normalized = Filter.normalize(
464 " b$l a$sitekey= foo ,domain= do main.com |foo .com,c sp= c s p " 494 " b$l a$sitekey= foo ,domain= do main.com |foo .com,c sp= c s p "
465 ); 495 );
466 test.equal( 496 test.equal(
467 normalized, 497 normalized,
468 "b$la$sitekey=foo,domain=domain.com|foo.com,csp=c s p" 498 "b$la$sitekey=foo,domain=domain.com|foo.com,csp=c s p"
469 ); 499 );
470 compareFilter( 500 compareFilter(
471 test, normalized, [ 501 test, normalized, [
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
538 filterRelative.rewriteUrl("http://content.server/file/foo.txt?bar"), 568 filterRelative.rewriteUrl("http://content.server/file/foo.txt?bar"),
539 "http://content.server/file/foo.txt/disable" 569 "http://content.server/file/foo.txt/disable"
540 ); 570 );
541 test.equal( 571 test.equal(
542 filterRelative.rewriteUrl("http://example.com/file/foo.txt?bar"), 572 filterRelative.rewriteUrl("http://example.com/file/foo.txt?bar"),
543 "http://example.com/file/foo.txt/disable" 573 "http://example.com/file/foo.txt/disable"
544 ); 574 );
545 575
546 test.done(); 576 test.done();
547 }; 577 };
OLDNEW
« 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