/// Sanitization preset for ammonia HTML cleaning. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum SanitizePreset { /// Default ammonia settings. Allows most safe HTML. Permissive, /// Default ammonia settings (same sanitization as Permissive; the difference /// is at the Renderer level: Standard strips images, Permissive doesn't). Standard, /// Adds `rel="noopener noreferrer nofollow"` to all links. Strict, /// Only allows p, em, strong, code, br, pre tags. Minimal, } impl SanitizePreset { pub(crate) fn clean(&self, html: &str) -> String { match self { SanitizePreset::Permissive | SanitizePreset::Standard => ammonia::clean(html), SanitizePreset::Strict => ammonia::Builder::default() .link_rel(Some("noopener noreferrer nofollow")) .clean(html) .to_string(), SanitizePreset::Minimal => { let tags: std::collections::HashSet<&str> = ["p", "em", "strong", "code", "br", "pre"] .iter() .copied() .collect(); ammonia::Builder::default() .tags(tags) .clean(html) .to_string() } } } } #[cfg(test)] mod tests { use super::*; #[test] fn permissive_allows_safe_html() { let html = "

Hello world

"; let result = SanitizePreset::Permissive.clean(html); assert!(result.contains("world")); } #[test] fn permissive_strips_script() { let html = "

Hello

"; let result = SanitizePreset::Permissive.clean(html); assert!(!result.contains("