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 testSelectorMatches(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 .map(filter => filter.text); | |
54 test.deepEqual(matches.sort(), expectedMatches.sort(), description); | 44 test.deepEqual(matches.sort(), expectedMatches.sort(), description); |
55 | 45 |
56 Snippets.clear(); | 46 Snippets.clear(); |
57 ElemHide.clear(); | |
58 } | 47 } |
59 | 48 |
60 testSelectorMatches( | 49 testScriptMatches( |
61 "Ignore selectors with exceptions", | |
62 [ | |
63 "example.com#$#foo", | |
64 "example.com#$#bar", | |
65 "example.com#@#foo" | |
66 ], | |
67 "example.com", | |
68 ["example.com#$#bar"] | |
69 ); | |
70 testSelectorMatches( | |
71 "Ignore filters that include parent domain but exclude subdomain", | 50 "Ignore filters that include parent domain but exclude subdomain", |
72 [ | 51 [ |
73 "~www.example.com,example.com#$#foo" | 52 "~www.example.com,example.com#$#foo" |
74 ], | 53 ], |
75 "www.example.com", | 54 "www.example.com", |
76 [] | 55 [] |
77 ); | 56 ); |
78 testSelectorMatches( | 57 testScriptMatches( |
79 "Ignore filters with parent domain if exception matches subdomain", | |
80 [ | |
81 "www.example.com#@#foo", | |
82 "example.com#$#foo" | |
83 ], | |
84 "www.example.com", | |
85 [] | |
86 ); | |
87 testSelectorMatches( | |
88 "Ignore filters for other subdomain", | 58 "Ignore filters for other subdomain", |
89 [ | 59 [ |
90 "www.example.com#$#foo", | 60 "www.example.com#$#foo", |
91 "other.example.com#$#foo" | 61 "other.example.com#$#foo" |
92 ], | 62 ], |
93 "other.example.com", | 63 "other.example.com", |
94 ["other.example.com#$#foo"] | 64 ["foo"] |
95 ); | 65 ); |
96 | 66 |
97 test.done(); | 67 test.done(); |
98 }; | 68 }; |
99 | 69 |
100 exports.testSnippetFiltersContainer = function(test) | 70 exports.testSnippetFiltersContainer = function(test) |
101 { | 71 { |
102 function compareRules(description, domain, expectedMatches) | 72 function compareRules(description, domain, expectedMatches) |
103 { | 73 { |
104 let result = Snippets.getScriptsForDomain(domain) | 74 let result = Snippets.getScriptsForDomain(domain); |
105 .map(filter => filter.text); | 75 expectedMatches = expectedMatches.map(filter => filter.script); |
106 expectedMatches = expectedMatches.map(filter => filter.text); | |
107 test.deepEqual(result.sort(), expectedMatches.sort(), description); | 76 test.deepEqual(result.sort(), expectedMatches.sort(), description); |
108 } | 77 } |
109 | 78 |
110 let domainFilter = Filter.fromText("example.com#$#filter1"); | 79 let domainFilter = Filter.fromText("example.com#$#filter1"); |
111 let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); | 80 let subdomainFilter = Filter.fromText("www.example.com#$#filter2"); |
112 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); | 81 let otherDomainFilter = Filter.fromText("other.example.com#$#filter3"); |
113 | 82 |
114 Snippets.add(domainFilter); | 83 Snippets.add(domainFilter); |
115 Snippets.add(subdomainFilter); | 84 Snippets.add(subdomainFilter); |
116 Snippets.add(otherDomainFilter); | 85 Snippets.add(otherDomainFilter); |
(...skipping 12 matching lines...) Expand all Loading... |
129 | 98 |
130 Snippets.clear(); | 99 Snippets.clear(); |
131 compareRules( | 100 compareRules( |
132 "Return no filters after clearing", | 101 "Return no filters after clearing", |
133 "www.example.com", | 102 "www.example.com", |
134 [] | 103 [] |
135 ); | 104 ); |
136 | 105 |
137 test.done(); | 106 test.done(); |
138 }; | 107 }; |
LEFT | RIGHT |