refactor(studio): one colour parser for the text toolbar, stricter rgb numbers

The inline text toolbar kept its own canvas fallback for named colours.
parseCssColor now resolves them, so the toolbar uses it alone.
rgb() channels must be well-formed numbers; that replaces the NaN check
after parsing and rejects rgb(1. 2 3) as CSS does.
This commit is contained in:
Miguel Ángel
2026-10-02 08:56:50 -04:00
parent a50373108f
commit 37e5e3cd21
3 changed files with 11 additions and 29 deletions
@@ -248,7 +248,7 @@ function placeOverSelection(
placeBelow,
styles,
colours,
pickerColour: toPickerColour(styles.color ?? colours[0], doc),
pickerColour: toPickerColour(styles.color ?? colours[0]),
};
}
@@ -259,25 +259,7 @@ function isBold(weight: string | undefined): boolean {
}
/** A colour input accepts only `#rrggbb`; normalise any valid CSS colour to it. */
function toPickerColour(value: string | undefined, doc: Document): string {
if (!value) return DEFAULT_COLOR;
const parsed = parseCssColor(value);
if (parsed) return toHexColor(parsed);
// Canvas delegates the full CSS colour grammar to the browser, including
// named colours that the small serialisation parser intentionally omits.
// DOM-only test environments can lack a canvas implementation, in which
// case the picker degrades to its explicit default while the swatch remains
// truthful because CSS still paints the original value.
try {
const context = doc.createElement("canvas").getContext("2d");
if (!context) return DEFAULT_COLOR;
context.fillStyle = DEFAULT_COLOR;
context.fillStyle = value;
const normalised =
typeof context.fillStyle === "string" ? parseCssColor(context.fillStyle) : null;
return normalised ? toHexColor(normalised) : DEFAULT_COLOR;
} catch {
return DEFAULT_COLOR;
}
function toPickerColour(value: string | undefined): string {
const parsed = value ? parseCssColor(value) : null;
return parsed ? toHexColor(parsed) : DEFAULT_COLOR;
}
@@ -37,7 +37,7 @@ describe("parseCssColor", () => {
expect(parseCssColor(input)).toEqual(expected);
});
it.each(["", "#12", "notacolor", "currentcolor", "none", "rgb(1..2, 3, 4)"])(
it.each(["", "#12", "notacolor", "currentcolor", "none", "rgb(1..2, 3, 4)", "rgb(1. 2 3)"])(
"rejects %s without a browser",
(input) => {
expect(parseCssColor(input)).toBeNull();
@@ -40,7 +40,7 @@ function parseSerializedColor(value: string): ParsedColor | null {
return { red: 0, green: 0, blue: 0, alpha: 0 };
}
const hex = trimmed.match(/^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i);
const hex = trimmed.match(/^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/);
if (hex) {
const digits = hex[1].length <= 4 ? [...hex[1]].map((digit) => digit + digit).join("") : hex[1];
return {
@@ -53,9 +53,11 @@ function parseSerializedColor(value: string): ParsedColor | null {
const rgba =
trimmed.match(
/^rgba?\(\s*([0-9.]+)\s*,\s*([0-9.]+)\s*,\s*([0-9.]+)(?:\s*,\s*([0-9.]+))?\s*\)$/,
/^rgba?\(\s*(\d*\.?\d+)\s*,\s*(\d*\.?\d+)\s*,\s*(\d*\.?\d+)(?:\s*,\s*(\d*\.?\d+))?\s*\)$/,
) ??
trimmed.match(/^rgba?\(\s*([\d.]+%?)\s+([\d.]+%?)\s+([\d.]+%?)(?:\s*\/\s*([\d.]+%?))?\s*\)$/);
trimmed.match(
/^rgba?\(\s*(\d*\.?\d+%?)\s+(\d*\.?\d+%?)\s+(\d*\.?\d+%?)(?:\s*\/\s*(\d*\.?\d+%?))?\s*\)$/,
);
if (rgba) {
return {
red: clampChannel(parseComponent(rgba[1], 255)),
@@ -112,9 +114,7 @@ function parseSrgbSerialization(
}
export function parseCssColor(value: string): ParsedColor | null {
const parsed = parseSerializedColor(value);
if (parsed && Object.values(parsed).every(Number.isFinite)) return parsed;
return parseBrowserColor(value.trim());
return parseSerializedColor(value) ?? parseBrowserColor(value.trim());
}
export function toColorPickerValue(value: string): string {