# stricter-htmlparser2
[](https://npmjs.org/package/stricter-htmlparser2)
[](https://npmjs.org/package/stricter-htmlparser2)
[](http://travis-ci.org/yanghuabei/htmlparser2)
[](https://coveralls.io/r/yanghuabei/htmlparser2)
A forgiving HTML/XML/RSS parser. The parser can handle streams and provides a callback interface.
## Installation
npm install 'stricter-htmlparser2'
A live demo of htmlparser2 is available [here](https://astexplorer.net/#/2AmVrGuGVJ).
## Differences to htmlparser2
1. Attribute value without quote wrapped is not allowed.
```
// 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
}
```
## Usage
```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