summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorVictor Kropp <[email protected]>2017-10-13 22:57:52 +0200
committerBjørn Erik Pedersen <[email protected]>2017-12-16 23:59:36 +0100
commit22cd89adc4792a3b55389d38acd4acfae3786775 (patch)
treef77c52ce94fbbe2caa25cdb40b71f98ebdc6bdc2
parentdb4b7a5c6742c75f9cd9627d3b054d3a72802ec8 (diff)
downloadhugo-22cd89adc4792a3b55389d38acd4acfae3786775.tar.gz
hugo-22cd89adc4792a3b55389d38acd4acfae3786775.zip
Make chomp return the type it receives
fixes #2187
-rw-r--r--tpl/strings/strings.go11
-rw-r--r--tpl/strings/strings_test.go18
2 files changed, 21 insertions, 8 deletions
diff --git a/tpl/strings/strings.go b/tpl/strings/strings.go
index f8acbfe2b..6d423391f 100644
--- a/tpl/strings/strings.go
+++ b/tpl/strings/strings.go
@@ -78,13 +78,20 @@ func (ns *Namespace) CountWords(s interface{}) (int, error) {
}
// Chomp returns a copy of s with all trailing newline characters removed.
-func (ns *Namespace) Chomp(s interface{}) (template.HTML, error) {
+func (ns *Namespace) Chomp(s interface{}) (interface{}, error) {
ss, err := cast.ToStringE(s)
if err != nil {
return "", err
}
- return template.HTML(_strings.TrimRight(ss, "\r\n")), nil
+ res := _strings.TrimRight(ss, "\r\n")
+ switch s.(type) {
+ case template.HTML:
+ return template.HTML(res), nil
+ default:
+ return res, nil
+ }
+
}
// Contains reports whether substr is in s.
diff --git a/tpl/strings/strings_test.go b/tpl/strings/strings_test.go
index 3ab73392c..91e71fd01 100644
--- a/tpl/strings/strings_test.go
+++ b/tpl/strings/strings_test.go
@@ -19,6 +19,7 @@ import (
"testing"
"github.com/gohugoio/hugo/deps"
+ "github.com/spf13/cast"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -35,12 +36,12 @@ func TestChomp(t *testing.T) {
s interface{}
expect interface{}
}{
- {"\n a\n", template.HTML("\n a")},
- {"\n a\n\n", template.HTML("\n a")},
- {"\n a\r\n", template.HTML("\n a")},
- {"\n a\n\r\n", template.HTML("\n a")},
- {"\n a\r\r", template.HTML("\n a")},
- {"\n a\r", template.HTML("\n a")},
+ {"\n a\n", "\n a"},
+ {"\n a\n\n", "\n a"},
+ {"\n a\r\n", "\n a"},
+ {"\n a\n\r\n", "\n a"},
+ {"\n a\r\r", "\n a"},
+ {"\n a\r", "\n a"},
// errors
{tstNoStringer{}, false},
} {
@@ -55,6 +56,11 @@ func TestChomp(t *testing.T) {
require.NoError(t, err, errMsg)
assert.Equal(t, test.expect, result, errMsg)
+
+ // repeat the check with template.HTML input
+ result, err = ns.Chomp(template.HTML(cast.ToString(test.s)))
+ require.NoError(t, err, errMsg)
+ assert.Equal(t, template.HTML(cast.ToString(test.expect)), result, errMsg)
}
}