Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 5 additions & 2 deletions mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -1151,16 +1151,19 @@ var matchers = (function () {
")[,|\\s]+(" +
CSS_UNIT +
")\\s*\\)?";
// Fourth value (alpha) is optional so rgba()/hsla()/hsva() match CSS
// Color 4, where rgb/rgba and hsl/hsla are aliases. Slash is allowed as
// the separator before alpha (`rgba(255 0 0 / 0.5)`).
var PERMISSIVE_MATCH4 =
"[\\s|\\(]+(" +
CSS_UNIT +
")[,|\\s]+(" +
CSS_UNIT +
")[,|\\s]+(" +
CSS_UNIT +
")[,|\\s]+(" +
")(?:[,|\\s/]+(" +
CSS_UNIT +
")\\s*\\)?";
"))?\\s*\\)?";

return {
CSS_UNIT: new RegExp(CSS_UNIT),
Expand Down
30 changes: 30 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,19 @@ Deno.test("RGB Text Parsing", function () {
tinycolor.equals({ r: 200, g: 100, b: 0, a: 0.4 }, "rgba 200 100 0 .4")
);
assert(!tinycolor.equals({ r: 199, g: 100, b: 0 }, "rgba 200 100 0 1"));
assert(
tinycolor.equals({ r: 200, g: 100, b: 0 }, "rgba(200, 100, 0)"),
"rgba without alpha"
);
assert(tinycolor.equals({ r: 200, g: 100, b: 0 }, "rgba 200 100 0"));
assert(
tinycolor("rgba(255, 0, 0)").isValid(),
"rgba without alpha is valid"
);
assert(
tinycolor.equals({ r: 255, g: 0, b: 0, a: 0.5 }, "rgba(255 0 0 / 0.5)"),
"rgba with slash-separated alpha"
);

assert(!tinycolor.equals({ r: 199, g: 100, b: 0 }, "rgb(200, 100, 0)"));
assert(!tinycolor.equals({ r: 199, g: 100, b: 0 }, "rgb 200 100 0"));
Expand Down Expand Up @@ -411,6 +424,13 @@ Deno.test("Percentage RGB Text Parsing", function () {
"rgba 90% 45% 0% .4"
)
);
assert(
tinycolor.equals(
{ r: "90%", g: "45%", b: "0%" },
"rgba(90%, 45%, 0%)"
),
"percentage rgba without alpha"
);
assert(
!tinycolor.equals({ r: "89%", g: "45%", b: "0%" }, "rgba 90% 45% 0% 1")
);
Expand Down Expand Up @@ -482,6 +502,11 @@ Deno.test("HSL parsing", function () {
"hsl(100, 20%, 10%)",
"problematic hsl"
);
assertEquals(
tinycolor("hsla(251, 100%, 38%)").toHslString(),
"hsl(251, 100%, 38%)",
"hsla without alpha"
);
});

Deno.test("Hex Parsing", function () {
Expand Down Expand Up @@ -511,6 +536,11 @@ Deno.test("HSV Parsing", function () {
tinycolor("hsva 251.1 0.887 0.918 0.5").toHsvString(),
"hsva(251, 89%, 92%, 0.5)"
);
assertEquals(
tinycolor("hsva 251.1 0.887 0.918").toHsvString(),
"hsv(251, 89%, 92%)",
"hsva without alpha"
);
});

Deno.test("Invalid Parsing", function () {
Expand Down