DefinitelyTyped/types/htmlparser2/htmlparser2-tests.ts
Johan Davidsson 3c9fd4a692 [@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
2019-03-25 09:39:20 -07:00

31 lines
870 B
TypeScript

/**
* Created by staticfunction on 8/4/14.
*/
import htmlparser = require('htmlparser2');
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();