Drop narration, history and restated-code comments across the repo, keep short ones for security boundaries, upstream quirks and ordering constraints, and record the convention in CLAUDE.md. Co-Authored-By: Claude Opus 5.5 <[email protected]>
52 lines
1.8 KiB
Go
52 lines
1.8 KiB
Go
package graph
|
|
|
|
// Hand-written helpers for background.resolvers.go, kept out of the resolver
|
|
// file for the same reason as box_helpers.go.
|
|
|
|
import (
|
|
"errors"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"adam-french.co.uk/backend/graph/model"
|
|
"adam-french.co.uk/backend/models"
|
|
)
|
|
|
|
// These go into a style binding; a quote or bracket in the image could still
|
|
// end url() early, hence the narrow shapes.
|
|
var (
|
|
// A hex colour, a colour name, or one of the colour functions — named,
|
|
// so that url() and friends don't pass as a "function".
|
|
bgColorPattern = regexp.MustCompile(`^(#[0-9a-fA-F]{3,8}|[a-zA-Z]+|(rgba?|hsla?|hwb|lab|lch|oklab|oklch)\([0-9a-z.,%/ -]*\))$`)
|
|
// A site path (an upload, /img/...) or an http(s) URL, with no quotes,
|
|
// brackets, backslashes or spaces.
|
|
bgImagePattern = regexp.MustCompile(`^(/|https?://)[^\s"'()\\<>]*$`)
|
|
)
|
|
|
|
const maxBgImageLen = 1000
|
|
|
|
var bgFits = map[string]bool{"cover": true, "contain": true, "tile": true}
|
|
|
|
// backgroundFrom validates input into a Background for page.
|
|
func backgroundFrom(page string, input model.BackgroundInput) (models.Background, error) {
|
|
bg := models.Background{
|
|
Page: page,
|
|
Color: strings.TrimSpace(input.Color),
|
|
Image: strings.TrimSpace(input.Image),
|
|
Fit: input.Fit,
|
|
}
|
|
if !validPageKey(page) {
|
|
return bg, errors.New("a background needs a valid page")
|
|
}
|
|
if bg.Color != "" && (len(bg.Color) > 64 || !bgColorPattern.MatchString(bg.Color)) {
|
|
return bg, errors.New(`colour must be a CSS colour such as #1b110e, black or rgb(0 0 0 / 50%)`)
|
|
}
|
|
if bg.Image != "" && (len(bg.Image) > maxBgImageLen || !bgImagePattern.MatchString(bg.Image)) {
|
|
return bg, errors.New("image must be a path on this site (e.g. /uploads/...) or an http(s) URL, without spaces or quotes")
|
|
}
|
|
if !bgFits[bg.Fit] {
|
|
return bg, errors.New(`fit must be "cover", "contain" or "tile"`)
|
|
}
|
|
return bg, nil
|
|
}
|