From ae26730e29eed6e319a9091ed989602114c6f537 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 3 Nov 2022 13:42:36 +0100 Subject: [PATCH] events: Move fragment stringification out of HtmlSanitizer::clean --- .../src/events/room/message/sanitize.rs | 4 +-- .../room/message/sanitize/html_sanitizer.rs | 26 +++++++++---------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/crates/ruma-common/src/events/room/message/sanitize.rs b/crates/ruma-common/src/events/room/message/sanitize.rs index 77ead8a6..c1f4d6b6 100644 --- a/crates/ruma-common/src/events/room/message/sanitize.rs +++ b/crates/ruma-common/src/events/room/message/sanitize.rs @@ -23,7 +23,7 @@ pub fn sanitize_html( remove_reply_fallback: RemoveReplyFallback, ) -> String { 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. @@ -64,7 +64,7 @@ pub enum RemoveReplyFallback { #[cfg(feature = "unstable-sanitize")] pub fn remove_html_reply_fallback(s: &str) -> String { 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. diff --git a/crates/ruma-common/src/events/room/message/sanitize/html_sanitizer.rs b/crates/ruma-common/src/events/room/message/sanitize/html_sanitizer.rs index aae59a27..06388158 100644 --- a/crates/ruma-common/src/events/room/message/sanitize/html_sanitizer.rs +++ b/crates/ruma-common/src/events/room/message/sanitize/html_sanitizer.rs @@ -58,7 +58,7 @@ impl HtmlSanitizer { } /// 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 root = fragment.nodes[0].first_child.unwrap(); @@ -68,7 +68,7 @@ impl HtmlSanitizer { self.clean_node(&mut fragment, child, 0); } - fragment.to_string() + fragment } fn clean_node(&self, fragment: &mut Fragment, node_id: usize, depth: u32) { @@ -313,7 +313,7 @@ mod tests { ); assert_eq!( - sanitized, + sanitized.to_string(), "\ \

This is a paragraph with some color

\ @@ -342,7 +342,7 @@ mod tests { ); assert_eq!( - sanitized, + sanitized.to_string(), "\ \
\ @@ -377,7 +377,7 @@ mod tests { ); assert_eq!( - sanitized, + sanitized.to_string(), "\ This has no tag\

But this is inside a tag

\ @@ -404,7 +404,7 @@ mod tests { ); assert_eq!( - sanitized, + sanitized.to_string(), "\ This keeps its tag\

But this is inside a tag

\ @@ -423,7 +423,7 @@ mod tests { ); assert_eq!( - sanitized, + sanitized.to_string(), "\

Title for important stuff

\

Look at me!

\ @@ -442,7 +442,7 @@ mod tests { ); assert_eq!( - sanitized, + sanitized.to_string(), "\

Look at that picture:

\ " @@ -459,7 +459,7 @@ mod tests { ); assert_eq!( - sanitized, + sanitized.to_string(), "\

Go see my local website

\ " @@ -476,7 +476,7 @@ mod tests { ", ); assert_eq!( - sanitized, + sanitized.to_string(), "\

Join my room

\

To talk about my cat

\ @@ -491,7 +491,7 @@ mod tests { ", ); assert_eq!( - sanitized, + sanitized.to_string(), "\

Join my room

\

To talk about my cat

\ @@ -512,7 +512,7 @@ mod tests { ); assert_eq!( - sanitized, + sanitized.to_string(), "\

                 type StringList = Vec<String>;
@@ -534,7 +534,7 @@ mod tests {
             .chain(std::iter::repeat("").take(100))
             .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 am in too deep!"));