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 `translate` css property into tuples (#3696)

Jaya Krishna Namburuc7a688c3c65f
diff --git a/packages/css-data/src/parse-css-value.test.ts b/packages/css-data/src/parse-css-value.test.ts
index 3dfa8a41c1..da2f89273c 100644
--- a/packages/css-data/src/parse-css-value.test.ts
+++ b/packages/css-data/src/parse-css-value.test.ts
@@ -457,3 +457,70 @@ test("parses transform values and returns invalid for invalid values", () => {
value: "matrix(1, 0.5, -0.5, 1, 100)",
});
});
+
+test("parses a valid translate value", () => {
+ expect(parseCssValue("translate", "100px")).toEqual({
+ type: "tuple",
+ value: [
+ {
+ type: "unit",
+ unit: "px",
+ value: 100,
+ },
+ ],
+ });
+
+ expect(parseCssValue("translate", "100px 200px")).toEqual({
+ type: "tuple",
+ value: [
+ {
+ type: "unit",
+ unit: "px",
+ value: 100,
+ },
+ {
+ type: "unit",
+ unit: "px",
+ value: 200,
+ },
+ ],
+ });
+
+ expect(parseCssValue("translate", "10em 10em 10em")).toEqual({
+ type: "tuple",
+ value: [
+ {
+ type: "unit",
+ unit: "em",
+ value: 10,
+ },
+ {
+ type: "unit",
+ unit: "em",
+ value: 10,
+ },
+ {
+ type: "unit",
+ unit: "em",
+ value: 10,
+ },
+ ],
+ });
+});
+
+test("parses and returns invalid for invalid translate values", () => {
+ expect(parseCssValue("translate", "foo bar")).toEqual({
+ type: "invalid",
+ value: "foo bar",
+ });
+
+ expect(parseCssValue("translate", "100px 200px 300px 400px")).toEqual({
+ type: "invalid",
+ value: "100px 200px 300px 400px",
+ });
+
+ expect(parseCssValue("translate", "100%, 200%")).toEqual({
+ type: "invalid",
+ value: "100%, 200%",
+ });
+});
diff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.ts
index a17f51f44c..b2304af6cb 100644
--- a/packages/css-data/src/parse-css-value.ts
+++ b/packages/css-data/src/parse-css-value.ts
@@ -14,8 +14,12 @@ import {
} from "@webstudio-is/css-engine";
import { keywordValues } from "./__generated__/keyword-values";
import { units } from "./__generated__/units";
-import { parseFilter, parseShadow } from "./property-parsers";
-import { parseTransform } from "./property-parsers/transform";
+import {
+ parseFilter,
+ parseShadow,
+ parseTranslate,
+ parseTransform,
+} from "./property-parsers";
export const cssTryParseValue = (input: string) => {
try {
@@ -156,6 +160,10 @@ export const parseCssValue = (
return invalidValue;
}
+ if (property === "translate") {
+ return parseTranslate(input);
+ }
+
if (property === "transform") {
return parseTransform(input);
}
diff --git a/packages/css-data/src/property-parsers/index.ts b/packages/css-data/src/property-parsers/index.ts
index 91df7f36f3..d611f6bf49 100644
--- a/packages/css-data/src/property-parsers/index.ts
+++ b/packages/css-data/src/property-parsers/index.ts
@@ -4,4 +4,5 @@ export * from "./transition";
export * from "./transition-property-extractor";
export * from "./shadow-properties-extractor";
export * from "./filter";
+export * from "./translate";
export * from "./transform";
diff --git a/packages/css-data/src/property-parsers/translate.ts b/packages/css-data/src/property-parsers/translate.ts
new file mode 100644
index 0000000000..d25e9e0f08
--- /dev/null
+++ b/packages/css-data/src/property-parsers/translate.ts
@@ -0,0 +1,69 @@
+import * as csstree from "css-tree";
+import type {
+ InvalidValue,
+ TupleValue,
+ TupleValueItem,
+ Unit,
+} from "@webstudio-is/css-engine";
+import { cssTryParseValue } from "../parse-css-value";
+
+export const parseTranslate = (input: string): TupleValue | InvalidValue => {
+ let tokenStream = input.trim();
+ tokenStream = tokenStream.endsWith(";")
+ ? tokenStream.slice(0, -1)
+ : tokenStream;
+
+ const cleanupKeywords = ["translate:"];
+
+ 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("translate", cssAst);
+ if (parsed.error) {
+ return {
+ type: "invalid",
+ value: input,
+ };
+ }
+
+ const translateValues: TupleValueItem[] = [];
+
+ csstree.walk(cssAst, (node) => {
+ if (node.type === "Value") {
+ const children = node.children;
+ for (const child of children) {
+ if (child.type === "Dimension") {
+ translateValues.push({
+ type: "unit",
+ value: Number(child.value),
+ unit: child.unit as Unit,
+ });
+ }
+
+ if (child.type === "Percentage") {
+ translateValues.push({
+ type: "unit",
+ value: Number(child.value),
+ unit: "%",
+ });
+ }
+ }
+ }
+ });
+
+ return {
+ type: "tuple",
+ value: translateValues,
+ };
+};