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: add universal css fragment parser (#3712)

Bogdan Chadkinae35b7d8f02a
diff --git a/apps/builder/app/builder/features/style-panel/sections/backdrop-filter/backdrop-filter.tsx b/apps/builder/app/builder/features/style-panel/sections/backdrop-filter/backdrop-filter.tsx
index 7a7042605f..421679af33 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backdrop-filter/backdrop-filter.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backdrop-filter/backdrop-filter.tsx
@@ -1,6 +1,6 @@
import { CollapsibleSectionRoot } from "~/builder/shared/collapsible-section";
import type { SectionProps } from "../shared/section";
-import type { StyleProperty } from "@webstudio-is/css-engine";
+import type { LayersValue, StyleProperty } from "@webstudio-is/css-engine";
import { useState } from "react";
import {
SectionTitle,
@@ -10,12 +10,12 @@ import {
Flex,
Text,
} from "@webstudio-is/design-system";
+import { parseCssValue } from "@webstudio-is/css-data";
import { PropertyName } from "../../shared/property-name";
import { getStyleSource } from "../../shared/style-info";
import { getDots } from "../../shared/collapsible-section";
import { InfoCircleIcon, PlusIcon } from "@webstudio-is/icons";
import { addLayer } from "../../style-layer-utils";
-import { parseFilter } from "@webstudio-is/css-data";
import { LayersList } from "../../style-layers-list";
import { FilterSectionContent } from "../../shared/filter-content";
@@ -50,7 +50,10 @@ export const Section = (props: SectionProps) => {
onClick={() => {
addLayer(
property,
- parseFilter(property, initialBackdropFilter),
+ parseCssValue(
+ property,
+ initialBackdropFilter
+ ) as LayersValue,
currentStyle,
props.createBatchUpdate
);
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 82149ad873..ecf29ede7f 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,5 +1,5 @@
import type { InvalidValue, StyleValue } from "@webstudio-is/css-engine";
-import { parseCssValue, parseBackground } from "@webstudio-is/css-data";
+import { parseCssValue } from "@webstudio-is/css-data";
import {
Flex,
TextArea,
@@ -9,6 +9,7 @@ import {
import { useEffect, useRef, useState } from "react";
import type { ControlProps } from "../../controls";
import { NonResetablePropertyName } from "../../shared/property-name";
+import { parseCssFragment } from "../../shared/parse-css-fragment";
type IntermediateValue = {
type: "intermediate";
@@ -62,9 +63,9 @@ export const BackgroundGradient = (
return;
}
- const { backgroundImage, backgroundColor } = parseBackground(
- intermediateValue.value
- );
+ const parsed = parseCssFragment(intermediateValue.value, "background");
+ const backgroundImage = parsed.get("backgroundImage");
+ const backgroundColor = parsed.get("backgroundColor");
const [layer] =
backgroundImage?.type === "layers"
? backgroundImage.value
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-image.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-image.tsx
index 5c974d5b18..db6371290b 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-image.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-image.tsx
@@ -1,4 +1,4 @@
-import { InvalidValue, type StyleProperty } from "@webstudio-is/css-engine";
+import { InvalidValue } from "@webstudio-is/css-engine";
import {
TextArea,
textVariants,
@@ -13,11 +13,9 @@ import { useRef, useState, useEffect } from "react";
import type { ControlProps } from "../../controls";
import { useStore } from "@nanostores/react";
import { $assets } from "~/shared/nano-states";
-import { parseCssValue } from "@webstudio-is/css-data";
import type { StyleUpdateOptions } from "../../shared/use-style-data";
import { InfoCircleIcon } from "@webstudio-is/icons";
-
-const property: StyleProperty = "backgroundImage";
+import { parseCssFragment } from "../../shared/parse-css-fragment";
type IntermediateValue = {
type: "intermediate";
@@ -37,7 +35,7 @@ export const BackgroundImage = (
) => {
const textAreaRef = useRef<HTMLTextAreaElement>(null);
const assets = useStore($assets);
- const styleValue = props.currentStyle[property]?.value;
+ const styleValue = props.currentStyle.backgroundImage?.value;
const [errors, setErrors] = useState<string[]>([]);
const [intermediateValue, setIntermediateValue] = useState<
IntermediateValue | InvalidValue | undefined
@@ -88,8 +86,10 @@ export const BackgroundImage = (
value: value,
});
- const newValue = parseCssValue(property, value);
- if (newValue.type === "invalid") {
+ const parsed = parseCssFragment(value, "background");
+ const newValue = parsed.get("backgroundImage");
+
+ if (newValue === undefined || newValue?.type === "invalid") {
setIntermediateValue({
type: "invalid",
value: value,
@@ -100,7 +100,7 @@ export const BackgroundImage = (
const [layer] = newValue.type === "layers" ? newValue.value : [newValue];
if (layer?.type === "keyword") {
setIntermediateValue(undefined);
- props.setProperty(property)(layer, options);
+ props.setProperty("backgroundImage")(layer, options);
}
if (layer?.type !== "image" || layer.value.type !== "url") {
setIntermediateValue({
@@ -112,7 +112,7 @@ export const BackgroundImage = (
const url = layer.value.url;
if (isAbsoluteURL(url) === true) {
- props.setProperty(property)(layer, options);
+ props.setProperty("backgroundImage")(layer, options);
} else {
const usedAsset = Array.from($assets.get().values()).find(
(asset) => asset.type === "image" && asset.name === url
@@ -128,7 +128,7 @@ export const BackgroundImage = (
}
setErrors([]);
- props.setProperty(property)(
+ props.setProperty("backgroundImage")(
{
type: "image",
value: {
@@ -189,7 +189,7 @@ export const BackgroundImage = (
}
if (event.key === "Escape") {
- props.deleteProperty(property, { isEphemeral: true });
+ props.deleteProperty("backgroundImage", { isEphemeral: true });
setIntermediateValue(undefined);
event.preventDefault();
}
diff --git a/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx b/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx
index 37a4018497..2a012dbf48 100644
--- a/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/box-shadows/box-shadows.tsx
@@ -6,7 +6,7 @@ import {
Text,
} from "@webstudio-is/design-system";
import { InfoCircleIcon, PlusIcon } from "@webstudio-is/icons";
-import type { StyleProperty } from "@webstudio-is/css-engine";
+import type { LayersValue, StyleProperty } from "@webstudio-is/css-engine";
import { CollapsibleSectionRoot } from "~/builder/shared/collapsible-section";
import { useState } from "react";
import { getDots } from "../../shared/collapsible-section";
@@ -15,7 +15,7 @@ import { getStyleSource } from "../../shared/style-info";
import type { SectionProps } from "../shared/section";
import { LayersList } from "../../style-layers-list";
import { addLayer } from "../../style-layer-utils";
-import { parseShadow } from "@webstudio-is/css-data";
+import { parseCssValue } from "@webstudio-is/css-data";
import { ShadowContent } from "../../shared/shadow-content";
export const properties = ["boxShadow"] satisfies Array<StyleProperty>;
@@ -48,7 +48,7 @@ export const Section = (props: SectionProps) => {
onClick={() => {
addLayer(
property,
- parseShadow(property, initialBoxShadow),
+ parseCssValue(property, initialBoxShadow) as LayersValue,
currentStyle,
props.createBatchUpdate
);
diff --git a/apps/builder/app/builder/features/style-panel/sections/filter/filter.tsx b/apps/builder/app/builder/features/style-panel/sections/filter/filter.tsx
index fde1b607dc..9669867bd6 100644
--- a/apps/builder/app/builder/features/style-panel/sections/filter/filter.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/filter/filter.tsx
@@ -9,14 +9,14 @@ import {
Tooltip,
Text,
} from "@webstudio-is/design-system";
+import { parseCssValue } from "@webstudio-is/css-data";
import { getDots } from "../../shared/collapsible-section";
import { PropertyName } from "../../shared/property-name";
import { InfoCircleIcon, PlusIcon } from "@webstudio-is/icons";
-import { type StyleProperty } from "@webstudio-is/css-engine";
+import { LayersValue, type StyleProperty } from "@webstudio-is/css-engine";
import { getStyleSource } from "../../shared/style-info";
import { LayersList } from "../../style-layers-list";
import { addLayer } from "../../style-layer-utils";
-import { parseFilter } from "@webstudio-is/css-data";
import { FilterSectionContent } from "../../shared/filter-content";
export const properties = ["filter"] satisfies Array<StyleProperty>;
@@ -50,7 +50,7 @@ export const Section = (props: SectionProps) => {
onClick={() => {
addLayer(
property,
- parseFilter(property, initialFilter),
+ parseCssValue(property, initialFilter) as LayersValue,
currentStyle,
props.createBatchUpdate
);
diff --git a/apps/builder/app/builder/features/style-panel/sections/text-shadows/text-shadows.tsx b/apps/builder/app/builder/features/style-panel/sections/text-shadows/text-shadows.tsx
index 5f92aa8ec0..b2b5a6ba08 100644
--- a/apps/builder/app/builder/features/style-panel/sections/text-shadows/text-shadows.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/text-shadows/text-shadows.tsx
@@ -1,6 +1,6 @@
import { CollapsibleSectionRoot } from "~/builder/shared/collapsible-section";
import type { SectionProps } from "../shared/section";
-import type { StyleProperty } from "@webstudio-is/css-engine";
+import type { LayersValue, StyleProperty } from "@webstudio-is/css-engine";
import { useState } from "react";
import {
SectionTitle,
@@ -11,7 +11,7 @@ import {
} from "@webstudio-is/design-system";
import { InfoCircleIcon, PlusIcon } from "@webstudio-is/icons";
import { addLayer } from "../../style-layer-utils";
-import { parseShadow } from "@webstudio-is/css-data";
+import { parseCssValue } from "@webstudio-is/css-data";
import { getDots } from "../../shared/collapsible-section";
import { PropertyName } from "../../shared/property-name";
import { getStyleSource } from "../../shared/style-info";
@@ -48,7 +48,7 @@ export const Section = (props: SectionProps) => {
onClick={() => {
addLayer(
property,
- parseShadow(property, initialTextShadow),
+ parseCssValue(property, initialTextShadow) as LayersValue,
currentStyle,
createBatchUpdate
);
diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-content.tsx b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-content.tsx
index 9224536d5f..2eb17289ec 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-content.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-content.tsx
@@ -7,6 +7,8 @@ import {
type KeywordValue,
type UnitValue,
type StyleProperty,
+ type StyleValue,
+ type TupleValueItem,
} from "@webstudio-is/css-engine";
import {
Flex,
@@ -39,9 +41,9 @@ import {
defaultTransitionTimingFunction,
defaultTransitionDelay,
deleteTransitionLayer,
- parseTransitionShorthandToLayers,
} from "./transition-utils";
import type { StyleInfo } from "../../shared/style-info";
+import { parseCssFragment } from "../../shared/parse-css-fragment";
type TransitionContentProps = {
index: number;
@@ -64,6 +66,9 @@ type TransitionContentProps = {
// We removed transition from properties list to drop support from advanced tab and so the typecasting.
const shortHandTransitionProperty = "transition" as StyleProperty;
+const getLayer = (value: undefined | StyleValue, index: number) =>
+ value?.type === "layers" ? value.value[index] : undefined;
+
export const TransitionContent = ({
layer,
index,
@@ -95,19 +100,27 @@ export const TransitionContent = ({
return;
}
- const layerValue = parseCssValue(
- shortHandTransitionProperty,
- intermediateValue.value
- );
- if (layerValue.type === "invalid") {
+ const parsed = parseCssFragment(intermediateValue.value, "transition");
+ const tuple: TupleValue = {
+ type: "tuple",
+ value: [
+ getLayer(parsed.get("transitionProperty"), 0),
+ getLayer(parsed.get("transitionDuration"), 0),
+ getLayer(parsed.get("transitionTimingFunction"), 0),
+ getLayer(parsed.get("transitionDelay"), 0),
+ // @todo getLayer(parsed.get("transitionBehavior"), 0),
+ ].filter(Boolean) as TupleValueItem[],
+ };
+ if (tuple.value.length === 0) {
setIntermediateValue({
type: "invalid",
value: intermediateValue.value,
});
- return;
}
-
- const layers = parseTransitionShorthandToLayers(intermediateValue.value);
+ const layers: LayersValue = {
+ type: "layers",
+ value: [tuple],
+ };
onEditLayer(index, layers, options);
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.ts b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.ts
index 08da9e5f7b..ac776b4b7b 100644
--- a/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.ts
+++ b/apps/builder/app/builder/features/style-panel/sections/transitions/transition-utils.ts
@@ -3,7 +3,6 @@ import {
UnparsedValue,
type KeywordValue,
type LayersValue,
- type StyleProperty,
type TupleValue,
type UnitValue,
} from "@webstudio-is/css-engine";
@@ -13,14 +12,10 @@ import type {
StyleUpdateOptions,
} from "../../shared/use-style-data";
import {
- expandShorthands,
extractTransitionProperties,
- isTransitionLongHandProperty,
isValidTransitionValue,
- parseCssValue,
transitionLongHandProperties,
} from "@webstudio-is/css-data";
-import { camelCase } from "change-case";
export const defaultTransitionProperty: UnparsedValue = {
type: "unparsed",
@@ -408,28 +403,3 @@ export const hideTransitionLayer = (props: {
batch.publish();
};
-
-export const parseTransitionShorthandToLayers = (
- transition: string
-): LayersValue => {
- const longhands = expandShorthands([["transition", transition]]);
-
- const layerTuple: TupleValue = { type: "tuple", value: [] };
- for (const [hyphenedProperty, value] of longhands) {
- const longhandProperty = camelCase(hyphenedProperty) as StyleProperty;
- const longhandValue = parseCssValue(longhandProperty, value);
-
- if (
- longhandValue.type === "layers" &&
- isTransitionLongHandProperty(longhandProperty) &&
- isValidTransitionValue(longhandValue.value[0])
- ) {
- layerTuple.value.push(longhandValue.value[0]);
- }
- }
-
- return {
- type: "layers",
- value: [layerTuple],
- };
-};
diff --git a/apps/builder/app/builder/features/style-panel/shared/filter-content.tsx b/apps/builder/app/builder/features/style-panel/shared/filter-content.tsx
index fe3ac5b5e6..16194e9786 100644
--- a/apps/builder/app/builder/features/style-panel/shared/filter-content.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/filter-content.tsx
@@ -1,3 +1,4 @@
+import { camelCase } from "change-case";
import {
type InvalidValue,
type TupleValue,
@@ -20,12 +21,13 @@ import {
CssValueInputContainer,
type IntermediateStyleValue,
} from "../shared/css-value-input";
-import { parseCssValue, parseFilter } from "@webstudio-is/css-data";
+import { parseCssValue } from "@webstudio-is/css-data";
import type {
DeleteProperty,
StyleUpdateOptions,
} from "../shared/use-style-data";
import { ShadowContent } from "./shadow-content";
+import { parseCssFragment } from "./parse-css-fragment";
// filters can't be validated directly in the css-engine. Because, these are not properties
// but functions that proeprties accept. So, we need to validate them manually using fake proeprties
@@ -150,17 +152,16 @@ export const FilterSectionContent = ({
value: string,
options: StyleUpdateOptions = { isEphemeral: false }
) => {
- const layers = parseFilter(property, value);
+ const parsed = parseCssFragment(value, camelCase(property));
+ const parsedValue = parsed.get(property);
+ const invalid = parsedValue === undefined || parsedValue.type === "invalid";
setIntermediateValue({
- type: layers.type === "invalid" ? "invalid" : "intermediate",
+ type: invalid ? "invalid" : "intermediate",
value,
});
-
- if (layers.type === "invalid") {
- return;
+ if (parsedValue?.type === "tuple") {
+ onEditLayer(index, parsedValue, options);
}
-
- onEditLayer(index, layers, options);
};
return (
diff --git a/apps/builder/app/builder/features/style-panel/shared/parse-css-fragment.test.ts b/apps/builder/app/builder/features/style-panel/shared/parse-css-fragment.test.ts
new file mode 100644
index 0000000000..1c4f90e8dd
--- /dev/null
+++ b/apps/builder/app/builder/features/style-panel/shared/parse-css-fragment.test.ts
@@ -0,0 +1,75 @@
+import { expect, test } from "@jest/globals";
+import { parseCssFragment } from "./parse-css-fragment";
+
+test("parse shorthand property", () => {
+ const result = new Map([
+ [
+ "transitionProperty",
+ {
+ type: "layers",
+ value: [{ type: "unparsed", value: "opacity" }],
+ },
+ ],
+ [
+ "transitionDuration",
+ {
+ type: "layers",
+ value: [{ type: "unit", unit: "s", value: 1 }],
+ },
+ ],
+ [
+ "transitionTimingFunction",
+ {
+ type: "layers",
+ value: [{ type: "keyword", value: "ease" }],
+ },
+ ],
+ [
+ "transitionDelay",
+ {
+ type: "layers",
+ value: [{ type: "unit", unit: "s", value: 0 }],
+ },
+ ],
+ [
+ "transitionBehavior",
+ {
+ type: "layers",
+ value: [{ type: "keyword", value: "normal" }],
+ },
+ ],
+ ]);
+ expect(parseCssFragment("opacity 1s", "transition")).toEqual(result);
+ expect(parseCssFragment("transition: opacity 1s", "transition")).toEqual(
+ result
+ );
+});
+
+test("parse longhand properties", () => {
+ expect(
+ parseCssFragment(
+ `
+ transition-property: opacity;
+ transition-duration: 1s;
+ `,
+ "transition"
+ )
+ ).toEqual(
+ new Map([
+ [
+ "transitionProperty",
+ {
+ type: "layers",
+ value: [{ type: "unparsed", value: "opacity" }],
+ },
+ ],
+ [
+ "transitionDuration",
+ {
+ type: "layers",
+ value: [{ type: "unit", unit: "s", value: 1 }],
+ },
+ ],
+ ])
+ );
+});
diff --git a/apps/builder/app/builder/features/style-panel/shared/parse-css-fragment.ts b/apps/builder/app/builder/features/style-panel/shared/parse-css-fragment.ts
new file mode 100644
index 0000000000..5f83023156
--- /dev/null
+++ b/apps/builder/app/builder/features/style-panel/shared/parse-css-fragment.ts
@@ -0,0 +1,11 @@
+import { parseCss } from "@webstudio-is/css-data";
+
+export const parseCssFragment = (css: string, fallbackProperty: string) => {
+ let parsed = parseCss(`.styles{${css}}`);
+ if (parsed.length === 0) {
+ parsed = parseCss(`.styles{${fallbackProperty}: ${css}}`);
+ }
+ return new Map(
+ parsed.map((styleDecl) => [styleDecl.property, styleDecl.value])
+ );
+};
diff --git a/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx b/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx
index d9e79fc81f..d21db3d980 100644
--- a/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx
+++ b/apps/builder/app/builder/features/style-panel/shared/shadow-content.tsx
@@ -9,7 +9,6 @@ import {
} from "@webstudio-is/css-engine";
import {
extractShadowProperties,
- parseShadow,
type ExtractedShadowProperties,
} from "@webstudio-is/css-data";
import {
@@ -36,6 +35,7 @@ import type {
SetProperty,
StyleUpdateOptions,
} from "../shared/use-style-data";
+import { parseCssFragment } from "./parse-css-fragment";
/*
When it comes to checking and validating individual CSS properties for the box-shadow,
@@ -131,16 +131,16 @@ export const ShadowContent = ({
if (intermediateValue === undefined) {
return;
}
- const layers = parseShadow(property, intermediateValue.value);
- if (layers.type === "invalid") {
- setIntermediateValue({
- type: "invalid",
- value: intermediateValue.value,
- });
+ const parsed = parseCssFragment(intermediateValue.value, property);
+ const parsedValue = parsed.get(property);
+ if (parsedValue?.type === "layers") {
+ onEditLayer(index, parsedValue, { isEphemeral: false });
return;
}
-
- onEditLayer(index, layers, { isEphemeral: false });
+ setIntermediateValue({
+ type: "invalid",
+ value: intermediateValue.value,
+ });
};
const handlePropertyChange = (
diff --git a/apps/builder/app/builder/features/style-panel/style-layer-utils.test.ts b/apps/builder/app/builder/features/style-panel/style-layer-utils.test.ts
index 81d39e6bc0..e8b831e73d 100644
--- a/apps/builder/app/builder/features/style-panel/style-layer-utils.test.ts
+++ b/apps/builder/app/builder/features/style-panel/style-layer-utils.test.ts
@@ -1,10 +1,10 @@
import { afterEach, describe, expect, test } from "@jest/globals";
-import {
+import type {
FunctionValue,
- type LayersValue,
- type StyleProperty,
- type StyleValue,
- type TupleValue,
+ LayersValue,
+ StyleProperty,
+ StyleValue,
+ TupleValue,
} from "@webstudio-is/css-engine";
import type { CreateBatchUpdate } from "./shared/use-style-data";
import {
@@ -16,7 +16,7 @@ import {
updateLayer,
} from "./style-layer-utils";
import type { StyleInfo } from "./shared/style-info";
-import { parseFilter, parseShadow } from "@webstudio-is/css-data";
+import { parseCssValue } from "@webstudio-is/css-data";
let styleInfo: StyleInfo = {};
let published = false;
@@ -37,7 +37,7 @@ const createBatchUpdate: CreateBatchUpdate = () => ({
deleteProperty: (propertyName: string) => {
deletedProperties.add(propertyName);
},
- publish: (options?: unknown) => {
+ publish: (_options?: unknown) => {
published = true;
},
});
@@ -209,10 +209,10 @@ describe("boxShadowUtils", () => {
test("adds layer to box-shadow proeprty", () => {
addLayer(
property,
- parseShadow(
+ parseCssValue(
"boxShadow",
- `box-shadow: 10px 10px 10px 0px rgba(0, 0, 0, 0.75)`
- ),
+ `10px 10px 10px 0px rgba(0, 0, 0, 0.75)`
+ ) as LayersValue,
styleInfo,
createBatchUpdate
);
@@ -322,10 +322,10 @@ describe("boxShadowUtils", () => {
test("swaps the layers using indexe's", () => {
addLayer(
property,
- parseShadow(
+ parseCssValue(
"boxShadow",
- `box-shadow: 0 60px 80px rgba(0,0,0,0.60), 0 45px 26px rgba(0,0,0,0.14);`
- ),
+ `0 60px 80px rgba(0,0,0,0.60), 0 45px 26px rgba(0,0,0,0.14)`
+ ) as LayersValue,
styleInfo,
createBatchUpdate
);
@@ -368,7 +368,7 @@ test("Generates humane layer names for shadow style layer", () => {
getHumanizedTextFromLayer(
"boxShadow",
(
- parseShadow(
+ parseCssValue(
"boxShadow",
"inset 2px 2px 5px rgba(0, 0, 0, 0.5)"
) as LayersValue
@@ -391,7 +391,7 @@ test("Generates humane layer names for shadow style layer", () => {
getHumanizedTextFromLayer(
"boxShadow",
(
- parseShadow(
+ parseCssValue(
"boxShadow",
"3px 3px 10px rgba(255, 0, 0, 0.7)"
) as LayersValue
@@ -414,7 +414,7 @@ test("Generates humane layer names for shadow style layer", () => {
expect(
getHumanizedTextFromLayer(
"textShadow",
- (parseShadow("textShadow", "text-shadow: 1px 1px 2px red") as LayersValue)
+ (parseCssValue("textShadow", "1px 1px 2px red") as LayersValue)
.value[0] as TupleValue
)
).toMatchInlineSnapshot(`
@@ -430,7 +430,7 @@ test("Generates humane layer names for filter style layer", () => {
expect(
getHumanizedTextFromLayer(
"filter",
- (parseFilter("filter", "blur(50px)") as TupleValue)
+ (parseCssValue("filter", "blur(50px)") as TupleValue)
.value[0] as FunctionValue
)
).toMatchInlineSnapshot(`
@@ -444,12 +444,8 @@ test("Generates humane layer names for filter style layer", () => {
expect(
getHumanizedTextFromLayer(
"filter",
- (
- parseFilter(
- "filter",
- "filter: drop-shadow(4px 4px 10px blue);"
- ) as TupleValue
- ).value[0] as FunctionValue
+ (parseCssValue("filter", "drop-shadow(4px 4px 10px blue)") as TupleValue)
+ .value[0] as FunctionValue
)
).toMatchInlineSnapshot(`
{
diff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.ts
index f3b36775a8..3826d7c262 100644
--- a/packages/css-data/src/parse-css-value.ts
+++ b/packages/css-data/src/parse-css-value.ts
@@ -14,13 +14,11 @@ import {
} from "@webstudio-is/css-engine";
import { keywordValues } from "./__generated__/keyword-values";
import { units } from "./__generated__/units";
-import {
- parseFilter,
- parseShadow,
- parseTranslate,
- parseTransform,
- parseScale,
-} from "./property-parsers";
+import { parseTranslate } from "./property-parsers/translate";
+import { parseTransform } from "./property-parsers/transform";
+import { parseScale } from "./property-parsers/scale";
+import { parseFilter } from "./property-parsers/filter";
+import { parseShadow } from "./property-parsers/shadows";
export const cssTryParseValue = (input: string) => {
try {
diff --git a/packages/css-data/src/parse-css.ts b/packages/css-data/src/parse-css.ts
index 69e867d230..632ece9ee0 100644
--- a/packages/css-data/src/parse-css.ts
+++ b/packages/css-data/src/parse-css.ts
@@ -17,7 +17,7 @@ export type ParsedStyleDecl = {
* and convert to camel case only unprefixed properties
* @todo stop converting to camel case and use hyphenated format
*/
-export const normalizePropertyName = (property: string) => {
+const normalizePropertyName = (property: string) => {
// these are manually added with pascal case
// convert unprefixed used by webflow version into prefixed one
if (property === "-webkit-font-smoothing" || property === "font-smoothing") {
diff --git a/packages/css-data/src/property-parsers/background.test.ts b/packages/css-data/src/property-parsers/background.test.ts
deleted file mode 100644
index b98f885306..0000000000
--- a/packages/css-data/src/property-parsers/background.test.ts
+++ /dev/null
@@ -1,195 +0,0 @@
-import { describe, expect, test } from "@jest/globals";
-
-import { parseBackground } from "./background";
-
-describe("parseBackground", () => {
- test("parse background from figma", () => {
- expect(
- parseBackground(
- "linear-gradient(180deg, #11181C 0%, rgba(17, 24, 28, 0) 36.09%), none, linear-gradient(180deg, rgba(230, 60, 254, 0.33) 0%, rgba(255, 174, 60, 0) 100%), radial-gradient(54.1% 95.83% at 100% 100%, #FFFFFF 0%, rgba(255, 255, 255, 0) 100%), linear-gradient(122.33deg, rgba(74, 78, 250, 0.2) 0%, rgba(0, 0, 0, 0) 69.38%), radial-gradient(92.26% 201.29% at 98.6% 10.65%, rgba(255, 174, 60, 0.3) 0%, rgba(227, 53, 255, 0) 100%) /* warning: gradient uses a rotation that is not supported by CSS and may not behave as expected */, radial-gradient(84.64% 267.51% at 10.07% 81.45%, rgba(53, 255, 182, 0.2) 0%, rgba(74, 78, 250, 0.2) 100%) /* warning: gradient uses a rotation that is not supported by CSS and may not behave as expected */, #EBFFFC;"
- )
- ).toEqual({
- backgroundColor: { alpha: 1, b: 252, g: 255, r: 235, type: "rgb" },
- backgroundImage: {
- type: "layers",
- value: [
- {
- type: "unparsed",
- value: "linear-gradient(180deg,#11181C 0%,rgba(17,24,28,0) 36.09%)",
- },
- {
- type: "keyword",
- value: "none",
- },
- {
- type: "unparsed",
- value:
- "linear-gradient(180deg,rgba(230,60,254,0.33) 0%,rgba(255,174,60,0) 100%)",
- },
- {
- type: "unparsed",
- value:
- "radial-gradient(54.1% 95.83%at 100% 100%,#FFFFFF 0%,rgba(255,255,255,0) 100%)",
- },
- {
- type: "unparsed",
- value:
- "linear-gradient(122.33deg,rgba(74,78,250,0.2) 0%,rgba(0,0,0,0) 69.38%)",
- },
- {
- type: "unparsed",
- value:
- "radial-gradient(92.26% 201.29%at 98.6% 10.65%,rgba(255,174,60,0.3) 0%,rgba(227,53,255,0) 100%)",
- },
- {
- type: "unparsed",
- value:
- "radial-gradient(84.64% 267.51%at 10.07% 81.45%,rgba(53,255,182,0.2) 0%,rgba(74,78,250,0.2) 100%)",
- },
- {
- type: "keyword",
- value: "none",
- },
- ],
- },
- });
- });
-
- test("parse background from figma without backgroundColor", () => {
- expect(
- parseBackground(
- "linear-gradient(180deg, #11181C 0%, rgba(17, 24, 28, 0) 36.09%), linear-gradient(180deg, rgba(230, 60, 254, 0.33) 0%, rgba(255, 174, 60, 0) 100%), radial-gradient(54.1% 95.83% at 100% 100%, #FFFFFF 0%, rgba(255, 255, 255, 0) 100%), linear-gradient(122.33deg, rgba(74, 78, 250, 0.2) 0%, rgba(0, 0, 0, 0) 69.38%), radial-gradient(92.26% 201.29% at 98.6% 10.65%, rgba(255, 174, 60, 0.3) 0%, rgba(227, 53, 255, 0) 100%) /* warning: gradient uses a rotation that is not supported by CSS and may not behave as expected */, radial-gradient(84.64% 267.51% at 10.07% 81.45%, rgba(53, 255, 182, 0.2) 0%, rgba(74, 78, 250, 0.2) 100%) /* warning: gradient uses a rotation that is not supported by CSS and may not behave as expected */"
- )
- ).toEqual({
- backgroundColor: { type: "keyword", value: "transparent" },
- backgroundImage: {
- type: "layers",
- value: [
- {
- type: "unparsed",
- value: "linear-gradient(180deg,#11181C 0%,rgba(17,24,28,0) 36.09%)",
- },
- {
- type: "unparsed",
- value:
- "linear-gradient(180deg,rgba(230,60,254,0.33) 0%,rgba(255,174,60,0) 100%)",
- },
- {
- type: "unparsed",
- value:
- "radial-gradient(54.1% 95.83%at 100% 100%,#FFFFFF 0%,rgba(255,255,255,0) 100%)",
- },
- {
- type: "unparsed",
- value:
- "linear-gradient(122.33deg,rgba(74,78,250,0.2) 0%,rgba(0,0,0,0) 69.38%)",
- },
- {
- type: "unparsed",
- value:
- "radial-gradient(92.26% 201.29%at 98.6% 10.65%,rgba(255,174,60,0.3) 0%,rgba(227,53,255,0) 100%)",
- },
- {
- type: "unparsed",
- value:
- "radial-gradient(84.64% 267.51%at 10.07% 81.45%,rgba(53,255,182,0.2) 0%,rgba(74,78,250,0.2) 100%)",
- },
- ],
- },
- });
- });
-
- test("parse background and skips url background", () => {
- expect(
- parseBackground(
- "url(https://hello.world/some-image), linear-gradient(180deg, rgba(230, 60, 254, 0.33) 0%, rgba(255, 174, 60, 0) 100%), radial-gradient(54.1% 95.83% at 100% 100%, #FFFFFF 0%, rgba(255, 255, 255, 0) 100%), linear-gradient(122.33deg, rgba(74, 78, 250, 0.2) 0%, rgba(0, 0, 0, 0) 69.38%), radial-gradient(92.26% 201.29% at 98.6% 10.65%, rgba(255, 174, 60, 0.3) 0%, rgba(227, 53, 255, 0) 100%) /* warning: gradient uses a rotation that is not supported by CSS and may not behave as expected */, radial-gradient(84.64% 267.51% at 10.07% 81.45%, rgba(53, 255, 182, 0.2) 0%, rgba(74, 78, 250, 0.2) 100%) /* warning: gradient uses a rotation that is not supported by CSS and may not behave as expected */"
- )
- ).toEqual({
- backgroundColor: { type: "keyword", value: "transparent" },
- backgroundImage: {
- type: "layers",
- value: [
- {
- type: "image",
- value: { type: "url", url: "https://hello.world/some-image" },
- },
- {
- type: "unparsed",
- value:
- "linear-gradient(180deg,rgba(230,60,254,0.33) 0%,rgba(255,174,60,0) 100%)",
- },
- {
- type: "unparsed",
- value:
- "radial-gradient(54.1% 95.83%at 100% 100%,#FFFFFF 0%,rgba(255,255,255,0) 100%)",
- },
- {
- type: "unparsed",
- value:
- "linear-gradient(122.33deg,rgba(74,78,250,0.2) 0%,rgba(0,0,0,0) 69.38%)",
- },
- {
- type: "unparsed",
- value:
- "radial-gradient(92.26% 201.29%at 98.6% 10.65%,rgba(255,174,60,0.3) 0%,rgba(227,53,255,0) 100%)",
- },
- {
- type: "unparsed",
- value:
- "radial-gradient(84.64% 267.51%at 10.07% 81.45%,rgba(53,255,182,0.2) 0%,rgba(74,78,250,0.2) 100%)",
- },
- ],
- },
- });
- });
-
- test("parse background partially copied background", () => {
- expect(
- parseBackground(
- "linear-gradient(180deg, #11181C 0%, rgba(17, 24, 28, 0) 36.09%), linear-gradient(180deg, "
- )
- ).toEqual({
- backgroundColor: { type: "keyword", value: "transparent" },
- backgroundImage: {
- type: "layers",
- value: [
- {
- type: "unparsed",
- value: "linear-gradient(180deg,#11181C 0%,rgba(17,24,28,0) 36.09%)",
- },
- { type: "keyword", value: "none" },
- ],
- },
- });
- });
-
- test("parse background partially commented", () => {
- expect(
- parseBackground(
- "linear-gradient(180deg, rgba(230, 60, 254, 0.33) 0%, rgba(255, 174, 60, 0) 100%), /* radial-gradient(54.1% 95.83% at 100% 100%, #FFFFFF 0%, rgba(255, 255, 255, 0) 100%) */"
- )
- ).toEqual({
- backgroundColor: { type: "keyword", value: "transparent" },
- backgroundImage: {
- type: "layers",
- value: [
- {
- type: "unparsed",
- value:
- "linear-gradient(180deg,rgba(230,60,254,0.33) 0%,rgba(255,174,60,0) 100%)",
- },
- ],
- },
- });
- });
-
- test("parse bad background", () => {
- expect(parseBackground("linear-gradient(180deg,")).toEqual({
- backgroundColor: { type: "keyword", value: "transparent" },
- backgroundImage: {
- type: "layers",
- value: [{ type: "keyword", value: "none" }],
- },
- });
- });
-});
diff --git a/packages/css-data/src/property-parsers/background.ts b/packages/css-data/src/property-parsers/background.ts
deleted file mode 100644
index aa20d1a3d3..0000000000
--- a/packages/css-data/src/property-parsers/background.ts
+++ /dev/null
@@ -1,44 +0,0 @@
-import type { StyleProperty, StyleValue } from "@webstudio-is/css-engine";
-import { expandShorthands } from "../shorthands";
-import { normalizePropertyName } from "../parse-css";
-import { parseCssValue } from "../parse-css-value";
-
-export const parseBackground = (
- background: string
-): {
- backgroundColor?: StyleValue;
- backgroundImage?: StyleValue;
-} => {
- let tokenStream = background.trim();
-
- tokenStream = tokenStream.endsWith(";")
- ? tokenStream.slice(0, -1)
- : tokenStream;
-
- // The user can copy the whole style together with the name of the property from figma or any other tool.
- // We need to remove the property name from the string.
- const cleanupKeywords = ["background:", "background-image:"];
-
- for (const cleanupKeyword of cleanupKeywords) {
- tokenStream = tokenStream.startsWith(cleanupKeyword)
- ? tokenStream.slice(cleanupKeyword.length).trim()
- : tokenStream;
- }
-
- return Object.fromEntries(
- expandShorthands([["background", tokenStream]])
- .filter(
- ([property]) =>
- property === "background-image" || property === "background-color"
- )
- .map(([property, value]) => {
- const normalizedProperty = normalizePropertyName(
- property
- ) as StyleProperty;
- return [normalizedProperty, parseCssValue(normalizedProperty, value)];
- })
- ) as {
- backgroundColor: StyleValue;
- backgroundImage: StyleValue;
- };
-};
diff --git a/packages/css-data/src/property-parsers/index.ts b/packages/css-data/src/property-parsers/index.ts
index 73b057bff5..1e3946dc7c 100644
--- a/packages/css-data/src/property-parsers/index.ts
+++ b/packages/css-data/src/property-parsers/index.ts
@@ -1,9 +1,3 @@
-export * from "./background";
-export * from "./shadows";
export * from "./transition";
export * from "./transition-property-extractor";
export * from "./shadow-properties-extractor";
-export * from "./filter";
-export * from "./scale";
-export * from "./translate";
-export * from "./transform";
diff --git a/packages/css-data/src/property-parsers/transition.ts b/packages/css-data/src/property-parsers/transition.ts
index 61569b4347..b4dcda6e6e 100644
--- a/packages/css-data/src/property-parsers/transition.ts
+++ b/packages/css-data/src/property-parsers/transition.ts
@@ -41,12 +41,6 @@ export const isAnimatableProperty = (
);
};
-export const isTransitionLongHandProperty = (
- property: string
-): property is (typeof transitionLongHandProperties)[number] => {
- return transitionLongHandProperties.some((item) => item === property);
-};
-
export const isValidTransitionValue = (
value: LayerValueItem
): value is KeywordValue | UnitValue | FunctionValue => {
diff --git a/packages/sdk-components-react-radix/scripts/generate-tailwind-theme.ts b/packages/sdk-components-react-radix/scripts/generate-tailwind-theme.ts
index b38d0c0ce8..851b9eb9a4 100644
--- a/packages/sdk-components-react-radix/scripts/generate-tailwind-theme.ts
+++ b/packages/sdk-components-react-radix/scripts/generate-tailwind-theme.ts
@@ -2,7 +2,7 @@ import { writeFile, mkdir } from "node:fs/promises";
// Imported theme https://github.com/tailwindlabs/tailwindcss/blob/e0c52a9332a64ef7eb0ba23d2a0fd5a16fe57ab7/stubs/config.full.js
import defaultTheme from "tailwindcss/defaultTheme";
import type { StyleValue, StyleProperty } from "@webstudio-is/css-engine";
-import { parseCssValue, parseShadow } from "@webstudio-is/css-data";
+import { parseCssValue } from "@webstudio-is/css-data";
import type { ResolvableTo } from "tailwindcss/types/config";
import { colors as colorOverrides } from "../src/theme/tailwind-colors";
@@ -158,7 +158,7 @@ const generatedThemeData: GeneratedThemeItem[] = [
values: Object.fromEntries(
Object.entries(theme.boxShadow).map(([name, value]) => [
name,
- parseShadow("boxShadow", value),
+ parseCssValue("boxShadow", value),
])
),
},