DefinitelyTyped/types/htmlparser2/htmlparser2-tests.ts

31 lines
870 B
TypeScript
Raw Normal View History

2014-08-04 12:54:42 +00:00
/**
* Created by staticfunction on 8/4/14.
*/
import htmlparser = require('htmlparser2');
2014-08-04 12:54:42 +00:00
const options: htmlparser.DomHandlerOptions = { withEndIndices: false, withDomLvl1: true }
const dh = new htmlparser.DomHandler((err: Error, dom: htmlparser.DomElement[]) => {
if(err) {
throw err;
2014-08-04 12:54:42 +00:00
}
// 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);
2014-08-04 12:54:42 +00:00
parser.write("Xyz <script type='text/javascript'>var foo = '<<bar>>';</script>");
parser.end();