#[cfg(feature = "sanitize")]
pub(crate) fn sanitize_html(html: &str) -> String {
ammonia::Builder::default()
.add_generic_attributes(["id"])
.clean(html)
.to_string()
}
#[cfg(not(feature = "sanitize"))]
pub(crate) fn sanitize_html(html: &str) -> String {
html.to_string()
}
#[cfg(all(test, feature = "sanitize"))]
mod tests {
use super::*;
#[test]
fn strips_script_tags() {
let dirty = "<p>hi</p><script>alert(1)</script>";
assert_eq!(sanitize_html(dirty), "<p>hi</p>");
}
#[test]
fn strips_event_handler_attributes() {
let dirty = r#"<img src="x" onerror="alert(1)">"#;
let clean = sanitize_html(dirty);
assert!(!clean.contains("onerror"));
}
#[test]
fn preserves_id_attribute_on_headings() {
let html = r#"<h2 id="getting-started">Getting Started</h2>"#;
assert_eq!(sanitize_html(html), html);
}
}