1717* Another example querySelectorAllDeep('#downloads-list div#title-area + a');
1818e.g.
1919*/
20- export function querySelectorAllDeep ( selector ) {
21- return _querySelectorDeep ( selector , true ) ;
20+ export function querySelectorAllDeep ( selector , root = document ) {
21+ return _querySelectorDeep ( selector , true , root ) ;
2222}
2323
24- export function querySelectorDeep ( selector ) {
25- return _querySelectorDeep ( selector ) ;
24+ export function querySelectorDeep ( selector , root = document ) {
25+ return _querySelectorDeep ( selector , false , root ) ;
2626}
2727
28- function _querySelectorDeep ( selector , findMany ) {
29- let lightElement = document . querySelector ( selector ) ;
28+ function _querySelectorDeep ( selector , findMany , root ) {
29+ let lightElement = root . querySelector ( selector ) ;
3030
3131 if ( document . head . createShadowRoot || document . head . attachShadow ) {
3232 // no need to do any special if selector matches something specific in light-dom
@@ -50,8 +50,8 @@ function _querySelectorDeep(selector, findMany) {
5050 // filter out entry white selectors
5151 . filter ( ( entry ) => ! ! entry ) ;
5252 const possibleElementsIndex = splitSelector . length - 1 ;
53- const possibleElements = collectAllElementsDeep ( splitSelector [ possibleElementsIndex ] ) ;
54- const findElements = findMatchingElement ( splitSelector , possibleElementsIndex ) ;
53+ const possibleElements = collectAllElementsDeep ( splitSelector [ possibleElementsIndex ] , root ) ;
54+ const findElements = findMatchingElement ( splitSelector , possibleElementsIndex , root ) ;
5555 if ( findMany ) {
5656 acc = acc . concat ( possibleElements . filter ( findElements ) ) ;
5757 return acc ;
@@ -66,13 +66,13 @@ function _querySelectorDeep(selector, findMany) {
6666 if ( ! findMany ) {
6767 return lightElement ;
6868 } else {
69- return document . querySelectorAll ( selector ) ;
69+ return root . querySelectorAll ( selector ) ;
7070 }
7171 }
7272
7373}
7474
75- function findMatchingElement ( splitSelector , possibleElementsIndex ) {
75+ function findMatchingElement ( splitSelector , possibleElementsIndex , root ) {
7676 return ( element ) => {
7777 let position = possibleElementsIndex ;
7878 let parent = element ;
@@ -86,7 +86,7 @@ function findMatchingElement(splitSelector, possibleElementsIndex) {
8686 if ( foundMatch ) {
8787 position -- ;
8888 }
89- parent = findParentOrHost ( parent ) ;
89+ parent = findParentOrHost ( parent , root ) ;
9090 }
9191 return foundElement ;
9292 } ;
@@ -112,9 +112,9 @@ function splitByCharacterUnlessQuoted(selector, character) {
112112}
113113
114114
115- function findParentOrHost ( element ) {
115+ function findParentOrHost ( element , root ) {
116116 const parentNode = element . parentNode ;
117- return ( parentNode && parentNode . host && parentNode . nodeType === 11 ) ? parentNode . host : parentNode === document ? null : parentNode ;
117+ return ( parentNode && parentNode . host && parentNode . nodeType === 11 ) ? parentNode . host : parentNode === root ? null : parentNode ;
118118}
119119
120120/**
@@ -124,7 +124,7 @@ function findParentOrHost(element) {
124124 * @author ebidel@ (Eric Bidelman)
125125 * License Apache-2.0
126126 */
127- function collectAllElementsDeep ( selector = null ) {
127+ function collectAllElementsDeep ( selector = null , root ) {
128128 const allElements = [ ] ;
129129
130130 const findAllElements = function ( nodes ) {
@@ -137,7 +137,7 @@ function collectAllElementsDeep(selector = null) {
137137 }
138138 } ;
139139
140- findAllElements ( document . querySelectorAll ( '*' ) ) ;
140+ findAllElements ( root . querySelectorAll ( '*' ) ) ;
141141
142142 return selector ? allElements . filter ( el => el . matches ( selector ) ) : allElements ;
143143}
0 commit comments