Skip to content

Commit

Permalink
Revert "Prevent highly generic cosmetic filters from affecting html/b…
Browse files Browse the repository at this point in the history
…ody elements"

This reverts commit 7c8aec2.

This will be brought back in a future dev cycle. Potentially
related issues:

- uBlockOrigin/uBlock-issues#1978
- uBlockOrigin/uBlock-issues#1983
  • Loading branch information
gorhill committed Feb 16, 2022
1 parent 9726e89 commit 5178b91
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 39 deletions.
5 changes: 0 additions & 5 deletions src/js/contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -676,11 +676,6 @@ vAPI.DOMFilterer = class {
getAllExceptionSelectors() {
return this.exceptions.join(',\n');
}

unwrapSelector(s) {
const match = /^:is\((.+)\):not\(html,body\)\/\*hg\*\/$/.exec(s);
return match !== null ? match[1] : s;
}
};

/******************************************************************************/
Expand Down
47 changes: 19 additions & 28 deletions src/js/cosmetic-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,13 @@ const FilterContainer = function() {
this.highlyGeneric.simple = {
canonical: 'highGenericHideSimple',
dict: new Set(),
str: '',
mru: new µb.MRUCache(16)
};
this.highlyGeneric.complex = {
canonical: 'highGenericHideComplex',
dict: new Set(),
str: '',
mru: new µb.MRUCache(16)
};

Expand Down Expand Up @@ -279,16 +281,11 @@ FilterContainer.prototype.reset = function() {

// highly generic selectors sets
this.highlyGeneric.simple.dict.clear();
this.highlyGeneric.simple.str = '';
this.highlyGeneric.simple.mru.reset();
this.highlyGeneric.complex.dict.clear();
this.highlyGeneric.complex.str = '';
this.highlyGeneric.complex.mru.reset();

// https://developer.mozilla.org/en-US/docs/Web/CSS/:is#browser_compatibility
// https://developer.mozilla.org/en-US/docs/Web/CSS/:not#browser_compatibility
// Need support for both `:is()` and `:not()` with selector list.
this.cssIs =
vAPI.webextFlavor.soup.has('firefox') && vAPI.webextFlavor.major >= 84 ||
vAPI.webextFlavor.soup.has('chromium') && vAPI.webextFlavor.major >= 88;
};

/******************************************************************************/
Expand All @@ -303,7 +300,9 @@ FilterContainer.prototype.freeze = function() {
this.lowlyGeneric.cl.simple.size !== 0 ||
this.lowlyGeneric.cl.complex.size !== 0;

this.highlyGeneric.simple.str = Array.from(this.highlyGeneric.simple.dict).join(',\n');
this.highlyGeneric.simple.mru.reset();
this.highlyGeneric.complex.str = Array.from(this.highlyGeneric.complex.dict).join(',\n');
this.highlyGeneric.complex.mru.reset();

this.frozen = true;
Expand Down Expand Up @@ -707,7 +706,9 @@ FilterContainer.prototype.fromSelfie = function(selfie) {
this.lowlyGeneric.cl.simple = new Set(selfie.lowlyGenericSCL);
this.lowlyGeneric.cl.complex = new Map(selfie.lowlyGenericCCL);
this.highlyGeneric.simple.dict = new Set(selfie.highSimpleGenericHideArray);
this.highlyGeneric.simple.str = selfie.highSimpleGenericHideArray.join(',\n');
this.highlyGeneric.complex.dict = new Set(selfie.highComplexGenericHideArray);
this.highlyGeneric.complex.str = selfie.highComplexGenericHideArray.join(',\n');
this.needDOMSurveyor =
selfie.lowlyGenericSID.length !== 0 ||
selfie.lowlyGenericCID.length !== 0 ||
Expand Down Expand Up @@ -1041,19 +1042,14 @@ FilterContainer.prototype.retrieveSpecificSelectors = function(
// indirectly in the mru cache: this is to prevent duplication of the
// string in memory, which I have observed occurs when the string is
// stored directly as a value in a Map.
//
// https://github.com/uBlockOrigin/uBlock-issues/issues/1692
// Wrap generic selectors to prevent matching `html` or `body`
// elements. Content script-side code must unwrap those selectors
// before reporting them in a user interface.
if ( options.noGenericCosmeticFiltering !== true ) {
const exceptionSetHash = out.exceptionFilters.join();
for ( const key in this.highlyGeneric ) {
const entry = this.highlyGeneric[key];
let genericSet = entry.dict;
let cache = entry.mru.lookup(exceptionSetHash);
if ( cache === undefined ) {
cache = { s: '', excepted: [] };
let str = entry.mru.lookup(exceptionSetHash);
if ( str === undefined ) {
str = { s: entry.str, excepted: [] };
let genericSet = entry.dict;
let hit = false;
for ( const exception of exceptionSet ) {
if ( (hit = genericSet.has(exception)) ) { break; }
Expand All @@ -1062,23 +1058,18 @@ FilterContainer.prototype.retrieveSpecificSelectors = function(
genericSet = new Set(entry.dict);
for ( const exception of exceptionSet ) {
if ( genericSet.delete(exception) ) {
cache.excepted.push(exception);
str.excepted.push(exception);
}
}
str.s = Array.from(genericSet).join(',\n');
}
let genericArr = Array.from(genericSet);
if ( this.cssIs ) {
genericArr =
genericArr.map(a => `:is(${a}):not(html,body)/*hg*/`);
}
cache.s = genericArr.join(',\n');
entry.mru.add(exceptionSetHash, cache);
entry.mru.add(exceptionSetHash, str);
}
if ( cache.excepted.length !== 0 ) {
out.exceptedFilters.push(...cache.excepted);
if ( str.excepted.length !== 0 ) {
out.exceptedFilters.push(...str.excepted);
}
if ( cache.s.length !== 0 ) {
injectedHideFilters.push(cache.s);
if ( str.s.length !== 0 ) {
injectedHideFilters.push(str.s);
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/js/scriptlets/cosmetic-logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,14 @@ const processDeclarativeSimple = function(node, out) {
) {
return;
}
const unwrapSelector = vAPI.domFilterer.unwrapSelector;
for ( const selector of simpleDeclarativeSet ) {
if (
(node === document || safeMatchSelector(selector, node) === false) &&
(safeQuerySelector(selector, node) === null)
) {
continue;
}
out.push(`##${unwrapSelector(selector)}`);
out.push(`##${selector}`);
simpleDeclarativeSet.delete(selector);
simpleDeclarativeStr = undefined;
loggedSelectors.add(selector);
Expand All @@ -114,10 +113,9 @@ const processDeclarativeComplex = function(out) {
complexDeclarativeStr = safeGroupSelectors(complexDeclarativeSet);
}
if ( document.querySelector(complexDeclarativeStr) === null ) { return; }
const unwrapSelector = vAPI.domFilterer.unwrapSelector;
for ( const selector of complexDeclarativeSet ) {
if ( safeQuerySelector(selector) === null ) { continue; }
out.push(`##${unwrapSelector(selector)}`);
out.push(`##${selector}`);
complexDeclarativeSet.delete(selector);
complexDeclarativeStr = undefined;
loggedSelectors.add(selector);
Expand Down
4 changes: 2 additions & 2 deletions src/js/scriptlets/dom-inspector.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2015-present Raymond Hill
Copyright (C) 2015-2018 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -509,7 +509,7 @@ const cosmeticFilterMapper = (function() {
// Declarative selectors.
for ( const entry of (details.declarative || []) ) {
for ( const selector of entry[0].split(',\n') ) {
let canonical = vAPI.domFilterer.unwrapSelector(selector);
let canonical = selector;
let nodes;
if ( entry[1] !== vAPI.hideStyle ) {
canonical += ':style(' + entry[1] + ')';
Expand Down

0 comments on commit 5178b91

Please sign in to comment.