From de268314f3c46bd764016d2b9ae515729f2036c8 Mon Sep 17 00:00:00 2001 From: Matthew Esposito Date: Tue, 26 Dec 2023 15:42:41 -0500 Subject: [PATCH] Fix tests --- src/settings.rs | 2 +- src/utils.rs | 34 ++++++++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/settings.rs b/src/settings.rs index 052941a..6f3803d 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -19,7 +19,7 @@ struct SettingsTemplate { // CONSTANTS -const PREFS: [&str; 13] = [ +const PREFS: [&str; 15] = [ "theme", "front_page", "layout", diff --git a/src/utils.rs b/src/utils.rs index ac41c4a..69177d3 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -573,6 +573,7 @@ pub struct Preferences { pub use_hls: String, pub autoplay_videos: String, pub fixed_navbar: String, + pub disable_visit_reddit_confirmation: String, pub comment_sort: String, pub post_sort: String, pub subscriptions: Vec, @@ -608,6 +609,7 @@ impl Preferences { hide_hls_notification: setting(&req, "hide_hls_notification"), autoplay_videos: setting(&req, "autoplay_videos"), fixed_navbar: setting_or_default(&req, "fixed_navbar", "on".to_string()), + disable_visit_reddit_confirmation: setting(req, "disable_visit_reddit_confirmation"), comment_sort: setting(&req, "comment_sort"), post_sort: setting(&req, "post_sort"), subscriptions: setting(&req, "subscriptions").split('+').map(String::from).filter(|s| !s.is_empty()).collect(), @@ -864,7 +866,7 @@ pub fn format_url(url: &str) -> String { } } -static REDDIT_REGEX: Lazy = Lazy::new(|| Regex::new(r#"href="(https|http|)://(www\.|old\.|np\.|amp\.|)(reddit\.com|redd\.it)/"#).unwrap()); +static REDDIT_REGEX: Lazy = Lazy::new(|| Regex::new(r#"href="(https|http|)://(www\.|old\.|np\.|amp\.|new\.|)(reddit\.com|redd\.it)/"#).unwrap()); static REDDIT_PREVIEW_REGEX: Lazy = Lazy::new(|| Regex::new(r"https?://external-preview\.redd\.it(.*)[^?]").unwrap()); // Rewrite Reddit links to Libreddit in body of text @@ -875,7 +877,7 @@ pub fn rewrite_urls(input_text: &str) -> String { .to_string() // Remove (html-encoded) "\" from URLs. .replace("%5C", "") - .replace('\\', ""); + .replace("\\_", "_"); // Rewrite external media previews to Libreddit if REDDIT_PREVIEW_REGEX.is_match(&text1) { @@ -1049,13 +1051,29 @@ mod tests { } #[test] - fn rewrite_urls_removes_backslashes() { - let comment_body_html = - r#"https://www.reddit.com/r/linux\\_gaming/comments/x/just\\_a\\_test/"#; + fn rewrite_urls_removes_backslashes_and_rewrites_url() { assert_eq!( - rewrite_urls(comment_body_html), - r#"https://www.reddit.com/r/linux_gaming/comments/x/just_a_test/"# - ) + rewrite_urls( + "https://new.reddit.com/r/linux\\_gaming/comments/x/just\\_a\\_test/" + ), + "https://new.reddit.com/r/linux_gaming/comments/x/just_a_test/" + ); + assert_eq!( + rewrite_urls( + "e.g. <a href=\"https://www.reddit.com/r/linux%5C_gaming/comments/ql9j15/anyone%5C_else%5C_confused%5C_with%5C_linus%5C_linux%5C_issues/\">https://www.reddit.com/r/linux\\_gaming/comments/ql9j15/anyone\\_else\\_confused\\_with\\_linus\\_linux\\_issues/</a>" + ), + "e.g. <a href=\"/r/linux_gaming/comments/ql9j15/anyone_else_confused_with_linus_linux_issues/\">https://www.reddit.com/r/linux_gaming/comments/ql9j15/anyone_else_confused_with_linus_linux_issues/</a>" + ); + } + + #[test] + fn rewrite_urls_keeps_intentional_backslashes() { + assert_eq!( + rewrite_urls( + "printf \"\\npolkit.addRule(function(action, subject)" + ), + "printf \"\\npolkit.addRule(function(action, subject)" + ); } #[test]