Index: lib/common.js |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/lib/common.js |
@@ -0,0 +1,50 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2016 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+/** |
+ * Converts filter text into regular expression string |
+ * @param {String} text as in Filter() |
+ * @return {String} regular expression representation of filter text |
+ */ |
+function filterToRegExp(text) |
+{ |
+ return text |
+ // remove multiple wildcards |
+ .replace(/\*+/g, "*") |
+ // remove anchors following separator placeholder |
+ .replace(/\^\|$/, "^") |
+ // escape special symbols |
+ .replace(/\W/g, "\\$&") |
+ // replace wildcards by .* |
+ .replace(/\\\*/g, ".*") |
+ // process separator placeholders (all ANSI characters but alphanumeric |
+ // characters and _%.-) |
+ .replace(/\\\^/g, "(?:[\\x00-\\x24\\x26-\\x2C\\x2F\\x3A-\\x40\\x5B-\\x5E\\x60\\x7B-\\x7F]|$)") |
+ // process extended anchor at expression start |
+ .replace(/^\\\|\\\|/, "^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?") |
+ // process anchor at expression start |
+ .replace(/^\\\|/, "^") |
+ // process anchor at expression end |
+ .replace(/\\\|$/, "$") |
+ // remove leading wildcards |
+ .replace(/^(\.\*)/, "") |
+ // remove trailing wildcards |
+ .replace(/(\.\*)$/, ""); |
+} |
+ |
+if (typeof exports != "undefined") |
+ exports.filterToRegExp = filterToRegExp; |