Keep "pre" and "code" tags when cleaning post content

This commit is contained in:
silverpill 2022-01-28 00:23:23 +00:00
parent fd0c7edb59
commit c4a0f12555

View file

@ -6,6 +6,8 @@ pub fn clean_html(unsafe_html: &str) -> String {
let mut allowed_tags = HashSet::new();
allowed_tags.insert("a");
allowed_tags.insert("br");
allowed_tags.insert("pre");
allowed_tags.insert("code");
let safe_html = Builder::default()
.tags(allowed_tags)
@ -20,8 +22,8 @@ mod tests {
#[test]
fn test_clean_html() {
let unsafe_html = r#"<p>test <b>bold</b><script>dangerous</script> with <a href="https://example.com">link</a></p>"#;
let unsafe_html = r#"<p>test <b>bold</b><script>dangerous</script> with <a href="https://example.com">link</a> and <code>code</code></p>"#;
let safe_html = clean_html(unsafe_html);
assert_eq!(safe_html, r#"test bold with <a href="https://example.com" rel="noopener noreferrer">link</a>"#);
assert_eq!(safe_html, r#"test bold with <a href="https://example.com" rel="noopener noreferrer">link</a> and <code>code</code>"#);
}
}