import type { BasicAcceptedElems } from './types.js'; import type { CheerioAPI } from './load.js'; import type { Cheerio } from './cheerio.js'; import type { AnyNode, Document } from 'domhandler'; import { textContent } from 'domutils'; import { type InternalOptions, type CheerioOptions, flattenOptions as flattenOptions, } from './options.js'; import type { ExtractedMap, ExtractMap } from './api/extract.js'; /** * Helper function to render a DOM. * * @param that - Cheerio instance to render. * @param dom - The DOM to render. Defaults to `that`'s root. * @param options - Options for rendering. * @returns The rendered document. */ function render( that: CheerioAPI, dom: BasicAcceptedElems | undefined, options: InternalOptions, ): string { if (!that) return ''; return that(dom ?? that._root.children, null, undefined, options).toString(); } /** * Checks if a passed object is an options object. * * @param dom - Object to check if it is an options object. * @param options - Options object. * @returns Whether the object is an options object. */ function isOptions( dom?: BasicAcceptedElems | CheerioOptions | null, options?: CheerioOptions, ): dom is CheerioOptions { return ( !options && typeof dom === 'object' && dom != null && !('length' in dom) && !('type' in dom) ); } /** * Renders the document. * * @category Static * @param options - Options for the renderer. * @returns The rendered document. */ export function html(this: CheerioAPI, options?: CheerioOptions): string; /** * Renders the document. * * @category Static * @param dom - Element to render. * @param options - Options for the renderer. * @returns The rendered document. */ export function html( this: CheerioAPI, dom?: BasicAcceptedElems, options?: CheerioOptions, ): string; export function html( this: CheerioAPI, dom?: BasicAcceptedElems | CheerioOptions, options?: CheerioOptions, ): string { /* * Be flexible about parameters, sometimes we call html(), * with options as only parameter * check dom argument for dom element specific properties * assume there is no 'length' or 'type' properties in the options object */ const toRender = isOptions(dom) ? ((options = dom), undefined) : dom; /* * Sometimes `$.html()` is used without preloading html, * so fallback non-existing options to the default ones. */ const opts = { ...this?._options, ...flattenOptions(options), }; return render(this, toRender, opts); } /** * Render the document as XML. * * @category Static * @param dom - Element to render. * @returns THe rendered document. */ export function xml( this: CheerioAPI, dom?: BasicAcceptedElems, ): string { const options = { ...this._options, xmlMode: true }; return render(this, dom, options); } /** * Render the document as text. * * This returns the `textContent` of the passed elements. The result will * include the contents of `