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

Delta Between Two Patch Sets: lib/filterClasses.js

Issue 29912636: Issue 7052 - Use string-based matching for literal patterns (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Abstract literal pattern test into separate function Created Oct. 17, 2018, 2:45 a.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | test/filterClasses.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 let tripleAnchorRegExp = new RegExp(filterToRegExp("|||")); 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("||") + "$");
29 34
30 /** 35 /**
31 * All known unique domain sources mapped to their parsed values. 36 * All known unique domain sources mapped to their parsed values.
32 * @type {Map.<string,Map.<string,boolean>>} 37 * @type {Map.<string,Map.<string,boolean>>}
33 */ 38 */
34 let knownDomainMaps = new Map(); 39 let knownDomainMaps = new Map();
35 40
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 */
36 function isLiteralPattern(pattern) 49 function isLiteralPattern(pattern)
37 { 50 {
38 return !/[*^|]/.test(pattern.replace(/^\|{2}/, "")); 51 return !/[*^|]/.test(pattern.replace(/^\|{2}/, ""));
39 } 52 }
40 53
41 /** 54 /**
42 * Abstract base class for filters 55 * Abstract base class for filters
43 * 56 *
44 * @param {string} text string representation of the filter 57 * @param {string} text string representation of the filter
45 * @constructor 58 * @constructor
(...skipping 755 matching lines...) Expand 10 before | Expand all | Expand 10 after
801 * @return {boolean} true in case of a match 814 * @return {boolean} true in case of a match
802 */ 815 */
803 matches(location, typeMask, docDomain, thirdParty, sitekey) 816 matches(location, typeMask, docDomain, thirdParty, sitekey)
804 { 817 {
805 return (this.contentType & typeMask) != 0 && 818 return (this.contentType & typeMask) != 0 &&
806 (this.thirdParty == null || this.thirdParty == thirdParty) && 819 (this.thirdParty == null || this.thirdParty == thirdParty) &&
807 this.isActiveOnDomain(docDomain, sitekey) && 820 this.isActiveOnDomain(docDomain, sitekey) &&
808 this.matchesLocation(location); 821 this.matchesLocation(location);
809 }, 822 },
810 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 */
811 matchesLocation(location) 829 matchesLocation(location)
812 { 830 {
813 let {regexp} = this; 831 let {regexp} = this;
814 832
815 if (regexp) 833 if (regexp)
816 return regexp.test(location); 834 return regexp.test(location);
817 835
818 if (!this.matchCase) 836 if (!this.matchCase)
819 location = location.toLowerCase(); 837 location = location.toLowerCase();
820 838
821 let {pattern} = this; 839 let {pattern} = this;
822 840
823 if (pattern[0] == "|" && pattern[1] == "|") 841 if (pattern[0] == "|" && pattern[1] == "|")
824 { 842 {
825 let index = location.indexOf(pattern.substring(2)); 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.
826 return index != -1 && location[index] != "/" && 847 return index != -1 && location[index] != "/" &&
827 tripleAnchorRegExp.test(location.substring(0, index)); 848 doubleAnchorRegExp.test(location.substring(0, index));
828 } 849 }
829 850
830 return location.includes(pattern); 851 return location.includes(pattern);
831 } 852 }
832 }); 853 });
833 854
834 /** 855 /**
835 * Yields the filter itself (required to optimize {@link Matcher}). 856 * Yields the filter itself (required to optimize {@link Matcher}).
836 * @yields {RegExpFilter} 857 * @yields {RegExpFilter}
837 */ 858 */
(...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after
1315 1336
1316 /** 1337 /**
1317 * Script that should be executed 1338 * Script that should be executed
1318 * @type {string} 1339 * @type {string}
1319 */ 1340 */
1320 get script() 1341 get script()
1321 { 1342 {
1322 return this.body; 1343 return this.body;
1323 } 1344 }
1324 }); 1345 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld