[@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
This commit is contained in:
Johan Davidsson
2019-03-25 17:39:20 +01:00
committed by timolinn
parent 486030d264
commit 58e4f00832
6 changed files with 74 additions and 42 deletions

View File

@@ -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 };

View File

@@ -2,7 +2,6 @@
// Project: https://github.com/fb55/DomHandler#readme
// Definitions by: Johan Davidsson <https://github.com/johandavidson>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
export interface DomHandlerOptions {
/***

View File

@@ -2,7 +2,6 @@
// Project: https://github.com/FB55/domutils#readme
// Definitions by: Johan Davidsson <https://github.com/johandavidson>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.1
import { DomElement } from "domhandler";
/***

View File

@@ -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 <script type='text/javascript'>var foo = '<<bar>>';</script>");
parser.end();

View File

@@ -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 <https://github.com/staticfunction>
// Linus Unnebäck <https://github.com/LinusU>
// Johan Davidsson <https://github.com/johandavidson>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
///<reference types="node"/>
///<reference types="domhandler"/>
///<reference types="domutils"/>
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 (<script> and <style>) should get special treatment
@@ -61,33 +51,53 @@ export interface Options {
*/
recognizeSelfClosing?: boolean;
}
/**
* @deprecated
*/
export type Options = ParserOptions
export declare class WritableStream extends Writable {
constructor(handler: Handler, options?: Options);
constructor(handler: DomHandler, options?: ParserOptions);
}
export declare class Parser {
constructor(handler: Handler, options?: Options);
constructor(handler: DomHandler, options?: ParserOptions);
/***
* Parses a chunk of data and calls the corresponding callbacks.
* @param input
*/
write(input: string): void;
/***
* alias for backwards compat
*/
parseChunk(input: string): void;
parseChunk(chunk: string): void;
/***
* Parses the end of the buffer and clears the stack, calls onend.
*/
end(): void;
/***
* Parses the end of the buffer and clears the stack, calls onend.
*/
end(chunk: string): void;
/***
* alias for backwards compat
*/
done(): void;
/***
* Pauses the parser
*/
pause(): void;
/***
* Resumes the parser
*/
resume(): void;
/***
* Resets the parser, parses the data & calls end.
@@ -99,4 +109,20 @@ export declare class Parser {
* Resets buffer & stack, calls onreset.
*/
reset(): void;
ontext(data: any): void;
onopentagname(name: string): void;
onopentagend(): void;
onclosetag(name: string): void;
onselfclosingtag(): void;
onattribname(name: string): void;
onattribend(): void;
ondeclaration(): void;
onprocessinginstruction(value: string): void;
oncomment(value: string): void;
oncdata(value: string): void;
onerror(err: Error): void;
onend(): void;
}
export { DomUtils }

View File

@@ -6,6 +6,7 @@
// Rinze de Laat <https://github.com/biermeester>
// Will Gibson <https://github.com/WillGibson>
// A penguin <https://github.com/sirMerr>
// Johan Davidsson <https://github.com/johandavidson>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
import {Options} from "htmlparser2";