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: fix comma parsing in style value and support comma in numbers with math expressions (#4081)
Oleg Isonenc64917aec3a5
diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.tsindex 7bac9ac921..7a539dc791 100644--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts@@ -14,7 +14,7 @@ export const parseIntermediateOrInvalidValue = (property: StyleProperty,styleValue: IntermediateStyleValue | InvalidValue): StyleValue => {- let value = styleValue.value.trim();+ const value = styleValue.value.trim();if (property.startsWith("--")) {return {@@ -23,12 +23,6 @@ export const parseIntermediateOrInvalidValue = (};}- // If input contains a number, we assume its either a value or value + unit.- // Users often mistype comma instead of dot and we want to be tolerant to that.- if (Number.isNaN(Number.parseFloat(value)) === false) {- value = value.replace(",", ".");- }-const valueInfo = properties[property as keyof typeof properties];// - When user enters a number, we don't know if its a valid unit value,@@ -73,7 +67,6 @@ export const parseIntermediateOrInvalidValue = (}// Try evaluate something like 10px + 4 or 13 + 4em-// Try to extract/remove anything similar to unit valueconst unitRegex = new RegExp(`(?:${unitsList.join("|")})`, "g");let matchedUnit = value.match(unitRegex)?.[0];@@ -114,6 +107,15 @@ export const parseIntermediateOrInvalidValue = (return styleInput;}+ // Users often mistype comma instead of dot and we want to be tolerant to that.+ // We need to try replace comma with dot and then try all parsing options again.+ if (value.includes(",")) {+ return parseIntermediateOrInvalidValue(property, {+ ...styleValue,+ value: value.replace(/,/g, "."),+ });+ }+// If we are here it means that value can be Valid but our parseCssValue can't handle it// or value is invalidreturn {diff --git a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.tsindex 0aba62e3d7..1de87fc6ac 100644--- a/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts+++ b/apps/builder/app/builder/features/style-panel/shared/css-value-input/parse-intermediate-or-invalid-value.ts.test.ts@@ -145,6 +145,20 @@ describe("Parse intermediate or invalid value without math evaluation", () => {unit: "rem",});});++ test("tolerate comma instead of dot typo while correctly parsing legit comma inside value", () => {+ const result = parseIntermediateOrInvalidValue("transitionDuration", {+ type: "intermediate",+ value: "1s, 2s",+ });+ expect(result).toEqual({+ type: "layers",+ value: [+ { type: "unit", unit: "s", value: 1 },+ { type: "unit", unit: "s", value: 2 },+ ],+ });+ });});describe("Parse intermediate or invalid value with math evaluation", () => {@@ -179,6 +193,36 @@ describe("Parse intermediate or invalid value with math evaluation", () => {}});+ test("tolerate comma instead of dot", () => {+ for (const propery of properties) {+ const result = parseIntermediateOrInvalidValue(propery, {+ type: "intermediate",+ value: "1,1 + 1,2",+ });++ expect(result).toEqual({+ type: "unit",+ value: 2.3,+ unit: "px",+ });+ }+ });++ test("tolerate comma instead of dot with unit", () => {+ for (const propery of properties) {+ const result = parseIntermediateOrInvalidValue(propery, {+ type: "intermediate",+ value: "1,1px + 1,2rem",+ });++ expect(result).toEqual({+ type: "unit",+ value: 2.3,+ unit: "px",+ });+ }+ });+test("switch on new unit if previous not known", () => {for (const propery of properties) {const result = parseIntermediateOrInvalidValue(propery, {