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: parse background values as layers (#3645)

Bogdan Chadkin5daf1cdacd68
diff --git a/packages/css-data/src/parse-css-value.test.ts b/packages/css-data/src/parse-css-value.test.ts
index ef1b606c53..468d0092eb 100644
--- a/packages/css-data/src/parse-css-value.test.ts
+++ b/packages/css-data/src/parse-css-value.test.ts
@@ -84,8 +84,8 @@ describe("Parse CSS value", () => {
});
describe("Tuples", () => {
- test("backgroundPosition", () => {
- expect(parseCssValue("backgroundPosition", "left top")).toEqual({
+ test("objectPosition", () => {
+ expect(parseCssValue("objectPosition", "left top")).toEqual({
type: "tuple",
value: [
{
@@ -140,3 +140,107 @@ describe("Parse CSS value", () => {
});
});
});
+
+test("parse background-image property as layers", () => {
+ expect(
+ parseCssValue(
+ "backgroundImage",
+ `linear-gradient(180deg, hsla(0, 0.00%, 0.00%, 0.11), white), url("https://667d0b7769e0cc3754b584f6"), none, url("https://667d0fe180995eadc1534a26")`
+ )
+ ).toEqual({
+ type: "layers",
+ value: [
+ {
+ type: "unparsed",
+ value: "linear-gradient(180deg,hsla(0,0.00%,0.00%,0.11),white)",
+ },
+ {
+ type: "image",
+ value: { type: "url", url: "https://667d0b7769e0cc3754b584f6" },
+ },
+ {
+ type: "keyword",
+ value: "none",
+ },
+ {
+ type: "image",
+ value: { type: "url", url: "https://667d0fe180995eadc1534a26" },
+ },
+ ],
+ });
+});
+
+test("parse background-position property as layers", () => {
+ expect(
+ parseCssValue("backgroundPosition", `0px 0px, 550px 0px, 0px 0px, 0px 0px`)
+ ).toEqual({
+ type: "layers",
+ value: [
+ {
+ type: "tuple",
+ value: [
+ { type: "unit", unit: "px", value: 0 },
+ { type: "unit", unit: "px", value: 0 },
+ ],
+ },
+ {
+ type: "tuple",
+ value: [
+ { type: "unit", unit: "px", value: 550 },
+ { type: "unit", unit: "px", value: 0 },
+ ],
+ },
+ {
+ type: "tuple",
+ value: [
+ { type: "unit", unit: "px", value: 0 },
+ { type: "unit", unit: "px", value: 0 },
+ ],
+ },
+ {
+ type: "tuple",
+ value: [
+ { type: "unit", unit: "px", value: 0 },
+ { type: "unit", unit: "px", value: 0 },
+ ],
+ },
+ ],
+ });
+});
+
+test("parse background-size property as layers", () => {
+ expect(parseCssValue("backgroundSize", `auto, contain, auto, auto`)).toEqual({
+ type: "layers",
+ value: [
+ { type: "keyword", value: "auto" },
+ { type: "keyword", value: "contain" },
+ { type: "keyword", value: "auto" },
+ { type: "keyword", value: "auto" },
+ ],
+ });
+ expect(
+ parseCssValue("backgroundRepeat", `repeat, no-repeat, repeat, repeat`)
+ ).toEqual({
+ type: "layers",
+ value: [
+ { type: "keyword", value: "repeat" },
+ { type: "keyword", value: "no-repeat" },
+ { type: "keyword", value: "repeat" },
+ { type: "keyword", value: "repeat" },
+ ],
+ });
+});
+
+test("parse background-attachment property as layers", () => {
+ expect(
+ parseCssValue("backgroundAttachment", `scroll, fixed, scroll, scroll`)
+ ).toEqual({
+ type: "layers",
+ value: [
+ { type: "keyword", value: "scroll" },
+ { type: "keyword", value: "fixed" },
+ { type: "keyword", value: "scroll" },
+ { type: "keyword", value: "scroll" },
+ ],
+ });
+});
diff --git a/packages/css-data/src/parse-css-value.ts b/packages/css-data/src/parse-css-value.ts
index 34952438df..b8718c719d 100644
--- a/packages/css-data/src/parse-css-value.ts
+++ b/packages/css-data/src/parse-css-value.ts
@@ -4,6 +4,7 @@ import warnOnce from "warn-once";
import {
TupleValue,
hyphenateProperty,
+ type LayerValueItem,
type StyleProperty,
type StyleValue,
type Unit,
@@ -12,6 +13,7 @@ import { keywordValues } from "./__generated__/keyword-values";
import { units } from "./__generated__/units";
import {
isTransitionLongHandProperty,
+ parseBackground,
parseFilter,
parseShadow,
parseTransitionLonghandProperty,
@@ -76,9 +78,20 @@ export const isValidDeclaration = (
return matchResult.matched != null;
};
+const repeatedProps = new Set<StyleProperty>([
+ "backgroundAttachment",
+ "backgroundClip",
+ "backgroundBlendMode",
+ "backgroundOrigin",
+ "backgroundPosition",
+ "backgroundRepeat",
+ "backgroundSize",
+]);
+
export const parseCssValue = (
property: StyleProperty, // Handles only long-hand values.
- input: string
+ input: string,
+ topLevel = true
): StyleValue => {
const invalidValue = {
type: "invalid",
@@ -115,11 +128,25 @@ export const parseCssValue = (
return parseTransitionLonghandProperty(property, input);
}
- if (
- ast != null &&
- ast.type === "Value" &&
- ast.children.first === ast.children.last
- ) {
+ // prevent infinite splitting into layers for items
+ if (repeatedProps.has(property) && topLevel) {
+ return {
+ type: "layers",
+ value: input
+ .split(",")
+ .map(
+ (single) => parseCssValue(property, single, 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;
@@ -195,14 +222,16 @@ export const parseCssValue = (
}
// Probably a tuple like background-position
- if (ast != null && ast.type === "Value" && ast.children.size === 2) {
+ if (ast.type === "Value" && ast.children.size === 2) {
const tupleFirst = parseCssValue(
property,
- csstree.generate(ast.children.first!)
+ csstree.generate(ast.children.first!),
+ false
);
const tupleLast = parseCssValue(
property,
- csstree.generate(ast.children.last!)
+ csstree.generate(ast.children.last!),
+ false
);
const tupleResult = TupleValue.safeParse({
diff --git a/packages/css-data/src/parse-css.test.ts b/packages/css-data/src/parse-css.test.ts
index add723437c..0a10c48baf 100644
--- a/packages/css-data/src/parse-css.test.ts
+++ b/packages/css-data/src/parse-css.test.ts
@@ -73,194 +73,6 @@ describe("Parse CSS", () => {
`);
});
- test("parses supported shorthand values 2", () => {
- const css = `
- .test {
- background-image: linear-gradient(180deg, hsla(0, 0.00%, 0.00%, 0.11), white), url("https://667d0b7769e0cc3754b584f6"), none, url("https://667d0fe180995eadc1534a26");
- background-position: 0px 0px, 550px 0px, 0px 0px, 0px 0px;
- background-size: auto, contain, auto, auto;
- background-repeat: repeat, no-repeat, repeat, repeat;
- background-attachment: scroll, fixed, scroll, scroll;
- }
- `;
- expect(parseCss(css)).toMatchInlineSnapshot(`
-{
- "test": [
- {
- "property": "backgroundImage",
- "value": {
- "type": "layers",
- "value": [
- {
- "type": "unparsed",
- "value": "linear-gradient(180deg,hsla(0,0.00%,0.00%,0.11),white)",
- },
- {
- "type": "image",
- "value": {
- "type": "url",
- "url": "https://667d0b7769e0cc3754b584f6",
- },
- },
- {
- "type": "keyword",
- "value": "none",
- },
- {
- "type": "image",
- "value": {
- "type": "url",
- "url": "https://667d0fe180995eadc1534a26",
- },
- },
- ],
- },
- },
- {
- "property": "backgroundPosition",
- "value": {
- "type": "layers",
- "value": [
- {
- "type": "tuple",
- "value": [
- {
- "type": "unit",
- "unit": "px",
- "value": 0,
- },
- {
- "type": "unit",
- "unit": "px",
- "value": 0,
- },
- ],
- },
- {
- "type": "tuple",
- "value": [
- {
- "type": "unit",
- "unit": "px",
- "value": 550,
- },
- {
- "type": "unit",
- "unit": "px",
- "value": 0,
- },
- ],
- },
- {
- "type": "tuple",
- "value": [
- {
- "type": "unit",
- "unit": "px",
- "value": 0,
- },
- {
- "type": "unit",
- "unit": "px",
- "value": 0,
- },
- ],
- },
- {
- "type": "tuple",
- "value": [
- {
- "type": "unit",
- "unit": "px",
- "value": 0,
- },
- {
- "type": "unit",
- "unit": "px",
- "value": 0,
- },
- ],
- },
- ],
- },
- },
- {
- "property": "backgroundSize",
- "value": {
- "type": "layers",
- "value": [
- {
- "type": "keyword",
- "value": "auto",
- },
- {
- "type": "keyword",
- "value": "contain",
- },
- {
- "type": "keyword",
- "value": "auto",
- },
- {
- "type": "keyword",
- "value": "auto",
- },
- ],
- },
- },
- {
- "property": "backgroundRepeat",
- "value": {
- "type": "layers",
- "value": [
- {
- "type": "keyword",
- "value": "repeat",
- },
- {
- "type": "keyword",
- "value": "no-repeat",
- },
- {
- "type": "keyword",
- "value": "repeat",
- },
- {
- "type": "keyword",
- "value": "repeat",
- },
- ],
- },
- },
- {
- "property": "backgroundAttachment",
- "value": {
- "type": "layers",
- "value": [
- {
- "type": "keyword",
- "value": "scroll",
- },
- {
- "type": "keyword",
- "value": "fixed",
- },
- {
- "type": "keyword",
- "value": "scroll",
- },
- {
- "type": "keyword",
- "value": "scroll",
- },
- ],
- },
- },
- ],
-}
-`);
- });
-
test("parses single layer", () => {
const css = `
.test {
diff --git a/packages/css-data/src/parse-css.ts b/packages/css-data/src/parse-css.ts
index e632b369d2..ff0d57e945 100644
--- a/packages/css-data/src/parse-css.ts
+++ b/packages/css-data/src/parse-css.ts
@@ -1,16 +1,11 @@
import * as csstree from "css-tree";
-import {
- LayersValue,
- StyleValue,
- type StyleProperty,
-} from "@webstudio-is/css-engine";
+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";
-import { parseBackground } from "./property-parsers";
/**
* Store prefixed properties without change
@@ -130,83 +125,6 @@ const cssTreeTryParse = (input: string) => {
}
};
-const convertBackgroundProps = (styles: EmbedTemplateStyleDecl[]) => {
- const backgroundProps = [
- "backgroundAttachment",
- "backgroundClip",
- "backgroundBlendMode",
- "backgroundOrigin",
- "backgroundPosition",
- "backgroundRepeat",
- "backgroundSize",
- ];
-
- return styles
- .map((style) => {
- if (backgroundProps.includes(style.property)) {
- if (style.value.type !== "unparsed") {
- const safeStyle = LayersValue.safeParse({
- type: "layers",
- value: [style.value],
- });
- if (safeStyle.success) {
- return {
- property: style.property,
- value: safeStyle.data,
- };
- }
- return style;
- }
-
- const layersResult = LayersValue.safeParse({
- type: "layers",
- value: style.value.value
- .split(",")
- .map((val) => parseCssValueLonghand(style.property, val)),
- });
-
- if (layersResult.success) {
- return {
- property: style.property,
- value: layersResult.data,
- };
- }
-
- console.error(
- `Failed to convert background property ${
- style.property
- } with value ${JSON.stringify(style.value)} to layers`
- );
- }
- return style;
- })
- .map((style) => {
- if (style.property === "backgroundImage") {
- if (style.value.type !== "unparsed") {
- const safeStyle = LayersValue.safeParse({
- type: "layers",
- value: [style.value],
- });
- if (safeStyle.success) {
- return {
- property: style.property,
- value: safeStyle.data,
- };
- }
- return style;
- }
-
- const { backgroundImage } = parseBackground(style.value.value);
-
- return {
- property: style.property,
- value: backgroundImage,
- };
- }
- return style;
- });
-};
-
export const parseCss = (css: string) => {
const ast = cssTreeTryParse(css);
let selectors: Selector[] = [];
@@ -294,11 +212,5 @@ export const parseCss = (css: string) => {
}
});
- const stylesResult: Styles = {};
-
- for (const [selector, declarations] of Object.entries(styles)) {
- stylesResult[selector] = convertBackgroundProps(declarations);
- }
-
- return stylesResult;
+ return styles;
};
diff --git a/packages/css-data/src/property-parsers/background.ts b/packages/css-data/src/property-parsers/background.ts
index 1bda3a98a7..0e047e5fee 100644
--- a/packages/css-data/src/property-parsers/background.ts
+++ b/packages/css-data/src/property-parsers/background.ts
@@ -28,7 +28,7 @@ export const parseBackground = (
backgroundToLonghand(background);
const backgroundColor = backgroundColorRaw
- ? (parseCssValue("backgroundColor", backgroundColorRaw) as RgbValue)
+ ? (parseCssValue("backgroundColor", backgroundColorRaw, false) as RgbValue)
: undefined;
return {
@@ -158,7 +158,7 @@ export const parseBackgroundImage = (
continue;
}
- const layerStyle = parseCssValue("backgroundImage", layer);
+ const layerStyle = parseCssValue("backgroundImage", layer, false);
if (layerStyle.type !== "unparsed") {
break;