Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions frontend/util/color-validator.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { validateCssColor } from "./color-validator";

describe("validateCssColor", () => {
beforeEach(() => {
vi.stubGlobal("CSS", {
supports: (_property: string, value: string) => {
return [
"red",
"#aabbcc",
"#aabbccdd",
"rgb(255, 0, 0)",
"rgba(255, 0, 0, 0.5)",
"hsl(120 100% 50%)",
"transparent",
"currentColor",
].includes(value);
},
});
});

afterEach(() => {
vi.unstubAllGlobals();
});

it("returns type for supported CSS color formats", () => {
expect(validateCssColor("red")).toBe("keyword");
expect(validateCssColor("#aabbcc")).toBe("hex");
expect(validateCssColor("#aabbccdd")).toBe("hex8");
expect(validateCssColor("rgb(255, 0, 0)")).toBe("rgb");
expect(validateCssColor("rgba(255, 0, 0, 0.5)")).toBe("rgba");
expect(validateCssColor("hsl(120 100% 50%)")).toBe("hsl");
expect(validateCssColor("transparent")).toBe("transparent");
expect(validateCssColor("currentColor")).toBe("currentcolor");
});

it("throws for invalid CSS colors", () => {
expect(() => validateCssColor(":not-a-color:")).toThrow("Invalid CSS color");
expect(() => validateCssColor("#12")).toThrow("Invalid CSS color");
expect(() => validateCssColor("rgb(255, 0)")).toThrow("Invalid CSS color");
});
});
57 changes: 57 additions & 0 deletions frontend/util/color-validator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const HexColorRegex = /^#([\da-f]{3}|[\da-f]{4}|[\da-f]{6}|[\da-f]{8})$/i;
const FunctionalColorRegex = /^([a-z-]+)\(/i;
const NamedColorRegex = /^[a-z]+$/i;

function isValidCssColor(color: string): boolean {
if (typeof CSS != "undefined" && typeof CSS.supports == "function") {
return CSS.supports("color", color);
}
if (typeof document == "undefined") {
return false;
}
const temp = document.createElement("div");
temp.style.color = "";
temp.style.color = color;
return temp.style.color != "";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Use strict equality operator

Should use !== instead of != when comparing to empty string. The project uses != only for null/undefined checks, not for string comparisons.

Suggested change
return temp.style.color != "";
return temp.style.color !== "";

}

function getCssColorType(color: string): string {
const normalizedColor = color.toLowerCase();
if (HexColorRegex.test(normalizedColor)) {
if (normalizedColor.length === 4) {
return "hex3";
}
if (normalizedColor.length === 5) {
return "hex4";
}
if (normalizedColor.length === 9) {
return "hex8";
}
return "hex";
}
if (normalizedColor === "transparent") {
return "transparent";
}
if (normalizedColor === "currentcolor") {
return "currentcolor";
}
const functionMatch = normalizedColor.match(FunctionalColorRegex);
if (functionMatch) {
return functionMatch[1];
}
if (NamedColorRegex.test(normalizedColor)) {
return "keyword";
}
return "color";
}

export function validateCssColor(color: string): string {
if (typeof color != "string") {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Use strict equality operator

Use !== instead of != for type-safe comparison.

Suggested change
if (typeof color != "string") {
if (typeof color !== "string") {

throw new Error(`Invalid CSS color: ${String(color)}`);
}
const normalizedColor = color.trim();
if (normalizedColor == "" || !isValidCssColor(normalizedColor)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WARNING: Use strict equality operator

Should use === instead of == when comparing to empty string. The project uses == only for null/undefined checks, not for string comparisons.

Suggested change
if (normalizedColor == "" || !isValidCssColor(normalizedColor)) {
if (normalizedColor === "" || !isValidCssColor(normalizedColor)) {

throw new Error(`Invalid CSS color: ${color}`);
}
return getCssColorType(normalizedColor);
}
Loading