From f68677844fcb5470dc789a880d79362a3943d469 Mon Sep 17 00:00:00 2001 From: "andy.patterson" Date: Mon, 11 Jun 2018 08:12:49 -0400 Subject: [PATCH 1/5] [parse5] copy typings for parse5@4.0.0 from original repo --- types/parse5/v4/index.d.ts | 1081 +++++++++++++++++++++++++++++++ types/parse5/v4/parse5-tests.ts | 86 +++ types/parse5/v4/tsconfig.json | 25 + types/parse5/v4/tslint.json | 1 + 4 files changed, 1193 insertions(+) create mode 100644 types/parse5/v4/index.d.ts create mode 100644 types/parse5/v4/parse5-tests.ts create mode 100644 types/parse5/v4/tsconfig.json create mode 100644 types/parse5/v4/tslint.json diff --git a/types/parse5/v4/index.d.ts b/types/parse5/v4/index.d.ts new file mode 100644 index 0000000000..1f8b5a8df7 --- /dev/null +++ b/types/parse5/v4/index.d.ts @@ -0,0 +1,1081 @@ +// Type definitions for parse5 4.0 +// Project: https://github.com/inikulin/parse5 +// Definitions by: Ivan Nikulin +// Andy Patterson +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/// +import * as stream from "stream"; + +// Markup data +//----------------------------------------------------------------------------------- +declare namespace MarkupData { + interface Location { + /** + * One-based line index + */ + line: number; + /** + * One-based column index + */ + col: number; + /** + * Zero-based first character index + */ + startOffset: number; + /** + * Zero-based last character index + */ + endOffset: number; + } + + interface AttributesLocation { + [attributeName: string]: Location; + } + + interface StartTagLocation extends Location { + /** + * Start tag attributes' location info + */ + attrs: AttributesLocation + } + + interface ElementLocation extends StartTagLocation { + /** + * Element's start tag location info. + */ + startTag: StartTagLocation; + /** + * Element's end tag location info. + */ + endTag: Location; + } +} + +// Options +//----------------------------------------------------------------------------------- +declare namespace Options { + export interface ParserOptions { + /** + * Enables source code location information. When enabled, each node (except the root node) will have a `__location` property. + * If the node is not an empty element, `__location` will be a {@link MarkupData.ElementLocation} object, otherwise it will be {@link MarkupData.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 `__location` property will be `undefined`. + * + * **Default:** `false` + */ + locationInfo?: boolean; + /** + * Specifies the resulting tree format. + * + * **Default:** `treeAdapters.default` + */ + treeAdapter?: AST.TreeAdapter; + } + + export interface SAXParserOptions { + /** + * Enables source code location information for the tokens. + * When enabled, each token event handler will receive {@link MarkupData.Location} (or {@link MarkupData.StartTagLocation}) + * object as its last argument. + */ + locationInfo?: boolean; + } + + export interface SerializerOptions { + /*** + * Specifies input tree format. + * + * **Default:** `treeAdapters.default` + */ + treeAdapter?: AST.TreeAdapter; + } +} + + +// AST +//----------------------------------------------------------------------------------- +declare namespace AST { + /** + * [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks). + */ + type DocumentMode = 'no-quirks' | 'quirks' | 'limited-quirks'; + + // Default tree adapter + namespace Default { + /** + * Element attribute. + */ + 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]{@link parse5.treeAdapters} Node interface. + */ + interface Node { + /** + * The name of the node. E.g. {@link Document} will have `nodeName` equal to '#document'`. + */ + nodeName: string; + } + + /** + * [Default tree adapter]{@link parse5.treeAdapters} ParentNode interface. + */ + interface ParentNode { + /** + * Child nodes. + */ + childNodes: Node[]; + } + + /** + * [Default tree adapter]{@link parse5.treeAdapters} DocumentType interface. + */ + export interface DocumentType extends Node { + /** + * 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]{@link parse5.treeAdapters} Document interface. + */ + export interface Document extends ParentNode { + /** + * The name of the node. + */ + nodeName: '#document'; + /** + * [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks). + */ + mode: DocumentMode; + } + + /** + * [Default tree adapter]{@link parse5.treeAdapters} DocumentFragment interface. + */ + export interface DocumentFragment extends ParentNode { + /** + * The name of the node. + */ + nodeName: '#document-fragment'; + } + + /** + * [Default tree adapter]{@link parse5.treeAdapters} Element interface. + */ + export interface Element extends ParentNode { + /** + * 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[]; + /** + * Parent node. + */ + parentNode: ParentNode; + /** + * Element source code location info. Available if location info is enabled via {@link Options.ParserOptions}. + */ + __location?: MarkupData.ElementLocation; + } + + /** + * [Default tree adapter]{@link parse5.treeAdapters} CommentNode interface. + */ + export interface CommentNode extends Node { + /** + * The name of the node. + */ + nodeName: '#comment'; + /** + * Comment text. + */ + data: string; + /** + * Parent node. + */ + parentNode: ParentNode; + /** + * Comment source code location info. Available if location info is enabled via {@link Options.ParserOptions}. + */ + __location?: MarkupData.Location; + } + + /** + * [Default tree adapter]{@link parse5.treeAdapters} TextNode interface. + */ + export interface TextNode extends Node { + /** + * The name of the node. + */ + nodeName: '#text'; + /** + * Text content. + */ + value: string; + /** + * Parent node. + */ + parentNode: ParentNode; + /** + * Text node source code location info. Available if location info is enabled via {@link Options.ParserOptions}. + */ + __location?: MarkupData.Location; + } + } + + + // htmlparser2 tree adapter + namespace HtmlParser2 { + /** + * [htmlparser2 tree adapter]{@link parse5.treeAdapters} Node interface. + */ + interface Node { + /** + * The type of the node. E.g. {@link Document} will have `type` equal to 'root'`. + */ + type: string; + /** + * [DOM spec](https://dom.spec.whatwg.org/#dom-node-nodetype)-compatible node {@link type}. + */ + nodeType: number; + /** + * Parent node. + */ + parent: ParentNode; + /** + * Same as {@link parent}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias. + */ + parentNode: ParentNode; + /** + * Previous sibling. + */ + prev: Node; + /** + * Same as {@link prev}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias. + */ + previousSibling: Node; + /** + * Next sibling. + */ + next: Node; + /** + * Same as {@link next}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias. + */ + nextSibling: Node; + } + + /** + * [htmlparser2 tree adapter]{@link parse5.treeAdapters} ParentNode interface. + */ + interface ParentNode extends Node { + /** + * Child nodes. + */ + children: Node[]; + /** + * Same as {@link children}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias. + */ + childNodes: Node[]; + /** + * First child of the node. + */ + firstChild: Node; + /** + * Last child of the node. + */ + lastChild: Node; + } + + /** + * [htmlparser2 tree adapter]{@link parse5.treeAdapters} DocumentType interface. + */ + export interface DocumentType extends Node { + /** + * The type of the node. + */ + type: 'directive'; + /** + * Node name. + */ + name: '!doctype'; + /** + * Serialized doctype {@link name}, {@link publicId} and {@link systemId}. + */ + data: string; + /** + * Document type name. + */ + 'x-name':string; + /** + * Document type public identifier. + */ + 'x-publicId': string; + /** + * Document type system identifier. + */ + 'x-systemId': string; + } + + /** + * [htmlparser2 tree adapter]{@link parse5.treeAdapters} Document interface. + */ + export interface Document extends ParentNode { + /** + * The type of the node. + */ + type: 'root'; + /** + * The name of the node. + */ + name: 'root'; + /** + * [Document mode](https://dom.spec.whatwg.org/#concept-document-limited-quirks). + */ + 'x-mode': DocumentMode; + } + + /** + * [htmlparser2 tree adapter]{@link parse5.treeAdapters} DocumentFragment interface. + */ + export interface DocumentFragment extends ParentNode { + /** + * The type of the node. + */ + type: 'root'; + /** + * The name of the node. + */ + name: 'root'; + } + + /** + * [htmlparser2 tree adapter]{@link parse5.treeAdapters} Element interface. + */ + export interface Element extends ParentNode { + /** + * The name of the node. Equals to element {@link tagName}. + */ + name: string; + /** + * Element tag name. + */ + tagName: string; + /** + * Element namespace. + */ + namespace: string; + /** + * Element attributes. + */ + attribs: { [name: string]: string }; + /** + * Element attribute namespaces. + */ + 'x-attribsNamespace': { [name: string]: string }; + /** + * Element attribute namespace-related prefixes. + */ + 'x-attribsPrefix': { [name: string]: string }; + /** + * Element source code location info. Available if location info is enabled via {@link Options.ParserOptions}. + */ + __location?: MarkupData.ElementLocation; + } + + /** + * [htmlparser2 tree adapter]{@link parse5.treeAdapters} CommentNode interface. + */ + export interface CommentNode extends Node { + /** + * The name of the node. + */ + name: 'comment'; + /** + * Comment text. + */ + data: string; + /** + * Same as {@link data}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias. + */ + nodeValue: string; + /** + * Comment source code location info. Available if location info is enabled via {@link Options.ParserOptions}. + */ + __location?: MarkupData.Location; + } + + /** + * [htmlparser2 tree adapter]{@link parse5.treeAdapters} TextNode interface. + */ + export interface TextNode extends Node { + /** + * The name of the node. + */ + name: 'text'; + /** + * Text content. + */ + data: string; + /** + * Same as {@link data}. [DOM spec](https://dom.spec.whatwg.org)-compatible alias. + */ + nodeValue: string; + /** + * Comment source code location info. Available if location info is enabled via {@link Options.ParserOptions}. + */ + __location?: MarkupData.Location; + } + } + + + // Unions + // NOTE: we use `Object` in unions to support custom tree adapter implementations. + // TypeScript Handbook suggests to always use `any` instead of `Object`, but in that + // case language service hints `any` as type, instead of actual union name. + /** + * Generic Node interface. + * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.Node}) to get access to the properties. + */ + type Node = Default.Node | HtmlParser2.Node | Object; + /** + * Generic ParentNode interface. + * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.ParentNode}) to get access to the properties. + */ + type ParentNode = Default.ParentNode | HtmlParser2.ParentNode | Object; + /** + * Generic DocumentType interface. + * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.DocumentType}) to get access to the properties. + */ + type DocumentType = Default.DocumentType | HtmlParser2.DocumentType | Object; + /** + * Generic Document interface. + * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.Document}) to get access to the properties. + */ + type Document = Default.Document | HtmlParser2.Document | Object; + /** + * Generic DocumentFragment interface. + * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.DocumentFragment}) to get access to the properties. + */ + type DocumentFragment = Default.DocumentFragment | HtmlParser2.DocumentFragment | Object; + /** + * Generic Element interface. + * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.Element}) to get access to the properties. + */ + type Element = Default.Element | HtmlParser2.Element | Object; + /** + * Generic TextNode interface. + * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.TextNode}) to get access to the properties. + */ + type TextNode = Default.TextNode | HtmlParser2.TextNode | Object; + /** + * Generic CommentNode interface. + * Cast to the actual AST interface (e.g. {@link parse5.AST.Default.CommentNode}) to get access to the properties. + */ + type CommentNode = Default.CommentNode | HtmlParser2.CommentNode | Object; + + + // Tree adapter interface + //----------------------------------------------------------------------------------- + + /** + * 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(): AST.Document; + /** + * Creates a document fragment node. + */ + createDocumentFragment(): AST.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: AST.Default.Attribute[]): AST.Element; + /** + * Creates a comment node. + * + * @param data - Comment text. + */ + createCommentNode(data: string): AST.CommentNode; + /** + * Appends a child node to the given parent node. + * + * @param parentNode - Parent node. + * @param newNode - Child node. + */ + appendChild(parentNode: AST.ParentNode, newNode: AST.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: AST.ParentNode, newNode: AST.Node, referenceNode: AST.Node): void; + /** + * Sets the `