From d62059ccc74074e8621c870720aa7501112cadc1 Mon Sep 17 00:00:00 2001 From: gorhill Date: Tue, 8 Nov 2016 07:13:26 -0500 Subject: [PATCH] fix #2103 --- src/js/pagestore.js | 9 ++--- src/js/static-net-filtering.js | 65 ++++++++++++++++++++++------------ 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/js/pagestore.js b/src/js/pagestore.js index 5470096ee..8f3f661b5 100644 --- a/src/js/pagestore.js +++ b/src/js/pagestore.js @@ -316,7 +316,7 @@ PageStore.prototype.init = function(tabId) { this.internalRedirectionCount = 0; this.noCosmeticFiltering = µb.hnSwitches.evaluateZ('no-cosmetic-filtering', tabContext.rootHostname) === true; - if ( µb.logger.isEnabled() && this.noCosmeticFiltering ) { + if ( this.noCosmeticFiltering && µb.logger.isEnabled() ) { µb.logger.writeOne( tabId, 'cosmetic', @@ -331,12 +331,13 @@ PageStore.prototype.init = function(tabId) { // Support `generichide` filter option. this.noGenericCosmeticFiltering = this.noCosmeticFiltering; if ( this.noGenericCosmeticFiltering !== true ) { - this.noGenericCosmeticFiltering = µb.staticNetFilteringEngine.matchStringExactType( + var result = µb.staticNetFilteringEngine.matchStringExactType( this.createContextFromPage(), tabContext.normalURL, 'generichide' - ) === false; - if ( µb.logger.isEnabled() && this.noGenericCosmeticFiltering ) { + ); + this.noGenericCosmeticFiltering = result === false; + if ( result !== undefined && µb.logger.isEnabled() ) { µb.logger.writeOne( tabId, 'net', diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index 5c60ecdb3..d62036bbe 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -106,6 +106,9 @@ var AllowAnyTypeAnyParty = AllowAction | AnyType | AnyParty; var AllowAnyType = AllowAction | AnyType; var AllowAnyParty = AllowAction | AnyParty; +var genericHideException = AllowAction | AnyParty | typeNameToTypeValue.generichide, + genericHideImportant = BlockAction | AnyParty | typeNameToTypeValue.generichide | Important; + // ABP filters: https://adblockplus.org/en/filters // regex tester: http://regex101.com/ @@ -1262,7 +1265,7 @@ FilterParser.prototype.parseOptions = function(s) { // `generichide` concept already supported, just a matter of // adding support for the new keyword. if ( opt === 'elemhide' || opt === 'generichide' ) { - if ( this.action === AllowAction ) { + if ( not === false ) { this.parseOptType('generichide', false); continue; } @@ -2188,11 +2191,43 @@ FilterContainer.prototype.matchTokens = function(bucket, url) { // Specialized handlers +// https://github.com/gorhill/uBlock/issues/1477 +// Special case: blocking-generichide filter ALWAYS exists, it is implicit -- +// thus we always first check for exception filters, then for important block +// filter if and only if there was a hit on an exception filter. +// https://github.com/gorhill/uBlock/issues/2103 +// User may want to override `generichide` exception filters. + +FilterContainer.prototype.matchStringGenericHide = function(context, requestURL) { + var url = this.urlTokenizer.setURL(requestURL); + + var bucket = this.categories.get(toHex(genericHideException)); + if ( !bucket || this.matchTokens(bucket, url) === false ) { + this.fRegister = null; + return; + } + + bucket = this.categories.get(toHex(genericHideImportant)); + if ( bucket && this.matchTokens(bucket, url) ) { + this.keyRegister = genericHideImportant; + return true; + } + + this.keyRegister = genericHideException; + return false; +}; + +/******************************************************************************/ + // https://github.com/chrisaljoudi/uBlock/issues/116 -// Some type of requests are exceptional, they need custom handling, -// not the generic handling. +// Some type of requests are exceptional, they need custom handling, +// not the generic handling. FilterContainer.prototype.matchStringExactType = function(context, requestURL, requestType) { + // Special case. + if ( requestType === 'generichide' ) { + return this.matchStringGenericHide(context, requestURL); + } // Be prepared to support unknown types var type = typeNameToTypeValue[requestType]; if ( type === undefined ) { @@ -2206,30 +2241,14 @@ FilterContainer.prototype.matchStringExactType = function(context, requestURL, r pageHostnameRegister = context.pageHostname || ''; requestHostnameRegister = µb.URI.hostnameFromURI(url); - var party = isFirstParty(context.pageDomain, requestHostnameRegister) ? FirstParty : ThirdParty; + var party = isFirstParty(context.pageDomain, requestHostnameRegister) ? FirstParty : ThirdParty, + categories = this.categories, + key, bucket; this.fRegister = null; - var categories = this.categories; - var key, bucket; - - // https://github.com/gorhill/uBlock/issues/1477 - // Special case: blocking generichide filter ALWAYS exists, it is implicit -- - // thus we always and only check for exception filters. - if ( requestType === 'generichide' ) { - key = AllowAnyParty | type; - if ( - (bucket = categories.get(toHex(key))) && - this.matchTokens(bucket, url) - ) { - this.keyRegister = key; - return false; - } - return undefined; - } - // https://github.com/chrisaljoudi/uBlock/issues/139 - // Test against important block filters + // Test against important block filters key = BlockAnyParty | Important | type; if ( (bucket = categories.get(toHex(key))) ) { if ( this.matchTokens(bucket, url) ) {