From 37e5e3cd21c2c2a245bac6236f20f3df3b32dbba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Fri, 2 Oct 2026 08:56:50 -0400 Subject: [PATCH] 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. --- .../components/editor/InlineTextToolbar.tsx | 26 +++---------------- .../src/components/editor/colorValue.test.ts | 2 +- .../src/components/editor/colorValue.ts | 12 ++++----- 3 files changed, 11 insertions(+), 29 deletions(-) diff --git a/packages/studio/src/components/editor/InlineTextToolbar.tsx b/packages/studio/src/components/editor/InlineTextToolbar.tsx index cc8f895490..2c0cfe697b 100644 --- a/packages/studio/src/components/editor/InlineTextToolbar.tsx +++ b/packages/studio/src/components/editor/InlineTextToolbar.tsx @@ -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; } diff --git a/packages/studio/src/components/editor/colorValue.test.ts b/packages/studio/src/components/editor/colorValue.test.ts index b3cd2eefb2..62e167744a 100644 --- a/packages/studio/src/components/editor/colorValue.test.ts +++ b/packages/studio/src/components/editor/colorValue.test.ts @@ -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(); diff --git a/packages/studio/src/components/editor/colorValue.ts b/packages/studio/src/components/editor/colorValue.ts index 4a2a6ee4ef..b76659e664 100644 --- a/packages/studio/src/components/editor/colorValue.ts +++ b/packages/studio/src/components/editor/colorValue.ts @@ -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 {