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.
TypeScript accessibilityalternativecloudflarecloudflare-workersdesignfreefrontendopen-sourceradix-uireactremixweb-developmentwebflowwebsite-builderwebsite-developmentwebsiteswebstudio
fix: expand CSS shorthands in MCP style updates (#5941)
Oleg Isonena803aa352d65
diff --git a/packages/project-build/src/runtime/styles.test.ts b/packages/project-build/src/runtime/styles.test.tsindex 36f84a3196..93a4935843 100644--- a/packages/project-build/src/runtime/styles.test.ts+++ b/packages/project-build/src/runtime/styles.test.ts@@ -84,7 +84,7 @@ import {createLocalStyleSourcePatchPlan,createLocalStyleSourcePlan,createStyleDecl as createStyleDeclValue,- createStyleDeclFromInput,+ createStyleDeclsFromInput,deleteLocalStyleSourcesMutable,deleteStyleDeclMutable,deleteStyleSourceMutable,@@ -2072,21 +2072,55 @@ describe("style declaration helpers", () => {});});- test("creates style declarations from user input with base breakpoint default", () => {+ test("creates a longhand style declaration with base breakpoint default", () => {expect(- createStyleDeclFromInput({+ createStyleDeclsFromInput({styleSourceId: "token",property: "color",value: { type: "keyword", value: "red" },state: ":hover",})- ).toEqual({- styleSourceId: "token",- breakpointId: "base",- property: "color",- value: { type: "keyword", value: "red" },- state: ":hover",- });+ ).toEqual([+ {+ styleSourceId: "token",+ breakpointId: "base",+ property: "color",+ value: { type: "keyword", value: "red" },+ state: ":hover",+ },+ ]);+ });++ test("expands shorthand style input into editable longhands", () => {+ expect(+ createStyleDeclsFromInput({+ styleSourceId: "token",+ property: "inset",+ value: { type: "unparsed", value: "0" },+ breakpoint: "desktop",+ state: ":hover",+ listed: true,+ })+ ).toEqual(+ ["top", "right", "bottom", "left"].map((property) => ({+ styleSourceId: "token",+ breakpointId: "desktop",+ listed: true,+ property,+ value: { type: "unit", value: 0, unit: "number" },+ state: ":hover",+ }))+ );+ });++ test("rejects invalid shorthand values", () => {+ expect(() =>+ createStyleDeclsFromInput({+ styleSourceId: "token",+ property: "inset",+ value: { type: "unparsed", value: "1px /" },+ })+ ).toThrow('Invalid value for shorthand CSS property "inset"');});test("creates style declaration keys from user input with base breakpoint default", () => {@@ -2617,6 +2651,35 @@ describe("createStyleDeclarationUpdatePayload", () => {expect(missingLocalStyleSourceInstanceIds).toEqual([]);});+ test("expands shorthand updates into local longhand patches", () => {+ const { payload, styleKeys } = createStyleDeclarationUpdatePayload({+ styleSources: sources([local("local")]),+ styleSourceSelections: [{ instanceId: "box", values: ["local"] }],+ styles: [],+ createId,+ updates: [+ {+ instanceId: "box",+ property: "inset",+ value: { type: "unparsed", value: "0" },+ },+ ],+ });++ expect(payload[0]?.patches.map((patch) => patch.path[0])).toEqual([+ "local:base:top:",+ "local:base:right:",+ "local:base:bottom:",+ "local:base:left:",+ ]);+ expect(styleKeys).toEqual([+ "local:base:top:",+ "local:base:right:",+ "local:base:bottom:",+ "local:base:left:",+ ]);+ });+test("reports missing local style source when creation is disabled", () => {const result = createStyleDeclarationUpdatePayload({styleSources: new Map(),@@ -2944,6 +3007,23 @@ describe("design token patch helpers", () => {expect(styleKeys).toEqual(["token:base:color:", "token:base:color:"]);});+ test("expands shorthand updates for design tokens", () => {+ const { payload } = createDesignTokenStyleUpdatePayload({+ designTokenId: "token",+ styles: [],+ updates: [+ { property: "gridArea", value: { type: "unparsed", value: "1 / 1" } },+ ],+ });++ expect(payload[0]?.patches.map((patch) => patch.path[0])).toEqual([+ "token:base:gridRowStart:",+ "token:base:gridColumnStart:",+ "token:base:gridRowEnd:",+ "token:base:gridColumnEnd:",+ ]);+ });+test("returns empty payload when token style updates are empty", () => {const { payload, styleKeys } = createDesignTokenStyleUpdatePayload({designTokenId: "token",diff --git a/packages/project-build/src/runtime/styles.ts b/packages/project-build/src/runtime/styles.tsindex 6c711f40a3..fb6457d280 100644--- a/packages/project-build/src/runtime/styles.ts+++ b/packages/project-build/src/runtime/styles.ts@@ -3,7 +3,9 @@ import { nanoid } from "nanoid";import deepEqual from "fast-deep-equal";import {camelCaseProperty,+ parseCss,parseCssValue,+ shorthandProperties,validateSelector,} from "@webstudio-is/css-data";import {@@ -1827,7 +1829,7 @@ export const createStyleDecl = ({listed,});-export const createStyleDeclFromInput = ({+export const createStyleDeclsFromInput = ({styleSourceId,property,value,@@ -1841,15 +1843,50 @@ export const createStyleDeclFromInput = ({breakpoint?: string;state?: StyleDecl["state"];listed?: StyleDecl["listed"];-}): StyleDecl =>- createStyleDecl({- styleSourceId,- breakpointId: breakpoint,- state,- property,- value,- listed,- });+}): StyleDecl[] => {+ const createStyleDeclWithInput = (property: unknown, value: unknown) =>+ createStyleDecl({+ styleSourceId,+ breakpointId: breakpoint,+ property,+ value,+ state,+ listed,+ });+ const normalizedProperty = normalizeStyleProperty(property);+ const cssProperty = hyphenateProperty(String(normalizedProperty));+ if (+ shorthandProperties.includes(+ cssProperty as (typeof shorthandProperties)[number]+ ) === false+ ) {+ return [createStyleDeclWithInput(property, value)];+ }++ const parsedValue = styleValue.safeParse(value);+ if (parsedValue.success === false) {+ return [createStyleDeclWithInput(property, value)];+ }+ const parsed = parseCss(+ `.styles{${cssProperty}:${toValue(parsedValue.data)}}`,+ new Map()+ );+ if (+ parsed.errors.length > 0 ||+ parsed.styles.length === 0 ||+ parsed.styles.some(({ value: parsedStyleValue }) =>+ ["invalid", "guaranteedInvalid"].includes(parsedStyleValue.type)+ )+ ) {+ return throwBuilderRuntimeError(+ "BAD_REQUEST",+ `Invalid value for shorthand CSS property "${cssProperty}"`+ );+ }+ return parsed.styles.map((style) =>+ createStyleDeclWithInput(style.property, style.value)+ );+};export const getStyleDeclKeyFromInput = ({styleSourceId,@@ -2609,18 +2646,19 @@ export const createDesignTokenCreatePayload = ({value: styleSource,});for (const declaration of createDesignTokenStyleInputs(token)) {- const style = createStyleDeclFromInput({+ for (const style of createStyleDeclsFromInput({styleSourceId: tokenId,breakpoint: declaration.breakpoint,property: declaration.property,value: declaration.value,state: declaration.state,- });- stylePatches.push({- op: "add" as const,- path: [getStyleDeclKey(style)],- value: style,- });+ })) {+ stylePatches.push({+ op: "add" as const,+ path: [getStyleDeclKey(style)],+ value: style,+ });+ }}}@@ -2655,23 +2693,24 @@ export const createDesignTokenStyleUpdatePayload = ({const styleKeys = new Set(Array.from(styles, (styleDecl) => getStyleDeclKey(styleDecl)));- const patches = updates.map((update) => {- const style = createStyleDeclFromInput({+ const patches = updates.flatMap((update) =>+ createStyleDeclsFromInput({styleSourceId: designTokenId,breakpoint: update.breakpoint,property: update.property,value: update.value,state: update.state,- });- const key = getStyleDeclKey(style);- const patch = {- op: styleKeys.has(key) ? ("replace" as const) : ("add" as const),- path: [key],- value: style,- };- styleKeys.add(key);- return patch;- });+ }).map((style) => {+ const key = getStyleDeclKey(style);+ const patch = {+ op: styleKeys.has(key) ? ("replace" as const) : ("add" as const),+ path: [key],+ value: style,+ };+ styleKeys.add(key);+ return patch;+ })+ );return {payload:@@ -2869,20 +2908,21 @@ export const createStyleDeclarationUpdatePayload = ({continue;}payload.push(...result.payload);- const nextStyleDecl = createStyleDeclFromInput({+ for (const nextStyleDecl of createStyleDeclsFromInput({styleSourceId: result.styleSourceId,breakpoint: update.breakpoint,property: update.property,value: update.value,state: update.state,listed: update.listed,- });- const key = getStyleDeclKey(nextStyleDecl);- stylePatches.set(key, {- op: existingStyleKeys.has(key) ? "replace" : "add",- path: [key],- value: nextStyleDecl,- });+ })) {+ const key = getStyleDeclKey(nextStyleDecl);+ stylePatches.set(key, {+ op: existingStyleKeys.has(key) ? "replace" : "add",+ path: [key],+ value: nextStyleDecl,+ });+ }}const patches = Array.from(stylePatches.values());@@ -3028,23 +3068,24 @@ export const createSelectedStyleDeclarationUpdatePayload = ({]);}- const styleDecl = createStyleDeclFromInput({+ for (const styleDecl of createStyleDeclsFromInput({styleSourceId: update.styleSourceId,breakpoint: update.breakpoint,property: update.property,value: update.value,state: update.state,listed: update.listed,- });- const styleKey = getStyleDeclKey(styleDecl);- stylePatches.push({- op: existingStyleKeys.has(styleKey)- ? ("replace" as const)- : ("add" as const),- path: [styleKey],- value: styleDecl,- });- existingStyleKeys.add(styleKey);+ })) {+ const styleKey = getStyleDeclKey(styleDecl);+ stylePatches.push({+ op: existingStyleKeys.has(styleKey)+ ? ("replace" as const)+ : ("add" as const),+ path: [styleKey],+ value: styleDecl,+ });+ existingStyleKeys.add(styleKey);+ }}if (styleSourcePatches.length > 0) {