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

Side by Side Diff: lib/filterClasses.js

Issue 29978573: Issue 7209 - Treat patterns with trailing separators as literals (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Created Jan. 10, 2019, 10:08 p.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 | no next file » | 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
(...skipping 15 matching lines...) Expand all
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 29 * Regular expression used to match the <code>||</code> prefix in an otherwise
30 * literal pattern. 30 * literal pattern.
31 * @type {RegExp} 31 * @type {RegExp}
32 */ 32 */
33 let doubleAnchorRegExp = new RegExp(filterToRegExp("||") + "$"); 33 let doubleAnchorRegExp = new RegExp(filterToRegExp("||") + "$");
34 34
35 /** 35 /**
36 * Regular expression used to match the <code>^</code> suffix in an otherwise
37 * literal pattern.
38 * @type {RegExp}
39 */
40 // Note: This should match the pattern in lib/common.js
41 let separatorRegExp = /[\x00-\x24\x26-\x2C\x2F\x3A-\x40\x5B-\x5E\x60\x7B-\x7F]/;
42
43 /**
36 * All known unique domain sources mapped to their parsed values. 44 * All known unique domain sources mapped to their parsed values.
37 * @type {Map.<string,Map.<string,boolean>>} 45 * @type {Map.<string,Map.<string,boolean>>}
38 */ 46 */
39 let knownDomainMaps = new Map(); 47 let knownDomainMaps = new Map();
40 48
41 /** 49 /**
42 * Checks whether the given pattern is a string of literal characters with no 50 * 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 51 * 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 52 * <code>||</code> or suffixed with a <code>^</code> but otherwise contains no
45 * considered to be a literal pattern. 53 * special characters, it is still considered to be a literal pattern.
46 * @param {string} pattern 54 * @param {string} pattern
47 * @returns {boolean} 55 * @returns {boolean}
48 */ 56 */
49 function isLiteralPattern(pattern) 57 function isLiteralPattern(pattern)
50 { 58 {
51 return !/[*^|]/.test(pattern.replace(/^\|{2}/, "")); 59 return !/[*^|]/.test(pattern.replace(/^\|{2}/, "").replace(/\^$/, ""));
52 } 60 }
53 61
54 /** 62 /**
55 * Abstract base class for filters 63 * Abstract base class for filters
56 * 64 *
57 * @param {string} text string representation of the filter 65 * @param {string} text string representation of the filter
58 * @constructor 66 * @constructor
59 */ 67 */
60 function Filter(text) 68 function Filter(text)
61 { 69 {
(...skipping 773 matching lines...) Expand 10 before | Expand all | Expand 10 after
835 let {regexp} = this; 843 let {regexp} = this;
836 844
837 if (regexp) 845 if (regexp)
838 return regexp.test(location); 846 return regexp.test(location);
839 847
840 if (!this.matchCase) 848 if (!this.matchCase)
841 location = location.toLowerCase(); 849 location = location.toLowerCase();
842 850
843 let {pattern} = this; 851 let {pattern} = this;
844 852
845 if (pattern[0] == "|" && pattern[1] == "|") 853 let startsWithDoubleAnchor = pattern[0] == "|" && pattern[1] == "|";
846 { 854 let endsWithSeparator = pattern[pattern.length - 1] == "^";
847 let index = location.indexOf(pattern.substring(2));
848 855
849 // The "||" prefix requires that the text that follows does not start 856 if (startsWithDoubleAnchor)
850 // with a forward slash. 857 pattern = pattern.substr(2);
851 return index != -1 && location[index] != "/" &&
852 doubleAnchorRegExp.test(location.substring(0, index));
853 }
854 858
855 return location.includes(pattern); 859 if (endsWithSeparator)
860 pattern = pattern.slice(0, -1);
861
862 let index = location.indexOf(pattern);
863
864 // The "||" prefix requires that the text that follows does not start
865 // with a forward slash.
866 return index != -1 &&
867 (!startsWithDoubleAnchor || location[index] != "/" &&
868 doubleAnchorRegExp.test(location.substring(0, index))) &&
869 (!endsWithSeparator || !location[index + pattern.length] ||
870 separatorRegExp.test(location[index + pattern.length]));
856 }, 871 },
857 872
858 /** 873 /**
859 * Checks whether this filter has only a URL pattern and no content type, 874 * Checks whether this filter has only a URL pattern and no content type,
860 * third-party flag, domains, or sitekeys. 875 * third-party flag, domains, or sitekeys.
861 * @returns {boolean} 876 * @returns {boolean}
862 */ 877 */
863 isLocationOnly() 878 isLocationOnly()
864 { 879 {
865 return this.contentType == RegExpFilter.prototype.contentType && 880 return this.contentType == RegExpFilter.prototype.contentType &&
(...skipping 487 matching lines...) Expand 10 before | Expand all | Expand 10 after
1353 1368
1354 /** 1369 /**
1355 * Script that should be executed 1370 * Script that should be executed
1356 * @type {string} 1371 * @type {string}
1357 */ 1372 */
1358 get script() 1373 get script()
1359 { 1374 {
1360 return this.body; 1375 return this.body;
1361 } 1376 }
1362 }); 1377 });
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld