1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-15 15:32:28 +02:00

Minor review of redirect-related code

Notably, I finally settled for implicit priority of
0, but now negative priority values are allowed.
This commit is contained in:
Raymond Hill 2020-12-02 08:18:55 -05:00
parent a48e986546
commit 26dc7a1490
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
4 changed files with 14 additions and 11 deletions

View File

@ -187,7 +187,7 @@ CodeMirror.defineMode('ubo-static-filtering', function() {
/[$,]redirect(-rule)?=$/.test(string.slice(0, pos)) /[$,]redirect(-rule)?=$/.test(string.slice(0, pos))
) { ) {
style = 'value'; style = 'value';
let end = parser.skipUntil( const end = parser.skipUntil(
parserSlot, parserSlot,
parser.commentSpan.i, parser.commentSpan.i,
parser.BITComma parser.BITComma

View File

@ -291,11 +291,11 @@ RedirectEngine.prototype.freeze = function() {
/******************************************************************************/ /******************************************************************************/
RedirectEngine.prototype.tokenToURL = function(fctxt, token) { RedirectEngine.prototype.tokenToURL = function(
const asDataURI = token.charCodeAt(0) === 0x25 /* '%' */; fctxt,
if ( asDataURI ) { token,
token = token.slice(1); asDataURI = false
} ) {
const entry = this.resources.get(this.aliases.get(token) || token); const entry = this.resources.get(this.aliases.get(token) || token);
if ( entry === undefined ) { return; } if ( entry === undefined ) { return; }
this.resourceNameRegister = token; this.resourceNameRegister = token;

View File

@ -1164,13 +1164,15 @@ const Parser = class {
static parseRedirectValue(arg) { static parseRedirectValue(arg) {
let token = arg.trim(); let token = arg.trim();
let priority = 10; let priority = 0;
const match = /:\d+$/.exec(token); const asDataURI = token.charCodeAt(0) === 0x25 /* '%' */;
if ( asDataURI ) { token = token.slice(1); }
const match = /:-?\d+$/.exec(token);
if ( match !== null ) { if ( match !== null ) {
token = token.slice(0, match.index); token = token.slice(0, match.index);
priority = parseInt(token.slice(match.index + 1), 10); priority = parseInt(token.slice(match.index + 1), 10);
} }
return { token, priority }; return { token, priority, asDataURI };
} }
static parseQueryPruneValue(arg) { static parseQueryPruneValue(arg) {

View File

@ -4236,12 +4236,13 @@ FilterContainer.prototype.redirectRequest = function(fctxt) {
const directives = this.matchAndFetchModifiers(fctxt, 'redirect-rule'); const directives = this.matchAndFetchModifiers(fctxt, 'redirect-rule');
// No directive is the most common occurrence. // No directive is the most common occurrence.
if ( directives === undefined ) { return; } if ( directives === undefined ) { return; }
const highest = directives.length - 1;
// More than a single directive means more work. // More than a single directive means more work.
if ( directives.length !== 1 ) { if ( highest !== 0 ) {
directives.sort(FilterContainer.compareRedirectRequests); directives.sort(FilterContainer.compareRedirectRequests);
} }
// Redirect to highest-ranked directive // Redirect to highest-ranked directive
const directive = directives[directives.length - 1]; const directive = directives[highest];
if ( (directive.bits & AllowAction) === 0 ) { if ( (directive.bits & AllowAction) === 0 ) {
const { token } = const { token } =
FilterContainer.parseRedirectRequestValue(directive.modifier); FilterContainer.parseRedirectRequestValue(directive.modifier);