@@ -28,7 +28,7 @@ const _compileRegEx = (regexText: string): RegExp => {
2828//
2929// If `exact` is true, then the string case it tested
3030// 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 => {
3232 if ( test [ 0 ] === '/' ) {
3333 return value . match ( _compileRegEx ( test ) ) !== null
3434 }
@@ -155,20 +155,20 @@ const _allChildrenRecursive = (element: HTMLElement): HTMLElement[] => {
155155 . filter ( e => e !== null ) as HTMLElement [ ]
156156}
157157
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+
158166// Implementation of ":css-selector" rule
159167const operatorCssSelector = ( selector : CSSSelector ,
160168 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-
169169 const trimmedSelector = selector . trimStart ( )
170170 if ( trimmedSelector . startsWith ( '+' ) ) {
171- const subOperator = _stripOperator ( '+' , trimmedSelector )
171+ const subOperator = _stripCssOperator ( '+' , trimmedSelector )
172172 if ( subOperator === null ) {
173173 return [ ]
174174 }
@@ -179,15 +179,15 @@ const operatorCssSelector = (selector: CSSSelector,
179179 return nextSibNode . matches ( subOperator ) ? [ nextSibNode ] : [ ]
180180 }
181181 else if ( trimmedSelector . startsWith ( '~' ) ) {
182- const subOperator = _stripOperator ( '~' , trimmedSelector )
182+ const subOperator = _stripCssOperator ( '~' , trimmedSelector )
183183 if ( subOperator === null ) {
184184 return [ ]
185185 }
186186 const allSiblingNodes = _allOtherSiblings ( element )
187187 return allSiblingNodes . filter ( x => x . matches ( subOperator ) )
188188 }
189189 else if ( trimmedSelector . startsWith ( '>' ) ) {
190- const subOperator = _stripOperator ( '>' , trimmedSelector )
190+ const subOperator = _stripCssOperator ( '>' , trimmedSelector )
191191 if ( subOperator === null ) {
192192 return [ ]
193193 }
@@ -201,9 +201,7 @@ const operatorCssSelector = (selector: CSSSelector,
201201 if ( element . matches ( selector ) ) {
202202 return [ element ]
203203 }
204- else {
205- return [ ]
206- }
204+ return [ ]
207205}
208206
209207const _hasPlainSelectorCase = ( selector : CSSSelector ,
@@ -346,7 +344,8 @@ const _upwardIntCase = (intNeedle: NeedlePosition,
346344 }
347345 if ( currentElement === null ) {
348346 return [ ]
349- } else {
347+ }
348+ else {
350349 const htmlElement = _asHTMLElement ( currentElement )
351350 return ( htmlElement === null ) ? [ ] : [ htmlElement ]
352351 }
@@ -463,8 +462,8 @@ const fastPathOperatorTypes: OperatorType[] = [
463462 'matches-path' ,
464463]
465464
466- const applyCompiledSelector = ( selector : CompiledProceduralSelector ,
467- initNodes ?: HTMLElement [ ] ) : HTMLElement [ ] => {
465+ const _determineInitNodesAndIndex = ( selector : CompiledProceduralSelector ,
466+ initNodes ?: HTMLElement [ ] ) : [ number , HTMLElement [ ] ] => {
468467 let nodesToConsider : HTMLElement [ ] = [ ]
469468 let index = 0
470469
@@ -499,6 +498,13 @@ const applyCompiledSelector = (selector: CompiledProceduralSelector,
499498 const allNodes = W . Array . from ( W . document . all )
500499 nodesToConsider = allNodes . filter ( _asHTMLElement ) as HTMLElement [ ]
501500 }
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
502508
503509 const numOperators = selector . length
504510 for ( index ; nodesToConsider . length > 0 && index < numOperators ; ++ index ) {
0 commit comments