Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: lib/filterClasses.js

Issue 29912636: Issue 7052 - Use string-based matching for literal patterns (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Move code back to lib/filterClasses.js Created Oct. 21, 2018, 11:46 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | test/filterClasses.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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} = require("./common");
27 27
28 /** 28 /**
29 * Regular expression used to match the <code>||</code> prefix in an otherwise
30 * literal pattern.
31 * @type {RegExp}
32 */
33 let doubleAnchorRegExp = new RegExp(filterToRegExp("||") + "$");
34
35 /**
29 * All known unique domain sources mapped to their parsed values. 36 * All known unique domain sources mapped to their parsed values.
30 * @type {Map.<string,Map.<string,boolean>>} 37 * @type {Map.<string,Map.<string,boolean>>}
31 */ 38 */
32 let knownDomainMaps = new Map(); 39 let knownDomainMaps = new Map();
33 40
34 /** 41 /**
42 * Checks whether the given pattern is a string of literal characters with no
43 * wildcards or any other special characters. If the pattern is prefixed with a
44 * <code>||</code> but otherwise contains no special characters, it is still
45 * considered to be a literal pattern.
46 * @param {string} pattern
47 * @returns {boolean}
48 */
49 function isLiteralPattern(pattern)
50 {
51 return !/[*^|]/.test(pattern.replace(/^\|{2}/, ""));
52 }
53
54 /**
35 * Abstract base class for filters 55 * Abstract base class for filters
36 * 56 *
37 * @param {string} text string representation of the filter 57 * @param {string} text string representation of the filter
38 * @constructor 58 * @constructor
39 */ 59 */
40 function Filter(text) 60 function Filter(text)
41 { 61 {
42 this.text = text; 62 this.text = text;
43 63
44 /** 64 /**
(...skipping 641 matching lines...) Expand 10 before | Expand all | Expand 10 after
686 regexpSource[regexpSource.length - 1] == "/") 706 regexpSource[regexpSource.length - 1] == "/")
687 { 707 {
688 // The filter is a regular expression - convert it immediately to 708 // The filter is a regular expression - convert it immediately to
689 // catch syntax errors 709 // catch syntax errors
690 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2), 710 let regexp = new RegExp(regexpSource.substr(1, regexpSource.length - 2),
691 this.matchCase ? "" : "i"); 711 this.matchCase ? "" : "i");
692 Object.defineProperty(this, "regexp", {value: regexp}); 712 Object.defineProperty(this, "regexp", {value: regexp});
693 } 713 }
694 else 714 else
695 { 715 {
716 if (!this.matchCase && isLiteralPattern(regexpSource))
717 regexpSource = regexpSource.toLowerCase();
718
696 // No need to convert this filter to regular expression yet, do it on demand 719 // No need to convert this filter to regular expression yet, do it on demand
697 this.pattern = regexpSource; 720 this.pattern = regexpSource;
698 } 721 }
699 } 722 }
700 exports.RegExpFilter = RegExpFilter; 723 exports.RegExpFilter = RegExpFilter;
701 724
702 RegExpFilter.prototype = extend(ActiveFilter, { 725 RegExpFilter.prototype = extend(ActiveFilter, {
703 /** 726 /**
704 * Number of filters contained, will always be 1 (required to 727 * Number of filters contained, will always be 1 (required to
705 * optimize {@link Matcher}). 728 * optimize {@link Matcher}).
(...skipping 11 matching lines...) Expand all
717 * for delayed creation of the regexp property 740 * for delayed creation of the regexp property
718 * @type {?string} 741 * @type {?string}
719 */ 742 */
720 pattern: null, 743 pattern: null,
721 /** 744 /**
722 * Regular expression to be used when testing against this filter 745 * Regular expression to be used when testing against this filter
723 * @type {RegExp} 746 * @type {RegExp}
724 */ 747 */
725 get regexp() 748 get regexp()
726 { 749 {
727 let source = filterToRegExp(this.pattern, this.rewrite != null); 750 let value = null;
728 let regexp = new RegExp(source, this.matchCase ? "" : "i"); 751
729 Object.defineProperty(this, "regexp", {value: regexp}); 752 let {pattern, rewrite} = this;
730 return regexp; 753 if (rewrite != null || !isLiteralPattern(pattern))
754 {
755 value = new RegExp(filterToRegExp(pattern, rewrite != null),
756 this.matchCase ? "" : "i");
757 }
758
759 Object.defineProperty(this, "regexp", {value});
760 return value;
731 }, 761 },
732 /** 762 /**
733 * Content types the filter applies to, combination of values from 763 * Content types the filter applies to, combination of values from
734 * RegExpFilter.typeMap 764 * RegExpFilter.typeMap
735 * @type {number} 765 * @type {number}
736 */ 766 */
737 contentType: 0x7FFFFFFF, 767 contentType: 0x7FFFFFFF,
738 /** 768 /**
739 * Defines whether the filter should distinguish between lower and 769 * Defines whether the filter should distinguish between lower and
740 * upper case letters 770 * upper case letters
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
781 * @param {boolean} [thirdParty] should be true if the URL is a third-party 811 * @param {boolean} [thirdParty] should be true if the URL is a third-party
782 * request 812 * request
783 * @param {string} [sitekey] public key provided by the document 813 * @param {string} [sitekey] public key provided by the document
784 * @return {boolean} true in case of a match 814 * @return {boolean} true in case of a match
785 */ 815 */
786 matches(location, typeMask, docDomain, thirdParty, sitekey) 816 matches(location, typeMask, docDomain, thirdParty, sitekey)
787 { 817 {
788 return (this.contentType & typeMask) != 0 && 818 return (this.contentType & typeMask) != 0 &&
789 (this.thirdParty == null || this.thirdParty == thirdParty) && 819 (this.thirdParty == null || this.thirdParty == thirdParty) &&
790 this.isActiveOnDomain(docDomain, sitekey) && 820 this.isActiveOnDomain(docDomain, sitekey) &&
791 this.regexp.test(location); 821 this.matchesLocation(location);
822 },
823
824 /**
825 * Checks whether the given URL matches this filter's pattern.
826 * @param {string} location The URL to check.
827 * @returns {boolean} <code>true</code> if the URL matches.
828 */
829 matchesLocation(location)
830 {
831 let {regexp} = this;
832
833 if (regexp)
834 return regexp.test(location);
835
836 if (!this.matchCase)
837 location = location.toLowerCase();
838
839 let {pattern} = this;
840
841 if (pattern[0] == "|" && pattern[1] == "|")
842 {
843 let index = location.indexOf(pattern.substring(2));
844
845 // The "||" prefix requires that the text that follows does not start
846 // with a forward slash.
847 return index != -1 && location[index] != "/" &&
848 doubleAnchorRegExp.test(location.substring(0, index));
849 }
850
851 return location.includes(pattern);
792 } 852 }
793 }); 853 });
794 854
795 /** 855 /**
796 * Yields the filter itself (required to optimize {@link Matcher}). 856 * Yields the filter itself (required to optimize {@link Matcher}).
797 * @yields {RegExpFilter} 857 * @yields {RegExpFilter}
798 */ 858 */
799 RegExpFilter.prototype[Symbol.iterator] = function*() 859 RegExpFilter.prototype[Symbol.iterator] = function*()
800 { 860 {
801 yield this; 861 yield this;
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
1276 1336
1277 /** 1337 /**
1278 * Script that should be executed 1338 * Script that should be executed
1279 * @type {string} 1339 * @type {string}
1280 */ 1340 */
1281 get script() 1341 get script()
1282 { 1342 {
1283 return this.body; 1343 return this.body;
1284 } 1344 }
1285 }); 1345 });
OLDNEW
« no previous file with comments | « no previous file | test/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld