UNPKG

tag-soup

Version:

The fastest pure JS SAX/DOM XML/HTML parser.

123 lines (122 loc) 3.28 kB
import type { ParserOptions, ResolvedParserOptions } from './types.js'; /** * Handler which methods are called during parsing. * * @group SAX */ export interface SAXHandler { /** * Called when a text is read. * * @param text The decoded text. */ onText?(text: string): void; /** * Called when a start tag name is read. * * @param tagName The start tag name. */ onStartTagOpening?(tagName: string): void; /** * Called when a start tag is closed. */ onStartTagClosing?(): void; /** * Called when a start tag is self-closed. */ onStartTagSelfClosing?(): void; /** * Called when a start tag is closed. * * @param tagName The start tag name. * @param attributes Associated attributes. * @param isSelfClosing `true` if tag is self-closing. */ onStartTag?(tagName: string, attributes: Record<string, string>, isSelfClosing: boolean): void; /** * Called when an end tag is read. * * @param tagName The tag name that matches the currently opened start tag. */ onEndTag?(tagName: string): void; /** * Called when an attribute and its value are read. * * @param name The attribute name. * @param value The decoded attribute value. */ onAttribute?(name: string, value: string): void; /** * Called when a CDATA section is read. * * @param data The CDATA section value. */ onCDATASection?(data: string): void; /** * Called when a comment is read. * * @param data The decoded comment. */ onComment?(data: string): void; /** * Called when a DOCTYPE is read. * * @param name The DOCTYPE name. */ onDoctype?(name: string): void; /** * Called when a processing instruction is read. * * @param target The processing instruction target. * @param data The processing instruction content. */ onProcessingInstruction?(target: string, data: string): void; } /** * Parses text as a stream of tokens. * * @group SAX */ export interface SAXParser { /** * Parses text as a document. * * @param input The text to parse. * @param handler The token handler. */ parseDocument(input: string, handler: SAXHandler): void; /** * Parses text as a document fragment. * * @param input The text to parse. * @param handler The token handler. */ parseFragment(input: string, handler: SAXHandler): void; } /** * Parses text as a stream of tokens. * * @example * import { createSAXParser, htmlTokenizerOptions } from 'tag-soup'; * * const parser = createSAXParser(htmlTokenizerOptions); * * parser.parseFragment('Hello, <b>Bob</b>!', { * onStartTagOpening(tagName) { * // Handle <b> tag * }, * }); * * @param options Parser options. * @group SAX */ export declare function createSAXParser(options?: ParserOptions): SAXParser; /** * Parses text as a stream of tokens. * * @param input The text to parse. * @param handler The token handler. * @param options Parser options. * @returns The document node. */ export declare function parseSAX(input: string, handler: SAXHandler, options?: ResolvedParserOptions): void;