@@ -28,7 +28,7 @@ const _compileRegEx = (regexText: string): RegExp => {
28
28
//
29
29
// If `exact` is true, then the string case it tested
30
30
// for an exact match (the regex case is not affected).
31
- const _testMatches = ( test : string , value : string , exact : boolean = false ) : boolean => {
31
+ const _testMatches = ( test : string , value : string , exact = false ) : boolean => {
32
32
if ( test [ 0 ] === '/' ) {
33
33
return value . match ( _compileRegEx ( test ) ) !== null
34
34
}
@@ -155,20 +155,20 @@ const _allChildrenRecursive = (element: HTMLElement): HTMLElement[] => {
155
155
. filter ( e => e !== null ) as HTMLElement [ ]
156
156
}
157
157
158
+ const _stripCssOperator = ( operator : string , selector : string ) => {
159
+ if ( selector [ 0 ] !== operator ) {
160
+ throw new Error (
161
+ `Expected to find ${ operator } in initial position of "${ selector } ` )
162
+ }
163
+ return selector . replace ( operator , '' ) . trimStart ( )
164
+ }
165
+
158
166
// Implementation of ":css-selector" rule
159
167
const operatorCssSelector = ( selector : CSSSelector ,
160
168
element : HTMLElement ) : OperatorResult => {
161
- const _stripOperator = ( operator : string , selector : string ) => {
162
- if ( selector [ 0 ] !== operator ) {
163
- throw new Error (
164
- `Expected to find ${ operator } in initial position of "${ selector } ` )
165
- }
166
- return selector . replace ( operator , '' ) . trimStart ( )
167
- }
168
-
169
169
const trimmedSelector = selector . trimStart ( )
170
170
if ( trimmedSelector . startsWith ( '+' ) ) {
171
- const subOperator = _stripOperator ( '+' , trimmedSelector )
171
+ const subOperator = _stripCssOperator ( '+' , trimmedSelector )
172
172
if ( subOperator === null ) {
173
173
return [ ]
174
174
}
@@ -179,15 +179,15 @@ const operatorCssSelector = (selector: CSSSelector,
179
179
return nextSibNode . matches ( subOperator ) ? [ nextSibNode ] : [ ]
180
180
}
181
181
else if ( trimmedSelector . startsWith ( '~' ) ) {
182
- const subOperator = _stripOperator ( '~' , trimmedSelector )
182
+ const subOperator = _stripCssOperator ( '~' , trimmedSelector )
183
183
if ( subOperator === null ) {
184
184
return [ ]
185
185
}
186
186
const allSiblingNodes = _allOtherSiblings ( element )
187
187
return allSiblingNodes . filter ( x => x . matches ( subOperator ) )
188
188
}
189
189
else if ( trimmedSelector . startsWith ( '>' ) ) {
190
- const subOperator = _stripOperator ( '>' , trimmedSelector )
190
+ const subOperator = _stripCssOperator ( '>' , trimmedSelector )
191
191
if ( subOperator === null ) {
192
192
return [ ]
193
193
}
@@ -201,9 +201,7 @@ const operatorCssSelector = (selector: CSSSelector,
201
201
if ( element . matches ( selector ) ) {
202
202
return [ element ]
203
203
}
204
- else {
205
- return [ ]
206
- }
204
+ return [ ]
207
205
}
208
206
209
207
const _hasPlainSelectorCase = ( selector : CSSSelector ,
@@ -346,7 +344,8 @@ const _upwardIntCase = (intNeedle: NeedlePosition,
346
344
}
347
345
if ( currentElement === null ) {
348
346
return [ ]
349
- } else {
347
+ }
348
+ else {
350
349
const htmlElement = _asHTMLElement ( currentElement )
351
350
return ( htmlElement === null ) ? [ ] : [ htmlElement ]
352
351
}
@@ -463,8 +462,8 @@ const fastPathOperatorTypes: OperatorType[] = [
463
462
'matches-path' ,
464
463
]
465
464
466
- const applyCompiledSelector = ( selector : CompiledProceduralSelector ,
467
- initNodes ?: HTMLElement [ ] ) : HTMLElement [ ] => {
465
+ const _determineInitNodesAndIndex = ( selector : CompiledProceduralSelector ,
466
+ initNodes ?: HTMLElement [ ] ) : [ number , HTMLElement [ ] ] => {
468
467
let nodesToConsider : HTMLElement [ ] = [ ]
469
468
let index = 0
470
469
@@ -499,6 +498,13 @@ const applyCompiledSelector = (selector: CompiledProceduralSelector,
499
498
const allNodes = W . Array . from ( W . document . all )
500
499
nodesToConsider = allNodes . filter ( _asHTMLElement ) as HTMLElement [ ]
501
500
}
501
+ return [ index , nodesToConsider ]
502
+ }
503
+
504
+ const applyCompiledSelector = ( selector : CompiledProceduralSelector ,
505
+ initNodes ?: HTMLElement [ ] ) : HTMLElement [ ] => {
506
+ const initState = _determineInitNodesAndIndex ( selector , initNodes )
507
+ let [ index , nodesToConsider ] = initState
502
508
503
509
const numOperators = selector . length
504
510
for ( index ; nodesToConsider . length > 0 && index < numOperators ; ++ index ) {
0 commit comments