| LEFT | RIGHT |
| 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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 {createSandbox} = require("./_common"); | 20 let {createSandbox} = require("./_common"); |
| 21 const {withNAD} = require("./_test-utils"); |
| 21 | 22 |
| 22 let Filter = null; | 23 let Filter = null; |
| 23 let InvalidFilter = null; | 24 let InvalidFilter = null; |
| 24 let CommentFilter = null; | 25 let CommentFilter = null; |
| 25 let ActiveFilter = null; | 26 let ActiveFilter = null; |
| 26 let RegExpFilter = null; | 27 let RegExpFilter = null; |
| 27 let BlockingFilter = null; | 28 let BlockingFilter = null; |
| 28 let WhitelistFilter = null; | 29 let WhitelistFilter = null; |
| 29 let ElemHideBase = null; | 30 let ElemHideBase = null; |
| 30 let ElemHideFilter = null; | 31 let ElemHideFilter = null; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 41 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, | 42 BlockingFilter, WhitelistFilter, ElemHideBase, ElemHideFilter, |
| 42 ElemHideException, ElemHideEmulationFilter | 43 ElemHideException, ElemHideEmulationFilter |
| 43 } = sandboxedRequire("../lib/filterClasses") | 44 } = sandboxedRequire("../lib/filterClasses") |
| 44 ); | 45 ); |
| 45 ({FilterNotifier} = sandboxedRequire("../lib/filterNotifier")); | 46 ({FilterNotifier} = sandboxedRequire("../lib/filterNotifier")); |
| 46 callback(); | 47 callback(); |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 exports.testFromText = function(test) | 50 exports.testFromText = function(test) |
| 50 { | 51 { |
| 52 // fields are: |
| 53 // text, type, typeName, collapse (optional, BlockingFilter only) |
| 51 let tests = [ | 54 let tests = [ |
| 52 ["!asdf", CommentFilter, "comment"], | 55 ["!asdf", CommentFilter, "comment"], |
| 53 ["asdf", BlockingFilter, "blocking"], | 56 ["asdf", BlockingFilter, "blocking", 1], |
| 54 ["asdf$image,~collapse", BlockingFilter, "blocking"], | 57 ["asdf$image,~collapse", BlockingFilter, "blocking", 0], |
| 55 ["/asdf/", BlockingFilter, "blocking"], | 58 ["/asdf/", BlockingFilter, "blocking", 1], |
| 56 ["/asdf??+/", InvalidFilter, "invalid"], | 59 ["/asdf??+/", InvalidFilter, "invalid"], |
| 57 ["@@asdf", WhitelistFilter, "whitelist"], | 60 ["@@asdf", WhitelistFilter, "whitelist"], |
| 58 ["@@asdf$image,~collapse", WhitelistFilter, "whitelist"], | 61 ["@@asdf$image,~collapse", WhitelistFilter, "whitelist"], |
| 59 ["@@/asdf/", WhitelistFilter, "whitelist"], | 62 ["@@/asdf/", WhitelistFilter, "whitelist"], |
| 60 ["@@/asdf??+/", InvalidFilter, "invalid"], | 63 ["@@/asdf??+/", InvalidFilter, "invalid"], |
| 61 ["##asdf", ElemHideFilter, "elemhide"], | 64 ["##asdf", ElemHideFilter, "elemhide"], |
| 62 ["#@#asdf", ElemHideException, "elemhideexception"], | 65 ["#@#asdf", ElemHideException, "elemhideexception"], |
| 63 ["foobar##asdf", ElemHideFilter, "elemhide"], | 66 ["foobar##asdf", ElemHideFilter, "elemhide"], |
| 64 ["foobar#@#asdf", ElemHideException, "elemhideexception"], | 67 ["foobar#@#asdf", ElemHideException, "elemhideexception"], |
| 65 ["foobar##a", ElemHideFilter, "elemhide"], | 68 ["foobar##a", ElemHideFilter, "elemhide"], |
| 66 ["foobar#@#a", ElemHideException, "elemhideexception"], | 69 ["foobar#@#a", ElemHideException, "elemhideexception"], |
| 67 | 70 |
| 68 ["foobar#asdf", BlockingFilter, "blocking"], | 71 ["foobar#asdf", BlockingFilter, "blocking", 1], |
| 69 ["foobar|foobas##asdf", BlockingFilter, "blocking"], | 72 ["foobar|foobas##asdf", BlockingFilter, "blocking", 1], |
| 70 ["foobar##asdf{asdf}", BlockingFilter, "blocking"], | 73 ["foobar##asdf{asdf}", ElemHideFilter, "elemhide"], |
| 71 ["foobar##", BlockingFilter, "blocking"], | 74 ["foobar##", BlockingFilter, "blocking", 1], |
| 72 ["foobar#@#", BlockingFilter, "blocking"], | 75 ["foobar#@#", BlockingFilter, "blocking", 1], |
| 73 ["asdf$foobar", InvalidFilter, "invalid"], | 76 ["asdf$foobar", InvalidFilter, "invalid"], |
| 74 ["asdf$image,foobar", InvalidFilter, "invalid"], | 77 ["asdf$image,foobar", InvalidFilter, "invalid"], |
| 75 ["asdf$image=foobar", BlockingFilter, "blocking"], | 78 ["asdf$image=foobar", BlockingFilter, "blocking", 1], |
| 76 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "blocking"], | 79 ["asdf$image=foobar=xyz,~collapse", BlockingFilter, "blocking", 0], |
| 77 | 80 |
| 78 ["#?#foo:-abp-properties(something)bar", InvalidFilter, "invalid"], | 81 ["#?#foo:-abp-properties(something)bar", InvalidFilter, "invalid"], |
| 79 ["#@#foo:-abp-properties(something)bar", ElemHideException, "elemhideexcepti
on"], | 82 ["#@#foo:-abp-properties(something)bar", ElemHideException, "elemhideexcepti
on"], |
| 80 ["example.com#?#foo:-abp-properties(something)bar", ElemHideEmulationFilter,
"elemhideemulation"], | 83 ["example.com#?#foo:-abp-properties(something)bar", ElemHideEmulationFilter,
"elemhideemulation"], |
| 81 ["example.com#@#foo:-abp-properties(something)bar", ElemHideException, "elem
hideexception"], | 84 ["example.com#@#foo:-abp-properties(something)bar", ElemHideException, "elem
hideexception"], |
| 82 ["~example.com#?#foo:-abp-properties(something)bar", InvalidFilter, "invalid
"], | 85 ["~example.com#?#foo:-abp-properties(something)bar", InvalidFilter, "invalid
"], |
| 83 ["~example.com#@#foo:-abp-properties(something)bar", ElemHideException, "ele
mhideexception"], | 86 ["~example.com#@#foo:-abp-properties(something)bar", ElemHideException, "ele
mhideexception"], |
| 84 ["~example.com,~example.info#?#foo:-abp-properties(something)bar", InvalidFi
lter, "invalid"], | 87 ["~example.com,~example.info#?#foo:-abp-properties(something)bar", InvalidFi
lter, "invalid"], |
| 85 ["~example.com,~example.info#@#foo:-abp-properties(something)bar", ElemHideE
xception, "elemhideexception"], | 88 ["~example.com,~example.info#@#foo:-abp-properties(something)bar", ElemHideE
xception, "elemhideexception"], |
| 86 ["~sub.example.com,example.com#?#foo:-abp-properties(something)bar", ElemHid
eEmulationFilter, "elemhideemulation"], | 89 ["~sub.example.com,example.com#?#foo:-abp-properties(something)bar", ElemHid
eEmulationFilter, "elemhideemulation"], |
| 87 ["~sub.example.com,example.com#@#foo:-abp-properties(something)bar", ElemHid
eException, "elemhideexception"], | 90 ["~sub.example.com,example.com#@#foo:-abp-properties(something)bar", ElemHid
eException, "elemhideexception"], |
| 88 ["example.com,~sub.example.com#?#foo:-abp-properties(something)bar", ElemHid
eEmulationFilter, "elemhideemulation"], | 91 ["example.com,~sub.example.com#?#foo:-abp-properties(something)bar", ElemHid
eEmulationFilter, "elemhideemulation"], |
| 89 ["example.com,~sub.example.com#@#foo:-abp-properties(something)bar", ElemHid
eException, "elemhideexception"], | 92 ["example.com,~sub.example.com#@#foo:-abp-properties(something)bar", ElemHid
eException, "elemhideexception"], |
| 90 ["example.com#?#:-abp-properties(something)", ElemHideEmulationFilter, "elem
hideemulation"], | 93 ["example.com#?#:-abp-properties(something)", ElemHideEmulationFilter, "elem
hideemulation"], |
| 91 ["example.com#@#:-abp-properties(something)", ElemHideException, "elemhideex
ception"], | 94 ["example.com#@#:-abp-properties(something)", ElemHideException, "elemhideex
ception"], |
| 92 ["example.com#?#:-abp-properties((something))", ElemHideEmulationFilter, "el
emhideemulation"], | 95 ["example.com#?#:-abp-properties((something))", ElemHideEmulationFilter, "el
emhideemulation"], |
| 93 ["example.com#@#:-abp-properties((something))", ElemHideException, "elemhide
exception"] | 96 ["example.com#@#:-abp-properties((something))", ElemHideException, "elemhide
exception"] |
| 94 ]; | 97 ]; |
| 95 for (let [text, type, typeName] of tests) | 98 for (let [text, type, typeName, collapse] of tests) |
| 96 { | 99 { |
| 97 let filter = Filter.fromText(text); | 100 withNAD(0, filter => |
| 98 test.ok(filter instanceof Filter, "Got filter for " + text); | 101 { |
| 99 test.equal(filter.text, text, "Correct filter text for " + text); | 102 test.ok(filter instanceof Filter, "Got filter for " + text); |
| 100 test.ok(filter instanceof type, "Correct filter type for " + text); | 103 test.equal(filter.text, text, "Correct filter text for " + text); |
| 101 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeNam
e); | 104 test.ok(filter instanceof type, "Correct filter type for " + text); |
| 102 if (type == InvalidFilter) | 105 test.equal(filter.type, typeName, "Type name for " + text + " is " + typeN
ame); |
| 103 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); | 106 if (filter instanceof BlockingFilter) |
| 104 filter.delete(); | 107 test.equal(filter.collapse, collapse); |
| 105 } | 108 else |
| 109 test.equal(filter.collapse, undefined); |
| 110 if (type == InvalidFilter) |
| 111 test.ok(filter.reason, "Invalid filter " + text + " has a reason set"); |
| 112 })(Filter.fromText(text)); |
| 113 } |
| 114 |
| 115 // conversion from old syntax. |
| 116 withNAD(0, filter => |
| 117 { |
| 118 test.equal(filter.text, "example.com#?#:-abp-properties(something)"); |
| 119 test.ok(filter instanceof ElemHideEmulationFilter); |
| 120 test.equal(filter.type, "elemhideemulation"); |
| 121 })(Filter.fromText("example.com##[-abp-properties='something']")); |
| 122 |
| 106 test.done(); | 123 test.done(); |
| 107 }; | 124 }; |
| 108 | 125 |
| 109 exports.testClassHierarchy = function(test) | 126 exports.testClassHierarchy = function(test) |
| 110 { | 127 { |
| 111 let allClasses = [ | 128 let allClasses = [ |
| 112 Filter, InvalidFilter, CommentFilter, ActiveFilter, | 129 Filter, InvalidFilter, CommentFilter, ActiveFilter, |
| 113 RegExpFilter, BlockingFilter, WhitelistFilter, ElemHideBase, | 130 RegExpFilter, BlockingFilter, WhitelistFilter, ElemHideBase, |
| 114 ElemHideFilter, ElemHideException, ElemHideEmulationFilter | 131 ElemHideFilter, ElemHideException, ElemHideEmulationFilter |
| 115 ]; | 132 ]; |
| (...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 } | 243 } |
| 227 | 244 |
| 228 test.done(); | 245 test.done(); |
| 229 }; | 246 }; |
| 230 | 247 |
| 231 exports.testSerialize = function(test) | 248 exports.testSerialize = function(test) |
| 232 { | 249 { |
| 233 // Comment | 250 // Comment |
| 234 let filter = Filter.fromText("! serialize"); | 251 let filter = Filter.fromText("! serialize"); |
| 235 test.equal(filter.serialize(), "[Filter]\ntext=! serialize\n"); | 252 test.equal(filter.serialize(), "[Filter]\ntext=! serialize\n"); |
| 253 test.equal(filter.collapse, undefined); |
| 236 filter.delete(); | 254 filter.delete(); |
| 237 | 255 |
| 238 // Blocking filter | 256 // Blocking filter |
| 239 filter = Filter.fromText("serialize"); | 257 filter = Filter.fromText("serialize"); |
| 240 test.equal(filter.serialize(), "[Filter]\ntext=serialize\n"); | 258 test.equal(filter.serialize(), "[Filter]\ntext=serialize\n"); |
| 241 filter.disabled = true; | 259 filter.disabled = true; |
| 242 test.equal(filter.serialize(), "[Filter]\ntext=serialize\ndisabled=true\n"); | 260 test.equal(filter.serialize(), "[Filter]\ntext=serialize\ndisabled=true\n"); |
| 243 filter.disabled = false; | 261 filter.disabled = false; |
| 244 filter.hitCount = 10; | 262 filter.hitCount = 10; |
| 245 filter.lastHit = 12; | 263 filter.lastHit = 12; |
| 246 test.equal(filter.serialize(), "[Filter]\ntext=serialize\nhitCount=10\nlastHit
=12\n"); | 264 test.equal(filter.serialize(), "[Filter]\ntext=serialize\nhitCount=10\nlastHit
=12\n"); |
| 265 test.ok(filter.collapse); |
| 247 filter.delete(); | 266 filter.delete(); |
| 248 | 267 |
| 249 // Invalid filter | 268 // Invalid filter |
| 250 filter = Filter.fromText("serialize$foobar"); | 269 filter = Filter.fromText("serialize$foobar"); |
| 251 test.equal(filter.serialize(), "[Filter]\ntext=serialize$foobar\n"); | 270 test.equal(filter.serialize(), "[Filter]\ntext=serialize$foobar\n"); |
| 252 filter.delete(); | 271 filter.delete(); |
| 253 | 272 |
| 254 // Element hiding filter | 273 // Element hiding filter |
| 255 filter = Filter.fromText("example.com##serialize"); | 274 filter = Filter.fromText("example.com##serialize"); |
| 256 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n"); | 275 test.equal(filter.serialize(), "[Filter]\ntext=example.com##serialize\n"); |
| (...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 | 389 |
| 371 for (let [text, selector, selectorDomain] of tests) | 390 for (let [text, selector, selectorDomain] of tests) |
| 372 { | 391 { |
| 373 doTest(text, selector, selectorDomain); | 392 doTest(text, selector, selectorDomain); |
| 374 doTest(text.replace("##", "#@#"), selector, selectorDomain); | 393 doTest(text.replace("##", "#@#"), selector, selectorDomain); |
| 375 } | 394 } |
| 376 | 395 |
| 377 test.done(); | 396 test.done(); |
| 378 }; | 397 }; |
| 379 | 398 |
| 399 exports.testElemHideRulesWithBraces = function(test) |
| 400 { |
| 401 withNAD(0, filter => |
| 402 { |
| 403 test.equal(filter.type, "elemhide"); |
| 404 test.equal(filter.selector, "#foo\\7B color: red\\7D "); |
| 405 })(Filter.fromText("###foo{color: red}")); |
| 406 |
| 407 // Filter conversion to the new syntax dealing with braces too. |
| 408 withNAD(0, filter => |
| 409 { |
| 410 test.equal(filter.type, "elemhideemulation"); |
| 411 test.equal(filter.selector, ":-abp-properties(/margin: [3-4]\\7B 2\\7D /)"); |
| 412 })(Filter.fromText("foo.com##[-abp-properties='/margin: [3-4]{2}/']")); |
| 413 |
| 414 test.done(); |
| 415 }; |
| 416 |
| 380 exports.testNotifications = function(test) | 417 exports.testNotifications = function(test) |
| 381 { | 418 { |
| 382 function checkNotifications(action, expected, message) | 419 function checkNotifications(action, expected, message) |
| 383 { | 420 { |
| 384 let result = null; | 421 let result = null; |
| 385 let listener = (topic, filter) => | 422 let listener = (topic, filter) => |
| 386 { | 423 { |
| 387 if (result) | 424 if (result) |
| 388 test.ok(false, "Got more that one notification - " + message); | 425 test.ok(false, "Got more that one notification - " + message); |
| 389 else | 426 else |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 filter.hitCount++; | 464 filter.hitCount++; |
| 428 }, ["filter.hitCount", "foobar"], "Increasing filter hit counts"); | 465 }, ["filter.hitCount", "foobar"], "Increasing filter hit counts"); |
| 429 checkNotifications(() => | 466 checkNotifications(() => |
| 430 { | 467 { |
| 431 filter.hitCount = 0; | 468 filter.hitCount = 0; |
| 432 }, ["filter.hitCount", "foobar"], "Resetting filter hit counts"); | 469 }, ["filter.hitCount", "foobar"], "Resetting filter hit counts"); |
| 433 | 470 |
| 434 filter.delete(); | 471 filter.delete(); |
| 435 test.done(); | 472 test.done(); |
| 436 }; | 473 }; |
| LEFT | RIGHT |