Better convention for safe template types

This commit is contained in:
Svilen Markov 2024-12-16 13:58:58 +00:00
parent 80a957bc5b
commit ae1fb05607
4 changed files with 10 additions and 9 deletions

View File

@ -2,7 +2,6 @@ package glance
import ( import (
"fmt" "fmt"
"html/template"
"os" "os"
"regexp" "regexp"
"strconv" "strconv"
@ -30,10 +29,6 @@ func (c *hslColorField) String() string {
return fmt.Sprintf("hsl(%d, %d%%, %d%%)", c.Hue, c.Saturation, c.Lightness) return fmt.Sprintf("hsl(%d, %d%%, %d%%)", c.Hue, c.Saturation, c.Lightness)
} }
func (c *hslColorField) AsCSSValue() template.CSS {
return template.CSS(c.String())
}
func (c *hslColorField) UnmarshalYAML(node *yaml.Node) error { func (c *hslColorField) UnmarshalYAML(node *yaml.Node) error {
var value string var value string

View File

@ -16,6 +16,12 @@ var intl = message.NewPrinter(language.English)
var globalTemplateFunctions = template.FuncMap{ var globalTemplateFunctions = template.FuncMap{
"formatViewerCount": formatViewerCount, "formatViewerCount": formatViewerCount,
"formatNumber": intl.Sprint, "formatNumber": intl.Sprint,
"safeCSS": func(str string) template.CSS {
return template.CSS(str)
},
"safeURL": func(str string) template.URL {
return template.URL(str)
},
"absInt": func(i int) int { "absInt": func(i int) int {
return int(math.Abs(float64(i))) return int(math.Abs(float64(i)))
}, },

View File

@ -3,7 +3,7 @@
{{ define "widget-content" }} {{ define "widget-content" }}
<div class="dynamic-columns list-gap-24 list-with-separator"> <div class="dynamic-columns list-gap-24 list-with-separator">
{{ range .Groups }} {{ range .Groups }}
<div class="bookmarks-group"{{ if .Color }} style="--bookmarks-group-color: {{ .Color.AsCSSValue }}"{{ end }}> <div class="bookmarks-group"{{ if .Color }} style="--bookmarks-group-color: {{ .Color.String | safeCSS }}"{{ end }}>
{{ if ne .Title "" }}<div class="bookmarks-group-title size-h3 margin-bottom-3">{{ .Title }}</div>{{ end }} {{ if ne .Title "" }}<div class="bookmarks-group-title size-h3 margin-bottom-3">{{ .Title }}</div>{{ end }}
<ul class="list list-gap-2"> <ul class="list list-gap-2">
{{ range .Links }} {{ range .Links }}

View File

@ -7,8 +7,8 @@
{{ end }} {{ end }}
{{ if ne 0.0 .ContrastMultiplier }}--cm: {{ .ContrastMultiplier }};{{ end }} {{ if ne 0.0 .ContrastMultiplier }}--cm: {{ .ContrastMultiplier }};{{ end }}
{{ if ne 0.0 .TextSaturationMultiplier }}--tsm: {{ .TextSaturationMultiplier }};{{ end }} {{ if ne 0.0 .TextSaturationMultiplier }}--tsm: {{ .TextSaturationMultiplier }};{{ end }}
{{ if .PrimaryColor }}--color-primary: {{ .PrimaryColor.AsCSSValue }};{{ end }} {{ if .PrimaryColor }}--color-primary: {{ .PrimaryColor.String | safeCSS }};{{ end }}
{{ if .PositiveColor }}--color-positive: {{ .PositiveColor.AsCSSValue }};{{ end }} {{ if .PositiveColor }}--color-positive: {{ .PositiveColor.String | safeCSS }};{{ end }}
{{ if .NegativeColor }}--color-negative: {{ .NegativeColor.AsCSSValue }};{{ end }} {{ if .NegativeColor }}--color-negative: {{ .NegativeColor.String | safeCSS }};{{ end }}
} }
</style> </style>