Merge remote-tracking branch 'origin/main' into feat/voice-quality
@@ -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;4
pub const MAX_JUMBO_EMOJI: usize = 3;4
pub const MAX_JUMBO_EMOJI: usize = 3;5
5
6
const EMOJI_ITEMTYPE: &str = "schema.skype.com/Emoji";6
const EMOJI_ITEMTYPE: &str = "schema.skype.com/Emoji";7
const SKIN_TONES: std::ops::RangeInclusive<char> = '\u{1f3fb}'..='\u{1f3ff}';7
const BLOCKING_ELEMENTS: [&str; 4] = ["table", "attachment", "blockquote", "pre"];8
const BLOCKING_ELEMENTS: [&str; 4] = ["table", "attachment", "blockquote", "pre"];8
const IGNORED_CHARACTERS: [char; 5] = ['\u{a0}', '\u{200b}', '\u{200c}', '\u{200d}', '\u{feff}'];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
fn glyph(element: ElementRef<'_>) -> String {107
fn glyph(element: ElementRef<'_>) -> String {107
element108
let raw = element108
.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.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_tone125
&& 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
cleaned113
}135
}114
136
115
fn unicode_emoji(text: &str) -> Option<Vec<JumboEmoji>> {137
fn unicode_emoji(text: &str) -> Option<Vec<JumboEmoji>> {@@ -165,6 +187,17 @@ fn is_pictographic(character: char) -> bool {165
mod tests {187
mod 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()),@@ -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 @@1
use scraper::{ElementRef, Html, Node};1
use scraper::{ElementRef, Html, Node};2
2
3
use crate::jumbo::without_repeated_skin_tone;4
3
#[derive(Debug, Clone, Copy, PartialEq, Eq)]5
#[derive(Debug, Clone, Copy, PartialEq, Eq)]4
pub enum FontSize {6
pub 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
380
fn push_image(element: ElementRef<'_>, spans: &mut Vec<Span>) {382
fn 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
}