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 const {createSandbox} = require("./_common"); | 20 const {createSandbox} = require("./_common"); |
21 | 21 |
22 let SnippetFilter = null; | |
23 let Snippets = null; | 22 let Snippets = null; |
24 let ElemHide = null; | |
25 let Filter = null; | 23 let Filter = null; |
26 | 24 |
27 exports.setUp = function(callback) | 25 exports.setUp = function(callback) |
28 { | 26 { |
29 let sandboxedRequire = createSandbox(); | 27 let sandboxedRequire = createSandbox(); |
30 ( | 28 ( |
31 {Filter, SnippetFilter} = sandboxedRequire("../lib/filterClasses"), | 29 {Filter} = sandboxedRequire("../lib/filterClasses"), |
32 {ElemHide} = sandboxedRequire("../lib/elemHide"), | |
33 {Snippets} = sandboxedRequire("../lib/snippets") | 30 {Snippets} = sandboxedRequire("../lib/snippets") |
34 ); | 31 ); |
35 | 32 |
36 callback(); | 33 callback(); |
37 }; | 34 }; |
38 | 35 |
39 exports.testDomainRestrictions = function(test) | 36 exports.testDomainRestrictions = function(test) |
40 { | 37 { |
41 function testScriptMatches(description, filters, domain, expectedMatches) | 38 function testScriptMatches(description, filters, domain, expectedMatches) |
42 { | 39 { |
43 for (let filter of filters) | 40 for (let filter of filters) |
44 { | 41 Snippets.add(Filter.fromText(filter)); |
45 filter = Filter.fromText(filter); | |
46 if (filter instanceof SnippetFilter) | |
47 Snippets.add(filter); | |
48 else | |
49 ElemHide.add(filter); | |
50 } | |
51 | 42 |
52 let matches = Snippets.getScriptsForDomain(domain); | 43 let matches = Snippets.getScriptsForDomain(domain); |
53 test.deepEqual(matches.sort(), expectedMatches.sort(), description); | 44 test.deepEqual(matches.sort(), expectedMatches.sort(), description); |
54 | 45 |
55 Snippets.clear(); | 46 Snippets.clear(); |
56 ElemHide.clear(); | |
57 } | 47 } |
58 | 48 |
59 testScriptMatches( | |
60 "Ignore scripts with exceptions", | |
61 [ | |
62 "example.com#$#foo", | |
63 "example.com#$#bar", | |
64 "example.com#@#foo" | |
65 ], | |
66 "example.com", | |
67 ["bar"] | |
68 ); | |
69 testScriptMatches( | 49 testScriptMatches( |
70 "Ignore filters that include parent domain but exclude subdomain", | 50 "Ignore filters that include parent domain but exclude subdomain", |
71 [ | 51 [ |
72 "~www.example.com,example.com#$#foo" | 52 "~www.example.com,example.com#$#foo" |
73 ], | |
74 "www.example.com", | |
75 [] | |
76 ); | |
77 testScriptMatches( | |
78 "Ignore filters with parent domain if exception matches subdomain", | |
79 [ | |
80 "www.example.com#@#foo", | |
81 "example.com#$#foo" | |
82 ], | 53 ], |
83 "www.example.com", | 54 "www.example.com", |
84 [] | 55 [] |
85 ); | 56 ); |
86 testScriptMatches( | 57 testScriptMatches( |
87 "Ignore filters for other subdomain", | 58 "Ignore filters for other subdomain", |
88 [ | 59 [ |
89 "www.example.com#$#foo", | 60 "www.example.com#$#foo", |
90 "other.example.com#$#foo" | 61 "other.example.com#$#foo" |
91 ], | 62 ], |
92 "other.example.com", | 63 "other.example.com", |
93 ["foo"] | 64 ["foo"] |
94 ); | 65 ); |
95 | 66 |
96 test.done(); | 67 test.done(); |
97 }; | 68 }; |
98 | 69 |
99 exports.testSnippetFiltersContainer = function(test) | 70 exports.testSnippetFiltersContainer = function(test) |
100 { | 71 { |
101 function compareRules(description, domain, expectedMatches) | 72 function compareRules(description, domain, expectedMatches) |
102 { | 73 { |
103 let result = Snippets.getScriptsForDomain(domain); | 74 let result = Snippets.getScriptsForDomain(domain); |
104 expectedMatches = expectedMatches.map(filter => filter.code); | 75 expectedMatches = expectedMatches.map(filter => filter.script); |
105 test.deepEqual(result.sort(), expectedMatches.sort(), description); | 76 test.deepEqual(result.sort(), expectedMatches.sort(), description); |
106 } | 77 } |
107 | 78 |
108 let domainFilter = Filter.fromText("example.com#$#filter1"); | 79 let domainFilter = Filter.fromText("example.com#$#filter1"); |
109 let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); | 80 let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); |
110 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); | 81 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); |
111 | 82 |
112 Snippets.add(domainFilter); | 83 Snippets.add(domainFilter); |
113 Snippets.add(subdomainFilter); | 84 Snippets.add(subdomainFilter); |
114 Snippets.add(otherDomainFilter); | 85 Snippets.add(otherDomainFilter); |
(...skipping 12 matching lines...) Expand all Loading... |
127 | 98 |
128 Snippets.clear(); | 99 Snippets.clear(); |
129 compareRules( | 100 compareRules( |
130 "Return no filters after clearing", | 101 "Return no filters after clearing", |
131 "www.example.com", | 102 "www.example.com", |
132 [] | 103 [] |
133 ); | 104 ); |
134 | 105 |
135 test.done(); | 106 test.done(); |
136 }; | 107 }; |
LEFT | RIGHT |