validate gradient color stops
This commit is contained in:
parent
31519e9d63
commit
4a6ab032e7
2 changed files with 18 additions and 3 deletions
|
|
@ -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)
|
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 {
|
for _, tc := range testCases {
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,8 @@ import (
|
||||||
"regexp"
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/mazznoer/csscolorparser"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Gradient struct {
|
type Gradient struct {
|
||||||
|
|
@ -241,10 +243,15 @@ func UniqueGradientID(cssGradient string) string {
|
||||||
return "grad-" + hash
|
return "grad-" + hash
|
||||||
}
|
}
|
||||||
|
|
||||||
var GradientRegex = regexp.MustCompile(`^(linear|radial)-gradient\((.+)\)$`)
|
|
||||||
|
|
||||||
func IsGradient(color string) bool {
|
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}'\)$`)
|
var URLGradientID = regexp.MustCompile(`^url\('#grad-[a-f0-9]{40}'\)$`)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue