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.

experimental: parse transform css property into tuples (#3704)

Jaya Krishna Namburu6abceecd74bf
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx
index 427067cc7f..645b8f9a1f 100644
--- a/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/plugin-webflow.test.tsx
@@ -2744,7 +2744,7 @@ describe("Styles", () => {
outline: 0 none currentColor
}
x {
- transform: translate3d(7px,74px,16px)
+ transform: translate3d(7px, 74px, 16px)
}
x:hover {
background-color: rgba(199, 16, 16, 1)
diff --git a/packages/css-data/src/parse-css-value.test.ts b/packages/css-data/src/parse-css-value.test.ts
index 2c7b0fc582..3dfa8a41c1 100644
--- a/packages/css-data/src/parse-css-value.test.ts
+++ b/packages/css-data/src/parse-css-value.test.ts
@@ -374,3 +374,86 @@ test("parse unknown properties as unparsed", () => {
}
);
});
+
+test("parse transform property as tuple", () => {
+ expect(
+ parseCssValue("transform", "rotateX(45deg) rotateY(30deg) rotateZ(60deg)")
+ ).toEqual({
+ type: "tuple",
+ value: [
+ {
+ type: "function",
+ name: "rotateX",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", value: 45, unit: "deg" }],
+ },
+ },
+ {
+ type: "function",
+ name: "rotateY",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", value: 30, unit: "deg" }],
+ },
+ },
+ {
+ type: "function",
+ name: "rotateZ",
+ args: {
+ type: "tuple",
+ value: [{ type: "unit", value: 60, unit: "deg" }],
+ },
+ },
+ ],
+ });
+
+ expect(parseCssValue("transform", "skew(30deg, 20deg)")).toEqual({
+ type: "tuple",
+ value: [
+ {
+ type: "function",
+ name: "skew",
+ args: {
+ type: "layers",
+ value: [
+ { type: "unit", value: 30, unit: "deg" },
+ { type: "unit", value: 20, unit: "deg" },
+ ],
+ },
+ },
+ ],
+ });
+
+ expect(
+ parseCssValue("transform", "translate3d(-100px, 50px, -150px)")
+ ).toEqual({
+ type: "tuple",
+ value: [
+ {
+ type: "function",
+ name: "translate3d",
+ args: {
+ type: "layers",
+ value: [
+ { type: "unit", value: -100, unit: "px" },
+ { type: "unit", value: 50, unit: "px" },
+ { type: "unit", value: -150, unit: "px" },
+ ],
+ },
+ },
+ ],
+ });
+});
+
+test("parses transform values and returns invalid for invalid values", () => {
+ expect(parseCssValue("transform", "scale(1.5, 50px)")).toEqual({
+ type: "invalid",
+ value: "scale(1.5, 50px)",
+ });
+
+ expect(parseCssValue("transform", "matrix(1, 0.5, -0.5, 1, 100)")).toEqual({
+ type: "invalid",
+ value: "matrix(1, 0.5, -0.5, 1, 100)",
+ });
+});
diff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.ts
index 4c46dd43b0..a17f51f44c 100644
--- a/packages/css-data/src/parse-css-value.ts
+++ b/packages/css-data/src/parse-css-value.ts
@@ -15,6 +15,7 @@ import {
import { keywordValues } from "./__generated__/keyword-values";
import { units } from "./__generated__/units";
import { parseFilter, parseShadow } from "./property-parsers";
+import { parseTransform } from "./property-parsers/transform";
export const cssTryParseValue = (input: string) => {
try {
@@ -155,6 +156,10 @@ export const parseCssValue = (
return invalidValue;
}
+ if (property === "transform") {
+ return parseTransform(input);
+ }
+
if (property === "filter" || property === "backdropFilter") {
return parseFilter(property, input);
}
diff --git a/packages/css-data/src/property-parsers/index.ts b/packages/css-data/src/property-parsers/index.ts
index 1d411f05a4..91df7f36f3 100644
--- a/packages/css-data/src/property-parsers/index.ts
+++ b/packages/css-data/src/property-parsers/index.ts
@@ -4,3 +4,4 @@ export * from "./transition";
export * from "./transition-property-extractor";
export * from "./shadow-properties-extractor";
export * from "./filter";
+export * from "./transform";
diff --git a/packages/css-data/src/property-parsers/transform.ts b/packages/css-data/src/property-parsers/transform.ts
new file mode 100644
index 0000000000..da7a967280
--- /dev/null
+++ b/packages/css-data/src/property-parsers/transform.ts
@@ -0,0 +1,95 @@
+import * as csstree from "css-tree";
+import type {
+ InvalidValue,
+ LayersValue,
+ TupleValue,
+ TupleValueItem,
+ Unit,
+} from "@webstudio-is/css-engine";
+import { cssTryParseValue } from "../parse-css-value";
+
+export const parseTransform = (input: string): TupleValue | InvalidValue => {
+ let tokenStream = input.trim();
+ tokenStream = tokenStream.endsWith(";")
+ ? tokenStream.slice(0, -1)
+ : tokenStream;
+
+ const cleanupKeywords = ["transform:"];
+
+ for (const cleanupKeyword of cleanupKeywords) {
+ tokenStream = tokenStream.startsWith(cleanupKeyword)
+ ? tokenStream.slice(cleanupKeyword.length).trim()
+ : tokenStream;
+ }
+
+ const cssAst = cssTryParseValue(tokenStream);
+ if (cssAst === undefined) {
+ return {
+ type: "invalid",
+ value: input,
+ };
+ }
+
+ const parsed = csstree.lexer.matchProperty("transform", cssAst);
+ if (parsed.error) {
+ return {
+ type: "invalid",
+ value: input,
+ };
+ }
+
+ const transformValue: TupleValueItem[] = [];
+
+ csstree.walk(cssAst, (node) => {
+ if (node.type === "Value") {
+ for (const child of node.children) {
+ if (child.type === "Function") {
+ const isCommaSeparatedArgs = child.children.some(
+ (arg) => arg.type === "Operator" && arg.value === ","
+ );
+
+ const args: TupleValue | LayersValue = isCommaSeparatedArgs
+ ? { type: "layers", value: [] }
+ : { type: "tuple", value: [] };
+
+ for (const arg of child.children) {
+ if (arg.type === "Number") {
+ args.value.push({
+ type: "unit",
+ value: Number(arg.value),
+ unit: "number",
+ });
+ }
+
+ if (arg.type === "Dimension") {
+ args.value.push({
+ type: "unit",
+ value: Number(arg.value),
+ unit: arg.unit as Unit,
+ });
+ }
+
+ if (arg.type === "Percentage") {
+ args.value.push({
+ type: "unit",
+ value: Number(arg.value),
+ unit: "%",
+ });
+ }
+ }
+
+ transformValue.push({
+ type: "function",
+ name: child.name,
+ args,
+ });
+ }
+ }
+ }
+ });
+
+ return {
+ type: "tuple",
+ value: transformValue,
+ };
+};