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

Side by Side Diff: test/filterClasses.js

Issue 29336430: Issue 3659 - Remove locale dependency from filter classes (Closed)
Patch Set: Created Feb. 16, 2016, 11:45 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 | « package.json ('k') | test_wrapper.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 "use strict";
2
3 let {
4 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter,
5 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter,
6 ElemHideException, CSSPropertyFilter
7 } = require("../lib/filterClasses");
8
9 function serializeFilter(filter)
10 {
11 // Filter serialization only writes out essential properties, need to do a ful l serialization here
12 let result = [];
13 result.push("text=" + filter.text);
14 if (filter instanceof InvalidFilter)
15 {
16 result.push("type=invalid");
17 result.push("reason=" + filter.reason);
18 }
19 else if (filter instanceof CommentFilter)
20 {
21 result.push("type=comment");
22 }
23 else if (filter instanceof ActiveFilter)
24 {
25 result.push("disabled=" + filter.disabled);
26 result.push("lastHit=" + filter.lastHit);
27 result.push("hitCount=" + filter.hitCount);
28
29 let domains = [];
30 if (filter.domains)
31 {
32 for (let domain in filter.domains)
33 if (domain != "")
34 domains.push(filter.domains[domain] ? domain : "~" + domain);
35 }
36 result.push("domains=" + domains.sort().join("|"));
37
38 if (filter instanceof RegExpFilter)
39 {
40 result.push("regexp=" + filter.regexp.source);
41 result.push("contentType=" + filter.contentType);
42 result.push("matchCase=" + filter.matchCase);
43
44 let sitekeys = filter.sitekeys || [];
45 result.push("sitekeys=" + sitekeys.slice().sort().join("|"));
46
47 result.push("thirdParty=" + filter.thirdParty);
48 if (filter instanceof BlockingFilter)
49 {
50 result.push("type=filterlist");
51 result.push("collapse=" + filter.collapse);
52 }
53 else if (filter instanceof WhitelistFilter)
54 {
55 result.push("type=whitelist");
56 }
57 }
58 else if (filter instanceof ElemHideBase)
59 {
60 if (filter instanceof ElemHideFilter)
61 result.push("type=elemhide");
62 else if (filter instanceof ElemHideException)
63 result.push("type=elemhideexception");
64 else if (filter instanceof CSSPropertyFilter)
65 {
66 result.push("type=cssrule");
67 result.push("prefix=" + (filter.selectorPrefix || ""));
68 result.push("regexp=" + filter.regexpString);
69 result.push("suffix=" + (filter.selectorSuffix || ""));
70 }
71
72 result.push("selectorDomain=" + (filter.selectorDomain || ""));
73 result.push("selector=" + filter.selector);
74 }
75 }
76 return result;
77 }
78
79 function addDefaults(expected)
80 {
81 let type = null;
82 let hasProperty = {};
83 for (let entry of expected)
84 {
85 if (/^type=(.*)/.test(entry))
86 type = RegExp.$1;
87 else if (/^(\w+)/.test(entry))
88 hasProperty[RegExp.$1] = true;
89 }
90
91 function addProperty(prop, value)
92 {
93 if (!(prop in hasProperty))
94 expected.push(prop + "=" + value);
95 }
96
97 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type == "elemhideexception" || type == "cssrule")
98 {
99 addProperty("disabled", "false");
100 addProperty("lastHit", "0");
101 addProperty("hitCount", "0");
102 }
103 if (type == "whitelist" || type == "filterlist")
104 {
105 addProperty("contentType", 0x7FFFFFFF & ~(
106 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE |
107 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE |
108 RegExpFilter.typeMap.GENERICBLOCK
109 ));
110 addProperty("matchCase", "false");
111 addProperty("thirdParty", "null");
112 addProperty("domains", "");
113 addProperty("sitekeys", "");
114 }
115 if (type == "filterlist")
116 {
117 addProperty("collapse", "null");
118 }
119 if (type == "elemhide" || type == "elemhideexception" || type == "cssrule")
120 {
121 addProperty("selectorDomain", "");
122 addProperty("domains", "");
123 }
124 if (type == "cssrule")
125 {
126 addProperty("regexp", "");
127 addProperty("prefix", "");
128 addProperty("suffix", "");
129 }
130 }
131
132 function compareFilter(test, text, expected, postInit)
133 {
134 addDefaults(expected);
135
136 let filter = Filter.fromText(text);
137 if (postInit)
138 postInit(filter)
139 let result = serializeFilter(filter);
140 test.equal(result.sort().join("\n"), expected.sort().join("\n"), text);
141
142 // Test round-trip
143 let filter2;
144 let buffer = [];
145 filter.serialize(buffer);
146 if (buffer.length)
147 {
148 let map = {__proto__: null};
149 for (let line of buffer.slice(1))
150 {
151 if (/(.*?)=(.*)/.test(line))
152 map[RegExp.$1] = RegExp.$2;
153 }
154 filter2 = Filter.fromObject(map);
155 }
156 else
157 {
158 filter2 = Filter.fromText(filter.text);
159 }
160
161 test.equal(serializeFilter(filter).join("\n"), serializeFilter(filter2).join(" \n"), text + " deserialization");
162 }
163
164 exports.testFilterClassDefinitions = function(test)
165 {
166 test.equal(typeof Filter, "function", "typeof Filter");
167 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter");
168 test.equal(typeof CommentFilter, "function", "typeof CommentFilter");
169 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter");
170 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter");
171 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter");
172 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter");
173 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase");
174 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter");
175 test.equal(typeof ElemHideException, "function", "typeof ElemHideException");
176 test.equal(typeof CSSPropertyFilter, "function", "typeof CSSPropertyFilter");
177
178 test.done();
179 };
180
181 exports.testComments = function(test)
182 {
183 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]);
184 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]);
185 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]);
186
187 test.done();
188 };
189
190 exports.testInvalidFilters = function(test)
191 {
192 compareFilter(test, "/??/", ["type=invalid", "text=/??/", "reason=filter_inval id_regexp"]);
193 compareFilter(test, "asd$foobar", ["type=invalid", "text=asd$foobar", "reason= filter_unknown_option"]);
194 compareFilter(test, "#dd(asd)(ddd)", ["type=invalid", "text=#dd(asd)(ddd)", "r eason=filter_elemhide_duplicate_id"]);
195 compareFilter(test, "#*", ["type=invalid", "text=#*", "reason=filter_elemhide_ nocriteria"]);
196
197 function compareCSSRule(domains)
198 {
199 let filterText = domains + "##[-abp-properties='abc']";
200 compareFilter(test, filterText, ["type=invalid", "text=" + filterText, "reas on=filter_cssproperty_nodomain"]);
201 }
202 compareCSSRule("");
203 compareCSSRule("~foo.com");
204 compareCSSRule("~foo.com,~bar.com");
205 compareCSSRule("foo");
206 compareCSSRule("~foo.com,bar");
207
208 test.done();
209 };
210
211 exports.testFiltersWithState = function(test)
212 {
213 compareFilter(test, "blabla", ["type=filterlist", "text=blabla", "regexp=blabl a"]);
214 compareFilter(test, "blabla_default", ["type=filterlist", "text=blabla_default ", "regexp=blabla_default"], function(filter)
215 {
216 filter.disabled = false;
217 filter.hitCount = 0;
218 filter.lastHit = 0;
219 });
220 compareFilter(test, "blabla_non_default", ["type=filterlist", "text=blabla_non _default", "regexp=blabla_non_default", "disabled=true", "hitCount=12", "lastHit =20"], function(filter)
221 {
222 filter.disabled = true;
223 filter.hitCount = 12;
224 filter.lastHit = 20;
225 });
226
227 test.done();
228 };
229
230 let t = RegExpFilter.typeMap;
231 let defaultTypes = 0x7FFFFFFF & ~(t.ELEMHIDE | t.DOCUMENT | t.POPUP | t.GENERICH IDE | t.GENERICBLOCK);
232
233 exports.testSpecialCharacters = function(test)
234 {
235 compareFilter(test, "/ddd|f?a[s]d/", ["type=filterlist", "text=/ddd|f?a[s]d/", "regexp=ddd|f?a[s]d"]);
236 compareFilter(test, "*asdf*d**dd*", ["type=filterlist", "text=*asdf*d**dd*", " regexp=asdf.*d.*dd"]);
237 compareFilter(test, "|*asd|f*d**dd*|", ["type=filterlist", "text=|*asd|f*d**dd *|", "regexp=^.*asd\\|f.*d.*dd.*$"]);
238 compareFilter(test, "dd[]{}$%<>&()d", ["type=filterlist", "text=dd[]{}$%<>&()d ", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d"]);
239
240 compareFilter(test, "@@/ddd|f?a[s]d/", ["type=whitelist", "text=@@/ddd|f?a[s]d /", "regexp=ddd|f?a[s]d", "contentType=" + defaultTypes]);
241 compareFilter(test, "@@*asdf*d**dd*", ["type=whitelist", "text=@@*asdf*d**dd*" , "regexp=asdf.*d.*dd", "contentType=" + defaultTypes]);
242 compareFilter(test, "@@|*asd|f*d**dd*|", ["type=whitelist", "text=@@|*asd|f*d* *dd*|", "regexp=^.*asd\\|f.*d.*dd.*$", "contentType=" + defaultTypes]);
243 compareFilter(test, "@@dd[]{}$%<>&()d", ["type=whitelist", "text=@@dd[]{}$%<>& ()d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d", "contentType=" + defaultTyp es]);
244
245 test.done();
246 };
247
248 exports.testFilterOptions = function(test)
249 {
250 compareFilter(test, "bla$match-case,script,other,third-party,domain=foo.com,si tekey=foo", ["type=filterlist", "text=bla$match-case,script,other,third-party,do main=foo.com,sitekey=foo", "regexp=bla", "matchCase=true", "contentType=" + (t.S CRIPT | t.OTHER), "thirdParty=true", "domains=FOO.COM", "sitekeys=FOO"]);
251 compareFilter(test, "bla$~match-case,~script,~other,~third-party,domain=~bar.c om", ["type=filterlist", "text=bla$~match-case,~script,~other,~third-party,domai n=~bar.com", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER )), "thirdParty=false", "domains=~BAR.COM"]);
252 compareFilter(test, "@@bla$match-case,script,other,third-party,domain=foo.com| bar.com|~bar.foo.com|~foo.bar.com,sitekey=foo|bar", ["type=whitelist", "text=@@b la$match-case,script,other,third-party,domain=foo.com|bar.com|~bar.foo.com|~foo. bar.com,sitekey=foo|bar", "regexp=bla", "matchCase=true", "contentType=" + (t.SC RIPT | t.OTHER), "thirdParty=true", "domains=BAR.COM|FOO.COM|~BAR.FOO.COM|~FOO.B AR.COM", "sitekeys=BAR|FOO"]);
253
254 // background and image should be the same for backwards compatibility
255 compareFilter(test, "bla$image", ["type=filterlist", "text=bla$image", "regexp =bla", "contentType=" + (t.IMAGE)]);
256 compareFilter(test, "bla$background", ["type=filterlist", "text=bla$background ", "regexp=bla", "contentType=" + (t.IMAGE)]);
257 compareFilter(test, "bla$~image", ["type=filterlist", "text=bla$~image", "rege xp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]);
258 compareFilter(test, "bla$~background", ["type=filterlist", "text=bla$~backgrou nd", "regexp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]);
259
260 compareFilter(test, "@@bla$~script,~other", ["type=whitelist", "text=@@bla$~sc ript,~other", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHE R))]);
261 compareFilter(test, "@@http://bla$~script,~other", ["type=whitelist", "text=@@ http://bla$~script,~other", "regexp=http\\:\\/\\/bla", "contentType=" + (default Types & ~(t.SCRIPT | t.OTHER))]);
262 compareFilter(test, "@@|ftp://bla$~script,~other", ["type=whitelist", "text=@@ |ftp://bla$~script,~other", "regexp=^ftp\\:\\/\\/bla", "contentType=" + (default Types & ~(t.SCRIPT | t.OTHER))]);
263 compareFilter(test, "@@bla$~script,~other,document", ["type=whitelist", "text= @@bla$~script,~other,document", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER) | t.DOCUMENT)]);
264 compareFilter(test, "@@bla$~script,~other,~document", ["type=whitelist", "text =@@bla$~script,~other,~document", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
265 compareFilter(test, "@@bla$document", ["type=whitelist", "text=@@bla$document" , "regexp=bla", "contentType=" + t.DOCUMENT]);
266 compareFilter(test, "@@bla$~script,~other,elemhide", ["type=whitelist", "text= @@bla$~script,~other,elemhide", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER) | t.ELEMHIDE)]);
267 compareFilter(test, "@@bla$~script,~other,~elemhide", ["type=whitelist", "text =@@bla$~script,~other,~elemhide", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHER))]);
268 compareFilter(test, "@@bla$elemhide", ["type=whitelist", "text=@@bla$elemhide" , "regexp=bla", "contentType=" + t.ELEMHIDE]);
269
270 compareFilter(test, "@@bla$~script,~other,donottrack", ["type=invalid", "text= @@bla$~script,~other,donottrack", "reason=filter_unknown_option"]);
271 compareFilter(test, "@@bla$~script,~other,~donottrack", ["type=invalid", "text =@@bla$~script,~other,~donottrack", "reason=filter_unknown_option"]);
272 compareFilter(test, "@@bla$donottrack", ["type=invalid", "text=@@bla$donottrac k", "reason=filter_unknown_option"]);
273 compareFilter(test, "@@bla$foobar", ["type=invalid", "text=@@bla$foobar", "rea son=filter_unknown_option"]);
274 compareFilter(test, "@@bla$image,foobar", ["type=invalid", "text=@@bla$image,f oobar", "reason=filter_unknown_option"]);
275 compareFilter(test, "@@bla$foobar,image", ["type=invalid", "text=@@bla$foobar, image", "reason=filter_unknown_option"]);
276
277 test.done();
278 };
279
280 exports.testElementHidingRules = function(test)
281 {
282 compareFilter(test, "#ddd", ["type=elemhide", "text=#ddd", "selector=ddd"]);
283 compareFilter(test, "#ddd(fff)", ["type=elemhide", "text=#ddd(fff)", "selector =ddd.fff,ddd#fff"]);
284 compareFilter(test, "#ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", ["typ e=elemhide", "text=#ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", 'selector =ddd[foo="bar"][foo2^="bar2"][foo3*="bar3"][foo4$="bar4"]']);
285 compareFilter(test, "#ddd(fff)(foo=bar)", ["type=elemhide", "text=#ddd(fff)(fo o=bar)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']);
286 compareFilter(test, "#*(fff)", ["type=elemhide", "text=#*(fff)", "selector=.ff f,#fff"]);
287 compareFilter(test, "#*(foo=bar)", ["type=elemhide", "text=#*(foo=bar)", 'sele ctor=[foo="bar"]']);
288 compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body > div:first-child", "selector=body > div:first-child"]);
289 compareFilter(test, "foo#ddd", ["type=elemhide", "text=foo#ddd", "selectorDoma in=foo", "selector=ddd", "domains=FOO"]);
290 compareFilter(test, "foo,bar#ddd", ["type=elemhide", "text=foo,bar#ddd", "sele ctorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
291 compareFilter(test, "foo,~bar#ddd", ["type=elemhide", "text=foo,~bar#ddd", "se lectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]);
292 compareFilter(test, "foo,~baz,bar#ddd", ["type=elemhide", "text=foo,~baz,bar#d dd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]);
293
294 test.done();
295 };
296
297 exports.testElementHidingExceptions = function(test)
298 {
299 compareFilter(test, "#@ddd", ["type=elemhideexception", "text=#@ddd", "selecto r=ddd"]);
300 compareFilter(test, "#@ddd(fff)", ["type=elemhideexception", "text=#@ddd(fff)" , "selector=ddd.fff,ddd#fff"]);
301 compareFilter(test, "#@ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)", ["ty pe=elemhideexception", "text=#@ddd(foo=bar)(foo2^=bar2)(foo3*=bar3)(foo4$=bar4)" , 'selector=ddd[foo="bar"][foo2^="bar2"][foo3*="bar3"][foo4$="bar4"]']);
302 compareFilter(test, "#@ddd(fff)(foo=bar)", ["type=elemhideexception", "text=#@ ddd(fff)(foo=bar)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']);
303 compareFilter(test, "#@*(fff)", ["type=elemhideexception", "text=#@*(fff)", "s elector=.fff,#fff"]);
304 compareFilter(test, "#@*(foo=bar)", ["type=elemhideexception", "text=#@*(foo=b ar)", 'selector=[foo="bar"]']);
305 compareFilter(test, "#@#body > div:first-child", ["type=elemhideexception", "t ext=#@#body > div:first-child", "selector=body > div:first-child"]);
306 compareFilter(test, "foo#@ddd", ["type=elemhideexception", "text=foo#@ddd", "s electorDomain=foo", "selector=ddd", "domains=FOO"]);
307 compareFilter(test, "foo,bar#@ddd", ["type=elemhideexception", "text=foo,bar#@ ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]);
308 compareFilter(test, "foo,~bar#@ddd", ["type=elemhideexception", "text=foo,~bar #@ddd", "selectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]);
309 compareFilter(test, "foo,~baz,bar#@ddd", ["type=elemhideexception", "text=foo, ~baz,bar#@ddd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ" ]);
310
311 test.done();
312 };
313
314 exports.testCSSPropertyFilters = function(test)
315 {
316 // Check valid domain combinations
317 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"]);
318 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"]);
319 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"]);
320 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"]);
321
322 compareFilter(test, "##[-abp-properties='']", ["type=elemhide", "text=##[-abp- properties='']", "selector=[-abp-properties='']"]);
323 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"]);
324 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"]);
325 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\\:.*\\)"]);
326
327 test.done();
328 };
OLDNEW
« no previous file with comments | « package.json ('k') | test_wrapper.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld