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.

refactor: merge timing function parser into value parser (#3694)

Bogdan Chadkind7b529a1188e
diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-timing.tsx b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-timing.tsx
index d832b00ab2..a05ace2c7c 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-timing.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-timing.tsx
@@ -4,6 +4,7 @@ import {
type FunctionValue,
type KeywordValue,
} from "@webstudio-is/css-engine";
+import { parseCssValue } from "@webstudio-is/css-data";
import {
Label,
Tooltip,
@@ -23,7 +24,6 @@ import {
type TimingFunctions,
findTimingFunctionFromValue,
} from "./transition-utils";
-import { parseTransitionLonghandProperty } from "@webstudio-is/css-data";
type TransitionTimingProps = {
timing: KeywordValue | FunctionValue;
@@ -57,23 +57,19 @@ export const TransitionTiming = ({
}
const selectedTiming = timingFunctions[value];
- const longhandValue = parseTransitionLonghandProperty(
+ const parsedEasing = parseCssValue(
"transitionTimingFunction",
selectedTiming
);
- if (longhandValue.type === "invalid") {
+ if (parsedEasing.type === "invalid") {
return;
}
+ const easingValue =
+ parsedEasing.type === "layers" ? parsedEasing.value[0] : parsedEasing;
- const timingValue = longhandValue.value[0];
- const isValidTransitionTimingFunc =
- timingValue.type === "keyword" || timingValue.type === "function";
-
- if (isValidTransitionTimingFunc === true) {
- onTimingSelection({
- timing: timingValue,
- });
+ if (easingValue.type === "keyword" || easingValue.type === "function") {
+ onTimingSelection({ timing: easingValue });
}
};
diff --git a/packages/css-data/src/parse-css-value.test.ts b/packages/css-data/src/parse-css-value.test.ts
index cb8db0b976..2c7b0fc582 100644
--- a/packages/css-data/src/parse-css-value.test.ts
+++ b/packages/css-data/src/parse-css-value.test.ts
@@ -1,6 +1,6 @@
import { describe, test, expect } from "@jest/globals";
import { parseCssValue } from "./parse-css-value";
-import type { StyleProperty } from "@webstudio-is/css-engine";
+import { toValue, type StyleProperty } from "@webstudio-is/css-engine";
describe("Parse CSS value", () => {
describe("number value", () => {
@@ -277,12 +277,71 @@ test("parse transition-property property", () => {
);
});
+test("parse transition-duration property", () => {
+ expect(parseCssValue("transitionDuration", `10ms, 10ms`)).toEqual({
+ type: "layers",
+ value: [
+ { type: "unit", unit: "ms", value: 10 },
+ { type: "unit", unit: "ms", value: 10 },
+ ],
+ });
+ expect(parseCssValue("transitionDuration", `10ms, foo`)).toEqual({
+ type: "invalid",
+ value: "10ms, foo",
+ });
+});
+
+test("parse transition-timing-function property", () => {
+ const parsedValue = parseCssValue(
+ "transitionTimingFunction",
+ "ease, ease-in, cubic-bezier(0.68,-0.6,.32,1.6), steps(4, jump-start)"
+ );
+ expect(parsedValue).toEqual({
+ type: "layers",
+ value: [
+ { type: "keyword", value: "ease" },
+ { type: "keyword", value: "ease-in" },
+ {
+ type: "function",
+ name: "cubic-bezier",
+ args: {
+ type: "layers",
+ value: [
+ { type: "keyword", value: "0.68" },
+ { type: "keyword", value: "-0.6" },
+ { type: "keyword", value: ".32" },
+ { type: "keyword", value: "1.6" },
+ ],
+ },
+ },
+ {
+ type: "function",
+ name: "steps",
+ args: {
+ type: "layers",
+ value: [
+ { type: "keyword", value: "4" },
+ { type: "keyword", value: "jump-start" },
+ ],
+ },
+ },
+ ],
+ });
+ expect(toValue(parsedValue)).toMatchInlineSnapshot(
+ `"ease, ease-in, cubic-bezier(0.68, -0.6, .32, 1.6), steps(4, jump-start)"`
+ );
+ expect(parseCssValue("transitionTimingFunction", "ease, testing")).toEqual({
+ type: "invalid",
+ value: "ease, testing",
+ });
+});
+
test("parse transition-behavior property as layers", () => {
expect(parseCssValue("transitionBehavior", `normal`)).toEqual({
type: "layers",
value: [{ type: "keyword", value: "normal" }],
});
- expect(parseCssValue("transitionBehavior", `normal, allow-discrete`)).toEqual(
+ expect(parseCssValue("transitionBehavior", `NORMAL, allow-discrete`)).toEqual(
{
type: "layers",
value: [
diff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.ts
index 4854e80a7e..4c46dd43b0 100644
--- a/packages/css-data/src/parse-css-value.ts
+++ b/packages/css-data/src/parse-css-value.ts
@@ -3,6 +3,7 @@ import * as csstree from "css-tree";
import type { CssNode } from "css-tree";
import warnOnce from "warn-once";
import {
+ LayersValue,
TupleValue,
cssWideKeywords,
hyphenateProperty,
@@ -13,12 +14,7 @@ import {
} from "@webstudio-is/css-engine";
import { keywordValues } from "./__generated__/keyword-values";
import { units } from "./__generated__/units";
-import {
- isTransitionLongHandProperty,
- parseFilter,
- parseShadow,
- parseTransitionLonghandProperty,
-} from "./property-parsers";
+import { parseFilter, parseShadow } from "./property-parsers";
export const cssTryParseValue = (input: string) => {
try {
@@ -111,6 +107,9 @@ const repeatedProps = new Set<StyleProperty>([
"backgroundSize",
"backgroundImage",
"transitionProperty",
+ "transitionDuration",
+ "transitionDelay",
+ "transitionTimingFunction",
"transitionBehavior",
]);
@@ -164,14 +163,6 @@ export const parseCssValue = (
return parseShadow(property, input);
}
- if (
- isTransitionLongHandProperty(property) &&
- property !== "transitionBehavior" &&
- property !== "transitionProperty"
- ) {
- return parseTransitionLonghandProperty(property, input);
- }
-
// prevent infinite splitting into layers for items
if (repeatedProps.has(property) && topLevel) {
const nodes = "children" in ast ? ast.children?.toArray() ?? [] : [ast];
@@ -200,11 +191,35 @@ export const parseCssValue = (
// csstree does not support transition-behavior
// so check keywords manually
if (property === "transitionBehavior") {
- return keywordValues[property].includes(input as never)
- ? { type: "keyword", value: input }
+ return keywordValues[property].includes(potentialKeyword as never)
+ ? { type: "keyword", value: potentialKeyword }
: invalidValue;
}
+ if (property === "transitionTimingFunction") {
+ const node = ast.type === "Value" ? ast.children.first : ast;
+ if (node?.type === "Identifier") {
+ return { type: "keyword", value: node.name };
+ }
+ if (node?.type === "Function") {
+ // transition timing function arguments are comma seperated values
+ const args: LayersValue = { type: "layers", value: [] };
+ for (const arg of node.children) {
+ if (arg.type === "Number") {
+ args.value.push({ type: "keyword", value: arg.value });
+ }
+ if (arg.type === "Identifier") {
+ args.value.push({ type: "keyword", value: arg.name });
+ }
+ if (arg.type === "Dimension") {
+ const unit = arg.unit as Unit;
+ args.value.push({ type: "unit", value: Number(arg.value), unit });
+ }
+ }
+ return { type: "function", args, name: node.name };
+ }
+ }
+
if (ast.type === "Value" && ast.children.first === ast.children.last) {
// Try extract units from 1st children
const first = ast.children.first;
diff --git a/packages/css-data/src/property-parsers/transition.test.ts b/packages/css-data/src/property-parsers/transition.test.ts
deleted file mode 100644
index 8afa0f4192..0000000000
--- a/packages/css-data/src/property-parsers/transition.test.ts
+++ /dev/null
@@ -1,180 +0,0 @@
-import { expect, test } from "@jest/globals";
-
-import { parseTransitionLonghandProperty } from "./transition";
-import { toValue } from "@webstudio-is/css-engine";
-
-test("parses a valid transitionDuration longhand property", () => {
- expect(parseTransitionLonghandProperty("transitionDuration", `10ms, 10ms`))
- .toMatchInlineSnapshot(`
-{
- "type": "layers",
- "value": [
- {
- "type": "unit",
- "unit": "ms",
- "value": 10,
- },
- {
- "type": "unit",
- "unit": "ms",
- "value": 10,
- },
- ],
-}
-`);
-});
-
-test("parses an in-valid transitionDuration longhand property", () => {
- expect(parseTransitionLonghandProperty("transitionDuration", `10ms, foo`))
- .toMatchInlineSnapshot(`
-{
- "type": "invalid",
- "value": "10ms, foo",
-}
-`);
-});
-
-test("parses a vaild transitionTimingFunction longhand property with 0 omitted", () => {
- const parsedValue = parseTransitionLonghandProperty(
- "transitionTimingFunction",
- "ease, ease-in, cubic-bezier(.68,-0.6,0.32,1.6), steps(4, jump-start)"
- );
- expect(parsedValue).toMatchInlineSnapshot(`
-{
- "type": "layers",
- "value": [
- {
- "type": "keyword",
- "value": "ease",
- },
- {
- "type": "keyword",
- "value": "ease-in",
- },
- {
- "args": {
- "type": "layers",
- "value": [
- {
- "type": "keyword",
- "value": ".68",
- },
- {
- "type": "keyword",
- "value": "-0.6",
- },
- {
- "type": "keyword",
- "value": "0.32",
- },
- {
- "type": "keyword",
- "value": "1.6",
- },
- ],
- },
- "name": "cubic-bezier",
- "type": "function",
- },
- {
- "args": {
- "type": "layers",
- "value": [
- {
- "type": "keyword",
- "value": "4",
- },
- {
- "type": "keyword",
- "value": "jump-start",
- },
- ],
- },
- "name": "steps",
- "type": "function",
- },
- ],
-}
-`);
-});
-
-test("parses a vaild transitionTimingFunction longhand property", () => {
- const parsedValue = parseTransitionLonghandProperty(
- "transitionTimingFunction",
- "ease, ease-in, cubic-bezier(0.68,-0.6,0.32,1.6), steps(4, jump-start)"
- );
- expect(parsedValue).toMatchInlineSnapshot(`
-{
- "type": "layers",
- "value": [
- {
- "type": "keyword",
- "value": "ease",
- },
- {
- "type": "keyword",
- "value": "ease-in",
- },
- {
- "args": {
- "type": "layers",
- "value": [
- {
- "type": "keyword",
- "value": "0.68",
- },
- {
- "type": "keyword",
- "value": "-0.6",
- },
- {
- "type": "keyword",
- "value": "0.32",
- },
- {
- "type": "keyword",
- "value": "1.6",
- },
- ],
- },
- "name": "cubic-bezier",
- "type": "function",
- },
- {
- "args": {
- "type": "layers",
- "value": [
- {
- "type": "keyword",
- "value": "4",
- },
- {
- "type": "keyword",
- "value": "jump-start",
- },
- ],
- },
- "name": "steps",
- "type": "function",
- },
- ],
-}
-`);
- expect(toValue(parsedValue)).toMatchInlineSnapshot(
- `"ease, ease-in, cubic-bezier(0.68, -0.6, 0.32, 1.6), steps(4, jump-start)"`
- );
-});
-
-test("parses any invalid transitionTimingFunction proeprty and returns invalud", () => {
- expect(
- parseTransitionLonghandProperty(
- "transitionTimingFunction",
- "ease, ease-in, cubic-bezier(0.68,-0.6,0.32,1.6), testing"
- )
- ).toMatchInlineSnapshot(`
-{
- "type": "invalid",
- "value": "ease, ease-in, cubic-bezier(0.68,-0.6,0.32,1.6), testing",
-}
-`);
-});
diff --git a/packages/css-data/src/property-parsers/transition.ts b/packages/css-data/src/property-parsers/transition.ts
index 1efe7ac6e6..61569b4347 100644
--- a/packages/css-data/src/property-parsers/transition.ts
+++ b/packages/css-data/src/property-parsers/transition.ts
@@ -1,18 +1,11 @@
-import * as csstree from "css-tree";
import type {
- InvalidValue,
KeywordValue,
LayerValueItem,
- LayersValue,
- Unit,
UnitValue,
FunctionValue,
StyleProperty,
} from "@webstudio-is/css-engine";
-import { animatableProperties } from "../";
-import { isTimingFunction } from "./transition-property-extractor";
-import { cssTryParseValue } from "../parse-css-value";
-import { kebabCase } from "change-case";
+import { animatableProperties } from "../__generated__/animatable-properties";
type AnimatableProperty = (typeof animatableProperties)[number];
@@ -64,105 +57,3 @@ export const isValidTransitionValue = (
value.type === "unparsed"
);
};
-
-export const parseTransitionLonghandProperty = (
- property: (typeof transitionLongHandProperties)[number],
- input: string
-): LayersValue | InvalidValue => {
- const cssAst = cssTryParseValue(input);
- if (cssAst === undefined) {
- return {
- type: "invalid",
- value: input,
- };
- }
-
- const parsed = csstree.lexer.matchProperty(kebabCase(property), cssAst);
- if (parsed.error) {
- return {
- type: "invalid",
- value: input,
- };
- }
-
- const layers: LayersValue = { type: "layers", value: [] };
- try {
- csstree.walk(cssAst, (node) => {
- if (node.type === "Value") {
- const children = node.children;
-
- for (const child of children) {
- switch (property) {
- case "transitionTimingFunction": {
- if (child.type === "Identifier") {
- layers.value.push({ type: "keyword", value: child.name });
- }
-
- if (child.type === "Function") {
- if (isTimingFunction(child.name) === false) {
- throw new Error(
- `Invalid timing function, received ${csstree.generate(child)}`
- );
- }
-
- // transition-timing function arguments are comma seperated values
- const args: LayersValue = { type: "layers", value: [] };
- const timingFunction: FunctionValue = {
- type: "function",
- args,
- name: child.name,
- };
-
- for (const arg of child.children) {
- if (arg.type === "Number") {
- args.value.push({ type: "keyword", value: arg.value });
- }
-
- if (arg.type === "Identifier") {
- args.value.push({ type: "keyword", value: arg.name });
- }
-
- if (arg.type === "Dimension") {
- args.value.push({
- type: "unit",
- value: Number(arg.value),
- unit: arg.unit as Unit,
- });
- }
- }
-
- layers.value.push(timingFunction);
- }
-
- break;
- }
-
- case "transitionDuration":
- case "transitionDelay": {
- if (child.type === "Dimension") {
- layers.value.push({
- type: "unit",
- value: Number(child.value),
- unit: child.unit as Unit,
- });
- }
-
- break;
- }
-
- default: {
- throw new Error(`Received invalid property to parse ${property}`);
- }
- }
- }
- }
- });
- } catch (error) {
- return {
- type: "invalid",
- value: input,
- };
- }
-
- return layers.value.length > 0 ? layers : { type: "invalid", value: input };
-};