Skip to content

Commit d9c0d8c

Browse files
docs(readme): document option trustedTypePolicy
1 parent e660afb commit d9c0d8c

2 files changed

Lines changed: 18 additions & 2 deletions

File tree

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ parse('<p>Hello, World!</p>'); // React.createElement('p', {}, 'Hello, World!')
4343
- [library](#library)
4444
- [htmlparser2](#htmlparser2)
4545
- [trim](#trim)
46+
- [trustedTypePolicy](#trustedtypepolicy)
4647
- [Migration](#migration)
4748
- [v5](#v5)
4849
- [v4](#v4)
@@ -443,6 +444,21 @@ However, intentional whitespace may be stripped out:
443444
parse('<p> </p>', { trim: true }); // React.createElement('p')
444445
```
445446

447+
### trustedTypePolicy
448+
449+
When running in the browser, you can pass a [Trusted Types](https://developer.mozilla.org/docs/Web/API/Trusted_Types_API) policy. The parser uses `trustedTypePolicy.createHTML` right before assigning 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+
446462
## Migration
447463

448464
### v6

examples/webpack/src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const root = createRoot(document.getElementById('root'));
66
const trustedHtml =
77
window.trustedTypes && window.trustedTypes.createPolicy
88
? window.trustedTypes.createPolicy('csp-react-html', {
9-
createHTML: function (string) {
10-
return string;
9+
createHTML: function (input) {
10+
return input;
1111
},
1212
})
1313
: null;

0 commit comments

Comments
 (0)