From f1889b02ee4f4770d625d9c9330a449e316b160a Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 22 Jan 2024 10:57:48 -0500 Subject: [PATCH] [mv3] Fix conversion of `:xpath` procedural filters Procedural filters with `:xpath` operator were silently rejected at conversion time because the parser was failing to evaluate the xpath expression due to the absence of a `document` object in nodejs. If `document` object is not present, the parser will assume the xpath expression is valid. --- platform/mv3/extension/js/scripting/css-procedural.js | 7 +++++++ src/js/static-dnr-filtering.js | 6 ++++-- src/js/static-filtering-parser.js | 1 + src/js/static-net-filtering.js | 2 +- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/platform/mv3/extension/js/scripting/css-procedural.js b/platform/mv3/extension/js/scripting/css-procedural.js index 818e697b0..8293278be 100644 --- a/platform/mv3/extension/js/scripting/css-procedural.js +++ b/platform/mv3/extension/js/scripting/css-procedural.js @@ -566,6 +566,13 @@ class PSelectorRoot extends PSelector { } return []; } + exec(input) { + try { + return super.exec(input); + } catch (ex) { + } + return []; + } } /******************************************************************************/ diff --git a/src/js/static-dnr-filtering.js b/src/js/static-dnr-filtering.js index fb677add3..ca66b861d 100644 --- a/src/js/static-dnr-filtering.js +++ b/src/js/static-dnr-filtering.js @@ -299,10 +299,10 @@ function addToDNR(context, list) { if ( parser.isComment() ) { if ( line === `!#trusted on ${context.secret}` ) { - parser.trustedSource = true; + parser.options.trustedSource = true; context.trustedSource = true; } else if ( line === `!#trusted off ${context.secret}` ) { - parser.trustedSource = false; + parser.options.trustedSource = false; context.trustedSource = false; } continue; @@ -312,6 +312,8 @@ function addToDNR(context, list) { if ( parser.hasError() ) { if ( parser.astError === sfp.AST_ERROR_OPTION_EXCLUDED ) { context.invalid.add(`Incompatible with DNR: ${line}`); + } else { + context.invalid.add(`Rejected filter: ${line}`); } continue; } diff --git a/src/js/static-filtering-parser.js b/src/js/static-filtering-parser.js index 908f83ef8..465d9f4e7 100644 --- a/src/js/static-filtering-parser.js +++ b/src/js/static-filtering-parser.js @@ -4022,6 +4022,7 @@ class ExtSelectorCompiler { compileXpathExpression(s) { const r = this.unquoteString(s); if ( r.i !== s.length ) { return; } + if ( globalThis.document instanceof Object === false ) { return r.s; } try { globalThis.document.createExpression(r.s, null); } catch (e) { diff --git a/src/js/static-net-filtering.js b/src/js/static-net-filtering.js index d2da484f4..8e6b3a4bc 100644 --- a/src/js/static-net-filtering.js +++ b/src/js/static-net-filtering.js @@ -4571,7 +4571,7 @@ FilterContainer.prototype.dnrFromCompiled = function(op, context, ...args) { } break; case 'uritransform': { - dnrAddRuleError(rule, `Unsupported uritransform=${rule.__modifierValue}`); + dnrAddRuleError(rule, `Incompatible with DNR: uritransform=${rule.__modifierValue}`); break; } default: