Skip to content

Commit 88aea56

Browse files
oishikazuobep
authored andcommitted
tpl: Fix strings/truncate CJK handling
Fix truncate position for mix of CJK and non-CJK text. Fixes #14039
1 parent f4c1157 commit 88aea56

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

‎tpl/strings/truncate.go‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ func (ns *Namespace) Truncate(s any, options ...any) (template.HTML, error) {
111111
if unicode.IsSpace(r) {
112112
lastWordIndex = lastNonSpace
113113
} else if unicode.In(r, unicode.Han, unicode.Hangul, unicode.Hiragana, unicode.Katakana) {
114-
lastWordIndex = i
114+
lastWordIndex = lastNonSpace
115+
lastNonSpace = i + utf8.RuneLen(r)
116+
if currentLen <= length {
117+
lastWordIndex = lastNonSpace
118+
}
115119
} else {
116120
lastNonSpace = i + utf8.RuneLen(r)
117121
}

‎tpl/strings/truncate_test.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ func TestTruncate(t *testing.T) {
4141
{10, template.HTML("<p>IamanextremelylongwordthatjustgoesonandonandonjusttoannoyyoualmostasifIwaswritteninGermanActuallyIbettheresagermanwordforthis</p>"), nil, template.HTML("<p>Iamanextre …</p>"), false},
4242
{13, template.HTML("With <a href=\"/markdown\">Markdown</a> inside."), nil, template.HTML("With <a href=\"/markdown\">Markdown …</a>"), false},
4343
{14, "Hello中国 Good 好的", nil, template.HTML("Hello中国 Good 好 …"), false},
44+
{12, "", "日本語でHugoを使います", template.HTML("日本語でHugoを使いま"), false},
45+
{12, "", "日本語で Hugo を使います", template.HTML("日本語で Hugo を使"), false},
46+
{8, "", "日本語でHugoを使います", template.HTML("日本語でHugo"), false},
47+
{10, "", "日本語で Hugo を使います", template.HTML("日本語で Hugo"), false},
48+
{9, "", "日本語で Hugo を使います", template.HTML("日本語で Hugo"), false},
49+
{7, "", "日本語でHugoを使います", template.HTML("日本語で"), false},
50+
{7, "", "日本語で Hugo を使います", template.HTML("日本語で"), false},
51+
{6, "", "日本語でHugoを使います", template.HTML("日本語で"), false},
52+
{6, "", "日本語で Hugo を使います", template.HTML("日本語で"), false},
53+
{5, "", "日本語でHugoを使います", template.HTML("日本語で"), false},
54+
{5, "", "日本語で Hugo を使います", template.HTML("日本語で"), false},
55+
{4, "", "日本語でHugoを使います", template.HTML("日本語で"), false},
56+
{4, "", "日本語で Hugo を使います", template.HTML("日本語で"), false},
4457
{15, "", template.HTML("A <br> tag that's not closed"), template.HTML("A <br> tag that's"), false},
4558
{14, template.HTML("<p>Hello中国 Good 好的</p>"), nil, template.HTML("<p>Hello中国 Good 好 …</p>"), false},
4659
{2, template.HTML("<p>P1</p><p>P2</p>"), nil, template.HTML("<p>P1 …</p>"), false},

0 commit comments

Comments
 (0)