diff options
| author | Kumpelinus <kumpelinus@jat.de> | 2025-05-01 20:26:04 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-05-01 13:26:04 -0500 |
| commit | 11a74f215e28d7c3971c9894351567edb68ef0f8 (patch) | |
| tree | 54f6be9b4152d71d0bf5016cc804854ccc6bcfb1 /azalea-chat/src/text_component.rs | |
| parent | 4a7d21425ca644de776bd380abbfabc816710f43 (diff) | |
| download | azalea-drasl-11a74f215e28d7c3971c9894351567edb68ef0f8.tar.xz | |
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 <git@matdoes.dev>
Diffstat (limited to 'azalea-chat/src/text_component.rs')
| -rw-r--r-- | azalea-chat/src/text_component.rs | 35 |
1 files changed, 34 insertions, 1 deletions
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(); @@ -164,6 +164,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]<br>{END_SPAN}{BOLD_AQUA}HAPPY HOLIDAYS{END_SPAN}", + END_SPAN = "</span>", + GREEN = "<span style=\"color: #55FF55;\">", + RED = "<span style=\"color: #FF5555;\">", + BOLD_AQUA = "<span style=\"color: #55FFFF;font-weight: bold;\">", + ) + ); + } + + #[test] + fn test_xss_html() { + let component = TextComponent::new("§a<b>&\n§b</b>".to_string()).get(); + + assert_eq!( + component.to_html(), + format!( + "{GREEN}<b>&<br>{END_SPAN}{AQUA}</b>{END_SPAN}", + END_SPAN = "</span>", + GREEN = "<span style=\"color: #55FF55;\">", + AQUA = "<span style=\"color: #55FFFF;\">", + ) + ); + } + + #[test] fn test_legacy_color_code_to_component() { let component = TextComponent::new("§lHello §r§1w§2o§3r§4l§5d".to_string()).get(); assert_eq!( |
