import { Node , Document , isDocument } from 'domhandler' ; import { parse as parseDocument , parseFragment , serialize } from 'parse5' ; import htmlparser2Adapter from 'parse5-htmlparser2-tree-adapter' ; import type { InternalOptions } from '../options' ; interface Parse5Options extends InternalOptions { context ? : Node ; } export function parseWithParse5 ( content , options , isDocument ? ) { const opts = { scriptingEnabled : typeof options . scriptingEnabled === 'boolean' ? options . scriptingEnabled : true , treeAdapter : htmlparser2Adapter , sourceCodeLocationInfo : options . sourceCodeLocationInfo , } ; const { context } = options ; return isDocument ? parseDocument ( content , opts ) : parseFragment ( context , content , opts ) ; } export function renderWithParse5 ( dom ) { const nodes = 'length' in dom ? dom : [ dom ] ; for ( let index = 0 ; index < nodes . length ; index += 1 ) { const node = nodes [ index ] ; if ( isDocument ( node ) ) { Array . prototype . splice . call ( nodes , index , 1 , ... node . children ) ; } } return serialize ( { children : nodes } , { treeAdapter : htmlparser2Adapter } ) ; }