OLD | NEW |
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. Normally this is part of the generated regular |
| 58 // expression, but since we're faking it here with string-based matching we |
| 59 // need to do the check ourselves. |
| 60 return index != -1 && location[index] != "/" && |
| 61 doubleAnchorRegExp.test(location.substring(0, index)); |
| 62 } |
| 63 |
| 64 return location.includes(pattern); |
| 65 } |
| 66 |
| 67 exports.locationMatchesPattern = locationMatchesPattern; |
| 68 |
| 69 /** |
21 * Converts raw text into a regular expression string | 70 * Converts raw text into a regular expression string |
22 * @param {string} text the string to convert | 71 * @param {string} text the string to convert |
23 * @return {string} regular expression representation of the text | 72 * @return {string} regular expression representation of the text |
24 */ | 73 */ |
25 function textToRegExp(text) | 74 function textToRegExp(text) |
26 { | 75 { |
27 return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"); | 76 return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"); |
28 } | 77 } |
29 | 78 |
30 exports.textToRegExp = textToRegExp; | 79 exports.textToRegExp = textToRegExp; |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 // Note that the first group in the regular expression is optional. If it | 256 // 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. | 257 // doesn't match (e.g. "#foo::nth-child(1)"), type will be an empty string. |
209 qualifiedSelector += sub.substr(0, index) + type + qualifier + rest; | 258 qualifiedSelector += sub.substr(0, index) + type + qualifier + rest; |
210 } | 259 } |
211 | 260 |
212 // Remove the initial comma and space. | 261 // Remove the initial comma and space. |
213 return qualifiedSelector.substr(2); | 262 return qualifiedSelector.substr(2); |
214 } | 263 } |
215 | 264 |
216 exports.qualifySelector = qualifySelector; | 265 exports.qualifySelector = qualifySelector; |
OLD | NEW |