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: Created Nov. 3, 2016, 3:42 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/04 15:45:56 Nit: Also mind fixing some of these long lines? (N
Felix Dahlke 2016/11/04 16:43:35 See my comment in elemHideEmulation.js.
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 {
109 result.push("type=cssrule"); 109 result.push("type=elemhideemulation");
110 result.push("prefix=" + (filter.selectorPrefix || "")); 110 result.push("features=" + filter.features);
111 result.push("regexp=" + filter.regexpString);
112 result.push("suffix=" + (filter.selectorSuffix || ""));
113 } 111 }
114 112
115 result.push("selectorDomain=" + (filter.selectorDomain || "")); 113 result.push("selectorDomain=" + (filter.selectorDomain || ""));
116 result.push("selector=" + filter.selector); 114 result.push("selector=" + filter.selector);
117 } 115 }
118 } 116 }
119 return result; 117 return result;
120 } 118 }
121 119
122 function addDefaults(expected) 120 function addDefaults(expected)
123 { 121 {
124 let type = null; 122 let type = null;
125 let hasProperty = {}; 123 let hasProperty = {};
126 for (let entry of expected) 124 for (let entry of expected)
127 { 125 {
128 if (/^type=(.*)/.test(entry)) 126 if (/^type=(.*)/.test(entry))
129 type = RegExp.$1; 127 type = RegExp.$1;
130 else if (/^(\w+)/.test(entry)) 128 else if (/^(\w+)/.test(entry))
131 hasProperty[RegExp.$1] = true; 129 hasProperty[RegExp.$1] = true;
132 } 130 }
133 131
134 function addProperty(prop, value) 132 function addProperty(prop, value)
135 { 133 {
136 if (!(prop in hasProperty)) 134 if (!(prop in hasProperty))
137 expected.push(prop + "=" + value); 135 expected.push(prop + "=" + value);
138 } 136 }
139 137
140 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type == "elemhideexception" || type == "cssrule") 138 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type == "elemhideexception" || type == "elemhideemulation")
141 { 139 {
142 addProperty("disabled", "false"); 140 addProperty("disabled", "false");
143 addProperty("lastHit", "0"); 141 addProperty("lastHit", "0");
144 addProperty("hitCount", "0"); 142 addProperty("hitCount", "0");
145 } 143 }
146 if (type == "whitelist" || type == "filterlist") 144 if (type == "whitelist" || type == "filterlist")
147 { 145 {
148 addProperty("contentType", 0x7FFFFFFF & ~( 146 addProperty("contentType", 0x7FFFFFFF & ~(
149 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | 147 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE |
150 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE | 148 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE |
151 RegExpFilter.typeMap.GENERICBLOCK 149 RegExpFilter.typeMap.GENERICBLOCK
152 )); 150 ));
153 addProperty("matchCase", "false"); 151 addProperty("matchCase", "false");
154 addProperty("thirdParty", "null"); 152 addProperty("thirdParty", "null");
155 addProperty("domains", ""); 153 addProperty("domains", "");
156 addProperty("sitekeys", ""); 154 addProperty("sitekeys", "");
157 } 155 }
158 if (type == "filterlist") 156 if (type == "filterlist")
159 { 157 {
160 addProperty("collapse", "null"); 158 addProperty("collapse", "null");
161 } 159 }
162 if (type == "elemhide" || type == "elemhideexception" || type == "cssrule") 160 if (type == "elemhide" || type == "elemhideexception" || type == "elemhideemul ation")
163 { 161 {
164 addProperty("selectorDomain", ""); 162 addProperty("selectorDomain", "");
165 addProperty("domains", ""); 163 addProperty("domains", "");
166 } 164 }
167 if (type == "cssrule") 165 if (type == "elemhideemulation")
168 { 166 {
169 addProperty("regexp", ""); 167 addProperty("features", "0");
170 addProperty("prefix", "");
171 addProperty("suffix", "");
172 } 168 }
173 } 169 }
174 170
175 function compareFilter(test, text, expected, postInit) 171 function compareFilter(test, text, expected, postInit)
176 { 172 {
177 addDefaults(expected); 173 addDefaults(expected);
178 174
179 let filter = Filter.fromText(text); 175 let filter = Filter.fromText(text);
180 if (postInit) 176 if (postInit)
181 postInit(filter) 177 postInit(filter)
(...skipping 27 matching lines...) Expand all
209 test.equal(typeof Filter, "function", "typeof Filter"); 205 test.equal(typeof Filter, "function", "typeof Filter");
210 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); 206 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
211 test.equal(typeof CommentFilter, "function", "typeof CommentFilter"); 207 test.equal(typeof CommentFilter, "function", "typeof CommentFilter");
212 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); 208 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
213 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); 209 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
214 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); 210 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
215 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); 211 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
216 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); 212 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
217 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); 213 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
218 test.equal(typeof ElemHideException, "function", "typeof ElemHideException"); 214 test.equal(typeof ElemHideException, "function", "typeof ElemHideException");
219 test.equal(typeof CSSPropertyFilter, "function", "typeof CSSPropertyFilter"); 215 test.equal(typeof ElemHideEmulationFilter, "function", "typeof ElemHideEmulati onFilter");
220 216
221 test.done(); 217 test.done();
222 }; 218 };
223 219
224 exports.testComments = function(test) 220 exports.testComments = function(test)
225 { 221 {
226 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]); 222 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]);
227 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]); 223 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]);
228 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]); 224 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]);
229 225
230 test.done(); 226 test.done();
231 }; 227 };
232 228
233 exports.testInvalidFilters = function(test) 229 exports.testInvalidFilters = function(test)
234 { 230 {
235 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]); 231 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]);
236 compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason= filter_unknown_option"]); 232 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"]); 233 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"]); 234 compareFilter(test, "#*", ["type=invalid", "text=#*", "reason=filter_elemhide_ nocriteria"]);
239 235
240 function compareCSSRule(domains) 236 function checkElemHideEmulationFilterInvalid(domains)
241 { 237 {
242 let filterText = domains + "##[-abp-properties='abc']"; 238 let filterText = domains + "##[-abp-properties='abc']";
243 compareFilter(test, filterText, ["type=invalid", "text=" + filterText, "reas on=filter_cssproperty_nodomain"]); 239 compareFilter(test, filterText, ["type=invalid", "text=" + filterText, "reas on=filter_elemhideemulation_nodomain"]);
244 } 240 }
245 compareCSSRule(""); 241 checkElemHideEmulationFilterInvalid("");
246 compareCSSRule("~foo.com"); 242 checkElemHideEmulationFilterInvalid("~foo.com");
247 compareCSSRule("~foo.com,~bar.com"); 243 checkElemHideEmulationFilterInvalid("~foo.com,~bar.com");
248 compareCSSRule("foo"); 244 checkElemHideEmulationFilterInvalid("foo");
249 compareCSSRule("~foo.com,bar"); 245 checkElemHideEmulationFilterInvalid("~foo.com,bar");
250 246
251 test.done(); 247 test.done();
252 }; 248 };
253 249
254 exports.testFiltersWithState = function(test) 250 exports.testFiltersWithState = function(test)
255 { 251 {
256 compareFilter(test, "blabla", ["type=filterlist", "text=blabla", "regexp=blabl a"]); 252 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) 253 compareFilter(test, "blabla_default", ["type=filterlist", "text=blabla_default ", "regexp=blabla_default"], function(filter)
258 { 254 {
259 filter.disabled = false; 255 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"]']); 340 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"]); 341 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"]); 342 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"]); 343 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"]); 344 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" ]); 345 compareFilter(test, "foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo, ~baz,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ" ]);
350 346
351 test.done(); 347 test.done();
352 }; 348 };
353 349
354 exports.testCSSPropertyFilters = function(test) 350 exports.testElemHideEmulationFilters = function(test)
355 { 351 {
356 // Check valid domain combinations 352 // 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"]); 353 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", "features=1"]);
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"]); 354 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", "featu res=1"]);
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"]); 355 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", "features=1"]);
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"]); 356 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", "featu res=1"]);
361 357
362 compareFilter(test, "##[-abp-properties='']", ["type=elemhide", "text=##[-abp- properties='']", "selector=[-abp-properties='']"]); 358 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"]); 359 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"]); 360 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", "features =1"]);
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\\:.*\\)"]); 361 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", "features=1"]);
362
363 // Test feature detection
364 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", "features=1"]);
365 compareFilter(test, "foo.com##:has(#ddd)", ["type=elemhideemulation", "text=fo o.com##:has(#ddd)", "selectorDomain=foo.com", "selector=:has(#ddd)", "domains=FO O.COM", "features=2"]);
366 compareFilter(test, "foo.com##:has([-abp-properties='abc'])", ["type=elemhidee mulation", "text=foo.com##:has([-abp-properties='abc'])", "selectorDomain=foo.co m", "selector=:has([-abp-properties='abc'])", "domains=FOO.COM", "features=3"]);
kzar 2016/11/04 15:45:56 Please don't hardcode the feature flag numbers, be
Felix Dahlke 2016/11/04 16:43:35 Good point, will do.
Felix Dahlke 2016/11/11 11:51:42 Done.
366 367
367 test.done(); 368 test.done();
368 }; 369 };
369 370
370 exports.testEmptyElemHideDomains = function(test) 371 exports.testEmptyElemHideDomains = function(test)
371 { 372 {
372 let emptyDomainFilters = [ 373 let emptyDomainFilters = [
373 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector", 374 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector",
374 ",foo.com##selector", "foo.com,~##selector", 375 ",foo.com##selector", "foo.com,~##selector",
375 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector" 376 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector"
376 ]; 377 ];
377 378
378 for (let filterText of emptyDomainFilters) 379 for (let filterText of emptyDomainFilters)
379 { 380 {
380 let filter = Filter.fromText(filterText); 381 let filter = Filter.fromText(filterText);
381 test.ok(filter instanceof InvalidFilter); 382 test.ok(filter instanceof InvalidFilter);
382 test.equal(filter.reason, "filter_invalid_domain"); 383 test.equal(filter.reason, "filter_invalid_domain");
383 } 384 }
384 385
385 test.done(); 386 test.done();
386 }; 387 };
OLDNEW

Powered by Google App Engine
This is Rietveld