html-react-parser 0.4.3 → 0.4.7
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 +38 -0
- package/README.md +81 -90
- package/dist/html-react-parser.js +144 -2222
- package/dist/html-react-parser.min.js +1 -1
- package/index.js +5 -10
- package/lib/attributes-to-props.js +53 -59
- package/lib/dom-to-react.js +60 -75
- package/lib/property-config.js +16 -28
- package/lib/utilities.js +39 -38
- package/package.json +23 -11
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,44 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
<a name="0.4.7"></a>
|
|
6
|
+
## [0.4.7](https://github.com/remarkablemark/html-react-parser/compare/v0.4.6...v0.4.7) (2018-09-14)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
<a name="0.4.6"></a>
|
|
11
|
+
## [0.4.6](https://github.com/remarkablemark/html-react-parser/compare/v0.4.5...v0.4.6) (2018-05-13)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Bug Fixes
|
|
15
|
+
|
|
16
|
+
* accidentally left a console ([953e564](https://github.com/remarkablemark/html-react-parser/commit/953e564))
|
|
17
|
+
* added test case for viewBox ([261ffb7](https://github.com/remarkablemark/html-react-parser/commit/261ffb7))
|
|
18
|
+
* moving svg mock to correct place ([6ffb148](https://github.com/remarkablemark/html-react-parser/commit/6ffb148))
|
|
19
|
+
* svg attributes now correctly handled ([94643e1](https://github.com/remarkablemark/html-react-parser/commit/94643e1))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
<a name="0.4.5"></a>
|
|
24
|
+
## [0.4.5](https://github.com/remarkablemark/html-react-parser/compare/v0.4.4...v0.4.5) (2018-05-09)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
### Bug Fixes
|
|
28
|
+
|
|
29
|
+
* **package:** upgrade style-to-object@0.2.1 ([d065c60](https://github.com/remarkablemark/html-react-parser/commit/d065c60))
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
<a name="0.4.4"></a>
|
|
34
|
+
## [0.4.4](https://github.com/remarkablemark/html-react-parser/compare/v0.4.3...v0.4.4) (2018-05-07)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
### Bug Fixes
|
|
38
|
+
|
|
39
|
+
* **package:** upgrade react-dom-core@0.0.3 ([b4a1c6e](https://github.com/remarkablemark/html-react-parser/commit/b4a1c6e))
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
5
43
|
<a name="0.4.3"></a>
|
|
6
44
|
## [0.4.3](https://github.com/remarkablemark/html-react-parser/compare/v0.4.2...v0.4.3) (2018-03-27)
|
|
7
45
|
|
package/README.md
CHANGED
|
@@ -7,15 +7,14 @@
|
|
|
7
7
|
[](https://coveralls.io/github/remarkablemark/html-react-parser?branch=master)
|
|
8
8
|
[](https://david-dm.org/remarkablemark/html-react-parser)
|
|
9
9
|
|
|
10
|
-
An HTML to React parser:
|
|
11
|
-
|
|
10
|
+
An HTML to React parser that works on the server and the browser:
|
|
12
11
|
```
|
|
13
|
-
|
|
12
|
+
HTMLReactParser(htmlString[, options])
|
|
14
13
|
```
|
|
15
14
|
|
|
16
|
-
|
|
15
|
+
It converts an HTML string to [React elements](https://facebook.github.io/react/docs/react-api.html#creating-react-elements).
|
|
17
16
|
|
|
18
|
-
There
|
|
17
|
+
There's also an [option](#options) to [replace](#replacedomnode) elements with your own custom React elements.
|
|
19
18
|
|
|
20
19
|
## Example
|
|
21
20
|
|
|
@@ -25,7 +24,7 @@ Parser('<p>Hello, world!</p>');
|
|
|
25
24
|
// same output as `React.createElement('p', {}, 'Hello, world!')`
|
|
26
25
|
```
|
|
27
26
|
|
|
28
|
-
[JSFiddle](https://jsfiddle.net/remarkablemark/7v86d800/)
|
|
27
|
+
[JSFiddle](https://jsfiddle.net/remarkablemark/7v86d800/) | [repl.it](https://repl.it/@remarkablemark/html-react-parser)
|
|
29
28
|
|
|
30
29
|
## Installation
|
|
31
30
|
|
|
@@ -49,64 +48,55 @@ yarn add html-react-parser
|
|
|
49
48
|
<script src="https://unpkg.com/html-react-parser@latest/dist/html-react-parser.min.js"></script>
|
|
50
49
|
```
|
|
51
50
|
|
|
52
|
-
|
|
51
|
+
More [examples](https://github.com/remarkablemark/html-react-parser/tree/master/examples).
|
|
53
52
|
|
|
54
53
|
## Usage
|
|
55
54
|
|
|
56
|
-
Given
|
|
57
|
-
|
|
55
|
+
Given you have the following imported:
|
|
58
56
|
```js
|
|
59
57
|
// ES Modules
|
|
60
58
|
import Parser from 'html-react-parser';
|
|
61
59
|
import { render } from 'react-dom';
|
|
62
60
|
```
|
|
63
61
|
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
Render a single element:
|
|
66
63
|
```js
|
|
67
64
|
render(
|
|
68
|
-
|
|
69
|
-
|
|
65
|
+
Parser('<h1>single</h1>'),
|
|
66
|
+
document.getElementById('root')
|
|
70
67
|
);
|
|
71
68
|
```
|
|
72
69
|
|
|
73
|
-
|
|
74
|
-
|
|
70
|
+
Render multiple elements:
|
|
75
71
|
```js
|
|
76
72
|
// with JSX
|
|
77
73
|
render(
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
</div>,
|
|
83
|
-
document.getElementById('root')
|
|
74
|
+
// the parser returns an array for adjacent elements
|
|
75
|
+
// so make sure they're nested under a parent React element
|
|
76
|
+
<div>{Parser('<p>brother</p><p>sister</p>')}</div>,
|
|
77
|
+
document.getElementById('root')
|
|
84
78
|
);
|
|
85
79
|
|
|
86
|
-
// without JSX
|
|
80
|
+
// or without JSX
|
|
87
81
|
render(
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
),
|
|
91
|
-
document.getElementById('root')
|
|
82
|
+
React.createElement('div', {}, Parser('<p>brother</p><p>sister</p>')),
|
|
83
|
+
document.getElementById('root')
|
|
92
84
|
);
|
|
93
85
|
```
|
|
94
86
|
|
|
95
|
-
|
|
96
|
-
|
|
87
|
+
Render nested elements:
|
|
97
88
|
```js
|
|
98
89
|
render(
|
|
99
|
-
|
|
100
|
-
|
|
90
|
+
Parser('<ul><li>inside</li></ul>'),
|
|
91
|
+
document.getElementById('root')
|
|
101
92
|
);
|
|
102
93
|
```
|
|
103
94
|
|
|
104
|
-
|
|
105
|
-
|
|
95
|
+
Renders with attributes preserved:
|
|
106
96
|
```js
|
|
107
97
|
render(
|
|
108
|
-
|
|
109
|
-
|
|
98
|
+
Parser('<p id="foo" class="bar baz" data-qux="42">look at me now</p>'),
|
|
99
|
+
document.getElementById('root')
|
|
110
100
|
);
|
|
111
101
|
```
|
|
112
102
|
|
|
@@ -114,94 +104,95 @@ render(
|
|
|
114
104
|
|
|
115
105
|
#### replace(domNode)
|
|
116
106
|
|
|
117
|
-
The `replace` method allows you to swap an element with your own React
|
|
107
|
+
The `replace` method allows you to swap an element with your own React element.
|
|
118
108
|
|
|
119
|
-
The first argument is `domNode
|
|
109
|
+
The first argument is `domNode`--an object with the same output as [htmlparser2](https://github.com/fb55/htmlparser2)'s [domhandler](https://github.com/fb55/domhandler#example).
|
|
120
110
|
|
|
121
|
-
The element is only
|
|
111
|
+
The element is replaced only if a valid React element is returned.
|
|
122
112
|
|
|
123
113
|
```js
|
|
124
|
-
// with JSX
|
|
125
114
|
Parser('<p id="replace">text</p>', {
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
}
|
|
115
|
+
replace: function(domNode) {
|
|
116
|
+
if (domNode.attribs && domNode.attribs.id === 'replace') {
|
|
117
|
+
return React.createElement('span', {}, 'replaced');
|
|
130
118
|
}
|
|
119
|
+
}
|
|
131
120
|
});
|
|
132
121
|
```
|
|
133
122
|
|
|
134
|
-
|
|
135
|
-
|
|
123
|
+
Here's an [example](https://repl.it/@remarkablemark/html-react-parser-replace-example) that replaces but keeps the children:
|
|
136
124
|
```js
|
|
137
125
|
// with ES6 and JSX
|
|
138
|
-
|
|
139
|
-
// converts DOM object to React Elements
|
|
140
126
|
import domToReact from 'html-react-parser/lib/dom-to-react';
|
|
141
127
|
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
</p>
|
|
149
|
-
</div>
|
|
128
|
+
const htmlString = `
|
|
129
|
+
<p id="main">
|
|
130
|
+
<span class="prettify">
|
|
131
|
+
keep me and make me pretty!
|
|
132
|
+
</span>
|
|
133
|
+
</p>
|
|
150
134
|
`;
|
|
151
135
|
|
|
152
136
|
const parserOptions = {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
</span>
|
|
169
|
-
);
|
|
170
|
-
}
|
|
137
|
+
replace: ({ attribs, children }) => {
|
|
138
|
+
if (!attribs) return;
|
|
139
|
+
|
|
140
|
+
if (attribs.id === 'main') {
|
|
141
|
+
return (
|
|
142
|
+
<h1 style={{ fontSize: 42 }}>
|
|
143
|
+
{domToReact(children, parserOptions)}
|
|
144
|
+
</h1>
|
|
145
|
+
);
|
|
146
|
+
} else if (attribs.class === 'prettify') {
|
|
147
|
+
return (
|
|
148
|
+
<span style={{ color: 'hotpink' }}>
|
|
149
|
+
{domToReact(children, parserOptions)}
|
|
150
|
+
</span>
|
|
151
|
+
);
|
|
171
152
|
}
|
|
153
|
+
}
|
|
172
154
|
};
|
|
173
155
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
document.getElementById('root')
|
|
177
|
-
);
|
|
156
|
+
const reactElement = Parser(htmlString, parserOptions);
|
|
157
|
+
ReactDOMServer.renderToStaticMarkup(reactElement);
|
|
178
158
|
```
|
|
179
159
|
|
|
180
|
-
|
|
181
|
-
|
|
160
|
+
[Output](https://repl.it/@remarkablemark/html-react-parser-replace-example):
|
|
182
161
|
```html
|
|
183
|
-
<
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
</span>
|
|
189
|
-
</div>
|
|
162
|
+
<h1 style="font-size:42px">
|
|
163
|
+
<span style="color:hotpink">
|
|
164
|
+
keep me and make me pretty!
|
|
165
|
+
</span>
|
|
166
|
+
</h1>
|
|
190
167
|
```
|
|
191
168
|
|
|
192
169
|
## Testing
|
|
193
170
|
|
|
194
171
|
```sh
|
|
195
|
-
|
|
196
|
-
|
|
172
|
+
npm test
|
|
173
|
+
npm run lint # npm run lint:fix
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
## Benchmarks
|
|
177
|
+
|
|
178
|
+
```sh
|
|
179
|
+
npm run test:benchmark
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Here's an example output of the benchmarks run on a MacBook Pro 2017:
|
|
183
|
+
|
|
184
|
+
```
|
|
185
|
+
html-to-react - Single x 415,186 ops/sec ±0.92% (85 runs sampled)
|
|
186
|
+
html-to-react - Multiple x 139,780 ops/sec ±2.32% (87 runs sampled)
|
|
187
|
+
html-to-react - Complex x 8,118 ops/sec ±2.99% (82 runs sampled)
|
|
197
188
|
```
|
|
198
189
|
|
|
199
190
|
## Release
|
|
200
191
|
|
|
201
192
|
```sh
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
193
|
+
npm run release
|
|
194
|
+
npm publish
|
|
195
|
+
git push --follow-tags
|
|
205
196
|
```
|
|
206
197
|
|
|
207
198
|
## Special Thanks
|