import { BasicAcceptedElems } from './types' ; import type { CheerioAPI , Cheerio } from '.' ; import { Node , Document , isText , hasChildren } from 'domhandler' ; import { InternalOptions , CheerioOptions , default as defaultOptions , flatten as flattenOptions , } from './options' ; import { ElementType } from 'htmlparser2' ; param that param dom param options returns function render ( that , dom , options ) { if ( ! that ) return '' ; return that ( dom ?? that . _root . children , null , undefined , options ) . toString ( ) ; } param dom returns function isOptions ( dom ? , options ? ) { return ( ! options && typeof dom === 'object' && dom != null && ! ( 'length' in dom ) && ! ( 'type' in dom ) ) ; } param options returns export function html ( this , options ? ) ; param dom param options returns export function html ( this , dom ? , options ? ) ; export function html ( this , dom ? , options ? ) { const toRender = isOptions ( dom ) ? ( ( options = dom ) , undefined ) : dom ; const opts = { ... defaultOptions , ... this ?. _options , ... flattenOptions ( options ?? { } ) , } ; return render ( this , toRender , opts ) ; } param dom returns export function xml ( this , dom ? ) { const options = { ... this . _options , xmlMode : true } ; return render ( this , dom , options ) ; } param elements returns export function text ( this , elements ? ) { const elems = elements ? elements : this ? this . root ( ) : [ ] ; let ret = '' ; for ( let i = 0 ; i < elems . length ; i ++ ) { const elem = elems [ i ] ; if ( isText ( elem ) ) ret += elem . data ; else if ( hasChildren ( elem ) && elem . type !== ElementType . Comment && elem . type !== ElementType . Script && elem . type !== ElementType . Style ) { ret += text ( elem . children ) ; } } return ret ; } param data param context param keepScripts returns alias see https export function parseHTML ( this , data , context ? , keepScripts ? ) ; export function parseHTML ( this , data ? ) ; export function parseHTML ( this , data ? , context ? , keepScripts = typeof context === 'boolean' ? context : false ) { if ( ! data || typeof data !== 'string' ) { return null ; } if ( typeof context === 'boolean' ) { keepScripts = context ; } const parsed = this . load ( data , defaultOptions , false ) ; if ( ! keepScripts ) { parsed ( 'script' ) . remove ( ) ; } return parsed . root ( ) [ 0 ] . children . slice ( ) ; } example returns alias export function root ( this ) { return this ( this . _root ) ; } param container param contained returns alias see https export function contains ( container , contained ) { if ( contained === container ) { return false ; } let next = contained ; while ( next && next !== next . parent ) { next = next . parent ; if ( next === container ) { return true ; } } return false ; } interface WritableArrayLike < T > extends ArrayLike < T > { length : number ; [ n ] : T ; } param arr1 param arr2 returns alias see https export function merge < T > ( arr1 , arr2 ) { if ( ! isArrayLike ( arr1 ) || ! isArrayLike ( arr2 ) ) { return ; } let newLength = arr1 . length ; const len = + arr2 . length ; for ( let i = 0 ; i < len ; i ++ ) { arr1 [ newLength ++ ] = arr2 [ i ] ; } arr1 . length = newLength ; return arr1 ; } param item returns function isArrayLike ( item ) { if ( Array . isArray ( item ) ) { return true ; } if ( typeof item !== 'object' || ! Object . prototype . hasOwnProperty . call ( item , 'length' ) || typeof item . length !== 'number' || item . length < 0 ) { return false ; } for ( let i = 0 ; i < item . length ; i ++ ) { if ( ! ( i in item ) ) { return false ; } } return true ; }