Skip to content

Commit cce2ff5

Browse files
authored
allow CSS Color Module Level 5 (#1454)
* allow CSS Color Module Level 5 * add tests
1 parent a063b22 commit cce2ff5

File tree

6 files changed

+406
-10
lines changed

6 files changed

+406
-10
lines changed

src/options.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {color, descending, quantile, range as rangei} from "d3";
1+
import {descending, quantile, range as rangei} from "d3";
22
import {parse as isoParse} from "isoformat";
33
import {defined} from "./defined.js";
44
import {maybeTimeInterval, maybeUtcInterval} from "./time.js";
@@ -459,20 +459,20 @@ export function isEvery(values, is) {
459459
return every;
460460
}
461461

462-
// Mostly relies on d3-color, with a few extra color keywords. Currently this
463-
// strictly requires that the value be a string; we might want to apply string
464-
// coercion here, though note that d3-color instances would need to support
465-
// valueOf to work correctly with InternMap.
462+
const namedColors = new Set("none,currentcolor,transparent,aliceblue,antiquewhite,aqua,aquamarine,azure,beige,bisque,black,blanchedalmond,blue,blueviolet,brown,burlywood,cadetblue,chartreuse,chocolate,coral,cornflowerblue,cornsilk,crimson,cyan,darkblue,darkcyan,darkgoldenrod,darkgray,darkgreen,darkgrey,darkkhaki,darkmagenta,darkolivegreen,darkorange,darkorchid,darkred,darksalmon,darkseagreen,darkslateblue,darkslategray,darkslategrey,darkturquoise,darkviolet,deeppink,deepskyblue,dimgray,dimgrey,dodgerblue,firebrick,floralwhite,forestgreen,fuchsia,gainsboro,ghostwhite,gold,goldenrod,gray,green,greenyellow,grey,honeydew,hotpink,indianred,indigo,ivory,khaki,lavender,lavenderblush,lawngreen,lemonchiffon,lightblue,lightcoral,lightcyan,lightgoldenrodyellow,lightgray,lightgreen,lightgrey,lightpink,lightsalmon,lightseagreen,lightskyblue,lightslategray,lightslategrey,lightsteelblue,lightyellow,lime,limegreen,linen,magenta,maroon,mediumaquamarine,mediumblue,mediumorchid,mediumpurple,mediumseagreen,mediumslateblue,mediumspringgreen,mediumturquoise,mediumvioletred,midnightblue,mintcream,mistyrose,moccasin,navajowhite,navy,oldlace,olive,olivedrab,orange,orangered,orchid,palegoldenrod,palegreen,paleturquoise,palevioletred,papayawhip,peachpuff,peru,pink,plum,powderblue,purple,rebeccapurple,red,rosybrown,royalblue,saddlebrown,salmon,sandybrown,seagreen,seashell,sienna,silver,skyblue,slateblue,slategray,slategrey,snow,springgreen,steelblue,tan,teal,thistle,tomato,turquoise,violet,wheat,white,whitesmoke,yellow".split(",")); // prettier-ignore
463+
464+
// Returns true if value is a valid CSS color string. This is intentionally lax
465+
// because the CSS color spec keeps growing, and we don’t need to parse these
466+
// colors—we just need to disambiguate them from column names.
466467
// https://www.w3.org/TR/SVG11/painting.html#SpecifyingPaint
468+
// https://www.w3.org/TR/css-color-5/
467469
export function isColor(value) {
468470
if (typeof value !== "string") return false;
469471
value = value.toLowerCase().trim();
470472
return (
471-
value === "none" ||
472-
value === "currentcolor" ||
473-
(value.startsWith("url(") && value.endsWith(")")) || // <funciri>, e.g. pattern or gradient
474-
(value.startsWith("var(") && value.endsWith(")")) || // CSS variable
475-
color(value) !== null
473+
/^#[0-9a-f]{3,8}$/.test(value) || // hex rgb, rgba, rrggbb, rrggbbaa
474+
/^(?:url|var|rgb|rgba|hsl|hsla|hwb|lab|lch|oklab|oklch|color|color-mix)\(.*\)$/.test(value) || // <funciri>, CSS variable, color, etc.
475+
namedColors.has(value) // currentColor, red, etc.
476476
);
477477
}
478478

test/output/varColor.svg

+124
Loading

test/output/varColor2.svg

+124
Loading

0 commit comments

Comments
 (0)