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

Unified Diff: lib/filterClasses.js

Issue 29760704: Issue 6592 - Implement $rewrite filter option (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Patch Set: Comment fixes. Created May 11, 2018, 3: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 | test/.eslintrc.json » ('j') | test/_common.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/filterClasses.js
===================================================================
--- a/lib/filterClasses.js
+++ b/lib/filterClasses.js
@@ -765,16 +765,17 @@
let contentType = null;
let matchCase = null;
let domains = null;
let sitekeys = null;
let thirdParty = null;
let collapse = null;
let csp = null;
+ let rewrite = null;
let options;
let match = (text.indexOf("$") >= 0 ? Filter.optionsRegExp.exec(text) : null);
if (match)
{
options = match[1].split(",");
text = match.input.substr(0, match.index);
for (let option of options)
{
@@ -812,30 +813,32 @@
else if (option == "~THIRD_PARTY")
thirdParty = false;
else if (option == "COLLAPSE")
collapse = true;
else if (option == "~COLLAPSE")
collapse = false;
else if (option == "SITEKEY" && value)
sitekeys = value.toUpperCase();
+ else if (option == "REWRITE" && value)
+ rewrite = value;
else
return new InvalidFilter(origText, "filter_unknown_option");
}
}
try
{
if (blocking)
{
if (csp && Filter.invalidCSPRegExp.test(csp))
return new InvalidFilter(origText, "filter_invalid_csp");
return new BlockingFilter(origText, text, contentType, matchCase, domains,
- thirdParty, sitekeys, collapse, csp);
+ thirdParty, sitekeys, collapse, csp, rewrite);
}
return new WhitelistFilter(origText, text, contentType, matchCase, domains,
thirdParty, sitekeys);
}
catch (e)
{
return new InvalidFilter(origText, "filter_invalid_regexp");
}
@@ -888,27 +891,30 @@
* @param {boolean} [matchCase] see RegExpFilter()
* @param {string} [domains] see RegExpFilter()
* @param {boolean} [thirdParty] see RegExpFilter()
* @param {string} [sitekeys] see RegExpFilter()
* @param {boolean} [collapse]
* defines whether the filter should collapse blocked content, can be null
* @param {string} [csp]
* Content Security Policy to inject when the filter matches
+ * @param {string} [rewrite]
+ * The rewrite expression
kzar 2018/05/15 14:08:58 Mind making this comment useful? (Same below.)
hub 2018/05/15 16:16:31 Done.
kzar 2018/05/16 09:22:40 Thanks, looking way better now.
* @constructor
* @augments RegExpFilter
*/
function BlockingFilter(text, regexpSource, contentType, matchCase, domains,
- thirdParty, sitekeys, collapse, csp)
+ thirdParty, sitekeys, collapse, csp, rewrite)
{
RegExpFilter.call(this, text, regexpSource, contentType, matchCase, domains,
thirdParty, sitekeys);
this.collapse = collapse;
this.csp = csp;
+ this.rewrite = rewrite;
}
exports.BlockingFilter = BlockingFilter;
BlockingFilter.prototype = extend(RegExpFilter, {
type: "blocking",
/**
* Defines whether the filter should collapse blocked content.
@@ -916,17 +922,43 @@
* @type {?boolean}
*/
collapse: null,
/**
* Content Security Policy to inject for matching requests.
* @type {?string}
*/
- csp: null
+ csp: null,
+
+ /**
+ * The rewrite expression
+ * @type {?string}
+ */
+ rewrite: null,
+
+ /**
+ * Rewrites an URL.
+ * @param {string} url the URL to rewrite
+ * @return {string} the rewritten URL, or the original in case of failure
+ */
+ rewriteUrl(url)
+ {
+ try
+ {
+ let rewrittenUrl = new URL(url.replace(this.regexp, this.rewrite), url);
+ if (rewrittenUrl.origin == new URL(url).origin)
+ return rewrittenUrl.href;
+ }
+ catch (e)
+ {
+ }
+
+ return url;
kzar 2018/05/15 14:24:57 Why do we return `url` on failure? Shouldn't we re
Sebastian Noack 2018/05/15 14:40:34 Even if no error is thrown and no origin change wa
kzar 2018/05/15 14:44:23 Not if we tweak the above logic, so we don't retur
Sebastian Noack 2018/05/15 14:51:02 Sure, we could add another check here, in order to
kzar 2018/05/15 14:53:58 Well it seems pointless to return the existing URL
Manish Jethani 2018/05/15 15:18:12 These are not the only cases in which the rewrite
hub 2018/05/15 16:16:31 The original idea is to not block the request in c
+ }
});
/**
* Class for whitelist filters
* @param {string} text see Filter()
* @param {string} regexpSource see RegExpFilter()
* @param {number} [contentType] see RegExpFilter()
* @param {boolean} [matchCase] see RegExpFilter()
« no previous file with comments | « no previous file | test/.eslintrc.json » ('j') | test/_common.js » ('J')

Powered by Google App Engine
This is Rietveld