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

Unified Diff: lib/common.js

Issue 29863579: Issue 6868 - Enable capturing of surrounding wildcards for rewrite filters (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Rename to captureAll Created Sept. 1, 2018, 2:57 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
« no previous file with comments | « no previous file | lib/filterClasses.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/common.js
===================================================================
--- a/lib/common.js
+++ b/lib/common.js
@@ -27,42 +27,53 @@
return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
}
exports.textToRegExp = textToRegExp;
/**
* Converts filter text into regular expression string
* @param {string} text as in Filter()
+ * @param {boolean} [captureAll=false] whether to enable the capturing of
+ * leading and trailing wildcards in the filter text; by default, leading and
+ * trailing wildcards are stripped out
* @return {string} regular expression representation of filter text
*/
-function filterToRegExp(text)
+function filterToRegExp(text, captureAll = false)
{
+ // remove multiple wildcards
+ text = text.replace(/\*+/g, "*");
+
+ if (!captureAll)
+ {
+ // remove leading wildcard
+ if (text[0] == "*")
+ text = text.substring(1);
+
+ // remove trailing wildcard
+ if (text[text.length - 1] == "*")
+ text = text.substring(0, text.length - 1);
+ }
+
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(/(\.\*)$/, "");
+ .replace(/\\\|$/, "$");
}
exports.filterToRegExp = filterToRegExp;
function splitSelector(selector)
{
if (!selector.includes(","))
return [selector];
« no previous file with comments | « no previous file | lib/filterClasses.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld