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.

fix: expandShorthands fails and prevent builder to load (#3702)

Ivan Starkovb6a164f90de5
diff --git a/apps/builder/app/builder/builder.tsx b/apps/builder/app/builder/builder.tsx
index 08edcd7d98..42fd75ce0f 100644
--- a/apps/builder/app/builder/builder.tsx
+++ b/apps/builder/app/builder/builder.tsx
@@ -391,7 +391,8 @@ export const Builder = ({
// when canvas is rendered
$dataLoadingState.set("loaded");
})
- .catch(() => {
+ .catch((error) => {
+ console.error(error);
// @todo make needs error handling and error state? e.g. a toast
$dataLoadingState.set("idle");
});
diff --git a/packages/css-data/src/shorthands.test.ts b/packages/css-data/src/shorthands.test.ts
index 480a898465..c44fe07c20 100644
--- a/packages/css-data/src/shorthands.test.ts
+++ b/packages/css-data/src/shorthands.test.ts
@@ -1448,3 +1448,88 @@ test("expand position-try", () => {
["position-try-options", "--dashed-ident"],
]);
});
+
+test("replace empty value with unset", () => {
+ expect(expandShorthands([["color", ""]])).toEqual([["color", "unset"]]);
+ expect(expandShorthands([["transition", ""]])).toEqual([
+ ["transition-property", "unset"],
+ ["transition-duration", "unset"],
+ ["transition-timing-function", "unset"],
+ ["transition-delay", "unset"],
+ ["transition-behavior", "unset"],
+ ]);
+});
+
+test("does not fail on empty value", () => {
+ expect(() => expandShorthands([["transition", ""]])).not.toThrow();
+ expect(() => expandShorthands([["border", ""]])).not.toThrow();
+ expect(() => expandShorthands([["font", ""]])).not.toThrow();
+ expect(() => expandShorthands([["font-synthesis", ""]])).not.toThrow();
+ expect(() => expandShorthands([["font-variant", ""]])).not.toThrow();
+ expect(() => expandShorthands([["text-decoration", ""]])).not.toThrow();
+ expect(() => expandShorthands([["text-emphasis", ""]])).not.toThrow();
+ expect(() => expandShorthands([["border-width", ""]])).not.toThrow();
+ expect(() => expandShorthands([["border-style", ""]])).not.toThrow();
+ expect(() => expandShorthands([["border-color", ""]])).not.toThrow();
+ expect(() => expandShorthands([["border-inline-width", ""]])).not.toThrow();
+ expect(() => expandShorthands([["border-inline-style", ""]])).not.toThrow();
+ expect(() => expandShorthands([["border-inline-color", ""]])).not.toThrow();
+ expect(() => expandShorthands([["border-block-width", ""]])).not.toThrow();
+ expect(() => expandShorthands([["border-block-style", ""]])).not.toThrow();
+ expect(() => expandShorthands([["border-block-color", ""]])).not.toThrow();
+ expect(() => expandShorthands([["border-radius", ""]])).not.toThrow();
+ expect(() => expandShorthands([["border-image", ""]])).not.toThrow();
+ expect(() => expandShorthands([["outline", ""]])).not.toThrow();
+ expect(() => expandShorthands([["mask", ""]])).not.toThrow();
+ expect(() => expandShorthands([["mask-border", ""]])).not.toThrow();
+ expect(() => expandShorthands([["margin", ""]])).not.toThrow();
+ expect(() => expandShorthands([["padding", ""]])).not.toThrow();
+ expect(() => expandShorthands([["margin-inline", ""]])).not.toThrow();
+ expect(() => expandShorthands([["margin-block", ""]])).not.toThrow();
+ expect(() => expandShorthands([["padding-inline", ""]])).not.toThrow();
+ expect(() => expandShorthands([["padding-block", ""]])).not.toThrow();
+ expect(() => expandShorthands([["inset", ""]])).not.toThrow();
+ expect(() => expandShorthands([["inset-inline", ""]])).not.toThrow();
+ expect(() => expandShorthands([["inset-block", ""]])).not.toThrow();
+ expect(() => expandShorthands([["gap", ""]])).not.toThrow();
+ expect(() => expandShorthands([["grid-gap", ""]])).not.toThrow();
+ expect(() => expandShorthands([["grid-row-gap", ""]])).not.toThrow();
+ expect(() => expandShorthands([["grid-column-gap", ""]])).not.toThrow();
+ expect(() => expandShorthands([["grid-area", ""]])).not.toThrow();
+ expect(() => expandShorthands([["grid-row", ""]])).not.toThrow();
+ expect(() => expandShorthands([["grid-column", ""]])).not.toThrow();
+ expect(() => expandShorthands([["grid-template", ""]])).not.toThrow();
+ expect(() => expandShorthands([["grid", ""]])).not.toThrow();
+ expect(() => expandShorthands([["flex", ""]])).not.toThrow();
+ expect(() => expandShorthands([["flex-flow", ""]])).not.toThrow();
+ expect(() => expandShorthands([["place-content", ""]])).not.toThrow();
+ expect(() => expandShorthands([["place-items", ""]])).not.toThrow();
+ expect(() => expandShorthands([["place-self", ""]])).not.toThrow();
+ expect(() => expandShorthands([["columns", ""]])).not.toThrow();
+ expect(() => expandShorthands([["column-rule", ""]])).not.toThrow();
+ expect(() => expandShorthands([["list-style", ""]])).not.toThrow();
+ expect(() => expandShorthands([["animation", ""]])).not.toThrow();
+ expect(() => expandShorthands([["animation-range", ""]])).not.toThrow();
+ expect(() => expandShorthands([["transition", ""]])).not.toThrow();
+ expect(() => expandShorthands([["offset", ""]])).not.toThrow();
+ expect(() => expandShorthands([["scroll-timeline", ""]])).not.toThrow();
+ expect(() => expandShorthands([["view-timeline", ""]])).not.toThrow();
+ expect(() => expandShorthands([["scroll-margin", ""]])).not.toThrow();
+ expect(() => expandShorthands([["scroll-padding", ""]])).not.toThrow();
+ expect(() => expandShorthands([["scroll-margin-inline", ""]])).not.toThrow();
+ expect(() => expandShorthands([["scroll-margin-block", ""]])).not.toThrow();
+ expect(() => expandShorthands([["scroll-padding-inline", ""]])).not.toThrow();
+ expect(() => expandShorthands([["scroll-padding-block", ""]])).not.toThrow();
+ expect(() => expandShorthands([["overflow", ""]])).not.toThrow();
+ expect(() => expandShorthands([["container", ""]])).not.toThrow();
+ expect(() =>
+ expandShorthands([["contain-intrinsic-size", ""]])
+ ).not.toThrow();
+ expect(() => expandShorthands([["white-space", ""]])).not.toThrow();
+ expect(() => expandShorthands([["text-wrap", ""]])).not.toThrow();
+ expect(() => expandShorthands([["caret", ""]])).not.toThrow();
+ expect(() => expandShorthands([["background-position", ""]])).not.toThrow();
+ expect(() => expandShorthands([["background", ""]])).not.toThrow();
+ expect(() => expandShorthands([["overscroll-behavior", ""]])).not.toThrow();
+ expect(() => expandShorthands([["position-try", ""]])).not.toThrow();
+});
diff --git a/packages/css-data/src/shorthands.ts b/packages/css-data/src/shorthands.ts
index a0d44103e2..f41b9ab2b7 100644
--- a/packages/css-data/src/shorthands.ts
+++ b/packages/css-data/src/shorthands.ts
@@ -7,6 +7,7 @@ import {
type CssNode,
type Value,
} from "css-tree";
+import warnOnce from "warn-once";
const cssWideKeywordsSyntax = Array.from(cssWideKeywords).join(" | ");
@@ -57,12 +58,9 @@ const splitByOperator = (node: CssNode, operator: string) => {
lists.at(-1)?.push(node);
}
}
- return lists.map((list) => {
- if (list.length === 0) {
- return;
- }
- return createValueNode(list);
- });
+ return lists
+ .filter((list) => list.length > 0)
+ .map((list) => createValueNode(list));
};
const joinByOperator = (list: List<CssNode> | CssNode[], operator: string) => {
@@ -1392,6 +1390,9 @@ const expandShorthand = function* (property: string, value: CssNode) {
const parseValue = function* (property: string, value: string) {
try {
const ast = parse(value, { context: "value" });
+ if (ast.type === "Value" && ast.children.isEmpty) {
+ ast.children.appendData({ type: "Identifier", name: "unset" });
+ }
yield [property, ast] as const;
} catch {
// empty block
@@ -1419,7 +1420,14 @@ export const expandShorthands = (
const generator = expandShorthand(property, value);
for (const [property, value] of generator) {
- longhands.push([property, generate(cssWideKeyword ?? value)]);
+ try {
+ longhands.push([property, generate(cssWideKeyword ?? value)]);
+ } catch {
+ warnOnce(
+ true,
+ `Failed to generate longhands for shorthand ${shorthands.map((shorthand) => shorthand.join("=")).join(", ")}`
+ );
+ }
}
}
}