Left: | ||
Right: |
OLD | NEW |
---|---|
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 26 matching lines...) Expand all Loading... | |
37 {Filter} = sandboxedRequire("../lib/filterClasses") | 37 {Filter} = sandboxedRequire("../lib/filterClasses") |
38 ); | 38 ); |
39 | 39 |
40 callback(); | 40 callback(); |
41 }; | 41 }; |
42 | 42 |
43 function normalizeSelectors(selectors) | 43 function normalizeSelectors(selectors) |
44 { | 44 { |
45 // getSelectorsForDomain is currently allowed to return duplicate selectors | 45 // getSelectorsForDomain is currently allowed to return duplicate selectors |
46 // for performance reasons, so we need to remove duplicates here. | 46 // for performance reasons, so we need to remove duplicates here. |
47 return selectors.sort().filter((selector, index, sortedSelectors) => | 47 return selectors.slice().sort().filter((selector, index, sortedSelectors) => |
Manish Jethani
2018/09/17 09:28:43
Need to make a copy now because the return value o
| |
48 { | 48 { |
49 return index == 0 || selector != sortedSelectors[index - 1]; | 49 return index == 0 || selector != sortedSelectors[index - 1]; |
50 }); | 50 }); |
51 } | 51 } |
52 | 52 |
53 function testResult(test, domain, expectedSelectors, specificOnly) | 53 function testResult(test, domain, expectedSelectors, specificOnly) |
54 { | 54 { |
55 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors); | 55 let normalizedExpectedSelectors = normalizeSelectors(expectedSelectors); |
56 | 56 |
57 test.deepEqual( | 57 test.deepEqual( |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 test.equal(filtersByDomain.size, 2); | 266 test.equal(filtersByDomain.size, 2); |
267 | 267 |
268 ElemHide.remove(Filter.fromText("example.com##test")); | 268 ElemHide.remove(Filter.fromText("example.com##test")); |
269 test.equal(filtersByDomain.size, 2); | 269 test.equal(filtersByDomain.size, 2); |
270 | 270 |
271 ElemHide.remove(Filter.fromText("example.com,~www.example.com##test")); | 271 ElemHide.remove(Filter.fromText("example.com,~www.example.com##test")); |
272 test.equal(filtersByDomain.size, 0); | 272 test.equal(filtersByDomain.size, 0); |
273 | 273 |
274 test.done(); | 274 test.done(); |
275 }; | 275 }; |
276 | |
277 exports.testCommonSelectorsDedup = function(test) | |
Manish Jethani
2018/09/17 09:28:43
Read this first.
| |
278 { | |
279 // Here we compare references to verify that the selector lookup function | |
280 // returns the same object for the common case. | |
281 | |
282 ElemHide.add(Filter.fromText("##test1")); | |
283 ElemHide.add(Filter.fromText("##test2")); | |
284 ElemHide.add(Filter.fromText("##test3")); | |
285 | |
286 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | |
287 ElemHide.getSelectorsForDomain("bar.example.com")); | |
288 | |
289 // Domain-specific filter. | |
290 ElemHide.add(Filter.fromText("foo.example.com##test4")); | |
291 | |
292 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"), | |
293 ElemHide.getSelectorsForDomain("bar.example.com")); | |
294 | |
295 ElemHide.remove(Filter.fromText("foo.example.com##test4")); | |
296 | |
297 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | |
298 ElemHide.getSelectorsForDomain("bar.example.com")); | |
299 | |
300 // Generic filter with domain excluded. | |
301 ElemHide.add(Filter.fromText("~foo.example.com##test4")); | |
302 | |
303 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"), | |
304 ElemHide.getSelectorsForDomain("bar.example.com")); | |
305 | |
306 ElemHide.remove(Filter.fromText("~foo.example.com##test4")); | |
307 | |
308 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | |
309 ElemHide.getSelectorsForDomain("bar.example.com")); | |
310 | |
311 // Domain-specific exception. | |
312 ElemHideExceptions.add(Filter.fromText("foo.example.com#@#test4")); | |
313 | |
314 // Since the exception does not apply to any filters yet, the objects | |
315 // returned for foo.example.com and bar.example.com are one and the same. | |
316 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | |
317 ElemHide.getSelectorsForDomain("bar.example.com")); | |
318 | |
319 ElemHide.add(Filter.fromText("##test4")); | |
320 | |
321 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"), | |
322 ElemHide.getSelectorsForDomain("bar.example.com")); | |
323 | |
324 ElemHide.remove(Filter.fromText("##test4")); | |
325 ElemHideExceptions.remove(Filter.fromText("foo.example.com#@#test4")); | |
326 | |
327 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | |
328 ElemHide.getSelectorsForDomain("bar.example.com")); | |
329 | |
330 // Generic exception with domain excluded. | |
331 ElemHideExceptions.add(Filter.fromText("~foo.example.com#@#test4")); | |
332 | |
333 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | |
334 ElemHide.getSelectorsForDomain("bar.example.com")); | |
335 | |
336 ElemHide.add(Filter.fromText("##test4")); | |
337 | |
338 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"), | |
339 ElemHide.getSelectorsForDomain("bar.example.com")); | |
340 | |
341 ElemHide.remove(Filter.fromText("##test4")); | |
342 ElemHideExceptions.remove(Filter.fromText("~foo.example.com#@#test4")); | |
343 | |
344 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | |
345 ElemHide.getSelectorsForDomain("bar.example.com")); | |
346 | |
347 // Generic exception. | |
348 ElemHide.add(Filter.fromText("##test4")); | |
349 ElemHideExceptions.add(Filter.fromText("#@#test4")); | |
350 | |
351 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | |
352 ElemHide.getSelectorsForDomain("bar.example.com")); | |
353 | |
354 ElemHideExceptions.add(Filter.fromText("example.com#@#test4")); | |
355 | |
356 // In this case, even though the exception applies to both domains, we get | |
357 // different objects. | |
358 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"), | |
359 ElemHide.getSelectorsForDomain("bar.example.com")); | |
360 test.deepEqual(ElemHide.getSelectorsForDomain("foo.example.com"), | |
361 ElemHide.getSelectorsForDomain("bar.example.com")); | |
362 | |
363 ElemHideExceptions.remove(Filter.fromText("example.com#@#test4")); | |
364 ElemHideExceptions.remove(Filter.fromText("#@#test4")); | |
365 ElemHide.remove(Filter.fromText("##test4")); | |
366 | |
367 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | |
368 ElemHide.getSelectorsForDomain("bar.example.com")); | |
369 | |
370 ElemHide.remove(Filter.fromText("##test1")); | |
371 ElemHide.remove(Filter.fromText("##test2")); | |
372 ElemHide.remove(Filter.fromText("##test3")); | |
373 | |
374 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | |
375 ElemHide.getSelectorsForDomain("bar.example.com")); | |
376 | |
377 test.done(); | |
378 }; | |
OLD | NEW |