Index: lib/common.js |
=================================================================== |
--- a/lib/common.js |
+++ b/lib/common.js |
@@ -13,16 +13,65 @@ |
* |
* You should have received a copy of the GNU General Public License |
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
*/ |
"use strict"; |
/** |
+ * Regular expression used to match the <code>||</code> prefix in an otherwise |
+ * literal pattern. |
+ * @type {RegExp} |
+ */ |
+let doubleAnchorRegExp = new RegExp(filterToRegExp("||") + "$"); |
+ |
+/** |
+ * Checks whether the given pattern is a string of literal characters with no |
+ * wildcards or any other special characters. If the pattern is prefixed with a |
+ * <code>||</code> but otherwise contains no special characters, it is still |
+ * considered to be a literal pattern. |
+ * @param {string} pattern |
+ * @returns {boolean} |
+ */ |
+function isLiteralPattern(pattern) |
+{ |
+ return !/[*^|]/.test(pattern.replace(/^\|{2}/, "")); |
+} |
+ |
+exports.isLiteralPattern = isLiteralPattern; |
+ |
+/** |
+ * Checks whether the given URL matches the given pattern. |
+ * @param {string} location The URL to check. |
+ * @param {string} pattern The pattern to match. This may be prefixed with a |
+ * <code>||</code> to match the beginning of the URL. Any other characters |
+ * are treated literally. |
+ * @returns {boolean} |
+ */ |
+function locationMatchesPattern(location, pattern) |
+{ |
+ if (pattern[0] == "|" && pattern[1] == "|") |
+ { |
+ let index = location.indexOf(pattern.substring(2)); |
+ |
+ // The "||" prefix requires that the text that follows does not start with |
+ // a forward slash. Normally this is part of the generated regular |
+ // expression, but since we're faking it here with string-based matching we |
Manish Jethani
2018/10/19 21:59:13
Since it's all in the same file now I don't think
|
+ // need to do the check ourselves. |
+ return index != -1 && location[index] != "/" && |
+ doubleAnchorRegExp.test(location.substring(0, index)); |
+ } |
+ |
+ return location.includes(pattern); |
+} |
+ |
+exports.locationMatchesPattern = locationMatchesPattern; |
+ |
+/** |
* Converts raw text into a regular expression string |
* @param {string} text the string to convert |
* @return {string} regular expression representation of the text |
*/ |
function textToRegExp(text) |
{ |
return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"); |
} |