@@ -24,7 +24,7 @@ export default class QuerySelector {
24
24
const matches = new NodeList < Element > ( ) ;
25
25
26
26
for ( const parts of this . getSelectorParts ( selector ) ) {
27
- for ( const element of this . findAll ( node , [ node ] , parts ) ) {
27
+ for ( const element of this . findAll ( node , parts ) ) {
28
28
if ( ! matches . includes ( element ) ) {
29
29
matches . push ( element ) ;
30
30
}
@@ -43,7 +43,7 @@ export default class QuerySelector {
43
43
*/
44
44
public static querySelector ( node : Node , selector : string ) : Element {
45
45
for ( const parts of this . getSelectorParts ( selector ) ) {
46
- const match = this . findFirst ( node , [ node ] , parts ) ;
46
+ const match = this . findFirst ( node , parts ) ;
47
47
48
48
if ( match ) {
49
49
return match ;
@@ -128,33 +128,32 @@ export default class QuerySelector {
128
128
* @param [selectorItem] Selector item.
129
129
* @returns HTML elements.
130
130
*/
131
- private static findAll ( rootNode : Node , nodes : Node [ ] , selectorParts : string [ ] , selectorItem ?: SelectorItem ) : Element [ ] {
131
+ private static findAll ( rootNode : Node , selectorParts : string [ ] , selectorItem ?: SelectorItem ) : Element [ ] {
132
132
const isDirectChild = selectorParts [ 0 ] === '>' ;
133
133
if ( isDirectChild ) {
134
134
selectorParts = selectorParts . slice ( 1 ) ;
135
135
}
136
136
const selector = selectorItem || new SelectorItem ( selectorParts [ 0 ] ) ;
137
137
let matched = [ ] ;
138
-
139
- for ( const node of nodes ) {
138
+ let node = rootNode . firstChild ;
139
+ while ( node ) {
140
140
if ( node . nodeType === Node . ELEMENT_NODE ) {
141
141
if ( selector . match ( < Element > node ) . matches ) {
142
142
if ( selectorParts . length === 1 ) {
143
143
if ( rootNode !== node ) {
144
144
matched . push ( node ) ;
145
145
}
146
146
} else {
147
- if ( node . firstChild ) {
148
- const childMatches = this . findAll ( rootNode , ( < Element > node ) . children , selectorParts . slice ( 1 ) , null ) ;
149
- matched = matched . concat ( childMatches ) ;
150
- }
147
+ const childMatches = this . findAll ( rootNode , selectorParts . slice ( 1 ) , null ) ;
148
+ matched = matched . concat ( childMatches ) ;
151
149
}
152
150
}
153
151
}
154
152
155
153
if ( ! isDirectChild && node [ 'firstChild' ] ) {
156
- matched = matched . concat ( this . findAll ( rootNode , node [ 'children' ] , selectorParts , selector ) ) ;
154
+ matched = matched . concat ( this . findAll ( rootNode , selectorParts , selector ) ) ;
157
155
}
156
+ node = node . nextSibling ;
158
157
}
159
158
160
159
return matched ;
@@ -170,34 +169,36 @@ export default class QuerySelector {
170
169
* @param [selectorItem] Selector item.
171
170
* @returns HTML element.
172
171
*/
173
- private static findFirst ( rootNode : Node , nodes : Node [ ] , selectorParts : string [ ] , selectorItem ?: SelectorItem ) : Element {
172
+ private static findFirst ( rootNode : Node , selectorParts : string [ ] , selectorItem ?: SelectorItem ) : Element {
174
173
const isDirectChild = selectorParts [ 0 ] === '>' ;
175
174
if ( isDirectChild ) {
176
175
selectorParts = selectorParts . slice ( 1 ) ;
177
176
}
178
177
const selector = selectorItem || new SelectorItem ( selectorParts [ 0 ] ) ;
178
+ let node = rootNode . firstChild ;
179
179
180
- for ( const node of nodes ) {
180
+ while ( node ) {
181
181
if ( node . nodeType === Node . ELEMENT_NODE && selector . match ( < Element > node ) . matches ) {
182
182
if ( selectorParts . length === 1 ) {
183
183
if ( rootNode !== node ) {
184
184
return < Element > node ;
185
185
}
186
186
} else {
187
- const childSelector = this . findFirst ( rootNode , ( < Element > node ) . children , selectorParts . slice ( 1 ) , null ) ;
187
+ const childSelector = this . findFirst ( rootNode , selectorParts . slice ( 1 ) , null ) ;
188
188
if ( childSelector ) {
189
189
return childSelector ;
190
190
}
191
191
}
192
192
}
193
193
194
194
if ( ! isDirectChild && node [ 'firstChild' ] ) {
195
- const childSelector = this . findFirst ( rootNode , node [ 'children' ] , selectorParts , selector ) ;
195
+ const childSelector = this . findFirst ( rootNode , selectorParts , selector ) ;
196
196
197
197
if ( childSelector ) {
198
198
return childSelector ;
199
199
}
200
200
}
201
+ node = node . nextSibling ;
201
202
}
202
203
203
204
return null ;
0 commit comments