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 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) => |
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 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 | 276 |
277 exports.testGenericSelectorDedup = function(test) | 277 exports.testCommonSelectorsDedup = function(test) |
278 { | 278 { |
| 279 // Here we compare references to verify that the selector lookup function |
| 280 // returns the same object for the common case. |
| 281 |
279 ElemHide.add(Filter.fromText("##test1")); | 282 ElemHide.add(Filter.fromText("##test1")); |
280 ElemHide.add(Filter.fromText("##test2")); | 283 ElemHide.add(Filter.fromText("##test2")); |
281 ElemHide.add(Filter.fromText("##test3")); | 284 ElemHide.add(Filter.fromText("##test3")); |
282 | 285 |
283 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | 286 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), |
284 ElemHide.getSelectorsForDomain("bar.example.com")); | 287 ElemHide.getSelectorsForDomain("bar.example.com")); |
285 | 288 |
| 289 // Domain-specific filter. |
286 ElemHide.add(Filter.fromText("foo.example.com##test4")); | 290 ElemHide.add(Filter.fromText("foo.example.com##test4")); |
287 | 291 |
288 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"), | 292 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"), |
289 ElemHide.getSelectorsForDomain("bar.example.com")); | 293 ElemHide.getSelectorsForDomain("bar.example.com")); |
290 | 294 |
291 ElemHide.remove(Filter.fromText("foo.example.com##test4")); | 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 // Even though the exception does not apply to any filters yet, the objects |
| 315 // returned for foo.example.com and bar.example.com are not one and the same. |
| 316 test.notEqual(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 // Even though the exception does not apply to any filters yet, the objects |
| 334 // returned for foo.example.com and bar.example.com are not one and the same. |
| 335 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"), |
| 336 ElemHide.getSelectorsForDomain("bar.example.com")); |
| 337 |
| 338 ElemHide.add(Filter.fromText("##test4")); |
| 339 |
| 340 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"), |
| 341 ElemHide.getSelectorsForDomain("bar.example.com")); |
| 342 |
| 343 ElemHide.remove(Filter.fromText("##test4")); |
| 344 ElemHideExceptions.remove(Filter.fromText("~foo.example.com#@#test4")); |
| 345 |
| 346 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), |
| 347 ElemHide.getSelectorsForDomain("bar.example.com")); |
| 348 |
| 349 // Generic exception. |
| 350 ElemHide.add(Filter.fromText("##test4")); |
| 351 ElemHideExceptions.add(Filter.fromText("#@#test4")); |
| 352 |
| 353 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), |
| 354 ElemHide.getSelectorsForDomain("bar.example.com")); |
| 355 |
| 356 ElemHideExceptions.add(Filter.fromText("example.com#@#test4")); |
| 357 |
| 358 // In this case, even though the exception applies to both domains, we get |
| 359 // different objects. |
| 360 test.notEqual(ElemHide.getSelectorsForDomain("foo.example.com"), |
| 361 ElemHide.getSelectorsForDomain("bar.example.com")); |
| 362 test.deepEqual(ElemHide.getSelectorsForDomain("foo.example.com"), |
| 363 ElemHide.getSelectorsForDomain("bar.example.com")); |
| 364 |
| 365 ElemHideExceptions.remove(Filter.fromText("example.com#@#test4")); |
| 366 |
| 367 // Generic exception with unrelated domain excluded. |
| 368 ElemHideExceptions.add(Filter.fromText("~x4aabd7c0ddcd.com#@#test4")); |
| 369 |
| 370 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), |
| 371 ElemHide.getSelectorsForDomain("bar.example.com")); |
| 372 |
| 373 ElemHideExceptions.remove(Filter.fromText("~x4aabd7c0ddcd.com#@#test4")); |
| 374 ElemHideExceptions.remove(Filter.fromText("#@#test4")); |
| 375 ElemHide.remove(Filter.fromText("##test4")); |
292 | 376 |
293 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | 377 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), |
294 ElemHide.getSelectorsForDomain("bar.example.com")); | 378 ElemHide.getSelectorsForDomain("bar.example.com")); |
295 | 379 |
296 ElemHide.remove(Filter.fromText("##test1")); | 380 ElemHide.remove(Filter.fromText("##test1")); |
297 ElemHide.remove(Filter.fromText("##test2")); | 381 ElemHide.remove(Filter.fromText("##test2")); |
298 ElemHide.remove(Filter.fromText("##test3")); | 382 ElemHide.remove(Filter.fromText("##test3")); |
299 | 383 |
300 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), | 384 test.equal(ElemHide.getSelectorsForDomain("foo.example.com"), |
301 ElemHide.getSelectorsForDomain("bar.example.com")); | 385 ElemHide.getSelectorsForDomain("bar.example.com")); |
302 | 386 |
303 test.done(); | 387 test.done(); |
304 }; | 388 }; |
LEFT | RIGHT |