From 3a786a248d3eff6e732aa94e87d6e88196e5147a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Tue, 30 Oct 2018 17:36:05 +0100 Subject: [PATCH] tpl: Fix BOM issue in templates Fixes #4895 --- hugolib/template_test.go | 24 ++++++++++++++++++++++++ tpl/tplimpl/template.go | 19 ++++++++++++++++++- 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/hugolib/template_test.go b/hugolib/template_test.go index eed3ee8ed..9cc523cb0 100644 --- a/hugolib/template_test.go +++ b/hugolib/template_test.go @@ -212,3 +212,27 @@ Some content } } + +// https://github.com/gohugoio/hugo/issues/4895 +func TestTemplateBOM(t *testing.T) { + + b := newTestSitesBuilder(t).WithSimpleConfigFile() + bom := "\ufeff" + + b.WithTemplatesAdded( + "_default/baseof.html", bom+` + Base: {{ block "main" . }}base main{{ end }}`, + "_default/single.html", bom+`{{ define "main" }}Hi!?{{ end }}`) + + b.WithContent("page.md", `--- +title: "Page" +--- + +Page Content +`) + + b.CreateSites().Build(BuildCfg{}) + + b.AssertFileContent("public/page/index.html", "Base: Hi!?") + +} diff --git a/tpl/tplimpl/template.go b/tpl/tplimpl/template.go index 8a26ce0e1..144bafdd8 100644 --- a/tpl/tplimpl/template.go +++ b/tpl/tplimpl/template.go @@ -655,6 +655,22 @@ func (t *textTemplates) handleMaster(name, overlayFilename, masterFilename strin } +func removeLeadingBOM(s string) string { + const bom = '\ufeff' + + for i, r := range s { + if i == 0 && r != bom { + return s + } + if i > 0 { + return s[i:] + } + } + + return s + +} + func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) error { t.checkState() @@ -666,7 +682,8 @@ func (t *templateHandler) addTemplateFile(name, baseTemplatePath, path string) e if err != nil { return templateInfo{filename: filename, fs: fs}, err } - s := string(b) + + s := removeLeadingBOM(string(b)) realFilename := filename if fi, err := fs.Stat(filename); err == nil {