Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

fix: allow users to use hex and rgba values in css-filters (#3356)

Jaya Krishna Namburuf1aa8db94620
diff --git a/packages/css-data/src/parse-css-value.test.ts b/packages/css-data/src/parse-css-value.test.ts
index f3a3ec0bae..e611f73c39 100644
--- a/packages/css-data/src/parse-css-value.test.ts
+++ b/packages/css-data/src/parse-css-value.test.ts
@@ -69,34 +69,12 @@ describe("Parse CSS value", () => {
describe("Unparesd valid values", () => {
test("Simple valid function values", () => {
- expect(parseCssValue("filter", "blur(4px)")).toEqual({
- type: "unparsed",
- value: "blur(4px)",
- });
-
expect(parseCssValue("width", "calc(4px + 16em)")).toEqual({
type: "unparsed",
value: "calc(4px + 16em)",
});
});
- test("Multiple valid function values", () => {
- expect(
- parseCssValue(
- "filter",
- "blur(4px) drop-shadow(16px 16px 20px blue) opacity(25%)"
- )
- ).toEqual({
- type: "unparsed",
- value: "blur(4px) drop-shadow(16px 16px 20px blue) opacity(25%)",
- });
-
- expect(parseCssValue("filter", "blur(calc(4px + 16em))")).toEqual({
- type: "unparsed",
- value: "blur(calc(4px + 16em))",
- });
- });
-
test("Invalid function values", () => {
expect(parseCssValue("width", "blur(4)")).toEqual({
type: "invalid",
diff --git a/packages/css-data/src/property-parsers/filter.test.ts b/packages/css-data/src/property-parsers/filter.test.ts
new file mode 100644
index 0000000000..4b5b5a3a95
--- /dev/null
+++ b/packages/css-data/src/property-parsers/filter.test.ts
@@ -0,0 +1,196 @@
+import { describe, expect, test } from "@jest/globals";
+import { parseFilter } from "./filter";
+
+describe("parse filter", () => {
+ test("parse values and returns the valid style property values", () => {
+ expect(parseFilter("blur(4px)")).toMatchInlineSnapshot(`
+{
+ "type": "tuple",
+ "value": [
+ {
+ "args": {
+ "type": "tuple",
+ "value": [
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 4,
+ },
+ ],
+ },
+ "name": "blur",
+ "type": "function",
+ },
+ ],
+}
+`);
+
+ expect(parseFilter("drop-shadow(10px 10px 25px rgba(0, 0, 255, 1))"))
+ .toMatchInlineSnapshot(`
+{
+ "type": "tuple",
+ "value": [
+ {
+ "args": {
+ "type": "tuple",
+ "value": [
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 10,
+ },
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 10,
+ },
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 25,
+ },
+ {
+ "alpha": 1,
+ "b": 255,
+ "g": 0,
+ "r": 0,
+ "type": "rgb",
+ },
+ ],
+ },
+ "name": "drop-shadow",
+ "type": "function",
+ },
+ ],
+}
+`);
+
+ expect(parseFilter("drop-shadow(10px 10px 25px #0000FF)"))
+ .toMatchInlineSnapshot(`
+{
+ "type": "tuple",
+ "value": [
+ {
+ "args": {
+ "type": "tuple",
+ "value": [
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 10,
+ },
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 10,
+ },
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 25,
+ },
+ {
+ "alpha": 1,
+ "b": 255,
+ "g": 0,
+ "r": 0,
+ "type": "rgb",
+ },
+ ],
+ },
+ "name": "drop-shadow",
+ "type": "function",
+ },
+ ],
+}
+`);
+ });
+
+ test("Multiple valid function values", () => {
+ expect(
+ parseFilter("blur(4px) drop-shadow(16px 16px 20px blue) opacity(25%)")
+ ).toMatchInlineSnapshot(`
+{
+ "type": "tuple",
+ "value": [
+ {
+ "args": {
+ "type": "tuple",
+ "value": [
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 4,
+ },
+ ],
+ },
+ "name": "blur",
+ "type": "function",
+ },
+ {
+ "args": {
+ "type": "tuple",
+ "value": [
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 16,
+ },
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 16,
+ },
+ {
+ "type": "unit",
+ "unit": "px",
+ "value": 20,
+ },
+ {
+ "type": "keyword",
+ "value": "blue",
+ },
+ ],
+ },
+ "name": "drop-shadow",
+ "type": "function",
+ },
+ {
+ "args": {
+ "type": "tuple",
+ "value": [
+ {
+ "type": "unit",
+ "unit": "%",
+ "value": 25,
+ },
+ ],
+ },
+ "name": "opacity",
+ "type": "function",
+ },
+ ],
+}
+`);
+ });
+
+ // parsers are used to use copied value. At the moment, we don't have support
+ // for complex functions in the UI like the one below like calc(4px + 16em)
+ test("Using complex functions inside filter function", () => {
+ expect(parseFilter("blur(calc(4px + 16em))")).toMatchInlineSnapshot(`
+{
+ "type": "tuple",
+ "value": [
+ {
+ "args": {
+ "type": "tuple",
+ "value": [],
+ },
+ "name": "blur",
+ "type": "function",
+ },
+ ],
+}
+`);
+ });
+});
diff --git a/packages/css-data/src/property-parsers/filter.ts b/packages/css-data/src/property-parsers/filter.ts
index 62d424da3e..e288978201 100644
--- a/packages/css-data/src/property-parsers/filter.ts
+++ b/packages/css-data/src/property-parsers/filter.ts
@@ -6,6 +6,7 @@ import type {
Unit,
} from "@webstudio-is/css-engine";
import { cssTryParseValue, isValidDeclaration } from "../parse-css-value";
+import { colord } from "colord";
/*
https://github.com/webstudio-is/webstudio/issues/1016
@@ -82,6 +83,20 @@ export const parseFilter = (input: string): TupleValue | InvalidValue => {
value: arg.value,
});
}
+
+ if (arg.type === "Function" || arg.type === "Hash") {
+ const colorValue = colord(csstree.generate(arg));
+ if (colorValue.isValid() === true) {
+ const rgb = colorValue.toRgb();
+ tuple.push({
+ type: "rgb",
+ alpha: rgb.a,
+ r: rgb.r,
+ g: rgb.g,
+ b: rgb.b,
+ });
+ }
+ }
}
layers.push({
diff --git a/packages/css-data/src/property-parsers/shadows.ts b/packages/css-data/src/property-parsers/shadows.ts
index 8fbf051cf2..51faf37683 100644
--- a/packages/css-data/src/property-parsers/shadows.ts
+++ b/packages/css-data/src/property-parsers/shadows.ts
@@ -86,17 +86,16 @@ export const parseShadow = (
if (item.type === "Function" || item.type === "Hash") {
const colorValue = colord(csstree.generate(item));
- if (!colorValue.isValid()) {
- return;
+ if (colorValue.isValid() === true) {
+ const rgb = colorValue.toRgb();
+ shadow.push({
+ type: "rgb",
+ alpha: rgb.a,
+ r: rgb.r,
+ g: rgb.g,
+ b: rgb.b,
+ });
}
- const rgb = colorValue.toRgb();
- shadow.push({
- type: "rgb",
- alpha: rgb.a,
- r: rgb.r,
- g: rgb.g,
- b: rgb.b,
- });
}
if (item.type === "Dimension") {