Skip to content

Commit

Permalink
[mv3] Fix conversion of :xpath procedural filters
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gorhill committed Jan 22, 2024
1 parent 46e19e4 commit f1889b0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
7 changes: 7 additions & 0 deletions platform/mv3/extension/js/scripting/css-procedural.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,13 @@ class PSelectorRoot extends PSelector {
}
return [];
}
exec(input) {
try {
return super.exec(input);
} catch (ex) {
}
return [];
}
}

/******************************************************************************/
Expand Down
6 changes: 4 additions & 2 deletions src/js/static-dnr-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/js/static-filtering-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/static-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit f1889b0

Please sign in to comment.