parse5 1.4.0 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,57 +1,69 @@
1
- ## 1.4.0
2
- * Add: Parser [decodeHtmlEntities](https://github.com/inikulin/parse5#optionsdecodehtmlentities) option.
3
- * Add: SimpleApiParser [decodeHtmlEntities](https://github.com/inikulin/parse5#optionsdecodehtmlentities-1) option.
4
- * Add: Parser [locationInfo](https://github.com/inikulin/parse5#optionslocationinfo) option.
5
- * Add: SimpleApiParser [locationInfo](https://github.com/inikulin/parse5#optionslocationinfo-1) option.
6
-
7
- ## 1.3.2
8
- * Fix: `<form>` processing in `<template>` (GH [#40](https://github.com/inikulin/parse5/issues/40))
9
-
10
- ## 1.3.1
11
- * Fix: text node in `<template>` serialization problem with custom tree adapter (GH [#38](https://github.com/inikulin/parse5/issues/38))
12
-
13
- ## 1.3.0
14
- * Add: Serializer `encodeHtmlEntities` option.
15
-
16
- ## 1.2.0
17
- * Add: `<template>` support
18
- * `parseFragment` now uses `<template>` as default `contextElement`. This leads to the more "forgiving" parsing manner.
19
- * `TreeSerializer` was renamed to `Serializer`. However, serializer is accessible as `parse5.TreeSerializer` for backward compatibility .
20
-
21
- ## 1.1.6
22
- * Fix: apply latest changes to the `htmlparser2` tree format (DOM Level1 node emulation).
23
-
24
- ## 1.1.5
25
- * Add: [jsdom](https://github.com/tmpvar/jsdom)-specific parser with scripting support. Undocumented for `jsdom` internal use only.
26
-
27
- ## 1.1.4
28
- * Add: logo
29
- * Fix: use fake `document` element for fragment parsing (required by [jsdom](https://github.com/tmpvar/jsdom)).
30
-
31
- ## 1.1.3
32
- * Development files (e.g. `.travis.yml`, `.editorconfig`) are removed from NPM package.
33
-
34
- ## 1.1.2
35
- * Fix: crash on Linux due to upper-case leading character in module name used in `require()`
36
-
37
- ## 1.1.1
38
- * Add: [SimpleApiParser](https://github.com/inikulin/parse5/#class-simpleapiparser)
39
- * Fix: new line serialization in `<pre>`
40
- * Fix: `SYSTEM`-only `DOCTYPE` serialization
41
- * Fix: quotes serialization in `DOCTYPE` IDs
42
-
43
- ## 1.0.0
44
- * First stable release, switch to semantic versioning.
45
-
46
- ## 0.8.3
47
- * Fix: siblings calculation bug in `appendChild` in `htmlparser2` tree adapter.
48
-
49
- ## 0.8.1
50
- * Add: [TreeSerializer](https://github.com/inikulin/parse5/#class-serializer).
51
- * Add: [htmlparser2 tree adapter](https://github.com/inikulin/parse5/#-treeadaptershtmlparser2).
52
-
53
- ## 0.6.1
54
- * Fix: incorrect `<menuitem>` handling in `<body>`.
55
-
56
- ## 0.6.0
57
- * Initial release.
1
+ ## 1.5.1
2
+ * Fix: Qualified tag name emission in Serializer (GH [#79](https://github.com/inikulin/parse5/issues/79)).
3
+
4
+ ## 1.5.0
5
+ * Add: Location info for the element start and end tags (by @sakagg).
6
+
7
+ ## 1.4.2
8
+ * Fix: htmlparser2 tree adapter `DocumentType.data` property rendering (GH [#45](https://github.com/inikulin/parse5/issues/45)).
9
+
10
+ ## 1.4.1
11
+ * Fix: Location info handling for the implicitly generated `<html>` and `<body>` elements (GH [#44](https://github.com/inikulin/parse5/issues/44)).
12
+
13
+ ## 1.4.0
14
+ * Add: Parser [decodeHtmlEntities](https://github.com/inikulin/parse5#optionsdecodehtmlentities) option.
15
+ * Add: SimpleApiParser [decodeHtmlEntities](https://github.com/inikulin/parse5#optionsdecodehtmlentities-1) option.
16
+ * Add: Parser [locationInfo](https://github.com/inikulin/parse5#optionslocationinfo) option.
17
+ * Add: SimpleApiParser [locationInfo](https://github.com/inikulin/parse5#optionslocationinfo-1) option.
18
+
19
+ ## 1.3.2
20
+ * Fix: `<form>` processing in `<template>` (GH [#40](https://github.com/inikulin/parse5/issues/40)).
21
+
22
+ ## 1.3.1
23
+ * Fix: text node in `<template>` serialization problem with custom tree adapter (GH [#38](https://github.com/inikulin/parse5/issues/38)).
24
+
25
+ ## 1.3.0
26
+ * Add: Serializer `encodeHtmlEntities` option.
27
+
28
+ ## 1.2.0
29
+ * Add: `<template>` support
30
+ * `parseFragment` now uses `<template>` as default `contextElement`. This leads to the more "forgiving" parsing manner.
31
+ * `TreeSerializer` was renamed to `Serializer`. However, serializer is accessible as `parse5.TreeSerializer` for backward compatibility .
32
+
33
+ ## 1.1.6
34
+ * Fix: apply latest changes to the `htmlparser2` tree format (DOM Level1 node emulation).
35
+
36
+ ## 1.1.5
37
+ * Add: [jsdom](https://github.com/tmpvar/jsdom)-specific parser with scripting support. Undocumented for `jsdom` internal use only.
38
+
39
+ ## 1.1.4
40
+ * Add: logo
41
+ * Fix: use fake `document` element for fragment parsing (required by [jsdom](https://github.com/tmpvar/jsdom)).
42
+
43
+ ## 1.1.3
44
+ * Development files (e.g. `.travis.yml`, `.editorconfig`) are removed from NPM package.
45
+
46
+ ## 1.1.2
47
+ * Fix: crash on Linux due to upper-case leading character in module name used in `require()`.
48
+
49
+ ## 1.1.1
50
+ * Add: [SimpleApiParser](https://github.com/inikulin/parse5/#class-simpleapiparser).
51
+ * Fix: new line serialization in `<pre>`.
52
+ * Fix: `SYSTEM`-only `DOCTYPE` serialization.
53
+ * Fix: quotes serialization in `DOCTYPE` IDs.
54
+
55
+ ## 1.0.0
56
+ * First stable release, switch to semantic versioning.
57
+
58
+ ## 0.8.3
59
+ * Fix: siblings calculation bug in `appendChild` in `htmlparser2` tree adapter.
60
+
61
+ ## 0.8.1
62
+ * Add: [TreeSerializer](https://github.com/inikulin/parse5/#class-serializer).
63
+ * Add: [htmlparser2 tree adapter](https://github.com/inikulin/parse5/#-treeadaptershtmlparser2).
64
+
65
+ ## 0.6.1
66
+ * Fix: incorrect `<menuitem>` handling in `<body>`.
67
+
68
+ ## 0.6.0
69
+ * Initial release.
package/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013-2014 Ivan Nikulin (ifaaan@gmail.com)
1
+ Copyright (c) 2013-2015 Ivan Nikulin (ifaaan@gmail.com)
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -1,246 +1,247 @@
1
- <p align="center">
2
- <img src="https://raw.github.com/inikulin/parse5/master/logo.png" alt="parse5" />
3
- </p>
4
-
5
- [![Build Status](https://api.travis-ci.org/inikulin/parse5.svg)](https://travis-ci.org/inikulin/parse5)
6
-
7
- *WHATWG HTML5 specification-compliant, fast and ready for production HTML parsing/serialization toolset for Node.*
8
-
9
- To build [TestCafé](http://testcafe.devexpress.com/) we needed fast and ready for production HTML parser, which will parse HTML as a modern browser's parser.
10
- Existing solutions were either too slow or their output was too inaccurate. So, this is how parse5 was born.
11
-
12
- **Included tools:**
13
- * [Parser](#class-parser) - HTML to DOM-tree parser.
14
- * [SimpleApiParser](#class-simpleapiparser) - [SAX](http://en.wikipedia.org/wiki/Simple_API_for_XML)-style parser for HTML.
15
- * [Serializer](#class-serializer) - DOM-tree to HTML code serializer.
16
-
17
- ## Install
18
- ```
19
- $ npm install parse5
20
- ```
21
-
22
-
23
- ## Usage
24
- ```js
25
- var Parser = require('parse5').Parser;
26
-
27
- //Instantiate parser
28
- var parser = new Parser();
29
-
30
- //Then feed it with an HTML document
31
- var document = parser.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>')
32
-
33
- //Now let's parse HTML-snippet
34
- var fragment = parser.parseFragment('<title>Parse5 is &#102;&#117;&#99;&#107;ing awesome!</title><h1>42</h1>');
35
-
36
- ```
37
-
38
- ## Is it fast?
39
- Check out [this benchmark](https://github.com/inikulin/node-html-parser-bench).
40
-
41
- ```
42
- Starting benchmark. Fasten your seatbelts...
43
- html5 (https://github.com/aredridel/html5) x 0.18 ops/sec ±5.92% (5 runs sampled)
44
- htmlparser (https://github.com/tautologistics/node-htmlparser/) x 3.83 ops/sec ±42.43% (14 runs sampled)
45
- htmlparser2 (https://github.com/fb55/htmlparser2) x 4.05 ops/sec ±39.27% (15 runs sampled)
46
- parse5 (https://github.com/inikulin/parse5) x 3.04 ops/sec ±51.81% (13 runs sampled)
47
- Fastest is htmlparser2 (https://github.com/fb55/htmlparser2),parse5 (https://github.com/inikulin/parse5)
48
- ```
49
-
50
- So, parse5 is as fast as simple specification incompatible parsers and ~15-times(!) faster than the current specification compatible parser available for the node.
51
-
52
-
53
- ## API reference
54
-
55
- ### Enum: TreeAdapters
56
- Provides built-in tree adapters which can be passed as an optional argument to the `Parser` and `Serializer` constructors.
57
-
58
- #### &bull; TreeAdapters.default
59
- Default tree format for parse5.
60
-
61
-
62
- #### &bull; TreeAdapters.htmlparser2
63
- Quite popular [htmlparser2](https://github.com/fb55/htmlparser2) tree format (e.g. used in [cheerio](https://github.com/MatthewMueller/cheerio) and [jsdom](https://github.com/tmpvar/jsdom)).
64
-
65
- ---------------------------------------
66
-
67
-
68
- ### Class: Parser
69
- Provides HTML parsing functionality.
70
-
71
- #### &bull; Parser.ctor([treeAdapter, options])
72
- Creates new reusable instance of the `Parser`. Optional `treeAdapter` argument specifies resulting tree format. If `treeAdapter` argument is not specified, `default` tree adapter will be used.
73
-
74
- `options` object provides the parsing algorithm modifications:
75
- ##### options.decodeHtmlEntities
76
- Decode HTML-entities like `&amp;`, `&nbsp;`, etc. Default: `true`. **Warning:** disabling this option may cause output which is not conform HTML5 specification.
77
- ##### options.locationInfo
78
- Enables source code location information for the nodes. Default: `false`. When enabled, each node (except root node) has `__location` property, which contains `start` and `end` indices of the node in the source code.
79
-
80
- *Example:*
81
- ```js
82
- var parse5 = require('parse5');
83
-
84
- //Instantiate new parser with default tree adapter
85
- var parser1 = new parse5.Parser();
86
-
87
- //Instantiate new parser with htmlparser2 tree adapter
88
- var parser2 = new parse5.Parser(parse5.TreeAdapters.htmlparser2);
89
- ```
90
-
91
-
92
-
93
- #### &bull; Parser.parse(html)
94
- Parses specified `html` string. Returns `document` node.
95
-
96
- *Example:*
97
- ```js
98
- var document = parser.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
99
- ```
100
-
101
-
102
- #### &bull; Parser.parseFragment(htmlFragment, [contextElement])
103
- Parses given `htmlFragment`. Returns `documentFragment` node. Optional `contextElement` argument specifies context in which given `htmlFragment` will be parsed (consider it as setting `contextElement.innerHTML` property). If `contextElement` argument is not specified then `<template>` element will be used as a context and fragment will be parsed in 'forgiving' manner.
104
-
105
- *Example:*
106
- ```js
107
- var documentFragment = parser.parseFragment('<table></table>');
108
-
109
- //Parse html fragment in context of the parsed <table> element
110
- var trFragment = parser.parseFragment('<tr><td>Shake it, baby</td></tr>', documentFragment.childNodes[0]);
111
- ```
112
-
113
- ---------------------------------------
114
-
115
-
116
- ### Class: SimpleApiParser
117
- Provides [SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)-style HTML parsing functionality.
118
-
119
- #### &bull; SimpleApiParser.ctor(handlers, [options])
120
- 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.
121
-
122
- `options` object provides the parsing algorithm modifications:
123
- ##### options.decodeHtmlEntities
124
- Decode HTML-entities like `&amp;`, `&nbsp;`, etc. Default: `true`. **Warning:** disabling this option may cause output which is not conform HTML5 specification.
125
- ##### options.locationInfo
126
- Enables source code location information for the tokens. Default: `false`. When enabled, each node handler receives `location` object as it's last argument. `location` object contains `start` and `end` indices of the token in the source code.
127
-
128
-
129
- *Example:*
130
- ```js
131
- var parse5 = require('parse5');
132
-
133
- var parser = new parse5.SimpleApiParser({
134
- doctype: function(name, publicId, systemId /*, [location] */) {
135
- //Handle doctype here
136
- },
137
-
138
- startTag: function(tagName, attrs, selfClosing /*, [location] */) {
139
- //Handle start tags here
140
- },
141
-
142
- endTag: function(tagName /*, [location] */) {
143
- //Handle end tags here
144
- },
145
-
146
- text: function(text /*, [location] */) {
147
- //Handle texts here
148
- },
149
-
150
- comment: function(text /*, [location] */) {
151
- //Handle comments here
152
- }
153
- });
154
- ```
155
-
156
- #### &bull; SimpleApiParser.parse(html)
157
- Raises parser events for the given `html`.
158
-
159
- *Example:*
160
- ```js
161
- var parse5 = require('parse5');
162
-
163
- var parser = new parse5.SimpleApiParser({
164
- text: function(text) {
165
- console.log(text);
166
- }
167
- });
168
-
169
- parser.parse('<body>Yo!</body>');
170
- ```
171
-
172
- ---------------------------------------
173
-
174
- ### Class: Serializer
175
- Provides tree-to-HTML serialization functionality.
176
- **Note:** prior to v1.2.0 this class was called `TreeSerializer`. However, it's still accessible as `parse5.TreeSerializer` for backward compatibility.
177
-
178
- #### &bull; Serializer.ctor([treeAdapter, options])
179
- Creates new reusable instance of the `Serializer`. Optional `treeAdapter` argument specifies input tree format. If `treeAdapter` argument is not specified, `default` tree adapter will be used.
180
-
181
- `options` object provides the serialization algorithm modifications:
182
- ##### options.encodeHtmlEntities
183
- HTML-encode characters like `<`, `>`, `&`, etc. Default: `true`. **Warning:** disabling this option may cause output which is not conform HTML5 specification.
184
-
185
-
186
- *Example:*
187
- ```js
188
- var parse5 = require('parse5');
189
-
190
- //Instantiate new serializer with default tree adapter
191
- var serializer1 = new parse5.Serializer();
192
-
193
- //Instantiate new serializer with htmlparser2 tree adapter
194
- var serializer2 = new parse5.Serializer(parse5.TreeAdapters.htmlparser2);
195
- ```
196
-
197
-
198
- #### &bull; Serializer.serialize(node)
199
- Serializes the given `node`. Returns HTML string.
200
-
201
- *Example:*
202
- ```js
203
- var document = parser.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
204
-
205
- //Serialize document
206
- var html = serializer.serialize(document);
207
-
208
- //Serialize <body> element content
209
- var bodyInnerHtml = serializer.serialize(document.childNodes[0].childNodes[1]);
210
- ```
211
-
212
- ---------------------------------------
213
-
214
-
215
- ## Testing
216
- Test data is adopted from [html5lib project](https://github.com/html5lib). Parser is covered by more than 8000 test cases.
217
- To run tests:
218
- ```
219
- $ npm test
220
- ```
221
-
222
-
223
- ## Custom tree adapter
224
- You can create a custom tree adapter so parse5 can work with your own DOM-tree implementation.
225
- Just pass your adapter implementation to the parser's constructor as an argument:
226
-
227
- ```js
228
- var Parser = require('parse5').Parser;
229
-
230
- var myTreeAdapter = {
231
- //Adapter methods...
232
- };
233
-
234
- //Instantiate parser
235
- var parser = new Parser(myTreeAdapter);
236
- ```
237
-
238
- Sample implementation can be found [here](https://github.com/inikulin/parse5/blob/master/lib/tree_adapters/default.js).
239
- The custom tree adapter should implement all methods exposed via `exports` in the sample implementation.
240
-
241
- ## Questions or suggestions?
242
- If you have any questions, please feel free to create an issue [here on github](https://github.com/inikulin/parse5/issues).
243
-
244
-
245
- ## Author
246
- [Ivan Nikulin](https://github.com/inikulin) (ifaaan@gmail.com)
1
+ <p align="center">
2
+ <img src="https://raw.github.com/inikulin/parse5/master/logo.png" alt="parse5" />
3
+ </p>
4
+
5
+ [![Build Status](https://api.travis-ci.org/inikulin/parse5.svg)](https://travis-ci.org/inikulin/parse5)
6
+ [![npm](https://img.shields.io/npm/v/parse5.svg)](https://www.npmjs.com/package/parse5)
7
+
8
+ *WHATWG HTML5 specification-compliant, fast and ready for production HTML parsing/serialization toolset for Node and io.js.*
9
+
10
+ I needed fast and ready for production HTML parser, which will parse HTML as a modern browser's parser.
11
+ Existing solutions were either too slow or their output was too inaccurate. So, this is how parse5 was born.
12
+
13
+ **Included tools:**
14
+ * [Parser](#class-parser) - HTML to DOM-tree parser.
15
+ * [SimpleApiParser](#class-simpleapiparser) - [SAX](http://en.wikipedia.org/wiki/Simple_API_for_XML)-style parser for HTML.
16
+ * [Serializer](#class-serializer) - DOM-tree to HTML code serializer.
17
+
18
+ ## Install
19
+ ```
20
+ $ npm install parse5
21
+ ```
22
+
23
+
24
+ ## Usage
25
+ ```js
26
+ var Parser = require('parse5').Parser;
27
+
28
+ //Instantiate parser
29
+ var parser = new Parser();
30
+
31
+ //Then feed it with an HTML document
32
+ var document = parser.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>')
33
+
34
+ //Now let's parse HTML-snippet
35
+ var fragment = parser.parseFragment('<title>Parse5 is &#102;&#117;&#99;&#107;ing awesome!</title><h1>42</h1>');
36
+
37
+ ```
38
+
39
+ ## Is it fast?
40
+ Check out [this benchmark](https://github.com/inikulin/node-html-parser-bench).
41
+
42
+ ```
43
+ Starting benchmark. Fasten your seatbelts...
44
+ html5 (https://github.com/aredridel/html5) x 0.18 ops/sec ±5.92% (5 runs sampled)
45
+ htmlparser (https://github.com/tautologistics/node-htmlparser/) x 3.83 ops/sec ±42.43% (14 runs sampled)
46
+ htmlparser2 (https://github.com/fb55/htmlparser2) x 4.05 ops/sec ±39.27% (15 runs sampled)
47
+ parse5 (https://github.com/inikulin/parse5) x 3.04 ops/sec ±51.81% (13 runs sampled)
48
+ Fastest is htmlparser2 (https://github.com/fb55/htmlparser2),parse5 (https://github.com/inikulin/parse5)
49
+ ```
50
+
51
+ So, parse5 is as fast as simple specification incompatible parsers and ~15-times(!) faster than the current specification compatible parser available for the node.
52
+
53
+
54
+ ## API reference
55
+
56
+ ### Enum: TreeAdapters
57
+ Provides built-in tree adapters which can be passed as an optional argument to the `Parser` and `Serializer` constructors.
58
+
59
+ #### &bull; TreeAdapters.default
60
+ Default tree format for parse5.
61
+
62
+
63
+ #### &bull; TreeAdapters.htmlparser2
64
+ Quite popular [htmlparser2](https://github.com/fb55/htmlparser2) tree format (e.g. used in [cheerio](https://github.com/MatthewMueller/cheerio) and [jsdom](https://github.com/tmpvar/jsdom)).
65
+
66
+ ---------------------------------------
67
+
68
+
69
+ ### Class: Parser
70
+ Provides HTML parsing functionality.
71
+
72
+ #### &bull; Parser.ctor([treeAdapter, options])
73
+ Creates new reusable instance of the `Parser`. Optional `treeAdapter` argument specifies resulting tree format. If `treeAdapter` argument is not specified, `default` tree adapter will be used.
74
+
75
+ `options` object provides the parsing algorithm modifications:
76
+ ##### options.decodeHtmlEntities
77
+ Decode HTML-entities like `&amp;`, `&nbsp;`, etc. Default: `true`. **Warning:** disabling this option may cause output which is not conform HTML5 specification.
78
+ ##### options.locationInfo
79
+ Enables source code location information for the nodes. Default: `false`. When enabled, each node (except root node) has `__location` property, which contains `start` and `end` indices of the node in the source code. If element was implicitly created by the parser it's `__location` property will be `null`. In case the node is not an empty element, `__location` has two addition properties `startTag` and `endTag` which contain location information for individual tags in a fashion similar to `__location` property.
80
+
81
+ *Example:*
82
+ ```js
83
+ var parse5 = require('parse5');
84
+
85
+ //Instantiate new parser with default tree adapter
86
+ var parser1 = new parse5.Parser();
87
+
88
+ //Instantiate new parser with htmlparser2 tree adapter
89
+ var parser2 = new parse5.Parser(parse5.TreeAdapters.htmlparser2);
90
+ ```
91
+
92
+
93
+
94
+ #### &bull; Parser.parse(html)
95
+ Parses specified `html` string. Returns `document` node.
96
+
97
+ *Example:*
98
+ ```js
99
+ var document = parser.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
100
+ ```
101
+
102
+
103
+ #### &bull; Parser.parseFragment(htmlFragment, [contextElement])
104
+ Parses given `htmlFragment`. Returns `documentFragment` node. Optional `contextElement` argument specifies context in which given `htmlFragment` will be parsed (consider it as setting `contextElement.innerHTML` property). If `contextElement` argument is not specified then `<template>` element will be used as a context and fragment will be parsed in 'forgiving' manner.
105
+
106
+ *Example:*
107
+ ```js
108
+ var documentFragment = parser.parseFragment('<table></table>');
109
+
110
+ //Parse html fragment in context of the parsed <table> element
111
+ var trFragment = parser.parseFragment('<tr><td>Shake it, baby</td></tr>', documentFragment.childNodes[0]);
112
+ ```
113
+
114
+ ---------------------------------------
115
+
116
+
117
+ ### Class: SimpleApiParser
118
+ Provides [SAX](https://en.wikipedia.org/wiki/Simple_API_for_XML)-style HTML parsing functionality.
119
+
120
+ #### &bull; SimpleApiParser.ctor(handlers, [options])
121
+ 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.
122
+
123
+ `options` object provides the parsing algorithm modifications:
124
+ ##### options.decodeHtmlEntities
125
+ Decode HTML-entities like `&amp;`, `&nbsp;`, etc. Default: `true`. **Warning:** disabling this option may cause output which is not conform HTML5 specification.
126
+ ##### options.locationInfo
127
+ Enables source code location information for the tokens. Default: `false`. When enabled, each node handler receives `location` object as it's last argument. `location` object contains `start` and `end` indices of the token in the source code.
128
+
129
+
130
+ *Example:*
131
+ ```js
132
+ var parse5 = require('parse5');
133
+
134
+ var parser = new parse5.SimpleApiParser({
135
+ doctype: function(name, publicId, systemId /*, [location] */) {
136
+ //Handle doctype here
137
+ },
138
+
139
+ startTag: function(tagName, attrs, selfClosing /*, [location] */) {
140
+ //Handle start tags here
141
+ },
142
+
143
+ endTag: function(tagName /*, [location] */) {
144
+ //Handle end tags here
145
+ },
146
+
147
+ text: function(text /*, [location] */) {
148
+ //Handle texts here
149
+ },
150
+
151
+ comment: function(text /*, [location] */) {
152
+ //Handle comments here
153
+ }
154
+ });
155
+ ```
156
+
157
+ #### &bull; SimpleApiParser.parse(html)
158
+ Raises parser events for the given `html`.
159
+
160
+ *Example:*
161
+ ```js
162
+ var parse5 = require('parse5');
163
+
164
+ var parser = new parse5.SimpleApiParser({
165
+ text: function(text) {
166
+ console.log(text);
167
+ }
168
+ });
169
+
170
+ parser.parse('<body>Yo!</body>');
171
+ ```
172
+
173
+ ---------------------------------------
174
+
175
+ ### Class: Serializer
176
+ Provides tree-to-HTML serialization functionality.
177
+ **Note:** prior to v1.2.0 this class was called `TreeSerializer`. However, it's still accessible as `parse5.TreeSerializer` for backward compatibility.
178
+
179
+ #### &bull; Serializer.ctor([treeAdapter, options])
180
+ Creates new reusable instance of the `Serializer`. Optional `treeAdapter` argument specifies input tree format. If `treeAdapter` argument is not specified, `default` tree adapter will be used.
181
+
182
+ `options` object provides the serialization algorithm modifications:
183
+ ##### options.encodeHtmlEntities
184
+ HTML-encode characters like `<`, `>`, `&`, etc. Default: `true`. **Warning:** disabling this option may cause output which is not conform HTML5 specification.
185
+
186
+
187
+ *Example:*
188
+ ```js
189
+ var parse5 = require('parse5');
190
+
191
+ //Instantiate new serializer with default tree adapter
192
+ var serializer1 = new parse5.Serializer();
193
+
194
+ //Instantiate new serializer with htmlparser2 tree adapter
195
+ var serializer2 = new parse5.Serializer(parse5.TreeAdapters.htmlparser2);
196
+ ```
197
+
198
+
199
+ #### &bull; Serializer.serialize(node)
200
+ Serializes the given `node`. Returns HTML string.
201
+
202
+ *Example:*
203
+ ```js
204
+ var document = parser.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
205
+
206
+ //Serialize document
207
+ var html = serializer.serialize(document);
208
+
209
+ //Serialize <body> element content
210
+ var bodyInnerHtml = serializer.serialize(document.childNodes[0].childNodes[1]);
211
+ ```
212
+
213
+ ---------------------------------------
214
+
215
+
216
+ ## Testing
217
+ Test data is adopted from [html5lib project](https://github.com/html5lib). Parser is covered by more than 8000 test cases.
218
+ To run tests:
219
+ ```
220
+ $ npm test
221
+ ```
222
+
223
+
224
+ ## Custom tree adapter
225
+ You can create a custom tree adapter so parse5 can work with your own DOM-tree implementation.
226
+ Just pass your adapter implementation to the parser's constructor as an argument:
227
+
228
+ ```js
229
+ var Parser = require('parse5').Parser;
230
+
231
+ var myTreeAdapter = {
232
+ //Adapter methods...
233
+ };
234
+
235
+ //Instantiate parser
236
+ var parser = new Parser(myTreeAdapter);
237
+ ```
238
+
239
+ Sample implementation can be found [here](https://github.com/inikulin/parse5/blob/master/lib/tree_adapters/default.js).
240
+ The custom tree adapter should implement all methods exposed via `exports` in the sample implementation.
241
+
242
+ ## Questions or suggestions?
243
+ If you have any questions, please feel free to create an issue [here on github](https://github.com/inikulin/parse5/issues).
244
+
245
+
246
+ ## Author
247
+ [Ivan Nikulin](https://github.com/inikulin) (ifaaan@gmail.com)