Skip to content

Commit

Permalink
Fix undue invalidation of pseudo element-based cosmetic filters
Browse files Browse the repository at this point in the history
Regression from:
- 97befd1

Related issue:
- uBlockOrigin/uBlock-issues#2170
  • Loading branch information
gorhill committed Jul 18, 2022
1 parent 0410f62 commit 9aeadee
Showing 1 changed file with 13 additions and 32 deletions.
45 changes: 13 additions & 32 deletions src/js/static-filtering-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1351,35 +1351,17 @@ Parser.prototype.SelectorCompiler = class {
// Resulting regex literal:
// ^(?:[A-Za-z_][\w-]*(?:[.#][A-Za-z_][\w-]*)*(?:\[[A-Za-z_][\w-]*[*^$]?="[^"\]\\]+"\])*|[.#][A-Za-z_][\w-]*(?:[.#][A-Za-z_][\w-]*)*(?:\[[A-Za-z_][\w-]*[*^$]?="[^"\]\\]+"\])*|\[[A-Za-z_][\w-]*[*^$]?="[^"\]\\]+"\](?:\[[A-Za-z_][\w-]*[*^$]?="[^"\]\\]+"\])*)(?:(?:\s+|\s*[>+~]\s*)(?:[A-Za-z_][\w-]*(?:[.#][A-Za-z_][\w-]*)*(?:\[[A-Za-z_][\w-]*[*^$]?="[^"\]\\]+"\])*|[.#][A-Za-z_][\w-]*(?:[.#][A-Za-z_][\w-]*)*(?:\[[A-Za-z_][\w-]*[*^$]?="[^"\]\\]+"\])*|\[[A-Za-z_][\w-]*[*^$]?="[^"\]\\]+"\](?:\[[A-Za-z_][\w-]*[*^$]?="[^"\]\\]+"\])*))*$

// We use an actual stylesheet to validate uncommon CSS selectors
// which can be used in a CSS declaration.
this.sheetValidatorRule = null;
(( ) => {
if ( typeof document !== 'object' ) { return; }
if ( document === null ) { return; }
try {
const styleElement = document.createElement('style');
document.body.append(styleElement);
const sheet = styleElement.sheet;
styleElement.remove();
sheet.insertRule('_z{color:red}');
const rule = sheet.cssRules[0];
this.sheetValidatorRule = rule;
} catch(ex) {
}
})();

// We use another stylsheet to validate CSS properties which can be
// used in a CSS declaration.
this.styleValidatorElement = null;
// We use an actual stylesheet to validate uncommon CSS selectors and
// CSS properties which can be used in a CSS declaration.
this.cssValidatorElement = null;
(( ) => {
if ( typeof document !== 'object' ) { return; }
if ( document === null ) { return; }
try {
const styleElement = document.createElement('style');
styleElement.appendChild(document.createTextNode(' '));
document.body.append(styleElement);
this.styleValidatorElement = styleElement;
this.cssValidatorElement = styleElement;
} catch(ex) {
}
})();
Expand All @@ -1402,7 +1384,6 @@ Parser.prototype.SelectorCompiler = class {
this.reDropScope = /^\s*:scope\s*(?=[+>~])/;
this.reIsDanglingSelector = /[+>~\s]\s*$/;
this.reIsCombinator = /^\s*[+>~]/;
this.reUnicodeSpecials = /[\uFFF0-\uFFFF]/;
this.regexToRawValue = new Map();
// https://github.com/gorhill/uBlock/issues/2793
this.normalizedOperators = new Map([
Expand Down Expand Up @@ -1508,14 +1489,14 @@ Parser.prototype.SelectorCompiler = class {
// Forbid multiple and unexpected CSS style declarations.
sheetSelectable(s) {
if ( this.reCommonSelector.test(s) ) { return true; }
const rule = this.sheetValidatorRule;
if ( rule === null ) { return true; }
if ( this.cssValidatorElement === null ) { return true; }
let valid = false;
try {
rule.selectorText = '_z';
rule.selectorText = `_z + ${s}`;
valid = rule.selectorText !== '_z' &&
this.reUnicodeSpecials.test(rule.selectorText) === false;
this.cssValidatorElement.childNodes[0].nodeValue = `_z + ${s}{color:red;} _z{color:red;}`;
const rules = this.cssValidatorElement.sheet.cssRules;
valid = rules.length === 2 &&
rules[0].style.cssText !== '' &&
rules[1].style.cssText !== '';
} catch (ex) {
}
return valid;
Expand Down Expand Up @@ -1639,11 +1620,11 @@ Parser.prototype.SelectorCompiler = class {
// - opening comment `/*`
compileStyleProperties(s) {
if ( /image-set\(|url\(|\/\s*\/|\\|\/\*/i.test(s) ) { return; }
if ( this.styleValidatorElement === null ) { return s; }
if ( this.cssValidatorElement === null ) { return s; }
let valid = false;
try {
this.styleValidatorElement.childNodes[0].nodeValue = `_z{${s}} _z{color:red;}`;
const rules = this.styleValidatorElement.sheet.cssRules;
this.cssValidatorElement.childNodes[0].nodeValue = `_z{${s}} _z{color:red;}`;
const rules = this.cssValidatorElement.sheet.cssRules;
valid = rules.length >= 2 &&
rules[0].style.cssText !== '' &&
rules[1].style.cssText !== '';
Expand Down

0 comments on commit 9aeadee

Please sign in to comment.