Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

experimental: expand background shorthand (#3647)

Bogdan Chadkinc1fc58ff4f6c
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
index 9d2cd8dc39..e48bbb0381 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/background-content.tsx
@@ -3,7 +3,7 @@
* as of now just implement feature parity with old backgrounds section
**/
-import type { RgbValue, StyleValue } from "@webstudio-is/css-engine";
+import type { StyleValue } from "@webstudio-is/css-engine";
import {
theme,
Flex,
@@ -48,7 +48,7 @@ type BackgroundContentProps = {
currentStyle: StyleInfo;
setProperty: SetBackgroundProperty;
deleteProperty: DeleteBackgroundProperty;
- setBackgroundColor: (color: RgbValue) => void;
+ setBackgroundColor: (color: StyleValue) => void;
};
const safeDeleteProperty = (
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 0e212ee476..82149ad873 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,4 +1,4 @@
-import type { InvalidValue, RgbValue } from "@webstudio-is/css-engine";
+import type { InvalidValue, StyleValue } from "@webstudio-is/css-engine";
import { parseCssValue, parseBackground } from "@webstudio-is/css-data";
import {
Flex,
@@ -17,7 +17,7 @@ type IntermediateValue = {
export const BackgroundGradient = (
props: Omit<ControlProps, "property" | "items"> & {
- setBackgroundColor: (color: RgbValue) => void;
+ setBackgroundColor: (color: StyleValue) => void;
}
) => {
const property = "backgroundImage";
@@ -65,20 +65,25 @@ export const BackgroundGradient = (
const { backgroundImage, backgroundColor } = parseBackground(
intermediateValue.value
);
-
- if (backgroundColor !== undefined) {
- props.setBackgroundColor(backgroundColor);
- }
-
- if (backgroundImage.type !== "invalid") {
- setIntermediateValue(undefined);
- props.setProperty(property)(backgroundImage);
+ const [layer] =
+ backgroundImage?.type === "layers"
+ ? backgroundImage.value
+ : [backgroundImage];
+
+ // set invalid state
+ if (
+ backgroundColor === undefined ||
+ backgroundColor.type === "invalid" ||
+ layer === undefined ||
+ layer.type === "invalid"
+ ) {
+ setIntermediateValue({ type: "invalid", value: intermediateValue.value });
+ props.deleteProperty(property, { isEphemeral: true });
return;
}
-
- // Set invalid state
- setIntermediateValue({ type: "invalid", value: intermediateValue.value });
- props.deleteProperty(property, { isEphemeral: true });
+ props.setBackgroundColor(backgroundColor);
+ setIntermediateValue(undefined);
+ props.setProperty(property)(layer);
};
const handleOnCompleteRef = useRef(handleOnComplete);
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 ea4f852357..5c974d5b18 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
@@ -16,7 +16,6 @@ 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";
-import { url } from "css-tree";
const property: StyleProperty = "backgroundImage";
@@ -98,9 +97,29 @@ export const BackgroundImage = (
return;
}
- if (newValue.type === "unparsed") {
- const urlFromProperty = url.decode(value);
- if (urlFromProperty === undefined) {
+ const [layer] = newValue.type === "layers" ? newValue.value : [newValue];
+ if (layer?.type === "keyword") {
+ setIntermediateValue(undefined);
+ props.setProperty(property)(layer, options);
+ }
+ if (layer?.type !== "image" || layer.value.type !== "url") {
+ setIntermediateValue({
+ type: "invalid",
+ value: value,
+ });
+ return;
+ }
+ const url = layer.value.url;
+
+ if (isAbsoluteURL(url) === true) {
+ props.setProperty(property)(layer, options);
+ } else {
+ const usedAsset = Array.from($assets.get().values()).find(
+ (asset) => asset.type === "image" && asset.name === url
+ );
+
+ if (usedAsset === undefined) {
+ setErrors([`Asset ${url} is not found in project `]);
setIntermediateValue({
type: "invalid",
value: value,
@@ -108,43 +127,17 @@ export const BackgroundImage = (
return;
}
- if (isAbsoluteURL(urlFromProperty) === true) {
- props.setProperty(property)(
- {
- type: "image",
- value: {
- type: "url",
- url: urlFromProperty,
- },
+ setErrors([]);
+ props.setProperty(property)(
+ {
+ type: "image",
+ value: {
+ type: "asset",
+ value: usedAsset.id,
},
- options
- );
- } else {
- const usedAsset = Array.from($assets.get().values()).find(
- (asset) => asset.type === "image" && asset.name === urlFromProperty
- );
-
- if (usedAsset === undefined) {
- setErrors([`Asset ${urlFromProperty} is not found in project `]);
- setIntermediateValue({
- type: "invalid",
- value: value,
- });
- return;
- }
-
- setErrors([]);
- props.setProperty(property)(
- {
- type: "image",
- value: {
- type: "asset",
- value: usedAsset.id,
- },
- },
- options
- );
- }
+ },
+ options
+ );
}
};
diff --git a/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx b/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx
index 19083c15ce..85c59fb2e5 100644
--- a/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx
+++ b/apps/builder/app/builder/features/style-panel/sections/backgrounds/backgrounds.tsx
@@ -43,7 +43,7 @@ import {
import { BackgroundContent } from "./background-content";
import { getLayerName, LayerThumbnail } from "./background-thumbnail";
import { useMemo } from "react";
-import type { RgbValue, StyleProperty } from "@webstudio-is/css-engine";
+import type { StyleProperty, StyleValue } from "@webstudio-is/css-engine";
import {
CollapsibleSectionRoot,
useOpenState,
@@ -58,7 +58,7 @@ const Layer = (props: {
setProperty: SetBackgroundProperty;
deleteProperty: DeleteBackgroundProperty;
deleteLayer: () => void;
- setBackgroundColor: (color: RgbValue) => void;
+ setBackgroundColor: (color: StyleValue) => void;
}) => {
const assets = useStore($assets);
diff --git a/apps/builder/app/shared/copy-paste/plugin-webflow/__generated__/style-presets.ts b/apps/builder/app/shared/copy-paste/plugin-webflow/__generated__/style-presets.ts
index 6b0e88a101..f119d4bb3d 100644
--- a/apps/builder/app/shared/copy-paste/plugin-webflow/__generated__/style-presets.ts
+++ b/apps/builder/app/shared/copy-paste/plugin-webflow/__generated__/style-presets.ts
@@ -557,8 +557,103 @@ export const styles = {
type: "layers",
value: [
{
- type: "invalid",
- value: "",
+ type: "keyword",
+ value: "none",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionX",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionY",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundSize",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "tuple",
+ value: [
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ ],
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundRepeat",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "repeat",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundAttachment",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "scroll",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundOrigin",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "padding-box",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundClip",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "border-box",
},
],
},
@@ -2983,6 +3078,101 @@ export const styles = {
],
},
},
+ {
+ property: "backgroundPositionX",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionY",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundSize",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "tuple",
+ value: [
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ ],
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundRepeat",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "repeat",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundAttachment",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "scroll",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundOrigin",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "padding-box",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundClip",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "border-box",
+ },
+ ],
+ },
+ },
{
property: "borderTopWidth",
value: {
@@ -7137,66 +7327,161 @@ export const styles = {
type: "layers",
value: [
{
- type: "invalid",
- value: "",
+ type: "keyword",
+ value: "none",
},
],
},
},
{
- property: "backgroundColor",
+ property: "backgroundPositionX",
value: {
- type: "rgb",
- alpha: 1,
- r: 255,
- g: 255,
- b: 255,
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
},
},
{
- property: "borderTopLeftRadius",
+ property: "backgroundPositionY",
value: {
- type: "unit",
- unit: "px",
- value: 3,
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
},
},
{
- property: "borderTopRightRadius",
+ property: "backgroundSize",
value: {
- type: "unit",
- unit: "px",
- value: 3,
+ type: "layers",
+ value: [
+ {
+ type: "tuple",
+ value: [
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ ],
+ },
+ ],
},
},
{
- property: "borderBottomRightRadius",
+ property: "backgroundRepeat",
value: {
- type: "unit",
- unit: "px",
- value: 3,
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "repeat",
+ },
+ ],
},
},
{
- property: "display",
+ property: "backgroundAttachment",
value: {
- type: "keyword",
- value: "inline-block",
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "scroll",
+ },
+ ],
},
},
{
- property: "marginBottom",
+ property: "backgroundOrigin",
value: {
- type: "unit",
- unit: "px",
- value: 8,
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "padding-box",
+ },
+ ],
},
},
{
- property: "content",
+ property: "backgroundClip",
value: {
- type: "unparsed",
- value: '" "',
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "border-box",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundColor",
+ value: {
+ type: "rgb",
+ alpha: 1,
+ r: 255,
+ g: 255,
+ b: 255,
+ },
+ },
+ {
+ property: "borderTopLeftRadius",
+ value: {
+ type: "unit",
+ unit: "px",
+ value: 3,
+ },
+ },
+ {
+ property: "borderTopRightRadius",
+ value: {
+ type: "unit",
+ unit: "px",
+ value: 3,
+ },
+ },
+ {
+ property: "borderBottomRightRadius",
+ value: {
+ type: "unit",
+ unit: "px",
+ value: 3,
+ },
+ },
+ {
+ property: "display",
+ value: {
+ type: "keyword",
+ value: "inline-block",
+ },
+ },
+ {
+ property: "marginBottom",
+ value: {
+ type: "unit",
+ unit: "px",
+ value: 8,
+ },
+ },
+ {
+ property: "content",
+ value: {
+ type: "unparsed",
+ value: '" "',
},
},
{
@@ -7937,8 +8222,103 @@ export const styles = {
type: "layers",
value: [
{
- type: "invalid",
- value: "",
+ type: "keyword",
+ value: "none",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionX",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionY",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundSize",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "tuple",
+ value: [
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ ],
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundRepeat",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "repeat",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundAttachment",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "scroll",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundOrigin",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "padding-box",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundClip",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "border-box",
},
],
},
@@ -9583,8 +9963,103 @@ export const styles = {
type: "layers",
value: [
{
- type: "invalid",
- value: "",
+ type: "keyword",
+ value: "none",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionX",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionY",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundSize",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "tuple",
+ value: [
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ ],
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundRepeat",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "repeat",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundAttachment",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "scroll",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundOrigin",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "padding-box",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundClip",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "border-box",
},
],
},
@@ -9870,8 +10345,103 @@ export const styles = {
type: "layers",
value: [
{
- type: "invalid",
- value: "",
+ type: "keyword",
+ value: "none",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionX",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionY",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundSize",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "tuple",
+ value: [
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ ],
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundRepeat",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "repeat",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundAttachment",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "scroll",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundOrigin",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "padding-box",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundClip",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "border-box",
},
],
},
@@ -10308,34 +10878,129 @@ export const styles = {
},
},
{
- property: "textOverflow",
+ property: "textOverflow",
+ value: {
+ type: "keyword",
+ value: "ellipsis",
+ },
+ },
+ {
+ property: "whiteSpaceCollapse",
+ value: {
+ type: "keyword",
+ value: "collapse",
+ },
+ },
+ {
+ property: "textWrapMode",
+ value: {
+ type: "keyword",
+ value: "nowrap",
+ },
+ },
+ {
+ property: "backgroundImage",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "none",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionX",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionY",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundSize",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "tuple",
+ value: [
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ ],
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundRepeat",
value: {
- type: "keyword",
- value: "ellipsis",
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "repeat",
+ },
+ ],
},
},
{
- property: "whiteSpaceCollapse",
+ property: "backgroundAttachment",
value: {
- type: "keyword",
- value: "collapse",
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "scroll",
+ },
+ ],
},
},
{
- property: "textWrapMode",
+ property: "backgroundOrigin",
value: {
- type: "keyword",
- value: "nowrap",
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "padding-box",
+ },
+ ],
},
},
{
- property: "backgroundImage",
+ property: "backgroundClip",
value: {
type: "layers",
value: [
{
- type: "invalid",
- value: "",
+ type: "keyword",
+ value: "border-box",
},
],
},
@@ -10494,7 +11159,19 @@ export const styles = {
},
},
{
- property: "backgroundPosition",
+ property: "backgroundPositionX",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "center",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionY",
value: {
type: "layers",
value: [
@@ -10958,8 +11635,103 @@ export const styles = {
type: "layers",
value: [
{
- type: "invalid",
- value: "",
+ type: "keyword",
+ value: "none",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionX",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionY",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundSize",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "tuple",
+ value: [
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ ],
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundRepeat",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "repeat",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundAttachment",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "scroll",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundOrigin",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "padding-box",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundClip",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "border-box",
},
],
},
@@ -11844,8 +12616,103 @@ export const styles = {
type: "layers",
value: [
{
- type: "invalid",
- value: "",
+ type: "keyword",
+ value: "none",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionX",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundPositionY",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "unit",
+ unit: "%",
+ value: 0,
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundSize",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "tuple",
+ value: [
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ {
+ type: "keyword",
+ value: "auto",
+ },
+ ],
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundRepeat",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "repeat",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundAttachment",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "scroll",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundOrigin",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "padding-box",
+ },
+ ],
+ },
+ },
+ {
+ property: "backgroundClip",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "border-box",
},
],
},
@@ -12989,7 +13856,7 @@ export const styles = {
},
},
{
- property: "backgroundPosition",
+ property: "backgroundPositionX",
value: {
type: "layers",
value: [
@@ -13001,6 +13868,18 @@ export const styles = {
],
},
},
+ {
+ property: "backgroundPositionY",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "center",
+ },
+ ],
+ },
+ },
{
property: "backgroundSize",
value: {
@@ -13383,7 +14262,7 @@ export const styles = {
},
},
{
- property: "backgroundPosition",
+ property: "backgroundPositionX",
value: {
type: "layers",
value: [
@@ -13395,6 +14274,18 @@ export const styles = {
],
},
},
+ {
+ property: "backgroundPositionY",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "center",
+ },
+ ],
+ },
+ },
{
property: "backgroundRepeat",
value: {
@@ -13527,7 +14418,7 @@ export const styles = {
},
},
{
- property: "backgroundPosition",
+ property: "backgroundPositionX",
value: {
type: "layers",
value: [
@@ -13539,6 +14430,18 @@ export const styles = {
],
},
},
+ {
+ property: "backgroundPositionY",
+ value: {
+ type: "layers",
+ value: [
+ {
+ type: "keyword",
+ value: "center",
+ },
+ ],
+ },
+ },
{
property: "backgroundRepeat",
value: {
diff --git a/apps/builder/package.json b/apps/builder/package.json
index 42c592ea79..e3a1bbed33 100644
--- a/apps/builder/package.json
+++ b/apps/builder/package.json
@@ -7,7 +7,8 @@
"scripts": {
"prebuild": "which remix",
"build": "remix vite:build",
- "build:webflow-presets": "css-to-ws ./app/shared/copy-paste/plugin-webflow/style-presets.css ./app/shared/copy-paste/plugin-webflow/__generated__/style-presets.ts",
+ "css-to-ws": "NODE_OPTIONS='--conditions=webstudio --import=tsx' css-to-ws",
+ "build:webflow-presets": "pnpm css-to-ws ./app/shared/copy-paste/plugin-webflow/style-presets.css ./app/shared/copy-paste/plugin-webflow/__generated__/style-presets.ts",
"start": "remix-serve build/server/index.js",
"dev": "remix vite:dev",
"typecheck": "tsc --noEmit --emitDeclarationOnly false",
diff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.ts
index f5dedc9d7f..787b026002 100644
--- a/packages/css-data/src/parse-css-value.ts
+++ b/packages/css-data/src/parse-css-value.ts
@@ -1,5 +1,6 @@
import { colord } from "colord";
import * as csstree from "css-tree";
+import type { CssNode } from "css-tree";
import warnOnce from "warn-once";
import {
TupleValue,
@@ -13,7 +14,6 @@ import { keywordValues } from "./__generated__/keyword-values";
import { units } from "./__generated__/units";
import {
isTransitionLongHandProperty,
- parseBackground,
parseFilter,
parseShadow,
parseTransitionLonghandProperty,
@@ -28,6 +28,18 @@ export const cssTryParseValue = (input: string) => {
}
};
+const splitRepeated = (nodes: CssNode[]) => {
+ const lists: Array<CssNode[]> = [[]];
+ for (const node of nodes) {
+ if (node.type === "Operator" && node.value === ",") {
+ lists.push([]);
+ } else {
+ lists.at(-1)?.push(node);
+ }
+ }
+ return lists;
+};
+
// Because csstree parser has bugs we use CSSStyleValue to validate css properties if available
// and fall back to csstree.
export const isValidDeclaration = (
@@ -87,6 +99,7 @@ const repeatedProps = new Set<StyleProperty>([
"backgroundPositionY",
"backgroundRepeat",
"backgroundSize",
+ "backgroundImage",
]);
export const parseCssValue = (
@@ -131,22 +144,23 @@ export const parseCssValue = (
// prevent infinite splitting into layers for items
if (repeatedProps.has(property) && topLevel) {
+ const nodes = "children" in ast ? ast.children?.toArray() ?? [] : [ast];
return {
type: "layers",
- value: input
- .split(",")
- .map(
- (single) => parseCssValue(property, single, false) as LayerValueItem
- ),
+ value: splitRepeated(nodes).map(
+ (nodes) =>
+ parseCssValue(
+ property,
+ csstree.generate({
+ type: "Value",
+ children: new csstree.List<CssNode>().fromArray(nodes),
+ }),
+ false
+ ) as LayerValueItem
+ ),
};
}
- // prevent infinite splitting into layers for items
- if (property === "backgroundImage" && topLevel) {
- const { backgroundImage } = parseBackground(input);
- return backgroundImage;
- }
-
if (ast.type === "Value" && ast.children.first === ast.children.last) {
// Try extract units from 1st children
const first = ast.children.first;
@@ -205,6 +219,16 @@ export const parseCssValue = (
};
}
}
+
+ if (first?.type === "Url") {
+ return {
+ type: "image",
+ value: {
+ type: "url",
+ url: first.value,
+ },
+ };
+ }
}
// Probably a color (we can use csstree.lexer.matchProperty(cssPropertyName, ast) to extract the type but this looks much simpler)
diff --git a/packages/css-data/src/parse-css.test.ts b/packages/css-data/src/parse-css.test.ts
index 4b5762bb7c..64918ef44d 100644
--- a/packages/css-data/src/parse-css.test.ts
+++ b/packages/css-data/src/parse-css.test.ts
@@ -55,6 +55,144 @@ describe("Parse CSS", () => {
"type": "unparsed",
"value": "linear-gradient(180deg,#11181C 0%,rgba(17,24,28,0) 36.09%)",
},
+ {
+ "type": "keyword",
+ "value": "none",
+ },
+ ],
+ },
+ },
+ {
+ "property": "backgroundPositionX",
+ "value": {
+ "type": "layers",
+ "value": [
+ {
+ "type": "unit",
+ "unit": "%",
+ "value": 0,
+ },
+ {
+ "type": "unit",
+ "unit": "%",
+ "value": 0,
+ },
+ ],
+ },
+ },
+ {
+ "property": "backgroundPositionY",
+ "value": {
+ "type": "layers",
+ "value": [
+ {
+ "type": "unit",
+ "unit": "%",
+ "value": 0,
+ },
+ {
+ "type": "unit",
+ "unit": "%",
+ "value": 0,
+ },
+ ],
+ },
+ },
+ {
+ "property": "backgroundSize",
+ "value": {
+ "type": "layers",
+ "value": [
+ {
+ "type": "tuple",
+ "value": [
+ {
+ "type": "keyword",
+ "value": "auto",
+ },
+ {
+ "type": "keyword",
+ "value": "auto",
+ },
+ ],
+ },
+ {
+ "type": "tuple",
+ "value": [
+ {
+ "type": "keyword",
+ "value": "auto",
+ },
+ {
+ "type": "keyword",
+ "value": "auto",
+ },
+ ],
+ },
+ ],
+ },
+ },
+ {
+ "property": "backgroundRepeat",
+ "value": {
+ "type": "layers",
+ "value": [
+ {
+ "type": "keyword",
+ "value": "repeat",
+ },
+ {
+ "type": "keyword",
+ "value": "repeat",
+ },
+ ],
+ },
+ },
+ {
+ "property": "backgroundAttachment",
+ "value": {
+ "type": "layers",
+ "value": [
+ {
+ "type": "keyword",
+ "value": "scroll",
+ },
+ {
+ "type": "keyword",
+ "value": "scroll",
+ },
+ ],
+ },
+ },
+ {
+ "property": "backgroundOrigin",
+ "value": {
+ "type": "layers",
+ "value": [
+ {
+ "type": "keyword",
+ "value": "padding-box",
+ },
+ {
+ "type": "keyword",
+ "value": "padding-box",
+ },
+ ],
+ },
+ },
+ {
+ "property": "backgroundClip",
+ "value": {
+ "type": "layers",
+ "value": [
+ {
+ "type": "keyword",
+ "value": "border-box",
+ },
+ {
+ "type": "keyword",
+ "value": "border-box",
+ },
],
},
},
diff --git a/packages/css-data/src/parse-css.ts b/packages/css-data/src/parse-css.ts
index ff0d57e945..92a26077ca 100644
--- a/packages/css-data/src/parse-css.ts
+++ b/packages/css-data/src/parse-css.ts
@@ -1,10 +1,8 @@
+import { camelCase } from "change-case";
import * as csstree from "css-tree";
import { StyleValue, type StyleProperty } from "@webstudio-is/css-engine";
import type { EmbedTemplateStyleDecl } from "@webstudio-is/react-sdk";
import { parseCssValue as parseCssValueLonghand } from "./parse-css-value";
-import * as parsers from "./property-parsers/parsers";
-import * as toLonghand from "./property-parsers/to-longhand";
-import { camelCase } from "change-case";
import { expandShorthands } from "./shorthands";
/**
@@ -49,48 +47,10 @@ type Selector = string;
export type Styles = Record<Selector, Array<EmbedTemplateStyleDecl>>;
-type Longhand = keyof typeof toLonghand;
-
const parseCssValue = (
property: string,
value: string
): Map<StyleProperty, StyleValue> => {
- const unwrap = toLonghand[property as Longhand];
-
- if (typeof unwrap === "function") {
- const longhands = unwrap(value);
-
- return new Map(
- 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 === undefined || value === "") {
- return [property, { type: "invalid", value: "" }];
- }
-
- return [
- property,
- parseCssValueLonghand(property as StyleProperty, value),
- ];
- })
- ) as Map<StyleProperty, StyleValue>;
- }
-
const expanded = new Map(expandShorthands([[property, value]]));
const final = new Map();
for (const [property, value] of expanded) {
diff --git a/packages/css-data/src/property-parsers/background.test.ts b/packages/css-data/src/property-parsers/background.test.ts
index 098f8ffcb7..b98f885306 100644
--- a/packages/css-data/src/property-parsers/background.test.ts
+++ b/packages/css-data/src/property-parsers/background.test.ts
@@ -8,50 +8,51 @@ describe("parseBackground", () => {
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;"
)
- ).toMatchInlineSnapshot(`
-{
- "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%)",
+ ).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",
+ },
+ ],
},
- {
- "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%)",
- },
- ],
- },
-}
-`);
+ });
});
test("parse background from figma without backgroundColor", () => {
@@ -59,40 +60,43 @@ describe("parseBackground", () => {
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 */"
)
- ).toMatchInlineSnapshot(`
- {
- "backgroundColor": undefined,
- "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%)",
- },
- ],
- },
- }
- `);
+ ).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", () => {
@@ -100,43 +104,43 @@ describe("parseBackground", () => {
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 */"
)
- ).toMatchInlineSnapshot(`
-{
- "backgroundColor": undefined,
- "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%)",
+ ).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%)",
+ },
+ ],
},
- {
- "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", () => {
@@ -144,20 +148,19 @@ describe("parseBackground", () => {
parseBackground(
"linear-gradient(180deg, #11181C 0%, rgba(17, 24, 28, 0) 36.09%), linear-gradient(180deg, "
)
- ).toMatchInlineSnapshot(`
- {
- "backgroundColor": undefined,
- "backgroundImage": {
- "type": "layers",
- "value": [
- {
- "type": "unparsed",
- "value": "linear-gradient(180deg,#11181C 0%,rgba(17,24,28,0) 36.09%)",
- },
- ],
- },
- }
- `);
+ ).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", () => {
@@ -165,31 +168,28 @@ describe("parseBackground", () => {
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%) */"
)
- ).toMatchInlineSnapshot(`
- {
- "backgroundColor": undefined,
- "backgroundImage": {
- "type": "layers",
- "value": [
- {
- "type": "unparsed",
- "value": "linear-gradient(180deg,rgba(230,60,254,0.33) 0%,rgba(255,174,60,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,")).toMatchInlineSnapshot(`
- {
- "backgroundColor": undefined,
- "backgroundImage": {
- "type": "invalid",
- "value": "linear-gradient(180deg,)",
- },
- }
- `);
+ 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
index 0e047e5fee..aa20d1a3d3 100644
--- a/packages/css-data/src/property-parsers/background.ts
+++ b/packages/css-data/src/property-parsers/background.ts
@@ -1,52 +1,14 @@
-import * as csstree from "css-tree";
-import type {
- ImageValue,
- InvalidValue,
- KeywordValue,
- LayersValue,
- RgbValue,
- UnparsedValue,
-} from "@webstudio-is/css-engine";
-import { parseCssValue, cssTryParseValue } from "../parse-css-value";
+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 gradientNames = [
- "conic-gradient",
- "linear-gradient",
- "radial-gradient",
- "repeating-conic-gradient",
- "repeating-linear-gradient",
- "repeating-radial-gradient",
-];
-// @todo rewrite to return a Map
export const parseBackground = (
background: string
): {
- backgroundImage: LayersValue | InvalidValue;
- backgroundColor: RgbValue | undefined;
+ backgroundColor?: StyleValue;
+ backgroundImage?: StyleValue;
} => {
- const { backgroundImage, backgroundColor: backgroundColorRaw } =
- backgroundToLonghand(background);
-
- const backgroundColor = backgroundColorRaw
- ? (parseCssValue("backgroundColor", backgroundColorRaw, false) 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[] = [];
let tokenStream = background.trim();
tokenStream = tokenStream.endsWith(";")
@@ -63,111 +25,20 @@ export const backgroundToLonghand = (
: tokenStream;
}
- const cssAst = cssTryParseValue(tokenStream);
-
- if (cssAst === undefined) {
- return {
- backgroundImage: [],
- backgroundColor: undefined,
- };
- }
-
- let backgroundColorRaw: string | undefined;
-
- let nestingLevel = 0;
-
- csstree.walk(cssAst, {
- enter: (node, item, list) => {
- if (node.type === "Function") {
- if (gradientNames.includes(node.name)) {
- layers.push(csstree.generate(node));
- }
-
- // If the depth is at level 0 and the next item is null, it's likely that the backgroundColor
- // is written as rgba(x,y,z,a) or similar format.
- // nestingLevel is used as a fast way to check existance of parent Function node
- if (item.next === null && nestingLevel === 0) {
- backgroundColorRaw = csstree.generate(node);
- }
-
- nestingLevel++;
- return;
- }
-
- if (node.type === "Hash" && item.next === null && nestingLevel === 0) {
- // If the depth is at level 0 and the next item is null, it's likely that the backgroundColor
- // is written as hex #XYZFGH
- backgroundColorRaw = csstree.generate(node);
- return;
- }
-
- if (node.type === "Url") {
- layers.push(csstree.generate(node));
- }
-
- if (node.type === "Identifier" && node.name === "none") {
- layers.push(csstree.generate(node));
- }
- },
- leave: (node, item, list) => {
- if (node.type === "Function") {
- nestingLevel--;
- }
- },
- });
-
- return {
- backgroundImage: layers,
- backgroundColor: backgroundColorRaw,
+ 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;
};
};
-
-export const parseBackgroundImage = (
- layers: string[]
-): LayersValue | InvalidValue => {
- const backgroundImages: (UnparsedValue | ImageValue | KeywordValue)[] = [];
-
- for (const layer of layers) {
- if (layer === "none") {
- backgroundImages.push({
- type: "keyword",
- value: "none",
- });
- continue;
- }
-
- if (
- gradientNames.some((gradientName) => layer.startsWith(gradientName)) ===
- false &&
- layer.startsWith("url(") === false
- ) {
- break;
- }
-
- if (layer.startsWith("url(")) {
- backgroundImages.push({
- type: "image",
- value: {
- type: "url",
- url: layer
- .replace(/^url\(/, "")
- .replace(/\)$/, "")
- .replaceAll(/\\(.)/g, "$1"),
- },
- });
- continue;
- }
-
- const layerStyle = parseCssValue("backgroundImage", layer, false);
-
- if (layerStyle.type !== "unparsed") {
- break;
- }
-
- backgroundImages.push(layerStyle);
- }
-
- return backgroundImages.length > 0
- ? { type: "layers", value: backgroundImages }
- : { type: "invalid", value: layers.join(",") };
-};
diff --git a/packages/css-data/src/property-parsers/parsers.ts b/packages/css-data/src/property-parsers/parsers.ts
deleted file mode 100644
index bd9705d247..0000000000
--- a/packages/css-data/src/property-parsers/parsers.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-/*
- Longhand properties parsers. Each named export should match a property name.
-*/
-export { parseBackgroundImage as backgroundImage } from "./background";
-export { parseShadow } from "./shadows";
diff --git a/packages/css-data/src/property-parsers/to-longhand.ts b/packages/css-data/src/property-parsers/to-longhand.ts
deleted file mode 100644
index ba2d1fc36a..0000000000
--- a/packages/css-data/src/property-parsers/to-longhand.ts
+++ /dev/null
@@ -1,4 +0,0 @@
-/*
- Shorthand properties unwrappers. Each named export should match a property name.
-*/
-export { backgroundToLonghand as background } from "./background";
diff --git a/packages/css-data/src/shorthands.test.ts b/packages/css-data/src/shorthands.test.ts
index 6ab5e02ecb..1c2c78919d 100644
--- a/packages/css-data/src/shorthands.test.ts
+++ b/packages/css-data/src/shorthands.test.ts
@@ -1112,8 +1112,100 @@ test("expand background-position", () => {
);
});
+test("expand background", () => {
+ expect(expandShorthands([["background", `none`]])).toEqual([
+ ["background-image", "none"],
+ ["background-position-x", "0%"],
+ ["background-position-y", "0%"],
+ ["background-size", "auto auto"],
+ ["background-repeat", "repeat"],
+ ["background-attachment", "scroll"],
+ ["background-origin", "padding-box"],
+ ["background-clip", "border-box"],
+ ["background-color", "transparent"],
+ ]);
+ expect(expandShorthands([["background", `green`]])).toEqual([
+ ["background-image", "none"],
+ ["background-position-x", "0%"],
+ ["background-position-y", "0%"],
+ ["background-size", "auto auto"],
+ ["background-repeat", "repeat"],
+ ["background-attachment", "scroll"],
+ ["background-origin", "padding-box"],
+ ["background-clip", "border-box"],
+ ["background-color", "green"],
+ ]);
+ expect(expandShorthands([["background", `transparent`]])).toEqual([
+ ["background-image", "none"],
+ ["background-position-x", "0%"],
+ ["background-position-y", "0%"],
+ ["background-size", "auto auto"],
+ ["background-repeat", "repeat"],
+ ["background-attachment", "scroll"],
+ ["background-origin", "padding-box"],
+ ["background-clip", "border-box"],
+ ["background-color", "transparent"],
+ ]);
+ expect(
+ expandShorthands([["background", `url("test.jpg") repeat-y`]])
+ ).toEqual([
+ ["background-image", "url(test.jpg)"],
+ ["background-position-x", "0%"],
+ ["background-position-y", "0%"],
+ ["background-size", "auto auto"],
+ ["background-repeat", "repeat-y"],
+ ["background-attachment", "scroll"],
+ ["background-origin", "padding-box"],
+ ["background-clip", "border-box"],
+ ["background-color", "transparent"],
+ ]);
+ expect(expandShorthands([["background", `border-box red`]])).toEqual([
+ ["background-image", "none"],
+ ["background-position-x", "0%"],
+ ["background-position-y", "0%"],
+ ["background-size", "auto auto"],
+ ["background-repeat", "repeat"],
+ ["background-attachment", "scroll"],
+ ["background-origin", "border-box"],
+ ["background-clip", "border-box"],
+ ["background-color", "red"],
+ ]);
+ expect(
+ expandShorthands([
+ ["background", `no-repeat center/80% url("../img/image.png")`],
+ ])
+ ).toEqual([
+ ["background-image", "url(../img/image.png)"],
+ ["background-position-x", "center"],
+ ["background-position-y", "center"],
+ ["background-size", "80%"],
+ ["background-repeat", "no-repeat"],
+ ["background-attachment", "scroll"],
+ ["background-origin", "padding-box"],
+ ["background-clip", "border-box"],
+ ["background-color", "transparent"],
+ ]);
+ expect(
+ expandShorthands([
+ [
+ "background",
+ `repeat scroll 0% 0% / auto padding-box border-box none transparent`,
+ ],
+ ])
+ ).toEqual([
+ ["background-image", "none"],
+ ["background-position-x", "0%"],
+ ["background-position-y", "0%"],
+ ["background-size", "auto"],
+ ["background-repeat", "repeat"],
+ ["background-attachment", "scroll"],
+ ["background-origin", "padding-box"],
+ ["background-clip", "border-box"],
+ ["background-color", "transparent"],
+ ]);
+});
+
test.todo("all - can negatively affect build size");
-test.todo("background - not used in webflow");
// https://developer.mozilla.org/en-US/docs/Web/CSS/animation-range
test.todo("animation-range");
// https://developer.mozilla.org/en-US/docs/Web/CSS/view-timeline
diff --git a/packages/css-data/src/shorthands.ts b/packages/css-data/src/shorthands.ts
index 3488b3c579..9b0aca6060 100644
--- a/packages/css-data/src/shorthands.ts
+++ b/packages/css-data/src/shorthands.ts
@@ -68,7 +68,7 @@ const joinByOperator = (list: List<CssNode> | CssNode[], operator: string) => {
if (joined.length > 0) {
joined.push({ type: "Operator", value: operator });
}
- joined.push(node);
+ joined.push(...getValueList(node));
}
return joined;
};
@@ -880,6 +880,88 @@ const expandBackgroundPosition = function* (value: CssNode) {
yield ["background-position-y", y] as const;
};
+/**
+ *
+ * background = <bg-layer>#? , <final-bg-layer>
+ *
+ * <bg-layer> =
+ * <bg-image> ||
+ * <bg-position> [ / <bg-size> ]? ||
+ * <repeat-style> ||
+ * <attachment> ||
+ * <visual-box> ||j
+ * <visual-box>
+ *
+ * <final-bg-layer> =
+ * <bg-image> ||
+ * <bg-position> [ / <bg-size> ]? ||
+ * <repeat-style> ||
+ * <attachment> ||
+ * <visual-box> ||
+ * <visual-box> ||
+ * <'background-color'>
+ *
+ */
+const expandBackground = function* (value: CssNode) {
+ let backgroundColor: Value = createIdentifier("transparent");
+ const [image, position, size, repeat, attachment, origin, clip] =
+ parseRepeated(value, (single) => {
+ const [
+ image,
+ positionAndSize,
+ repeatStyle,
+ attachment,
+ origin,
+ clip,
+ color,
+ ] = parseUnordered(
+ [
+ `<bg-image>`,
+ `<bg-position> [ / <bg-size> ]?`,
+ `<repeat-style>`,
+ `<attachment>`,
+ `<visual-box>`,
+ `<visual-box>`,
+ `<'background-color'>`,
+ ],
+ single
+ );
+ let position: undefined | Value;
+ let size: undefined | Value;
+ if (positionAndSize) {
+ [position, size] = splitByOperator(positionAndSize, "/");
+ }
+ if (color) {
+ backgroundColor = color;
+ }
+ return [
+ image ?? createIdentifier("none"),
+ position ??
+ createValueNode([
+ { type: "Dimension", value: "0", unit: "%" },
+ { type: "Dimension", value: "0", unit: "%" },
+ ]),
+ size ??
+ createValueNode([
+ { type: "Identifier", name: "auto" },
+ { type: "Identifier", name: "auto" },
+ ]),
+ repeatStyle ?? createIdentifier("repeat"),
+ attachment ?? createIdentifier("scroll"),
+ origin ?? createIdentifier("padding-box"),
+ clip ?? origin ?? createIdentifier("border-box"),
+ ];
+ });
+ yield ["background-image", image] as const;
+ yield* expandBackgroundPosition(position);
+ yield ["background-size", size] as const;
+ yield ["background-repeat", repeat] as const;
+ yield ["background-attachment", attachment] as const;
+ yield ["background-origin", origin] as const;
+ yield ["background-clip", clip] as const;
+ yield ["background-color", backgroundColor] as const;
+};
+
const expandShorthand = function* (property: string, value: CssNode) {
switch (property) {
case "font":
@@ -1183,6 +1265,10 @@ const expandShorthand = function* (property: string, value: CssNode) {
yield* expandBackgroundPosition(value);
break;
+ case "background":
+ yield* expandBackground(value);
+ break;
+
default:
yield [property, value] as const;
}