validate gradient color stops

This commit is contained in:
melsonic 2025-04-16 00:49:12 +05:30
parent 31519e9d63
commit 4a6ab032e7
No known key found for this signature in database
GPG key ID: DFA426742F621CD7
2 changed files with 18 additions and 3 deletions

View file

@ -3929,6 +3929,14 @@ svc_1.t2 -> b: do with B
tassert.Equal(t, "d2/testdata/d2compiler/TestCompile/meow.d2", g.Layers[0].Layers[0].AST.Range.Path)
},
},
{
name: "invalid_gradient_color_stop",
text: `
x
x.style.fill: "linear-gradient(#ggg, #000)"
`,
expErr: `d2/testdata/d2compiler/TestCompile/invalid_gradient_color_stop.d2:3:19: expected "fill" to be a valid named color ("orange"), a hex code ("#f0ff3a"), or a gradient ("linear-gradient(red, blue)")`,
},
}
for _, tc := range testCases {

View file

@ -9,6 +9,8 @@ import (
"regexp"
"strconv"
"strings"
"github.com/mazznoer/csscolorparser"
)
type Gradient struct {
@ -241,10 +243,15 @@ func UniqueGradientID(cssGradient string) string {
return "grad-" + hash
}
var GradientRegex = regexp.MustCompile(`^(linear|radial)-gradient\((.+)\)$`)
func IsGradient(color string) bool {
return GradientRegex.MatchString(color)
gradient, err := ParseGradient(color)
for _, colorStop := range gradient.ColorStops {
_, err = csscolorparser.Parse(colorStop.Color)
if err != nil {
break
}
}
return err == nil
}
var URLGradientID = regexp.MustCompile(`^url\('#grad-[a-f0-9]{40}'\)$`)