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

Unified Diff: lib/shared.js

Issue 29361668: Issue 4394 - Create a filter class for element hiding emulation filters (Closed) Base URL: https://bitbucket.org/fhd/adblockpluscore
Patch Set: Created Nov. 3, 2016, 3:42 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/shared.js
===================================================================
new file mode 100644
--- /dev/null
+++ b/lib/shared.js
@@ -0,0 +1,51 @@
+/*
+ * 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/>.
+ */
+
+/**
+ * Map of features supported by element hiding emulation filters
+ */
+var elemHideEmulationFeatureMap = {
+ PROPERTY_SELECTOR: 1,
+ HAS_PSEUDO_CLASS: 2
+};
+
+/**
+ * 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
+ .replace(/\*+/g, "*") // remove multiple wildcards
+ .replace(/\^\|$/, "^") // remove anchors following separator placeholder
kzar 2016/11/04 15:45:56 Nit: Mind fixing these long lines?
Felix Dahlke 2016/11/04 16:43:35 Well, I tried to leave the code alone since I'm ju
Felix Dahlke 2016/11/11 11:51:42 Done.
+ .replace(/\W/g, "\\$&") // escape special symbols
+ .replace(/\\\*/g, ".*") // replace wildcards by .*
+ // process separator placeholders (all ANSI characters but alphanumeric characters and _%.-)
+ .replace(/\\\^/g, "(?:[\\x00-\\x24\\x26-\\x2C\\x2F\\x3A-\\x40\\x5B-\\x5E\\x60\\x7B-\\x7F]|$)")
+ .replace(/^\\\|\\\|/, "^[\\w\\-]+:\\/+(?!\\/)(?:[^\\/]+\\.)?") // process extended anchor at expression start
+ .replace(/^\\\|/, "^") // process anchor at expression start
+ .replace(/\\\|$/, "$") // process anchor at expression end
+ .replace(/^(\.\*)/, "") // remove leading wildcards
+ .replace(/(\.\*)$/, ""); // remove trailing wildcards
+}
+
+if (typeof exports != "undefined")
+{
+ exports.filterToRegExp = filterToRegExp;
+ exports.elemHideEmulationFeatureMap = elemHideEmulationFeatureMap;
+}

Powered by Google App Engine
This is Rietveld