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 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 function normalizeSelectors(selectors) | 48 function normalizeSelectors(selectors) |
49 { | 49 { |
50 // generateStyleSheetForDomain is currently allowed to return duplicate | 50 // generateStyleSheetForDomain is currently allowed to return duplicate |
51 // selectors for performance reasons, so we need to remove duplicates here. | 51 // selectors for performance reasons, so we need to remove duplicates here. |
52 return selectors.slice().sort().filter((selector, index, sortedSelectors) => | 52 return selectors.slice().sort().filter((selector, index, sortedSelectors) => |
53 { | 53 { |
54 return index == 0 || selector != sortedSelectors[index - 1]; | 54 return index == 0 || selector != sortedSelectors[index - 1]; |
55 }); | 55 }); |
56 } | 56 } |
57 | 57 |
58 function testResult(domain, expectedSelectors, specificOnly) | 58 function testResult(domain, expectedSelectors, |
| 59 {specificOnly = false, expectedExceptions = []} = {}) |
59 { | 60 { |
60 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors); | 61 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors); |
61 | 62 |
62 let {code, selectors} = | 63 let {code, selectors, exceptions} = |
63 ElemHide.generateStyleSheetForDomain(domain, specificOnly, true); | 64 ElemHide.generateStyleSheetForDomain(domain, specificOnly, true, true); |
64 | 65 |
65 assert.deepEqual(normalizeSelectors(selectors), normalizedExpectedSelectors)
; | 66 assert.deepEqual(normalizeSelectors(selectors), normalizedExpectedSelectors)
; |
| 67 |
| 68 // Test for consistency in exception free case. |
| 69 assert.deepEqual(ElemHide.generateStyleSheetForDomain( |
| 70 domain, specificOnly, true, false), { |
| 71 code, |
| 72 selectors, |
| 73 exceptions: null |
| 74 }); |
| 75 |
| 76 assert.deepEqual(exceptions.map(({text}) => text), expectedExceptions); |
66 | 77 |
67 // Make sure each expected selector is in the actual CSS code. | 78 // Make sure each expected selector is in the actual CSS code. |
68 for (let selector of normalizedExpectedSelectors) | 79 for (let selector of normalizedExpectedSelectors) |
69 { | 80 { |
70 assert.ok(code.includes(selector + ", ") || | 81 assert.ok(code.includes(selector + ", ") || |
71 code.includes(selector + " {display: none !important;}\n")); | 82 code.includes(selector + " {display: none !important;}\n")); |
72 } | 83 } |
73 } | 84 } |
74 | 85 |
75 it("Generating Style Sheet", () => | 86 it("Generating Style Sheet", () => |
(...skipping 17 matching lines...) Expand all Loading... |
93 testResult("", []); | 104 testResult("", []); |
94 | 105 |
95 addFilter("foo.example.com##turnip"); | 106 addFilter("foo.example.com##turnip"); |
96 testResult("foo.example.com", ["turnip"]); | 107 testResult("foo.example.com", ["turnip"]); |
97 testResult("example.com", ["foo"]); | 108 testResult("example.com", ["foo"]); |
98 testResult("com", []); | 109 testResult("com", []); |
99 testResult("", []); | 110 testResult("", []); |
100 | 111 |
101 addException("example.com#@#foo"); | 112 addException("example.com#@#foo"); |
102 testResult("foo.example.com", ["turnip"]); | 113 testResult("foo.example.com", ["turnip"]); |
103 testResult("example.com", []); | 114 testResult("example.com", [], { |
| 115 expectedExceptions: ["example.com#@#foo"] |
| 116 }); |
104 testResult("com", []); | 117 testResult("com", []); |
105 testResult("", []); | 118 testResult("", []); |
106 | 119 |
107 addFilter("com##bar"); | 120 addFilter("com##bar"); |
108 testResult("foo.example.com", ["turnip", "bar"]); | 121 testResult("foo.example.com", ["turnip", "bar"]); |
109 testResult("example.com", ["bar"]); | 122 testResult("example.com", ["bar"], { |
| 123 expectedExceptions: ["example.com#@#foo"] |
| 124 }); |
110 testResult("com", ["bar"]); | 125 testResult("com", ["bar"]); |
111 testResult("", []); | 126 testResult("", []); |
112 | 127 |
113 addException("example.com#@#bar"); | 128 addException("example.com#@#bar"); |
114 testResult("foo.example.com", ["turnip"]); | 129 testResult("foo.example.com", ["turnip"], { |
115 testResult("example.com", []); | 130 expectedExceptions: ["example.com#@#bar"] |
| 131 }); |
| 132 testResult("example.com", [], { |
| 133 expectedExceptions: ["example.com#@#foo", "example.com#@#bar"] |
| 134 }); |
116 testResult("com", ["bar"]); | 135 testResult("com", ["bar"]); |
117 testResult("", []); | 136 testResult("", []); |
118 | 137 |
119 removeException("example.com#@#foo"); | 138 removeException("example.com#@#foo"); |
120 testResult("foo.example.com", ["turnip"]); | 139 testResult("foo.example.com", ["turnip"], { |
121 testResult("example.com", ["foo"]); | 140 expectedExceptions: ["example.com#@#bar"] |
| 141 }); |
| 142 testResult("example.com", ["foo"], { |
| 143 expectedExceptions: ["example.com#@#bar"] |
| 144 }); |
122 testResult("com", ["bar"]); | 145 testResult("com", ["bar"]); |
123 testResult("", []); | 146 testResult("", []); |
124 | 147 |
125 removeException("example.com#@#bar"); | 148 removeException("example.com#@#bar"); |
126 testResult("foo.example.com", ["turnip", "bar"]); | 149 testResult("foo.example.com", ["turnip", "bar"]); |
127 testResult("example.com", ["foo", "bar"]); | 150 testResult("example.com", ["foo", "bar"]); |
128 testResult("com", ["bar"]); | 151 testResult("com", ["bar"]); |
129 testResult("", []); | 152 testResult("", []); |
130 | 153 |
131 addFilter("##generic"); | 154 addFilter("##generic"); |
132 testResult("foo.example.com", ["turnip", "bar", "generic"]); | 155 testResult("foo.example.com", ["turnip", "bar", "generic"]); |
133 testResult("example.com", ["foo", "bar", "generic"]); | 156 testResult("example.com", ["foo", "bar", "generic"]); |
134 testResult("com", ["bar", "generic"]); | 157 testResult("com", ["bar", "generic"]); |
135 testResult("", ["generic"]); | 158 testResult("", ["generic"]); |
136 testResult("foo.example.com", ["turnip", "bar"], true); | 159 testResult("foo.example.com", ["turnip", "bar"], {specificOnly: true}); |
137 testResult("example.com", ["foo", "bar"], true); | 160 testResult("example.com", ["foo", "bar"], {specificOnly: true}); |
138 testResult("com", ["bar"], true); | 161 testResult("com", ["bar"], {specificOnly: true}); |
139 testResult("", [], true); | 162 testResult("", [], {specificOnly: true}); |
140 removeFilter("##generic"); | 163 removeFilter("##generic"); |
141 | 164 |
142 addFilter("~adblockplus.org##example"); | 165 addFilter("~adblockplus.org##example"); |
143 testResult("adblockplus.org", []); | 166 testResult("adblockplus.org", []); |
144 testResult("", ["example"]); | 167 testResult("", ["example"]); |
145 testResult("foo.example.com", ["turnip", "bar", "example"]); | 168 testResult("foo.example.com", ["turnip", "bar", "example"]); |
146 testResult("foo.example.com", ["turnip", "bar"], true); | 169 testResult("foo.example.com", ["turnip", "bar"], {specificOnly: true}); |
147 removeFilter("~adblockplus.org##example"); | 170 removeFilter("~adblockplus.org##example"); |
148 | 171 |
149 removeFilter("~foo.example.com,example.com##foo"); | 172 removeFilter("~foo.example.com,example.com##foo"); |
150 testResult("foo.example.com", ["turnip", "bar"]); | 173 testResult("foo.example.com", ["turnip", "bar"]); |
151 testResult("example.com", ["bar"]); | 174 testResult("example.com", ["bar"]); |
152 testResult("com", ["bar"]); | 175 testResult("com", ["bar"]); |
153 testResult("", []); | 176 testResult("", []); |
154 | 177 |
155 removeFilter("com##bar"); | 178 removeFilter("com##bar"); |
156 testResult("foo.example.com", ["turnip"]); | 179 testResult("foo.example.com", ["turnip"]); |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 testResult("com", ["foo"]); | 216 testResult("com", ["foo"]); |
194 testResult("", ["foo"]); | 217 testResult("", ["foo"]); |
195 removeFilter("~example.com##foo"); | 218 removeFilter("~example.com##foo"); |
196 | 219 |
197 removeFilter("~foo.example.com,example.com##foo"); | 220 removeFilter("~foo.example.com,example.com##foo"); |
198 | 221 |
199 // Test criteria | 222 // Test criteria |
200 addFilter("##hello"); | 223 addFilter("##hello"); |
201 addFilter("~example.com##world"); | 224 addFilter("~example.com##world"); |
202 addFilter("foo.com##specific"); | 225 addFilter("foo.com##specific"); |
203 testResult("foo.com", ["specific"], true); | 226 testResult("foo.com", ["specific"], {specificOnly: true}); |
204 testResult("foo.com", ["hello", "specific", "world"], false); | |
205 testResult("foo.com", ["hello", "specific", "world"]); | 227 testResult("foo.com", ["hello", "specific", "world"]); |
206 testResult("foo.com.", ["hello", "specific", "world"]); | 228 testResult("example.com", [], {specificOnly: true}); |
207 testResult("example.com", [], true); | |
208 removeFilter("foo.com##specific"); | 229 removeFilter("foo.com##specific"); |
209 removeFilter("~example.com##world"); | 230 removeFilter("~example.com##world"); |
210 removeFilter("##hello"); | 231 removeFilter("##hello"); |
211 testResult("foo.com", []); | 232 testResult("foo.com", []); |
212 | 233 |
213 addFilter("##hello"); | 234 addFilter("##hello"); |
214 testResult("foo.com", [], true); | 235 testResult("foo.com", [], {specificOnly: true}); |
215 testResult("foo.com", ["hello"], false); | 236 testResult("foo.com", ["hello"]); |
216 testResult("foo.com", ["hello"]); | 237 testResult("bar.com", [], {specificOnly: true}); |
217 testResult("bar.com", [], true); | |
218 testResult("bar.com", ["hello"], false); | |
219 testResult("bar.com", ["hello"]); | 238 testResult("bar.com", ["hello"]); |
220 addException("foo.com#@#hello"); | 239 addException("foo.com#@#hello"); |
221 testResult("foo.com", [], true); | 240 testResult("foo.com", [], {specificOnly: true}); |
222 testResult("foo.com", [], false); | 241 testResult("foo.com", [], {expectedExceptions: ["foo.com#@#hello"]}); |
223 testResult("foo.com", []); | 242 testResult("bar.com", [], {specificOnly: true}); |
224 testResult("bar.com", [], true); | |
225 testResult("bar.com", ["hello"], false); | |
226 testResult("bar.com", ["hello"]); | 243 testResult("bar.com", ["hello"]); |
227 removeException("foo.com#@#hello"); | 244 removeException("foo.com#@#hello"); |
228 testResult("foo.com", [], true); | 245 testResult("foo.com", [], {specificOnly: true}); |
229 // Note: We don't take care to track conditional selectors which became | 246 // Note: We don't take care to track conditional selectors which became |
230 // unconditional when a filter was removed. This was too expensive. | 247 // unconditional when a filter was removed. This was too expensive. |
231 testResult("foo.com", ["hello"], false); | 248 testResult("foo.com", ["hello"]); |
232 testResult("foo.com", ["hello"]); | 249 testResult("bar.com", [], {specificOnly: true}); |
233 testResult("bar.com", [], true); | |
234 testResult("bar.com", ["hello"], false); | |
235 testResult("bar.com", ["hello"]); | 250 testResult("bar.com", ["hello"]); |
236 removeFilter("##hello"); | 251 removeFilter("##hello"); |
237 testResult("foo.com", []); | 252 testResult("foo.com", []); |
238 testResult("bar.com", []); | 253 testResult("bar.com", []); |
239 | 254 |
240 addFilter("##hello"); | 255 addFilter("##hello"); |
241 addFilter("foo.com##hello"); | 256 addFilter("foo.com##hello"); |
242 testResult("foo.com", ["hello"]); | 257 testResult("foo.com", ["hello"]); |
243 removeFilter("foo.com##hello"); | 258 removeFilter("foo.com##hello"); |
244 testResult("foo.com", ["hello"]); | 259 testResult("foo.com", ["hello"]); |
245 removeFilter("##hello"); | 260 removeFilter("##hello"); |
246 testResult("foo.com", []); | 261 testResult("foo.com", []); |
247 | 262 |
248 addFilter("##hello"); | 263 addFilter("##hello"); |
249 addFilter("foo.com##hello"); | 264 addFilter("foo.com##hello"); |
250 testResult("foo.com", ["hello"]); | 265 testResult("foo.com", ["hello"]); |
251 removeFilter("##hello"); | 266 removeFilter("##hello"); |
252 testResult("foo.com", ["hello"]); | 267 testResult("foo.com", ["hello"]); |
253 removeFilter("foo.com##hello"); | 268 removeFilter("foo.com##hello"); |
254 testResult("foo.com", []); | 269 testResult("foo.com", []); |
255 }); | 270 }); |
256 | 271 |
257 it("Zero filter key", () => | 272 it("Zero filter key", () => |
258 { | 273 { |
259 ElemHide.add(Filter.fromText("##test")); | 274 ElemHide.add(Filter.fromText("##test")); |
260 ElemHideExceptions.add(Filter.fromText("foo.com#@#test")); | 275 ElemHideExceptions.add(Filter.fromText("foo.com#@#test")); |
261 testResult("foo.com", []); | 276 testResult("foo.com", [], {expectedExceptions: ["foo.com#@#test"]}); |
262 testResult("bar.com", ["test"]); | 277 testResult("bar.com", ["test"]); |
263 }); | 278 }); |
264 | 279 |
265 it("Filters by Domain", () => | 280 it("Filters by Domain", () => |
266 { | 281 { |
267 assert.equal(filtersByDomain.size, 0); | 282 assert.equal(filtersByDomain.size, 0); |
268 | 283 |
269 ElemHide.add(Filter.fromText("##test")); | 284 ElemHide.add(Filter.fromText("##test")); |
270 assert.equal(filtersByDomain.size, 0); | 285 assert.equal(filtersByDomain.size, 0); |
271 | 286 |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 // terminated with a newline character, including the last rule. If this is | 342 // terminated with a newline character, including the last rule. If this is |
328 // not the case, the function goes into an infinite loop. It should only be | 343 // not the case, the function goes into an infinite loop. It should only be |
329 // used with the return value of the createStyleSheet function. | 344 // used with the return value of the createStyleSheet function. |
330 | 345 |
331 assert.deepEqual([...rulesFromStyleSheet("")], []); | 346 assert.deepEqual([...rulesFromStyleSheet("")], []); |
332 assert.deepEqual([...rulesFromStyleSheet("#foo {}\n")], ["#foo {}"]); | 347 assert.deepEqual([...rulesFromStyleSheet("#foo {}\n")], ["#foo {}"]); |
333 assert.deepEqual([...rulesFromStyleSheet("#foo {}\n#bar {}\n")], | 348 assert.deepEqual([...rulesFromStyleSheet("#foo {}\n#bar {}\n")], |
334 ["#foo {}", "#bar {}"]); | 349 ["#foo {}", "#bar {}"]); |
335 }); | 350 }); |
336 }); | 351 }); |
LEFT | RIGHT |