-
-
Notifications
You must be signed in to change notification settings - Fork 799
Make frontend CSS color validation Chromium-only (remove DOM style fallback) #2946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3e82a19
Initial plan
Copilot c46da47
Add CSS color validator utility and tests
Copilot 7ed95d9
Refine color validator test typing
Copilot 9c46dcb
Remove DOM fallback from color validator
Copilot d941910
add header, update copyright
sawka e3a3c73
fix nit
sawka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,54 @@ | ||||||
| // Copyright 2026, Command Line Inc. | ||||||
| // SPDX-License-Identifier: Apache-2.0 | ||||||
|
|
||||||
| 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 false; | ||||||
| } | ||||||
| return CSS.supports("color", 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") { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WARNING: Use strict equality operator Use
Suggested change
|
||||||
| throw new Error(`Invalid CSS color: ${String(color)}`); | ||||||
| } | ||||||
| const normalizedColor = color.trim(); | ||||||
| if (normalizedColor === "" || !isValidCssColor(normalizedColor)) { | ||||||
| throw new Error(`Invalid CSS color: ${color}`); | ||||||
| } | ||||||
| return getCssColorType(normalizedColor); | ||||||
| } | ||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WARNING: Use strict equality operators
Use
===instead of==and!==instead of!=for type-safe comparisons.