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

Side by Side Diff: lib/common.js

Issue 29912636: Issue 7052 - Use string-based matching for literal patterns (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Remove unnecessary comment Created Oct. 19, 2018, 9:52 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 | lib/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 * Regular expression used to match the <code>||</code> prefix in an otherwise
22 * literal pattern.
23 * @type {RegExp}
24 */
25 let doubleAnchorRegExp = new RegExp(filterToRegExp("||") + "$");
26
27 /**
28 * Checks whether the given pattern is a string of literal characters with no
29 * wildcards or any other special characters. If the pattern is prefixed with a
30 * <code>||</code> but otherwise contains no special characters, it is still
31 * considered to be a literal pattern.
32 * @param {string} pattern
33 * @returns {boolean}
34 */
35 function isLiteralPattern(pattern)
36 {
37 return !/[*^|]/.test(pattern.replace(/^\|{2}/, ""));
38 }
39
40 exports.isLiteralPattern = isLiteralPattern;
41
42 /**
43 * Checks whether the given URL matches the given pattern.
44 * @param {string} location The URL to check.
45 * @param {string} pattern The pattern to match. This may be prefixed with a
46 * <code>||</code> to match the beginning of the URL. Any other characters
47 * are treated literally.
48 * @returns {boolean}
49 */
50 function locationMatchesPattern(location, pattern)
51 {
52 if (pattern[0] == "|" && pattern[1] == "|")
53 {
54 let index = location.indexOf(pattern.substring(2));
55
56 // The "||" prefix requires that the text that follows does not start with
57 // a forward slash.
58 return index != -1 && location[index] != "/" &&
59 doubleAnchorRegExp.test(location.substring(0, index));
60 }
61
62 return location.includes(pattern);
63 }
64
65 exports.locationMatchesPattern = locationMatchesPattern;
66
67 /**
21 * Converts raw text into a regular expression string 68 * Converts raw text into a regular expression string
22 * @param {string} text the string to convert 69 * @param {string} text the string to convert
23 * @return {string} regular expression representation of the text 70 * @return {string} regular expression representation of the text
24 */ 71 */
25 function textToRegExp(text) 72 function textToRegExp(text)
26 { 73 {
27 return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"); 74 return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
28 } 75 }
29 76
30 exports.textToRegExp = textToRegExp; 77 exports.textToRegExp = textToRegExp;
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 // Note that the first group in the regular expression is optional. If it 254 // Note that the first group in the regular expression is optional. If it
208 // doesn't match (e.g. "#foo::nth-child(1)"), type will be an empty string. 255 // doesn't match (e.g. "#foo::nth-child(1)"), type will be an empty string.
209 qualifiedSelector += sub.substr(0, index) + type + qualifier + rest; 256 qualifiedSelector += sub.substr(0, index) + type + qualifier + rest;
210 } 257 }
211 258
212 // Remove the initial comma and space. 259 // Remove the initial comma and space.
213 return qualifiedSelector.substr(2); 260 return qualifiedSelector.substr(2);
214 } 261 }
215 262
216 exports.qualifySelector = qualifySelector; 263 exports.qualifySelector = qualifySelector;
OLDNEW
« no previous file with comments | « no previous file | lib/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld