2023-11-08 23:04:18 +00:00
|
|
|
package safe
|
2023-11-08 19:12:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"html/template"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
2023-11-08 23:04:18 +00:00
|
|
|
type Namespace struct{}
|
|
|
|
|
2023-11-08 19:12:45 +00:00
|
|
|
var ErrSafeHTML error = errors.New("unsupported input type for SafeHTML")
|
|
|
|
|
2023-11-08 23:04:18 +00:00
|
|
|
func New() *Namespace {
|
|
|
|
return &Namespace{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Namespace) HTML(v reflect.Value) (template.HTML, error) {
|
2023-11-08 19:12:45 +00:00
|
|
|
switch v.Kind() {
|
|
|
|
default:
|
|
|
|
return "", ErrSafeHTML
|
|
|
|
case reflect.Slice:
|
2023-11-09 03:01:34 +00:00
|
|
|
switch v.Type().Elem().Kind() {
|
|
|
|
default:
|
2023-11-08 23:04:18 +00:00
|
|
|
return "", ErrSafeHTML
|
2023-11-09 03:01:34 +00:00
|
|
|
case reflect.Uint8:
|
|
|
|
return template.HTML(v.Bytes()), nil
|
2023-11-08 23:04:18 +00:00
|
|
|
}
|
2023-11-08 19:12:45 +00:00
|
|
|
case reflect.String:
|
|
|
|
return template.HTML(v.String()), nil
|
|
|
|
}
|
|
|
|
}
|