Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text/template: document function name rules and refactor goodName #70943

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/html/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,11 @@ type FuncMap = template.FuncMap
// Funcs adds the elements of the argument map to the template's function map.
// It must be called before the template is parsed.
// It panics if a value in the map is not a function with appropriate return
// type. However, it is legal to overwrite elements of the map. The return
// value is the template, so calls can be chained.
// type or if the name cannot be used syntactically as a function in a template.
// A valid function name is a sequence of letters, digits, and underscores,
// starting with a letter or an underscore.
// It is legal to overwrite elements of the map. The return value is the template,
// so calls can be chained.
func (t *Template) Funcs(funcMap FuncMap) *Template {
t.text.Funcs(template.FuncMap(funcMap))
return t
Expand Down
6 changes: 3 additions & 3 deletions src/text/template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ func goodName(name string) bool {
}
for i, r := range name {
switch {
case unicode.IsLetter(r):
case r == '_':
case i == 0 && !unicode.IsLetter(r):
return false
case !unicode.IsLetter(r) && !unicode.IsDigit(r):
case i > 0 && unicode.IsDigit(r):
default:
return false
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/text/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func (t *Template) Delims(left, right string) *Template {
// It must be called before the template is parsed.
// It panics if a value in the map is not a function with appropriate return
// type or if the name cannot be used syntactically as a function in a template.
// A valid function name is a sequence of letters, digits, and underscores,
// starting with a letter or an underscore.
// It is legal to overwrite elements of the map. The return value is the template,
// so calls can be chained.
func (t *Template) Funcs(funcMap FuncMap) *Template {
Expand Down