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: Remove unnecessary comment Created Oct. 19, 2018, 9:52 p.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, isLiteralPattern, 26 const {filterToRegExp} = require("./common");
27 locationMatchesPattern} = require("./common"); 27
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("||") + "$");
28 34
29 /** 35 /**
30 * All known unique domain sources mapped to their parsed values. 36 * All known unique domain sources mapped to their parsed values.
31 * @type {Map.<string,Map.<string,boolean>>} 37 * @type {Map.<string,Map.<string,boolean>>}
32 */ 38 */
33 let knownDomainMaps = new Map(); 39 let knownDomainMaps = new Map();
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 */
49 function isLiteralPattern(pattern)
50 {
51 return !/[*^|]/.test(pattern.replace(/^\|{2}/, ""));
52 }
34 53
35 /** 54 /**
36 * Abstract base class for filters 55 * Abstract base class for filters
37 * 56 *
38 * @param {string} text string representation of the filter 57 * @param {string} text string representation of the filter
39 * @constructor 58 * @constructor
40 */ 59 */
41 function Filter(text) 60 function Filter(text)
42 { 61 {
43 this.text = text; 62 this.text = text;
(...skipping 758 matching lines...) Expand 10 before | Expand all | Expand 10 after
802 this.matchesLocation(location); 821 this.matchesLocation(location);
803 }, 822 },
804 823
805 /** 824 /**
806 * Checks whether the given URL matches this filter's pattern. 825 * Checks whether the given URL matches this filter's pattern.
807 * @param {string} location The URL to check. 826 * @param {string} location The URL to check.
808 * @returns {boolean} <code>true</code> if the URL matches. 827 * @returns {boolean} <code>true</code> if the URL matches.
809 */ 828 */
810 matchesLocation(location) 829 matchesLocation(location)
811 { 830 {
812 if (this.regexp) 831 let {regexp} = this;
813 return this.regexp.test(location); 832
833 if (regexp)
834 return regexp.test(location);
814 835
815 if (!this.matchCase) 836 if (!this.matchCase)
816 location = location.toLowerCase(); 837 location = location.toLowerCase();
817 838
818 return locationMatchesPattern(location, this.pattern); 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);
819 } 852 }
820 }); 853 });
821 854
822 /** 855 /**
823 * Yields the filter itself (required to optimize {@link Matcher}). 856 * Yields the filter itself (required to optimize {@link Matcher}).
824 * @yields {RegExpFilter} 857 * @yields {RegExpFilter}
825 */ 858 */
826 RegExpFilter.prototype[Symbol.iterator] = function*() 859 RegExpFilter.prototype[Symbol.iterator] = function*()
827 { 860 {
828 yield this; 861 yield this;
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after
1303 1336
1304 /** 1337 /**
1305 * Script that should be executed 1338 * Script that should be executed
1306 * @type {string} 1339 * @type {string}
1307 */ 1340 */
1308 get script() 1341 get script()
1309 { 1342 {
1310 return this.body; 1343 return this.body;
1311 } 1344 }
1312 }); 1345 });
LEFTRIGHT

Powered by Google App Engine
This is Rietveld