From 7dd48a6c8c4d9153b4bc3f56904f3abfb5b23512 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Thu, 30 Jul 2020 11:58:49 -0400 Subject: [PATCH] Allow `:upward()` operator to select `html` element Related feedback: - https://www.reddit.com/r/uBlockOrigin/comments/hvwwj0/element_hiding_by_url_not_by_domain_is_it_possible/fyzykw0/ --- src/js/contentscript.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/js/contentscript.js b/src/js/contentscript.js index 4964cb47a..da8e4c7aa 100644 --- a/src/js/contentscript.js +++ b/src/js/contentscript.js @@ -573,8 +573,12 @@ vAPI.injectScriptlet = function(doc, text) { const PSelectorSpathTask = class { constructor(task) { this.spath = task[1]; + this.nth = /^(?:\s*[+~]|:)/.test(this.spath); } - transpose(node, output) { + qsa(node) { + if ( this.nth === false ) { + return node.querySelectorAll(this.spath); + } const parent = node.parentElement; if ( parent === null ) { return; } let pos = 1; @@ -583,9 +587,13 @@ vAPI.injectScriptlet = function(doc, text) { if ( node === null ) { break; } pos += 1; } - const nodes = parent.querySelectorAll( + return parent.querySelectorAll( `:scope > :nth-child(${pos})${this.spath}` ); + } + transpose(node, output) { + const nodes = this.qsa(node); + if ( nodes === undefined ) { return; } for ( const node of nodes ) { output.push(node); }