Sanitize HTML using clean()
masterAmmonia is a whitelist-based HTML sanitization library designed to prevent XSS, layout breaking, and clickjacking. It uses html5ever to parse and serialize document fragments according to the HTML5 specification, making it resilient to syntactic obfuscation.
Important Note on Parsing: Ammonia parses input strictly according to the HTML5 spec. It will not perform automatic transformations like linkifying URLs, inserting line breaks, or converting entities like (C) to ©. If you require these features, use a markup processor (like pulldown-cmark) to generate HTML before passing the result to Ammonia's clean() function.
use ammonia::clean;
let unsafe_html = "<script>alert('xss')</script><p>Hello</p>";
let safe_html = clean(unsafe_html);
// safe_html will contain only the allowed elements, e.g., "<p>Hello</p>"