Skip to content

Commit

Permalink
Have urltransform= use the same syntax as replace=
Browse files Browse the repository at this point in the history
  • Loading branch information
gorhill committed Nov 5, 2023
1 parent 5f78d83 commit d7c99b4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
12 changes: 7 additions & 5 deletions src/js/static-filtering-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1490,9 +1490,10 @@ export class AstFilterParser {
realBad = true;
break;
}
if ( this.interactive ) {
const value = this.getNetOptionValue(NODE_TYPE_NET_OPTION_NAME_REPLACE);
realBad = parseReplaceValue(value) === undefined;
const value = this.getNetOptionValue(NODE_TYPE_NET_OPTION_NAME_REPLACE);
if ( parseReplaceValue(value) === undefined ) {
this.astError = AST_ERROR_OPTION_BADVALUE;
realBad = true;
}
break;
}
Expand All @@ -1504,8 +1505,8 @@ export class AstFilterParser {
realBad = true;
break;
}
const path = this.getNetOptionValue(NODE_TYPE_NET_OPTION_NAME_URLTRANSFORM);
if ( path.charCodeAt(0) !== 0x2F /* / */ ) {
const value = this.getNetOptionValue(NODE_TYPE_NET_OPTION_NAME_URLTRANSFORM);
if ( parseReplaceValue(value) === undefined ) {
this.astError = AST_ERROR_OPTION_BADVALUE;
realBad = true;
}
Expand Down Expand Up @@ -3008,6 +3009,7 @@ export function parseReplaceValue(s) {
if ( parser.transform ) {
pattern = parser.normalizeArg(pattern);
}
if ( pattern === '' ) { return; }
pattern = pattern
.replace(reEscapedDollarSign, '$1$$$')
.replace(reEscapedComma, '$1,');
Expand Down
16 changes: 14 additions & 2 deletions src/js/static-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -5275,9 +5275,21 @@ FilterContainer.prototype.transformRequest = function(fctxt) {
if ( directives === undefined ) { return; }
const directive = directives[directives.length-1];
if ( (directive.bits & ALLOW_REALM) !== 0 ) { return directives; }
if ( directive.refs instanceof Object === false ) { return; }
const { refs } = directive;
if ( refs.$cache === null ) {
refs.$cache = sfp.parseReplaceValue(refs.value);
}
const cache = refs.$cache;
if ( cache === undefined ) { return; }
const redirectURL = new URL(fctxt.url);
if ( directive.value === redirectURL.pathname ) { return; }
redirectURL.pathname = directive.value;
const before = redirectURL.pathname + redirectURL.search;
if ( cache.re.test(before) !== true ) { return; }
const after = before.replace(cache.re, cache.replacement);
if ( after === before ) { return; }
const searchPos = after.includes('?') && after.indexOf('?') || after.length;
redirectURL.pathname = after.slice(0, searchPos);
redirectURL.search = after.slice(searchPos);
fctxt.redirectURL = redirectURL.href;
return directives;
};
Expand Down

0 comments on commit d7c99b4

Please sign in to comment.