| OLD | NEW | 
| (Empty) |  | 
 |    1 /* | 
 |    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"; | 
 |   19  | 
 |   20 let Filter = require("filterClasses").Filter; | 
 |   21 let ContentBlockerList = require("../lib/abp2blocklist.js").ContentBlockerList; | 
 |   22  | 
 |   23 function testRules(test, filters, expected, transformFunction) | 
 |   24 { | 
 |   25   let blockerList = new ContentBlockerList(); | 
 |   26   for (let filter of filters) | 
 |   27     blockerList.addFilter(Filter.fromText(filter)); | 
 |   28  | 
 |   29   let rules = blockerList.generateRules(); | 
 |   30   if (transformFunction) | 
 |   31     rules = transformFunction(rules); | 
 |   32  | 
 |   33   test.deepEqual(rules, expected); | 
 |   34 } | 
 |   35  | 
 |   36 exports.generateRules = { | 
 |   37   testElementHiding: function(test) | 
 |   38   { | 
 |   39     testRules(test, ["##.whatever"], [ | 
 |   40       {trigger: {"url-filter": "^https?://", | 
 |   41                  "url-filter-is-case-sensitive": true}, | 
 |   42        action: {type: "css-display-none", selector: ".whatever"}} | 
 |   43     ]); | 
 |   44     testRules(test, ["test.com##.whatever"], [ | 
 |   45       {trigger: {"url-filter": "^https?://([^/:]*\\.)?test\\.com[/:]", | 
 |   46                  "url-filter-is-case-sensitive": true}, | 
 |   47        action: {type: "css-display-none", selector: ".whatever"}} | 
 |   48     ]); | 
 |   49  | 
 |   50     test.done(); | 
 |   51   }, | 
 |   52  | 
 |   53   testElementHidingExceptions: function(test) | 
 |   54   { | 
 |   55     testRules(test, ["#@#whatever"], []); | 
 |   56     testRules(test, ["test.com#@#whatever"], []); | 
 |   57     testRules(test, ["~test.com#@#whatever"], []); | 
 |   58  | 
 |   59     // We currently completely ignore any element hiding filters that have the | 
 |   60     // same selector as an element hiding exception. In these examples #whatever | 
 |   61     // should be hidden for all domains not ending in test.com instead of | 
 |   62     // nowhere! | 
 |   63     testRules(test, ["test.com#@#whatever", "##whatever"], []); | 
 |   64     testRules(test, ["~test.com##whatever"], []); | 
 |   65  | 
 |   66     test.done(); | 
 |   67   }, | 
 |   68  | 
 |   69   testRequestFilters: function(test) | 
 |   70   { | 
 |   71     testRules(test, ["/foo", "||test.com", "http://example.com/foo"], [ | 
 |   72       {trigger: {"url-filter": "^https?://.*/foo", | 
 |   73                  "resource-type": ["image", "style-sheet", "script", "font", | 
 |   74                                    "media", "raw", "document"]}, | 
 |   75        action: {type: "block"}}, | 
 |   76       {trigger: {"url-filter": "^https?://test\\.com", | 
 |   77                  "url-filter-is-case-sensitive": true, | 
 |   78                  "resource-type": ["image", "style-sheet", "script", "font", | 
 |   79                                    "media", "raw", "document"]}, | 
 |   80        action: {type: "block"}}, | 
 |   81       {trigger: {"url-filter": "http://example\\.com/foo", | 
 |   82                  "resource-type": ["image", "style-sheet", "script", "font", | 
 |   83                                    "media", "raw", "document"]}, | 
 |   84        action: {type: "block"}} | 
 |   85     ]); | 
 |   86  | 
 |   87     test.done(); | 
 |   88   }, | 
 |   89  | 
 |   90   testRequestFilterExceptions: function(test) | 
 |   91   { | 
 |   92     testRules(test, ["@@example.com"], [ | 
 |   93       {trigger: {"url-filter": "^https?://.*example\\.com", | 
 |   94                  "resource-type": ["image", "style-sheet", "script", "font", | 
 |   95                                    "media", "raw", "document"]}, | 
 |   96        action: {type: "ignore-previous-rules"}} | 
 |   97     ]); | 
 |   98  | 
 |   99     test.done(); | 
 |  100   }, | 
 |  101  | 
 |  102   testElementIDattributeFormat: function(test) | 
 |  103   { | 
 |  104     testRules(test, | 
 |  105               ["###example", "test.com###EXAMPLE"], | 
 |  106               ["[id=example]", "[id=EXAMPLE]"], | 
 |  107               rules => rules.map(rule => rule.action.selector)); | 
 |  108  | 
 |  109     test.done(); | 
 |  110   }, | 
 |  111  | 
 |  112   testDomainWhitelisting: function(test) | 
 |  113   { | 
 |  114     testRules(test, ["@@||example.com^$document"], [ | 
 |  115       {trigger: {"url-filter": ".*", | 
 |  116                  "if-domain": ["example.com", "www.example.com"]}, | 
 |  117        action: {type: "ignore-previous-rules"}} | 
 |  118     ]); | 
 |  119     testRules(test, ["@@||example.com^$document,image"], [ | 
 |  120       {trigger: {"url-filter": ".*", | 
 |  121                  "if-domain": ["example.com", "www.example.com"]}, | 
 |  122        action: {type: "ignore-previous-rules"}}, | 
 |  123       {trigger: {"url-filter": "^https?://example\\.com", | 
 |  124                  "url-filter-is-case-sensitive": true, | 
 |  125                  "resource-type": ["image"]}, | 
 |  126        action: {type: "ignore-previous-rules"}} | 
 |  127     ]); | 
 |  128     testRules(test, ["@@||example.com/path^$font,document"], [ | 
 |  129       {trigger: {"url-filter": "^https?://example\\.com/path", | 
 |  130                  "resource-type": ["font"]}, | 
 |  131        action: {type: "ignore-previous-rules"}} | 
 |  132     ]); | 
 |  133  | 
 |  134     test.done(); | 
 |  135   }, | 
 |  136  | 
 |  137   testRuleOrdering: function(test) | 
 |  138   { | 
 |  139     testRules( | 
 |  140       test, | 
 |  141       ["/ads.jpg", "@@example.com", "test.com#@#foo", "##bar"], | 
 |  142       ["css-display-none", "block", "ignore-previous-rules"], | 
 |  143       rules => rules.map(rule => rule.action.type) | 
 |  144     ); | 
 |  145     testRules( | 
 |  146       test, | 
 |  147       ["@@example.com", "##bar", "/ads.jpg", "test.com#@#foo"], | 
 |  148       ["css-display-none", "block", "ignore-previous-rules"], | 
 |  149       rules => rules.map(rule => rule.action.type) | 
 |  150     ); | 
 |  151  | 
 |  152     test.done(); | 
 |  153   }, | 
 |  154  | 
 |  155   testRequestTypeMapping: function(test) | 
 |  156   { | 
 |  157     testRules( | 
 |  158       test, | 
 |  159       ["1", "2$image", "3$stylesheet", "4$script", "5$font", "6$media", | 
 |  160        "7$popup", "8$object", "9$object_subrequest", "10$xmlhttprequest", | 
 |  161        "11$ping", "12$subdocument", "13$other", "14$IMAGE", "15$document", | 
 |  162        "16$script,PING,Popup", "17$~image"], | 
 |  163       [["image", "style-sheet", "script", "font", "media", "raw", "document" ], | 
 |  164        ["image"], | 
 |  165        ["style-sheet"], | 
 |  166        ["script"], | 
 |  167        ["font"], | 
 |  168        ["media"], | 
 |  169        ["popup"], | 
 |  170        ["media"], | 
 |  171        ["raw"], | 
 |  172        ["raw"], | 
 |  173        ["raw"], | 
 |  174        ["document"], | 
 |  175        ["raw"], | 
 |  176        ["image"], | 
 |  177        [], | 
 |  178        ["script", "popup", "raw" ], | 
 |  179        ["style-sheet", "script", "font", "media", "raw", "document"]], | 
 |  180       rules => rules.map(rule => rule.trigger["resource-type"]) | 
 |  181     ); | 
 |  182  | 
 |  183     test.done(); | 
 |  184   }, | 
 |  185  | 
 |  186   testUnsupportedfilters: function(test) | 
 |  187   { | 
 |  188     // These types of filters are currently completely unsupported. | 
 |  189     testRules(test, ["foo$sitekey=bar", "@@foo$genericblock", | 
 |  190                      "@@bar$generichide"], []); | 
 |  191  | 
 |  192     test.done(); | 
 |  193   }, | 
 |  194  | 
 |  195   testFilterOptions: function(test) | 
 |  196   { | 
 |  197     testRules(test, ["1$domain=foo.com"], ["foo.com", "www.foo.com"], | 
 |  198               rules => rules[0]["trigger"]["if-domain"]); | 
 |  199     testRules(test, ["2$domain=third-party"], ["third-party"], | 
 |  200               rules => rules[0]["trigger"]["if-domain"]); | 
 |  201     testRules(test, ["foo$match_case"], true, | 
 |  202               rules => rules[0]["trigger"]["url-filter-is-case-sensitive"]); | 
 |  203  | 
 |  204     test.done(); | 
 |  205   }, | 
 |  206  | 
 |  207   testUnicode: function(test) | 
 |  208   { | 
 |  209     testRules(test, ["$domain=🐈.cat"], ["xn--zn8h.cat", "www.xn--zn8h.cat"], | 
 |  210               rules => rules[0]["trigger"]["if-domain"]); | 
 |  211     testRules(test, ["🐈$domain=🐈.cat"], []); | 
 |  212     testRules(test, ["###🐈"], []); | 
 |  213  | 
 |  214     test.done(); | 
 |  215   } | 
 |  216 }; | 
| OLD | NEW |