| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /* | |
|
sergei
2016/06/16 21:17:30
Why is license header removed?
Wladimir Palant
2016/12/06 10:48:31
It rather wasn't there in the first place. But I u
| |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | |
| 4 * | |
| 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 | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 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/>. | |
| 16 */ | |
| 17 | |
| 18 "use strict"; | 1 "use strict"; |
| 19 | 2 |
| 20 let { | 3 let { |
| 21 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, | 4 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, |
| 22 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, | 5 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, |
| 23 ElemHideException, CSSPropertyFilter | 6 ElemHideException, CSSPropertyFilter |
| 24 } = require("../lib/filterClasses"); | 7 } = require("../lib/filterClasses"); |
| 25 | 8 |
| 26 function serializeFilter(filter) | 9 exports.testFromText = function(test) |
| 27 { | 10 { |
| 28 // Filter serialization only writes out essential properties, need to do a ful l serialization here | 11 let tests = [ |
| 29 let result = []; | 12 ["!asdf", CommentFilter, "comment"], |
| 30 result.push("text=" + filter.text); | 13 ["asdf", BlockingFilter, "blocking"], |
| 31 if (filter instanceof InvalidFilter) | 14 ["asdf$image,~collapse", BlockingFilter, "blocking"], |
| 32 { | 15 ["/asdf/", BlockingFilter, "blocking"], |
| 33 result.push("type=invalid"); | 16 ["/asdf??+/", InvalidFilter, "invalid"], |
| 34 result.push("reason=" + filter.reason); | 17 ["@@asdf", WhitelistFilter, "whitelist"], |
| 35 } | 18 ["@@asdf$image,~collapse", WhitelistFilter, "whitelist"], |
| 36 else if (filter instanceof CommentFilter) | 19 ["@@/asdf/", WhitelistFilter, "whitelist"], |
| 37 { | 20 ["@@/asdf??+/", InvalidFilter, "invalid"], |
| 38 result.push("type=comment"); | 21 ["##asdf", ElemHideFilter, "elemhide"], |
| 39 } | 22 ["#@#asdf", ElemHideException, "elemhideexception"], |
| 40 else if (filter instanceof ActiveFilter) | 23 ["foobar##asdf", ElemHideFilter, "elemhide"], |
| 41 { | 24 ["foobar#@#asdf", ElemHideException, "elemhideexception"], |
| 42 result.push("disabled=" + filter.disabled); | 25 ["foobar##a", ElemHideFilter, "elemhide"], |
| 43 result.push("lastHit=" + filter.lastHit); | 26 ["foobar#@#a", ElemHideException, "elemhideexception"], |
| 44 result.push("hitCount=" + filter.hitCount); | 27 |
| 45 | 28 ["foobar#asdf", BlockingFilter, "blocking"], |
| 46 let domains = []; | 29 ["foobar|foobas##asdf", BlockingFilter, "blocking"], |
| 47 if (filter.domains) | 30 ["foobar##asdf{asdf}", BlockingFilter, "blocking"], |
| 31 ["foobar##", BlockingFilter, "blocking"], | |
| 32 ["foobar#@#", BlockingFilter, "blocking"], | |
| 33 ["asdf$foobar", InvalidFilter, "invalid"], | |
| 34 ["asdf$image,foobar", InvalidFilter, "invalid"], | |
| 35 ["asdf$image=foobar", BlockingFilter, "blocking"], | |
| 36 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "blocking"], | |
| 37 | |
| 38 ["##foo[-abp-properties='something']bar", InvalidFilter, "invalid"], | |
| 39 ["#@#foo[-abp-properties='something']bar", ElemHideException, "elemhideexcep tion"], | |
| 40 ["example.com##foo[-abp-properties='something']bar", CSSPropertyFilter, "css property"], | |
| 41 ["example.com#@#foo[-abp-properties='something']bar", ElemHideException, "el emhideexception"], | |
| 42 ["~example.com##foo[-abp-properties='something']bar", InvalidFilter, "invali d"], | |
| 43 ["~example.com#@#foo[-abp-properties='something']bar", ElemHideException, "e lemhideexception"], | |
| 44 ["~example.com,~example.info##foo[-abp-properties='something']bar", InvalidF ilter, "invalid"], | |
| 45 ["~example.com,~example.info#@#foo[-abp-properties='something']bar", ElemHid eException, "elemhideexception"], | |
| 46 ["~sub.example.com,example.com##foo[-abp-properties='something']bar", CSSPro pertyFilter, "cssproperty"], | |
| 47 ["~sub.example.com,example.com#@#foo[-abp-properties='something']bar", ElemH ideException, "elemhideexception"], | |
| 48 ["example.com,~sub.example.com##foo[-abp-properties='something']bar", CSSPro pertyFilter, "cssproperty"], | |
| 49 ["example.com,~sub.example.com#@#foo[-abp-properties='something']bar", ElemH ideException, "elemhideexception"], | |
| 50 ["example.com##[-abp-properties='something']", CSSPropertyFilter, "cssproper ty"], | |
| 51 ["example.com#@#[-abp-properties='something']", ElemHideException, "elemhide exception"], | |
| 52 ["example.com##[-abp-properties=\"something\"]", CSSPropertyFilter, "cssprop erty"], | |
| 53 ["example.com#@#[-abp-properties=\"something\"]", ElemHideException, "elemhi deexception"], | |
| 54 ["example.com##[-abp-properties=(something)]", ElemHideFilter, "elemhide"], | |
| 55 ["example.com#@#[-abp-properties=(something)]", ElemHideException, "elemhide exception"], | |
| 56 ]; | |
| 57 for (let [text, type, typeName, location] of tests) | |
| 58 { | |
| 59 let filter = Filter.fromText(text); | |
| 60 test.ok(filter instanceof Filter, "Got filter for " + text); | |
| 61 test.equal(filter.text, text, "Correct filter text for " + text); | |
| 62 test.ok(filter instanceof type, "Correct filter type for " + text); | |
| 63 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeNam e); | |
| 64 if (type == InvalidFilter) | |
| 65 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); | |
| 66 filter.delete(); | |
| 67 } | |
| 68 test.done(); | |
| 69 }; | |
| 70 | |
| 71 exports.testClassHierarchy = function(test) | |
| 72 { | |
| 73 let allClasses = ["Filter", "InvalidFilter", "CommentFilter", "ActiveFilter", | |
| 74 "RegExpFilter", "BlockingFilter", "WhitelistFilter", "ElemHideBase", | |
| 75 "ElemHideFilter", "ElemHideException", "CSSPropertyFilter"]; | |
| 76 let tests = [ | |
| 77 ["/asdf??+/", "Filter", "InvalidFilter"], | |
| 78 ["!asdf", "Filter", "CommentFilter"], | |
| 79 ["asdf", "Filter", "ActiveFilter", "RegExpFilter", "BlockingFilter"], | |
| 80 ["@@asdf", "Filter", "ActiveFilter", "RegExpFilter", "WhitelistFilter"], | |
| 81 ["##asdf", "Filter", "ActiveFilter", "ElemHideBase", "ElemHideFilter"], | |
| 82 ["#@#asdf", "Filter", "ActiveFilter", "ElemHideBase", "ElemHideException"], | |
| 83 ["example.com##[-abp-properties='something']", "Filter", "ActiveFilter", "El emHideBase", "CSSPropertyFilter"], | |
| 84 ]; | |
| 85 | |
| 86 for (let list of tests) | |
| 87 { | |
| 88 let filter = Filter.fromText(list.shift()); | |
| 89 for (let cls of list) | |
| 48 { | 90 { |
| 49 for (let domain in filter.domains) | 91 test.ok(filter instanceof eval(cls), |
| 50 if (domain != "") | 92 "Filter " + filter.text + " is an instance of " + cls); |
| 51 domains.push(filter.domains[domain] ? domain : "~" + domain); | |
| 52 } | 93 } |
| 53 result.push("domains=" + domains.sort().join("|")); | 94 |
| 54 | 95 for (let cls of allClasses) |
| 55 if (filter instanceof RegExpFilter) | |
| 56 { | 96 { |
| 57 result.push("regexp=" + filter.regexp.source); | 97 if (list.indexOf(cls) < 0) |
| 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 { | 98 { |
| 67 result.push("type=filterlist"); | 99 test.ok(!(filter instanceof eval(cls)), |
| 68 result.push("collapse=" + filter.collapse); | 100 "Filter " + filter.text + " isn't an instance of " + cls); |
| 69 } | |
| 70 else if (filter instanceof WhitelistFilter) | |
| 71 { | |
| 72 result.push("type=whitelist"); | |
| 73 } | 101 } |
| 74 } | 102 } |
| 75 else if (filter instanceof ElemHideBase) | 103 filter.delete(); |
| 104 } | |
| 105 | |
| 106 test.done(); | |
| 107 }; | |
| 108 | |
| 109 exports.testGetKnownFilter = function(test) | |
| 110 { | |
| 111 let filter1 = Filter.getKnownFilter("someknownfilter"); | |
| 112 test.ok(!filter1, "Unknown filter returns null"); | |
| 113 | |
| 114 let filter2 = Filter.fromText("someknownfilter"); | |
| 115 filter1 = Filter.getKnownFilter("someknownfilter"); | |
| 116 test.ok(filter1, "Known filter returned"); | |
| 117 | |
| 118 filter2.hitCount = 432; | |
| 119 test.equal(filter1.hitCount, 432, "Changes on previous instance are reflected on new instance"); | |
| 120 | |
| 121 filter1.delete(); | |
| 122 filter2.delete(); | |
| 123 | |
| 124 test.done(); | |
| 125 }; | |
| 126 | |
| 127 exports.testNormalize = function(test) | |
| 128 { | |
| 129 let tests = [ | |
| 130 [" ! comment something ", "! comment something"], | |
| 131 [" ! \n comment something ", "! comment something"], | |
| 132 [" foo bar ", "foobar"], | |
| 133 [" foo , bar # # foo > bar ", "foo,bar##foo > bar", "foo,bar", "foo > bar"] , | |
| 134 [" foo , bar # @ # foo > bar ", "foo,bar#@#foo > bar", "foo,bar", "foo > b ar"], | |
| 135 ["foOBar"], | |
| 136 ["foOBar#xyz"], | |
| 137 ["foOBar$iMaGe,object_subrequest,~coLLapse", "foOBar$image,object-subrequest ,~collapse"], | |
| 138 ["foOBar$doMain=EXample.COM|~exAMPLE.РФ", "foOBar$domain=example.com|~exampl e.рф"], | |
| 139 ["foOBar$sitekeY=SiteKey", "foOBar$sitekey=SiteKey"], | |
| 140 ["exampLE.com##fooBAr", "example.com##fooBAr"], | |
| 141 ["exampLE.com#@#fooBAr", "example.com#@#fooBAr"], | |
| 142 ["exampLE.РФ#@#fooBAr", "example.рф#@#fooBAr"], | |
| 143 ]; | |
| 144 | |
| 145 for (let [text, expected, selectorDomain, selector] of tests) | |
| 146 { | |
| 147 if (!expected) | |
| 148 expected = text; | |
| 149 | |
| 150 let filter1 = Filter.fromText(text); | |
| 151 let filter2 = Filter.fromText(expected); | |
| 152 | |
| 153 test.equal(filter1.text, expected, "Filter text " + text + " got normalized" ); | |
| 154 test.equal(filter2.text, expected, "Already normalized text " + expected + " didn't change"); | |
| 155 | |
| 156 if (filter1 instanceof ActiveFilter) | |
| 76 { | 157 { |
| 77 if (filter instanceof ElemHideFilter) | 158 filter1.hitCount = 567; |
| 78 result.push("type=elemhide"); | 159 test.equal(filter1.hitCount, filter2.hitCount, "Property changes on filter " + text + " get reflected on filter " + expected); |
| 79 else if (filter instanceof ElemHideException) | |
| 80 result.push("type=elemhideexception"); | |
| 81 else if (filter instanceof CSSPropertyFilter) | |
| 82 { | |
| 83 result.push("type=cssrule"); | |
| 84 result.push("prefix=" + (filter.selectorPrefix || "")); | |
| 85 result.push("regexp=" + filter.regexpString); | |
| 86 result.push("suffix=" + (filter.selectorSuffix || "")); | |
| 87 } | |
| 88 | |
| 89 result.push("selectorDomain=" + (filter.selectorDomain || "")); | |
| 90 result.push("selector=" + filter.selector); | |
| 91 } | 160 } |
| 92 } | 161 |
| 93 return result; | 162 if (selectorDomain) |
| 163 { | |
| 164 let expectedDomains = selectorDomain.split(",").sort().join(","); | |
| 165 let actualDomains1 = filter1.selectorDomain.split(",").sort().join(","); | |
| 166 let actualDomains2 = filter2.selectorDomain.split(",").sort().join(","); | |
| 167 test.equal(actualDomains1, expectedDomains, "Correct selector domain for f ilter " + text); | |
| 168 test.equal(actualDomains1, expectedDomains, "Correct selector domain for f ilter " + expected); | |
| 169 | |
| 170 test.equal(filter1.selector, selector, "Correct selector for filter " + te xt); | |
| 171 test.equal(filter2.selector, selector, "Correct selector for filter " + ex pected); | |
| 172 } | |
| 173 | |
| 174 filter1.delete(); | |
| 175 filter2.delete(); | |
| 176 } | |
| 177 | |
| 178 test.done(); | |
| 179 }; | |
| 180 | |
| 181 exports.testSerialize = function(test) | |
| 182 { | |
| 183 // Comment | |
| 184 let filter = Filter.fromText("! serialize"); | |
| 185 test.equal(filter.serialize(), "[Filter]\ntext=! serialize\n"); | |
| 186 filter.delete(); | |
| 187 | |
| 188 // Blocking filter | |
| 189 filter = Filter.fromText("serialize"); | |
| 190 test.equal(filter.serialize(), "[Filter]\ntext=serialize\n"); | |
| 191 filter.disabled = true; | |
| 192 test.equal(filter.serialize(), "[Filter]\ntext=serialize\ndisabled=true\n"); | |
| 193 filter.disabled = false; | |
| 194 filter.hitCount = 10; | |
| 195 filter.lastHit = 12; | |
| 196 test.equal(filter.serialize(), "[Filter]\ntext=serialize\nhitCount=10\nlastHit =12\n"); | |
| 197 filter.delete(); | |
| 198 | |
| 199 // Invalid filter | |
| 200 filter = Filter.fromText("serialize$foobar"); | |
| 201 test.equal(filter.serialize(), "[Filter]\ntext=serialize$foobar\n"); | |
| 202 filter.delete(); | |
| 203 | |
| 204 // Element hiding filter | |
| 205 filter = Filter.fromText("example.com##serialize"); | |
| 206 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n"); | |
| 207 filter.disabled = true; | |
| 208 filter.lastHit = 5; | |
| 209 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\ndisable d=true\nlastHit=5\n"); | |
| 210 filter.delete(); | |
| 211 | |
| 212 test.done(); | |
| 213 }; | |
| 214 | |
| 215 exports.testInvalidReasons = function(test) | |
| 216 { | |
| 217 let tests = [ | |
| 218 ["/??/", "filter_invalid_regexp"], | |
| 219 ["asd$foobar", "filter_unknown_option"], | |
| 220 ["~foo.com##[-abp-properties='abc']", "filter_cssproperty_nodomain"], | |
| 221 ]; | |
| 222 | |
| 223 for (let [text, reason] of tests) | |
| 224 { | |
| 225 let filter = Filter.fromText(text); | |
| 226 test.equals(filter.reason, reason, "Reason why filter " + text + " is invali d"); | |
| 227 filter.delete(); | |
| 228 } | |
| 229 | |
| 230 test.done(); | |
| 231 }; | |
| 232 | |
| 233 exports.testActiveFilter = function(test) | |
| 234 { | |
| 235 let filter1 = Filter.fromText("asdf"); | |
| 236 let filter1copy = Filter.fromText("asdf"); | |
| 237 let filter2 = Filter.fromText("##foobar"); | |
| 238 | |
| 239 test.ok(!filter1.disabled && !filter1copy.disabled && !filter2.disabled, "Filt ers are initially enabled"); | |
| 240 filter1.disabled = true; | |
| 241 test.ok(filter1.disabled, "Disabling filter works"); | |
| 242 test.ok(filter1copy.disabled, "Filter copies are also disabled"); | |
| 243 test.ok(!filter2.disabled, "Disabling one filter doesn't disable others"); | |
| 244 | |
| 245 test.ok(filter1.hitCount === 0 && filter1copy.hitCount === 0 && filter2.hitCou nt === 0, "Filters have no hit initially"); | |
| 246 filter1.hitCount = 5; | |
| 247 test.equal(filter1.hitCount, 5, "Changing hit count works"); | |
| 248 test.equal(filter1copy.hitCount, 5, "Hit count of filter copies is also change d"); | |
| 249 test.equal(filter2.hitCount, 0, "Hit count of other filters isn't affected"); | |
| 250 | |
| 251 test.ok(filter1.lastHit === 0 && filter1copy.lastHit === 0 && filter2.lastHit === 0, "Filters have no last hit time initially"); | |
| 252 filter1.lastHit = 10; | |
| 253 test.equal(filter1.lastHit, 10, "Changing last hit time works"); | |
| 254 test.equal(filter1copy.lastHit, 10, "Last hit time of filter copies is also ch anged"); | |
| 255 test.equal(filter2.lastHit, 0, "Last hit time of other filters isn't affected" ); | |
| 256 | |
| 257 filter1.delete(); | |
| 258 filter1copy.delete(); | |
| 259 filter2.delete(); | |
| 260 | |
| 261 test.done(); | |
| 262 }; | |
| 263 | |
| 264 exports.testIsGeneric = function(test) | |
| 265 { | |
| 266 let tests = [ | |
| 267 ["asfd", true], | |
| 268 ["|http://example.com/asdf", true], | |
| 269 ["||example.com/asdf", true], | |
| 270 ["asfd$third-party", true], | |
| 271 ["asdf$domain=com", false], | |
| 272 ["asdf$domain=example.com", false], | |
| 273 ["asdf$image,domain=example.com", false], | |
| 274 ["asdf$~image,domain=example.com", false], | |
| 275 ["asdf$third-party,domain=example.com", false], | |
| 276 ["||example.com/asdf$~coLLapse,domain=example.com", false], | |
| 277 ["||example.com/asdf$domain=~example.com", true], | |
| 278 ["||example.com/asdf$third-party,domain=~example.com", true], | |
| 279 ["asdf$domain=foo.example.com|~example.com", false], | |
| 280 ["asdf$domain=foo.com|~example.com", false], | |
| 281 ["asdf$domain=~foo.com|~example.com", true], | |
| 282 ]; | |
| 283 | |
| 284 for (let [text, generic] of tests) | |
| 285 { | |
| 286 let filter = Filter.fromText(text); | |
| 287 test.equal(filter.isGeneric(), generic, "Filter " + text + " is generic"); | |
| 288 filter.delete(); | |
| 289 } | |
| 290 | |
| 291 test.done(); | |
| 94 } | 292 } |
| 95 | 293 |
| 96 function addDefaults(expected) | 294 exports.testElemHideSelector = function(test) |
| 97 { | 295 { |
| 98 let type = null; | 296 function doTest(text, selector, selectorDomain) |
| 99 let hasProperty = {}; | 297 { |
| 100 for (let entry of expected) | 298 let filter = Filter.fromText(text); |
| 101 { | 299 test.equal(filter.selector, selector, "Correct selector for " + text); |
| 102 if (/^type=(.*)/.test(entry)) | 300 |
| 103 type = RegExp.$1; | 301 let actualDomains = filter.selectorDomain.split(",").sort().join(","); |
| 104 else if (/^(\w+)/.test(entry)) | 302 let expectedDomains = selectorDomain.split(",").sort().join(","); |
| 105 hasProperty[RegExp.$1] = true; | 303 test.equal(actualDomains, expectedDomains, "Correct domains list for " + tex t); |
| 106 } | 304 |
| 107 | 305 filter.delete(); |
| 108 function addProperty(prop, value) | 306 } |
| 109 { | 307 |
| 110 if (!(prop in hasProperty)) | 308 let tests = [ |
| 111 expected.push(prop + "=" + value); | 309 ["##foobar", "foobar", ""], |
| 112 } | 310 ["~example.com##foobar", "foobar", ""], |
| 113 | 311 ["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") | 312 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)","xyz"], |
| 115 { | 313 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"], |
| 116 addProperty("disabled", "false"); | 314 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"], |
| 117 addProperty("lastHit", "0"); | 315 |
| 118 addProperty("hitCount", "0"); | 316 // Good idea to test this? Maybe consider behavior undefined in this case. |
| 119 } | 317 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"], |
| 120 if (type == "whitelist" || type == "filterlist") | 318 ]; |
| 121 { | 319 |
| 122 addProperty("contentType", 0x7FFFFFFF & ~( | 320 for (let [text, selector, selectorDomain] of tests) |
| 123 RegExpFilter.typeMap.DOCUMENT | RegExpFilter.typeMap.ELEMHIDE | | 321 { |
| 124 RegExpFilter.typeMap.POPUP | RegExpFilter.typeMap.GENERICHIDE | | 322 doTest(text, selector, selectorDomain); |
| 125 RegExpFilter.typeMap.GENERICBLOCK | 323 doTest(text.replace("##", "#@#"), selector, selectorDomain); |
| 126 )); | 324 } |
| 127 addProperty("matchCase", "false"); | 325 |
| 128 addProperty("thirdParty", "null"); | 326 test.done(); |
| 129 addProperty("domains", ""); | 327 }; |
| 130 addProperty("sitekeys", ""); | 328 |
| 131 } | 329 exports.testCSSRules = function(test) |
| 132 if (type == "filterlist") | 330 { |
| 133 { | 331 let tests = [ |
| 134 addProperty("collapse", "null"); | 332 ["foo.com##[-abp-properties='abc']", "abc", "", ""], |
| 135 } | 333 ["foo.com##[-abp-properties='a\"bc']", "a\\\"bc", "", ""], |
| 136 if (type == "elemhide" || type == "elemhideexception" || type == "cssrule") | 334 ["foo.com##[-abp-properties=\"abc\"]", "abc", "", ""], |
| 137 { | 335 ["foo.com##[-abp-properties=\"a'bc\"]", "a\\'bc", "", ""], |
| 138 addProperty("selectorDomain", ""); | 336 ["foo.com##aaa [-abp-properties='abc'] bbb", "abc", "aaa ", " bbb"], |
| 139 addProperty("domains", ""); | 337 ["foo.com##[-abp-properties='|background-image: url(data:*)']", "^background \\-image\\:\\ url\\(data\\:.*\\)", "", ""], |
| 140 } | 338 ]; |
| 141 if (type == "cssrule") | 339 |
| 142 { | 340 for (let [text, regexp, prefix, suffix] of tests) |
| 143 addProperty("regexp", ""); | 341 { |
| 144 addProperty("prefix", ""); | 342 let filter = Filter.fromText(text); |
| 145 addProperty("suffix", ""); | 343 test.equal(filter.regexpString, regexp, "Regular expression of " + text); |
| 146 } | 344 test.equal(filter.selectorPrefix, prefix, "Selector prefix of " + text); |
| 147 } | 345 test.equal(filter.selectorSuffix, suffix, "Selector suffix of " + text); |
| 148 | 346 filter.delete(); |
| 149 function compareFilter(test, text, expected, postInit) | 347 } |
| 150 { | 348 |
| 151 addDefaults(expected); | 349 test.done(); |
| 152 | 350 }; |
| 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 }; | |
| OLD | NEW |