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

Side by Side Diff: test/filterClasses.js

Issue 29361668: Issue 4394 - Create a filter class for element hiding emulation filters (Closed) Base URL: https://bitbucket.org/fhd/adblockpluscore
Patch Set: Address Wladimir's comments Created Nov. 21, 2016, 2:34 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
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-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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
(...skipping 11 matching lines...) Expand all
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 WhitelistFilter = null; 28 let WhitelistFilter = null;
29 let ElemHideBase = null; 29 let ElemHideBase = null;
30 let ElemHideFilter = null; 30 let ElemHideFilter = null;
31 let ElemHideException = null; 31 let ElemHideException = null;
32 let CSSPropertyFilter = null; 32 let ElemHideEmulationFilter = null;
33 33
34 let t = null; 34 let t = null;
35 let defaultTypes = null; 35 let defaultTypes = null;
36 36
37 exports.setUp = function(callback) 37 exports.setUp = function(callback)
38 { 38 {
39 let sandboxedRequire = createSandbox(); 39 let sandboxedRequire = createSandbox();
40 ( 40 (
41 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, 41 {Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter,
42 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, 42 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter,
43 ElemHideException, CSSPropertyFilter} = sandboxedRequire("../lib/filterClas ses") 43 ElemHideException, ElemHideEmulationFilter} = sandboxedRequire("../lib/filt erClasses")
kzar 2016/11/21 16:24:55 Nit: This and some of the other lines you've modif
Felix Dahlke 2016/11/21 17:26:12 I'd actually prefer to leave those alone as part o
kzar 2016/11/21 19:18:22 On 2016/11/21 17:26:12, Felix Dahlke wrote:
Felix Dahlke 2016/11/21 20:11:48 Fair enough, I fixed all lines I'm touching know,
44 ); 44 );
45 t = RegExpFilter.typeMap; 45 t = RegExpFilter.typeMap;
46 defaultTypes = 0x7FFFFFFF & ~(t.ELEMHIDE | t.DOCUMENT | t.POPUP | 46 defaultTypes = 0x7FFFFFFF & ~(t.ELEMHIDE | t.DOCUMENT | t.POPUP |
47 t.GENERICHIDE | t.GENERICBLOCK); 47 t.GENERICHIDE | t.GENERICBLOCK);
48 48
49 callback(); 49 callback();
50 }; 50 };
51 51
52 function serializeFilter(filter) 52 function serializeFilter(filter)
53 { 53 {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 { 97 {
98 result.push("type=whitelist"); 98 result.push("type=whitelist");
99 } 99 }
100 } 100 }
101 else if (filter instanceof ElemHideBase) 101 else if (filter instanceof ElemHideBase)
102 { 102 {
103 if (filter instanceof ElemHideFilter) 103 if (filter instanceof ElemHideFilter)
104 result.push("type=elemhide"); 104 result.push("type=elemhide");
105 else if (filter instanceof ElemHideException) 105 else if (filter instanceof ElemHideException)
106 result.push("type=elemhideexception"); 106 result.push("type=elemhideexception");
107 else if (filter instanceof CSSPropertyFilter) 107 else if (filter instanceof ElemHideEmulationFilter)
108 { 108 result.push("type=elemhideemulation");
109 result.push("type=cssrule");
110 result.push("prefix=" + (filter.selectorPrefix || ""));
111 result.push("regexp=" + filter.regexpString);
112 result.push("suffix=" + (filter.selectorSuffix || ""));
113 }
114 109
115 result.push("selectorDomain=" + (filter.selectorDomain || "")); 110 result.push("selectorDomain=" + (filter.selectorDomain || ""));
116 result.push("selector=" + filter.selector); 111 result.push("selector=" + filter.selector);
117 } 112 }
118 } 113 }
119 return result; 114 return result;
120 } 115 }
121 116
122 function addDefaults(expected) 117 function addDefaults(expected)
123 { 118 {
124 let type = null; 119 let type = null;
125 let hasProperty = {}; 120 let hasProperty = {};
126 for (let entry of expected) 121 for (let entry of expected)
127 { 122 {
128 if (/^type=(.*)/.test(entry)) 123 if (/^type=(.*)/.test(entry))
129 type = RegExp.$1; 124 type = RegExp.$1;
130 else if (/^(\w+)/.test(entry)) 125 else if (/^(\w+)/.test(entry))
131 hasProperty[RegExp.$1] = true; 126 hasProperty[RegExp.$1] = true;
132 } 127 }
133 128
134 function addProperty(prop, value) 129 function addProperty(prop, value)
135 { 130 {
136 if (!(prop in hasProperty)) 131 if (!(prop in hasProperty))
137 expected.push(prop + "=" + value); 132 expected.push(prop + "=" + value);
138 } 133 }
139 134
140 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type == "elemhideexception" || type == "cssrule") 135 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type == "elemhideexception" || type == "elemhideemulation")
141 { 136 {
142 addProperty("disabled", "false"); 137 addProperty("disabled", "false");
143 addProperty("lastHit", "0"); 138 addProperty("lastHit", "0");
144 addProperty("hitCount", "0"); 139 addProperty("hitCount", "0");
145 } 140 }
146 if (type == "whitelist" || type == "filterlist") 141 if (type == "whitelist" || type == "filterlist")
147 { 142 {
148 addProperty("contentType", 0x7FFFFFFF & ~( 143 addProperty("contentType", 0x7FFFFFFF & ~(
149 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | 144 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE |
150 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE | 145 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE |
151 RegExpFilter.typeMap.GENERICBLOCK 146 RegExpFilter.typeMap.GENERICBLOCK
152 )); 147 ));
153 addProperty("matchCase", "false"); 148 addProperty("matchCase", "false");
154 addProperty("thirdParty", "null"); 149 addProperty("thirdParty", "null");
155 addProperty("domains", ""); 150 addProperty("domains", "");
156 addProperty("sitekeys", ""); 151 addProperty("sitekeys", "");
157 } 152 }
158 if (type == "filterlist") 153 if (type == "filterlist")
159 { 154 {
160 addProperty("collapse", "null"); 155 addProperty("collapse", "null");
161 } 156 }
162 if (type == "elemhide" || type == "elemhideexception" || type == "cssrule") 157 if (type == "elemhide" || type == "elemhideexception" || type == "elemhideemul ation")
163 { 158 {
164 addProperty("selectorDomain", ""); 159 addProperty("selectorDomain", "");
165 addProperty("domains", ""); 160 addProperty("domains", "");
166 } 161 }
167 if (type == "cssrule")
168 {
169 addProperty("regexp", "");
170 addProperty("prefix", "");
171 addProperty("suffix", "");
172 }
173 } 162 }
174 163
175 function compareFilter(test, text, expected, postInit) 164 function compareFilter(test, text, expected, postInit)
176 { 165 {
177 addDefaults(expected); 166 addDefaults(expected);
178 167
179 let filter = Filter.fromText(text); 168 let filter = Filter.fromText(text);
180 if (postInit) 169 if (postInit)
181 postInit(filter) 170 postInit(filter)
182 let result = serializeFilter(filter); 171 let result = serializeFilter(filter);
(...skipping 26 matching lines...) Expand all
209 test.equal(typeof Filter, "function", "typeof Filter"); 198 test.equal(typeof Filter, "function", "typeof Filter");
210 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); 199 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
211 test.equal(typeof CommentFilter, "function", "typeof CommentFilter"); 200 test.equal(typeof CommentFilter, "function", "typeof CommentFilter");
212 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); 201 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
213 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); 202 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
214 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); 203 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
215 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); 204 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
216 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); 205 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
217 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); 206 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
218 test.equal(typeof ElemHideException, "function", "typeof ElemHideException"); 207 test.equal(typeof ElemHideException, "function", "typeof ElemHideException");
219 test.equal(typeof CSSPropertyFilter, "function", "typeof CSSPropertyFilter"); 208 test.equal(typeof ElemHideEmulationFilter, "function", "typeof ElemHideEmulati onFilter");
220 209
221 test.done(); 210 test.done();
222 }; 211 };
223 212
224 exports.testComments = function(test) 213 exports.testComments = function(test)
225 { 214 {
226 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]); 215 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]);
227 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]); 216 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]);
228 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]); 217 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]);
229 218
230 test.done(); 219 test.done();
231 }; 220 };
232 221
233 exports.testInvalidFilters = function(test) 222 exports.testInvalidFilters = function(test)
234 { 223 {
235 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]); 224 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]);
236 compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason= filter_unknown_option"]); 225 compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason= filter_unknown_option"]);
237 compareFilter(test, "#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "r eason=filter_elemhide_duplicate_id"]); 226 compareFilter(test, "#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "r eason=filter_elemhide_duplicate_id"]);
238 compareFilter(test, "#*", ["type=invalid", "text=#*", "reason=filter_elemhide_ nocriteria"]); 227 compareFilter(test, "#*", ["type=invalid", "text=#*", "reason=filter_elemhide_ nocriteria"]);
239 228
240 function compareCSSRule(domains) 229 function checkElemHideEmulationFilterInvalid(domains)
241 { 230 {
242 let filterText = domains + "##[-abp-properties='abc']"; 231 let filterText = domains + "##[-abp-properties='abc']";
243 compareFilter(test, filterText, ["type=invalid", "text=" + filterText, "reas on=filter_cssproperty_nodomain"]); 232 compareFilter(test, filterText, ["type=invalid", "text=" + filterText, "reas on=filter_elemhideemulation_nodomain"]);
244 } 233 }
245 compareCSSRule(""); 234 checkElemHideEmulationFilterInvalid("");
246 compareCSSRule("~foo.com"); 235 checkElemHideEmulationFilterInvalid("~foo.com");
247 compareCSSRule("~foo.com,~bar.com"); 236 checkElemHideEmulationFilterInvalid("~foo.com,~bar.com");
248 compareCSSRule("foo"); 237 checkElemHideEmulationFilterInvalid("foo");
249 compareCSSRule("~foo.com,bar"); 238 checkElemHideEmulationFilterInvalid("~foo.com,bar");
250 239
251 test.done(); 240 test.done();
252 }; 241 };
253 242
254 exports.testFiltersWithState = function(test) 243 exports.testFiltersWithState = function(test)
255 { 244 {
256 compareFilter(test, "blabla", ["type=filterlist", "text=blabla", "regexp=blabl a"]); 245 compareFilter(test, "blabla", ["type=filterlist", "text=blabla", "regexp=blabl a"]);
257 compareFilter(test, "blabla_default", ["type=filterlist", "text=blabla_default ", "regexp=blabla_default"], function(filter) 246 compareFilter(test, "blabla_default", ["type=filterlist", "text=blabla_default ", "regexp=blabla_default"], function(filter)
258 { 247 {
259 filter.disabled = false; 248 filter.disabled = false;
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
344 compareFilter(test, "#@*(foo=bar)", ["type=elemhideexception", "text=#@*(foo=b ar)", 'selector=[foo="bar"]']); 333 compareFilter(test, "#@*(foo=bar)", ["type=elemhideexception", "text=#@*(foo=b ar)", 'selector=[foo="bar"]']);
345 compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "t ext=#@#body > div:first-child", "selector=body > div:first-child"]); 334 compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "t ext=#@#body > div:first-child", "selector=body > div:first-child"]);
346 compareFilter(test, "foo#@ddd", ["type=elemhideexception", "text=foo#@ddd", "s electorDomain=foo", "selector=ddd", "domains=FOO"]); 335 compareFilter(test, "foo#@ddd", ["type=elemhideexception", "text=foo#@ddd", "s electorDomain=foo", "selector=ddd", "domains=FOO"]);
347 compareFilter(test, "foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); 336 compareFilter(test, "foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
348 compareFilter(test, "foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar #@ddd", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); 337 compareFilter(test, "foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar #@ddd", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]);
349 compareFilter(test, "foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo, ~baz,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ" ]); 338 compareFilter(test, "foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo, ~baz,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ" ]);
350 339
351 test.done(); 340 test.done();
352 }; 341 };
353 342
354 exports.testCSSPropertyFilters = function(test) 343 exports.testElemHideEmulationFilters = function(test)
355 { 344 {
356 // Check valid domain combinations 345 // Check valid domain combinations
357 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=cssrule", "text =foo.com##[-abp-properties='abc']", "selectorDomain=foo.com", "selector=[-abp-pr operties='abc']", "domains=FOO.COM", "regexp=abc"]); 346 compareFilter(test, "foo.com##[-abp-properties='abc']", ["type=elemhideemulati on", "text=foo.com##[-abp-properties='abc']", "selectorDomain=foo.com", "selecto r=[-abp-properties='abc']", "domains=FOO.COM"]);
358 compareFilter(test, "foo.com,~bar.com##[-abp-properties='abc']", ["type=cssrul e", "text=foo.com,~bar.com##[-abp-properties='abc']", "selectorDomain=foo.com", "selector=[-abp-properties='abc']", "domains=FOO.COM|~BAR.COM", "regexp=abc"]); 347 compareFilter(test, "foo.com,~bar.com##[-abp-properties='abc']", ["type=elemhi deemulation", "text=foo.com,~bar.com##[-abp-properties='abc']", "selectorDomain= foo.com", "selector=[-abp-properties='abc']", "domains=FOO.COM|~BAR.COM"]);
359 compareFilter(test, "foo.com,~bar##[-abp-properties='abc']", ["type=cssrule", "text=foo.com,~bar##[-abp-properties='abc']", "selectorDomain=foo.com", "selecto r=[-abp-properties='abc']", "domains=FOO.COM|~BAR", "regexp=abc"]); 348 compareFilter(test, "foo.com,~bar##[-abp-properties='abc']", ["type=elemhideem ulation", "text=foo.com,~bar##[-abp-properties='abc']", "selectorDomain=foo.com" , "selector=[-abp-properties='abc']", "domains=FOO.COM|~BAR"]);
360 compareFilter(test, "~foo.com,bar.com##[-abp-properties='abc']", ["type=cssrul e", "text=~foo.com,bar.com##[-abp-properties='abc']", "selectorDomain=bar.com", "selector=[-abp-properties='abc']", "domains=BAR.COM|~FOO.COM", "regexp=abc"]); 349 compareFilter(test, "~foo.com,bar.com##[-abp-properties='abc']", ["type=elemhi deemulation", "text=~foo.com,bar.com##[-abp-properties='abc']", "selectorDomain= bar.com", "selector=[-abp-properties='abc']", "domains=BAR.COM|~FOO.COM"]);
361 350
362 compareFilter(test, "##[-abp-properties='']", ["type=elemhide", "text=##[-abp- properties='']", "selector=[-abp-properties='']"]); 351 compareFilter(test, "##[-abp-properties='']", ["type=invalid", "text=##[-abp-p roperties='']", "reason=filter_elemhideemulation_nodomain"]);
363 compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexcept ion", "text=foo.com#@#[-abp-properties='abc']", "selectorDomain=foo.com", "selec tor=[-abp-properties='abc']", "domains=FOO.COM"]); 352 compareFilter(test, "foo.com#@#[-abp-properties='abc']", ["type=elemhideexcept ion", "text=foo.com#@#[-abp-properties='abc']", "selectorDomain=foo.com", "selec tor=[-abp-properties='abc']", "domains=FOO.COM"]);
364 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=cssrule ", "text=foo.com##aaa [-abp-properties='abc'] bbb", "selectorDomain=foo.com", "s elector=aaa [-abp-properties='abc'] bbb", "domains=FOO.COM", "prefix=aaa ", "reg exp=abc", "suffix= bbb"]); 353 compareFilter(test, "foo.com##aaa [-abp-properties='abc'] bbb", ["type=elemhid eemulation", "text=foo.com##aaa [-abp-properties='abc'] bbb", "selectorDomain=fo o.com", "selector=aaa [-abp-properties='abc'] bbb", "domains=FOO.COM"]);
365 compareFilter(test, "foo.com##[-abp-properties='|background-image: url(data:*) ']", ["type=cssrule", "text=foo.com##[-abp-properties='|background-image: url(da ta:*)']", "selectorDomain=foo.com", "selector=[-abp-properties='|background-imag e: url(data:*)']", "domains=FOO.COM", "regexp=^background\\-image\\:\\ url\\(dat a\\:.*\\)"]); 354 compareFilter(test, "foo.com##[-abp-properties='|background-image: url(data:*) ']", ["type=elemhideemulation", "text=foo.com##[-abp-properties='|background-ima ge: url(data:*)']", "selectorDomain=foo.com", "selector=[-abp-properties='|backg round-image: url(data:*)']", "domains=FOO.COM"]);
366
367 // Test regexp property matching
368 compareFilter(test, "foo.com##[-abp-properties='/abc']", ["type=cssrule", "tex t=foo.com##[-abp-properties='/abc']", "selectorDomain=foo.com", "selector=[-abp- properties='/abc']", "domains=FOO.COM", "regexp=\\/abc"]);
369 compareFilter(test, "foo.com##[-abp-properties='abc/']", ["type=cssrule", "tex t=foo.com##[-abp-properties='abc/']", "selectorDomain=foo.com", "selector=[-abp- properties='abc/']", "domains=FOO.COM", "regexp=abc\\/"]);
370 compareFilter(test, "foo.com##[-abp-properties='/abc/']", ["type=cssrule", "te xt=foo.com##[-abp-properties='/abc/']", "selectorDomain=foo.com", "selector=[-ab p-properties='/abc/']", "domains=FOO.COM", "regexp=abc"]);
371 355
372 test.done(); 356 test.done();
373 }; 357 };
374 358
375 exports.testEmptyElemHideDomains = function(test) 359 exports.testEmptyElemHideDomains = function(test)
376 { 360 {
377 let emptyDomainFilters = [ 361 let emptyDomainFilters = [
378 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector", 362 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector",
379 ",foo.com##selector", "foo.com,~##selector", 363 ",foo.com##selector", "foo.com,~##selector",
380 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector" 364 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector"
381 ]; 365 ];
382 366
383 for (let filterText of emptyDomainFilters) 367 for (let filterText of emptyDomainFilters)
384 { 368 {
385 let filter = Filter.fromText(filterText); 369 let filter = Filter.fromText(filterText);
386 test.ok(filter instanceof InvalidFilter); 370 test.ok(filter instanceof InvalidFilter);
387 test.equal(filter.reason, "filter_invalid_domain"); 371 test.equal(filter.reason, "filter_invalid_domain");
388 } 372 }
389 373
390 test.done(); 374 test.done();
391 }; 375 };
OLDNEW
« lib/filterClasses.js ('K') | « test/elemHideEmulation.js ('k') | test/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld