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 const {withNAD} = require("./_test-utils"); |
21 | 22 |
22 let ElemHideEmulationFilter = null; | 23 let ElemHideEmulationFilter = null; |
23 let ElemHideEmulation = null; | 24 let ElemHideEmulation = null; |
24 let ElemHide = null; | 25 let ElemHide = null; |
25 let Filter = null; | 26 let Filter = null; |
26 | 27 |
27 exports.setUp = function(callback) | 28 exports.setUp = function(callback) |
28 { | 29 { |
29 let sandboxedRequire = createSandbox(); | 30 let sandboxedRequire = createSandbox(); |
30 ( | 31 ( |
31 {Filter, | 32 {Filter, |
32 ElemHideEmulationFilter} = sandboxedRequire("../lib/filterClasses"), | 33 ElemHideEmulationFilter} = sandboxedRequire("../lib/filterClasses"), |
33 {ElemHideEmulation} = sandboxedRequire("../lib/elemHideEmulation"), | 34 {ElemHideEmulation} = sandboxedRequire("../lib/elemHideEmulation"), |
34 {ElemHide} = sandboxedRequire("../lib/elemHide") | 35 {ElemHide} = sandboxedRequire("../lib/elemHide") |
35 ); | 36 ); |
36 | 37 |
37 callback(); | 38 callback(); |
38 }; | 39 }; |
39 | 40 |
40 exports.testElemHideAPI = function(test) | 41 exports.testElemHideAPI = function(test) |
41 { | 42 { |
42 let elemHide = ElemHide.create(); | 43 withNAD(0, elemHide => |
43 | |
44 { | 44 { |
45 let filter = Filter.fromText("###ads"); | 45 withNAD(0, filter => |
46 elemHide.add(filter); | 46 { |
47 | 47 elemHide.add(filter); |
48 test.equal(filter.selectorDomain, ""); | 48 test.equal(filter.selectorDomain, ""); |
49 let unconditionals = elemHide.getUnconditionalSelectors(); | 49 })(Filter.fromText("###ads")); |
50 test.equal(unconditionals.selectorCount, 1); | 50 |
51 test.equal(unconditionals.selectorAt(0), "#ads"); | 51 withNAD(0, unconditionals => |
52 test.equal(unconditionals.filterKeyAt(0), "###ads"); | 52 { |
53 filter.delete(); | 53 test.equal(unconditionals.selectorCount, 1); |
54 unconditionals.delete(); | 54 test.equal(unconditionals.selectorAt(0), "#ads"); |
55 } | 55 test.equal(unconditionals.filterKeyAt(0), "###ads"); |
56 | 56 })(elemHide.getUnconditionalSelectors()); |
57 { | 57 |
58 let filter2 = Filter.fromText("example.com##.foo"); | 58 withNAD(0, filter => |
59 elemHide.add(filter2); | 59 { |
60 test.equal(filter2.selectorDomain, "example.com"); | 60 elemHide.add(filter); |
61 filter2.delete(); | 61 test.equal(filter.selectorDomain, "example.com"); |
62 } | 62 })(Filter.fromText("example.com##.foo")); |
63 | 63 |
64 let unconditionals = elemHide.getUnconditionalSelectors(); | 64 withNAD( |
65 test.equal(unconditionals.selectorCount, 1); | 65 0, unconditionals => |
66 unconditionals.delete(); | 66 test.equal(unconditionals.selectorCount, 1))(elemHide.getUnconditionalSe
lectors()); |
67 | 67 |
68 let selectors = elemHide.getSelectorsForDomain("example.com", 1); | 68 withNAD(0, selectors => |
69 test.equal(selectors.selectorCount, 1); | 69 { |
70 test.equal(selectors.selectorAt(0), ".foo"); | 70 test.equal(selectors.selectorCount, 1); |
71 test.equal(selectors.filterKeyAt(0), "example.com##.foo"); | 71 test.equal(selectors.selectorAt(0), ".foo"); |
72 selectors.delete(); | 72 test.equal(selectors.filterKeyAt(0), "example.com##.foo"); |
73 | 73 })(elemHide.getSelectorsForDomain("example.com", 1)); |
74 selectors = elemHide.getSelectorsForDomain("example.com", 0); | 74 |
75 test.equal(selectors.selectorCount, 2); | 75 withNAD(0, selectors => |
76 test.equal(selectors.selectorAt(0), "#ads"); | 76 { |
77 test.equal(selectors.filterKeyAt(0), "###ads"); | 77 test.equal(selectors.selectorCount, 2); |
78 test.equal(selectors.selectorAt(1), ".foo"); | 78 test.equal(selectors.selectorAt(0), "#ads"); |
79 test.equal(selectors.filterKeyAt(1), "example.com##.foo"); | 79 test.equal(selectors.filterKeyAt(0), "###ads"); |
80 selectors.delete(); | 80 test.equal(selectors.selectorAt(1), ".foo"); |
81 | 81 test.equal(selectors.filterKeyAt(1), "example.com##.foo"); |
82 let filter3 = Filter.fromText("example.com##.message"); | 82 })(elemHide.getSelectorsForDomain("example.com", 0)); |
83 elemHide.add(filter3); | 83 |
84 selectors = elemHide.getSelectorsForDomain("example.com", 0); | 84 withNAD(0, filter3 => |
85 test.equal(selectors.selectorCount, 3); | 85 { |
86 selectors.delete(); | 86 elemHide.add(filter3); |
87 selectors = elemHide.getSelectorsForDomain("mail.example.com", 0); | 87 |
88 test.equal(selectors.selectorCount, 3); | 88 withNAD( |
89 selectors.delete(); | 89 0, selectors => |
90 | 90 test.equal(selectors.selectorCount, 3))( |
91 let filter4 = Filter.fromText("mail.example.com#@#.message"); | 91 elemHide.getSelectorsForDomain("example.com", 0)); |
92 elemHide.add(filter4); | 92 |
93 selectors = elemHide.getSelectorsForDomain("example.com", 0); | 93 withNAD( |
94 test.equal(selectors.selectorCount, 3); | 94 0, selectors => |
95 selectors.delete(); | 95 test.equal(selectors.selectorCount, 3))( |
96 selectors = elemHide.getSelectorsForDomain("mail.example.com", 0); | 96 elemHide.getSelectorsForDomain("mail.example.com", 0)); |
97 test.equal(selectors.selectorCount, 2); | 97 |
98 selectors.delete(); | 98 withNAD(0, filter4 => |
99 | 99 { |
100 unconditionals = elemHide.getUnconditionalSelectors(); | 100 elemHide.add(filter4); |
101 test.equal(unconditionals.selectorCount, 1); | 101 withNAD( |
102 unconditionals.delete(); | 102 0, selectors => |
103 | 103 test.equal(selectors.selectorCount, 3))( |
104 elemHide.remove(filter4); | 104 elemHide.getSelectorsForDomain("example.com", 0)); |
105 filter4.delete(); | 105 |
106 selectors = elemHide.getSelectorsForDomain("example.com", 0); | 106 withNAD( |
107 test.equal(selectors.selectorCount, 3); | 107 0, selectors => |
108 selectors.delete(); | 108 test.equal(selectors.selectorCount, 2))( |
109 selectors = elemHide.getSelectorsForDomain("mail.example.com", 0); | 109 elemHide.getSelectorsForDomain("mail.example.com", 0)); |
110 test.equal(selectors.selectorCount, 3); | 110 |
111 selectors.delete(); | 111 withNAD( |
112 | 112 0, |
113 elemHide.remove(filter3); | 113 unconditionals => |
114 filter3.delete(); | 114 test.equal(unconditionals.selectorCount, 1))(elemHide.getUncondition
alSelectors()); |
115 selectors = elemHide.getSelectorsForDomain("example.com", 0); | 115 |
116 test.equal(selectors.selectorCount, 2); | 116 elemHide.remove(filter4); |
117 selectors.delete(); | 117 })(Filter.fromText("mail.example.com#@#.message")); |
118 | 118 |
119 elemHide.delete(); | 119 withNAD( |
| 120 0, selectors => |
| 121 test.equal(selectors.selectorCount, 3))( |
| 122 elemHide.getSelectorsForDomain("example.com", 0)); |
| 123 |
| 124 withNAD( |
| 125 0, selectors => |
| 126 test.equal(selectors.selectorCount, 3))( |
| 127 elemHide.getSelectorsForDomain("mail.example.com", 0)); |
| 128 |
| 129 elemHide.remove(filter3); |
| 130 })(Filter.fromText("example.com##.message")); |
| 131 |
| 132 withNAD( |
| 133 0, selectors => |
| 134 test.equal(selectors.selectorCount, 2))( |
| 135 elemHide.getSelectorsForDomain("example.com", 0)); |
| 136 })(ElemHide.create()); |
| 137 |
120 test.done(); | 138 test.done(); |
121 }; | 139 }; |
122 | 140 |
123 exports.testDomainRestrictions = function(test) | 141 exports.testDomainRestrictions = function(test) |
124 { | 142 { |
125 function testSelectorMatches(description, filters, domain, expectedMatches) | 143 function testSelectorMatches(description, filters, domain, expectedMatches) |
126 { | 144 { |
127 let elemHide = ElemHide.create(); | 145 withNAD(0, elemHide => |
128 | 146 { |
129 for (let text of filters) | 147 let addFilter = withNAD(0, filter => |
130 { | 148 { |
131 let filter = Filter.fromText(text); | 149 if (filter instanceof ElemHideEmulationFilter) |
132 if (filter instanceof ElemHideEmulationFilter) | 150 ElemHideEmulation.add(filter); |
133 ElemHideEmulation.add(filter); | 151 else |
134 else | 152 elemHide.add(filter); |
135 elemHide.add(filter); | 153 }); |
136 filter.delete(); | 154 |
137 } | 155 for (let text of filters) |
138 let matches = ElemHideEmulation.getRulesForDomain(domain, elemHide) | 156 addFilter(Filter.fromText(text)); |
139 .map(filter => filter.text); | 157 |
140 test.deepEqual(matches.sort(), expectedMatches.sort(), description); | 158 let matches = ElemHideEmulation.getRulesForDomain(domain, elemHide) |
141 | 159 .map(filter => filter.text); |
142 ElemHideEmulation.clear(); | 160 test.deepEqual(matches.sort(), expectedMatches.sort(), description); |
143 | 161 |
144 elemHide.delete(); | 162 ElemHideEmulation.clear(); |
| 163 })(ElemHide.create()); |
145 } | 164 } |
146 | 165 |
147 testSelectorMatches( | 166 testSelectorMatches( |
148 "Ignore generic filters", | 167 "Ignore generic filters", |
149 [ | 168 [ |
150 "##[-abp-properties='foo']", "example.com##[-abp-properties='foo']", | 169 "##[-abp-properties='foo']", "example.com##[-abp-properties='foo']", |
151 "~example.com##[-abp-properties='foo']" | 170 "~example.com##[-abp-properties='foo']" |
152 ], | 171 ], |
153 "example.com", | 172 "example.com", |
154 ["example.com##[-abp-properties='foo']"] | 173 ["example.com##[-abp-properties='foo']"] |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 ], | 207 ], |
189 "other.example.com", | 208 "other.example.com", |
190 ["other.example.com##[-abp-properties='foo']"] | 209 ["other.example.com##[-abp-properties='foo']"] |
191 ); | 210 ); |
192 | 211 |
193 test.done(); | 212 test.done(); |
194 }; | 213 }; |
195 | 214 |
196 exports.testElemHideEmulationFiltersContainer = function(test) | 215 exports.testElemHideEmulationFiltersContainer = function(test) |
197 { | 216 { |
198 let elemHide = ElemHide.create(); | 217 withNAD(0, elemHide => |
199 | |
200 function compareRules(description, domain, expectedMatches) | |
201 { | 218 { |
202 let result = ElemHideEmulation.getRulesForDomain(domain, elemHide) | 219 function compareRules(description, domain, expectedMatches) |
203 .map(filter => filter.text); | 220 { |
204 expectedMatches = expectedMatches.map(filter => filter.text); | 221 let result = ElemHideEmulation.getRulesForDomain(domain, elemHide) |
205 test.deepEqual(result.sort(), expectedMatches.sort(), description); | 222 .map(filter => filter.text); |
206 } | 223 expectedMatches = expectedMatches.map(filter => filter.text); |
207 | 224 test.deepEqual(result.sort(), expectedMatches.sort(), description); |
208 let domainFilter = Filter.fromText("example.com##filter1"); | 225 } |
209 let subdomainFilter = Filter.fromText("www.example.com##filter2"); | 226 |
210 let otherDomainFilter = Filter.fromText("other.example.com##filter3"); | 227 withNAD([0, 1, 2], (domainFilter, subdomainFilter, otherDomainFilter) => |
211 | 228 { |
212 ElemHideEmulation.add(domainFilter); | 229 ElemHideEmulation.add(domainFilter); |
213 ElemHideEmulation.add(subdomainFilter); | 230 ElemHideEmulation.add(subdomainFilter); |
214 ElemHideEmulation.add(otherDomainFilter); | 231 ElemHideEmulation.add(otherDomainFilter); |
215 compareRules( | 232 compareRules( |
216 "Return all matching filters", | 233 "Return all matching filters", |
217 "www.example.com", | 234 "www.example.com", |
218 [domainFilter, subdomainFilter] | 235 [domainFilter, subdomainFilter] |
219 ); | 236 ); |
220 | 237 |
221 ElemHideEmulation.remove(domainFilter); | 238 ElemHideEmulation.remove(domainFilter); |
222 compareRules( | 239 compareRules( |
223 "Return all matching filters after removing one", | 240 "Return all matching filters after removing one", |
224 "www.example.com", | 241 "www.example.com", |
225 [subdomainFilter] | 242 [subdomainFilter] |
226 ); | 243 ); |
227 | 244 |
228 ElemHideEmulation.clear(); | 245 ElemHideEmulation.clear(); |
229 compareRules( | 246 compareRules( |
230 "Return no filters after clearing", | 247 "Return no filters after clearing", |
231 "www.example.com", | 248 "www.example.com", |
232 [] | 249 [] |
233 ); | 250 ); |
234 | 251 })(Filter.fromText("example.com##filter1"), |
235 elemHide.delete(); | 252 Filter.fromText("www.example.com##filter2"), |
| 253 Filter.fromText("other.example.com##filter3")); |
| 254 })(ElemHide.create()); |
236 | 255 |
237 test.done(); | 256 test.done(); |
238 }; | 257 }; |
LEFT | RIGHT |