Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v10.1.x] Slug: Combine various slugify fixes for special character handling #73173

Merged
merged 1 commit into from Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 29 additions & 7 deletions pkg/infra/slugify/slugify.go
Expand Up @@ -42,6 +42,7 @@ import (
var (
simpleSlugger = &slugger{
isValidCharacter: validCharacter,
replaceCharacter: '-',
replacementMap: getDefaultReplacements(),
omitMap: getDefaultOmitments(),
}
Expand All @@ -64,14 +65,12 @@ func validCharacter(c rune) bool {
if c >= '0' && c <= '9' {
return true
}
if c == '_' || c == '-' {
return true
}
return false
}

// Slugifier based on settings
type slugger struct {
replaceCharacter rune
isValidCharacter func(c rune) bool
replacementMap map[rune]string
omitMap map[rune]struct{}
Expand All @@ -81,37 +80,52 @@ type slugger struct {
func (s slugger) Slugify(value string) string {
value = strings.ToLower(value)
var buffer bytes.Buffer
lastCharacterWasInvalid := false

for len(value) > 0 {
c, size := utf8.DecodeRuneInString(value)
value = value[size:]

if newCharacter, ok := s.replacementMap[c]; ok {
if lastCharacterWasInvalid {
buffer.WriteRune(s.replaceCharacter)
}
buffer.WriteString(newCharacter)
lastCharacterWasInvalid = false
continue
}

if s.isValidCharacter(c) {
if lastCharacterWasInvalid {
buffer.WriteRune(s.replaceCharacter)
}
buffer.WriteRune(c)
lastCharacterWasInvalid = false
continue
}

if _, ok := s.omitMap[c]; ok {
lastCharacterWasInvalid = true
continue
}

p := make([]byte, 4)
size = utf8.EncodeRune(p, c)
if lastCharacterWasInvalid {
buffer.WriteRune(s.replaceCharacter)
}
for i := 0; i < size; i++ {
buffer.WriteString(fmt.Sprintf("%%%x", p[i]))
buffer.WriteString(fmt.Sprintf("%x", p[i]))
}
lastCharacterWasInvalid = true
}

return buffer.String()
return strings.Trim(buffer.String(), string(s.replaceCharacter))
}

func getDefaultOmitments() map[rune]struct{} {
return map[rune]struct{}{
' ': {},
',': {},
'"': {},
'\'': {},
Expand All @@ -122,13 +136,21 @@ func getDefaultOmitments() map[rune]struct{} {
'.': {},
'(': {},
')': {},
'-': {},
'_': {},
'[': {},
']': {},
'/': {},
'\\': {},
'!': {},
'{': {},
'}': {},
'%': {},
}
}

func getDefaultReplacements() map[rune]string {
return map[rune]string{
' ': "-",

'&': "and",
'@': "at",
'©': "c",
Expand Down
8 changes: 6 additions & 2 deletions pkg/infra/slugify/slugify_test.go
Expand Up @@ -9,12 +9,16 @@ func TestSlugify(t *testing.T) {
results["hello-playground"] = "Hello, playground"
results["00a4bc92-3695-5702-9ddf-6719fdf11567"] = "😢 😣 😤 😥 😦 😧 😨 😩 😪 😫 😬 Hello, it's paradise"
results["61db60b5-f1e7-5853-9b81-0f074fc268ea"] = "😢 😣 😤 😥 😦 😧 😨 😩 😪 😫 😬"
results["%f0%9f%98%a2--"] = "😢 -"
results["a-"] = "?,a . \n "
results["f09f98a2"] = "😢 -"
results["a"] = "?,a . \n "
results["0a68eb57-c88a-5f34-9e9d-27f85e68af4f"] = "" // empty input has a slug!
results["3cbb528a-0ebf-54ad-bed2-2a188cd1824e"] = "方向盤後面 hi this is a test خلف المقو"
results["cong-hoa-xa-hoi-chu-nghia-viet-nam"] = "Cộng hòa xã hội chủ nghĩa Việt Nam"
results["noi-nang-canh-canh-ben-long-bieng-khuay"] = "Nỗi nàng canh cánh bên lòng biếng khuây" // This line in a poem called Truyen Kieu
results["hello-playground"] = "Hello / playground"
results["hello-playground"] = "Hello % playground"
results["hello-and-playground"] = "Hello & //% playground"
results["hello-2a-23-playground"] = "Hello *# playground"

for slug, original := range results {
actual := Slugify(original)
Expand Down
2 changes: 1 addition & 1 deletion pkg/services/accesscontrol/models_test.go
Expand Up @@ -64,7 +64,7 @@ func TestSaveExternalServiceRoleCommand_Validate(t *testing.T) {
Permissions: []Permission{{Action: "users:read", Scope: "users:id:1"}},
},
wantErr: false,
wantID: "thisis-a-very-strange-___-app-name",
wantID: "thisis-a-very-strange-app-name",
},
{
name: "invalid empty Action",
Expand Down
6 changes: 3 additions & 3 deletions pkg/services/dashboards/models_test.go
Expand Up @@ -77,9 +77,9 @@ func TestSlugifyTitle(t *testing.T) {
testCases := map[string]string{
"Grafana Play Home": "grafana-play-home",
"snöräv-över-ån": "snorav-over-an",
"漢字": "%e6%bc%a2%e5%ad%97", // "han-zi", // Hanzi for hanzi
"🇦🇶": "%f0%9f%87%a6%f0%9f%87%b6", // flag of Antarctica-emoji, using fallback
"𒆠": "%f0%92%86%a0", // cuneiform Ki, using fallback
"漢字": "e6bca2-e5ad97", // "han-zi", // Hanzi for hanzi
"🇦🇶": "f09f87a6-f09f87b6", // flag of Antarctica-emoji, using fallback
"𒆠": "f09286a0", // cuneiform Ki, using fallback
}

for input, expected := range testCases {
Expand Down