| 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 |
| 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 /** | 20 /** |
| 21 * @fileOverview Definition of Filter class and its subclasses. | 21 * @fileOverview Definition of Filter class and its subclasses. |
| 22 */ | 22 */ |
| 23 | 23 |
| 24 const {filterNotifier} = require("./filterNotifier"); | 24 const {filterNotifier} = require("./filterNotifier"); |
| 25 const {extend} = require("./coreUtils"); | 25 const {extend} = require("./coreUtils"); |
| 26 const {filterToRegExp} = require("./common"); | 26 const {filterToRegExp, isLiteralPattern, |
| 27 locationMatchesPattern} = require("./common"); |
| 27 | 28 |
| 28 /** | 29 /** |
| 29 * All known unique domain sources mapped to their parsed values. | 30 * All known unique domain sources mapped to their parsed values. |
| 30 * @type {Map.<string,Map.<string,boolean>>} | 31 * @type {Map.<string,Map.<string,boolean>>} |
| 31 */ | 32 */ |
| 32 let knownDomainMaps = new Map(); | 33 let knownDomainMaps = new Map(); |
| 33 | 34 |
| 34 /** | 35 /** |
| 35 * Abstract base class for filters | 36 * Abstract base class for filters |
| 36 * | 37 * |
| (...skipping 649 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 686 regexpSource[regexpSource.length - 1] == "/") | 687 regexpSource[regexpSource.length - 1] == "/") |
| 687 { | 688 { |
| 688 // The filter is a regular expression - convert it immediately to | 689 // The filter is a regular expression - convert it immediately to |
| 689 // catch syntax errors | 690 // catch syntax errors |
| 690 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), | 691 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), |
| 691 this.matchCase ? "" : "i"); | 692 this.matchCase ? "" : "i"); |
| 692 Object.defineProperty(this, "regexp", {value: regexp}); | 693 Object.defineProperty(this, "regexp", {value: regexp}); |
| 693 } | 694 } |
| 694 else | 695 else |
| 695 { | 696 { |
| 697 if (!this.matchCase && isLiteralPattern(regexpSource)) |
| 698 regexpSource = regexpSource.toLowerCase(); |
| 699 |
| 696 // No need to convert this filter to regular expression yet, do it on demand | 700 // No need to convert this filter to regular expression yet, do it on demand |
| 697 this.pattern = regexpSource; | 701 this.pattern = regexpSource; |
| 698 } | 702 } |
| 699 } | 703 } |
| 700 exports.RegExpFilter = RegExpFilter; | 704 exports.RegExpFilter = RegExpFilter; |
| 701 | 705 |
| 702 RegExpFilter.prototype = extend(ActiveFilter, { | 706 RegExpFilter.prototype = extend(ActiveFilter, { |
| 703 /** | 707 /** |
| 704 * Number of filters contained, will always be 1 (required to | 708 * Number of filters contained, will always be 1 (required to |
| 705 * optimize {@link Matcher}). | 709 * optimize {@link Matcher}). |
| (...skipping 11 matching lines...) Expand all Loading... |
| 717 * for delayed creation of the regexp property | 721 * for delayed creation of the regexp property |
| 718 * @type {?string} | 722 * @type {?string} |
| 719 */ | 723 */ |
| 720 pattern: null, | 724 pattern: null, |
| 721 /** | 725 /** |
| 722 * Regular expression to be used when testing against this filter | 726 * Regular expression to be used when testing against this filter |
| 723 * @type {RegExp} | 727 * @type {RegExp} |
| 724 */ | 728 */ |
| 725 get regexp() | 729 get regexp() |
| 726 { | 730 { |
| 727 let source = filterToRegExp(this.pattern, this.rewrite != null); | 731 let value = null; |
| 728 let regexp = new RegExp(source, this.matchCase ? "" : "i"); | 732 |
| 729 Object.defineProperty(this, "regexp", {value: regexp}); | 733 let {pattern, rewrite} = this; |
| 730 return regexp; | 734 if (rewrite != null || !isLiteralPattern(pattern)) |
| 735 { |
| 736 value = new RegExp(filterToRegExp(pattern, rewrite != null), |
| 737 this.matchCase ? "" : "i"); |
| 738 } |
| 739 |
| 740 Object.defineProperty(this, "regexp", {value}); |
| 741 return value; |
| 731 }, | 742 }, |
| 732 /** | 743 /** |
| 733 * Content types the filter applies to, combination of values from | 744 * Content types the filter applies to, combination of values from |
| 734 * RegExpFilter.typeMap | 745 * RegExpFilter.typeMap |
| 735 * @type {number} | 746 * @type {number} |
| 736 */ | 747 */ |
| 737 contentType: 0x7FFFFFFF, | 748 contentType: 0x7FFFFFFF, |
| 738 /** | 749 /** |
| 739 * Defines whether the filter should distinguish between lower and | 750 * Defines whether the filter should distinguish between lower and |
| 740 * upper case letters | 751 * upper case letters |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 781 * @param {boolean} [thirdParty] should be true if the URL is a third-party | 792 * @param {boolean} [thirdParty] should be true if the URL is a third-party |
| 782 * request | 793 * request |
| 783 * @param {string} [sitekey] public key provided by the document | 794 * @param {string} [sitekey] public key provided by the document |
| 784 * @return {boolean} true in case of a match | 795 * @return {boolean} true in case of a match |
| 785 */ | 796 */ |
| 786 matches(location, typeMask, docDomain, thirdParty, sitekey) | 797 matches(location, typeMask, docDomain, thirdParty, sitekey) |
| 787 { | 798 { |
| 788 return (this.contentType & typeMask) != 0 && | 799 return (this.contentType & typeMask) != 0 && |
| 789 (this.thirdParty == null || this.thirdParty == thirdParty) && | 800 (this.thirdParty == null || this.thirdParty == thirdParty) && |
| 790 this.isActiveOnDomain(docDomain, sitekey) && | 801 this.isActiveOnDomain(docDomain, sitekey) && |
| 791 this.regexp.test(location); | 802 this.matchesLocation(location); |
| 803 }, |
| 804 |
| 805 /** |
| 806 * Checks whether the given URL matches this filter's pattern. |
| 807 * @param {string} location The URL to check. |
| 808 * @returns {boolean} <code>true</code> if the URL matches. |
| 809 */ |
| 810 matchesLocation(location) |
| 811 { |
| 812 if (this.regexp) |
| 813 return this.regexp.test(location); |
| 814 |
| 815 if (!this.matchCase) |
| 816 location = location.toLowerCase(); |
| 817 |
| 818 return locationMatchesPattern(location, this.pattern); |
| 792 } | 819 } |
| 793 }); | 820 }); |
| 794 | 821 |
| 795 /** | 822 /** |
| 796 * Yields the filter itself (required to optimize {@link Matcher}). | 823 * Yields the filter itself (required to optimize {@link Matcher}). |
| 797 * @yields {RegExpFilter} | 824 * @yields {RegExpFilter} |
| 798 */ | 825 */ |
| 799 RegExpFilter.prototype[Symbol.iterator] = function*() | 826 RegExpFilter.prototype[Symbol.iterator] = function*() |
| 800 { | 827 { |
| 801 yield this; | 828 yield this; |
| (...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1276 | 1303 |
| 1277 /** | 1304 /** |
| 1278 * Script that should be executed | 1305 * Script that should be executed |
| 1279 * @type {string} | 1306 * @type {string} |
| 1280 */ | 1307 */ |
| 1281 get script() | 1308 get script() |
| 1282 { | 1309 { |
| 1283 return this.body; | 1310 return this.body; |
| 1284 } | 1311 } |
| 1285 }); | 1312 }); |
| OLD | NEW |