// Type definitions for parse5 5.0 // Project: https://github.com/inikulin/parse5 // Definitions by: Ivan Nikulin // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 export interface Location { /** * One-based line index of the first character */ startLine: number; /** * One-based column index of the first character */ startCol: number; /** * One-based line index of the last character */ endLine: number; /** * One-based column index of the last character */ endCol: number; /** * Zero-based first character index */ startOffset: number; /** * Zero-based last character index */ endOffset: number; } export interface AttributesLocation { [attributeName: string]: Location; } export interface StartTagLocation extends Location { /** * Start tag attributes' location info */ attrs: AttributesLocation; } export interface ElementLocation extends StartTagLocation { /** * Element's start tag location info. */ startTag: StartTagLocation; /** * Element's end tag location info. */ endTag: Location; } export interface ParserOptions { /** * The [scripting flag](https://html.spec.whatwg.org/multipage/parsing.html#scripting-flag). If set * to `true`, `noscript` element content will be parsed as text. * * **Default:** `true` */ scriptingEnabled?: boolean; /** * Enables source code location information. When enabled, each node (except the root node) * will have a `sourceCodeLocation` property. If the node is not an empty element, `sourceCodeLocation` will * be a {@link ElementLocation} object, otherwise it will be {@link Location}. * If the element was implicitly created by the parser (as part of * [tree correction](https://html.spec.whatwg.org/multipage/syntax.html#an-introduction-to-error-handling-and-strange-cases-in-the-parser)), * its `sourceCodeLocation` property will be `undefined`. * * **Default:** `false` */ sourceCodeLocationInfo?: boolean; /** * Specifies the resulting tree format. * * **Default:** `treeAdapters.default` */ treeAdapter?: TreeAdapter; } export interface SerializerOptions { /*** * Specifies input tree format. * * **Default:** `treeAdapters.default` */ treeAdapter?: TreeAdapter; } /** * [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks). */ export type DocumentMode = "no-quirks" | "quirks" | "limited-quirks"; // Default tree adapter /** * Element attribute. */ export interface Attribute { /** * The name of the attribute. */ name: string; /** * The value of the attribute. */ value: string; /** * The namespace of the attribute. */ namespace?: string; /** * The namespace-related prefix of the attribute. */ prefix?: string; } /** * Default tree adapter Node interface. */ export interface DefaultTreeNode { /** * The name of the node. E.g. {@link Document} will have `nodeName` equal to '#document'`. */ nodeName: string; } /** * Default tree adapter ParentNode interface. */ export interface DefaultTreeParentNode extends DefaultTreeNode { /** * Child nodes. */ childNodes: DefaultTreeNode[]; } /** * Default tree adapter ChildNode interface. */ export interface DefaultTreeChildNode extends DefaultTreeNode { /** * Parent node. */ parentNode: DefaultTreeParentNode; } /** * Default tree adapter DocumentType interface. */ export interface DefaultTreeDocumentType extends DefaultTreeNode { /** * The name of the node. */ nodeName: "#documentType"; /** * Document type name. */ name: string; /** * Document type public identifier. */ publicId: string; /** * Document type system identifier. */ systemId: string; } /** * Default tree adapter Document interface. */ export interface DefaultTreeDocument extends DefaultTreeParentNode { /** * The name of the node. */ nodeName: "#document"; /** * [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks). */ mode: DocumentMode; } /** * Default tree adapter DocumentFragment interface. */ export interface DefaultTreeDocumentFragment extends DefaultTreeParentNode { /** * The name of the node. */ nodeName: "#document-fragment"; } /** * Default tree adapter Element interface. */ export interface DefaultTreeElement extends DefaultTreeChildNode, DefaultTreeParentNode { /** * The name of the node. Equals to element {@link tagName}. */ nodeName: string; /** * Element tag name. */ tagName: string; /** * Element namespace. */ namespaceURI: string; /** * List of element attributes. */ attrs: Attribute[]; /** * Element source code location info. Available if location info is enabled via {@link ParserOptions}. */ sourceCodeLocation?: ElementLocation; } /** * Default tree adapter CommentNode interface. */ export interface DefaultTreeCommentNode extends DefaultTreeChildNode { /** * The name of the node. */ nodeName: "#comment"; /** * Comment text. */ data: string; /** * Comment source code location info. Available if location info is enabled via {@link ParserOptions}. */ sourceCodeLocation?: Location; } /** * Default tree adapter TextNode interface. */ export interface DefaultTreeTextNode extends DefaultTreeChildNode { /** * The name of the node. */ nodeName: "#text"; /** * Text content. */ value: string; /** * Text node source code location info. Available if location info is enabled via {@link ParserOptions}. */ sourceCodeLocation?: Location; } // Generic node intefaces /** * Generic Node interface. * Cast to the actual AST interface (e.g. {@link parse5.DefaultTreeNode}) to get access to the properties. */ export type Node = DefaultTreeNode | object; /** * Generic ChildNode interface. * Cast to the actual AST interface (e.g. {@link parse5.DefaultTreeChildNode}) to get access to the properties. */ export type ChildNode = DefaultTreeChildNode | object; /** * Generic ParentNode interface. * Cast to the actual AST interface (e.g. {@link parse5.DefaultTreeParentNode}) to get access to the properties. */ export type ParentNode = DefaultTreeParentNode | object; /** * Generic DocumentType interface. * Cast to the actual AST interface (e.g. {@link parse5.DefaultTreeDocumentType}) to get access to the properties. */ export type DocumentType = DefaultTreeDocumentType | object; /** * Generic Document interface. * Cast to the actual AST interface (e.g. {@link parse5.DefaultTreeDocument}) to get access to the properties. */ export type Document = DefaultTreeDocument | object; /** * Generic DocumentFragment interface. * Cast to the actual AST interface (e.g. {@link parse5.DefaultTreeDocumentFragment}) to get access to the properties. */ export type DocumentFragment = DefaultTreeDocumentFragment | object; /** * Generic Element interface. * Cast to the actual AST interface (e.g. {@link parse5.DefaultTreeElement}) to get access to the properties. */ export type Element = DefaultTreeElement | object; /** * Generic TextNode interface. * Cast to the actual AST interface (e.g. {@link parse5.DefaultTreeTextNode}) to get access to the properties. */ export type TextNode = DefaultTreeTextNode | object; /** * Generic CommentNode interface. * Cast to the actual AST interface (e.g. {@link parse5.Default.CommentNode}) to get access to the properties. */ export type CommentNode = DefaultTreeCommentNode | object; /** * Tree adapter is a set of utility functions that provides minimal required abstraction layer beetween parser and a specific AST format. * Note that `TreeAdapter` is not designed to be a general purpose AST manipulation library. You can build such library * on top of existing `TreeAdapter` or use one of the existing libraries from npm. * * @see [default implementation](https://github.com/inikulin/parse5/blob/master/lib/tree_adapters/default.js) */ export interface TreeAdapter { /** * Creates a document node. */ createDocument(): Document; /** * Creates a document fragment node. */ createDocumentFragment(): DocumentFragment; /** * Creates an element node. * * @param tagName - Tag name of the element. * @param namespaceURI - Namespace of the element. * @param attrs - Attribute name-value pair array. Foreign attributes may contain `namespace` and `prefix` fields as well. */ createElement( tagName: string, namespaceURI: string, attrs: Attribute[] ): Element; /** * Creates a comment node. * * @param data - Comment text. */ createCommentNode(data: string): CommentNode; /** * Appends a child node to the given parent node. * * @param parentNode - Parent node. * @param newNode - Child node. */ appendChild(parentNode: ParentNode, newNode: Node): void; /** * Inserts a child node to the given parent node before the given reference node. * * @param parentNode - Parent node. * @param newNode - Child node. * @param referenceNode - Reference node. */ insertBefore( parentNode: ParentNode, newNode: Node, referenceNode: Node ): void; /** * Sets the `