import { removeElement } from 'domutils' ; import { Node , Document , NodeWithChildren , isDocument as checkIsDocument , } from 'domhandler' ; import type { InternalOptions } from './options' ; export function getParse ( parser ) { return function parse ( content , options , isDocument ) : Document { if ( typeof Buffer !== 'undefined' && Buffer . isBuffer ( content ) ) { content = content . toString ( ) ; } if ( typeof content === 'string' ) { return parser ( content , options , isDocument ) ; } const doc = content as Node | Node [ ] | Document ; if ( ! Array . isArray ( doc ) && checkIsDocument ( doc ) ) { return doc ; } const root = new Document ( [ ] ) ; update ( doc , root ) ; return root ; } ; } param newChilds param parent returns export function update ( newChilds , parent ) { const arr = Array . isArray ( newChilds ) ? newChilds : [ newChilds ] ; if ( parent ) { parent . children = arr ; } else { parent = null ; } for ( let i = 0 ; i < arr . length ; i ++ ) { const node = arr [ i ] ; if ( node . parent && node . parent . children !== arr ) { removeElement ( node ) ; } if ( parent ) { node . prev = arr [ i - 1 ] || null ; node . next = arr [ i + 1 ] || null ; } else { node . prev = node . next = null ; } node . parent = parent ; } return parent ; }