Provides the DOMPurify XSS sanitizer as the Drupal library dompurify/dompurify.
Modules and themes add it as a library dependency instead of each shipping their own copy, so DOMPurify loads once per page.
Usage
Declare the library as a dependency in your *.libraries.yml:
my-library:
js:
js/my_library.js: {}
dependencies:
- core/drupal
- core/once
- dompurify/dompurify
Sanitize untrusted HTML before inserting it into the page:
(function (Drupal, once, DOMPurify) {
Drupal.behaviors.myLibrary = {
attach(context) {
once('my-library', '[data-untrusted-html]', context).forEach((element) => {
element.innerHTML = DOMPurify.sanitize(element.dataset.untrustedHtml, {
USE_PROFILES: { html: true },
});
});
},
};
})(Drupal, once, DOMPurify);
Library source
By default DOMPurify 3.5.0 loads from the jsDelivr CDN with a Subresource Integrity hash. If libraries/dompurify/dist/purify.min.js exists, the module serves that local copy instead. See README.md to install it with Composer through Asset Packagist.
Documentation
Ask your assistant about DOMPurify
Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works
Depends on
Dependencies of the latest stable release
No dependencies recorded for this project.
Required by
Tracked projects that depend on this one
No tracked projects depend on this one yet.