From 11a74f215e28d7c3971c9894351567edb68ef0f8 Mon Sep 17 00:00:00 2001 From: Kumpelinus Date: Thu, 1 May 2025 20:26:04 +0200 Subject: Implement a to_html method for FormattedText (#208) * Implement a to_html method for FormattedText Also fix a small issue with ansi formatting where it duplicated text. * cargo fmt * Make format conversion generic * cargo lint and fmt * Fix ascii conversion cleanup * Implement suggested changes * format, improve sanitization, add xss test --------- Co-authored-by: mat --- azalea-chat/src/text_component.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'azalea-chat/src/text_component.rs') diff --git a/azalea-chat/src/text_component.rs b/azalea-chat/src/text_component.rs index db1b4edf..7332adfa 100644 --- a/azalea-chat/src/text_component.rs +++ b/azalea-chat/src/text_component.rs @@ -146,7 +146,7 @@ mod tests { use crate::style::Ansi; #[test] - fn test_hypixel_motd() { + fn test_hypixel_motd_ansi() { let component = TextComponent::new("§aHypixel Network §c[1.8-1.18]\n§b§lHAPPY HOLIDAYS".to_string()) .get(); @@ -163,6 +163,39 @@ mod tests { ); } + #[test] + fn test_hypixel_motd_html() { + let component = + TextComponent::new("§aHypixel Network §c[1.8-1.18]\n§b§lHAPPY HOLIDAYS".to_string()) + .get(); + + assert_eq!( + component.to_html(), + format!( + "{GREEN}Hypixel Network {END_SPAN}{RED}[1.8-1.18]
{END_SPAN}{BOLD_AQUA}HAPPY HOLIDAYS{END_SPAN}", + END_SPAN = "", + GREEN = "", + RED = "", + BOLD_AQUA = "", + ) + ); + } + + #[test] + fn test_xss_html() { + let component = TextComponent::new("§a&\n§b".to_string()).get(); + + assert_eq!( + component.to_html(), + format!( + "{GREEN}<b>&
{END_SPAN}{AQUA}</b>{END_SPAN}", + END_SPAN = "
", + GREEN = "", + AQUA = "", + ) + ); + } + #[test] fn test_legacy_color_code_to_component() { let component = TextComponent::new("§lHello §r§1w§2o§3r§4l§5d".to_string()).get(); -- cgit v1.2.3