diff --git a/tpl/strings/truncate.go b/tpl/strings/truncate.go index e8da7b84b50..efa91061821 100644 --- a/tpl/strings/truncate.go +++ b/tpl/strings/truncate.go @@ -18,6 +18,7 @@ import ( "html" "html/template" "regexp" + "strings" "unicode" "unicode/utf8" @@ -92,12 +93,12 @@ func (ns *Namespace) Truncate(s any, options ...any) (template.HTML, error) { } if isHTML { - // Make sure we keep tag of HTML tags + // Make sure we keep tagname of HTML tags slice := text[i:] m := tagRE.FindStringSubmatchIndex(slice) if len(m) > 0 && m[0] == 0 { nextTag = i + m[1] - tagname := slice[m[4]:m[5]] + tagname := strings.Fields(slice[m[4]:m[5]])[0] lastWordIndex = lastNonSpace _, singlet := htmlSinglets[tagname] if !singlet && m[6] == -1 { diff --git a/tpl/strings/truncate_test.go b/tpl/strings/truncate_test.go index 62125e1b4e7..e2339aa2739 100644 --- a/tpl/strings/truncate_test.go +++ b/tpl/strings/truncate_test.go @@ -47,6 +47,23 @@ func TestTruncate(t *testing.T) { {3, template.HTML(strings.Repeat("
P
", 20)), nil, template.HTML("P
P
P …
"), false}, {18, template.HTML("test hello test something
"), nil, template.HTML("test hello test …
"), false}, {4, template.HTML("abc d e
"), nil, template.HTML("abc …
"), false}, + { + 42, + template.HTML(`With strangely formatted + HTML + inside.`), + nil, + template.HTML(`With strangely formatted + HTML …`), + false, + }, {10, nil, nil, template.HTML(""), true}, {nil, nil, nil, template.HTML(""), true}, }