From d15493ae4940281f222baa3c39019805b591619f Mon Sep 17 00:00:00 2001 From: nicojs Date: Thu, 18 Feb 2016 22:40:17 +0100 Subject: [PATCH] Added typing information for parse5 (github.com/inikulin/parse5) --- parse5/parse5-tests.ts | 41 +++++++ parse5/parse5.d.ts | 263 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 304 insertions(+) create mode 100644 parse5/parse5-tests.ts create mode 100644 parse5/parse5.d.ts diff --git a/parse5/parse5-tests.ts b/parse5/parse5-tests.ts new file mode 100644 index 000000000..420fda599 --- /dev/null +++ b/parse5/parse5-tests.ts @@ -0,0 +1,41 @@ +// Type definitions for parse5 2.1.5 +// Project: https://github.com/inikulin/parse5 +// Definitions by: Nico Jansen +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import * as parse5 from 'parse5'; + +// parse5.SAXParser() +var parser = new parse5.SAXParser({ locationInfo: true }); +parser.on('startTag', (name, attrs, selfClosing, location) => { + console.log(name, attrs, selfClosing, location); +}); +parser.on('text', (text, location) => { + console.log(text, location); +}); + +// parse5.parse() +parse5.parse('html', { locationInfo: true, treeAdapter: parse5.treeAdapters.default }); +parse5.parse('html', {}); +parse5.parse('html'); + +// parse5.ParserStream() +var parserStream = new parse5.ParserStream({ locationInfo: true, treeAdapter: parse5.treeAdapters.htmlparser2 }); +parserStream = new parse5.ParserStream({ }); +parserStream = new parse5.ParserStream(); +var node = parserStream.document.childNodes[0]; +node.parentNode.attrs = [{name: '', value: ''}]; + +// parse5.parseFragment() +var fragment = parse5.parseFragment(''); +fragment = parse5.parseFragment('', {locationInfo: true}); + +// parse5.ASTNode +fragment.quirksMode = true; +fragment.namespaceURI = ''; +fragment.nodeName = ''; +fragment.value = ''; +fragment = fragment.parentNode; +fragment = fragment.childNodes[0]; diff --git a/parse5/parse5.d.ts b/parse5/parse5.d.ts new file mode 100644 index 000000000..287225150 --- /dev/null +++ b/parse5/parse5.d.ts @@ -0,0 +1,263 @@ +// Type definitions for parse5 2.1.5 +// Project: https://github.com/inikulin/parse5 +// Definitions by: Nico Jansen +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module 'parse5' { + import * as stream from "stream"; + import * as events from "events"; + + /** + * Parses an HTML string. + * @function + * @param {string} html - Input HTML string. + * @param {ParserOptions} html - Parsing options. + * + * @example + * var parse5 = require('parse5'); + * var document = parse5.parse('Hi there!'); + */ + export function parse(html: string, options?: ParserOptions): ASTNode; + + /** + * Parses an HTML fragment. + * + * @param {string} html - Input html fragment + * @param {ParserOptions} - Parsign options + * @example + * var parse5 = require('parse5'); + * var documentFragment = parse5.parseFragment('
'); + * // Parses the html fragment in the context of the parsed element. + * var trFragment = parser.parseFragment(documentFragment.childNodes[0], ''); + */ + export function parseFragment(html: string, options?: ParserOptions): ASTNode; + export function parseFragment(fragmentContext: any, html: string, options?: ParserOptions): ASTNode; + + /** + * Serializes an AST node to an HTML string. + * @param node Node to serialize. + * @param options Serialization options + */ + export function serialize(node: ASTNode, options?: SerializerOptions): string; + + export interface ASTAttribute { + name: string; + value: string; + } + + export interface Attribute { + key: string; + value: string; + } + + export interface ASTNode { + attrs: ASTAttribute[]; + childNodes?: ASTNode[]; + namespaceURI?: string; + parentNode?: ASTNode; + nodeName: string; + quirksMode?: boolean; + value?: string; + __location: LocationInfo | ElementLocationInfo; + } + + export interface LocationInfo { + /** + * One-based line index + */ + line: number; + /** + * Zero-based first character index + */ + col: number; + /** + * Zero-based first character index + */ + startOffset: number; + /** + * Zero-based last character index + */ + endOffset: number; + } + + + /** + * Streaming AST node to an HTML serializer. A readable stream. + */ + export class SerializerStream extends stream.Readable { + /** + * Streaming AST node to an HTML serializer. A readable stream. + * + * @param Node to serialize. + * @param options Serialization options. + */ + constructor(node: ASTNode, options: SerializerOptions); + } + + /** + * Streaming HTML parser with scripting support. A writable stream. + */ + export class ParserStream extends stream.Writable { + /** + * @param options Parsing options. + */ + constructor(options?: ParserOptions); + /** + * The resulting document node. + */ + document: ASTNode; + on(event: string, listener: Function): ParserStream; + /** + * Raised then parser encounters a
Shake it, baby