Skip to content

Commit 2d613dd

Browse files
authored
tpl/tplimpl: Fix escaped HTML Go 1.9 multioutput issue (#3880)
Fixes #3876
1 parent 642ba6c commit 2d613dd

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

‎hugolib/site_output_test.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ outputs: %s
125125
# Doc
126126
127127
{{< myShort >}}
128+
129+
{{< myOtherShort >}}
130+
128131
`
129132

130133
mf := afero.NewMemMapFs()
@@ -144,6 +147,7 @@ other = "Olboge"
144147
"layouts/partials/GoHugo.html", `Go Hugo Partial`,
145148
"layouts/_default/baseof.json", `START JSON:{{block "main" .}}default content{{ end }}:END JSON`,
146149
"layouts/_default/baseof.html", `START HTML:{{block "main" .}}default content{{ end }}:END HTML`,
150+
"layouts/shortcodes/myOtherShort.html", `OtherShort: {{ "<h1>Hi!</h1>" | safeHTML }}`,
147151
"layouts/shortcodes/myShort.html", `ShortHTML`,
148152
"layouts/shortcodes/myShort.json", `ShortJSON`,
149153

@@ -210,6 +214,7 @@ Content: {{ .Content }}
210214
"Output/Rel: HTML/canonical|",
211215
"en: Elbow",
212216
"ShortJSON",
217+
"OtherShort: <h1>Hi!</h1>",
213218
)
214219

215220
th.assertFileContent("public/index.html",
@@ -218,6 +223,7 @@ Content: {{ .Content }}
218223
`List HTML|JSON Home|<atom:link href=http://example.com/blog/ rel="self" type="text/html&#43;html" />`,
219224
"en: Elbow",
220225
"ShortHTML",
226+
"OtherShort: <h1>Hi!</h1>",
221227
)
222228
th.assertFileContent("public/nn/index.html",
223229
"List HTML|JSON Nynorsk Heim|",
@@ -228,6 +234,7 @@ Content: {{ .Content }}
228234
// JSON is plain text, so no need to safeHTML this and that
229235
`<atom:link href=http://example.com/blog/index.json rel="self" type="application/json+json" />`,
230236
"ShortJSON",
237+
"OtherShort: <h1>Hi!</h1>",
231238
)
232239
th.assertFileContent("public/nn/index.json",
233240
"List JSON|JSON Nynorsk Heim|",

‎tpl/tplimpl/template.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,8 @@ func (t *htmlTemplates) addTemplateIn(tt *template.Template, name, tpl string) e
305305
// We need to keep track of one ot the output format's shortcode template
306306
// without knowing the rendering context.
307307
withoutExt := strings.TrimSuffix(name, path.Ext(name))
308-
tt.AddParseTree(withoutExt, templ.Tree)
308+
clone := template.Must(templ.Clone())
309+
tt.AddParseTree(withoutExt, clone.Tree)
309310
}
310311

311312
return nil
@@ -334,7 +335,8 @@ func (t *textTemplates) addTemplateIn(tt *texttemplate.Template, name, tpl strin
334335
// We need to keep track of one ot the output format's shortcode template
335336
// without knowing the rendering context.
336337
withoutExt := strings.TrimSuffix(name, path.Ext(name))
337-
tt.AddParseTree(withoutExt, templ.Tree)
338+
clone := texttemplate.Must(templ.Clone())
339+
tt.AddParseTree(withoutExt, clone.Tree)
338340
}
339341

340342
return nil

‎tpl/tplimpl/template_test.go‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2017-present The Hugo Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package tplimpl
15+
16+
import (
17+
"testing"
18+
19+
"github.com/gohugoio/hugo/deps"
20+
"github.com/gohugoio/hugo/hugofs"
21+
"github.com/spf13/viper"
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
type handler interface {
26+
addTemplate(name, tpl string) error
27+
}
28+
29+
// #3876
30+
func TestHTMLEscape(t *testing.T) {
31+
assert := require.New(t)
32+
33+
data := map[string]string{
34+
"html": "<h1>Hi!</h1>",
35+
"other": "<h1>Hi!</h1>",
36+
}
37+
v := viper.New()
38+
fs := hugofs.NewMem(v)
39+
40+
//afero.WriteFile(fs.Source, filepath.Join(workingDir, "README.txt"), []byte("Hugo Rocks!"), 0755)
41+
42+
depsCfg := newDepsConfig(v)
43+
depsCfg.Fs = fs
44+
d, err := deps.New(depsCfg)
45+
assert.NoError(err)
46+
47+
tpl := `{{ "<h1>Hi!</h1>" | safeHTML }}`
48+
49+
provider := DefaultTemplateProvider
50+
provider.Update(d)
51+
52+
h := d.Tmpl.(handler)
53+
54+
assert.NoError(h.addTemplate("shortcodes/myShort.html", tpl))
55+
56+
s, err := d.Tmpl.Lookup("shortcodes/myShort.html").ExecuteToString(data)
57+
assert.NoError(err)
58+
assert.Contains(s, "<h1>Hi!</h1>")
59+
60+
s, err = d.Tmpl.Lookup("shortcodes/myShort").ExecuteToString(data)
61+
assert.NoError(err)
62+
assert.Contains(s, "<h1>Hi!</h1>")
63+
64+
}

0 commit comments

Comments
 (0)