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: Add comment in test file Created Aug. 24, 2018, 4: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
« 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)
hub 2018/08/29 16:23:50 also, on second thought, I'm not certain that `opt
Manish Jethani 2018/09/01 14:57:52 OK, it already captures everything else, just not
{
+ if (optimize)
+ {
+ // remove multiple wildcards
+ text = text.replace(/\*+/g, "*");
hub 2018/08/28 22:02:55 I think we should keep this one unconditionally li
Manish Jethani 2018/08/29 13:25:24 Since the `optimize` argument is supposed to skip
hub 2018/08/29 16:23:50 this is more about correctness IMHO. There is an e
Manish Jethani 2018/09/01 14:57:52 Alright, if we go with the renaming then it makes
+
+ // 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