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 | |
41 exports.testElemHideAPI = function(test) | 41 exports.testElemHideAPI = function(test) |
42 { | 42 { |
43 let filter = Filter.fromText("###ads"); | 43 withNAD(0, elemHide => |
44 ElemHide.add(filter); | 44 { |
45 | 45 withNAD(0, filter => |
46 test.equal(filter.selectorDomain, ""); | 46 { |
47 let unconditionals = ElemHide.getUnconditionalSelectors(); | 47 elemHide.add(filter); |
48 test.equal(unconditionals.selectorCount, 1); | 48 test.equal(filter.selectorDomain, ""); |
49 test.equal(unconditionals.selectorAt(0), "#ads"); | 49 })(Filter.fromText("###ads")); |
50 test.equal(unconditionals.filterKeyAt(0), "###ads"); | 50 |
51 | 51 withNAD(0, unconditionals => |
52 let filter2 = Filter.fromText("example.com##.foo"); | 52 { |
53 ElemHide.add(filter2); | 53 test.equal(unconditionals.selectorCount, 1); |
54 test.equal(filter2.selectorDomain, "example.com"); | 54 test.equal(unconditionals.selectorAt(0), "#ads"); |
55 | 55 test.equal(unconditionals.filterKeyAt(0), "###ads"); |
56 unconditionals = ElemHide.getUnconditionalSelectors(); | 56 })(elemHide.getUnconditionalSelectors()); |
57 test.equal(unconditionals.selectorCount, 1); | 57 |
58 | 58 withNAD(0, filter => |
59 let selectors = ElemHide.getSelectorsForDomain("example.com", 1); | 59 { |
60 test.equal(selectors.selectorCount, 1); | 60 elemHide.add(filter); |
61 test.equal(selectors.selectorAt(0), ".foo"); | 61 test.equal(filter.selectorDomain, "example.com"); |
62 test.equal(selectors.filterKeyAt(0), "example.com##.foo"); | 62 })(Filter.fromText("example.com##.foo")); |
63 | 63 |
64 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 64 withNAD( |
65 test.equal(selectors.selectorCount, 2); | 65 0, unconditionals => |
66 test.equal(selectors.selectorAt(0), "#ads"); | 66 test.equal(unconditionals.selectorCount, 1))(elemHide.getUnconditionalSe
lectors()); |
67 test.equal(selectors.filterKeyAt(0), "###ads"); | 67 |
68 test.equal(selectors.selectorAt(1), ".foo"); | 68 withNAD(0, selectors => |
69 test.equal(selectors.filterKeyAt(1), "example.com##.foo"); | 69 { |
70 | 70 test.equal(selectors.selectorCount, 1); |
71 let filter3 = Filter.fromText("example.com##.message"); | 71 test.equal(selectors.selectorAt(0), ".foo"); |
72 ElemHide.add(filter3); | 72 test.equal(selectors.filterKeyAt(0), "example.com##.foo"); |
73 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 73 })(elemHide.getSelectorsForDomain("example.com", 1)); |
74 test.equal(selectors.selectorCount, 3); | 74 |
75 selectors = ElemHide.getSelectorsForDomain("mail.example.com", 0); | 75 withNAD(0, selectors => |
76 test.equal(selectors.selectorCount, 3); | 76 { |
77 | 77 test.equal(selectors.selectorCount, 2); |
78 let filter4 = Filter.fromText("mail.example.com#@#.message"); | 78 test.equal(selectors.selectorAt(0), "#ads"); |
79 ElemHide.add(filter4); | 79 test.equal(selectors.filterKeyAt(0), "###ads"); |
80 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 80 test.equal(selectors.selectorAt(1), ".foo"); |
81 test.equal(selectors.selectorCount, 3); | 81 test.equal(selectors.filterKeyAt(1), "example.com##.foo"); |
82 selectors = ElemHide.getSelectorsForDomain("mail.example.com", 0); | 82 })(elemHide.getSelectorsForDomain("example.com", 0)); |
83 test.equal(selectors.selectorCount, 2); | 83 |
84 | 84 withNAD(0, filter3 => |
85 unconditionals = ElemHide.getUnconditionalSelectors(); | 85 { |
86 test.equal(unconditionals.selectorCount, 1); | 86 elemHide.add(filter3); |
87 | 87 |
88 ElemHide.remove(filter4); | 88 withNAD( |
89 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 89 0, selectors => |
90 test.equal(selectors.selectorCount, 3); | 90 { |
91 selectors = ElemHide.getSelectorsForDomain("mail.example.com", 0); | 91 test.equal(selectors.selectorCount, 3); |
92 test.equal(selectors.selectorCount, 3); | 92 test.equal(selectors.selectorAt(0), "#ads"); |
93 | 93 test.equal(selectors.selectorAt(1), ".message"); |
94 ElemHide.remove(filter3); | 94 test.equal(selectors.selectorAt(2), ".foo"); |
95 selectors = ElemHide.getSelectorsForDomain("example.com", 0); | 95 })( |
96 test.equal(selectors.selectorCount, 2); | 96 elemHide.getSelectorsForDomain("example.com", 0)); |
| 97 |
| 98 withNAD( |
| 99 0, selectors => |
| 100 { |
| 101 test.equal(selectors.selectorCount, 3); |
| 102 test.equal(selectors.selectorAt(0), "#ads"); |
| 103 test.equal(selectors.selectorAt(1), ".message"); |
| 104 test.equal(selectors.selectorAt(2), ".foo"); |
| 105 })( |
| 106 elemHide.getSelectorsForDomain("mail.example.com", 0)); |
| 107 |
| 108 withNAD(0, filter4 => |
| 109 { |
| 110 elemHide.add(filter4); |
| 111 withNAD( |
| 112 0, selectors => |
| 113 { |
| 114 test.equal(selectors.selectorCount, 3); |
| 115 test.equal(selectors.selectorAt(0), "#ads"); |
| 116 test.equal(selectors.selectorAt(1), ".message"); |
| 117 test.equal(selectors.selectorAt(2), ".foo"); |
| 118 })( |
| 119 elemHide.getSelectorsForDomain("example.com", 0)); |
| 120 |
| 121 withNAD( |
| 122 0, selectors => |
| 123 { |
| 124 test.equal(selectors.selectorCount, 2); |
| 125 test.equal(selectors.selectorAt(0), "#ads"); |
| 126 test.equal(selectors.selectorAt(1), ".foo"); |
| 127 })( |
| 128 elemHide.getSelectorsForDomain("mail.example.com", 0)); |
| 129 |
| 130 withNAD( |
| 131 0, |
| 132 unconditionals => |
| 133 { |
| 134 test.equal(unconditionals.selectorCount, 1); |
| 135 test.equal(unconditionals.selectorAt(0), "#ads"); |
| 136 })(elemHide.getUnconditionalSelectors()); |
| 137 |
| 138 elemHide.remove(filter4); |
| 139 })(Filter.fromText("mail.example.com#@#.message")); |
| 140 |
| 141 withNAD( |
| 142 0, selectors => |
| 143 { |
| 144 test.equal(selectors.selectorCount, 3); |
| 145 test.equal(selectors.selectorAt(0), "#ads"); |
| 146 test.equal(selectors.selectorAt(1), ".message"); |
| 147 test.equal(selectors.selectorAt(2), ".foo"); |
| 148 })(elemHide.getSelectorsForDomain("example.com", 0)); |
| 149 |
| 150 withNAD( |
| 151 0, selectors => |
| 152 { |
| 153 test.equal(selectors.selectorCount, 3); |
| 154 test.equal(selectors.selectorAt(0), "#ads"); |
| 155 test.equal(selectors.selectorAt(1), ".message"); |
| 156 test.equal(selectors.selectorAt(2), ".foo"); |
| 157 })(elemHide.getSelectorsForDomain("mail.example.com", 0)); |
| 158 |
| 159 elemHide.remove(filter3); |
| 160 })(Filter.fromText("example.com##.message")); |
| 161 |
| 162 withNAD( |
| 163 0, selectors => |
| 164 test.equal(selectors.selectorCount, 2))( |
| 165 elemHide.getSelectorsForDomain("example.com", 0)); |
| 166 })(ElemHide.create()); |
97 | 167 |
98 test.done(); | 168 test.done(); |
99 }; | 169 }; |
100 | 170 |
101 exports.testSyntaxConversion = function(test) | 171 exports.testSyntaxConversion = function(test) |
102 { | 172 { |
103 function checkConvertedFilter(old, converted) | 173 function checkConvertedFilter(old, converted) |
104 { | 174 { |
105 let filter = Filter.fromText(old); | 175 withNAD( |
106 | 176 0, filter => test.equal(filter.text, converted))(Filter.fromText(old)); |
107 test.equal(filter.text, converted); | |
108 } | 177 } |
109 | 178 |
110 checkConvertedFilter("example.com#?#foo[-abp-properties='something']bar", "exa
mple.com#?#foo:-abp-properties(something)bar"); | 179 checkConvertedFilter("example.com##foo[-abp-properties='something']bar", "exam
ple.com#?#foo:-abp-properties(something)bar"); |
111 checkConvertedFilter("example.com#?#[-abp-properties=\"something\"]", "example
.com#?#:-abp-properties(something)"); | 180 checkConvertedFilter("example.com#@#foo[-abp-properties='something']bar", "exa
mple.com#@#foo:-abp-properties(something)bar"); |
112 checkConvertedFilter("example.com#?#[-abp-properties=(something)]", "example.c
om#?#:-abp-properties((something))"); | 181 checkConvertedFilter("example.com##[-abp-properties=\"something\"]", "example.
com#?#:-abp-properties(something)"); |
| 182 checkConvertedFilter("example.com##[-abp-properties='(something)']", "example.
com#?#:-abp-properties((something))"); |
113 | 183 |
114 test.done(); | 184 test.done(); |
115 }; | 185 }; |
116 | 186 |
117 exports.testDomainRestrictions = function(test) | 187 exports.testDomainRestrictions = function(test) |
118 { | 188 { |
119 function testSelectorMatches(description, filters, domain, expectedMatches) | 189 function testSelectorMatches(description, filters, domain, expectedMatches) |
120 { | 190 { |
121 for (let text of filters) | 191 withNAD([0, 1], (elemHide, elemHideEmulation) => |
122 { | 192 { |
123 let filter = Filter.fromText(text); | 193 let addFilter = withNAD(0, filter => |
124 if (filter instanceof ElemHideEmulationFilter) | 194 { |
125 ElemHideEmulation.add(filter); | 195 if (filter instanceof ElemHideEmulationFilter) |
126 else | 196 elemHideEmulation.add(filter); |
127 ElemHide.add(filter); | 197 else |
128 } | 198 elemHide.add(filter); |
129 | 199 }); |
130 let rules = ElemHideEmulation.getRulesForDomain(domain); | 200 |
131 let matches = []; | 201 for (let text of filters) |
132 for (let i = 0; i < rules.filterCount; i++) | 202 addFilter(Filter.fromText(text)); |
133 matches.push(rules.filterAt(i).text); | 203 |
134 test.deepEqual(matches.sort(), expectedMatches.sort(), description); | 204 withNAD(0, rules => |
135 | 205 { |
136 ElemHideEmulation.clear(); | 206 let matches = []; |
137 ElemHide.clear(); | 207 let push = withNAD(0, filter => matches.push(filter.text)); |
| 208 |
| 209 for (let i = 0; i < rules.filterCount; i++) |
| 210 push(rules.filterAt(i)); |
| 211 |
| 212 test.deepEqual(matches.sort(), expectedMatches.sort(), description); |
| 213 })(elemHideEmulation.getRulesForDomain(elemHide, domain)); |
| 214 |
| 215 elemHideEmulation.clear(); |
| 216 })(ElemHide.create(), ElemHideEmulation.create()); |
138 } | 217 } |
139 | 218 |
140 testSelectorMatches( | 219 testSelectorMatches( |
141 "Ignore generic filters", | 220 "Ignore generic filters", |
142 [ | 221 [ |
143 "#?#:-abp-properties(foo)", "example.com#?#:-abp-properties(foo)", | 222 "#?#:-abp-properties(foo)", "example.com#?#:-abp-properties(foo)", |
144 "~example.com##:-abp-properties(foo)" | 223 "~example.com##:-abp-properties(foo)" |
145 ], | 224 ], |
146 "example.com", | 225 "example.com", |
147 ["example.com#?#:-abp-properties(foo)"] | 226 ["example.com#?#:-abp-properties(foo)"] |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
181 ], | 260 ], |
182 "other.example.com", | 261 "other.example.com", |
183 ["other.example.com#?#:-abp-properties(foo)"] | 262 ["other.example.com#?#:-abp-properties(foo)"] |
184 ); | 263 ); |
185 | 264 |
186 test.done(); | 265 test.done(); |
187 }; | 266 }; |
188 | 267 |
189 exports.testElemHideEmulationFiltersContainer = function(test) | 268 exports.testElemHideEmulationFiltersContainer = function(test) |
190 { | 269 { |
191 function compareRules(description, domain, expectedMatches) | 270 withNAD([0, 1], (elemHide, elemHideEmulation) => |
192 { | 271 { |
193 let rules = ElemHideEmulation.getRulesForDomain(domain); | 272 function compareRules(description, domain, expectedMatches) |
194 let result = []; | 273 { |
195 for (let i = 0; i < rules.filterCount; i++) | 274 withNAD(0, rules => |
196 result.push(rules.filterAt(i).text); | 275 { |
197 expectedMatches = expectedMatches.map(filter => filter.text); | 276 let result = []; |
198 test.deepEqual(result.sort(), expectedMatches.sort(), description); | 277 for (let i = 0; i < rules.filterCount; i++) |
199 } | 278 withNAD(0, filter => result.push(filter.text))(rules.filterAt(i)); |
200 | 279 |
201 let domainFilter = Filter.fromText("example.com##filter1"); | 280 expectedMatches = expectedMatches.map(filter => filter.text); |
202 let subdomainFilter = Filter.fromText("www.example.com##filter2"); | 281 test.deepEqual(result.sort(), expectedMatches.sort(), description); |
203 let otherDomainFilter = Filter.fromText("other.example.com##filter3"); | 282 })(elemHideEmulation.getRulesForDomain(elemHide, domain)); |
204 | 283 } |
205 ElemHideEmulation.add(domainFilter); | 284 |
206 ElemHideEmulation.add(subdomainFilter); | 285 withNAD([0, 1, 2], (domainFilter, subdomainFilter, otherDomainFilter) => |
207 ElemHideEmulation.add(otherDomainFilter); | 286 { |
208 compareRules( | 287 elemHideEmulation.add(domainFilter); |
209 "Return all matching filters", | 288 elemHideEmulation.add(subdomainFilter); |
210 "www.example.com", | 289 elemHideEmulation.add(otherDomainFilter); |
211 [domainFilter, subdomainFilter] | 290 compareRules( |
212 ); | 291 "Return all matching filters", |
213 | 292 "www.example.com", |
214 ElemHideEmulation.remove(domainFilter); | 293 [domainFilter, subdomainFilter] |
215 compareRules( | 294 ); |
216 "Return all matching filters after removing one", | 295 |
217 "www.example.com", | 296 elemHideEmulation.remove(domainFilter); |
218 [subdomainFilter] | 297 compareRules( |
219 ); | 298 "Return all matching filters after removing one", |
220 | 299 "www.example.com", |
221 ElemHideEmulation.clear(); | 300 [subdomainFilter] |
222 compareRules( | 301 ); |
223 "Return no filters after clearing", | 302 |
224 "www.example.com", | 303 elemHideEmulation.clear(); |
225 [] | 304 compareRules( |
226 ); | 305 "Return no filters after clearing", |
| 306 "www.example.com", |
| 307 [] |
| 308 ); |
| 309 })(Filter.fromText("example.com##filter1"), |
| 310 Filter.fromText("www.example.com##filter2"), |
| 311 Filter.fromText("other.example.com##filter3")); |
| 312 })(ElemHide.create(), ElemHideEmulation.create()); |
227 | 313 |
228 test.done(); | 314 test.done(); |
229 }; | 315 }; |
LEFT | RIGHT |