html-tokenizer
Version:
Small, fast, event-driven, fault-tolerant html tokenizer. Works in node or browsers.
80 lines • 1.94 kB
TypeScript
import { Entities } from './types';
/**
* Options passed to a parser on instantiation.
*/
export interface ParserOptions {
entities?: Entities;
}
/**
* A token emitted during a parsing run.
*/
export declare type ParseToken = OpenParseToken | TextParseToken | CommentParseToken | CloseParseToken;
/**
* Opening tag.
*/
export interface OpenParseToken {
type: 'open';
/** Name of tag. */
name: string;
/** Set of attributes. */
attributes: Attributes;
/** Whether this tag is self-closing. */
selfClosing: boolean;
}
/**
* Text token.
*/
export interface TextParseToken {
type: 'text';
/** The text content. */
text: string;
}
/**
* Comment.
*/
export interface CommentParseToken {
type: 'comment';
/** The comment content. */
text: string;
}
/**
* Closing tag.
*/
export interface CloseParseToken {
type: 'close';
/** Name of the tag. */
name: string;
/** Whether tag was self closing. */
selfClosing: boolean;
}
/**
* A set of attributes.
*/
export interface Attributes {
[attrName: string]: string;
}
/**
* An object capable of parsing HTML.
*/
export declare class Parser {
private readonly tokenizer;
/**
* Static method to parse HTML without instantiating a Parser instance.
* @param html HTML string to parse.
* @param opts Optional parser configuration options.
*/
static parse(html: string, opts?: ParserOptions): IterableIterator<ParseToken>;
/**
* Static factory to create a parser.
* @param opts Parser options.
*/
static from(opts: ParserOptions): Parser;
private constructor();
/**
* Parse an HTML string. Returns an iterator, thus allowing parse
* tokens to be consumed via for/of or other iteration mechanisms.
* @param html HTML string to parse.
*/
parse(html: string): IterableIterator<ParseToken>;
}
//# sourceMappingURL=parser.d.ts.map