Template
1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo synced 2024-11-30 22:06:11 +01:00
forgejo/modules/templates/scopedtmpl/scopedtmpl_test.go
wxiaoguang 722dab5286
Make HTML template functions support context (#24056)
# Background

Golang template is not friendly for large projects, and Golang template
team is quite slow, related:
* `https://github.com/golang/go/issues/54450`

Without upstream support, we can also have our solution to make HTML
template functions support context.

It helps a lot, the above Golang template issue `#54450` explains a lot:

1. It makes `{{Locale.Tr}}` could be used in any template, without
passing unclear `(dict "root" . )` anymore.
2. More and more functions need `context`, like `avatar`, etc, we do not
need to do `(dict "Context" $.Context)` anymore.
3. Many request-related functions could be shared by parent&children
templates, like "user setting" / "system setting"

See the test `TestScopedTemplateSetFuncMap`, one template set, two
`Execute` calls with different `CtxFunc`.

# The Solution

Instead of waiting for upstream, this PR re-uses the escaped HTML
template trees, use `AddParseTree` to add related templates/trees to a
new template instance, then the new template instance can have its own
FuncMap , the function calls in the template trees will always use the
new template's FuncMap.

`template.New` / `template.AddParseTree` / `adding-FuncMap` are all
quite fast, so the performance is not affected.

The details:

1. Make a new `html/template/Template` for `all` templates
2. Add template code to the `all` template
3. Freeze the `all` template, reset its exec func map, it shouldn't
execute any template.
4. When a router wants to render a template by its `name`
    1. Find the `name` in `all`
    2. Find all its related sub templates
3. Escape all related templates (just like what the html template
package does)
4. Add the escaped parse-trees of related templates into a new (scoped)
`text/template/Template`
    5. Add context-related func map into the new (scoped) text template
    6. Execute the new (scoped) text template
7. To improve performance, the escaped templates are cached to `template
sets`

# FAQ

## There is a `unsafe` call, is this PR unsafe?

This PR is safe. Golang has strict language definition, it's safe to do
so: https://pkg.go.dev/unsafe#Pointer (1) Conversion of a *T1 to Pointer
to *T2


## What if Golang template supports such feature in the future?

The public structs/interfaces/functions introduced by this PR is quite
simple, the code of `HTMLRender` is not changed too much. It's very easy
to switch to the official mechanism if there would be one.

## Does this PR change the template execution behavior?

No, see the tests (welcome to design more tests if it's necessary)

---------

Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: Jason Song <i@wolfogre.com>
Co-authored-by: Giteabot <teabot@gitea.io>
2023-04-20 04:08:58 -04:00

99 lines
2.3 KiB
Go

// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package scopedtmpl
import (
"bytes"
"html/template"
"strings"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestScopedTemplateSetFuncMap(t *testing.T) {
all := template.New("")
all.Funcs(template.FuncMap{"CtxFunc": func(s string) string {
return "default"
}})
_, err := all.New("base").Parse(`{{CtxFunc "base"}}`)
assert.NoError(t, err)
_, err = all.New("test").Parse(strings.TrimSpace(`
{{template "base"}}
{{CtxFunc "test"}}
{{template "base"}}
{{CtxFunc "test"}}
`))
assert.NoError(t, err)
ts, err := newScopedTemplateSet(all, "test")
assert.NoError(t, err)
// try to use different CtxFunc to render concurrently
funcMap1 := template.FuncMap{
"CtxFunc": func(s string) string {
time.Sleep(100 * time.Millisecond)
return s + "1"
},
}
funcMap2 := template.FuncMap{
"CtxFunc": func(s string) string {
time.Sleep(100 * time.Millisecond)
return s + "2"
},
}
out1 := bytes.Buffer{}
out2 := bytes.Buffer{}
wg := sync.WaitGroup{}
wg.Add(2)
go func() {
err := ts.newExecutor(funcMap1).Execute(&out1, nil)
assert.NoError(t, err)
wg.Done()
}()
go func() {
err := ts.newExecutor(funcMap2).Execute(&out2, nil)
assert.NoError(t, err)
wg.Done()
}()
wg.Wait()
assert.Equal(t, "base1\ntest1\nbase1\ntest1", out1.String())
assert.Equal(t, "base2\ntest2\nbase2\ntest2", out2.String())
}
func TestScopedTemplateSetEscape(t *testing.T) {
all := template.New("")
_, err := all.New("base").Parse(`<a href="?q={{.param}}">{{.text}}</a>`)
assert.NoError(t, err)
_, err = all.New("test").Parse(`{{template "base" .}}<form action="?q={{.param}}">{{.text}}</form>`)
assert.NoError(t, err)
ts, err := newScopedTemplateSet(all, "test")
assert.NoError(t, err)
out := bytes.Buffer{}
err = ts.newExecutor(nil).Execute(&out, map[string]string{"param": "/", "text": "<"})
assert.NoError(t, err)
assert.Equal(t, `<a href="?q=%2f">&lt;</a><form action="?q=%2f">&lt;</form>`, out.String())
}
func TestScopedTemplateSetUnsafe(t *testing.T) {
all := template.New("")
_, err := all.New("test").Parse(`<a href="{{if true}}?{{end}}a={{.param}}"></a>`)
assert.NoError(t, err)
_, err = newScopedTemplateSet(all, "test")
assert.ErrorContains(t, err, "appears in an ambiguous context within a URL")
}