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 |
(...skipping 14 matching lines...) Expand all Loading... |
25 function textToRegExp(text) | 25 function textToRegExp(text) |
26 { | 26 { |
27 return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"); | 27 return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"); |
28 } | 28 } |
29 | 29 |
30 exports.textToRegExp = textToRegExp; | 30 exports.textToRegExp = textToRegExp; |
31 | 31 |
32 /** | 32 /** |
33 * Converts filter text into regular expression string | 33 * Converts filter text into regular expression string |
34 * @param {string} text as in Filter() | 34 * @param {string} text as in Filter() |
| 35 * @param {boolean} [optimize=true] whether to optimize the regular expression |
35 * @return {string} regular expression representation of filter text | 36 * @return {string} regular expression representation of filter text |
36 */ | 37 */ |
37 function filterToRegExp(text) | 38 function filterToRegExp(text, optimize = true) |
38 { | 39 { |
| 40 if (optimize) |
| 41 { |
| 42 // remove multiple wildcards |
| 43 text = text.replace(/\*+/g, "*"); |
| 44 |
| 45 // remove leading wildcard |
| 46 if (text[0] == "*") |
| 47 text = text.substring(1); |
| 48 |
| 49 // remove trailing wildcard |
| 50 if (text[text.length - 1] == "*") |
| 51 text = text.substring(0, text.length - 1); |
| 52 } |
| 53 |
39 return text | 54 return text |
40 // remove multiple wildcards | |
41 .replace(/\*+/g, "*") | |
42 // remove anchors following separator placeholder | 55 // remove anchors following separator placeholder |
43 .replace(/\^\|$/, "^") | 56 .replace(/\^\|$/, "^") |
44 // escape special symbols | 57 // escape special symbols |
45 .replace(/\W/g, "\\$&") | 58 .replace(/\W/g, "\\$&") |
46 // replace wildcards by .* | 59 // replace wildcards by .* |
47 .replace(/\\\*/g, ".*") | 60 .replace(/\\\*/g, ".*") |
48 // process separator placeholders (all ANSI characters but alphanumeric | 61 // process separator placeholders (all ANSI characters but alphanumeric |
49 // characters and _%.-) | 62 // characters and _%.-) |
50 .replace(/\\\^/g, "(?:[\\x00-\\x24\\x26-\\x2C\\x2F\\x3A-\\x40\\x5B-\\x5E\\x6
0\\x7B-\\x7F]|$)") | 63 .replace(/\\\^/g, "(?:[\\x00-\\x24\\x26-\\x2C\\x2F\\x3A-\\x40\\x5B-\\x5E\\x6
0\\x7B-\\x7F]|$)") |
51 // process extended anchor at expression start | 64 // process extended anchor at expression start |
52 .replace(/^\\\|\\\|/, "^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?") | 65 .replace(/^\\\|\\\|/, "^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?") |
53 // process anchor at expression start | 66 // process anchor at expression start |
54 .replace(/^\\\|/, "^") | 67 .replace(/^\\\|/, "^") |
55 // process anchor at expression end | 68 // process anchor at expression end |
56 .replace(/\\\|$/, "$") | 69 .replace(/\\\|$/, "$"); |
57 // remove leading wildcards | |
58 .replace(/^(\.\*)/, "") | |
59 // remove trailing wildcards | |
60 .replace(/(\.\*)$/, ""); | |
61 } | 70 } |
62 | 71 |
63 exports.filterToRegExp = filterToRegExp; | 72 exports.filterToRegExp = filterToRegExp; |
64 | 73 |
65 function splitSelector(selector) | 74 function splitSelector(selector) |
66 { | 75 { |
67 if (!selector.includes(",")) | 76 if (!selector.includes(",")) |
68 return [selector]; | 77 return [selector]; |
69 | 78 |
70 let selectors = []; | 79 let selectors = []; |
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 // Note that the first group in the regular expression is optional. If it | 205 // Note that the first group in the regular expression is optional. If it |
197 // doesn't match (e.g. "#foo::nth-child(1)"), type will be an empty string. | 206 // doesn't match (e.g. "#foo::nth-child(1)"), type will be an empty string. |
198 qualifiedSelector += sub.substr(0, index) + type + qualifier + rest; | 207 qualifiedSelector += sub.substr(0, index) + type + qualifier + rest; |
199 } | 208 } |
200 | 209 |
201 // Remove the initial comma and space. | 210 // Remove the initial comma and space. |
202 return qualifiedSelector.substr(2); | 211 return qualifiedSelector.substr(2); |
203 } | 212 } |
204 | 213 |
205 exports.qualifySelector = qualifySelector; | 214 exports.qualifySelector = qualifySelector; |
OLD | NEW |