LEFT | RIGHT |
(no file at all) | |
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 {createSandbox} = require("./_common"); |
21 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, | 21 |
22 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, | 22 let Filter = null; |
23 ElemHideException, CSSPropertyFilter | 23 let InvalidFilter = null; |
24 } = require("../lib/filterClasses"); | 24 let CommentFilter = null; |
25 | 25 let ActiveFilter = null; |
26 function serializeFilter(filter) | 26 let RegExpFilter = null; |
27 { | 27 let BlockingFilter = null; |
28 // Filter serialization only writes out essential properties, need to do a ful
l serialization here | 28 let WhitelistFilter = null; |
29 let result = []; | 29 let ElemHideBase = null; |
30 result.push("text=" + filter.text); | 30 let ElemHideFilter = null; |
31 if (filter instanceof InvalidFilter) | 31 let ElemHideException = null; |
32 { | 32 let CSSPropertyFilter = null; |
33 result.push("type=invalid"); | 33 |
34 result.push("reason=" + filter.reason); | 34 exports.setUp = function(callback) |
35 } | 35 { |
36 else if (filter instanceof CommentFilter) | 36 let sandboxedRequire = createSandbox(); |
37 { | 37 ( |
38 result.push("type=comment"); | 38 { |
39 } | 39 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, |
40 else if (filter instanceof ActiveFilter) | 40 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, |
41 { | 41 ElemHideException, CSSPropertyFilter |
42 result.push("disabled=" + filter.disabled); | 42 } = sandboxedRequire("../lib/filterClassesNew") |
43 result.push("lastHit=" + filter.lastHit); | 43 ); |
44 result.push("hitCount=" + filter.hitCount); | 44 callback(); |
45 | 45 }; |
46 let domains = []; | 46 |
47 if (filter.domains) | 47 exports.testFromText = function(test) |
48 { | 48 { |
49 for (let domain in filter.domains) | 49 let tests = [ |
50 if (domain != "") | 50 ["!asdf", CommentFilter, "comment"], |
51 domains.push(filter.domains[domain] ? domain : "~" + domain); | 51 ["asdf", BlockingFilter, "blocking"], |
| 52 ["asdf$image,~collapse", BlockingFilter, "blocking"], |
| 53 ["/asdf/", BlockingFilter, "blocking"], |
| 54 ["/asdf??+/", InvalidFilter, "invalid"], |
| 55 ["@@asdf", WhitelistFilter, "whitelist"], |
| 56 ["@@asdf$image,~collapse", WhitelistFilter, "whitelist"], |
| 57 ["@@/asdf/", WhitelistFilter, "whitelist"], |
| 58 ["@@/asdf??+/", InvalidFilter, "invalid"], |
| 59 ["##asdf", ElemHideFilter, "elemhide"], |
| 60 ["#@#asdf", ElemHideException, "elemhideexception"], |
| 61 ["foobar##asdf", ElemHideFilter, "elemhide"], |
| 62 ["foobar#@#asdf", ElemHideException, "elemhideexception"], |
| 63 ["foobar##a", ElemHideFilter, "elemhide"], |
| 64 ["foobar#@#a", ElemHideException, "elemhideexception"], |
| 65 |
| 66 ["foobar#asdf", BlockingFilter, "blocking"], |
| 67 ["foobar|foobas##asdf", BlockingFilter, "blocking"], |
| 68 ["foobar##asdf{asdf}", BlockingFilter, "blocking"], |
| 69 ["foobar##", BlockingFilter, "blocking"], |
| 70 ["foobar#@#", BlockingFilter, "blocking"], |
| 71 ["asdf$foobar", InvalidFilter, "invalid"], |
| 72 ["asdf$image,foobar", InvalidFilter, "invalid"], |
| 73 ["asdf$image=foobar", BlockingFilter, "blocking"], |
| 74 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "blocking"], |
| 75 |
| 76 ["##foo[-abp-properties='something']bar", InvalidFilter, "invalid"], |
| 77 ["#@#foo[-abp-properties='something']bar", ElemHideException, "elemhideexcep
tion"], |
| 78 ["example.com##foo[-abp-properties='something']bar", CSSPropertyFilter, "css
property"], |
| 79 ["example.com#@#foo[-abp-properties='something']bar", ElemHideException, "el
emhideexception"], |
| 80 ["~example.com##foo[-abp-properties='something']bar", InvalidFilter, "invali
d"], |
| 81 ["~example.com#@#foo[-abp-properties='something']bar", ElemHideException, "e
lemhideexception"], |
| 82 ["~example.com,~example.info##foo[-abp-properties='something']bar", InvalidF
ilter, "invalid"], |
| 83 ["~example.com,~example.info#@#foo[-abp-properties='something']bar", ElemHid
eException, "elemhideexception"], |
| 84 ["~sub.example.com,example.com##foo[-abp-properties='something']bar", CSSPro
pertyFilter, "cssproperty"], |
| 85 ["~sub.example.com,example.com#@#foo[-abp-properties='something']bar", ElemH
ideException, "elemhideexception"], |
| 86 ["example.com,~sub.example.com##foo[-abp-properties='something']bar", CSSPro
pertyFilter, "cssproperty"], |
| 87 ["example.com,~sub.example.com#@#foo[-abp-properties='something']bar", ElemH
ideException, "elemhideexception"], |
| 88 ["example.com##[-abp-properties='something']", CSSPropertyFilter, "cssproper
ty"], |
| 89 ["example.com#@#[-abp-properties='something']", ElemHideException, "elemhide
exception"], |
| 90 ["example.com##[-abp-properties=\"something\"]", CSSPropertyFilter, "cssprop
erty"], |
| 91 ["example.com#@#[-abp-properties=\"something\"]", ElemHideException, "elemhi
deexception"], |
| 92 ["example.com##[-abp-properties=(something)]", ElemHideFilter, "elemhide"], |
| 93 ["example.com#@#[-abp-properties=(something)]", ElemHideException, "elemhide
exception"], |
| 94 ]; |
| 95 for (let [text, type, typeName, location] of tests) |
| 96 { |
| 97 let filter = Filter.fromText(text); |
| 98 test.ok(filter instanceof Filter, "Got filter for " + text); |
| 99 test.equal(filter.text, text, "Correct filter text for " + text); |
| 100 test.ok(filter instanceof type, "Correct filter type for " + text); |
| 101 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeNam
e); |
| 102 if (type == InvalidFilter) |
| 103 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); |
| 104 filter.delete(); |
| 105 } |
| 106 test.done(); |
| 107 }; |
| 108 |
| 109 exports.testClassHierarchy = function(test) |
| 110 { |
| 111 let allClasses = ["Filter", "InvalidFilter", "CommentFilter", "ActiveFilter", |
| 112 "RegExpFilter", "BlockingFilter", "WhitelistFilter", "ElemHideBase", |
| 113 "ElemHideFilter", "ElemHideException", "CSSPropertyFilter"]; |
| 114 let tests = [ |
| 115 ["/asdf??+/", "Filter", "InvalidFilter"], |
| 116 ["!asdf", "Filter", "CommentFilter"], |
| 117 ["asdf", "Filter", "ActiveFilter", "RegExpFilter", "BlockingFilter"], |
| 118 ["@@asdf", "Filter", "ActiveFilter", "RegExpFilter", "WhitelistFilter"], |
| 119 ["##asdf", "Filter", "ActiveFilter", "ElemHideBase", "ElemHideFilter"], |
| 120 ["#@#asdf", "Filter", "ActiveFilter", "ElemHideBase", "ElemHideException"], |
| 121 ["example.com##[-abp-properties='something']", "Filter", "ActiveFilter", "El
emHideBase", "CSSPropertyFilter"], |
| 122 ]; |
| 123 |
| 124 for (let list of tests) |
| 125 { |
| 126 let filter = Filter.fromText(list.shift()); |
| 127 for (let cls of list) |
| 128 { |
| 129 test.ok(filter instanceof eval(cls), |
| 130 "Filter " + filter.text + " is an instance of " + cls); |
52 } | 131 } |
53 result.push("domains=" + domains.sort().join("|")); | 132 |
54 | 133 for (let cls of allClasses) |
55 if (filter instanceof RegExpFilter) | 134 { |
56 { | 135 if (list.indexOf(cls) < 0) |
57 result.push("regexp=" + filter.regexp.source); | |
58 result.push("contentType=" + filter.contentType); | |
59 result.push("matchCase=" + filter.matchCase); | |
60 | |
61 let sitekeys = filter.sitekeys || []; | |
62 result.push("sitekeys=" + sitekeys.slice().sort().join("|")); | |
63 | |
64 result.push("thirdParty=" + filter.thirdParty); | |
65 if (filter instanceof BlockingFilter) | |
66 { | 136 { |
67 result.push("type=filterlist"); | 137 test.ok(!(filter instanceof eval(cls)), |
68 result.push("collapse=" + filter.collapse); | 138 "Filter " + filter.text + " isn't an instance of " + cls); |
69 } | |
70 else if (filter instanceof WhitelistFilter) | |
71 { | |
72 result.push("type=whitelist"); | |
73 } | 139 } |
74 } | 140 } |
75 else if (filter instanceof ElemHideBase) | 141 filter.delete(); |
76 { | 142 } |
77 if (filter instanceof ElemHideFilter) | 143 |
78 result.push("type=elemhide"); | 144 test.done(); |
79 else if (filter instanceof ElemHideException) | 145 }; |
80 result.push("type=elemhideexception"); | 146 |
81 else if (filter instanceof CSSPropertyFilter) | 147 exports.testGC = function(test) |
82 { | 148 { |
83 result.push("type=cssrule"); | 149 let filter1 = Filter.fromText("someknownfilter"); |
84 result.push("prefix=" + (filter.selectorPrefix || "")); | 150 test.equal(filter1.hitCount, 0, "Initial hit count"); |
85 result.push("regexp=" + filter.regexpString); | 151 |
86 result.push("suffix=" + (filter.selectorSuffix || "")); | 152 filter1.hitCount = 432; |
87 } | 153 |
88 | 154 let filter2 = Filter.fromText("someknownfilter"); |
89 result.push("selectorDomain=" + (filter.selectorDomain || "")); | 155 test.equal(filter2.hitCount, 432, "Known filter returned"); |
90 result.push("selector=" + filter.selector); | 156 |
| 157 filter2.hitCount = 234; |
| 158 test.equal(filter1.hitCount, 234, "Changing second wrapper modifies original a
s well"); |
| 159 |
| 160 filter1.delete(); |
| 161 filter2.delete(); |
| 162 |
| 163 let filter3 = Filter.fromText("someknownfilter"); |
| 164 test.equal(filter3.hitCount, 0, "Filter data has been reset once previous inst
ances have been released"); |
| 165 filter3.delete(); |
| 166 |
| 167 test.done(); |
| 168 }; |
| 169 |
| 170 exports.testNormalize = function(test) |
| 171 { |
| 172 let tests = [ |
| 173 [" ! comment something ", "! comment something"], |
| 174 [" ! \n comment something ", "! comment something"], |
| 175 [" foo bar ", "foobar"], |
| 176 [" foo , bar # # foo > bar ", "foo,bar##foo > bar", "foo,bar", "foo > bar"]
, |
| 177 [" foo , bar # @ # foo > bar ", "foo,bar#@#foo > bar", "foo,bar", "foo > b
ar"], |
| 178 ["foOBar"], |
| 179 ["foOBar#xyz"], |
| 180 ["foOBar$iMaGe,object_subrequest,~coLLapse", "foOBar$image,object-subrequest
,~collapse"], |
| 181 ["foOBar$doMain=EXample.COM|~exAMPLE.РФ", "foOBar$domain=example.com|~exampl
e.рф"], |
| 182 ["foOBar$sitekeY=SiteKey", "foOBar$sitekey=SiteKey"], |
| 183 ["exampLE.com##fooBAr", "example.com##fooBAr"], |
| 184 ["exampLE.com#@#fooBAr", "example.com#@#fooBAr"], |
| 185 ["exampLE.РФ#@#fooBAr", "example.рф#@#fooBAr"], |
| 186 ]; |
| 187 |
| 188 for (let [text, expected, selectorDomain, selector] of tests) |
| 189 { |
| 190 if (!expected) |
| 191 expected = text; |
| 192 |
| 193 let filter1 = Filter.fromText(text); |
| 194 let filter2 = Filter.fromText(expected); |
| 195 |
| 196 test.equal(filter1.text, expected, "Filter text " + text + " got normalized"
); |
| 197 test.equal(filter2.text, expected, "Already normalized text " + expected + "
didn't change"); |
| 198 |
| 199 if (filter1 instanceof ActiveFilter) |
| 200 { |
| 201 filter1.hitCount = 567; |
| 202 test.equal(filter1.hitCount, filter2.hitCount, "Property changes on filter
" + text + " get reflected on filter " + expected); |
91 } | 203 } |
92 } | 204 |
93 return result; | 205 if (selectorDomain) |
| 206 { |
| 207 let expectedDomains = selectorDomain.split(",").sort().join(","); |
| 208 let actualDomains1 = filter1.selectorDomain.split(",").sort().join(","); |
| 209 let actualDomains2 = filter2.selectorDomain.split(",").sort().join(","); |
| 210 test.equal(actualDomains1, expectedDomains, "Correct selector domain for f
ilter " + text); |
| 211 test.equal(actualDomains1, expectedDomains, "Correct selector domain for f
ilter " + expected); |
| 212 |
| 213 test.equal(filter1.selector, selector, "Correct selector for filter " + te
xt); |
| 214 test.equal(filter2.selector, selector, "Correct selector for filter " + ex
pected); |
| 215 } |
| 216 |
| 217 filter1.delete(); |
| 218 filter2.delete(); |
| 219 } |
| 220 |
| 221 test.done(); |
| 222 }; |
| 223 |
| 224 exports.testSerialize = function(test) |
| 225 { |
| 226 // Comment |
| 227 let filter = Filter.fromText("! serialize"); |
| 228 test.equal(filter.serialize(), "[Filter]\ntext=! serialize\n"); |
| 229 filter.delete(); |
| 230 |
| 231 // Blocking filter |
| 232 filter = Filter.fromText("serialize"); |
| 233 test.equal(filter.serialize(), "[Filter]\ntext=serialize\n"); |
| 234 filter.disabled = true; |
| 235 test.equal(filter.serialize(), "[Filter]\ntext=serialize\ndisabled=true\n"); |
| 236 filter.disabled = false; |
| 237 filter.hitCount = 10; |
| 238 filter.lastHit = 12; |
| 239 test.equal(filter.serialize(), "[Filter]\ntext=serialize\nhitCount=10\nlastHit
=12\n"); |
| 240 filter.delete(); |
| 241 |
| 242 // Invalid filter |
| 243 filter = Filter.fromText("serialize$foobar"); |
| 244 test.equal(filter.serialize(), "[Filter]\ntext=serialize$foobar\n"); |
| 245 filter.delete(); |
| 246 |
| 247 // Element hiding filter |
| 248 filter = Filter.fromText("example.com##serialize"); |
| 249 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n"); |
| 250 filter.disabled = true; |
| 251 filter.lastHit = 5; |
| 252 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\ndisable
d=true\nlastHit=5\n"); |
| 253 filter.delete(); |
| 254 |
| 255 test.done(); |
| 256 }; |
| 257 |
| 258 exports.testInvalidReasons = function(test) |
| 259 { |
| 260 let tests = [ |
| 261 ["/??/", "filter_invalid_regexp"], |
| 262 ["asd$foobar", "filter_unknown_option"], |
| 263 ["~foo.com##[-abp-properties='abc']", "filter_cssproperty_nodomain"], |
| 264 ]; |
| 265 |
| 266 for (let [text, reason] of tests) |
| 267 { |
| 268 let filter = Filter.fromText(text); |
| 269 test.equals(filter.reason, reason, "Reason why filter " + text + " is invali
d"); |
| 270 filter.delete(); |
| 271 } |
| 272 |
| 273 test.done(); |
| 274 }; |
| 275 |
| 276 exports.testActiveFilter = function(test) |
| 277 { |
| 278 let filter1 = Filter.fromText("asdf"); |
| 279 let filter1copy = Filter.fromText("asdf"); |
| 280 let filter2 = Filter.fromText("##foobar"); |
| 281 |
| 282 test.ok(!filter1.disabled && !filter1copy.disabled && !filter2.disabled, "Filt
ers are initially enabled"); |
| 283 filter1.disabled = true; |
| 284 test.ok(filter1.disabled, "Disabling filter works"); |
| 285 test.ok(filter1copy.disabled, "Filter copies are also disabled"); |
| 286 test.ok(!filter2.disabled, "Disabling one filter doesn't disable others"); |
| 287 |
| 288 test.ok(filter1.hitCount === 0 && filter1copy.hitCount === 0 && filter2.hitCou
nt === 0, "Filters have no hit initially"); |
| 289 filter1.hitCount = 5; |
| 290 test.equal(filter1.hitCount, 5, "Changing hit count works"); |
| 291 test.equal(filter1copy.hitCount, 5, "Hit count of filter copies is also change
d"); |
| 292 test.equal(filter2.hitCount, 0, "Hit count of other filters isn't affected"); |
| 293 |
| 294 test.ok(filter1.lastHit === 0 && filter1copy.lastHit === 0 && filter2.lastHit
=== 0, "Filters have no last hit time initially"); |
| 295 filter1.lastHit = 10; |
| 296 test.equal(filter1.lastHit, 10, "Changing last hit time works"); |
| 297 test.equal(filter1copy.lastHit, 10, "Last hit time of filter copies is also ch
anged"); |
| 298 test.equal(filter2.lastHit, 0, "Last hit time of other filters isn't affected"
); |
| 299 |
| 300 filter1.delete(); |
| 301 filter1copy.delete(); |
| 302 filter2.delete(); |
| 303 |
| 304 test.done(); |
| 305 }; |
| 306 |
| 307 exports.testIsGeneric = function(test) |
| 308 { |
| 309 let tests = [ |
| 310 ["asfd", true], |
| 311 ["|http://example.com/asdf", true], |
| 312 ["||example.com/asdf", true], |
| 313 ["asfd$third-party", true], |
| 314 ["asdf$domain=com", false], |
| 315 ["asdf$domain=example.com", false], |
| 316 ["asdf$image,domain=example.com", false], |
| 317 ["asdf$~image,domain=example.com", false], |
| 318 ["asdf$third-party,domain=example.com", false], |
| 319 ["||example.com/asdf$~coLLapse,domain=example.com", false], |
| 320 ["||example.com/asdf$domain=~example.com", true], |
| 321 ["||example.com/asdf$third-party,domain=~example.com", true], |
| 322 ["asdf$domain=foo.example.com|~example.com", false], |
| 323 ["asdf$domain=foo.com|~example.com", false], |
| 324 ["asdf$domain=~foo.com|~example.com", true], |
| 325 ]; |
| 326 |
| 327 for (let [text, generic] of tests) |
| 328 { |
| 329 let filter = Filter.fromText(text); |
| 330 test.equal(filter.isGeneric(), generic, "Filter " + text + " is generic"); |
| 331 filter.delete(); |
| 332 } |
| 333 |
| 334 test.done(); |
94 } | 335 } |
95 | 336 |
96 function addDefaults(expected) | 337 exports.testElemHideSelector = function(test) |
97 { | 338 { |
98 let type = null; | 339 function doTest(text, selector, selectorDomain) |
99 let hasProperty = {}; | 340 { |
100 for (let entry of expected) | 341 let filter = Filter.fromText(text); |
101 { | 342 test.equal(filter.selector, selector, "Correct selector for " + text); |
102 if (/^type=(.*)/.test(entry)) | 343 |
103 type = RegExp.$1; | 344 let actualDomains = filter.selectorDomain.split(",").sort().join(","); |
104 else if (/^(\w+)/.test(entry)) | 345 let expectedDomains = selectorDomain.split(",").sort().join(","); |
105 hasProperty[RegExp.$1] = true; | 346 test.equal(actualDomains, expectedDomains, "Correct domains list for " + tex
t); |
106 } | 347 |
107 | 348 filter.delete(); |
108 function addProperty(prop, value) | 349 } |
109 { | 350 |
110 if (!(prop in hasProperty)) | 351 let tests = [ |
111 expected.push(prop + "=" + value); | 352 ["##foobar", "foobar", ""], |
112 } | 353 ["~example.com##foobar", "foobar", ""], |
113 | 354 ["example.com##body > div:first-child", "body > div:first-child", "example.c
om"], |
114 if (type == "whitelist" || type == "filterlist" || type == "elemhide" || type
== "elemhideexception" || type == "cssrule") | 355 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)","xyz"], |
115 { | 356 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"], |
116 addProperty("disabled", "false"); | 357 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"], |
117 addProperty("lastHit", "0"); | 358 |
118 addProperty("hitCount", "0"); | 359 // Good idea to test this? Maybe consider behavior undefined in this case. |
119 } | 360 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"], |
120 if (type == "whitelist" || type == "filterlist") | 361 ]; |
121 { | 362 |
122 addProperty("contentType", 0x7FFFFFFF & ~( | 363 for (let [text, selector, selectorDomain] of tests) |
123 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | | 364 { |
124 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE | | 365 doTest(text, selector, selectorDomain); |
125 RegExpFilter.typeMap.GENERICBLOCK | 366 doTest(text.replace("##", "#@#"), selector, selectorDomain); |
126 )); | 367 } |
127 addProperty("matchCase", "false"); | 368 |
128 addProperty("thirdParty", "null"); | 369 test.done(); |
129 addProperty("domains", ""); | 370 }; |
130 addProperty("sitekeys", ""); | 371 |
131 } | 372 exports.testCSSRules = function(test) |
132 if (type == "filterlist") | 373 { |
133 { | 374 let tests = [ |
134 addProperty("collapse", "null"); | 375 ["foo.com##[-abp-properties='abc']", "abc", "", ""], |
135 } | 376 ["foo.com##[-abp-properties='a\"bc']", "a\\\"bc", "", ""], |
136 if (type == "elemhide" || type == "elemhideexception" || type == "cssrule") | 377 ["foo.com##[-abp-properties=\"abc\"]", "abc", "", ""], |
137 { | 378 ["foo.com##[-abp-properties=\"a'bc\"]", "a\\'bc", "", ""], |
138 addProperty("selectorDomain", ""); | 379 ["foo.com##aaa [-abp-properties='abc'] bbb", "abc", "aaa ", " bbb"], |
139 addProperty("domains", ""); | 380 ["foo.com##[-abp-properties='|background-image: url(data:*)']", "^background
\\-image\\:\\ url\\(data\\:.*\\)", "", ""], |
140 } | 381 ]; |
141 if (type == "cssrule") | 382 |
142 { | 383 for (let [text, regexp, prefix, suffix] of tests) |
143 addProperty("regexp", ""); | 384 { |
144 addProperty("prefix", ""); | 385 let filter = Filter.fromText(text); |
145 addProperty("suffix", ""); | 386 test.equal(filter.regexpString, regexp, "Regular expression of " + text); |
146 } | 387 test.equal(filter.selectorPrefix, prefix, "Selector prefix of " + text); |
147 } | 388 test.equal(filter.selectorSuffix, suffix, "Selector suffix of " + text); |
148 | 389 filter.delete(); |
149 function compareFilter(test, text, expected, postInit) | 390 } |
150 { | 391 |
151 addDefaults(expected); | 392 test.done(); |
152 | 393 }; |
153 let filter = Filter.fromText(text); | |
154 if (postInit) | |
155 postInit(filter) | |
156 let result = serializeFilter(filter); | |
157 test.equal(result.sort().join("\n"), expected.sort().join("\n"), text); | |
158 | |
159 // Test round-trip | |
160 let filter2; | |
161 let buffer = []; | |
162 filter.serialize(buffer); | |
163 if (buffer.length) | |
164 { | |
165 let map = {__proto__: null}; | |
166 for (let line of buffer.slice(1)) | |
167 { | |
168 if (/(.*?)=(.*)/.test(line)) | |
169 map[RegExp.$1] = RegExp.$2; | |
170 } | |
171 filter2 = Filter.fromObject(map); | |
172 } | |
173 else | |
174 { | |
175 filter2 = Filter.fromText(filter.text); | |
176 } | |
177 | |
178 test.equal(serializeFilter(filter).join("\n"), serializeFilter(filter2).join("
\n"), text + " deserialization"); | |
179 } | |
180 | |
181 exports.testFilterClassDefinitions = function(test) | |
182 { | |
183 test.equal(typeof Filter, "function", "typeof Filter"); | |
184 test.equal(typeof InvalidFilter, "function", "typeof InvalidFilter"); | |
185 test.equal(typeof CommentFilter, "function", "typeof CommentFilter"); | |
186 test.equal(typeof ActiveFilter, "function", "typeof ActiveFilter"); | |
187 test.equal(typeof RegExpFilter, "function", "typeof RegExpFilter"); | |
188 test.equal(typeof BlockingFilter, "function", "typeof BlockingFilter"); | |
189 test.equal(typeof WhitelistFilter, "function", "typeof WhitelistFilter"); | |
190 test.equal(typeof ElemHideBase, "function", "typeof ElemHideBase"); | |
191 test.equal(typeof ElemHideFilter, "function", "typeof ElemHideFilter"); | |
192 test.equal(typeof ElemHideException, "function", "typeof ElemHideException"); | |
193 test.equal(typeof CSSPropertyFilter, "function", "typeof CSSPropertyFilter"); | |
194 | |
195 test.done(); | |
196 }; | |
197 | |
198 exports.testComments = function(test) | |
199 { | |
200 compareFilter(test, "!asdf", ["type=comment", "text=!asdf"]); | |
201 compareFilter(test, "!foo#bar", ["type=comment", "text=!foo#bar"]); | |
202 compareFilter(test, "!foo##bar", ["type=comment", "text=!foo##bar"]); | |
203 | |
204 test.done(); | |
205 }; | |
206 | |
207 exports.testInvalidFilters = function(test) | |
208 { | |
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"]); | |
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"]); | |
213 | |
214 function compareCSSRule(domains) | |
215 { | |
216 let filterText = domains + "##[-abp-properties='abc']"; | |
217 compareFilter(test, filterText, ["type=invalid", "text=" + filterText, "reas
on=filter_cssproperty_nodomain"]); | |
218 } | |
219 compareCSSRule(""); | |
220 compareCSSRule("~foo.com"); | |
221 compareCSSRule("~foo.com,~bar.com"); | |
222 compareCSSRule("foo"); | |
223 compareCSSRule("~foo.com,bar"); | |
224 | |
225 test.done(); | |
226 }; | |
227 | |
228 exports.testFiltersWithState = function(test) | |
229 { | |
230 compareFilter(test, "blabla", ["type=filterlist", "text=blabla", "regexp=blabl
a"]); | |
231 compareFilter(test, "blabla_default", ["type=filterlist", "text=blabla_default
", "regexp=blabla_default"], function(filter) | |
232 { | |
233 filter.disabled = false; | |
234 filter.hitCount = 0; | |
235 filter.lastHit = 0; | |
236 }); | |
237 compareFilter(test, "blabla_non_default", ["type=filterlist", "text=blabla_non
_default", "regexp=blabla_non_default", "disabled=true", "hitCount=12", "lastHit
=20"], function(filter) | |
238 { | |
239 filter.disabled = true; | |
240 filter.hitCount = 12; | |
241 filter.lastHit = 20; | |
242 }); | |
243 | |
244 test.done(); | |
245 }; | |
246 | |
247 let t = RegExpFilter.typeMap; | |
248 let defaultTypes = 0x7FFFFFFF & ~(t.ELEMHIDE | t.DOCUMENT | t.POPUP | t.GENERICH
IDE | t.GENERICBLOCK); | |
249 | |
250 exports.testSpecialCharacters = function(test) | |
251 { | |
252 compareFilter(test, "/ddd|f?a[s]d/", ["type=filterlist", "text=/ddd|f?a[s]d/",
"regexp=ddd|f?a[s]d"]); | |
253 compareFilter(test, "*asdf*d**dd*", ["type=filterlist", "text=*asdf*d**dd*", "
regexp=asdf.*d.*dd"]); | |
254 compareFilter(test, "|*asd|f*d**dd*|", ["type=filterlist", "text=|*asd|f*d**dd
*|", "regexp=^.*asd\\|f.*d.*dd.*$"]); | |
255 compareFilter(test, "dd[]{}$%<>&()d", ["type=filterlist", "text=dd[]{}$%<>&()d
", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d"]); | |
256 | |
257 compareFilter(test, "@@/ddd|f?a[s]d/", ["type=whitelist", "text=@@/ddd|f?a[s]d
/", "regexp=ddd|f?a[s]d", "contentType=" + defaultTypes]); | |
258 compareFilter(test, "@@*asdf*d**dd*", ["type=whitelist", "text=@@*asdf*d**dd*"
, "regexp=asdf.*d.*dd", "contentType=" + defaultTypes]); | |
259 compareFilter(test, "@@|*asd|f*d**dd*|", ["type=whitelist", "text=@@|*asd|f*d*
*dd*|", "regexp=^.*asd\\|f.*d.*dd.*$", "contentType=" + defaultTypes]); | |
260 compareFilter(test, "@@dd[]{}$%<>&()d", ["type=whitelist", "text=@@dd[]{}$%<>&
()d", "regexp=dd\\[\\]\\{\\}\\$\\%\\<\\>\\&\\(\\)d", "contentType=" + defaultTyp
es]); | |
261 | |
262 test.done(); | |
263 }; | |
264 | |
265 exports.testFilterOptions = function(test) | |
266 { | |
267 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"]); | |
268 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"]); | |
269 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"]); | |
270 | |
271 // background and image should be the same for backwards compatibility | |
272 compareFilter(test, "bla$image", ["type=filterlist", "text=bla$image", "regexp
=bla", "contentType=" + (t.IMAGE)]); | |
273 compareFilter(test, "bla$background", ["type=filterlist", "text=bla$background
", "regexp=bla", "contentType=" + (t.IMAGE)]); | |
274 compareFilter(test, "bla$~image", ["type=filterlist", "text=bla$~image", "rege
xp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]); | |
275 compareFilter(test, "bla$~background", ["type=filterlist", "text=bla$~backgrou
nd", "regexp=bla", "contentType=" + (defaultTypes & ~t.IMAGE)]); | |
276 | |
277 compareFilter(test, "@@bla$~script,~other", ["type=whitelist", "text=@@bla$~sc
ript,~other", "regexp=bla", "contentType=" + (defaultTypes & ~(t.SCRIPT | t.OTHE
R))]); | |
278 compareFilter(test, "@@http://bla$~script,~other", ["type=whitelist", "text=@@
http://bla$~script,~other", "regexp=http\\:\\/\\/bla", "contentType=" + (default
Types & ~(t.SCRIPT | t.OTHER))]); | |
279 compareFilter(test, "@@|ftp://bla$~script,~other", ["type=whitelist", "text=@@
|ftp://bla$~script,~other", "regexp=^ftp\\:\\/\\/bla", "contentType=" + (default
Types & ~(t.SCRIPT | t.OTHER))]); | |
280 compareFilter(test, "@@bla$~script,~other,document", ["type=whitelist", "text=
@@bla$~script,~other,document", "regexp=bla", "contentType=" + (defaultTypes &
~(t.SCRIPT | t.OTHER) | t.DOCUMENT)]); | |
281 compareFilter(test, "@@bla$~script,~other,~document", ["type=whitelist", "text
=@@bla$~script,~other,~document", "regexp=bla", "contentType=" + (defaultTypes &
~(t.SCRIPT | t.OTHER))]); | |
282 compareFilter(test, "@@bla$document", ["type=whitelist", "text=@@bla$document"
, "regexp=bla", "contentType=" + t.DOCUMENT]); | |
283 compareFilter(test, "@@bla$~script,~other,elemhide", ["type=whitelist", "text=
@@bla$~script,~other,elemhide", "regexp=bla", "contentType=" + (defaultTypes &
~(t.SCRIPT | t.OTHER) | t.ELEMHIDE)]); | |
284 compareFilter(test, "@@bla$~script,~other,~elemhide", ["type=whitelist", "text
=@@bla$~script,~other,~elemhide", "regexp=bla", "contentType=" + (defaultTypes &
~(t.SCRIPT | t.OTHER))]); | |
285 compareFilter(test, "@@bla$elemhide", ["type=whitelist", "text=@@bla$elemhide"
, "regexp=bla", "contentType=" + t.ELEMHIDE]); | |
286 | |
287 compareFilter(test, "@@bla$~script,~other,donottrack", ["type=invalid", "text=
@@bla$~script,~other,donottrack", "reason=filter_unknown_option"]); | |
288 compareFilter(test, "@@bla$~script,~other,~donottrack", ["type=invalid", "text
=@@bla$~script,~other,~donottrack", "reason=filter_unknown_option"]); | |
289 compareFilter(test, "@@bla$donottrack", ["type=invalid", "text=@@bla$donottrac
k", "reason=filter_unknown_option"]); | |
290 compareFilter(test, "@@bla$foobar", ["type=invalid", "text=@@bla$foobar", "rea
son=filter_unknown_option"]); | |
291 compareFilter(test, "@@bla$image,foobar", ["type=invalid", "text=@@bla$image,f
oobar", "reason=filter_unknown_option"]); | |
292 compareFilter(test, "@@bla$foobar,image", ["type=invalid", "text=@@bla$foobar,
image", "reason=filter_unknown_option"]); | |
293 | |
294 test.done(); | |
295 }; | |
296 | |
297 exports.testElementHidingRules = function(test) | |
298 { | |
299 compareFilter(test, "#ddd", ["type=elemhide", "text=#ddd", "selector=ddd"]); | |
300 compareFilter(test, "#ddd(fff)", ["type=elemhide", "text=#ddd(fff)", "selector
=ddd.fff,ddd#fff"]); | |
301 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"]']); | |
302 compareFilter(test, "#ddd(fff)(foo=bar)", ["type=elemhide", "text=#ddd(fff)(fo
o=bar)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']); | |
303 compareFilter(test, "#*(fff)", ["type=elemhide", "text=#*(fff)", "selector=.ff
f,#fff"]); | |
304 compareFilter(test, "#*(foo=bar)", ["type=elemhide", "text=#*(foo=bar)", 'sele
ctor=[foo="bar"]']); | |
305 compareFilter(test, "##body > div:first-child", ["type=elemhide", "text=##body
> div:first-child", "selector=body > div:first-child"]); | |
306 compareFilter(test, "foo#ddd", ["type=elemhide", "text=foo#ddd", "selectorDoma
in=foo", "selector=ddd", "domains=FOO"]); | |
307 compareFilter(test, "foo,bar#ddd", ["type=elemhide", "text=foo,bar#ddd", "sele
ctorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO"]); | |
308 compareFilter(test, "foo,~bar#ddd", ["type=elemhide", "text=foo,~bar#ddd", "se
lectorDomain=foo", "selector=ddd", "domains=FOO|~BAR"]); | |
309 compareFilter(test, "foo,~baz,bar#ddd", ["type=elemhide", "text=foo,~baz,bar#d
dd", "selectorDomain=foo,bar", "selector=ddd", "domains=BAR|FOO|~BAZ"]); | |
310 | |
311 test.done(); | |
312 }; | |
313 | |
314 exports.testElementHidingExceptions = function(test) | |
315 { | |
316 compareFilter(test, "#@ddd", ["type=elemhideexception", "text=#@ddd", "selecto
r=ddd"]); | |
317 compareFilter(test, "#@ddd(fff)", ["type=elemhideexception", "text=#@ddd(fff)"
, "selector=ddd.fff,ddd#fff"]); | |
318 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"]']); | |
319 compareFilter(test, "#@ddd(fff)(foo=bar)", ["type=elemhideexception", "text=#@
ddd(fff)(foo=bar)", 'selector=ddd.fff[foo="bar"],ddd#fff[foo="bar"]']); | |
320 compareFilter(test, "#@*(fff)", ["type=elemhideexception", "text=#@*(fff)", "s
elector=.fff,#fff"]); | |
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"]); | |
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"]); | |
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"
]); | |
327 | |
328 test.done(); | |
329 }; | |
330 | |
331 exports.testCSSPropertyFilters = function(test) | |
332 { | |
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"]); | |
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"]); | |
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"]); | |
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"]); | |
338 | |
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"]); | |
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"]); | |
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\\:.*\\)"]); | |
343 | |
344 test.done(); | |
345 }; | |
LEFT | RIGHT |