The DOM handler creates a tree containing all nodes of a page. The tree can be manipulated using the domutils or cheerio libraries and rendered using dom-serializer .
Usage
const handler = new DomHandler([ <func> callback(err, dom), ] [ <obj> options ]); // const parser = new Parser(handler[, options]);
Available options are described below.
Example
const { Parser } = require("htmlparser2"); const { DomHandler } = require("domhandler"); const rawHtml = "Xyz <script language= javascript>var foo = '<<bar>>';</script><!--<!-- Waah! -- -->"; const handler = new DomHandler((error, dom) => { if (error) { // Handle error } else { // Parsing completed, do something console.log(dom); } }); const parser = new Parser(handler); parser.write(rawHtml); parser.end();
Output:
"[
{
data: "Xyz ",
type: "text",
},
{
type: "script",
name: "script",
attribs: {
language: "javascript",
},
children: [
{
data: "var foo = ' ';<",
type: "text",
},
],
},
{
data: "