events: Move fragment stringification out of HtmlSanitizer::clean

This commit is contained in:
Jonas Platte 2022-11-03 13:42:36 +01:00
parent 69c807bdc1
commit ae26730e29
No known key found for this signature in database
GPG Key ID: AAA7A61F696C3E0C
2 changed files with 15 additions and 15 deletions

View File

@ -23,7 +23,7 @@ pub fn sanitize_html(
remove_reply_fallback: RemoveReplyFallback, remove_reply_fallback: RemoveReplyFallback,
) -> String { ) -> String {
let sanitizer = HtmlSanitizer::new(mode, remove_reply_fallback); let sanitizer = HtmlSanitizer::new(mode, remove_reply_fallback);
sanitizer.clean(s) sanitizer.clean(s).to_string()
} }
/// What HTML [tags and attributes] should be kept by the sanitizer. /// What HTML [tags and attributes] should be kept by the sanitizer.
@ -64,7 +64,7 @@ pub enum RemoveReplyFallback {
#[cfg(feature = "unstable-sanitize")] #[cfg(feature = "unstable-sanitize")]
pub fn remove_html_reply_fallback(s: &str) -> String { pub fn remove_html_reply_fallback(s: &str) -> String {
let sanitizer = HtmlSanitizer::reply_fallback_remover(); let sanitizer = HtmlSanitizer::reply_fallback_remover();
sanitizer.clean(s) sanitizer.clean(s).to_string()
} }
/// Remove the [rich reply fallback] of the given plain text string. /// Remove the [rich reply fallback] of the given plain text string.

View File

@ -58,7 +58,7 @@ impl HtmlSanitizer {
} }
/// Clean the given HTML string with this sanitizer. /// Clean the given HTML string with this sanitizer.
pub fn clean(&self, html: &str) -> String { pub fn clean(&self, html: &str) -> Fragment {
let mut fragment = Fragment::parse_html(html); let mut fragment = Fragment::parse_html(html);
let root = fragment.nodes[0].first_child.unwrap(); let root = fragment.nodes[0].first_child.unwrap();
@ -68,7 +68,7 @@ impl HtmlSanitizer {
self.clean_node(&mut fragment, child, 0); self.clean_node(&mut fragment, child, 0);
} }
fragment.to_string() fragment
} }
fn clean_node(&self, fragment: &mut Fragment, node_id: usize, depth: u32) { fn clean_node(&self, fragment: &mut Fragment, node_id: usize, depth: u32) {
@ -313,7 +313,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
sanitized, sanitized.to_string(),
"\ "\
<ul><li>This</li><li>has</li><li>no</li><li>tag</li></ul>\ <ul><li>This</li><li>has</li><li>no</li><li>tag</li></ul>\
<p>This is a paragraph <span data-mx-color=\"green\">with some color</span></p>\ <p>This is a paragraph <span data-mx-color=\"green\">with some color</span></p>\
@ -342,7 +342,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
sanitized, sanitized.to_string(),
"\ "\
<mx-reply>\ <mx-reply>\
<blockquote>\ <blockquote>\
@ -377,7 +377,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
sanitized, sanitized.to_string(),
"\ "\
This has no tag\ This has no tag\
<p>But this is inside a tag</p>\ <p>But this is inside a tag</p>\
@ -404,7 +404,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
sanitized, sanitized.to_string(),
"\ "\
<keep-me>This keeps its tag</keep-me>\ <keep-me>This keeps its tag</keep-me>\
<p>But this is inside a tag</p>\ <p>But this is inside a tag</p>\
@ -423,7 +423,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
sanitized, sanitized.to_string(),
"\ "\
<h1>Title for important stuff</h1>\ <h1>Title for important stuff</h1>\
<p>Look at <font color=\"blue\">me!</font></p>\ <p>Look at <font color=\"blue\">me!</font></p>\
@ -442,7 +442,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
sanitized, sanitized.to_string(),
"\ "\
<p>Look at that picture:</p>\ <p>Look at that picture:</p>\
" "
@ -459,7 +459,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
sanitized, sanitized.to_string(),
"\ "\
<p>Go see my local website</p>\ <p>Go see my local website</p>\
" "
@ -476,7 +476,7 @@ mod tests {
", ",
); );
assert_eq!( assert_eq!(
sanitized, sanitized.to_string(),
"\ "\
<p>Join my room</p>\ <p>Join my room</p>\
<p>To talk about <a href=\"https://mycat.org\">my cat</a></p>\ <p>To talk about <a href=\"https://mycat.org\">my cat</a></p>\
@ -491,7 +491,7 @@ mod tests {
", ",
); );
assert_eq!( assert_eq!(
sanitized, sanitized.to_string(),
"\ "\
<p>Join <a href=\"matrix:r/myroom:notareal.hs\">my room</a></p>\ <p>Join <a href=\"matrix:r/myroom:notareal.hs\">my room</a></p>\
<p>To talk about <a href=\"https://mycat.org\">my cat</a></p>\ <p>To talk about <a href=\"https://mycat.org\">my cat</a></p>\
@ -512,7 +512,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
sanitized, sanitized.to_string(),
"\ "\
<pre><code class=\"language-rust\"> <pre><code class=\"language-rust\">
type StringList = Vec&lt;String&gt;; type StringList = Vec&lt;String&gt;;
@ -534,7 +534,7 @@ mod tests {
.chain(std::iter::repeat("</div>").take(100)) .chain(std::iter::repeat("</div>").take(100))
.collect(); .collect();
let sanitized = sanitizer.clean(&deeply_nested_html); let sanitized = sanitizer.clean(&deeply_nested_html).to_string();
assert!(sanitized.contains("I should be fine.")); assert!(sanitized.contains("I should be fine."));
assert!(!sanitized.contains("I am in too deep!")); assert!(!sanitized.contains("I am in too deep!"));