Skip to content

A platform for community discussion. Free, open, simple.

FIX: Correctly handle VS16 in toned ZWJ emoji sequences (#37419)

LY9738c429c860
diff --git a/app/models/emoji.rb b/app/models/emoji.rb
index d17a994ceb1..b24ce968036 100644
--- a/app/models/emoji.rb
+++ b/app/models/emoji.rb
@@ -308,7 +308,10 @@ class Emoji
replacements[code] = name
if is_tonable_emojis.include?(name)
fitzpatrick_scales.each_with_index do |scale, index|
- toned_code = code.codepoints.insert(1, scale).pack("U*")
+ codepoints = code.codepoints
+ codepoints.delete_at(1) if codepoints[1] == 0xfe0f
+
+ toned_code = codepoints.insert(1, scale).pack("U*")
replacements[toned_code] = "#{name}:t#{index + 2}"
end
end
@@ -348,7 +351,10 @@ class Emoji
map[e["name"]] = code
if is_tonable_emojis.include?(e["name"])
FITZPATRICK_SCALE.each_with_index do |scale, index|
- toned_code = (code.codepoints.insert(1, scale.to_i(16))).pack("U*")
+ codepoints = code.codepoints
+ codepoints.delete_at(1) if codepoints[1] == 0xfe0f
+
+ toned_code = (codepoints.insert(1, scale.to_i(16))).pack("U*")
map["#{e["name"]}:t#{index + 2}"] = toned_code
end
end
diff --git a/spec/lib/pretty_text_spec.rb b/spec/lib/pretty_text_spec.rb
index 3c6138e1bae..6db0e2772dd 100644
--- a/spec/lib/pretty_text_spec.rb
+++ b/spec/lib/pretty_text_spec.rb
@@ -1692,6 +1692,29 @@ RSpec.describe PrettyText do
expect(PrettyText.cook("❤️💣")).to match(/<img src[^>]+bomb[^>]+>/)
end
+ it "correctly cooks skin tone and gendered ZWJ sequences" do
+ # 1. Standard RGI Input (Man Bouncing Ball: Light Skin Tone)
+ # Structure: ⛹ (26f9) + 🏻 (1f3fb) + ZWJ (200d) + ♂ (2642) + VS16 (fe0f)
+ standard_input = [0x26f9, 0x1f3fb, 0x200d, 0x2642, 0xfe0f].pack("U*")
+ expect(PrettyText.cook(standard_input)).to eq(
+ "<p><img src=\"/images/emoji/twitter/man_bouncing_ball/2.png?v=#{Emoji::EMOJI_VERSION}\" title=\":man_bouncing_ball:t2:\" class=\"emoji only-emoji\" alt=\":man_bouncing_ball:t2:\" loading=\"lazy\" width=\"20\" height=\"20\"></p>",
+ )
+
+ # 2. Regression: Ordinary Skin Tone (Thumbs Up: Light Skin Tone)
+ # Structure: 👍 (1f44d) + 🏻 (1f3fb)
+ thumbs_up = [0x1f44d, 0x1f3fb].pack("U*")
+ expect(PrettyText.cook(thumbs_up)).to eq(
+ "<p><img src=\"/images/emoji/twitter/+1/2.png?v=#{Emoji::EMOJI_VERSION}\" title=\":+1:t2:\" class=\"emoji only-emoji\" alt=\":+1:t2:\" loading=\"lazy\" width=\"20\" height=\"20\"></p>",
+ )
+
+ # 3. Regression: Ordinary ZWJ Sequence (Family: Man, Woman, Girl)
+ # Structure: 👨 (1f468) + ZWJ (200d) + 👩 (1f469) + ZWJ (200d) + 👧 (1f467)
+ family = [0x1f468, 0x200d, 0x1f469, 0x200d, 0x1f467].pack("U*")
+ expect(PrettyText.cook(family)).to eq(
+ "<p><img src=\"/images/emoji/twitter/family_man_woman_girl.png?v=#{Emoji::EMOJI_VERSION}\" title=\":family_man_woman_girl:\" class=\"emoji only-emoji\" alt=\":family_man_woman_girl:\" loading=\"lazy\" width=\"20\" height=\"20\"></p>",
+ )
+ end
+
it "replaces Emoji from Unicode 14.0" do
expect(PrettyText.cook("🫣")).to match(/\:face_with_peeking_eye\:/)
end
diff --git a/spec/models/emoji_spec.rb b/spec/models/emoji_spec.rb
index 364b63dec26..124fd768c4f 100644
--- a/spec/models/emoji_spec.rb
+++ b/spec/models/emoji_spec.rb
@@ -17,6 +17,49 @@ RSpec.describe Emoji do
end
end
+ describe ".unicode_replacements" do
+ before { Emoji.clear_cache }
+
+ it "generates correct keys for skin tone and gendered ZWJ sequences" do
+ replacements = Emoji.unicode_replacements
+
+ # Case 1: Man Bouncing Ball: Light Skin Tone (RGI)
+ # 26f9 (Base) + 1f3fb (Tone) + 200d (ZWJ) + 2642 (Gender) + fe0f (VS16)
+ man_bouncing_ball_rgi = [0x26f9, 0x1f3fb, 0x200d, 0x2642, 0xfe0f].pack("U*")
+ # Malformed sequence (Old Bug): VS16 incorrectly persisting in the middle
+ malformed_key = [0x26f9, 0x1f3fb, 0xfe0f, 0x200d, 0x2642, 0xfe0f].pack("U*")
+ expect(replacements.keys).to include(man_bouncing_ball_rgi)
+ expect(replacements.keys).not_to include(malformed_key)
+ expect(replacements[man_bouncing_ball_rgi]).to eq("man_bouncing_ball:t2")
+
+ # Case 2: Thumbs Up: Light Skin Tone
+ thumbs_up = [0x1f44d, 0x1f3fb].pack("U*")
+ expect(replacements.keys).to include(thumbs_up)
+
+ # Case 3: Family (Man, Woman, Girl)
+ family = [0x1f468, 0x200d, 0x1f469, 0x200d, 0x1f467].pack("U*")
+ expect(replacements.keys).to include(family)
+ end
+ end
+
+ describe ".lookup_unicode" do
+ before { Emoji.clear_cache }
+
+ it "returns correct unicode for skin tone and gendered ZWJ sequences" do
+ # Case 1: Man Bouncing Ball: Light Skin Tone (RGI)
+ expected_rgi = [0x26f9, 0x1f3fb, 0x200d, 0x2642, 0xfe0f].pack("U*")
+ expect(Emoji.lookup_unicode("man_bouncing_ball:t2")).to eq(expected_rgi)
+
+ # Case 2: Thumbs Up: Light Skin Tone
+ thumbs_up = [0x1f44d, 0x1f3fb].pack("U*")
+ expect(Emoji.lookup_unicode("+1:t2")).to eq(thumbs_up)
+
+ # Case 3: Family (Man, Woman, Girl)
+ family_skinned = [0x1f468, 0x200d, 0x1f469, 0x200d, 0x1f467].pack("U*")
+ expect(Emoji.lookup_unicode("family_man_woman_girl")).to eq(family_skinned)
+ end
+ end
+
describe ".lookup_unicode" do
it "returns unicode for emoji, aliases, and skin tones" do
expect(Emoji.lookup_unicode("trade_mark")).to eq("™")