import { CheerioOptions , InternalOptions , default as defaultOptions , flatten as flattenOptions , } from './options' ; import * as staticMethods from './static' ; import { Cheerio } from './cheerio' ; import { isHtml , isCheerio } from './utils' ; import type { Node , Document , Element } from 'domhandler' ; import { SelectorType , BasicAcceptedElems } from './types' ; type StaticType = typeof staticMethods ; /**
 * A querying function, bound to a document created from the provided markup.
 *
 * Also provides several helper methods for dealing with the document as a whole.
 */ export interface CheerioAPI extends StaticType { example param selector param context param root < T extends Node , S extends string > ( selector ? , context ? , root ? , options ? ) : Cheerio < S extends SelectorType ? Element : T > ; private _root : Document ; private _options : InternalOptions ; /** Mimic jQuery's prototype alias for plugin authors. */ fn : typeof Cheerio . prototype ; load : ReturnType < typeof getLoad > ; } export function getLoad ( parse , render ) { param content param options param isDocument returns see https for additional usage information.
      return function load ( content , options ? , isDocument = true ) : CheerioAPI { if ( ( content as string | null ) == null ) { throw new Error ( 'cheerio.load() expects a string' ) ; } const internalOpts = { ... defaultOptions , ... flattenOptions ( options ) } ; const initialRoot = parse ( content , internalOpts , isDocument ) ; /** Create an extended class here, so that extensions only live on one instance. */ class LoadedCheerio < T > extends Cheerio < T > { _make < T > ( selector ? , context ? ) { const cheerio = initialize ( selector , context ) ; cheerio . prevObject = this ; return cheerio ; } _parse ( content , options , isDocument ) { return parse ( content , options , isDocument ) ; } _render ( dom ) { return render ( dom , this . options ) ; } } function initialize < T = Node , S extends string = string > ( selector ? , context ? , root = initialRoot , opts ? ) { type Result = S extends SelectorType ? Element : T ; if ( selector && isCheerio < Result > ( selector ) ) return selector ; const options = { ... internalOpts , ... flattenOptions ( opts ) , } ; const r = typeof root === 'string' ? [ parse ( root , options , false ) ] : 'length' in root ? root : [ root ] ; const rootInstance = isCheerio < Document > ( r ) ? r : new LoadedCheerio < Document > ( r , null , options ) ; rootInstance . _root = rootInstance ; if ( ! selector ) { return new LoadedCheerio < Result > ( undefined , rootInstance , options ) ; } const elements = typeof selector === 'string' && isHtml ( selector ) ? parse ( selector , options , false ) . children : isNode ( selector ) ? [ selector ] : Array . isArray ( selector ) ? selector : undefined ; const instance = new LoadedCheerio ( elements , rootInstance , options ) ; if ( elements ) { return instance as any ; } if ( typeof selector !== 'string' ) { throw new Error ( 'Unexpected type of selector' ) ; } let search = selector ; const searchContext = ! context ? rootInstance : typeof context === 'string' ? isHtml ( context ) ? new LoadedCheerio < Document > ( [ parse ( context , options , false ) ] , rootInstance , options ) : ( ( search = `${ context } ${ search }` as S ) , rootInstance ) : isCheerio < Node > ( context ) ? context : new LoadedCheerio < Node > ( Array . isArray ( context ) ? context : [ context ] , rootInstance , options ) ; if ( ! searchContext ) return instance as any ; return searchContext . find ( search ) as Cheerio < Result > ; } Object . assign ( initialize , staticMethods , { load , _root : initialRoot , _options : internalOpts , fn : LoadedCheerio . prototype , prototype : LoadedCheerio . prototype , } ) ; return initialize as CheerioAPI ; } ; } function isNode ( obj ) { return ( ! ! obj . name || obj . type === 'root' || obj . type === 'text' || obj . type === 'comment' ) ; }