@@ -51,50 +51,24 @@ export function formatNumber(number: number): string {
51
51
// Returns the element found by selector string
52
52
// Supports querying through a shadow DOM using '>>>'
53
53
export function getElement ( selector : string , root : Document | ShadowRoot | HTMLElement = document ) : HTMLElement {
54
- let element ;
55
- const selectors = selector . split ( '>>>' ) ;
56
-
57
- // No shadowRoot in selector: return native querySelector
58
- if ( selectors . length == 1 ) {
59
- return root . querySelector ( selector ) ;
60
- }
61
-
62
- // shadowRoot in selector: return querySelector through shadowRoot(s)
63
- while ( selectors . length ) {
64
- if ( root ) {
65
- element = root . querySelector ( selectors . shift ( ) . trim ( ) ) ;
66
-
67
- if ( element ) {
68
- if ( selectors . length == 0 ) {
69
- return element ;
70
- } else {
71
- root = element . shadowRoot ;
72
- }
73
- } else {
74
- return null ;
75
- }
76
- } else {
77
- return null ;
78
- }
79
- }
54
+ return getElementCore ( selector , root , 'querySelector' ) as HTMLElement ;
80
55
}
81
56
82
57
// Returns the elements found by selector string
83
58
// Supports querying through a shadow DOM using '>>>'
84
- export function getElements ( selector : string , root : Document | HTMLElement | ShadowRoot = document ) : NodeListOf < HTMLElement > {
59
+ function getElementCore ( selector : string , root : Document | HTMLElement | ShadowRoot = document , queryMethod = 'querySelector' ) : HTMLElement | NodeListOf < HTMLElement > {
85
60
let element ;
86
61
const selectors = selector . split ( '>>>' ) ;
87
62
88
- // No shadowRoot in selector: return native querySelectorAll
89
- if ( selectors . length == 1 ) return root . querySelectorAll ( selector ) ;
63
+ // No shadowRoot in selector: return native querySelector[All]
64
+ if ( selectors . length == 1 ) return root [ queryMethod ] ( selector ) ;
90
65
91
- // shadowRoot in selector: return querySelectorAll through shadowRoot(s)
66
+ // shadowRoot in selector: return querySelector[All] through shadowRoot(s)
92
67
while ( selectors . length ) {
93
68
if ( root ) {
94
69
const currentSelector = selectors . shift ( ) . trim ( ) ;
95
-
96
70
if ( selectors . length == 0 ) {
97
- return root . querySelectorAll ( currentSelector ) ;
71
+ return root [ queryMethod ] ( currentSelector ) ;
98
72
} else {
99
73
element = root . querySelector ( currentSelector ) ;
100
74
if ( element ) {
@@ -109,6 +83,12 @@ export function getElements(selector: string, root: Document | HTMLElement | Sha
109
83
}
110
84
}
111
85
86
+ // Returns the elements found by selector string
87
+ // Supports querying through a shadow DOM using '>>>'
88
+ export function getElements ( selector : string , root : Document | HTMLElement | ShadowRoot = document ) : NodeListOf < HTMLElement > {
89
+ return getElementCore ( selector , root , 'querySelectorAll' ) as NodeListOf < HTMLElement > ;
90
+ }
91
+
112
92
export function getParent ( node : HTMLElement , level : number = 1 ) : HTMLElement {
113
93
if ( ! node ) {
114
94
return null ;
0 commit comments