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: Created Aug. 24, 2018, 4:36 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,51 @@
return text.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&");
}
exports.textToRegExp = textToRegExp;
/**
* Converts filter text into regular expression string
* @param {string} text as in Filter()
+ * @param {boolean} [optimize=true] whether to optimize the regular expression
* @return {string} regular expression representation of filter text
*/
-function filterToRegExp(text)
+function filterToRegExp(text, optimize = true)
{
+ if (optimize)
+ {
+ // remove multiple wildcards
+ text = text.replace(/\*+/g, "*");
+
+ // 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