From 58e4f00832fa8ffd2fb060a710eecb52ba858e5b Mon Sep 17 00:00:00 2001 From: Johan Davidsson Date: Mon, 25 Mar 2019 17:39:20 +0100 Subject: [PATCH] [@types/htmlparser2] Update typings for Htmlparser2 (and related modules) to match updates (#33921) * Added new types for domelementtype * Modify existing types for htmlparser2 * Updating PULL_REQUEST_TEMPLATE.md * Revert changes to .github/PULL_REQUEST_TEMPLATE.md * Fixed missing optional * Adding DomUtils * Update dependencies * Removed requirement for TypeScript 2.1 and updated test * Updated test to include DomUtils and DomHandlerOptions. * Updated domhandler tests * Remove incorrect constructor in Parser * Remove incorrect constructor in WritableStream * Change way of importing in tests * Adding deprecated fallback to Options * Add deprecated Options as a fallback to sanitize-html * Add ParserOptions * Use the deprecated Options --- types/domhandler/domhandler-tests.ts | 8 +-- types/domhandler/index.d.ts | 1 - types/domutils/index.d.ts | 1 - types/htmlparser2/htmlparser2-tests.ts | 37 ++++++++------ types/htmlparser2/index.d.ts | 68 ++++++++++++++++++-------- types/sanitize-html/index.d.ts | 1 + 6 files changed, 74 insertions(+), 42 deletions(-) diff --git a/types/domhandler/domhandler-tests.ts b/types/domhandler/domhandler-tests.ts index eea6441ad7..4b1c5406ec 100644 --- a/types/domhandler/domhandler-tests.ts +++ b/types/domhandler/domhandler-tests.ts @@ -1,6 +1,6 @@ -import { DomHandler, DomHandlerOptions, Node } from "domhandler"; +import { DomHandler, DomHandlerOptions, Node, DomElement } from "domhandler"; -const handler = new DomHandler((error: Error, dom: any) => { +const handler = new DomHandler((error: Error, dom: DomElement[]) => { if (error) console.error('There has been an error...'); else @@ -8,7 +8,7 @@ const handler = new DomHandler((error: Error, dom: any) => { }); handler.ontext = (data: string) => { console.log(data); }; handler.onreset = () => { console.log('We have a reset.'); }; -handler.onerror = (error: Error) => { console.error(Error); }; -handler.onopentag = (name: string, attribs) => { console.log(name, attribs); }; +handler.onerror = (error: Error) => { console.error(error); }; +handler.onopentag = (name: string, attribs: { [s: string]: string }) => { console.log(name, attribs); }; const dho: DomHandlerOptions = { normalizeWhitespace: true, withDomLvl1: true, withEndIndices: true, withStartIndices: true }; diff --git a/types/domhandler/index.d.ts b/types/domhandler/index.d.ts index 9d33055bbd..49dcd6325f 100644 --- a/types/domhandler/index.d.ts +++ b/types/domhandler/index.d.ts @@ -2,7 +2,6 @@ // Project: https://github.com/fb55/DomHandler#readme // Definitions by: Johan Davidsson // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.1 export interface DomHandlerOptions { /*** diff --git a/types/domutils/index.d.ts b/types/domutils/index.d.ts index 5300a68a8c..6790f9bf82 100644 --- a/types/domutils/index.d.ts +++ b/types/domutils/index.d.ts @@ -2,7 +2,6 @@ // Project: https://github.com/FB55/domutils#readme // Definitions by: Johan Davidsson // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.1 import { DomElement } from "domhandler"; /*** diff --git a/types/htmlparser2/htmlparser2-tests.ts b/types/htmlparser2/htmlparser2-tests.ts index 21289b96f5..807f2fe904 100644 --- a/types/htmlparser2/htmlparser2-tests.ts +++ b/types/htmlparser2/htmlparser2-tests.ts @@ -1,23 +1,30 @@ /** * Created by staticfunction on 8/4/14. */ -import htmlparser = require("htmlparser2"); +import htmlparser = require('htmlparser2'); -var parser = new htmlparser.Parser({ - onopentag: (name:string, attribs:{[s:string]:string}) => { - if(name === "script" && attribs['type'] === "text/javascript"){ - console.log("JS! Hooray!"); - } - }, - ontext: (text: string) => { - console.log("-->", text); - }, - onclosetag: (tagname:string) => { - if(tagname === "script"){ - console.log("That's it?!"); - } +const options: htmlparser.DomHandlerOptions = { withEndIndices: false, withDomLvl1: true } +const dh = new htmlparser.DomHandler((err: Error, dom: htmlparser.DomElement[]) => { + if(err) { + throw err; } -}); + + // Use DomUtils to get name of first element in dom + console.log(htmlparser.DomUtils.getName(dom[0])); +}, options); +dh.onopentag = (name:string, attribs:{[s:string]:string}) => { + if(name === "script" && attribs['type'] === "text/javascript"){ + console.log("JS! Hooray!"); + } +}; +dh.ontext = (text: string) => { + console.log("-->", text); +}; +dh.onclosetag = () => { + console.log("That's it?!"); +}; + +var parser = new htmlparser.Parser(dh); parser.write("Xyz "); parser.end(); diff --git a/types/htmlparser2/index.d.ts b/types/htmlparser2/index.d.ts index 313ff56a96..c10dea047e 100644 --- a/types/htmlparser2/index.d.ts +++ b/types/htmlparser2/index.d.ts @@ -1,30 +1,20 @@ -// Type definitions for htmlparser2 v3.7.x +// Type definitions for htmlparser2 v3.10.x // Project: https://github.com/fb55/htmlparser2/ // Definitions by: James Roland Cabresos // Linus Unnebäck +// Johan Davidsson // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped /// +/// +/// import { Writable } from 'stream' +import { DomHandler } from 'domhandler'; +import * as DomUtils from 'domutils'; +export { DomElement, DomHandlerOptions, DomHandler, Element, Node } from 'domhandler'; -export interface Handler { - onopentag?: (name: string, attribs: { [type: string]: string }) => void; - onopentagname?: (name: string) => void; - onattribute?: (name: string, value: string) => void; - ontext?: (text: string) => void; - onclosetag?: (text: string) => void; - onprocessinginstruction?: (name: string, data: string) => void; - oncomment?: (data: string) => void; - oncommentend?: () => void; - oncdatastart?: () => void; - oncdataend?: () => void; - onerror?: (error: Error) => void; - onreset?: () => void; - onend?: () => void; -} - -export interface Options { +export interface ParserOptions { /*** * Indicates whether special tags (