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

Unified Diff: lib/common.js

Issue 29361668: Issue 4394 - Create a filter class for element hiding emulation filters (Closed) Base URL: https://bitbucket.org/fhd/adblockpluscore
Patch Set: Address Wladimir's comments Created Nov. 21, 2016, 2:34 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/common.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/common.js
@@ -0,0 +1,52 @@
+/*
+ * 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")
kzar 2016/11/21 16:24:55 Nit: The braces here aren't required.
Felix Dahlke 2016/11/21 17:26:12 Done.
+{
+ exports.filterToRegExp = filterToRegExp;
+}

Powered by Google App Engine
This is Rietveld