html-react-parser 6.0.0 → 6.1.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 +35 -13
- package/dist/html-react-parser.js +1645 -1273
- 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/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -1
- package/lib/index.js.map +1 -1
- package/lib/types.d.ts +2 -1
- package/lib/types.d.ts.map +1 -1
- package/package.json +22 -22
- package/src/index.ts +6 -4
- package/src/types.ts +2 -1
package/README.md
CHANGED
|
@@ -43,6 +43,7 @@ parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')
|
|
|
43
43
|
- [library](#library)
|
|
44
44
|
- [htmlparser2](#htmlparser2)
|
|
45
45
|
- [trim](#trim)
|
|
46
|
+
- [trustedTypePolicy](#trustedtypepolicy)
|
|
46
47
|
- [Migration](#migration)
|
|
47
48
|
- [v5](#v5)
|
|
48
49
|
- [v4](#v4)
|
|
@@ -208,7 +209,7 @@ parse('<br>', {
|
|
|
208
209
|
|
|
209
210
|
> [!NOTE]
|
|
210
211
|
>
|
|
211
|
-
> The index will restart at 0 when traversing the node's children so don't rely on index being a unique key
|
|
212
|
+
> The index will restart at 0 when traversing the node's children so don't rely on index being a unique key (see [#1259](https://github.com/remarkablemark/html-react-parser/issues/1259#issuecomment-1889574133)).
|
|
212
213
|
|
|
213
214
|
#### replace with TypeScript
|
|
214
215
|
|
|
@@ -244,7 +245,7 @@ If you're having issues, take a look at our [Create React App example](./example
|
|
|
244
245
|
|
|
245
246
|
#### replace element and children
|
|
246
247
|
|
|
247
|
-
Replace the element and its children
|
|
248
|
+
Replace the element and its children:
|
|
248
249
|
|
|
249
250
|
```tsx
|
|
250
251
|
import parse, { domToReact } from 'html-react-parser';
|
|
@@ -335,7 +336,7 @@ parse(html, options);
|
|
|
335
336
|
|
|
336
337
|
#### replace and remove element
|
|
337
338
|
|
|
338
|
-
|
|
339
|
+
Exclude an element from rendering by replacing it with `<React.Fragment>`:
|
|
339
340
|
|
|
340
341
|
```tsx
|
|
341
342
|
parse('<p><br id="remove"></p>', {
|
|
@@ -443,8 +444,29 @@ However, intentional whitespace may be stripped out:
|
|
|
443
444
|
parse('<p> </p>', { trim: true }); // React.createElement('p')
|
|
444
445
|
```
|
|
445
446
|
|
|
447
|
+
### trustedTypePolicy
|
|
448
|
+
|
|
449
|
+
When running in a browser, you can pass a [Trusted Types](https://developer.mozilla.org/docs/Web/API/Trusted_Types_API) policy so the parser calls `trustedTypePolicy.createHTML` before assigning content to `innerHTML`:
|
|
450
|
+
|
|
451
|
+
```ts
|
|
452
|
+
parse('<div>Hello</div>', {
|
|
453
|
+
trustedTypePolicy: window.trustedTypes?.createPolicy('my-policy', {
|
|
454
|
+
createHTML(input) {
|
|
455
|
+
// apply sanitization logic here
|
|
456
|
+
return DOMPurify.sanitize(input);
|
|
457
|
+
},
|
|
458
|
+
}),
|
|
459
|
+
});
|
|
460
|
+
```
|
|
461
|
+
|
|
446
462
|
## Migration
|
|
447
463
|
|
|
464
|
+
### v6
|
|
465
|
+
|
|
466
|
+
Changed build target from `es5` to `es2016`.
|
|
467
|
+
|
|
468
|
+
[html-dom-parser](https://github.com/remarkablemark/html-dom-parser) has been upgraded to [v7](https://github.com/remarkablemark/html-dom-parser/releases/tag/v7.0.0) and [domhandler](https://github.com/fb55/domhandler) has been upgraded to [v6](https://github.com/fb55/domhandler/releases/tag/v6.0.1).
|
|
469
|
+
|
|
448
470
|
### v5
|
|
449
471
|
|
|
450
472
|
Migrated to TypeScript. CommonJS imports require the `.default` key:
|
|
@@ -505,27 +527,27 @@ Since [v1.1.1](https://github.com/remarkablemark/html-react-parser/releases/tag/
|
|
|
505
527
|
|
|
506
528
|
### Is this XSS safe?
|
|
507
529
|
|
|
508
|
-
No, this library is
|
|
530
|
+
No, this library is **not** [XSS (cross-site scripting)](https://wikipedia.org/wiki/Cross-site_scripting) safe (see [#94](https://github.com/remarkablemark/html-react-parser/issues/94)). However, you can mitigate this risk by enforcing a Content Security Policy (CSP) with [Trusted Types](#trustedtypepolicy).
|
|
509
531
|
|
|
510
532
|
### Does invalid HTML get sanitized?
|
|
511
533
|
|
|
512
|
-
No, this library does
|
|
534
|
+
No, this library does **not** sanitize HTML (see [#124](https://github.com/remarkablemark/html-react-parser/issues/124), [#125](https://github.com/remarkablemark/html-react-parser/issues/125), and [#141](https://github.com/remarkablemark/html-react-parser/issues/141)).
|
|
513
535
|
|
|
514
536
|
### Are `<script>` tags parsed?
|
|
515
537
|
|
|
516
|
-
Although `<script>` tags and their contents are rendered on the server-side, they're not evaluated on the client-side
|
|
538
|
+
Although `<script>` tags and their contents are rendered on the server-side, they're not evaluated on the client-side (see [#98](https://github.com/remarkablemark/html-react-parser/issues/98)).
|
|
517
539
|
|
|
518
540
|
### Attributes aren't getting called
|
|
519
541
|
|
|
520
|
-
The reason why your HTML attributes aren't getting called is because [inline event handlers](https://developer.mozilla.org/docs/Web/Guide/Events/Event_handlers) (e.g., `onclick`) are parsed as a _string_ rather than a _function_
|
|
542
|
+
The reason why your HTML attributes aren't getting called is because [inline event handlers](https://developer.mozilla.org/docs/Web/Guide/Events/Event_handlers) (e.g., `onclick`) are parsed as a _string_ rather than a _function_ (see [#73](https://github.com/remarkablemark/html-react-parser/issues/73)).
|
|
521
543
|
|
|
522
544
|
### Parser throws an error
|
|
523
545
|
|
|
524
|
-
If the parser throws an error, check if your arguments are valid
|
|
546
|
+
If the parser throws an error, check if your arguments are valid (see ["Does invalid HTML get sanitized?"](#does-invalid-html-get-sanitized)).
|
|
525
547
|
|
|
526
548
|
### Is SSR supported?
|
|
527
549
|
|
|
528
|
-
Yes, server-side rendering on Node.js is supported by this library
|
|
550
|
+
Yes, server-side rendering on Node.js is supported by this library:
|
|
529
551
|
|
|
530
552
|
### Elements aren't nested correctly
|
|
531
553
|
|
|
@@ -558,15 +580,15 @@ parse('<CustomElement>', options); // React.createElement('CustomElement')
|
|
|
558
580
|
> Warning: <CustomElement> is using incorrect casing. Use PascalCase for React components, or lowercase for HTML elements.
|
|
559
581
|
> ```
|
|
560
582
|
|
|
561
|
-
See [#62](https://github.com/remarkablemark/html-react-parser/issues/62)
|
|
583
|
+
See [#62](https://github.com/remarkablemark/html-react-parser/issues/62).
|
|
562
584
|
|
|
563
585
|
### TS Error: Property 'attribs' does not exist on type 'DOMNode'
|
|
564
586
|
|
|
565
|
-
The TypeScript error occurs because `DOMNode` needs to be an instance of domhandler's `Element
|
|
587
|
+
The TypeScript error occurs because `DOMNode` needs to be an instance of domhandler's `Element` (see [migration](#migration) or [#199](https://github.com/remarkablemark/html-react-parser/issues/199)).
|
|
566
588
|
|
|
567
589
|
### Can I enable `trim` for certain elements?
|
|
568
590
|
|
|
569
|
-
Yes, you can enable or disable [`trim`](#trim) for certain elements using the [`replace`](#replace) option
|
|
591
|
+
Yes, you can enable or disable [`trim`](#trim) for certain elements using the [`replace`](#replace) option (see [#205](https://github.com/remarkablemark/html-react-parser/issues/205)).
|
|
570
592
|
|
|
571
593
|
### Webpack build warnings
|
|
572
594
|
|
|
@@ -601,7 +623,7 @@ node_modules/htmlparser2/lib/index.d.ts:2:23 - error TS1005: ',' expected.
|
|
|
601
623
|
~~~~~~~~~~~~~
|
|
602
624
|
```
|
|
603
625
|
|
|
604
|
-
Then upgrade to the latest version of [typescript](https://www.npmjs.com/package/typescript)
|
|
626
|
+
Then upgrade to the latest version of [typescript](https://www.npmjs.com/package/typescript) (see [#748](https://github.com/remarkablemark/html-react-parser/issues/748)).
|
|
605
627
|
|
|
606
628
|
## Performance
|
|
607
629
|
|