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: move transform parser into css value parser (#3726)

Bogdan Chadkinb1ef716ae7fd
diff --git a/packages/css-data/src/parse-css-value.test.ts b/packages/css-data/src/parse-css-value.test.ts
index 8a3a6d3636..2359e8fc21 100644
--- a/packages/css-data/src/parse-css-value.test.ts
+++ b/packages/css-data/src/parse-css-value.test.ts
@@ -385,7 +385,7 @@ test("parse transform property as tuple", () => {
type: "function",
name: "rotateX",
args: {
- type: "tuple",
+ type: "layers",
value: [{ type: "unit", value: 45, unit: "deg" }],
},
},
@@ -393,7 +393,7 @@ test("parse transform property as tuple", () => {
type: "function",
name: "rotateY",
args: {
- type: "tuple",
+ type: "layers",
value: [{ type: "unit", value: 30, unit: "deg" }],
},
},
@@ -401,7 +401,7 @@ test("parse transform property as tuple", () => {
type: "function",
name: "rotateZ",
args: {
- type: "tuple",
+ type: "layers",
value: [{ type: "unit", value: 60, unit: "deg" }],
},
},
@@ -461,49 +461,21 @@ test("parses transform values and returns invalid for invalid values", () => {
test("parses a valid translate value", () => {
expect(parseCssValue("translate", "100px")).toEqual({
type: "tuple",
- value: [
- {
- type: "unit",
- unit: "px",
- value: 100,
- },
- ],
+ 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,
- },
+ { 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,
- },
+ { type: "unit", unit: "em", value: 10 },
+ { type: "unit", unit: "em", value: 10 },
+ { type: "unit", unit: "em", value: 10 },
],
});
});
@@ -513,51 +485,29 @@ test("parses and returns invalid for invalid translate values", () => {
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%",
});
});
-test("parses a valid translate value", () => {
+test("parses a valid scale value", () => {
expect(parseCssValue("scale", "1.5")).toEqual({
type: "tuple",
- value: [
- {
- type: "unit",
- value: 1.5,
- unit: "number",
- },
- ],
+ value: [{ type: "unit", value: 1.5, unit: "number" }],
});
-
expect(parseCssValue("scale", "5 10 15")).toEqual({
type: "tuple",
value: [
- {
- type: "unit",
- value: 5,
- unit: "number",
- },
- {
- type: "unit",
- value: 10,
- unit: "number",
- },
- {
- type: "unit",
- value: 15,
- unit: "number",
- },
+ { type: "unit", value: 5, unit: "number" },
+ { type: "unit", value: 10, unit: "number" },
+ { type: "unit", value: 15, unit: "number" },
],
});
-
expect(parseCssValue("scale", "50%")).toEqual({
type: "tuple",
value: [{ type: "unit", value: 50, unit: "%" }],
diff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.ts
index 2ccff13e3d..175f948076 100644
--- a/packages/css-data/src/parse-css-value.ts
+++ b/packages/css-data/src/parse-css-value.ts
@@ -1,6 +1,6 @@
import { colord } from "colord";
import * as csstree from "css-tree";
-import { type CssNode, generate, List } from "css-tree";
+import { type CssNode, generate, lexer, List } from "css-tree";
import warnOnce from "warn-once";
import {
cssWideKeywords,
@@ -16,12 +16,10 @@ import {
type StyleValue,
type Unit,
type VarValue,
+ type FunctionValue,
} from "@webstudio-is/css-engine";
import { keywordValues } from "./__generated__/keyword-values";
import { units } from "./__generated__/units";
-import { parseTranslate } from "./property-parsers/translate";
-import { parseTransform } from "./property-parsers/transform";
-import { parseScale } from "./property-parsers/scale";
import { parseFilter } from "./property-parsers/filter";
export const cssTryParseValue = (input: string) => {
@@ -98,6 +96,15 @@ export const isValidDeclaration = (
return false;
}
+ // scale css-proeprty accepts both number and percentage.
+ // The syntax from MDN is incorrect and should be updated.
+ // Here is a PR that fixes the same, but it is not merged yet.
+ // https://github.com/mdn/data/pull/746
+ if (cssPropertyName === "scale") {
+ const syntax = "none | [ <number> | <percentage> ]{1,3}";
+ return lexer.match(syntax, ast).matched !== null;
+ }
+
const matchResult = csstree.lexer.matchProperty(cssPropertyName, ast);
// allow to parse unknown properties as unparsed
@@ -127,7 +134,14 @@ const repeatedProps = new Set<StyleProperty>([
"textShadow",
]);
-const tupleProps = new Set<StyleProperty>(["boxShadow", "textShadow"]);
+const tupleProps = new Set<StyleProperty>([
+ "boxShadow",
+ "textShadow",
+ "scale",
+ "translate",
+ "rotate",
+ "transform",
+]);
const availableUnits = new Set<string>(Object.values(units).flat());
@@ -148,7 +162,14 @@ const parseColor = (colorString: string): undefined | RgbValue => {
const parseLiteral = (
node: undefined | null | CssNode,
keywords?: readonly string[]
-): undefined | UnitValue | KeywordValue | ImageValue | RgbValue | VarValue => {
+):
+ | undefined
+ | UnitValue
+ | KeywordValue
+ | ImageValue
+ | RgbValue
+ | VarValue
+ | FunctionValue => {
if (node?.type === "Number") {
return {
type: "unit",
@@ -195,6 +216,7 @@ const parseLiteral = (
}
}
if (node?.type === "Function") {
+ // <color-function>
if (
node.name === "hsl" ||
node.name === "hsla" ||
@@ -223,6 +245,48 @@ const parseLiteral = (
};
}
}
+ if (
+ // <transform-function>
+ // 2d
+ node.name === "matrix" ||
+ node.name === "translate" ||
+ node.name === "translateX" ||
+ node.name === "translateY" ||
+ node.name === "scale" ||
+ node.name === "scaleX" ||
+ node.name === "scaleY" ||
+ node.name === "rotate" ||
+ node.name === "skew" ||
+ node.name === "skewX" ||
+ node.name === "skewY" ||
+ // 3d
+ node.name === "matrix3d" ||
+ node.name === "translate3d" ||
+ node.name === "translateZ" ||
+ node.name === "scale3d" ||
+ node.name === "scaleZ" ||
+ node.name === "rotate3d" ||
+ node.name === "rotateX" ||
+ node.name === "rotateY" ||
+ node.name === "rotateZ" ||
+ node.name === "perspective" ||
+ // <easing-function>
+ node.name === "linear" ||
+ node.name === "cubic-bezier" ||
+ node.name === "steps"
+ ) {
+ const args: LayersValue = { type: "layers", value: [] };
+ for (const arg of node.children) {
+ const matchedValue = parseLiteral(arg);
+ if (matchedValue) {
+ args.value.push(matchedValue as LayerValueItem);
+ }
+ if (arg.type === "Identifier") {
+ args.value.push({ type: "keyword", value: arg.name });
+ }
+ }
+ return { type: "function", args, name: node.name };
+ }
}
};
@@ -245,10 +309,6 @@ export const parseCssValue = (
}
}
- if (property === "scale") {
- return parseScale(input);
- }
-
const invalidValue = {
type: "invalid",
value: input,
@@ -272,14 +332,6 @@ export const parseCssValue = (
return invalidValue;
}
- if (property === "translate") {
- return parseTranslate(input);
- }
-
- if (property === "transform") {
- return parseTransform(input);
- }
-
if (property === "filter" || property === "backdropFilter") {
return parseFilter(property, input);
}
@@ -320,32 +372,27 @@ export const parseCssValue = (
return invalidValue;
}
- if (property === "transitionTimingFunction") {
- const node = ast.type === "Value" ? ast.children.first : ast;
- const keyword = parseLiteral(node, keywordValues[property]);
- if (keyword) {
- return keyword;
- }
- if (node?.type === "Function") {
- // transition timing function arguments are comma seperated values
- const args: LayersValue = { type: "layers", value: [] };
- for (const arg of node.children) {
- const matchedValue = parseLiteral(arg);
- if (matchedValue) {
- args.value.push(matchedValue as never);
- }
- if (arg.type === "Identifier") {
- args.value.push({ type: "keyword", value: arg.name });
- }
+ // Probably a tuple like background-size or box-shadow
+ if (
+ ast.type === "Value" &&
+ (ast.children.size > 1 || tupleProps.has(property))
+ ) {
+ const tuple: TupleValue = {
+ type: "tuple",
+ value: [],
+ };
+ for (const node of ast.children) {
+ const matchedValue = parseLiteral(node, keywordValues[property as never]);
+ if (matchedValue) {
+ tuple.value.push(matchedValue as never);
}
- return { type: "function", args, name: node.name };
}
+ return tuple;
}
if (ast.type === "Value" && ast.children.size === 1) {
// Try extract units from 1st children
const first = ast.children.first;
-
const matchedValue = parseLiteral(first, keywordValues[property as never]);
// parse only references in custom properties
if (property.startsWith("--")) {
@@ -359,24 +406,6 @@ export const parseCssValue = (
}
}
- // Probably a tuple like background-size or box-shadow
- if (
- ast.type === "Value" &&
- (ast.children.size > 1 || tupleProps.has(property))
- ) {
- const tuple: TupleValue = {
- type: "tuple",
- value: [],
- };
- for (const node of ast.children) {
- const matchedValue = parseLiteral(node, keywordValues[property as never]);
- if (matchedValue) {
- tuple.value.push(matchedValue as never);
- }
- }
- return tuple;
- }
-
return {
type: "unparsed",
value: input,
diff --git a/packages/css-data/src/property-parsers/scale.ts b/packages/css-data/src/property-parsers/scale.ts
deleted file mode 100644
index 2983d6b51b..0000000000
--- a/packages/css-data/src/property-parsers/scale.ts
+++ /dev/null
@@ -1,79 +0,0 @@
-import * as csstree from "css-tree";
-import type {
- InvalidValue,
- TupleValue,
- TupleValueItem,
-} from "@webstudio-is/css-engine";
-import { cssTryParseValue } from "../parse-css-value";
-
-export const parseScale = (input: string): TupleValue | InvalidValue => {
- let tokenStream = input.trim();
- tokenStream = tokenStream.endsWith(";")
- ? tokenStream.slice(0, -1)
- : tokenStream;
-
- const cleanupKeywords = ["scale:"];
-
- 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,
- };
- }
-
- // scale css-proeprty accepts both number and percentage.
- // The syntax from MDN is incorrect and should be updated.
- // Here is a PR that fixes the same, but it is not merged yet.
- // https://github.com/mdn/data/pull/746
- const parsed = csstree.lexer.match("none | <number-percentage>{1,3}", input);
- if (parsed.error) {
- return {
- type: "invalid",
- value: input,
- };
- }
-
- const scaleValue: TupleValueItem[] = [];
-
- try {
- csstree.walk(cssAst, (node) => {
- if (node.type === "Value") {
- const children = node.children;
- for (const child of children) {
- if (child.type === "Number") {
- scaleValue.push({
- type: "unit",
- value: Number(child.value),
- unit: "number",
- });
- }
-
- if (child.type === "Percentage") {
- scaleValue.push({
- type: "unit",
- value: Number(child.value),
- unit: "%",
- });
- }
- }
- }
- });
- } catch {
- return {
- type: "invalid",
- value: input,
- };
- }
-
- return {
- type: "tuple",
- value: scaleValue,
- };
-};
diff --git a/packages/css-data/src/property-parsers/transform.ts b/packages/css-data/src/property-parsers/transform.ts
deleted file mode 100644
index da7a967280..0000000000
--- a/packages/css-data/src/property-parsers/transform.ts
+++ /dev/null
@@ -1,95 +0,0 @@
-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,
- };
-};
diff --git a/packages/css-data/src/property-parsers/translate.ts b/packages/css-data/src/property-parsers/translate.ts
deleted file mode 100644
index d25e9e0f08..0000000000
--- a/packages/css-data/src/property-parsers/translate.ts
+++ /dev/null
@@ -1,69 +0,0 @@
-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,
- };
-};