Fast & forgiving HTML/XML/RSS parser with stricter attribute parsing rules.
npm install stricter-htmlparser2



A forgiving HTML/XML/RSS parser. The parser can handle streams and provides a callback interface.
A live demo of htmlparser2 is available here.
``
// output
{
attribs: {
name: "",
value: ""
}
}
`
2. "\"" is allowed in attribute value.
`
// output
{
attribs: {
name: "hello \\"world"
}
}
`
3. Record attributes key in a object that value is wrapped by single quote. The object is passed to onopentag method of parser's domhandler through the third argument.
`
// The third argument passed to onopentag:
{
name: true,
class: true
}
`
// If used with x-domhandler which is exported by this module, the parse result is like this:
`javascript`
{
type: "tag",
name: "foo",
attribs: {
name: "{{flag ? \"true\" : \"false\"}}",
title: "title",
class: "hidden"
},
singleQuoteAttribs: {
name: true,
class: true
},
selfClose: false
}
4. Add selfClose flag to parsed node element.
Input:
`html`
Parser code:
`javascript
const {Parser, DomHandler} = require('stricter-htmlparser2');
const parser = new Parser(new DomHandler(), {}).end(inputStr);
`
Output
`javascript`
{
type: "tag",
name: "foo",
attribs: {
name: "{{flag ? \"true\" : \"false\"}}",
title: "title",
class: "hidden"
},
singleQuoteAttribs: {
name: true,
class: true
},
selfClose: true
}
`javascript
var htmlparser = require("stricter-htmlparser2");
var parser = new htmlparser.Parser({
onopentag: function(name, attribs){
if(name === "script" && attribs.type === "text/javascript"){
console.log("JS! Hooray!");
}
},
ontext: function(text){
console.log("-->", text);
},
onclosetag: function(tagname){
if(tagname === "script"){
console.log("That's it?!");
}
}
}, {decodeEntities: true});
parser.write("Xyz