File tree 1 file changed +14
-8
lines changed
1 file changed +14
-8
lines changed Original file line number Diff line number Diff line change @@ -293,23 +293,29 @@ export const parentsUntil = _matchUntil(
293
293
*/
294
294
export function closest < T extends Node > (
295
295
this : Cheerio < T > ,
296
- selector ?: AcceptedFilters < Node >
296
+ selector ?: AcceptedFilters < Element >
297
297
) : Cheerio < Node > {
298
298
const set : Node [ ] = [ ] ;
299
299
300
300
if ( ! selector ) {
301
301
return this . _make ( set ) ;
302
302
}
303
303
304
+ const selectOpts = {
305
+ xmlMode : this . options . xmlMode ,
306
+ root : this . _root ?. [ 0 ] ,
307
+ } ;
308
+
309
+ const selectFn =
310
+ typeof selector === 'string'
311
+ ? ( elem : Element ) => select . is ( elem , selector , selectOpts )
312
+ : getFilterFn ( selector ) ;
313
+
304
314
domEach ( this , ( elem : Node | null ) => {
305
- while ( elem && elem . type !== 'root' ) {
306
- if (
307
- ! selector ||
308
- filterArray ( [ elem ] , selector , this . options . xmlMode , this . _root ?. [ 0 ] )
309
- . length
310
- ) {
315
+ while ( elem && isTag ( elem ) ) {
316
+ if ( selectFn ( elem , 0 ) ) {
311
317
// Do not add duplicate elements to the set
312
- if ( elem && ! set . includes ( elem ) ) {
318
+ if ( ! set . includes ( elem ) ) {
313
319
set . push ( elem ) ;
314
320
}
315
321
break ;
You can’t perform that action at this time.
0 commit comments