From 15e0778750c3b669c933d042460de8a73ad01e8a Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 24 Sep 2020 10:53:18 -0400 Subject: [PATCH] Prevent spurious instantiation of procedural filterer Injecting declarative CSS `:style()` selector could cause the instatiation of the procedural filterer, even when there was no actual procedural cosmetic filter to enforce. This commit ensure that the procedural cosmetic filterer is instantiated only when there are actual procedural filters to enforce. --- src/js/contentscript.js | 58 +++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 26 deletions(-) diff --git a/src/js/contentscript.js b/src/js/contentscript.js index 713bafa7c..7d97c5ccb 100644 --- a/src/js/contentscript.js +++ b/src/js/contentscript.js @@ -774,35 +774,19 @@ vAPI.injectScriptlet = function(doc, text) { addProceduralSelectors(selectors) { const addedSelectors = []; let mustCommit = this.domIsWatched; - for ( const raw of selectors ) { - if ( this.selectors.has(raw) ) { continue; } - const o = JSON.parse(raw); - // CSS selector-based styles. - if ( - o.action !== undefined && - o.action[0] === ':style' && - o.tasks === undefined - ) { - this.domFilterer.addCSSRule(o.selector, o.action[1]); - mustCommit = true; - continue; - } - if ( o.pseudo !== undefined ) { - this.domFilterer.addCSSRule(o.selector, vAPI.hideStyle); - mustCommit = true; - continue; - } + for ( const selector of selectors ) { + if ( this.selectors.has(selector.raw) ) { continue; } let style, styleToken; - if ( o.action === undefined ) { + if ( selector.action === undefined ) { style = vAPI.hideStyle; - } else if ( o.action[0] === ':style' ) { - style = o.action[1]; + } else if ( selector.action[0] === ':style' ) { + style = selector.action[1]; } if ( style !== undefined ) { styleToken = this.styleTokenFromStyle(style); } - const pselector = new PSelectorRoot(o, styleToken); - this.selectors.set(raw, pselector); + const pselector = new PSelectorRoot(selector, styleToken); + this.selectors.set(selector.raw, pselector); addedSelectors.push(pselector); mustCommit = true; } @@ -1063,9 +1047,31 @@ vAPI.injectScriptlet = function(doc, text) { return this.proceduralFilterer; } - addProceduralSelectors(aa) { - if ( Array.isArray(aa) === false || aa.length === 0 ) { return; } - this.proceduralFiltererInstance().addProceduralSelectors(aa); + addProceduralSelectors(selectors) { + if ( Array.isArray(selectors) === false || selectors.length === 0 ) { + return; + } + const procedurals = []; + for ( const raw of selectors ) { + const o = JSON.parse(raw); + if ( + o.action !== undefined && + o.action[0] === ':style' && + o.tasks === undefined + ) { + this.addCSSRule(o.selector, o.action[1]); + continue; + } + if ( o.pseudo !== undefined ) { + this.addCSSRule(o.selector, vAPI.hideStyle); + continue; + } + procedurals.push(o); + } + if ( procedurals.length !== 0 ) { + this.proceduralFiltererInstance() + .addProceduralSelectors(procedurals); + } } createProceduralFilter(o) {