Skip to content

Commit 11f5450

Browse files
authored
fix: CSS unitless properties parsing (#4278)
## Description ref #3399 ## Steps for reproduction 1. click button 2. expect xyz ## Code Review - [ ] hi @kof, I need you to do - conceptual review (architecture, feature-correctness) - detailed review (read every line) - test it on preview ## Before requesting a review - [ ] made a self-review - [ ] added inline comments where things may be not obvious (the "why", not "what") ## Before merging - [ ] tested locally and on preview environment (preview dev login: 5de6) - [ ] updated [test cases](https://github.com/webstudio-is/webstudio/blob/main/apps/builder/docs/test-cases.md) document - [ ] added tests - [ ] if any new env variables are added, added them to `.env` file
1 parent 80778e3 commit 11f5450

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,15 @@ export const parseIntermediateOrInvalidValue = (
2727
"children" in ast && ast.children?.size === 1
2828
? ast.children.first
2929
: undefined;
30+
3031
if (node?.type === "Number") {
31-
const testUnit = "unit" in styleValue ? (styleValue.unit ?? "px") : "px";
32+
const unit = "unit" in styleValue ? styleValue.unit : undefined;
33+
34+
// Use number as a fallback for custom properties
35+
const fallbackUnitAsString = property.startsWith("--") ? "" : "px";
36+
37+
const testUnit = unit === "number" ? "" : (unit ?? fallbackUnitAsString);
38+
3239
const styleInput = parseCssValue(property, `${value}${testUnit}`);
3340

3441
if (styleInput.type !== "invalid") {

apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,24 @@ test("parse unit in css variable", () => {
570570
});
571571
});
572572

573+
test("prefer unitless css variable", () => {
574+
expect(
575+
parseIntermediateOrInvalidValue("--size", {
576+
type: "intermediate",
577+
value: "1",
578+
unit: undefined,
579+
})
580+
).toEqual({ type: "unit", value: 1, unit: "number" });
581+
582+
expect(
583+
parseIntermediateOrInvalidValue("--size", {
584+
type: "intermediate",
585+
value: "1",
586+
unit: "number",
587+
})
588+
).toEqual({ type: "unit", value: 1, unit: "number" });
589+
});
590+
573591
test("parse color in css variable", () => {
574592
expect(
575593
parseIntermediateOrInvalidValue("--size", {

0 commit comments

Comments
 (0)