| OLD | NEW |
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH | 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * | 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
| 8 * | 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 let Filter = null; | 22 let Filter = null; |
| 23 let InvalidFilter = null; | 23 let InvalidFilter = null; |
| 24 let CommentFilter = null; | 24 let CommentFilter = null; |
| 25 let ActiveFilter = null; | 25 let ActiveFilter = null; |
| 26 let RegExpFilter = null; | 26 let RegExpFilter = null; |
| 27 let BlockingFilter = null; | 27 let BlockingFilter = null; |
| 28 let WhitelistFilter = null; | 28 let WhitelistFilter = null; |
| 29 let ElemHideBase = null; | 29 let ElemHideBase = null; |
| 30 let ElemHideFilter = null; | 30 let ElemHideFilter = null; |
| 31 let ElemHideException = null; | 31 let ElemHideException = null; |
| 32 let CSSPropertyFilter = null; | 32 let ElemHideEmulationFilter = null; |
| 33 | 33 |
| 34 exports.setUp = function(callback) | 34 exports.setUp = function(callback) |
| 35 { | 35 { |
| 36 let sandboxedRequire = createSandbox(); | 36 let sandboxedRequire = createSandbox(); |
| 37 ( | 37 ( |
| 38 { | 38 { |
| 39 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, | 39 Filter, InvalidFilter, CommentFilter, ActiveFilter, RegExpFilter, |
| 40 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, | 40 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, |
| 41 ElemHideException, CSSPropertyFilter | 41 ElemHideException, ElemHideEmulationFilter |
| 42 } = sandboxedRequire("../lib/filterClassesNew") | 42 } = sandboxedRequire("../lib/filterClassesNew") |
| 43 ); | 43 ); |
| 44 callback(); | 44 callback(); |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 exports.testFromText = function(test) | 47 exports.testFromText = function(test) |
| 48 { | 48 { |
| 49 let tests = [ | 49 let tests = [ |
| 50 ["!asdf", CommentFilter, "comment"], | 50 ["!asdf", CommentFilter, "comment"], |
| 51 ["asdf", BlockingFilter, "blocking"], | 51 ["asdf", BlockingFilter, "blocking"], |
| (...skipping 16 matching lines...) Expand all Loading... |
| 68 ["foobar##asdf{asdf}", BlockingFilter, "blocking"], | 68 ["foobar##asdf{asdf}", BlockingFilter, "blocking"], |
| 69 ["foobar##", BlockingFilter, "blocking"], | 69 ["foobar##", BlockingFilter, "blocking"], |
| 70 ["foobar#@#", BlockingFilter, "blocking"], | 70 ["foobar#@#", BlockingFilter, "blocking"], |
| 71 ["asdf$foobar", InvalidFilter, "invalid"], | 71 ["asdf$foobar", InvalidFilter, "invalid"], |
| 72 ["asdf$image,foobar", InvalidFilter, "invalid"], | 72 ["asdf$image,foobar", InvalidFilter, "invalid"], |
| 73 ["asdf$image=foobar", BlockingFilter, "blocking"], | 73 ["asdf$image=foobar", BlockingFilter, "blocking"], |
| 74 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "blocking"], | 74 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "blocking"], |
| 75 | 75 |
| 76 ["##foo[-abp-properties='something']bar", InvalidFilter, "invalid"], | 76 ["##foo[-abp-properties='something']bar", InvalidFilter, "invalid"], |
| 77 ["#@#foo[-abp-properties='something']bar", ElemHideException, "elemhideexcep
tion"], | 77 ["#@#foo[-abp-properties='something']bar", ElemHideException, "elemhideexcep
tion"], |
| 78 ["example.com##foo[-abp-properties='something']bar", CSSPropertyFilter, "css
property"], | 78 ["example.com##foo[-abp-properties='something']bar", ElemHideEmulationFilter
, "elemhideemulation"], |
| 79 ["example.com#@#foo[-abp-properties='something']bar", ElemHideException, "el
emhideexception"], | 79 ["example.com#@#foo[-abp-properties='something']bar", ElemHideException, "el
emhideexception"], |
| 80 ["~example.com##foo[-abp-properties='something']bar", InvalidFilter, "invali
d"], | 80 ["~example.com##foo[-abp-properties='something']bar", InvalidFilter, "invali
d"], |
| 81 ["~example.com#@#foo[-abp-properties='something']bar", ElemHideException, "e
lemhideexception"], | 81 ["~example.com#@#foo[-abp-properties='something']bar", ElemHideException, "e
lemhideexception"], |
| 82 ["~example.com,~example.info##foo[-abp-properties='something']bar", InvalidF
ilter, "invalid"], | 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"], | 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"], | 84 ["~sub.example.com,example.com##foo[-abp-properties='something']bar", ElemHi
deEmulationFilter, "elemhideemulation"], |
| 85 ["~sub.example.com,example.com#@#foo[-abp-properties='something']bar", ElemH
ideException, "elemhideexception"], | 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"], | 86 ["example.com,~sub.example.com##foo[-abp-properties='something']bar", ElemHi
deEmulationFilter, "elemhideemulation"], |
| 87 ["example.com,~sub.example.com#@#foo[-abp-properties='something']bar", ElemH
ideException, "elemhideexception"], | 87 ["example.com,~sub.example.com#@#foo[-abp-properties='something']bar", ElemH
ideException, "elemhideexception"], |
| 88 ["example.com##[-abp-properties='something']", CSSPropertyFilter, "cssproper
ty"], | 88 ["example.com##[-abp-properties='something']", ElemHideEmulationFilter, "ele
mhideemulation"], |
| 89 ["example.com#@#[-abp-properties='something']", ElemHideException, "elemhide
exception"], | 89 ["example.com#@#[-abp-properties='something']", ElemHideException, "elemhide
exception"], |
| 90 ["example.com##[-abp-properties=\"something\"]", CSSPropertyFilter, "cssprop
erty"], | 90 ["example.com##[-abp-properties=\"something\"]", ElemHideEmulationFilter, "e
lemhideemulation"], |
| 91 ["example.com#@#[-abp-properties=\"something\"]", ElemHideException, "elemhi
deexception"], | 91 ["example.com#@#[-abp-properties=\"something\"]", ElemHideException, "elemhi
deexception"], |
| 92 ["example.com##[-abp-properties=(something)]", ElemHideFilter, "elemhide"], | 92 ["example.com##[-abp-properties=(something)]", ElemHideEmulationFilter, "ele
mhideemulation"], |
| 93 ["example.com#@#[-abp-properties=(something)]", ElemHideException, "elemhide
exception"], | 93 ["example.com#@#[-abp-properties=(something)]", ElemHideException, "elemhide
exception"], |
| 94 ]; | 94 ]; |
| 95 for (let [text, type, typeName, location] of tests) | 95 for (let [text, type, typeName, location] of tests) |
| 96 { | 96 { |
| 97 let filter = Filter.fromText(text); | 97 let filter = Filter.fromText(text); |
| 98 test.ok(filter instanceof Filter, "Got filter for " + text); | 98 test.ok(filter instanceof Filter, "Got filter for " + text); |
| 99 test.equal(filter.text, text, "Correct filter text for " + text); | 99 test.equal(filter.text, text, "Correct filter text for " + text); |
| 100 test.ok(filter instanceof type, "Correct filter type 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); | 101 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeNam
e); |
| 102 if (type == InvalidFilter) | 102 if (type == InvalidFilter) |
| 103 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); | 103 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); |
| 104 filter.delete(); | 104 filter.delete(); |
| 105 } | 105 } |
| 106 test.done(); | 106 test.done(); |
| 107 }; | 107 }; |
| 108 | 108 |
| 109 exports.testClassHierarchy = function(test) | 109 exports.testClassHierarchy = function(test) |
| 110 { | 110 { |
| 111 let allClasses = ["Filter", "InvalidFilter", "CommentFilter", "ActiveFilter", | 111 let allClasses = ["Filter", "InvalidFilter", "CommentFilter", "ActiveFilter", |
| 112 "RegExpFilter", "BlockingFilter", "WhitelistFilter", "ElemHideBase", | 112 "RegExpFilter", "BlockingFilter", "WhitelistFilter", "ElemHideBase", |
| 113 "ElemHideFilter", "ElemHideException", "CSSPropertyFilter"]; | 113 "ElemHideFilter", "ElemHideException", "ElemHideEmulationFilter"]; |
| 114 let tests = [ | 114 let tests = [ |
| 115 ["/asdf??+/", "Filter", "InvalidFilter"], | 115 ["/asdf??+/", "Filter", "InvalidFilter"], |
| 116 ["!asdf", "Filter", "CommentFilter"], | 116 ["!asdf", "Filter", "CommentFilter"], |
| 117 ["asdf", "Filter", "ActiveFilter", "RegExpFilter", "BlockingFilter"], | 117 ["asdf", "Filter", "ActiveFilter", "RegExpFilter", "BlockingFilter"], |
| 118 ["@@asdf", "Filter", "ActiveFilter", "RegExpFilter", "WhitelistFilter"], | 118 ["@@asdf", "Filter", "ActiveFilter", "RegExpFilter", "WhitelistFilter"], |
| 119 ["##asdf", "Filter", "ActiveFilter", "ElemHideBase", "ElemHideFilter"], | 119 ["##asdf", "Filter", "ActiveFilter", "ElemHideBase", "ElemHideFilter"], |
| 120 ["#@#asdf", "Filter", "ActiveFilter", "ElemHideBase", "ElemHideException"], | 120 ["#@#asdf", "Filter", "ActiveFilter", "ElemHideBase", "ElemHideException"], |
| 121 ["example.com##[-abp-properties='something']", "Filter", "ActiveFilter", "El
emHideBase", "CSSPropertyFilter"], | 121 ["example.com##[-abp-properties='something']", "Filter", "ActiveFilter", "El
emHideBase", "ElemHideEmulationFilter"], |
| 122 ]; | 122 ]; |
| 123 | 123 |
| 124 for (let list of tests) | 124 for (let list of tests) |
| 125 { | 125 { |
| 126 let filter = Filter.fromText(list.shift()); | 126 let filter = Filter.fromText(list.shift()); |
| 127 for (let cls of list) | 127 for (let cls of list) |
| 128 { | 128 { |
| 129 test.ok(filter instanceof eval(cls), | 129 test.ok(filter instanceof eval(cls), |
| 130 "Filter " + filter.text + " is an instance of " + cls); | 130 "Filter " + filter.text + " is an instance of " + cls); |
| 131 } | 131 } |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 filter.delete(); | 253 filter.delete(); |
| 254 | 254 |
| 255 test.done(); | 255 test.done(); |
| 256 }; | 256 }; |
| 257 | 257 |
| 258 exports.testInvalidReasons = function(test) | 258 exports.testInvalidReasons = function(test) |
| 259 { | 259 { |
| 260 let tests = [ | 260 let tests = [ |
| 261 ["/??/", "filter_invalid_regexp"], | 261 ["/??/", "filter_invalid_regexp"], |
| 262 ["asd$foobar", "filter_unknown_option"], | 262 ["asd$foobar", "filter_unknown_option"], |
| 263 ["~foo.com##[-abp-properties='abc']", "filter_cssproperty_nodomain"], | 263 ["~foo.com##[-abp-properties='abc']", "filter_elemhideemulation_nodomain"], |
| 264 ]; | 264 ]; |
| 265 | 265 |
| 266 for (let [text, reason] of tests) | 266 for (let [text, reason] of tests) |
| 267 { | 267 { |
| 268 let filter = Filter.fromText(text); | 268 let filter = Filter.fromText(text); |
| 269 test.equals(filter.reason, reason, "Reason why filter " + text + " is invali
d"); | 269 test.equals(filter.reason, reason, "Reason why filter " + text + " is invali
d"); |
| 270 filter.delete(); | 270 filter.delete(); |
| 271 } | 271 } |
| 272 | 272 |
| 273 test.done(); | 273 test.done(); |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 filter.delete(); | 348 filter.delete(); |
| 349 } | 349 } |
| 350 | 350 |
| 351 let tests = [ | 351 let tests = [ |
| 352 ["##foobar", "foobar", ""], | 352 ["##foobar", "foobar", ""], |
| 353 ["~example.com##foobar", "foobar", ""], | 353 ["~example.com##foobar", "foobar", ""], |
| 354 ["example.com##body > div:first-child", "body > div:first-child", "example.c
om"], | 354 ["example.com##body > div:first-child", "body > div:first-child", "example.c
om"], |
| 355 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)","xyz"], | 355 ["xYz,~example.com##foobar:not(whatever)", "foobar:not(whatever)","xyz"], |
| 356 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"], | 356 ["~xyz,com,~abc.com,example.info##foobar", "foobar", "com,example.info"], |
| 357 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"], | 357 ["foo,bar,bas,bam##foobar", "foobar", "foo,bar,bas,bam"], |
| 358 ["foo.com##x[-abp-properties='abc']y", "x[-abp-properties='abc']y", "foo.com
"], |
| 358 | 359 |
| 359 // Good idea to test this? Maybe consider behavior undefined in this case. | 360 // Good idea to test this? Maybe consider behavior undefined in this case. |
| 360 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"], | 361 ["foo,bar,bas,~bar##foobar", "foobar", "foo,bas"], |
| 361 ]; | 362 ]; |
| 362 | 363 |
| 363 for (let [text, selector, selectorDomain] of tests) | 364 for (let [text, selector, selectorDomain] of tests) |
| 364 { | 365 { |
| 365 doTest(text, selector, selectorDomain); | 366 doTest(text, selector, selectorDomain); |
| 366 doTest(text.replace("##", "#@#"), selector, selectorDomain); | 367 doTest(text.replace("##", "#@#"), selector, selectorDomain); |
| 367 } | 368 } |
| 368 | 369 |
| 369 test.done(); | 370 test.done(); |
| 370 }; | 371 }; |
| 371 | |
| 372 exports.testCSSRules = function(test) | |
| 373 { | |
| 374 let tests = [ | |
| 375 ["foo.com##[-abp-properties='abc']", "abc", "", ""], | |
| 376 ["foo.com##[-abp-properties='a\"bc']", "a\\\"bc", "", ""], | |
| 377 ["foo.com##[-abp-properties=\"abc\"]", "abc", "", ""], | |
| 378 ["foo.com##[-abp-properties=\"a'bc\"]", "a\\'bc", "", ""], | |
| 379 ["foo.com##aaa [-abp-properties='abc'] bbb", "abc", "aaa ", " bbb"], | |
| 380 ["foo.com##[-abp-properties='|background-image: url(data:*)']", "^background
\\-image\\:\\ url\\(data\\:.*\\)", "", ""], | |
| 381 ]; | |
| 382 | |
| 383 for (let [text, regexp, prefix, suffix] of tests) | |
| 384 { | |
| 385 let filter = Filter.fromText(text); | |
| 386 test.equal(filter.regexpString, regexp, "Regular expression of " + text); | |
| 387 test.equal(filter.selectorPrefix, prefix, "Selector prefix of " + text); | |
| 388 test.equal(filter.selectorSuffix, suffix, "Selector suffix of " + text); | |
| 389 filter.delete(); | |
| 390 } | |
| 391 | |
| 392 test.done(); | |
| 393 }; | |
| OLD | NEW |