diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 702295099..57568e279 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -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 { diff --git a/lib/color/gradient.go b/lib/color/gradient.go index 51dbde637..7aae4d0f4 100644 --- a/lib/color/gradient.go +++ b/lib/color/gradient.go @@ -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}'\)$`)