@@ -81,26 +81,28 @@ export function createCSSEngine(shadow: boolean): SelectorEngine {
81
81
} ,
82
82
83
83
query ( root : SelectorRoot , selector : string ) : Element | undefined {
84
- const simple = root . querySelector ( selector ) ;
85
- if ( simple )
86
- return simple ;
87
- if ( ! shadow )
88
- return ;
84
+ // TODO: uncomment for performance.
85
+ // const simple = root.querySelector(selector);
86
+ // if (simple)
87
+ // return simple;
88
+ // if (!shadow)
89
+ // return;
89
90
const parts = split ( selector ) ;
90
91
if ( ! parts . length )
91
92
return ;
92
93
parts . reverse ( ) ;
93
- return queryShadowInternal ( root , root , parts ) ;
94
+ return queryShadowInternal ( root , root , parts , shadow ) ;
94
95
} ,
95
96
96
97
queryAll ( root : SelectorRoot , selector : string ) : Element [ ] {
97
- if ( ! shadow )
98
- return Array . from ( root . querySelectorAll ( selector ) ) ;
98
+ // TODO: uncomment for performance.
99
+ // if (!shadow)
100
+ // return Array.from(root.querySelectorAll(selector));
99
101
const result : Element [ ] = [ ] ;
100
102
const parts = split ( selector ) ;
101
103
if ( parts . length ) {
102
104
parts . reverse ( ) ;
103
- queryShadowAllInternal ( root , root , parts , result ) ;
105
+ queryShadowAllInternal ( root , root , parts , shadow , result ) ;
104
106
}
105
107
return result ;
106
108
}
@@ -109,43 +111,45 @@ export function createCSSEngine(shadow: boolean): SelectorEngine {
109
111
return engine ;
110
112
}
111
113
112
- function queryShadowInternal ( boundary : SelectorRoot , root : SelectorRoot , parts : string [ ] ) : Element | undefined {
114
+ function queryShadowInternal ( boundary : SelectorRoot , root : SelectorRoot , parts : string [ ] , shadow : boolean ) : Element | undefined {
113
115
const matching = root . querySelectorAll ( parts [ 0 ] ) ;
114
116
for ( let i = 0 ; i < matching . length ; i ++ ) {
115
117
const element = matching [ i ] ;
116
118
if ( parts . length === 1 || matches ( element , parts , boundary ) )
117
119
return element ;
118
120
}
121
+ if ( ! shadow )
122
+ return ;
119
123
if ( ( root as Element ) . shadowRoot ) {
120
- const child = queryShadowInternal ( boundary , ( root as Element ) . shadowRoot ! , parts ) ;
124
+ const child = queryShadowInternal ( boundary , ( root as Element ) . shadowRoot ! , parts , shadow ) ;
121
125
if ( child )
122
126
return child ;
123
127
}
124
128
const elements = root . querySelectorAll ( '*' ) ;
125
129
for ( let i = 0 ; i < elements . length ; i ++ ) {
126
130
const element = elements [ i ] ;
127
131
if ( element . shadowRoot ) {
128
- const child = queryShadowInternal ( boundary , element . shadowRoot , parts ) ;
132
+ const child = queryShadowInternal ( boundary , element . shadowRoot , parts , shadow ) ;
129
133
if ( child )
130
134
return child ;
131
135
}
132
136
}
133
137
}
134
138
135
- function queryShadowAllInternal ( boundary : SelectorRoot , root : SelectorRoot , parts : string [ ] , result : Element [ ] ) {
139
+ function queryShadowAllInternal ( boundary : SelectorRoot , root : SelectorRoot , parts : string [ ] , shadow : boolean , result : Element [ ] ) {
136
140
const matching = root . querySelectorAll ( parts [ 0 ] ) ;
137
141
for ( let i = 0 ; i < matching . length ; i ++ ) {
138
142
const element = matching [ i ] ;
139
143
if ( parts . length === 1 || matches ( element , parts , boundary ) )
140
144
result . push ( element ) ;
141
145
}
142
- if ( ( root as Element ) . shadowRoot )
143
- queryShadowAllInternal ( boundary , ( root as Element ) . shadowRoot ! , parts , result ) ;
146
+ if ( shadow && ( root as Element ) . shadowRoot )
147
+ queryShadowAllInternal ( boundary , ( root as Element ) . shadowRoot ! , parts , shadow , result ) ;
144
148
const elements = root . querySelectorAll ( '*' ) ;
145
149
for ( let i = 0 ; i < elements . length ; i ++ ) {
146
150
const element = elements [ i ] ;
147
- if ( element . shadowRoot )
148
- queryShadowAllInternal ( boundary , element . shadowRoot , parts , result ) ;
151
+ if ( shadow && element . shadowRoot )
152
+ queryShadowAllInternal ( boundary , element . shadowRoot , parts , shadow , result ) ;
149
153
}
150
154
}
151
155
0 commit comments