html-react-parser 4.2.10 → 5.0.0
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/README.md +79 -55
- package/dist/html-react-parser.js +491 -514
- package/dist/html-react-parser.js.map +1 -1
- package/dist/html-react-parser.min.js +1 -1
- package/dist/html-react-parser.min.js.map +1 -1
- package/esm/attributes-to-props.mjs +3 -0
- package/esm/dom-to-react.mjs +3 -0
- package/esm/index.mjs +13 -0
- package/esm/utilities.mjs +8 -0
- package/lib/attributes-to-props.d.ts +8 -11
- package/lib/attributes-to-props.d.ts.map +1 -0
- package/lib/attributes-to-props.js +58 -75
- package/lib/attributes-to-props.js.map +1 -0
- package/lib/dom-to-react.d.ts +7 -13
- package/lib/dom-to-react.d.ts.map +1 -0
- package/lib/dom-to-react.js +104 -123
- package/lib/dom-to-react.js.map +1 -0
- package/lib/index.d.ts +17 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +36 -0
- package/lib/index.js.map +1 -0
- package/lib/types.d.ts +17 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/types.js +3 -0
- package/lib/types.js.map +1 -0
- package/lib/utilities.d.ts +42 -0
- package/lib/utilities.d.ts.map +1 -0
- package/lib/utilities.js +77 -79
- package/lib/utilities.js.map +1 -0
- package/package.json +27 -21
- package/src/attributes-to-props.ts +108 -0
- package/src/dom-to-react.ts +156 -0
- package/src/index.ts +37 -0
- package/src/types.ts +29 -0
- package/src/utilities.ts +108 -0
- package/index.d.ts +0 -60
- package/index.js +0 -50
- package/index.mjs +0 -13
package/README.md
CHANGED
|
@@ -21,8 +21,9 @@ To replace an element with another element, check out the [`replace`](#replace)
|
|
|
21
21
|
|
|
22
22
|
#### Example
|
|
23
23
|
|
|
24
|
-
```
|
|
25
|
-
|
|
24
|
+
```ts
|
|
25
|
+
import parse from 'html-react-parser';
|
|
26
|
+
|
|
26
27
|
parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')
|
|
27
28
|
```
|
|
28
29
|
|
|
@@ -43,6 +44,7 @@ parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')
|
|
|
43
44
|
- [htmlparser2](#htmlparser2)
|
|
44
45
|
- [trim](#trim)
|
|
45
46
|
- [Migration](#migration)
|
|
47
|
+
- [v5](#v5)
|
|
46
48
|
- [v4](#v4)
|
|
47
49
|
- [v3](#v3)
|
|
48
50
|
- [v2](#v2)
|
|
@@ -100,31 +102,31 @@ yarn add html-react-parser
|
|
|
100
102
|
|
|
101
103
|
Import ES module:
|
|
102
104
|
|
|
103
|
-
```
|
|
105
|
+
```ts
|
|
104
106
|
import parse from 'html-react-parser';
|
|
105
107
|
```
|
|
106
108
|
|
|
107
109
|
Or require CommonJS module:
|
|
108
110
|
|
|
109
|
-
```
|
|
110
|
-
const parse = require('html-react-parser');
|
|
111
|
+
```ts
|
|
112
|
+
const parse = require('html-react-parser').default;
|
|
111
113
|
```
|
|
112
114
|
|
|
113
115
|
Parse single element:
|
|
114
116
|
|
|
115
|
-
```
|
|
117
|
+
```ts
|
|
116
118
|
parse('<h1>single</h1>');
|
|
117
119
|
```
|
|
118
120
|
|
|
119
121
|
Parse multiple elements:
|
|
120
122
|
|
|
121
|
-
```
|
|
123
|
+
```ts
|
|
122
124
|
parse('<li>Item 1</li><li>Item 2</li>');
|
|
123
125
|
```
|
|
124
126
|
|
|
125
127
|
Make sure to render parsed adjacent elements under a parent element:
|
|
126
128
|
|
|
127
|
-
```
|
|
129
|
+
```tsx
|
|
128
130
|
<ul>
|
|
129
131
|
{parse(`
|
|
130
132
|
<li>Item 1</li>
|
|
@@ -135,15 +137,15 @@ Make sure to render parsed adjacent elements under a parent element:
|
|
|
135
137
|
|
|
136
138
|
Parse nested elements:
|
|
137
139
|
|
|
138
|
-
```
|
|
140
|
+
```ts
|
|
139
141
|
parse('<body><p>Lorem ipsum</p></body>');
|
|
140
142
|
```
|
|
141
143
|
|
|
142
144
|
Parse element with attributes:
|
|
143
145
|
|
|
144
|
-
```
|
|
146
|
+
```ts
|
|
145
147
|
parse(
|
|
146
|
-
'<hr id="foo" class="bar" data-attr="baz" custom="qux" style="top:42px;">'
|
|
148
|
+
'<hr id="foo" class="bar" data-attr="baz" custom="qux" style="top:42px;">',
|
|
147
149
|
);
|
|
148
150
|
```
|
|
149
151
|
|
|
@@ -153,11 +155,11 @@ The `replace` option allows you to replace an element with another element.
|
|
|
153
155
|
|
|
154
156
|
The `replace` callback's first argument is [domhandler](https://github.com/fb55/domhandler#example)'s node:
|
|
155
157
|
|
|
156
|
-
```
|
|
158
|
+
```ts
|
|
157
159
|
parse('<br>', {
|
|
158
|
-
replace
|
|
160
|
+
replace(domNode) {
|
|
159
161
|
console.dir(domNode, { depth: null });
|
|
160
|
-
}
|
|
162
|
+
},
|
|
161
163
|
});
|
|
162
164
|
```
|
|
163
165
|
|
|
@@ -165,7 +167,7 @@ parse('<br>', {
|
|
|
165
167
|
<summary>Console output</summary>
|
|
166
168
|
<p>
|
|
167
169
|
|
|
168
|
-
```
|
|
170
|
+
```ts
|
|
169
171
|
Element {
|
|
170
172
|
type: 'tag',
|
|
171
173
|
parent: null,
|
|
@@ -184,29 +186,43 @@ Element {
|
|
|
184
186
|
|
|
185
187
|
The element is replaced if a **valid** React element is returned:
|
|
186
188
|
|
|
187
|
-
```
|
|
189
|
+
```tsx
|
|
188
190
|
parse('<p id="replace">text</p>', {
|
|
189
|
-
replace
|
|
191
|
+
replace(domNode) {
|
|
190
192
|
if (domNode.attribs && domNode.attribs.id === 'replace') {
|
|
191
193
|
return <span>replaced</span>;
|
|
192
194
|
}
|
|
193
|
-
}
|
|
195
|
+
},
|
|
194
196
|
});
|
|
195
197
|
```
|
|
196
198
|
|
|
197
199
|
#### replace with TypeScript
|
|
198
200
|
|
|
199
|
-
For TypeScript
|
|
201
|
+
For TypeScript, you'll need to check that `domNode` is an instance of domhandler's `Element`:
|
|
200
202
|
|
|
201
203
|
```tsx
|
|
202
204
|
import { HTMLReactParserOptions, Element } from 'html-react-parser';
|
|
203
205
|
|
|
204
206
|
const options: HTMLReactParserOptions = {
|
|
205
|
-
replace
|
|
207
|
+
replace(domNode) {
|
|
206
208
|
if (domNode instanceof Element && domNode.attribs) {
|
|
207
209
|
// ...
|
|
208
210
|
}
|
|
209
|
-
}
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
Or use a type assertion:
|
|
216
|
+
|
|
217
|
+
```tsx
|
|
218
|
+
import { HTMLReactParserOptions, Element } from 'html-react-parser';
|
|
219
|
+
|
|
220
|
+
const options: HTMLReactParserOptions = {
|
|
221
|
+
replace(domNode) {
|
|
222
|
+
if ((domNode as Element).attribs) {
|
|
223
|
+
// ...
|
|
224
|
+
}
|
|
225
|
+
},
|
|
210
226
|
};
|
|
211
227
|
```
|
|
212
228
|
|
|
@@ -216,7 +232,7 @@ If you're having issues take a look at our [Create React App example](./examples
|
|
|
216
232
|
|
|
217
233
|
Replace the element and its children (see [demo](https://replit.com/@remarkablemark/html-react-parser-replace-example)):
|
|
218
234
|
|
|
219
|
-
```
|
|
235
|
+
```tsx
|
|
220
236
|
import parse, { domToReact } from 'html-react-parser';
|
|
221
237
|
|
|
222
238
|
const html = `
|
|
@@ -228,7 +244,7 @@ const html = `
|
|
|
228
244
|
`;
|
|
229
245
|
|
|
230
246
|
const options = {
|
|
231
|
-
replace
|
|
247
|
+
replace({ attribs, children }) {
|
|
232
248
|
if (!attribs) {
|
|
233
249
|
return;
|
|
234
250
|
}
|
|
@@ -244,7 +260,7 @@ const options = {
|
|
|
244
260
|
</span>
|
|
245
261
|
);
|
|
246
262
|
}
|
|
247
|
-
}
|
|
263
|
+
},
|
|
248
264
|
};
|
|
249
265
|
|
|
250
266
|
parse(html, options);
|
|
@@ -273,7 +289,7 @@ parse(html, options);
|
|
|
273
289
|
|
|
274
290
|
Convert DOM attributes to React props with `attributesToProps`:
|
|
275
291
|
|
|
276
|
-
```
|
|
292
|
+
```tsx
|
|
277
293
|
import parse, { attributesToProps } from 'html-react-parser';
|
|
278
294
|
|
|
279
295
|
const html = `
|
|
@@ -281,12 +297,12 @@ const html = `
|
|
|
281
297
|
`;
|
|
282
298
|
|
|
283
299
|
const options = {
|
|
284
|
-
replace
|
|
300
|
+
replace(domNode) {
|
|
285
301
|
if (domNode.attribs && domNode.name === 'main') {
|
|
286
302
|
const props = attributesToProps(domNode.attribs);
|
|
287
303
|
return <div {...props} />;
|
|
288
304
|
}
|
|
289
|
-
}
|
|
305
|
+
},
|
|
290
306
|
};
|
|
291
307
|
|
|
292
308
|
parse(html, options);
|
|
@@ -307,9 +323,9 @@ parse(html, options);
|
|
|
307
323
|
|
|
308
324
|
[Exclude](https://replit.com/@remarkablemark/html-react-parser-56) an element from rendering by replacing it with `<React.Fragment>`:
|
|
309
325
|
|
|
310
|
-
```
|
|
326
|
+
```tsx
|
|
311
327
|
parse('<p><br id="remove"></p>', {
|
|
312
|
-
replace: ({ attribs }) => attribs
|
|
328
|
+
replace: ({ attribs }) => attribs?.id === 'remove' && <></>,
|
|
313
329
|
});
|
|
314
330
|
```
|
|
315
331
|
|
|
@@ -330,12 +346,12 @@ The `transform` option allows you to transform each element individually after i
|
|
|
330
346
|
|
|
331
347
|
The `transform` callback's first argument is the React element:
|
|
332
348
|
|
|
333
|
-
```
|
|
349
|
+
```tsx
|
|
334
350
|
parse('<br>', {
|
|
335
|
-
transform
|
|
351
|
+
transform(reactNode, domNode, index) {
|
|
336
352
|
// this will wrap every element in a div
|
|
337
353
|
return <div>{reactNode}</div>;
|
|
338
|
-
}
|
|
354
|
+
},
|
|
339
355
|
});
|
|
340
356
|
```
|
|
341
357
|
|
|
@@ -345,15 +361,15 @@ The `library` option specifies the UI library. The default library is **React**.
|
|
|
345
361
|
|
|
346
362
|
To use Preact:
|
|
347
363
|
|
|
348
|
-
```
|
|
364
|
+
```ts
|
|
349
365
|
parse('<br>', {
|
|
350
|
-
library: require('preact')
|
|
366
|
+
library: require('preact'),
|
|
351
367
|
});
|
|
352
368
|
```
|
|
353
369
|
|
|
354
370
|
Or a custom library:
|
|
355
371
|
|
|
356
|
-
```
|
|
372
|
+
```ts
|
|
357
373
|
parse('<br>', {
|
|
358
374
|
library: {
|
|
359
375
|
cloneElement: () => {
|
|
@@ -364,8 +380,8 @@ parse('<br>', {
|
|
|
364
380
|
},
|
|
365
381
|
isValidElement: () => {
|
|
366
382
|
/* ... */
|
|
367
|
-
}
|
|
368
|
-
}
|
|
383
|
+
},
|
|
384
|
+
},
|
|
369
385
|
});
|
|
370
386
|
```
|
|
371
387
|
|
|
@@ -377,11 +393,11 @@ Default [htmlparser2 options](https://github.com/fb55/htmlparser2/wiki/Parser-op
|
|
|
377
393
|
|
|
378
394
|
To enable [`xmlMode`](https://github.com/fb55/htmlparser2/wiki/Parser-options#option-xmlmode):
|
|
379
395
|
|
|
380
|
-
```
|
|
396
|
+
```ts
|
|
381
397
|
parse('<p /><p />', {
|
|
382
398
|
htmlparser2: {
|
|
383
|
-
xmlMode: true
|
|
384
|
-
}
|
|
399
|
+
xmlMode: true,
|
|
400
|
+
},
|
|
385
401
|
});
|
|
386
402
|
```
|
|
387
403
|
|
|
@@ -389,30 +405,38 @@ parse('<p /><p />', {
|
|
|
389
405
|
|
|
390
406
|
By default, whitespace is preserved:
|
|
391
407
|
|
|
392
|
-
```
|
|
408
|
+
```ts
|
|
393
409
|
parse('<br>\n'); // [React.createElement('br'), '\n']
|
|
394
410
|
```
|
|
395
411
|
|
|
396
412
|
But certain elements like `<table>` will strip out invalid whitespace:
|
|
397
413
|
|
|
398
|
-
```
|
|
414
|
+
```ts
|
|
399
415
|
parse('<table>\n</table>'); // React.createElement('table')
|
|
400
416
|
```
|
|
401
417
|
|
|
402
418
|
To remove whitespace, enable the `trim` option:
|
|
403
419
|
|
|
404
|
-
```
|
|
420
|
+
```ts
|
|
405
421
|
parse('<br>\n', { trim: true }); // React.createElement('br')
|
|
406
422
|
```
|
|
407
423
|
|
|
408
424
|
However, intentional whitespace may be stripped out:
|
|
409
425
|
|
|
410
|
-
```
|
|
426
|
+
```ts
|
|
411
427
|
parse('<p> </p>', { trim: true }); // React.createElement('p')
|
|
412
428
|
```
|
|
413
429
|
|
|
414
430
|
## Migration
|
|
415
431
|
|
|
432
|
+
### v5
|
|
433
|
+
|
|
434
|
+
Migrated to TypeScript. CommonJS imports require the `.default` key:
|
|
435
|
+
|
|
436
|
+
```ts
|
|
437
|
+
const parse = require('html-react-parser').default;
|
|
438
|
+
```
|
|
439
|
+
|
|
416
440
|
### v4
|
|
417
441
|
|
|
418
442
|
[htmlparser2](https://github.com/fb55/htmlparser2) has been upgraded to [v9](https://github.com/fb55/htmlparser2/releases/tag/v9.0.0).
|
|
@@ -437,11 +461,11 @@ For the `replace` option, you may need to do the following:
|
|
|
437
461
|
import { Element } from 'domhandler/lib/node';
|
|
438
462
|
|
|
439
463
|
parse('<br class="remove">', {
|
|
440
|
-
replace
|
|
464
|
+
replace(domNode) {
|
|
441
465
|
if (domNode instanceof Element && domNode.attribs.class === 'remove') {
|
|
442
466
|
return <></>;
|
|
443
467
|
}
|
|
444
|
-
}
|
|
468
|
+
},
|
|
445
469
|
});
|
|
446
470
|
```
|
|
447
471
|
|
|
@@ -490,8 +514,8 @@ Tags are lowercased by default. To prevent that from happening, pass the [htmlpa
|
|
|
490
514
|
```js
|
|
491
515
|
const options = {
|
|
492
516
|
htmlparser2: {
|
|
493
|
-
lowerCaseTags: false
|
|
494
|
-
}
|
|
517
|
+
lowerCaseTags: false,
|
|
518
|
+
},
|
|
495
519
|
};
|
|
496
520
|
parse('<CustomElement>', options); // React.createElement('CustomElement')
|
|
497
521
|
```
|
|
@@ -527,8 +551,8 @@ Then update your Webpack config to:
|
|
|
527
551
|
module.exports = {
|
|
528
552
|
// ...
|
|
529
553
|
resolve: {
|
|
530
|
-
mainFields: ['browser', 'main', 'module']
|
|
531
|
-
}
|
|
554
|
+
mainFields: ['browser', 'main', 'module'],
|
|
555
|
+
},
|
|
532
556
|
};
|
|
533
557
|
```
|
|
534
558
|
|
|
@@ -552,15 +576,15 @@ Then upgrade to the latest version of [typescript](https://www.npmjs.com/package
|
|
|
552
576
|
Run benchmark:
|
|
553
577
|
|
|
554
578
|
```sh
|
|
555
|
-
npm run
|
|
579
|
+
npm run benchmark
|
|
556
580
|
```
|
|
557
581
|
|
|
558
|
-
Output of benchmark run on MacBook Pro
|
|
582
|
+
Output of benchmark run on MacBook Pro 2021:
|
|
559
583
|
|
|
560
584
|
```
|
|
561
|
-
html-to-react - Single x
|
|
562
|
-
html-to-react - Multiple x
|
|
563
|
-
html-to-react - Complex x
|
|
585
|
+
html-to-react - Single x 1,018,239 ops/sec ±0.43% (94 runs sampled)
|
|
586
|
+
html-to-react - Multiple x 380,037 ops/sec ±0.61% (97 runs sampled)
|
|
587
|
+
html-to-react - Complex x 35,091 ops/sec ±0.50% (96 runs sampled)
|
|
564
588
|
```
|
|
565
589
|
|
|
566
590
|
Run [Size Limit](https://github.com/ai/size-limit):
|