` element will be used.
*Example:*
```js
var documentFragment = parser.parseFragment('
');
//Parse html fragment in context of the parsed
element
var trFragment = parser.parseFragment('Shake it, baby |
', documentFragment.childNodes[0]);
```
---------------------------------------
###Class: SimpleApiParser
Provides [SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)-style HTML parsing functionality.
####• SimpleApiParser.ctor(handlers)
Creates new reusable instance of the `SimpleApiParser`. `handlers` argument specifies object that contains parser's event handlers. Possible events and their signatures are shown in the example.
*Example:*
```js
var parse5 = require('parse5');
var parser = new parse5.SimpleApiParser({
doctype: function(name, publicId, systemId) {
//Handle doctype here
},
startTag: function(tagName, attrs, selfClosing) {
//Handle start tags here
},
endTag: function(tagName) {
//Handle end tags here
},
text: function(text) {
//Handle texts here
},
comment: function(text) {
//Handle comments here
}
});
```
####• SimpleApiParser.parse(html)
Raises parser events for the given `html`.
*Example:*
```js
var parse5 = require('parse5');
var parser = new parse5.SimpleApiParser({
text: function(text) {
console.log(text);
}
});
parser.parse('Yo!');
```
---------------------------------------
###Class: TreeSerializer
Provides tree-to-HTML serialization functionality.
####• TreeSerializer.ctor([treeAdapter])
Creates new reusable instance of the `TreeSerializer`. Optional `treeAdapter` argument specifies input tree format. If `treeAdapter` argument is not specified, `default` tree adapter will be used.
*Example:*
```js
var parse5 = require('parse5');
//Instantiate new serializer with default tree adapter
var serializer1 = new parse5.TreeSerializer();
//Instantiate new serializer with htmlparser2 tree adapter
var serializer2 = new parse5.TreeSerializer(parse5.TreeAdapters.htmlparser2);
```
####• TreeSerializer.serialize(node)
Serializes the given `node`. Returns HTML string.
*Example:*
```js
var document = parser.parse('Hi there!');
//Serialize document
var html = serializer.serialize(document);
//Serialize element content
var bodyInnerHtml = serializer.serialize(document.childNodes[0].childNodes[1]);
```
---------------------------------------
##Testing
Test data is adopted from [html5lib project](https://github.com/html5lib). Parser is covered by more than 8000 test cases.
To run tests:
```
$ npm test
```
##Custom tree adapter
You can create a custom tree adapter so parse5 can work with your own DOM-tree implementation.
Just pass your adapter implementation to the parser's constructor as an argument:
```js
var Parser = require('parse5').Parser;
var myTreeAdapter = {
//Adapter methods...
};
//Instantiate parser
var parser = new Parser(myTreeAdapter);
```
Sample implementation can be found [here](https://github.com/inikulin/parse5/blob/master/lib/tree_adapters/default.js).
The custom tree adapter should implement all methods exposed via `exports` in the sample implementation.
##Questions or suggestions?
If you have any questions, please feel free to create an issue [here on github](https://github.com/inikulin/parse5/issues).
##Author
[Ivan Nikulin](https://github.com/inikulin) (ifaaan@gmail.com)