Adding types for module domhandler

This commit is contained in:
Johan Davidsson
2019-03-13 20:50:05 +01:00
parent ae3762c3f3
commit 7ff66525e2
4 changed files with 117 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
import * as htmlparser from "htmlparser2";
const rawHtml = "Xyz <script language= javascript>var foo = '<<bar>>';< / script><!--<!-- Waah! -- -->";
const handler = new htmlparser.DomHandler((error: Error, dom: htmlparser.DomElement[]) => {
if (error)
console.error('There has been an error...');
else
console.log(dom);
});
const parser = new htmlparser.Parser(handler);
parser.write(rawHtml);
parser.end();

80
types/domhandler/index.d.ts vendored Normal file
View File

@@ -0,0 +1,80 @@
// Type definitions for domhandler 2.4.2
// 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 {
/***
* Indicates whether the whitespace in text nodes should be normalized
* (= all whitespace should be replaced with single spaces). The default value is "false".
*/
normalizeWhitespace: boolean;
/***
* Adds DOM level 1 properties to all elements.
*/
withDomLvl1: boolean;
/***
* Indicates whether a startIndex property will be added to nodes.
* When the parser is used in a non-streaming fashion, startIndex is an integer
* indicating the position of the start of the node in the document.
* The default value is "false".
*/
withStartIndices: boolean;
/***
* Indicates whether a endIndex property will be added to nodes.
* When the parser is used in a non-streaming fashion, endIndex is an integer
* indicating the position of the end of the node in the document.
* The default value is "false".
*/
withEndIndices: boolean;
}
export interface DomElement {
attribs?: {[s: string]: string};
children?: DomElement[];
data?: any;
name?: string;
next?: DomElement;
parent?: DomElement;
prev?: DomElement;
type?: string;
}
export interface Element extends DomElement {
name: string;
}
export interface Node extends DomElement {
firstChild(): DomElement;
lastChild(): DomElement;
nodeType(): number;
}
export class DomHandler {
constructor(callback: (error: any, dom: DomElement[]) => any);
constructor(callback: (error: any, dom: DomElement[]) => any, options: DomHandlerOptions);
onparserinit(parser: any): void;
/***
* Resets the handler back to starting state
*/
onreset(): void;
/***
* Signals the handler that parsing is done
*/
onend(): void;
onerror(error: Error): void;
onclosetag(): void;
onopentag(name: string, attribs: {[s: string]: string}): void;
ontext(data: string): void;
oncomment(data: string): void;
oncdatastart(): void;
oncommentend(): void;
onprocessinginstruction(name: string, data: string): void;
}

View File

@@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": [
"es6"
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
"../"
],
"types": [],
"noEmit": true,
"forceConsistentCasingInFileNames": true
},
"files": [
"index.d.ts",
"domhandler-tests.ts"
]
}

View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }