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.

Initial CSS parser (#1652)

Giuseppe6a9c4c013ca4
diff --git a/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx b/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx
index 712a43c7f8..f3c430fdd3 100644
--- a/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx
+++ b/apps/builder/app/builder/features/style-panel/controls/select/select-control.tsx
@@ -2,7 +2,7 @@ import { toValue } from "@webstudio-is/css-engine";
import { Select } from "@webstudio-is/design-system";
import { styleConfigByName } from "../../shared/configs";
import { toPascalCase } from "../../shared/keyword-utils";
-import { parseCssValue } from "../../shared/parse-css-value";
+import { parseCssValue } from "@webstudio-is/css-data";
import type { ControlProps } from "../../style-sections";
export const SelectControl = ({
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx
index 2994686541..10d62f3d82 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-gradient.tsx
@@ -1,9 +1,8 @@
import type { InvalidValue, RgbValue } from "@webstudio-is/css-data";
+import { parseCssValue, parseBackground } from "@webstudio-is/css-data";
import { TextArea, theme } from "@webstudio-is/design-system";
import { useEffect, useRef, useState } from "react";
-import { parseCssValue } from "../../shared/parse-css-value";
import type { ControlProps } from "../../style-sections";
-import { parseBackground } from "./background-parser";
type IntermediateValue = {
type: "intermediate";
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx
index 7c2258f890..3cbcd78186 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-size.tsx
@@ -2,7 +2,7 @@ import { Grid, Label, Select, theme } from "@webstudio-is/design-system";
import { toValue } from "@webstudio-is/css-engine";
import { styleConfigByName } from "../../shared/configs";
import { toPascalCase } from "../../shared/keyword-utils";
-import { parseCssValue } from "../../shared/parse-css-value";
+import { parseCssValue } from "@webstudio-is/css-data";
import type { ControlProps } from "../../style-sections";
import { CssValueInputContainer } from "../../controls/position/css-value-input-container";
import {
diff --git a/apps/builder/app/builder/features/style-panel/sections/space/scrub.tsx b/apps/builder/app/builder/features/style-panel/sections/space/scrub.tsx
index 0f8cb335f4..a4aa7a5244 100644
--- a/apps/builder/app/builder/features/style-panel/sections/space/scrub.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/space/scrub.tsx
@@ -4,7 +4,7 @@ import { useState, useEffect, useRef } from "react";
import { useModifierKeys } from "../../shared/modifier-keys";
import type { StyleUpdateOptions } from "../../shared/use-style-data";
import type { SpaceStyleProperty, HoverTagret } from "./types";
-import { isValid } from "../../shared/parse-css-value";
+import { isValidDeclaration } from "@webstudio-is/css-data";
import { parseIntermediateOrInvalidValue } from "../../shared/css-value-input/parse-intermediate-or-invalid-value";
import { toValue } from "@webstudio-is/css-engine";
import type { CssValueInputValue } from "../../shared/css-value-input/css-value-input";
@@ -71,7 +71,7 @@ export const useScrub = (props: {
unit: unitRef.current,
} as const;
- if (isValid(property, toValue(value)) === false) {
+ if (isValidDeclaration(property, toValue(value)) === false) {
value = parseIntermediateOrInvalidValue(property, {
type: "intermediate",
value: `${value.value}`,
@@ -81,7 +81,7 @@ export const useScrub = (props: {
// In case of negative values for some properties, we might end up with invalid value.
if (value.type === "invalid") {
// Try return unitless
- if (isValid(property, "0")) {
+ if (isValidDeclaration(property, "0")) {
value = {
type: "unit",
unit: "number",
diff --git a/apps/builder/app/builder/features/style-panel/sections/space/value-text.stories.tsx b/apps/builder/app/builder/features/style-panel/sections/space/value-text.stories.tsx
index 9ce844343c..8b412cf6c0 100644
--- a/apps/builder/app/builder/features/style-panel/sections/space/value-text.stories.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/space/value-text.stories.tsx
@@ -1,6 +1,6 @@
import type { ComponentMeta } from "@storybook/react";
import React from "react";
-import { parseCssValue } from "../../shared/parse-css-value";
+import { parseCssValue } from "@webstudio-is/css-data";
import { SpaceLayout } from "./layout";
import { ValueText as ValueTextComponent } from "./value-text";
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx
index 161bdf5697..8bd9cd6fb9 100644
--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/css-value-input.tsx
@@ -33,7 +33,7 @@ import { toValue } from "@webstudio-is/css-engine";
import { useDebouncedCallback } from "use-debounce";
import type { StyleSource } from "../style-info";
import { toPascalCase } from "../keyword-utils";
-import { isValid } from "../parse-css-value";
+import { isValidDeclaration } from "@webstudio-is/css-data";
import {
selectedInstanceBrowserStyleStore,
selectedInstanceUnitSizesStore,
@@ -95,7 +95,7 @@ const useScrub = ({
if (
value.type === "unit" &&
- isValid(property, toValue(value)) === false
+ isValidDeclaration(property, toValue(value)) === false
) {
value = parseIntermediateOrInvalidValue(property, {
type: "intermediate",
@@ -106,7 +106,7 @@ const useScrub = ({
// In case of negative values for some properties, we might end up with invalid value.
if (value.type === "invalid") {
// Try return unitless
- if (isValid(property, "0")) {
+ if (isValidDeclaration(property, "0")) {
value = {
type: "unit",
unit: "number",
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts
index 4f9d29be96..5150e4a99b 100644
--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts
@@ -4,9 +4,8 @@ import type {
InvalidValue,
} from "@webstudio-is/css-data";
import type { IntermediateStyleValue } from "./css-value-input";
-import { parseCssValue } from "../parse-css-value";
import { evaluateMath } from "./evaluate-math";
-import { units } from "@webstudio-is/css-data";
+import { units, parseCssValue } from "@webstudio-is/css-data";
import { toKebabCase } from "../keyword-utils";
const unitsList = Object.values(units).flat();
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select-options.ts b/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select-options.ts
index 1857e3bc44..052d6221b6 100644
--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select-options.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/unit-select-options.ts
@@ -1,7 +1,11 @@
import type { CssValueInputValue } from "./css-value-input";
-import { isValid } from "../parse-css-value";
import { toPascalCase } from "../keyword-utils";
-import { keywordValues, properties, units } from "@webstudio-is/css-data";
+import {
+ keywordValues,
+ properties,
+ units,
+ isValidDeclaration,
+} from "@webstudio-is/css-data";
import type { UnitOption } from "./unit-select";
// To make sorting stable
@@ -62,7 +66,7 @@ export const buildOptions = (
// Special case for 0, which is often used as a unitless value
const showUnitless =
value.type === "unit" || value.type === "intermediate"
- ? isValid(property, `${value.value}`)
+ ? isValidDeclaration(property, `${value.value}`)
: false;
if (
diff --git a/apps/builder/app/builder/features/style-panel/shared/use-style-data.ts b/apps/builder/app/builder/features/style-panel/shared/use-style-data.ts
index ce84b4d0c7..1eec45f6e9 100644
--- a/apps/builder/app/builder/features/style-panel/shared/use-style-data.ts
+++ b/apps/builder/app/builder/features/style-panel/shared/use-style-data.ts
@@ -18,7 +18,7 @@ import {
} from "~/shared/nano-states";
import { selectedBreakpointStore } from "~/shared/nano-states";
// @todo: must be removed, now it's only for compatibility with existing code
-import { parseCssValue } from "./parse-css-value";
+import { parseCssValue } from "@webstudio-is/css-data";
import { useStyleInfo } from "./style-info";
export type StyleUpdate =
diff --git a/apps/builder/package.json b/apps/builder/package.json
index 73e8746bd5..10d5854f33 100644
--- a/apps/builder/package.json
+++ b/apps/builder/package.json
@@ -55,7 +55,6 @@
"@webstudio-is/react-sdk": "workspace:^",
"@webstudio-is/trpc-interface": "workspace:^",
"colord": "^2.9.3",
- "css-tree": "^2.3.1",
"debug": "^4.3.4",
"detect-font": "^0.1.5",
"downshift": "^6.1.7",
@@ -99,7 +98,6 @@
"@jest/globals": "^29.3.1",
"@remix-run/dev": "1.15.0",
"@storybook/react": "^6.5.16",
- "@types/css-tree": "^2.0.0",
"@types/hyphenate-style-name": "^1.0.0",
"@types/lodash.debounce": "^4.0.6",
"@types/react": "^18.0.35",
@@ -111,7 +109,6 @@
"@webstudio-is/jest-config": "workspace:^",
"@webstudio-is/storybook-config": "workspace:^",
"@webstudio-is/tsconfig": "workspace:^",
- "css-tree": "^2.3.0",
"dotenv": "^16.0.0",
"dotenv-cli": "^7.1.0",
"html-tags": "^3.2.0",
diff --git a/package.json b/package.json
index 2e78e935f8..d0843326d7 100644
--- a/package.json
+++ b/package.json
@@ -23,7 +23,7 @@
},
"devDependencies": {
"@storybook/react": "^6.5.16",
- "@types/css-tree": "^2.0.0",
+ "@types/css-tree": "^2.3.1",
"@webstudio-is/eslint-config-custom": "workspace:^",
"nano-staged": "^0.8.0",
"prettier": "2.8.7",
@@ -64,7 +64,8 @@
"react-dom": "18.2.0"
},
"patchedDependencies": {
- "@stitches/[email protected]": "patches/@[email protected]"
+ "@stitches/[email protected]": "patches/@[email protected]",
}
}
}
diff --git a/packages/css-data/bin/mdn-data.ts b/packages/css-data/bin/mdn-data.ts
index 3fbe60c08c..968da06289 100755
--- a/packages/css-data/bin/mdn-data.ts
+++ b/packages/css-data/bin/mdn-data.ts
@@ -1,4 +1,6 @@
/* eslint-disable import/no-internal-modules */
+// eslint-disable-next-line @typescript-eslint/ban-ts-comment
+// @ts-ignore @todo add missing type defitions for definitionSyntax, type DSNode, type CssNode
import { parse, definitionSyntax, type DSNode, type CssNode } from "css-tree";
import properties from "mdn-data/css/properties.json";
import syntaxes from "mdn-data/css/syntaxes.json";
@@ -169,6 +171,8 @@ const walkSyntax = (
return;
}
parsedSyntaxes.add(syntax);
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore @todo add missing type defitions for definitionSyntax
const parsed = definitionSyntax.parse(syntax);
const walk = (node: DSNode) => {
diff --git a/packages/css-data/jest.config.js b/packages/css-data/jest.config.js
new file mode 100644
index 0000000000..e7fc137533
--- /dev/null
+++ b/packages/css-data/jest.config.js
@@ -0,0 +1 @@
+export { default } from "@webstudio-is/jest-config";
diff --git a/packages/css-data/package.json b/packages/css-data/package.json
index 8bb5938e29..e8afcedf3a 100644
--- a/packages/css-data/package.json
+++ b/packages/css-data/package.json
@@ -12,15 +12,17 @@
"build": "build-package",
"build:mdn-data": "tsx ./bin/mdn-data.ts ./src/__generated__ && prettier --write \"./src/__generated__/**/*.ts\"",
"dts": "tsc --declarationDir lib/types",
- "lint": "eslint ./src --ext .ts,.tsx --max-warnings 0"
+ "lint": "eslint ./src --ext .ts,.tsx --max-warnings 0",
+ "test": "NODE_OPTIONS=--experimental-vm-modules jest"
},
"devDependencies": {
- "@types/css-tree": "^2.0.0",
+ "@types/css-tree": "^2.3.1",
"@webstudio-is/scripts": "workspace:^",
+ "@webstudio-is/jest-config": "workspace:^",
"@webstudio-is/tsconfig": "workspace:^",
"camelcase": "^6.3.0",
- "css-tree": "^2.3.1",
"html-tags": "^3.2.0",
+ "jest": "^29.3.1",
"mdn-data": "2.0.30",
"tsx": "^3.12.6",
"type-fest": "^3.7.1",
@@ -43,5 +45,11 @@
],
"license": "MIT",
"private": false,
- "sideEffects": false
+ "sideEffects": false,
+ "dependencies": {
+ "colord": "^2.9.3",
+ "hyphenate-style-name": "^1.0.4",
+ "warn-once": "^0.1.1",
+ "css-tree": "^2.3.1"
+ }
}
diff --git a/packages/css-data/src/index.ts b/packages/css-data/src/index.ts
index a1bfe9592f..9aacf6f67d 100644
--- a/packages/css-data/src/index.ts
+++ b/packages/css-data/src/index.ts
@@ -7,6 +7,12 @@ export * from "./__generated__/keyword-values";
export * from "./__generated__/units";
export * from "./schema";
+// longhand property parsers
+export * from "./property-parsers/index";
+// shorthand property parsers
+export * from "./parse-css-value";
+export * from "./parse-css";
+
import { properties as generatedProperties } from "./__generated__/properties";
// convert to writable to avoid conflicts with schema type
diff --git a/apps/builder/app/builder/features/style-panel/shared/parse-css-value.test.ts b/packages/css-data/src/parse-css-value.test.ts
similarity index 100%
rename from apps/builder/app/builder/features/style-panel/shared/parse-css-value.test.ts
rename to packages/css-data/src/parse-css-value.test.ts
diff --git a/apps/builder/app/builder/features/style-panel/shared/parse-css-value.ts b/packages/css-data/src/parse-css-value.ts
similarity index 86%
rename from apps/builder/app/builder/features/style-panel/shared/parse-css-value.ts
rename to packages/css-data/src/parse-css-value.ts
index 8fa5368d20..f473c7e22e 100644
--- a/apps/builder/app/builder/features/style-panel/shared/parse-css-value.ts
+++ b/packages/css-data/src/parse-css-value.ts
@@ -1,9 +1,10 @@
+import { colord } from "colord";
import * as csstree from "css-tree";
import hyphenate from "hyphenate-style-name";
-import type { StyleProperty, StyleValue, Unit } from "@webstudio-is/css-data";
-import { units, keywordValues } from "@webstudio-is/css-data";
import warnOnce from "warn-once";
-import { colord } from "colord";
+import { keywordValues } from "./__generated__/keyword-values";
+import { units } from "./__generated__/units";
+import type { StyleProperty, StyleValue, Unit } from "./schema";
const cssTryParseValue = (input: string) => {
try {
@@ -14,7 +15,10 @@ const cssTryParseValue = (input: string) => {
}
};
-export const isValid = (property: string, value: string): boolean => {
+export const isValidDeclaration = (
+ property: string,
+ value: string
+): boolean => {
const ast = cssTryParseValue(value);
if (ast == null) {
@@ -25,10 +29,10 @@ export const isValid = (property: string, value: string): boolean => {
const matchResult = csstree.lexer.matchProperty(cssPropertyName, ast);
- const isValid = matchResult.matched != null;
+ const isValidDeclaration = matchResult.matched != null;
// @todo remove after fix https://github.com/csstree/csstree/issues/246
- if (isValid && typeof CSSStyleValue !== "undefined") {
+ if (isValidDeclaration && typeof CSSStyleValue !== "undefined") {
try {
CSSStyleValue.parse(cssPropertyName, value);
} catch {
@@ -41,11 +45,11 @@ export const isValid = (property: string, value: string): boolean => {
}
}
- return isValid;
+ return isValidDeclaration;
};
export const parseCssValue = (
- property: StyleProperty,
+ property: StyleProperty, // Handles only long-hand values.
input: string
): StyleValue => {
const invalidValue = {
@@ -57,7 +61,7 @@ export const parseCssValue = (
return invalidValue;
}
- if (!isValid(property, input)) {
+ if (!isValidDeclaration(property, input)) {
return invalidValue;
}
diff --git a/packages/css-data/src/parse-css.test.ts b/packages/css-data/src/parse-css.test.ts
new file mode 100644
index 0000000000..a748396f4f
--- /dev/null
+++ b/packages/css-data/src/parse-css.test.ts
@@ -0,0 +1,101 @@
+import { describe, expect, test } from "@jest/globals";
+import { parseCss } from "./parse-css";
+
+describe("Parse CSS", () => {
+ test("one class selector rules", () => {
+ expect(parseCss(`.test { color: #ff0000 }`)).toMatchInlineSnapshot(`
+ {
+ "test": [
+ {
+ "property": "color",
+ "value": {
+ "alpha": 1,
+ "b": 0,
+ "g": 0,
+ "r": 255,
+ "type": "rgb",
+ },
+ },
+ ],
+ }
+ `);
+ });
+
+ test("parses supported shorthand values", () => {
+ expect(
+ parseCss(
+ `.test { background: #ff0000 linear-gradient(180deg, #11181C 0%, rgba(17, 24, 28, 0) 36.09%), #EBFFFC; }`
+ ).test
+ ).toMatchInlineSnapshot(`
+ [
+ {
+ "property": "backgroundImage",
+ "value": {
+ "type": "layers",
+ "value": [
+ {
+ "type": "unparsed",
+ "value": "linear-gradient(180deg,#11181C 0%,rgba(17,24,28,0) 36.09%)",
+ },
+ ],
+ },
+ },
+ {
+ "property": "backgroundColor",
+ "value": {
+ "alpha": 1,
+ "b": 252,
+ "g": 255,
+ "r": 235,
+ "type": "rgb",
+ },
+ },
+ ]
+ `);
+ });
+
+ test("parses unsupported shorthand values", () => {
+ expect(parseCss(`.test { padding: 4px }`).test[0]).toMatchInlineSnapshot(`
+ {
+ "property": "padding",
+ "value": {
+ "type": "unit",
+ "unit": "px",
+ "value": 4,
+ },
+ }
+ `);
+ });
+
+ test("complex selector rules", () => {
+ expect(parseCss(`.test, a, .test2, .test:hover { color: #ff0000 }`))
+ .toMatchInlineSnapshot(`
+ {
+ "test": [
+ {
+ "property": "color",
+ "value": {
+ "alpha": 1,
+ "b": 0,
+ "g": 0,
+ "r": 255,
+ "type": "rgb",
+ },
+ },
+ ],
+ "test2": [
+ {
+ "property": "color",
+ "value": {
+ "alpha": 1,
+ "b": 0,
+ "g": 0,
+ "r": 255,
+ "type": "rgb",
+ },
+ },
+ ],
+ }
+ `);
+ });
+});
diff --git a/packages/css-data/src/parse-css.ts b/packages/css-data/src/parse-css.ts
new file mode 100644
index 0000000000..6f7e78f586
--- /dev/null
+++ b/packages/css-data/src/parse-css.ts
@@ -0,0 +1,124 @@
+import * as csstree from "css-tree";
+import { parseCssValue as parseCssValueLonghand } from "./parse-css-value";
+import * as parsers from "./property-parsers/parsers";
+import * as toLonghand from "./property-parsers/to-longhand";
+import { StyleValue, type Style as S, type StyleProperty } from "./schema";
+
+type Selector = string;
+type Style = {
+ // @todo add support for states and media queries in addition to declarations
+ property: StyleProperty;
+ value: StyleValue;
+};
+
+export type Styles = Record<Selector, Style[]>;
+
+type Longhand = keyof typeof toLonghand;
+
+const parseCssValue = function parseCssValue(
+ property: Longhand | StyleProperty,
+ value: string
+): S {
+ const unwrap = toLonghand[property as Longhand];
+
+ if (typeof unwrap === "function") {
+ const longhands = unwrap(value);
+
+ return Object.fromEntries(
+ Object.entries(longhands).map(([property, value]) => {
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
+ // @ts-ignore @todo remove this ignore: property is a `keyof typeof longhands` which is a key in parsers but TS can't infer the link
+ const valueParser = parsers[property];
+
+ if (typeof valueParser === "function") {
+ return [property, valueParser(value)];
+ }
+
+ if (Array.isArray(value)) {
+ return [
+ property,
+ {
+ type: "invalid",
+ value: value.join(""),
+ },
+ ];
+ }
+
+ if (!value) {
+ return [property, { type: "invalid", value: "" }];
+ }
+
+ return [
+ property,
+ parseCssValueLonghand(property as StyleProperty, value),
+ ];
+ })
+ );
+ }
+
+ return {
+ [property]: parseCssValueLonghand(property as StyleProperty, value),
+ };
+};
+
+export const parseCss = function cssToWS(css: string) {
+ const ast = csstree.parse(css);
+
+ let selectors: Selector[] = [];
+ const styles: Styles = {};
+
+ csstree.walk(ast, (node, item) => {
+ if (node.type === "SelectorList") {
+ selectors = [];
+ }
+ if (node.type === "ClassSelector") {
+ if (!item.prev && !item.next) {
+ selectors.push(node.name);
+ }
+ return;
+ }
+
+ if (node.type === "Declaration") {
+ const stringValue = csstree.generate(node.value);
+
+ const parsedCss = parseCssValue(
+ node.property as Longhand | StyleProperty,
+ stringValue
+ );
+
+ (Object.entries(parsedCss) as [StyleProperty, StyleValue][]).forEach(
+ ([property, value]) => {
+ try {
+ StyleValue.parse(value);
+ selectors.forEach((selector) => {
+ if (Array.isArray(styles[selector])) {
+ styles[selector].push({
+ property,
+ value,
+ });
+ } else {
+ styles[selector] = [{ property, value }];
+ }
+ });
+ } catch (error) {
+ if (process.env.NODE_ENV !== "production") {
+ // eslint-disable-next-line no-console
+ console.warn(
+ true,
+ `Declaration parsing for \`${selectors.join(", ")}.${
+ node.property
+ }: ${stringValue}\` failed:\n\n${JSON.stringify(
+ parsedCss,
+ null,
+ 2
+ )}`
+ );
+ }
+ }
+ }
+ );
+ }
+ });
+
+ return styles;
+};
diff --git a/packages/css-data/src/property-parsers/README.md b/packages/css-data/src/property-parsers/README.md
new file mode 100644
index 0000000000..b4da473a39
--- /dev/null
+++ b/packages/css-data/src/property-parsers/README.md
@@ -0,0 +1,11 @@
+## Properties Parsers
+
+This folder contains:
+
+- `parsers.ts` individual properties parses
+- `to-longhand.ts` shorthand forms unwrappers
+
+The idea is that these two modules export a number of utilities whose name match the property that they parse or unwrap e.g.:
+
+- `backgroundImage` is a property parser
+- `background` is a "to-longhand" unwrapper
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-parser.test.ts b/packages/css-data/src/property-parsers/background.test.ts
similarity index 97%
rename from apps/builder/app/builder/features/style-panel/sections/backgrounds/background-parser.test.ts
rename to packages/css-data/src/property-parsers/background.test.ts
index c3463f7508..75e5f6489f 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-parser.test.ts
+++ b/packages/css-data/src/property-parsers/background.test.ts
@@ -1,6 +1,6 @@
-import { describe, test, expect } from "@jest/globals";
+import { describe, expect, test } from "@jest/globals";
-import { parseBackground } from "./background-parser";
+import { parseBackground } from "./background";
describe("parseBackground", () => {
test("parse background from figma", () => {
@@ -176,7 +176,7 @@ describe("parseBackground", () => {
"backgroundColor": undefined,
"backgroundImage": {
"type": "invalid",
- "value": "linear-gradient(180deg,",
+ "value": "linear-gradient(180deg,)",
},
}
`);
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-parser.ts b/packages/css-data/src/property-parsers/background.ts
similarity index 73%
rename from apps/builder/app/builder/features/style-panel/sections/backgrounds/background-parser.ts
rename to packages/css-data/src/property-parsers/background.ts
index 347df3cf0a..775d71c394 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-parser.ts
+++ b/packages/css-data/src/property-parsers/background.ts
@@ -1,12 +1,11 @@
import * as csstree from "css-tree";
-import { colord } from "colord";
-import { parseCssValue } from "../../shared/parse-css-value";
+import { parseCssValue } from "../parse-css-value";
import type {
InvalidValue,
LayersValue,
RgbValue,
UnparsedValue,
-} from "@webstudio-is/css-data";
+} from "../schema";
export const gradientNames = [
"conic-gradient",
@@ -22,6 +21,28 @@ export const parseBackground = (
): {
backgroundImage: LayersValue | InvalidValue;
backgroundColor: RgbValue | undefined;
+} => {
+ const { backgroundImage, backgroundColor: backgroundColorRaw } =
+ backgroundToLonghand(background);
+
+ const backgroundColor = backgroundColorRaw
+ ? (parseCssValue("backgroundColor", backgroundColorRaw) as RgbValue)
+ : undefined;
+
+ return {
+ backgroundImage: parseBackgroundImage(backgroundImage),
+ backgroundColor:
+ backgroundColor && backgroundColor.type === "rgb"
+ ? backgroundColor
+ : undefined,
+ };
+};
+
+export const backgroundToLonghand = (
+ background: string
+): {
+ backgroundImage: string[];
+ backgroundColor: string | undefined;
} => {
const layers: string[] = [];
@@ -77,6 +98,15 @@ export const parseBackground = (
},
});
+ return {
+ backgroundImage: layers,
+ backgroundColor: backgroundColorRaw,
+ };
+};
+
+export const parseBackgroundImage = (
+ layers: string[]
+): LayersValue | InvalidValue => {
const backgroundImages: UnparsedValue[] = [];
for (const layer of layers) {
@@ -96,28 +126,7 @@ export const parseBackground = (
backgroundImages.push(layerStyle);
}
- let backgroundColor: RgbValue | undefined;
-
- if (backgroundColorRaw !== undefined) {
- const colordValue = colord(backgroundColorRaw);
-
- if (colordValue.isValid()) {
- const rgb = colordValue.toRgb();
- backgroundColor = {
- type: "rgb",
- r: rgb.r,
- g: rgb.g,
- b: rgb.b,
- alpha: rgb.a ?? 1,
- };
- }
- }
-
- return {
- backgroundImage:
- backgroundImages.length > 0
- ? { type: "layers", value: backgroundImages }
- : { type: "invalid", value: background },
- backgroundColor,
- };
+ return backgroundImages.length > 0
+ ? { type: "layers", value: backgroundImages }
+ : { type: "invalid", value: layers.join(",") };
};
diff --git a/packages/css-data/src/property-parsers/index.ts b/packages/css-data/src/property-parsers/index.ts
new file mode 100644
index 0000000000..f9c0d37739
--- /dev/null
+++ b/packages/css-data/src/property-parsers/index.ts
@@ -0,0 +1 @@
+export * from "./background";
diff --git a/packages/css-data/src/property-parsers/parsers.ts b/packages/css-data/src/property-parsers/parsers.ts
new file mode 100644
index 0000000000..6b947c4ec2
--- /dev/null
+++ b/packages/css-data/src/property-parsers/parsers.ts
@@ -0,0 +1,4 @@
+/*
+ Longhand properties parsers. Each named export should match a property name.
+*/
+export { parseBackgroundImage as backgroundImage } from "./background";
diff --git a/packages/css-data/src/property-parsers/to-longhand.ts b/packages/css-data/src/property-parsers/to-longhand.ts
new file mode 100644
index 0000000000..ba2d1fc36a
--- /dev/null
+++ b/packages/css-data/src/property-parsers/to-longhand.ts
@@ -0,0 +1,4 @@
+/*
+ Shorthand properties unwrappers. Each named export should match a property name.
+*/
+export { backgroundToLonghand as background } from "./background";
diff --git a/packages/css-data/tsconfig.json b/packages/css-data/tsconfig.json
index bac8765395..4a738235bd 100644
--- a/packages/css-data/tsconfig.json
+++ b/packages/css-data/tsconfig.json
@@ -1,6 +1,6 @@
{
"extends": "@webstudio-is/tsconfig/base.json",
- "include": ["src", "bin"],
+ "include": ["src", "bin", "../../@types/**/*.d.ts"],
"compilerOptions": {
"module": "ES2020",
"target": "ES2020",
diff --git a/packages/css-engine/tsconfig.json b/packages/css-engine/tsconfig.json
index 243d0954f6..ddff9acf9e 100644
--- a/packages/css-engine/tsconfig.json
+++ b/packages/css-engine/tsconfig.json
@@ -1,6 +1,6 @@
{
"extends": "@webstudio-is/tsconfig/base.json",
- "include": ["src"],
+ "include": ["src", "../../@types/**/*.d.ts"],
"compilerOptions": {
"module": "ES2020",
"target": "ES2020",
diff --git a/packages/form-handlers/tsconfig.json b/packages/form-handlers/tsconfig.json
index 74b8f3611b..292c6d9f01 100644
--- a/packages/form-handlers/tsconfig.json
+++ b/packages/form-handlers/tsconfig.json
@@ -1,6 +1,6 @@
{
"extends": "@webstudio-is/tsconfig/base.json",
- "include": ["src"],
+ "include": ["src", "../../@types/**/*.d.ts"],
"compilerOptions": {
"module": "ES2020",
"target": "ES2020"
diff --git a/packages/project-build/tsconfig.json b/packages/project-build/tsconfig.json
index 74b8f3611b..292c6d9f01 100644
--- a/packages/project-build/tsconfig.json
+++ b/packages/project-build/tsconfig.json
@@ -1,6 +1,6 @@
{
"extends": "@webstudio-is/tsconfig/base.json",
- "include": ["src"],
+ "include": ["src", "../../@types/**/*.d.ts"],
"compilerOptions": {
"module": "ES2020",
"target": "ES2020"
diff --git a/patches/[email protected] b/patches/[email protected]
new file mode 100644
index 0000000000..5a7894d523
--- /dev/null
+++ b/patches/[email protected]
@@ -0,0 +1,18 @@
+diff --git a/package.json b/package.json
+index 934e699f19c6f1a265c1ef29121c6dde92cd25ed..a54e28a44de8e1adcf775be9564c2ad8b15b5c98 100644
+--- a/package.json
++++ b/package.json
+@@ -18,11 +18,11 @@
+ "validation"
+ ],
+ "type": "module",
+- "module": "./lib/index.js",
++ "module": "./dist/csstree.esm.js",
+ "main": "./cjs/index.cjs",
+ "exports": {
+ ".": {
+- "import": "./lib/index.js",
++ "import": "./dist/csstree.esm.js",
+ "require": "./cjs/index.cjs"
+ },
+ "./dist/*": "./dist/*.js",
\ No newline at end of file
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 889539d58d..09d1db5ce4 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -9,6 +9,9 @@ patchedDependencies:
'@stitches/[email protected]':
hash: knml42mpr6mlzseo3d6gjljiq4
path: patches/@[email protected]
+ hash: epgcmebti7rfrc2ej4odb3t4jy
+ path: patches/[email protected]
importers:
@@ -18,8 +21,8 @@ importers:
specifier: ^6.5.16
'@types/css-tree':
- specifier: ^2.0.0
- version: 2.0.0
+ specifier: ^2.3.1
+ version: 2.3.1
'@webstudio-is/eslint-config-custom':
specifier: workspace:^
version: link:packages/eslint-config-custom
@@ -158,9 +161,6 @@ importers:
colord:
specifier: ^2.9.3
version: 2.9.3
- css-tree:
- specifier: ^2.3.1
- version: 2.3.1
debug:
specifier: ^4.3.4
version: 4.3.4
@@ -285,9 +285,6 @@ importers:
'@storybook/react':
specifier: ^6.5.16
- '@types/css-tree':
- specifier: ^2.0.0
- version: 2.0.0
'@types/hyphenate-style-name':
specifier: ^1.0.0
version: 1.0.0
@@ -433,10 +430,26 @@ importers:
version: 5.0.3
packages/css-data:
+ dependencies:
+ colord:
+ specifier: ^2.9.3
+ version: 2.9.3
+ css-tree:
+ specifier: ^2.3.1
+ version: 2.3.1(patch_hash=epgcmebti7rfrc2ej4odb3t4jy)
+ hyphenate-style-name:
+ specifier: ^1.0.4
+ version: 1.0.4
+ warn-once:
+ specifier: ^0.1.1
+ version: 0.1.1
devDependencies:
'@types/css-tree':
- specifier: ^2.0.0
- version: 2.0.0
+ specifier: ^2.3.1
+ version: 2.3.1
+ '@webstudio-is/jest-config':
+ specifier: workspace:^
+ version: link:../jest-config
'@webstudio-is/scripts':
specifier: workspace:^
version: link:../scripts
@@ -446,12 +459,12 @@ importers:
camelcase:
specifier: ^6.3.0
version: 6.3.0
- css-tree:
- specifier: ^2.3.1
- version: 2.3.1
html-tags:
specifier: ^3.2.0
version: 3.2.0
+ jest:
+ specifier: ^29.3.1
+ version: 29.3.1(@types/[email protected])
mdn-data:
specifier: 2.0.30
version: 2.0.30
@@ -8302,7 +8315,7 @@ packages:
resolution: {integrity: sha512-0e4B41q+GnS8TI+SWHQw78ziW6V07Li2TMLS8FwbvKOTIJL0ua1Sat1AI0AFAjHlAePz9b+1VtyJzv1516edJw==}
hasBin: true
dependencies:
- css-tree: 2.3.1
+ css-tree: 2.3.1(patch_hash=epgcmebti7rfrc2ej4odb3t4jy)
svgo: 3.0.2
dev: true
@@ -8417,8 +8430,8 @@ packages:
resolution: {integrity: sha512-XW/Aa8APYr6jSVVA1y/DEIZX0/GMKLEVekNG727R8cs56ahETkRAy/3DR7+fJyh7oUgGwNQaRfXCun0+KbWY7Q==}
- resolution: {integrity: sha512-mY2sXRLBnUPMYw6mkOT+6dABeaNxAEKZz6scE9kQPNJx8fKe1fOsm8Honl7+xFYe6TKX8WNk2+7oMp2vBArJ9Q==}
+ resolution: {integrity: sha512-3m636Jz4d9d+lHVMp6FNLsUWQrfOx1xpm1SBxPbQYSNNgXMe+XswcsDeo1ldyULiuzYyWKk1kmvkLTgNq+215Q==}
dev: true
@@ -10837,12 +10850,13 @@ packages:
source-map-js: 1.0.2
dev: true
+ /[email protected](patch_hash=epgcmebti7rfrc2ej4odb3t4jy):
resolution: {integrity: sha512-6Fv1DV/TYw//QF5IzQdqsNDjx/wc8TrMBZsqjL9eW01tWb7R7k/mq+/VXfJCl7SoD5emsJop9cOByJZfs8hYIw==}
engines: {node: ^10 || ^12.20.0 || ^14.13.0 || >=15.0.0}
dependencies:
mdn-data: 2.0.30
source-map-js: 1.0.2
+ patched: true
resolution: {integrity: sha512-arSMRWIIFY0hV8pIxZMEfmMI47Wj3R/aWpZDDxWYCPEiOMv6tfOrnpDtgxBYPEQD4V0Y/958+1TdC3iWTFcUPw==}
@@ -18647,7 +18661,7 @@ packages:
'@trysound/sax': 0.2.0
commander: 7.2.0
css-select: 5.1.0
- css-tree: 2.3.1
+ css-tree: 2.3.1(patch_hash=epgcmebti7rfrc2ej4odb3t4jy)
csso: 5.0.5
picocolors: 1.0.0
dev: true