BlueGit

ict rusty-teams

fix(chat): skin-toned emoji show without a stray colour square

- Graph repeats the skin tone in emoticon alt text (πŸ‘πŸΌπŸΌ, πŸ€·πŸ½β€β™‚οΈπŸ½); the duplicate is dropped for text and jumbo emoji

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

metis Daniel DΓ€schle Β· pushed by metis

Authored 2026-10-15 Β· 6 weeks ago

Hash
cd07b8d0919ff6bb4419205f858854f207ff2694
Parent
fb9db24f
2 files changed +39 βˆ’4
crates/core/src/jumbo.rs +35 βˆ’2
@@ -4,6 +4,7 @@ use unicode_segmentation::UnicodeSegmentation;
4 4 pub const MAX_JUMBO_EMOJI: usize = 3;
5 5
6 6 const EMOJI_ITEMTYPE: &str = "schema.skype.com/Emoji";
7 const SKIN_TONES: std::ops::RangeInclusive<char> = '\u{1f3fb}'..='\u{1f3ff}';
7 8 const BLOCKING_ELEMENTS: [&str; 4] = ["table", "attachment", "blockquote", "pre"];
8 9 const IGNORED_CHARACTERS: [char; 5] = ['\u{a0}', '\u{200b}', '\u{200c}', '\u{200d}', '\u{feff}'];
9 10
@@ -104,12 +105,33 @@ fn visit_element(element: ElementRef<'_>, scan: &mut Scan) {
104 105 }
105 106
106 107 fn glyph(element: ElementRef<'_>) -> String {
107 element
108 let raw = element
108 109 .value()
109 110 .attr("alt")
110 111 .filter(|alt| !alt.is_empty())
111 112 .map(str::to_owned)
112 .unwrap_or_else(|| element.text().collect())
113 .unwrap_or_else(|| element.text().collect());
114 without_repeated_skin_tone(&raw)
115 }
116
117 /// Graph repeats the skin tone in emoticon alt text (`πŸ‘πŸΌπŸΌ`, `πŸ€·πŸ½β€β™‚οΈπŸ½`); the extra one renders as a colour square.
118 pub(crate) fn without_repeated_skin_tone(glyph: &str) -> String {
119 let mut cleaned = String::with_capacity(glyph.len());
120 let mut previous: Option<char> = None;
121 let mut tone_seen = false;
122 for character in glyph.chars() {
123 let is_tone = SKIN_TONES.contains(&character);
124 let stray = is_tone
125 && previous.is_some_and(|previous| {
126 SKIN_TONES.contains(&previous) || (tone_seen && !is_pictographic(previous))
127 });
128 if !stray {
129 cleaned.push(character);
130 }
131 tone_seen |= is_tone;
132 previous = Some(character);
133 }
134 cleaned
113 135 }
114 136
115 137 fn unicode_emoji(text: &str) -> Option<Vec<JumboEmoji>> {
@@ -165,6 +187,17 @@ fn is_pictographic(character: char) -> bool {
165 187 mod tests {
166 188 use super::*;
167 189
190 #[test]
191 fn repeated_skin_tones_from_graph_are_dropped() {
192 assert_eq!(without_repeated_skin_tone("πŸ‘πŸΌπŸΌ"), "πŸ‘πŸΌ");
193 assert_eq!(without_repeated_skin_tone("🀷🏽\u{200d}β™‚οΈπŸ½"), "🀷🏽\u{200d}♂️");
194 assert_eq!(without_repeated_skin_tone("πŸ§‘πŸ»\u{200d}🀝\u{200d}πŸ§‘πŸΏ"), "πŸ§‘πŸ»\u{200d}🀝\u{200d}πŸ§‘πŸΏ");
195 assert_eq!(
196 jumbo_emoji(r#"<emoji id="yes" alt="πŸ‘πŸΌπŸΌ"></emoji>"#),
197 Some(vec![emoticon("yes", "πŸ‘πŸΌ")])
198 );
199 }
200
168 201 fn emoticon(id: &str, glyph: &str) -> JumboEmoji {
169 202 JumboEmoji {
170 203 id: Some(id.to_owned()),
@@ -4,6 +4,7 @@ use unicode_segmentation::UnicodeSegmentation;
4pub const MAX_JUMBO_EMOJI: usize = 3;
4pub const MAX_JUMBO_EMOJI: usize = 3;
5
5
6const EMOJI_ITEMTYPE: &str = "schema.skype.com/Emoji";
6const EMOJI_ITEMTYPE: &str = "schema.skype.com/Emoji";
7const SKIN_TONES: std::ops::RangeInclusive<char> = '\u{1f3fb}'..='\u{1f3ff}';
7const BLOCKING_ELEMENTS: [&str; 4] = ["table", "attachment", "blockquote", "pre"];
8const BLOCKING_ELEMENTS: [&str; 4] = ["table", "attachment", "blockquote", "pre"];
8const IGNORED_CHARACTERS: [char; 5] = ['\u{a0}', '\u{200b}', '\u{200c}', '\u{200d}', '\u{feff}'];
9const IGNORED_CHARACTERS: [char; 5] = ['\u{a0}', '\u{200b}', '\u{200c}', '\u{200d}', '\u{feff}'];
9
10
@@ -104,12 +105,33 @@ fn visit_element(element: ElementRef<'_>, scan: &mut Scan) {
104}
105}
105
106
106fn glyph(element: ElementRef<'_>) -> String {
107fn glyph(element: ElementRef<'_>) -> String {
107 element
108 let raw = element
108 .value()
109 .value()
109 .attr("alt")
110 .attr("alt")
110 .filter(|alt| !alt.is_empty())
111 .filter(|alt| !alt.is_empty())
111 .map(str::to_owned)
112 .map(str::to_owned)
112 .unwrap_or_else(|| element.text().collect())
113 .unwrap_or_else(|| element.text().collect());
114 without_repeated_skin_tone(&raw)
115}
116
117/// Graph repeats the skin tone in emoticon alt text (`πŸ‘πŸΌπŸΌ`, `πŸ€·πŸ½β€β™‚οΈπŸ½`); the extra one renders as a colour square.
118pub(crate) fn without_repeated_skin_tone(glyph: &str) -> String {
119 let mut cleaned = String::with_capacity(glyph.len());
120 let mut previous: Option<char> = None;
121 let mut tone_seen = false;
122 for character in glyph.chars() {
123 let is_tone = SKIN_TONES.contains(&character);
124 let stray = is_tone
125 && previous.is_some_and(|previous| {
126 SKIN_TONES.contains(&previous) || (tone_seen && !is_pictographic(previous))
127 });
128 if !stray {
129 cleaned.push(character);
130 }
131 tone_seen |= is_tone;
132 previous = Some(character);
133 }
134 cleaned
113}
135}
114
136
115fn unicode_emoji(text: &str) -> Option<Vec<JumboEmoji>> {
137fn unicode_emoji(text: &str) -> Option<Vec<JumboEmoji>> {
@@ -165,6 +187,17 @@ fn is_pictographic(character: char) -> bool {
165mod tests {
187mod tests {
166 use super::*;
188 use super::*;
167
189
190 #[test]
191 fn repeated_skin_tones_from_graph_are_dropped() {
192 assert_eq!(without_repeated_skin_tone("πŸ‘πŸΌπŸΌ"), "πŸ‘πŸΌ");
193 assert_eq!(without_repeated_skin_tone("🀷🏽\u{200d}β™‚οΈπŸ½"), "🀷🏽\u{200d}♂️");
194 assert_eq!(without_repeated_skin_tone("πŸ§‘πŸ»\u{200d}🀝\u{200d}πŸ§‘πŸΏ"), "πŸ§‘πŸ»\u{200d}🀝\u{200d}πŸ§‘πŸΏ");
195 assert_eq!(
196 jumbo_emoji(r#"<emoji id="yes" alt="πŸ‘πŸΌπŸΌ"></emoji>"#),
197 Some(vec![emoticon("yes", "πŸ‘πŸΌ")])
198 );
199 }
200
168 fn emoticon(id: &str, glyph: &str) -> JumboEmoji {
201 fn emoticon(id: &str, glyph: &str) -> JumboEmoji {
169 JumboEmoji {
202 JumboEmoji {
170 id: Some(id.to_owned()),
203 id: Some(id.to_owned()),
crates/core/src/spans.rs +4 βˆ’2
@@ -1,5 +1,7 @@
1 1 use scraper::{ElementRef, Html, Node};
2 2
3 use crate::jumbo::without_repeated_skin_tone;
4
3 5 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
4 6 pub enum FontSize {
5 7 Small,
@@ -374,7 +376,7 @@ fn push_emoji(element: ElementRef<'_>, spans: &mut Vec<Span>) {
374 376 .map(str::to_owned)
375 377 .filter(|alt| !alt.is_empty())
376 378 .unwrap_or_else(|| raw_text(element));
377 push_text(spans, &glyph);
379 push_text(spans, &without_repeated_skin_tone(&glyph));
378 380 }
379 381
380 382 fn push_image(element: ElementRef<'_>, spans: &mut Vec<Span>) {
@@ -384,7 +386,7 @@ fn push_image(element: ElementRef<'_>, spans: &mut Vec<Span>) {
384 386 .is_some_and(|itemtype| itemtype.contains(EMOJI_ITEMTYPE));
385 387 if is_emoji {
386 388 if let Some(alt) = value.attr("alt") {
387 push_text(spans, alt);
389 push_text(spans, &without_repeated_skin_tone(alt));
388 390 }
389 391 return;
390 392 }
@@ -1,5 +1,7 @@
1use scraper::{ElementRef, Html, Node};
1use scraper::{ElementRef, Html, Node};
2
2
3use crate::jumbo::without_repeated_skin_tone;
4
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum FontSize {
6pub enum FontSize {
5 Small,
7 Small,
@@ -374,7 +376,7 @@ fn push_emoji(element: ElementRef<'_>, spans: &mut Vec<Span>) {
374 .map(str::to_owned)
376 .map(str::to_owned)
375 .filter(|alt| !alt.is_empty())
377 .filter(|alt| !alt.is_empty())
376 .unwrap_or_else(|| raw_text(element));
378 .unwrap_or_else(|| raw_text(element));
377 push_text(spans, &glyph);
379 push_text(spans, &without_repeated_skin_tone(&glyph));
378}
380}
379
381
380fn push_image(element: ElementRef<'_>, spans: &mut Vec<Span>) {
382fn push_image(element: ElementRef<'_>, spans: &mut Vec<Span>) {
@@ -384,7 +386,7 @@ fn push_image(element: ElementRef<'_>, spans: &mut Vec<Span>) {
384 .is_some_and(|itemtype| itemtype.contains(EMOJI_ITEMTYPE));
386 .is_some_and(|itemtype| itemtype.contains(EMOJI_ITEMTYPE));
385 if is_emoji {
387 if is_emoji {
386 if let Some(alt) = value.attr("alt") {
388 if let Some(alt) = value.attr("alt") {
387 push_text(spans, alt);
389 push_text(spans, &without_repeated_skin_tone(alt));
388 }
390 }
389 return;
391 return;
390 }
392 }