{"record":{"id":"28de4e29cfccdc36","repo":"d2lang/d2","slug":"invalid-gradient-syntax","errorCode":null,"errorMessage":"invalid gradient syntax","messagePattern":"invalid gradient syntax","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/color/gradient.go","lineNumber":32,"sourceCode":"type Gradient struct {\n\tType       string\n\tDirection  string\n\tColorStops []ColorStop\n\tID         string\n}\n\ntype ColorStop struct {\n\tColor    string\n\tPosition string\n}\n\nfunc ParseGradient(cssGradient string) (Gradient, error) {\n\tcssGradient = strings.TrimSpace(cssGradient)\n\n\tre := regexp.MustCompile(`^(linear-gradient|radial-gradient)\\((.*)\\)$`)\n\tmatches := re.FindStringSubmatch(cssGradient)\n\tif matches == nil {\n\t\treturn Gradient{}, errors.New(\"invalid gradient syntax\")\n\t}\n\n\tgradientType := matches[1]\n\tparams := matches[2]\n\n\tgradient := Gradient{\n\t\tType: strings.TrimSuffix(gradientType, \"-gradient\"),\n\t}\n\n\tparamList := splitParams(params)\n\n\tif len(paramList) == 0 {\n\t\treturn Gradient{}, errors.New(\"no parameters in gradient\")\n\t}\n\n\tfirstParam := strings.TrimSpace(paramList[0])\n\n\tif gradient.Type == \"linear\" && (strings.HasSuffix(firstParam, \"deg\") || strings.HasPrefix(firstParam, \"to \")) {","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/d2lang/d2/blob/0d69dca6f532ceaeacd615d35d1eaa41a238ffdb/lib/color/gradient.go#L14-L50","documentation":"lib/color.ParseGradient parses CSS gradient strings like 'linear-gradient(...)' or 'radial-gradient(...)'. If the trimmed input does not match the required outer syntax — the gradient function name with parentheses wrapping parameters — it returns 'invalid gradient syntax'. The parser is strict: it accepts only these two gradient types with a balanced parenthesized parameter list.","triggerScenarios":"Calling ParseGradient (directly or indirectly through ValidColor or color parsing of a style value) with a string that is not of the form 'linear-gradient(...)' or 'radial-gradient(...)', e.g. 'conic-gradient(red, blue)', 'linear-gradient(red, blue' (unbalanced parens), or extra text outside the parens.","commonSituations":"D2 style values using unsupported gradient types like conic-gradient; typos in the gradient function name; trailing semicolons or whitespace/newline artifacts; copying gradients from other CSS contexts that include vendor prefixes or surrounding declarations.","solutions":["Use only linear-gradient(...) or radial-gradient(...) syntax as the color value","Trim whitespace and remove any trailing punctuation (e.g. ';') from the value before parsing","Validate the string with lib/color.ValidColor (which uses ParseGradient) before assigning it to a style"],"exampleFix":"// before\ngrad, err := color.ParseGradient(\"conic-gradient(from 0deg, red, blue)\") // invalid syntax\n// after\ngrad, err := color.ParseGradient(\"linear-gradient(to right, red, blue)\")","handlingStrategy":"validation","validationCode":"re := regexp.MustCompile(`^(linear-gradient|radial-gradient)\\(.*\\)$`)\nif !re.MatchString(strings.TrimSpace(cssGradient)) {\n    return errors.New(\"not a supported linear/radial gradient\")\n}","typeGuard":"func isSupportedGradient(s string) bool {\n    s = strings.TrimSpace(s)\n    return regexp.MustCompile(`^(linear-gradient|radial-gradient)\\(.*\\)$`).MatchString(s)\n}","tryCatchPattern":"grad, err := color.ParseGradient(input)\nif err != nil && strings.Contains(err.Error(), \"invalid gradient syntax\") {\n    // fall back to a solid color\n}","preventionTips":["Only emit linear-gradient/radial-gradient values into style fields","Trim whitespace and stray punctuation before parsing","Prefer ValidColor to validate any custom color/gradient input"],"tags":["color","gradient","parsing"],"backgroundTag":"invalid-gradient-syntax","analyzedSha":"0d69dca6f532ceaeacd615d35d1eaa41a238ffdb","analyzedAt":"2026-08-31T12:19:21.182Z","schemaVersion":2},"datasetVersion":"2026-08-31T14:17:45.589Z"}