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: Improve compliance with the 80 column rule Created Nov. 21, 2016, 8:10 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
« no previous file with comments | « test/elemHideEmulation.js ('k') | test/filterListener.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-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,
44 ElemHideEmulationFilter} = sandboxedRequire("../lib/filterClasses")
44 ); 45 );
45 t = RegExpFilter.typeMap; 46 t = RegExpFilter.typeMap;
46 defaultTypes = 0x7FFFFFFF & ~(t.ELEMHIDE | t.DOCUMENT | t.POPUP | 47 defaultTypes = 0x7FFFFFFF & ~(t.ELEMHIDE | t.DOCUMENT | t.POPUP |
47 t.GENERICHIDE | t.GENERICBLOCK); 48 t.GENERICHIDE | t.GENERICBLOCK);
48 49
49 callback(); 50 callback();
50 }; 51 };
51 52
52 function serializeFilter(filter) 53 function serializeFilter(filter)
53 { 54 {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
97 { 98 {
98 result.push("type=whitelist"); 99 result.push("type=whitelist");
99 } 100 }
100 } 101 }
101 else if (filter instanceof ElemHideBase) 102 else if (filter instanceof ElemHideBase)
102 { 103 {
103 if (filter instanceof ElemHideFilter) 104 if (filter instanceof ElemHideFilter)
104 result.push("type=elemhide"); 105 result.push("type=elemhide");
105 else if (filter instanceof ElemHideException) 106 else if (filter instanceof ElemHideException)
106 result.push("type=elemhideexception"); 107 result.push("type=elemhideexception");
107 else if (filter instanceof CSSPropertyFilter) 108 else if (filter instanceof ElemHideEmulationFilter)
108 { 109 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 110
115 result.push("selectorDomain=" + (filter.selectorDomain || "")); 111 result.push("selectorDomain=" + (filter.selectorDomain || ""));
116 result.push("selector=" + filter.selector); 112 result.push("selector=" + filter.selector);
117 } 113 }
118 } 114 }
119 return result; 115 return result;
120 } 116 }
121 117
122 function addDefaults(expected) 118 function addDefaults(expected)
123 { 119 {
124 let type = null; 120 let type = null;
125 let hasProperty = {}; 121 let hasProperty = {};
126 for (let entry of expected) 122 for (let entry of expected)
127 { 123 {
128 if (/^type=(.*)/.test(entry)) 124 if (/^type=(.*)/.test(entry))
129 type = RegExp.$1; 125 type = RegExp.$1;
130 else if (/^(\w+)/.test(entry)) 126 else if (/^(\w+)/.test(entry))
131 hasProperty[RegExp.$1] = true; 127 hasProperty[RegExp.$1] = true;
132 } 128 }
133 129
134 function addProperty(prop, value) 130 function addProperty(prop, value)
135 { 131 {
136 if (!(prop in hasProperty)) 132 if (!(prop in hasProperty))
137 expected.push(prop + "=" + value); 133 expected.push(prop + "=" + value);
138 } 134 }
139 135
140 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type == "elemhideexception" || type == "cssrule") 136 if (type == "whitelist" || type == "filterlist" || type == "elemhide" ||
137 type == "elemhideexception" || type == "elemhideemulation")
141 { 138 {
142 addProperty("disabled", "false"); 139 addProperty("disabled", "false");
143 addProperty("lastHit", "0"); 140 addProperty("lastHit", "0");
144 addProperty("hitCount", "0"); 141 addProperty("hitCount", "0");
145 } 142 }
146 if (type == "whitelist" || type == "filterlist") 143 if (type == "whitelist" || type == "filterlist")
147 { 144 {
148 addProperty("contentType", 0x7FFFFFFF & ~( 145 addProperty("contentType", 0x7FFFFFFF & ~(
149 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | 146 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE |
150 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE | 147 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE |
151 RegExpFilter.typeMap.GENERICBLOCK 148 RegExpFilter.typeMap.GENERICBLOCK
152 )); 149 ));
153 addProperty("matchCase", "false"); 150 addProperty("matchCase", "false");
154 addProperty("thirdParty", "null"); 151 addProperty("thirdParty", "null");
155 addProperty("domains", ""); 152 addProperty("domains", "");
156 addProperty("sitekeys", ""); 153 addProperty("sitekeys", "");
157 } 154 }
158 if (type == "filterlist") 155 if (type == "filterlist")
159 { 156 {
160 addProperty("collapse", "null"); 157 addProperty("collapse", "null");
161 } 158 }
162 if (type == "elemhide" || type == "elemhideexception" || type == "cssrule") 159 if (type == "elemhide" || type == "elemhideexception" ||
160 type == "elemhideemulation")
163 { 161 {
164 addProperty("selectorDomain", ""); 162 addProperty("selectorDomain", "");
165 addProperty("domains", ""); 163 addProperty("domains", "");
166 } 164 }
167 if (type == "cssrule")
168 {
169 addProperty("regexp", "");
170 addProperty("prefix", "");
171 addProperty("suffix", "");
172 }
173 } 165 }
174 166
175 function compareFilter(test, text, expected, postInit) 167 function compareFilter(test, text, expected, postInit)
176 { 168 {
177 addDefaults(expected); 169 addDefaults(expected);
178 170
179 let filter = Filter.fromText(text); 171 let filter = Filter.fromText(text);
180 if (postInit) 172 if (postInit)
181 postInit(filter) 173 postInit(filter)
182 let result = serializeFilter(filter); 174 let result = serializeFilter(filter);
(...skipping 26 matching lines...) Expand all
209 test.equal(typeof Filter, "function", "typeof Filter"); 201 test.equal(typeof Filter, "function", "typeof Filter");
210 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); 202 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
211 test.equal(typeof CommentFilter, "function", "typeof CommentFilter"); 203 test.equal(typeof CommentFilter, "function", "typeof CommentFilter");
212 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); 204 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
213 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); 205 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
214 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); 206 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
215 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); 207 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
216 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); 208 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
217 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); 209 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
218 test.equal(typeof ElemHideException, "function", "typeof ElemHideException"); 210 test.equal(typeof ElemHideException, "function", "typeof ElemHideException");
219 test.equal(typeof CSSPropertyFilter, "function", "typeof CSSPropertyFilter"); 211 test.equal(typeof ElemHideEmulationFilter, "function",
212 "typeof ElemHideEmulationFilter");
220 213
221 test.done(); 214 test.done();
222 }; 215 };
223 216
224 exports.testComments = function(test) 217 exports.testComments = function(test)
225 { 218 {
226 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]); 219 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]);
227 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]); 220 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]);
228 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]); 221 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]);
229 222
230 test.done(); 223 test.done();
231 }; 224 };
232 225
233 exports.testInvalidFilters = function(test) 226 exports.testInvalidFilters = function(test)
234 { 227 {
235 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]); 228 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]);
236 compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason= filter_unknown_option"]); 229 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"]); 230 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"]); 231 compareFilter(test, "#*", ["type=invalid", "text=#*", "reason=filter_elemhide_ nocriteria"]);
239 232
240 function compareCSSRule(domains) 233 function checkElemHideEmulationFilterInvalid(domains)
241 { 234 {
242 let filterText = domains + "##[-abp-properties='abc']"; 235 let filterText = domains + "##[-abp-properties='abc']";
243 compareFilter(test, filterText, ["type=invalid", "text=" + filterText, "reas on=filter_cssproperty_nodomain"]); 236 compareFilter(test, filterText,
237 ["type=invalid", "text=" + filterText,
238 "reason=filter_elemhideemulation_nodomain"]);
244 } 239 }
245 compareCSSRule(""); 240 checkElemHideEmulationFilterInvalid("");
246 compareCSSRule("~foo.com"); 241 checkElemHideEmulationFilterInvalid("~foo.com");
247 compareCSSRule("~foo.com,~bar.com"); 242 checkElemHideEmulationFilterInvalid("~foo.com,~bar.com");
248 compareCSSRule("foo"); 243 checkElemHideEmulationFilterInvalid("foo");
249 compareCSSRule("~foo.com,bar"); 244 checkElemHideEmulationFilterInvalid("~foo.com,bar");
250 245
251 test.done(); 246 test.done();
252 }; 247 };
253 248
254 exports.testFiltersWithState = function(test) 249 exports.testFiltersWithState = function(test)
255 { 250 {
256 compareFilter(test, "blabla", ["type=filterlist", "text=blabla", "regexp=blabl a"]); 251 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) 252 compareFilter(test, "blabla_default", ["type=filterlist", "text=blabla_default ", "regexp=blabla_default"], function(filter)
258 { 253 {
259 filter.disabled = false; 254 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"]']); 339 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"]); 340 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"]); 341 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"]); 342 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"]); 343 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" ]); 344 compareFilter(test, "foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo, ~baz,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ" ]);
350 345
351 test.done(); 346 test.done();
352 }; 347 };
353 348
354 exports.testCSSPropertyFilters = function(test) 349 exports.testElemHideEmulationFilters = function(test)
355 { 350 {
356 // Check valid domain combinations 351 // 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"]); 352 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"]); 353 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"]); 354 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"]); 355 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 356
362 compareFilter(test, "##[-abp-properties='']", ["type=elemhide", "text=##[-abp- properties='']", "selector=[-abp-properties='']"]); 357 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"]); 358 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"]); 359 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\\:.*\\)"]); 360 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 361
372 test.done(); 362 test.done();
373 }; 363 };
374 364
375 exports.testEmptyElemHideDomains = function(test) 365 exports.testEmptyElemHideDomains = function(test)
376 { 366 {
377 let emptyDomainFilters = [ 367 let emptyDomainFilters = [
378 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector", 368 ",##selector", ",,,##selector", "~,foo.com##selector", "foo.com,##selector",
379 ",foo.com##selector", "foo.com,~##selector", 369 ",foo.com##selector", "foo.com,~##selector",
380 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector" 370 "foo.com,,bar.com##selector", "foo.com,~,bar.com##selector"
381 ]; 371 ];
382 372
383 for (let filterText of emptyDomainFilters) 373 for (let filterText of emptyDomainFilters)
384 { 374 {
385 let filter = Filter.fromText(filterText); 375 let filter = Filter.fromText(filterText);
386 test.ok(filter instanceof InvalidFilter); 376 test.ok(filter instanceof InvalidFilter);
387 test.equal(filter.reason, "filter_invalid_domain"); 377 test.equal(filter.reason, "filter_invalid_domain");
388 } 378 }
389 379
390 test.done(); 380 test.done();
391 }; 381 };
OLDNEW
« no previous file with comments | « test/elemHideEmulation.js ('k') | test/filterListener.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld