| 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 /* eslint no-new-func: "off" */ | 18 /* eslint no-new-func: "off" */ | 
| 19 | 19 | 
| 20 "use strict"; | 20 "use strict"; | 
| 21 | 21 | 
| 22 const assert = require("assert"); | 22 const assert = require("assert"); | 
| 23 const {createSandbox} = require("./_common"); | 23 const {createSandbox} = require("./_common"); | 
| 24 | 24 | 
| 25 let Snippets = null; | 25 let snippets = null; | 
| 26 let parseScript = null; | 26 let parseScript = null; | 
| 27 let compileScript = null; | 27 let compileScript = null; | 
| 28 let Filter = null; | 28 let Filter = null; | 
| 29 let SnippetFilter = null; | 29 let SnippetFilter = null; | 
| 30 | 30 | 
| 31 describe("Snippets", () => | 31 describe("Snippets", () => | 
| 32 { | 32 { | 
| 33   beforeEach(() => | 33   beforeEach(() => | 
| 34   { | 34   { | 
| 35     let sandboxedRequire = createSandbox(); | 35     let sandboxedRequire = createSandbox(); | 
| 36     ( | 36     ( | 
| 37       {Filter, SnippetFilter} = sandboxedRequire("../lib/filterClasses"), | 37       {Filter, SnippetFilter} = sandboxedRequire("../lib/filterClasses"), | 
| 38       {Snippets, parseScript, compileScript} = sandboxedRequire("../lib/snippets
     ") | 38       {snippets, parseScript, compileScript} = sandboxedRequire("../lib/snippets
     ") | 
| 39     ); | 39     ); | 
| 40   }); | 40   }); | 
| 41 | 41 | 
| 42   it("Domain Restrictions", () => | 42   it("Domain Restrictions", () => | 
| 43   { | 43   { | 
| 44     function testScriptMatches(description, filters, domain, expectedMatches) | 44     function testScriptMatches(description, filters, domain, expectedMatches) | 
| 45     { | 45     { | 
| 46       for (let filter of filters.map(Filter.fromText)) | 46       for (let filter of filters.map(Filter.fromText)) | 
| 47       { | 47       { | 
| 48         if (filter instanceof SnippetFilter) | 48         if (filter instanceof SnippetFilter) | 
| 49           Snippets.add(filter); | 49           snippets.add(filter); | 
| 50       } | 50       } | 
| 51 | 51 | 
| 52       let matches = Snippets.getFiltersForDomain(domain).map( | 52       let matches = snippets.getFiltersForDomain(domain).map( | 
| 53         filter => filter.script | 53         filter => filter.script | 
| 54       ); | 54       ); | 
| 55       assert.deepEqual(matches.sort(), expectedMatches.sort(), description); | 55       assert.deepEqual(matches.sort(), expectedMatches.sort(), description); | 
| 56 | 56 | 
| 57       Snippets.clear(); | 57       snippets.clear(); | 
| 58     } | 58     } | 
| 59 | 59 | 
| 60     testScriptMatches( | 60     testScriptMatches( | 
| 61       "Ignore generic filters", | 61       "Ignore generic filters", | 
| 62       [ | 62       [ | 
| 63         "#$#foo-1", "example.com#$#foo-2", | 63         "#$#foo-1", "example.com#$#foo-2", | 
| 64         "~example.com#$#foo-3" | 64         "~example.com#$#foo-3" | 
| 65       ], | 65       ], | 
| 66       "example.com", | 66       "example.com", | 
| 67       ["foo-2"] | 67       ["foo-2"] | 
| (...skipping 21 matching lines...) Expand all  Loading... | 
| 89   { | 89   { | 
| 90     let events = []; | 90     let events = []; | 
| 91 | 91 | 
| 92     function eventHandler(...args) | 92     function eventHandler(...args) | 
| 93     { | 93     { | 
| 94       events.push([...args]); | 94       events.push([...args]); | 
| 95     } | 95     } | 
| 96 | 96 | 
| 97     function compareRules(description, domain, expectedMatches) | 97     function compareRules(description, domain, expectedMatches) | 
| 98     { | 98     { | 
| 99       let result = Snippets.getFiltersForDomain(domain); | 99       let result = snippets.getFiltersForDomain(domain); | 
| 100       assert.deepEqual(result.sort(), expectedMatches.sort(), description); | 100       assert.deepEqual(result.sort(), expectedMatches.sort(), description); | 
| 101     } | 101     } | 
| 102 | 102 | 
| 103     Snippets.on("snippets.filterAdded", | 103     snippets.on("snippets.filterAdded", | 
| 104                 eventHandler.bind(null, "snippets.filterAdded")); | 104                 eventHandler.bind(null, "snippets.filterAdded")); | 
| 105     Snippets.on("snippets.filterRemoved", | 105     snippets.on("snippets.filterRemoved", | 
| 106                 eventHandler.bind(null, "snippets.filterRemoved")); | 106                 eventHandler.bind(null, "snippets.filterRemoved")); | 
| 107     Snippets.on("snippets.filtersCleared", | 107     snippets.on("snippets.filtersCleared", | 
| 108                 eventHandler.bind(null, "snippets.filtersCleared")); | 108                 eventHandler.bind(null, "snippets.filtersCleared")); | 
| 109 | 109 | 
| 110     let domainFilter = Filter.fromText("example.com#$#filter1"); | 110     let domainFilter = Filter.fromText("example.com#$#filter1"); | 
| 111     let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); | 111     let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); | 
| 112     let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); | 112     let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); | 
| 113 | 113 | 
| 114     Snippets.add(domainFilter); | 114     snippets.add(domainFilter); | 
| 115     Snippets.add(subdomainFilter); | 115     snippets.add(subdomainFilter); | 
| 116     Snippets.add(otherDomainFilter); | 116     snippets.add(otherDomainFilter); | 
| 117     compareRules( | 117     compareRules( | 
| 118       "Return all matching filters", | 118       "Return all matching filters", | 
| 119       "www.example.com", | 119       "www.example.com", | 
| 120       [domainFilter, subdomainFilter] | 120       [domainFilter, subdomainFilter] | 
| 121     ); | 121     ); | 
| 122 | 122 | 
| 123     Snippets.remove(domainFilter); | 123     snippets.remove(domainFilter); | 
| 124     compareRules( | 124     compareRules( | 
| 125       "Return all matching filters after removing one", | 125       "Return all matching filters after removing one", | 
| 126       "www.example.com", | 126       "www.example.com", | 
| 127       [subdomainFilter] | 127       [subdomainFilter] | 
| 128     ); | 128     ); | 
| 129 | 129 | 
| 130     Snippets.clear(); | 130     snippets.clear(); | 
| 131     compareRules( | 131     compareRules( | 
| 132       "Return no filters after clearing", | 132       "Return no filters after clearing", | 
| 133       "www.example.com", | 133       "www.example.com", | 
| 134       [] | 134       [] | 
| 135     ); | 135     ); | 
| 136 | 136 | 
| 137     assert.deepEqual(events, [ | 137     assert.deepEqual(events, [ | 
| 138       ["snippets.filterAdded", domainFilter], | 138       ["snippets.filterAdded", domainFilter], | 
| 139       ["snippets.filterAdded", subdomainFilter], | 139       ["snippets.filterAdded", subdomainFilter], | 
| 140       ["snippets.filterAdded", otherDomainFilter], | 140       ["snippets.filterAdded", otherDomainFilter], | 
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 299     // (2) script execution is stateless, i.e. the values are not retained | 299     // (2) script execution is stateless, i.e. the values are not retained | 
| 300     // between executions. In the example below, assertFoo does not find 456 but | 300     // between executions. In the example below, assertFoo does not find 456 but | 
| 301     // it doesn't find 123 either. It's the initial value 0. | 301     // it doesn't find 123 either. It's the initial value 0. | 
| 302     new Function( | 302     new Function( | 
| 303       compileScript("setFoo 456; assertFoo 0", [ | 303       compileScript("setFoo 456; assertFoo 0", [ | 
| 304         ...libraries, "let foo = 1; exports.setFoo = value => { foo = value; };" | 304         ...libraries, "let foo = 1; exports.setFoo = value => { foo = value; };" | 
| 305       ]) | 305       ]) | 
| 306     )(); | 306     )(); | 
| 307   }); | 307   }); | 
| 308 }); | 308 }); | 
| LEFT | RIGHT | 
|---|