| 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 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 | 293 |
| 294 /** | 294 /** |
| 295 * Checks whether the entries for a particular keyword match a URL | 295 * Checks whether the entries for a particular keyword match a URL |
| 296 * @param {string} keyword | 296 * @param {string} keyword |
| 297 * @param {string} location | 297 * @param {string} location |
| 298 * @param {number} typeMask | 298 * @param {number} typeMask |
| 299 * @param {string} [docDomain] | 299 * @param {string} [docDomain] |
| 300 * @param {boolean} [thirdParty] | 300 * @param {boolean} [thirdParty] |
| 301 * @param {string} [sitekey] | 301 * @param {string} [sitekey] |
| 302 * @param {boolean} [specificOnly] | 302 * @param {boolean} [specificOnly] |
| 303 * @param {Array.<Filter>} collection | |
|
hub
2019/01/25 20:52:57
I think this parameter deserve a little bit of exp
Manish Jethani
2019/01/28 17:37:08
Done.
| |
| 303 * @returns {?Filter} | 304 * @returns {?Filter} |
| 304 * @protected | 305 * @protected |
| 305 */ | 306 */ |
| 306 checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey, | 307 checkEntryMatch(keyword, location, typeMask, docDomain, thirdParty, sitekey, |
| 307 specificOnly) | 308 specificOnly, collection) |
| 308 { | 309 { |
| 309 // We need to skip the simple (location-only) filters if the type mask does | 310 // We need to skip the simple (location-only) filters if the type mask does |
| 310 // not contain any default content types. | 311 // not contain any default content types. |
| 311 if ((typeMask & DEFAULT_TYPES) != 0) | 312 if ((typeMask & DEFAULT_TYPES) != 0) |
| 312 { | 313 { |
| 313 let simpleSet = this._simpleFiltersByKeyword.get(keyword); | 314 let simpleSet = this._simpleFiltersByKeyword.get(keyword); |
| 314 if (simpleSet) | 315 if (simpleSet) |
| 315 { | 316 { |
| 316 for (let filter of simpleSet) | 317 for (let filter of simpleSet) |
| 317 { | 318 { |
| 318 if (specificOnly && !(filter instanceof WhitelistFilter)) | 319 if (specificOnly && !(filter instanceof WhitelistFilter)) |
| 319 continue; | 320 continue; |
| 320 | 321 |
| 321 if (filter.matchesLocation(location)) | 322 if (filter.matchesLocation(location)) |
| 322 return filter; | 323 { |
| 324 if (!collection) | |
| 325 return filter; | |
| 326 | |
| 327 collection.push(filter); | |
| 328 } | |
| 323 } | 329 } |
| 324 } | 330 } |
| 325 } | 331 } |
| 326 | 332 |
| 327 let complexSet = null; | 333 let complexSet = null; |
| 328 | 334 |
| 329 // If the type mask contains a non-default type (first condition) and it is | 335 // If the type mask contains a non-default type (first condition) and it is |
| 330 // the only type in the mask (second condition), we can use the | 336 // the only type in the mask (second condition), we can use the |
| 331 // type-specific map, which typically contains a lot fewer filters. This | 337 // type-specific map, which typically contains a lot fewer filters. This |
| 332 // enables faster lookups for whitelisting types like $document, $elemhide, | 338 // enables faster lookups for whitelisting types like $document, $elemhide, |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 344 | 350 |
| 345 if (complexSet) | 351 if (complexSet) |
| 346 { | 352 { |
| 347 for (let filter of complexSet) | 353 for (let filter of complexSet) |
| 348 { | 354 { |
| 349 if (specificOnly && filter.isGeneric() && | 355 if (specificOnly && filter.isGeneric() && |
| 350 !(filter instanceof WhitelistFilter)) | 356 !(filter instanceof WhitelistFilter)) |
| 351 continue; | 357 continue; |
| 352 | 358 |
| 353 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey)) | 359 if (filter.matches(location, typeMask, docDomain, thirdParty, sitekey)) |
| 354 return filter; | 360 { |
| 361 if (!collection) | |
| 362 return filter; | |
| 363 | |
| 364 collection.push(filter); | |
| 365 } | |
| 355 } | 366 } |
| 356 } | 367 } |
| 357 | 368 |
| 358 return null; | 369 return null; |
| 359 } | 370 } |
| 360 | 371 |
| 361 /** | 372 /** |
| 362 * Tests whether the URL matches any of the known filters | 373 * Tests whether the URL matches any of the known filters |
| 363 * @param {string} location | 374 * @param {string} location |
| 364 * URL to be tested | 375 * URL to be tested |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 523 { | 534 { |
| 524 whitelistHit = this._whitelist.checkEntryMatch(candidates[i], location, | 535 whitelistHit = this._whitelist.checkEntryMatch(candidates[i], location, |
| 525 typeMask, docDomain, | 536 typeMask, docDomain, |
| 526 thirdParty, sitekey); | 537 thirdParty, sitekey); |
| 527 } | 538 } |
| 528 } | 539 } |
| 529 | 540 |
| 530 return whitelistHit || blacklistHit; | 541 return whitelistHit || blacklistHit; |
| 531 } | 542 } |
| 532 | 543 |
| 544 _searchInternal(location, typeMask, docDomain, thirdParty, sitekey, | |
| 545 specificOnly, filterType) | |
| 546 { | |
| 547 let hits = {}; | |
| 548 | |
| 549 let searchBlocking = filterType == "blocking" || filterType == "all"; | |
| 550 let searchWhitelist = filterType == "whitelist" || filterType == "all"; | |
| 551 | |
| 552 if (searchBlocking) | |
| 553 hits.blocking = []; | |
| 554 | |
| 555 if (searchWhitelist) | |
| 556 hits.whitelist = []; | |
| 557 | |
| 558 // If the type mask includes no types other than whitelist-only types, we | |
| 559 // can skip the blacklist. | |
| 560 if ((typeMask & ~WHITELIST_ONLY_TYPES) == 0) | |
| 561 searchBlocking = false; | |
| 562 | |
| 563 let candidates = location.toLowerCase().match(/[a-z0-9%]{3,}/g); | |
| 564 if (candidates === null) | |
| 565 candidates = []; | |
| 566 candidates.push(""); | |
| 567 | |
| 568 for (let i = 0, l = candidates.length; i < l; i++) | |
| 569 { | |
| 570 if (searchBlocking) | |
| 571 { | |
| 572 this._blacklist.checkEntryMatch(candidates[i], location, typeMask, | |
| 573 docDomain, thirdParty, sitekey, | |
| 574 specificOnly, hits.blocking); | |
| 575 } | |
| 576 | |
| 577 if (searchWhitelist) | |
| 578 { | |
| 579 this._whitelist.checkEntryMatch(candidates[i], location, typeMask, | |
| 580 docDomain, thirdParty, sitekey, | |
| 581 false, hits.whitelist); | |
| 582 } | |
| 583 } | |
| 584 | |
| 585 return hits; | |
| 586 } | |
| 587 | |
| 533 /** | 588 /** |
| 534 * @see Matcher#matchesAny | 589 * @see Matcher#matchesAny |
| 535 * @inheritdoc | 590 * @inheritdoc |
| 536 */ | 591 */ |
| 537 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) | 592 matchesAny(location, typeMask, docDomain, thirdParty, sitekey, specificOnly) |
| 538 { | 593 { |
| 539 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + | 594 let key = location + " " + typeMask + " " + docDomain + " " + thirdParty + |
| 540 " " + sitekey + " " + specificOnly; | 595 " " + sitekey + " " + specificOnly; |
| 541 | 596 |
| 542 let result = this._resultCache.get(key); | 597 let result = this._resultCache.get(key); |
| 543 if (typeof result != "undefined") | 598 if (typeof result != "undefined") |
| 544 return result; | 599 return result; |
| 545 | 600 |
| 546 result = this._matchesAnyInternal(location, typeMask, docDomain, | 601 result = this._matchesAnyInternal(location, typeMask, docDomain, |
| 547 thirdParty, sitekey, specificOnly); | 602 thirdParty, sitekey, specificOnly); |
| 548 | 603 |
| 549 if (this._resultCache.size >= this.maxCacheEntries) | 604 if (this._resultCache.size >= this.maxCacheEntries) |
| 550 this._resultCache.clear(); | 605 this._resultCache.clear(); |
| 551 | 606 |
| 552 this._resultCache.set(key, result); | 607 this._resultCache.set(key, result); |
| 553 | 608 |
| 554 return result; | 609 return result; |
| 555 } | 610 } |
| 556 | 611 |
| 557 /** | 612 /** |
| 613 * @typedef {object} MatcherSearchResults | |
| 614 * @property {Array.<BlockingFilter>} [blocking] List of blocking filters | |
| 615 * found. | |
| 616 * @property {Array.<WhitelistFilter>} [whitelist] List of whitelist filters | |
| 617 * found. | |
| 618 */ | |
| 619 | |
| 620 /** | |
| 621 * Searches all blocking and whitelist filters and returns results matching | |
| 622 * the given parameters. | |
| 623 * | |
| 624 * @param {string} location | |
| 625 * @param {number} typeMask | |
| 626 * @param {string} [docDomain] | |
| 627 * @param {boolean} [thirdParty] | |
| 628 * @param {string} [sitekey] | |
| 629 * @param {boolean} [specificOnly] | |
| 630 * @param {string} [filterType] The types of filters to look for. This can be | |
| 631 * <code>"blocking"</code>, <code>"whitelist"</code>, or | |
| 632 * <code>"all"</code> (default). | |
| 633 * | |
| 634 * @returns {MatcherSearchResults} | |
| 635 */ | |
| 636 search(location, typeMask, docDomain, thirdParty, sitekey, specificOnly, | |
| 637 filterType = "all") | |
| 638 { | |
| 639 let key = "* " + location + " " + typeMask + " " + docDomain + " " + | |
| 640 thirdParty + " " + sitekey + " " + specificOnly + " " + | |
| 641 filterType; | |
| 642 | |
| 643 let result = this._resultCache.get(key); | |
| 644 if (typeof result != "undefined") | |
| 645 return result; | |
| 646 | |
| 647 result = this._searchInternal(location, typeMask, docDomain, thirdParty, | |
| 648 sitekey, specificOnly, filterType); | |
| 649 | |
| 650 if (this._resultCache.size >= this.maxCacheEntries) | |
| 651 this._resultCache.clear(); | |
| 652 | |
| 653 this._resultCache.set(key, result); | |
| 654 | |
| 655 return result; | |
| 656 } | |
| 657 | |
| 658 /** | |
| 558 * Tests whether the URL is whitelisted | 659 * Tests whether the URL is whitelisted |
| 559 * @see Matcher#matchesAny | 660 * @see Matcher#matchesAny |
| 560 * @inheritdoc | 661 * @inheritdoc |
| 561 * @returns {boolean} | 662 * @returns {boolean} |
| 562 */ | 663 */ |
| 563 isWhitelisted(location, typeMask, docDomain, thirdParty, sitekey, | 664 isWhitelisted(location, typeMask, docDomain, thirdParty, sitekey, |
| 564 specificOnly) | 665 specificOnly) |
| 565 { | 666 { |
| 566 return !!this._whitelist.matchesAny(location, typeMask, docDomain, | 667 return !!this._whitelist.matchesAny(location, typeMask, docDomain, |
| 567 thirdParty, sitekey, specificOnly); | 668 thirdParty, sitekey, specificOnly); |
| 568 } | 669 } |
| 569 } | 670 } |
| 570 | 671 |
| 571 exports.CombinedMatcher = CombinedMatcher; | 672 exports.CombinedMatcher = CombinedMatcher; |
| 572 | 673 |
| 573 /** | 674 /** |
| 574 * Shared {@link CombinedMatcher} instance that should usually be used. | 675 * Shared {@link CombinedMatcher} instance that should usually be used. |
| 575 * @type {CombinedMatcher} | 676 * @type {CombinedMatcher} |
| 576 */ | 677 */ |
| 577 let defaultMatcher = new CombinedMatcher(); | 678 let defaultMatcher = new CombinedMatcher(); |
| 578 | 679 |
| 579 exports.defaultMatcher = defaultMatcher; | 680 exports.defaultMatcher = defaultMatcher; |
| OLD | NEW |