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); }