From a85f64c562ac036dbe776e0db4d38a0b6dcc6ff5 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 8 Dec 2014 15:37:35 -0200 Subject: [PATCH] this addresses #405 --- src/js/pagestore.js | 5 ++++ src/js/ublock.js | 61 ++++++++++++++++++++++++++------------------- 2 files changed, 41 insertions(+), 25 deletions(-) diff --git a/src/js/pagestore.js b/src/js/pagestore.js index ba56eaf9f..3fb2d7f04 100644 --- a/src/js/pagestore.js +++ b/src/js/pagestore.js @@ -333,6 +333,11 @@ PageStore.prototype.reuse = function(pageURL, context) { this.pageDomain = µb.URI.domainFromHostname(this.pageHostname) || this.pageHostname; this.rootHostname = this.pageHostname; this.rootDomain = this.pageDomain; + + // As part of https://github.com/gorhill/uBlock/issues/405 + // URL changed, force a re-evaluation of filtering switch + this.netFilteringReadTime = 0; + return this; } // A new page is completely reloaded from scratch, reset all. diff --git a/src/js/ublock.js b/src/js/ublock.js index 7b32d0776..726d735f9 100644 --- a/src/js/ublock.js +++ b/src/js/ublock.js @@ -47,19 +47,9 @@ } var i = exceptions.length; - var exception; while ( i-- ) { - exception = exceptions[i]; - if ( exception.indexOf('/') !== -1 ) { - if ( exception.slice(-1) === '*' ) { - exception = exception.slice(0, -1); - if ( keyURL.slice(0, exception.length) === exception ) { - return false; - } - } else if ( keyURL === exception ) { - return false; - } - } else if ( keyHostname.slice(-exception.length) === exception ) { + if ( this.matchWhitelistException(keyURL, keyHostname, exceptions[i]) ) { + // console.log('"%s" matche url "%s"', exceptions[i], keyURL); return false; } } @@ -105,21 +95,10 @@ return true; } - // Remove from exception list + // Remove from exception list whatever causes current URL to be whitelisted var i = exceptions.length; - var exception; while ( i-- ) { - exception = exceptions[i]; - if ( exception.indexOf('/') !== -1 ) { - if ( exception.slice(-1) === '*' ) { - exception = exception.slice(0, -1); - if ( keyURL.slice(0, exception.length) === exception ) { - exceptions.splice(i, 1); - } - } else if ( keyURL === exception ) { - exceptions.splice(i, 1); - } - } else if ( keyHostname.slice(-exception.length) === exception ) { + if ( this.matchWhitelistException(keyURL, keyHostname, exceptions[i]) ) { exceptions.splice(i, 1); } } @@ -132,6 +111,38 @@ /******************************************************************************/ +// https://github.com/gorhill/uBlock/issues/405 +// Be more flexible with whitelist syntax + +// TODO: Need to harden it against edge cases, like when an asterisk is used in +// place of protocol. + +µBlock.matchWhitelistException = function(url, hostname, exception) { + // Exception is a plain hostname + if ( exception.indexOf('/') === -1 ) { + return hostname.slice(-exception.length) === exception; + } + // Match URL exactly + if ( exception.indexOf('*') === -1 ) { + return url === exception; + } + // Regex escape code inspired from: + // "Is there a RegExp.escape function in Javascript?" + // http://stackoverflow.com/a/3561711 + var reStr = exception.replace(this.whitelistExceptionEscape, '\\$&') + .replace(this.whitelistExceptionEscapeAsterisk, '.*'); + var re = new RegExp(reStr); + return re.test(url); +}; + +// Any special regexp char will be escaped +µBlock.whitelistExceptionEscape = /[-\/\\^$+?.()|[\]{}]/g; + +// All `*` will be expanded into `.*` +µBlock.whitelistExceptionEscapeAsterisk = /\*/g; + +/******************************************************************************/ + // For now we will use the net exception list µBlock.getCosmeticFilteringSwitch = function(url, domain) {