• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# parse5
2
3> **NOTE:** By default all functions operate with [tree format](tree-adapter/default/interface-list.md) produced
4> by the default tree adapter. Tree format can be changed by providing custom [tree adapter](tree-adapter/interface.md) implementation.
5
6### Functions
7
8* [parse](#parse)
9* [parseFragment](#parsefragment)
10* [serialize](#serialize)
11
12<a id="parse"></a>
13
14### parse
15
16▸ **parse**(html: _`string`_, options?: _[ParserOptions](options/parser-options.md)_): Document
17
18Parses an HTML string.
19
20_**example**_:
21
22```js
23const parse5 = require('parse5');
24
25const document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
26
27console.log(document.childNodes[1].tagName); //> 'html'
28```
29
30**Parameters:**
31
32| Param              | Type                                                                                                           | Description        |
33| ------------------ | -------------------------------------------------------------------------------------------------------------- | ------------------ |
34| html               | `string`                                                                                                       | Input HTML string. |
35| `Optional` options | [ParserOptions](options/parser-options.md) | Parsing options.   |
36
37**Returns:** Document
38
39---
40
41<a id="parsefragment"></a>
42
43### parseFragment
44
45▸ **parseFragment**(fragmentContext: _Element_, html: _`string`_, options?: _[ParserOptions](options/parser-options.md)_): DocumentFragment
46
47▸ **parseFragment**(html: _`string`_, options?: _[ParserOptions](options/parser-options.md)_): DocumentFragment
48
49Parses an HTML fragment.
50
51_**example**_:
52
53```js
54const parse5 = require('parse5');
55
56const documentFragment = parse5.parseFragment('<table></table>');
57
58console.log(documentFragment.childNodes[0].tagName); //> 'table'
59
60// Parses the html fragment in the context of the parsed <table> element.
61const trFragment = parser.parseFragment(documentFragment.childNodes[0], '<tr><td>Shake it, baby</td></tr>');
62
63console.log(trFragment.childNodes[0].childNodes[0].tagName); //> 'td'
64```
65
66**Parameters:**
67
68| Param                      | Type                                                                                                           | Description                                                                                                                            |
69| -------------------------- | -------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
70| `Optional` fragmentContext | Element                                                                                                        | Parsing context element. If specified, given fragment will be parsed as if it was set to the context element's \`innerHTML\` property. |
71| html                       | `string`                                                                                                       | Input HTML fragment string.                                                                                                            |
72| `Optional` options         | [ParserOptions](options/parser-options.md) | Parsing options.                                                                                                                       |
73
74**Returns:** DocumentFragment
75
76---
77
78<a id="serialize"></a>
79
80### serialize
81
82▸ **serialize**(node: _Node_, options?: _[SerializerOptions](options/serializer-options.md)_): `string`
83
84Serializes an AST node to an HTML string.
85
86_**example**_:
87
88```js
89const parse5 = require('parse5');
90
91const document = parse5.parse('<!DOCTYPE html><html><head></head><body>Hi there!</body></html>');
92
93// Serializes a document.
94const html = parse5.serialize(document);
95
96// Serializes the <html> element content.
97const str = parse5.serialize(document.childNodes[1]);
98
99console.log(str); //> '<head></head><body>Hi there!</body>'
100```
101
102**Parameters:**
103
104| Param              | Type                                                                                                                   | Description            |
105| ------------------ | ---------------------------------------------------------------------------------------------------------------------- | ---------------------- |
106| node               | Node                                                                                                                   | Node to serialize.     |
107| `Optional` options | [SerializerOptions](options/serializer-options.md) | Serialization options. |
108
109**Returns:** `string`
110
111---
112