From 26dc7a1490ce1ae422b0e1a7e9a29eb16057a20c Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 2 Dec 2020 08:18:55 -0500 Subject: [PATCH] Minor review of redirect-related code Notably, I finally settled for implicit priority of 0, but now negative priority values are allowed. --- src/js/codemirror/ubo-static-filtering.js | 2 +- src/js/redirect-engine.js | 10 +++++----- src/js/static-filtering-parser.js | 8 +++++--- src/js/static-net-filtering.js | 5 +++-- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/js/codemirror/ubo-static-filtering.js b/src/js/codemirror/ubo-static-filtering.js index 976554a20..cc55c7548 100644 --- a/src/js/codemirror/ubo-static-filtering.js +++ b/src/js/codemirror/ubo-static-filtering.js @@ -187,7 +187,7 @@ CodeMirror.defineMode('ubo-static-filtering', function() { /[$,]redirect(-rule)?=$/.test(string.slice(0, pos)) ) { style = 'value'; - let end = parser.skipUntil( + const end = parser.skipUntil( parserSlot, parser.commentSpan.i, parser.BITComma diff --git a/src/js/redirect-engine.js b/src/js/redirect-engine.js index 57a79a173..1b8780996 100644 --- a/src/js/redirect-engine.js +++ b/src/js/redirect-engine.js @@ -291,11 +291,11 @@ RedirectEngine.prototype.freeze = function() { /******************************************************************************/ -RedirectEngine.prototype.tokenToURL = function(fctxt, token) { - const asDataURI = token.charCodeAt(0) === 0x25 /* '%' */; - if ( asDataURI ) { - token = token.slice(1); - } +RedirectEngine.prototype.tokenToURL = function( + fctxt, + token, + asDataURI = false +) { const entry = this.resources.get(this.aliases.get(token) || token); if ( entry === undefined ) { return; } this.resourceNameRegister = token; diff --git a/src/js/static-filtering-parser.js b/src/js/static-filtering-parser.js index cd2732628..ccdfa506c 100644 --- a/src/js/static-filtering-parser.js +++ b/src/js/static-filtering-parser.js @@ -1164,13 +1164,15 @@ const Parser = class { static parseRedirectValue(arg) { let token = arg.trim(); - let priority = 10; - const match = /:\d+$/.exec(token); + let priority = 0; + const asDataURI = token.charCodeAt(0) === 0x25 /* '%' */; + if ( asDataURI ) { token = token.slice(1); } + const match = /:-?\d+$/.exec(token); if ( match !== null ) { token = token.slice(0, match.index); priority = parseInt(token.slice(match.index + 1), 10); } - return { token, priority }; + return { token, priority, asDataURI }; } static parseQueryPruneValue(arg) { diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index b5f83382b..75fb0d663 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -4236,12 +4236,13 @@ FilterContainer.prototype.redirectRequest = function(fctxt) { const directives = this.matchAndFetchModifiers(fctxt, 'redirect-rule'); // No directive is the most common occurrence. if ( directives === undefined ) { return; } + const highest = directives.length - 1; // More than a single directive means more work. - if ( directives.length !== 1 ) { + if ( highest !== 0 ) { directives.sort(FilterContainer.compareRedirectRequests); } // Redirect to highest-ranked directive - const directive = directives[directives.length - 1]; + const directive = directives[highest]; if ( (directive.bits & AllowAction) === 0 ) { const { token } = FilterContainer.parseRedirectRequestValue(directive.modifier);