1
0
mirror of https://github.com/gorhill/uBlock.git synced 2024-09-02 17:19:38 +02:00

Fix broken :not() operator when forcing parsing as procedural

Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/2262
This commit is contained in:
Raymond Hill 2022-09-13 16:15:22 -04:00
parent 93e5133783
commit d11a3f2fa3
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2

View File

@ -1534,6 +1534,9 @@ Parser.prototype.SelectorCompiler = class {
compileProceduralSelector(raw, asProcedural = false) { compileProceduralSelector(raw, asProcedural = false) {
const compiled = this.compileProcedural(raw, true, asProcedural); const compiled = this.compileProcedural(raw, true, asProcedural);
if ( compiled !== undefined ) { if ( compiled !== undefined ) {
if ( asProcedural ) {
this.optimizeCompiledProcedural(compiled);
}
compiled.raw = this.decompileProcedural(compiled); compiled.raw = this.decompileProcedural(compiled);
} }
return compiled; return compiled;
@ -1688,6 +1691,31 @@ Parser.prototype.SelectorCompiler = class {
return s; return s;
} }
optimizeCompiledProcedural(compiled) {
if ( typeof compiled === 'string' ) { return; }
if ( Array.isArray(compiled.tasks) === false ) { return; }
const tasks = [];
let selector = compiled.selector;
for ( const task of compiled.tasks ) {
switch ( task[0] ) {
case ':not':
case ':if-not':
this.optimizeCompiledProcedural(task[1]);
if ( tasks.length === 0 && typeof task[1] === 'string' ) {
selector += `:not(${task[1]})`;
break;
}
tasks.push(task);
break;
default:
tasks.push(task);
break;
}
}
compiled.selector = selector;
compiled.tasks = tasks.length !== 0 ? tasks : undefined;
}
// https://github.com/gorhill/uBlock/issues/2793#issuecomment-333269387 // https://github.com/gorhill/uBlock/issues/2793#issuecomment-333269387
// Normalize (somewhat) the stringified version of procedural // Normalize (somewhat) the stringified version of procedural
// cosmetic filters -- this increase the likelihood of detecting // cosmetic filters -- this increase the likelihood of detecting
@ -1696,6 +1724,7 @@ Parser.prototype.SelectorCompiler = class {
// The normalized string version is what is reported in the logger, // The normalized string version is what is reported in the logger,
// by design. // by design.
decompileProcedural(compiled) { decompileProcedural(compiled) {
if ( typeof compiled === 'string' ) { return compiled; }
const tasks = compiled.tasks || []; const tasks = compiled.tasks || [];
const raw = [ compiled.selector ]; const raw = [ compiled.selector ];
for ( const task of tasks ) { for ( const task of tasks ) {
@ -1736,6 +1765,10 @@ Parser.prototype.SelectorCompiler = class {
break; break;
case ':not': case ':not':
case ':if-not': case ':if-not':
if ( typeof(task[1]) === 'string' ) {
raw.push(`:not(${task[1]})`);
break;
}
raw.push(`:not(${this.decompileProcedural(task[1])})`); raw.push(`:not(${this.decompileProcedural(task[1])})`);
break; break;
case ':spath': case ':spath':