Skip to content

Commit 9a79b47

Browse files
mrsdizzielafriks
authored andcommitted
Fix emoji detection in certain cases (go-gitea#12320)
* Fix emoji detection certain cases Previous tests weren't complicated enough so there were some situations where emojis were't detected properly. Find the earliest occurance in addition to checking for the longest combination. Fixes go-gitea#12312 * ok spell bot Co-authored-by: Lauris BH <[email protected]>
1 parent 8d1cd4d commit 9a79b47

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

modules/emoji/emoji.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,35 @@ func ReplaceAliases(s string) string {
130130
// FindEmojiSubmatchIndex returns index pair of longest emoji in a string
131131
func FindEmojiSubmatchIndex(s string) []int {
132132
loadMap()
133+
found := make(map[int]int)
134+
keys := make([]int, 0)
133135

134136
//see if there are any emoji in string before looking for position of specific ones
135137
//no performance difference when there is a match but 10x faster when there are not
136138
if s == ReplaceCodes(s) {
137139
return nil
138140
}
139141

142+
// get index of first emoji occurrence while also checking for longest combination
140143
for j := range GemojiData {
141144
i := strings.Index(s, GemojiData[j].Emoji)
142145
if i != -1 {
143-
return []int{i, i + len(GemojiData[j].Emoji)}
146+
if _, ok := found[i]; !ok {
147+
if len(keys) == 0 || i < keys[0] {
148+
found[i] = j
149+
keys = []int{i}
150+
}
151+
if i == 0 {
152+
break
153+
}
154+
}
144155
}
145156
}
157+
158+
if len(keys) > 0 {
159+
index := keys[0]
160+
return []int{index, index + len(GemojiData[found[index]].Emoji)}
161+
}
162+
146163
return nil
147164
}

modules/markup/html_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ func TestRender_emoji(t *testing.T) {
266266
test(
267267
"Some text with 😄😄 2 emoji next to each other",
268268
`<p>Some text with <span class="emoji" aria-label="grinning face with smiling eyes">😄</span><span class="emoji" aria-label="grinning face with smiling eyes">😄</span> 2 emoji next to each other</p>`)
269+
test(
270+
"😎🤪🔐🤑❓",
271+
`<p><span class="emoji" aria-label="smiling face with sunglasses">😎</span><span class="emoji" aria-label="zany face">🤪</span><span class="emoji" aria-label="locked with key">🔐</span><span class="emoji" aria-label="money-mouth face">🤑</span><span class="emoji" aria-label="question mark">❓</span></p>`)
272+
269273
// should match nothing
270274
test(
271275
"2001:0db8:85a3:0000:0000:8a2e:0370:7334",

0 commit comments

Comments
 (0)