export as namespace htmlparser;
export = htmlparser;
declare namespace htmlparser {
interface BaseDomNode {
parent?: DomNode | null;
next?: DomNode | null;
prev?: DomNode | null;
startIndex?: number;
endIndex?: number;
}
type DomNode =
| DomTextNode
| DomDirectiveNode
| DomCommentNode
| DomTagNode
| DomCdataNode;
type Dom = DomNode[];
interface DomTextNode extends BaseDomNode {
type: "text";
data: string;
name?: undefined;
attribs?: undefined;
children?: undefined;
}
interface DomDirectiveNode extends BaseDomNode {
type: "directive";
data: string;
// Actually, `name` is `string`, but this breaks type guards like `if (el.name === 'p') ...`.
name?: undefined;
attribs?: undefined;
children?: undefined;
}
interface DomCommentNode extends BaseDomNode {
type: "comment";
data: string;
name?: undefined;
attribs?: undefined;
children?: undefined;
}
interface DomTagNode extends BaseDomNode {
type: "tag" | "script" | "style";
data?: undefined;
name: string;
attribs: { [name: string]: string };
children: DomNode[];
}
interface DomCdataNode extends BaseDomNode {
type: "cdata";
data?: undefined;
name?: undefined;
attribs?: undefined;
// Specifying `DomTextNode[]` is not practical because of https://github.com/microsoft/TypeScript/issues/35045
children: DomNode[];
}
class Parser {
constructor(handler: Handler, options?: ParserOptions);
/***
* Parses a chunk of data and calls the corresponding callbacks.
*/
write(input: string): void;
/***
* Parses the end of the buffer and clears the stack, calls onend.
*/
end(): void;
/***
* Resets the parser, parses the data & calls end.
*/
parseComplete(input: string): void;
/***
* Resets buffer & stack, calls onreset.
*/
reset(): void;
}
function parse(
markup: string,
options?: ParserOptions & HandlerOptions,
): DomNode[];
function serialize(
dom: DomNode | DomNode[],
options?: SerializerOptions,
): string;
function create(
tagName: string,
...childrenOrAttribs: Array<
| DomNode
| string
| { [name: string]: string }
| undefined
| Array
>
): DomTagNode;
/** Tests whether a node is a tag (`tag`, `script` or `style`). */
function isTag(node: DomNode): node is DomTagNode;
function getSiblings(node: DomNode): DomNode[];
function hasAttrib(tag: DomTagNode, name: string): boolean;
/**
*
* @param node Node to remove
* @param dom Array of top-level nodes, e.g. returned from `parse`.
* Ignored if `number`, so `forEach` can be used: `nodes.forEach(remove)`
*/
function remove(nodes: DomNode | DomNode[], dom?: Dom | number): void;
/**
* Note that if `replacement` is part of the DOM, it should be removed first
* (e.g. using `remove`) for proper cleanup.
*/
function replace(node: DomNode, replacement: DomNode, dom?: Dom): void;
function appendChild(tag: DomTagNode, child: DomNode, dom?: Dom): void;
function prependChild(tag: DomTagNode, child: DomNode, dom?: Dom): void;
/** Insert `next` after `node`. */
function append(node: DomNode, next: DomNode): void;
/** Insert `prev` before `node`. */
function prepend(node: DomNode, prev: DomNode): void;
/**
* Recursive, depth-first.
* @param [recursive=true]
* @param [limit=Infinity]
*/
function filter(
test: (node: DomNode) => boolean,
nodes: DomNode | DomNode[],
recursive?: boolean,
limit?: number,
): DomNode[];
/**
* Searches only for tags, ignores text nodes, etc.
* Non-recursive, depth-first.
*/
function findOne(
test: ((el: DomTagNode) => boolean) | string,
nodes: DomNode[],
): DomTagNode | null;
/**
* Searches only for tags, ignores text nodes, etc.
* Non-recursive, depth-first.
* @param test Tag name or predicate function
*/
function findAll(
test: ((el: DomTagNode) => boolean) | string,
nodes: DomNode[],
): DomTagNode[];
interface Handler {
onopentag?: (name: string, attribs: { [type: string]: string }) => void;
onopentagname?: (name: string) => void;
onattribute?: (name: string, value: string) => void;
ontext?: (text: string) => void;
onclosetag?: (text: string) => void;
onprocessinginstruction?: (name: string, data: string) => void;
oncomment?: (data: string) => void;
oncommentend?: () => void;
oncdatastart?: () => void;
oncdataend?: () => void;
onerror?: (error: Error) => void;
onreset?: () => void;
onend?: () => void;
}
interface ParserOptions {
/***
* Indicates whether special tags (