github.com/shohhei1126/hugo@v0.42.2-0.20180623210752-3d5928889ad7/tpl/tplimpl/template_test.go (about)

     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/stretchr/testify/require"
    22  )
    23  
    24  type handler interface {
    25  	addTemplate(name, tpl string) error
    26  }
    27  
    28  // #3876
    29  func TestHTMLEscape(t *testing.T) {
    30  	assert := require.New(t)
    31  
    32  	data := map[string]string{
    33  		"html":  "<h1>Hi!</h1>",
    34  		"other": "<h1>Hi!</h1>",
    35  	}
    36  	v := newTestConfig()
    37  	fs := hugofs.NewMem(v)
    38  
    39  	//afero.WriteFile(fs.Source, filepath.Join(workingDir, "README.txt"), []byte("Hugo Rocks!"), 0755)
    40  
    41  	depsCfg := newDepsConfig(v)
    42  	depsCfg.Fs = fs
    43  	d, err := deps.New(depsCfg)
    44  	assert.NoError(err)
    45  
    46  	tpl := `{{ "<h1>Hi!</h1>" | safeHTML }}`
    47  
    48  	provider := DefaultTemplateProvider
    49  	provider.Update(d)
    50  
    51  	h := d.Tmpl.(handler)
    52  
    53  	assert.NoError(h.addTemplate("shortcodes/myShort.html", tpl))
    54  
    55  	s, err := d.Tmpl.Lookup("shortcodes/myShort.html").ExecuteToString(data)
    56  	assert.NoError(err)
    57  	assert.Contains(s, "<h1>Hi!</h1>")
    58  
    59  	s, err = d.Tmpl.Lookup("shortcodes/myShort").ExecuteToString(data)
    60  	assert.NoError(err)
    61  	assert.Contains(s, "<h1>Hi!</h1>")
    62  
    63  }