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 +69 -57
- package/LICENSE +1 -1
- package/README.md +247 -246
- package/docs/build/index.md +963 -0
- package/lib/{tree_construction → common}/doctype.js +25 -0
- package/lib/serialization/serializer.js +178 -194
- package/lib/simple_api/simple_api_parser.js +107 -107
- package/lib/simple_api/tokenizer_proxy.js +122 -122
- package/lib/tokenization/location_info_mixin.js +80 -80
- package/lib/tokenization/named_entity_trie.js +1 -54
- package/lib/tokenization/preprocessor.js +115 -115
- package/lib/tree_adapters/htmlparser2.js +4 -12
- package/lib/tree_construction/location_info_mixin.js +197 -166
- package/lib/tree_construction/parser.js +2827 -2827
- package/package.json +47 -46
package/CHANGELOG.md
CHANGED
|
@@ -1,57 +1,69 @@
|
|
|
1
|
-
## 1.
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
* Add:
|
|
6
|
-
|
|
7
|
-
## 1.
|
|
8
|
-
* Fix:
|
|
9
|
-
|
|
10
|
-
## 1.
|
|
11
|
-
* Fix:
|
|
12
|
-
|
|
13
|
-
## 1.
|
|
14
|
-
* Add:
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
* Add:
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
*
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
*
|
|
41
|
-
* Fix:
|
|
42
|
-
|
|
43
|
-
## 1.
|
|
44
|
-
*
|
|
45
|
-
|
|
46
|
-
##
|
|
47
|
-
* Fix:
|
|
48
|
-
|
|
49
|
-
##
|
|
50
|
-
* Add: [
|
|
51
|
-
*
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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-
|
|
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
|
-
[](https://travis-ci.org/inikulin/parse5)
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
* [
|
|
15
|
-
* [
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.github.com/inikulin/parse5/master/logo.png" alt="parse5" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
[](https://travis-ci.org/inikulin/parse5)
|
|
6
|
+
[](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 fucking 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
|
+
#### • TreeAdapters.default
|
|
60
|
+
Default tree format for parse5.
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
#### • 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
|
+
#### • 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 `&`, ` `, 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
|
+
#### • 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
|
+
#### • 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
|
+
#### • 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 `&`, ` `, 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
|
+
#### • 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
|
+
#### • 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
|
+
#### • 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)
|