From ccef74bc28b5d81503f92a9b5c3e169649cad910 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 31 Oct 2018 19:34:54 -0300 Subject: [PATCH] Make "none" a reserved "redirect=" keyword See https://github.com/uBlockOrigin/uAssets/commit/deefe875551197d655f79cb540e62dfc17c95f42 --- src/js/redirect-engine.js | 42 +++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/src/js/redirect-engine.js b/src/js/redirect-engine.js index 9cf7e687d..bb3b2a99f 100644 --- a/src/js/redirect-engine.js +++ b/src/js/redirect-engine.js @@ -28,7 +28,7 @@ /******************************************************************************/ /******************************************************************************/ -var warResolve = (function() { +let warResolve = (function() { var warPairs = []; var onPairsReady = function() { @@ -73,7 +73,7 @@ var warResolve = (function() { /******************************************************************************/ /******************************************************************************/ -var RedirectEntry = function() { +let RedirectEntry = function() { this.mime = ''; this.data = ''; this.warURL = undefined; @@ -141,7 +141,7 @@ RedirectEntry.fromSelfie = function(selfie) { /******************************************************************************/ /******************************************************************************/ -var RedirectEngine = function() { +let RedirectEngine = function() { this.resources = new Map(); this.reset(); this.resourceNameRegister = ''; @@ -207,9 +207,9 @@ RedirectEngine.prototype.lookup = function(context) { }; RedirectEngine.prototype.lookupToken = function(entries, reqURL) { - var j = entries.length, entry; + let j = entries.length; while ( j-- ) { - entry = entries[j]; + let entry = entries[j]; if ( entry.pat instanceof RegExp === false ) { entry.pat = new RegExp(entry.pat, 'i'); } @@ -223,9 +223,9 @@ RedirectEngine.prototype.lookupToken = function(entries, reqURL) { /******************************************************************************/ RedirectEngine.prototype.toURL = function(context) { - var token = this.lookup(context); + let token = this.lookup(context); if ( token === undefined ) { return; } - var entry = this.resources.get(token); + let entry = this.resources.get(token); if ( entry !== undefined ) { return entry.toURL(context); } @@ -446,24 +446,26 @@ RedirectEngine.prototype.resourceContentFromName = function(name, mime) { // TODO: combine same key-redirect pairs into a single regex. +// https://github.com/uBlockOrigin/uAssets/commit/deefe875551197d655f79cb540e62dfc17c95f42 +// Consider 'none' a reserved keyword, to be used to disable redirection. + RedirectEngine.prototype.resourcesFromString = function(text) { - var line, fields, encoded, + let fields, encoded, reNonEmptyLine = /\S/, lineIter = new µBlock.LineIterator(text); this.resources = new Map(); while ( lineIter.eot() === false ) { - line = lineIter.next(); + let line = lineIter.next(); if ( line.startsWith('#') ) { continue; } if ( fields === undefined ) { - fields = line.trim().split(/\s+/); - if ( fields.length === 2 ) { - encoded = fields[1].indexOf(';') !== -1; - } else { - fields = undefined; - } + let head = line.trim().split(/\s+/); + if ( head.length !== 2 ) { continue; } + if ( head[0] === 'none' ) { continue; } + encoded = head[1].indexOf(';') !== -1; + fields = head; continue; } @@ -473,14 +475,20 @@ RedirectEngine.prototype.resourcesFromString = function(text) { } // No more data, add the resource. - this.resources.set(fields[0], RedirectEntry.fromFields(fields[1], fields.slice(2))); + this.resources.set( + fields[0], + RedirectEntry.fromFields(fields[1], fields.slice(2)) + ); fields = undefined; } // Process pending resource data. if ( fields !== undefined ) { - this.resources.set(fields[0], RedirectEntry.fromFields(fields[1], fields.slice(2))); + this.resources.set( + fields[0], + RedirectEntry.fromFields(fields[1], fields.slice(2)) + ); } warResolve();