Skip to content
Open
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
21 changes: 17 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ package pongo2
import (
"errors"
"fmt"
"regexp"
)

var reIdentifiers = regexp.MustCompile("^[a-zA-Z0-9_]+$")

var autoescape = true

func SetAutoescape(newValue bool) {
Expand All @@ -30,7 +27,7 @@ type Context map[string]any

func (c Context) checkForValidIdentifiers() *Error {
for k, v := range c {
if !reIdentifiers.MatchString(k) {
if !isValidIdentifier(k) {
return &Error{
Sender: "checkForValidIdentifiers",
OrigError: fmt.Errorf("context-key '%s' (value: '%+v') is not a valid identifier", k, v),
Expand All @@ -40,6 +37,22 @@ func (c Context) checkForValidIdentifiers() *Error {
return nil
}

func isValidIdentifier(s string) bool {
for i := range s {
if !isValidIdentifierChar(s[i]) {
return false
}
}
return true
}

func isValidIdentifierChar(c byte) bool {
return (c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '_'
}

// Update updates this context with the key/value-pairs from another context.
func (c Context) Update(other Context) Context {
for k, v := range other {
Expand Down