isCssColor
Checks whether a value is a syntactically-safe, plain CSS color: a hex color
(#rgb, #rgba, #rrggbb, #rrggbbaa), a functional notation (rgb(),
rgba(), hsl(), hsla()), or a single-word named color (red,
rebeccapurple).
Intended to sanitize a color value before interpolating it into inline
style/cssText — it does not implement the full CSS color grammar or
validate named colors against the real keyword list, it only rejects
characters ({, }, ;, “) or shapes that could smuggle extra CSS
declarations into the surrounding rule.
Available since next
Import
Section titled “Import”Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
value | unknown | The value to check |
Returns
Section titled “Returns”value is string — true if value is a string that looks like a safe, plain CSS color
Examples
Section titled “Examples”Validate a color before writing it to inline style
Section titled “Validate a color before writing it to inline style”Accepts hex, functional (rgb/rgba/hsl/hsla), and named colors.
Reject values that could inject extra CSS declarations
Section titled “Reject values that could inject extra CSS declarations”Guards against untrusted data smuggling a second declaration into a style attribute.
