mirror of https://github.com/miniflux/v2.git
Fix invalid output when truncating Unicode text in templates
This commit is contained in:
parent
2538eea177
commit
d79bab2997
|
@ -5,7 +5,6 @@
|
||||||
package template // import "miniflux.app/template"
|
package template // import "miniflux.app/template"
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/mail"
|
"net/mail"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -80,15 +79,13 @@ func (f *funcMap) Map() template.FuncMap {
|
||||||
},
|
},
|
||||||
"dict": dict,
|
"dict": dict,
|
||||||
"truncate": func(str string, max int) string {
|
"truncate": func(str string, max int) string {
|
||||||
if len(str) > max {
|
runes := 0
|
||||||
var buffer bytes.Buffer
|
for i := range str {
|
||||||
|
runes++
|
||||||
buffer.WriteString(str[:max-1])
|
if runes > max {
|
||||||
buffer.WriteString("…")
|
return str[:i] + "…"
|
||||||
|
}
|
||||||
return buffer.String()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return str
|
return str
|
||||||
},
|
},
|
||||||
"theme_color": func(theme string) string {
|
"theme_color": func(theme string) string {
|
||||||
|
|
|
@ -0,0 +1,48 @@
|
||||||
|
// Copyright 2018 Frédéric Guillot. All rights reserved.
|
||||||
|
// Use of this source code is governed by the Apache 2.0
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package template // import "miniflux.app/template"
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTruncate(t *testing.T) {
|
||||||
|
fm := funcMap{}
|
||||||
|
if f, ok := fm.Map()["truncate"]; ok {
|
||||||
|
if truncate := f.(func(str string, max int) string); ok {
|
||||||
|
shortEnglishText := "Short text"
|
||||||
|
shortUnicodeText := "Короткий текст"
|
||||||
|
|
||||||
|
// edge case
|
||||||
|
if truncate(shortEnglishText, len(shortEnglishText)) != shortEnglishText {
|
||||||
|
t.Fatal("Invalid truncation")
|
||||||
|
}
|
||||||
|
// real case
|
||||||
|
if truncate(shortEnglishText, 25) != shortEnglishText {
|
||||||
|
t.Fatal("Invalid truncation")
|
||||||
|
}
|
||||||
|
if truncate(shortUnicodeText, len(shortUnicodeText)) != shortUnicodeText {
|
||||||
|
t.Fatal("Invalid truncation")
|
||||||
|
}
|
||||||
|
if truncate(shortUnicodeText, 25) != shortUnicodeText {
|
||||||
|
t.Fatal("Invalid truncation")
|
||||||
|
}
|
||||||
|
|
||||||
|
longEnglishText := "This is really pretty long English text"
|
||||||
|
longRussianText := "Это реально очень длинный русский текст"
|
||||||
|
|
||||||
|
if truncate(longEnglishText, 25) != "This is really pretty lon…" {
|
||||||
|
t.Fatal("Invalid truncation")
|
||||||
|
}
|
||||||
|
if truncate(longRussianText, 25) != "Это реально очень длинный…" {
|
||||||
|
t.Fatal("Invalid truncation")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Fatal("Type assetion for this func is failed, check func, maybe it was changed")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
t.Fatal("There is no such function in this map, check key, maybe it was changed")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue