diff --git a/types/parse5-html-rewriting-stream/index.d.ts b/types/parse5-html-rewriting-stream/index.d.ts new file mode 100644 index 0000000000..42e0ef9734 --- /dev/null +++ b/types/parse5-html-rewriting-stream/index.d.ts @@ -0,0 +1,80 @@ +// Type definitions for parse5-html-rewriting-stream 5.1 +// Project: https://github.com/inikulin/parse5/tree/master/packages/parse5-html-rewriting-stream +// Definitions by: Sam Li +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +import * as parse5 from 'parse5-sax-parser'; +import * as stream from 'stream'; + +/** + * Streaming SAX-style HTML rewriter. A transform stream (which means you can + * pipe through it, see example). Rewriter uses raw source representation of + * tokens if they are not modified by the user, therefore resulting HTML is + * not affected by parser error-recovery mechanisms as in the classical + * parsing-serialization roundtrip. + */ +export default class RewritingStream extends stream.Transform { + on(event: string, listener: (...args: any[]) => void): this; + + /** + * Raised when the rewriter encounters a start tag. + */ + on(type: 'startTag', + callback: (startTag: parse5.StartTagToken, rawHtml: string) => void): + this; + + /** + * Raised when rewriter encounters an end tag. + */ + on(type: 'endTag', + callback: (endTag: parse5.EndTagToken, rawHtml: string) => void): this; + + /** + * Raised when rewriter encounters a comment. + */ + on(type: 'comment', + callback: (comment: parse5.CommentToken, rawHtml: string) => void): this; + + /** + * Raised when rewriter encounters text content. + */ + on(type: 'text', + callback: (text: parse5.TextToken, rawHtml: string) => void): this; + + /** + * Raised when rewriter encounters a document type declaration. + */ + on(type: 'doctype', + callback: (doctype: parse5.DoctypeToken, rawHtml: string) => void): this; + + /** + * Emits serialized start tag token into the output stream. + */ + emitStartTag(startTag: parse5.StartTagToken): void; + + /** + * Emits serialized end tag token into the output stream. + */ + emitEndTag(endTag: parse5.EndTagToken): void; + + /** + * Emits serialized text token into the output stream. + */ + emitText(text: parse5.TextToken): void; + + /** + * Emits serialized comment token into the output stream. + */ + emitComment(text: parse5.CommentToken): void; + + /** + * Emits serialized document type token into the output stream. + */ + emitDoctype(text: parse5.DoctypeToken): void; + + /** + * Emits raw HTML string into the output stream. + */ + emitRaw(html: string): void; +} diff --git a/types/parse5-html-rewriting-stream/parse5-html-rewriting-stream-tests.ts b/types/parse5-html-rewriting-stream/parse5-html-rewriting-stream-tests.ts new file mode 100644 index 0000000000..f134e4807b --- /dev/null +++ b/types/parse5-html-rewriting-stream/parse5-html-rewriting-stream-tests.ts @@ -0,0 +1,10 @@ +import RewritingStream from 'parse5-html-rewriting-stream'; + +const rewritingStream = new RewritingStream(); +rewritingStream.on('startTag', (startTag, rawHtml) => { + startTag.tagName; // $ExpectType string + startTag.selfClosing; // $ExpectType boolean + rawHtml; // $ExpectType string + + rewritingStream.emitStartTag(startTag); // $ExpectType void +}); diff --git a/types/parse5-html-rewriting-stream/tsconfig.json b/types/parse5-html-rewriting-stream/tsconfig.json new file mode 100644 index 0000000000..604400ba20 --- /dev/null +++ b/types/parse5-html-rewriting-stream/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "parse5-html-rewriting-stream-tests.ts" + ] +} diff --git a/types/parse5-html-rewriting-stream/tslint.json b/types/parse5-html-rewriting-stream/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/parse5-html-rewriting-stream/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }