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

Side by Side Diff: test/filterClasses.js

Issue 29354827: Issue 4394 - Create a filter class for element hiding emulation filters [WIP] (Closed)
Patch Set: Created Sept. 23, 2016, 4:21 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
« lib/filterClasses.js ('K') | « lib/filterListener.js ('k') | no next file » | 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
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 let { 20 let {
21 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, 21 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter,
22 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, 22 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter,
23 ElemHideException, CSSPropertyFilter 23 ElemHideException, ElemHideEmulationFilter
24 } = require("../lib/filterClasses"); 24 } = require("../lib/filterClasses");
25 25
26 function serializeFilter(filter) 26 function serializeFilter(filter)
27 { 27 {
28 // Filter serialization only writes out essential properties, need to do a ful l serialization here 28 // Filter serialization only writes out essential properties, need to do a ful l serialization here
29 let result = []; 29 let result = [];
30 result.push("text=" + filter.text); 30 result.push("text=" + filter.text);
31 if (filter instanceof InvalidFilter) 31 if (filter instanceof InvalidFilter)
32 { 32 {
33 result.push("type=invalid"); 33 result.push("type=invalid");
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 { 71 {
72 result.push("type=whitelist"); 72 result.push("type=whitelist");
73 } 73 }
74 } 74 }
75 else if (filter instanceof ElemHideBase) 75 else if (filter instanceof ElemHideBase)
76 { 76 {
77 if (filter instanceof ElemHideFilter) 77 if (filter instanceof ElemHideFilter)
78 result.push("type=elemhide"); 78 result.push("type=elemhide");
79 else if (filter instanceof ElemHideException) 79 else if (filter instanceof ElemHideException)
80 result.push("type=elemhideexception"); 80 result.push("type=elemhideexception");
81 else if (filter instanceof CSSPropertyFilter) 81 else if (filter instanceof ElemHideEmulationFilter)
82 { 82 {
83 result.push("type=cssrule"); 83 result.push("type=elemhideemulation");
84 result.push("prefix=" + (filter.selectorPrefix || "")); 84 result.push("prefix=" + (filter.selectorPrefix || ""));
85 result.push("regexp=" + filter.regexpString); 85 result.push("regexp=" + filter.regexpString);
86 result.push("suffix=" + (filter.selectorSuffix || "")); 86 result.push("suffix=" + (filter.selectorSuffix || ""));
87 } 87 }
88 88
89 result.push("selectorDomain=" + (filter.selectorDomain || "")); 89 result.push("selectorDomain=" + (filter.selectorDomain || ""));
90 result.push("selector=" + filter.selector); 90 result.push("selector=" + filter.selector);
91 } 91 }
92 } 92 }
93 return result; 93 return result;
(...skipping 10 matching lines...) Expand all
104 else if (/^(\w+)/.test(entry)) 104 else if (/^(\w+)/.test(entry))
105 hasProperty[RegExp.$1] = true; 105 hasProperty[RegExp.$1] = true;
106 } 106 }
107 107
108 function addProperty(prop, value) 108 function addProperty(prop, value)
109 { 109 {
110 if (!(prop in hasProperty)) 110 if (!(prop in hasProperty))
111 expected.push(prop + "=" + value); 111 expected.push(prop + "=" + value);
112 } 112 }
113 113
114 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type == "elemhideexception" || type == "cssrule") 114 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type == "elemhideexception" || type == "elemhideemulation")
115 { 115 {
116 addProperty("disabled", "false"); 116 addProperty("disabled", "false");
117 addProperty("lastHit", "0"); 117 addProperty("lastHit", "0");
118 addProperty("hitCount", "0"); 118 addProperty("hitCount", "0");
119 } 119 }
120 if (type == "whitelist" || type == "filterlist") 120 if (type == "whitelist" || type == "filterlist")
121 { 121 {
122 addProperty("contentType", 0x7FFFFFFF & ~( 122 addProperty("contentType", 0x7FFFFFFF & ~(
123 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | 123 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE |
124 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE | 124 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE |
125 RegExpFilter.typeMap.GENERICBLOCK 125 RegExpFilter.typeMap.GENERICBLOCK
126 )); 126 ));
127 addProperty("matchCase", "false"); 127 addProperty("matchCase", "false");
128 addProperty("thirdParty", "null"); 128 addProperty("thirdParty", "null");
129 addProperty("domains", ""); 129 addProperty("domains", "");
130 addProperty("sitekeys", ""); 130 addProperty("sitekeys", "");
131 } 131 }
132 if (type == "filterlist") 132 if (type == "filterlist")
133 { 133 {
134 addProperty("collapse", "null"); 134 addProperty("collapse", "null");
135 } 135 }
136 if (type == "elemhide" || type == "elemhideexception" || type == "cssrule") 136 if (type == "elemhide" || type == "elemhideexception" || type == "elemhideemul ation")
137 { 137 {
138 addProperty("selectorDomain", ""); 138 addProperty("selectorDomain", "");
139 addProperty("domains", ""); 139 addProperty("domains", "");
140 } 140 }
141 if (type == "cssrule") 141 if (type == "elemhideemulation")
142 { 142 {
143 addProperty("regexp", ""); 143 addProperty("regexp", "");
144 addProperty("prefix", ""); 144 addProperty("prefix", "");
145 addProperty("suffix", ""); 145 addProperty("suffix", "");
146 } 146 }
147 } 147 }
148 148
149 function compareFilter(test, text, expected, postInit) 149 function compareFilter(test, text, expected, postInit)
150 { 150 {
151 addDefaults(expected); 151 addDefaults(expected);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
183 test.equal(typeof Filter, "function", "typeof Filter"); 183 test.equal(typeof Filter, "function", "typeof Filter");
184 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); 184 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
185 test.equal(typeof CommentFilter, "function", "typeof CommentFilter"); 185 test.equal(typeof CommentFilter, "function", "typeof CommentFilter");
186 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); 186 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
187 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); 187 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
188 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); 188 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
189 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); 189 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
190 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); 190 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
191 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); 191 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
192 test.equal(typeof ElemHideException, "function", "typeof ElemHideException"); 192 test.equal(typeof ElemHideException, "function", "typeof ElemHideException");
193 test.equal(typeof CSSPropertyFilter, "function", "typeof CSSPropertyFilter"); 193 test.equal(typeof ElemHideEmulationFilter, "function", "typeof ElemHideEmulati onFilter");
194 194
195 test.done(); 195 test.done();
196 }; 196 };
197 197
198 exports.testComments = function(test) 198 exports.testComments = function(test)
199 { 199 {
200 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]); 200 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]);
201 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]); 201 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]);
202 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]); 202 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]);
203 203
204 test.done(); 204 test.done();
205 }; 205 };
206 206
207 exports.testInvalidFilters = function(test) 207 exports.testInvalidFilters = function(test)
208 { 208 {
209 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]); 209 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]);
210 compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason= filter_unknown_option"]); 210 compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason= filter_unknown_option"]);
211 compareFilter(test, "#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "r eason=filter_elemhide_duplicate_id"]); 211 compareFilter(test, "#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "r eason=filter_elemhide_duplicate_id"]);
212 compareFilter(test, "#*", ["type=invalid", "text=#*", "reason=filter_elemhide_ nocriteria"]); 212 compareFilter(test, "#*", ["type=invalid", "text=#*", "reason=filter_elemhide_ nocriteria"]);
213 213
214 function compareCSSRule(domains) 214 function compareCSSRule(domains)
215 { 215 {
216 let filterText = domains + "##[-abp-properties='abc']"; 216 let filterText = domains + "##[-abp-properties='abc']";
217 compareFilter(test, filterText, ["type=invalid", "text=" + filterText, "reas on=filter_cssproperty_nodomain"]); 217 compareFilter(test, filterText, ["type=invalid", "text=" + filterText, "reas on=filter_elemhideemulation_nodomain"]);
218 } 218 }
219 compareCSSRule(""); 219 compareCSSRule("");
220 compareCSSRule("~foo.com"); 220 compareCSSRule("~foo.com");
221 compareCSSRule("~foo.com,~bar.com"); 221 compareCSSRule("~foo.com,~bar.com");
222 compareCSSRule("foo"); 222 compareCSSRule("foo");
223 compareCSSRule("~foo.com,bar"); 223 compareCSSRule("~foo.com,bar");
224 224
225 test.done(); 225 test.done();
226 }; 226 };
227 227
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 compareFilter(test, "#@*(foo=bar)", ["type=elemhideexception", "text=#@*(foo=b ar)", 'selector=[foo="bar"]']); 321 compareFilter(test, "#@*(foo=bar)", ["type=elemhideexception", "text=#@*(foo=b ar)", 'selector=[foo="bar"]']);
322 compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "t ext=#@#body > div:first-child", "selector=body > div:first-child"]); 322 compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "t ext=#@#body > div:first-child", "selector=body > div:first-child"]);
323 compareFilter(test, "foo#@ddd", ["type=elemhideexception", "text=foo#@ddd", "s electorDomain=foo", "selector=ddd", "domains=FOO"]); 323 compareFilter(test, "foo#@ddd", ["type=elemhideexception", "text=foo#@ddd", "s electorDomain=foo", "selector=ddd", "domains=FOO"]);
324 compareFilter(test, "foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); 324 compareFilter(test, "foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
325 compareFilter(test, "foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar #@ddd", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); 325 compareFilter(test, "foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar #@ddd", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]);
326 compareFilter(test, "foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo, ~baz,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ" ]); 326 compareFilter(test, "foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo, ~baz,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ" ]);
327 327
328 test.done(); 328 test.done();
329 }; 329 };
330 330
331 exports.testCSSPropertyFilters = function(test) 331 exports.testElemHideEmulationFilters = function(test)
Felix Dahlke 2016/09/23 16:41:59 I'll add to these tests to make sure the parsing c
332 { 332 {
333 // Check valid domain combinations 333 // Check valid domain combinations
334 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"]); 334 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", "regexp=abc"]);
335 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"]); 335 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", "regex p=abc"]);
336 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"]); 336 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", "regexp=abc"]);
337 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"]); 337 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", "regex p=abc"]);
338 338
339 compareFilter(test, "##[-abp-properties='']", ["type=elemhide", "text=##[-abp- properties='']", "selector=[-abp-properties='']"]); 339 compareFilter(test, "##[-abp-properties='']", ["type=elemhide", "text=##[-abp- properties='']", "selector=[-abp-properties='']"]);
340 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"]); 340 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"]);
341 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"]); 341 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", "prefix=a aa ", "regexp=abc", "suffix= bbb"]);
342 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\\:.*\\)"]); 342 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", "regexp=^background\\-image\\:\\ url\\(data\\:.*\\)"]);
343 343
344 test.done(); 344 test.done();
345 }; 345 };
OLDNEW
« lib/filterClasses.js ('K') | « lib/filterListener.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld