diff --git a/apps/designer/app/designer/features/style-panel/shared/parse-css-value.ts b/apps/designer/app/designer/features/style-panel/shared/parse-css-value.ts index e127d845bf..16d253c65d 100644 --- a/apps/designer/app/designer/features/style-panel/shared/parse-css-value.ts +++ b/apps/designer/app/designer/features/style-panel/shared/parse-css-value.ts @@ -22,31 +22,36 @@ export const isValid = (property: string, value: string): boolean => { return true; }; -// wtf? -// eslint-disable-next-line no-useless-escape -const mathRegex = /[\+\-\*\/]/; +// If expression is a math expression, evaluates it. +// Otherwise returns undefined. +const evaluateMath = (expression: string) => { + if (/^[\d\s.+*/-]+$/.test(expression) === false) { + return undefined; + } + try { + // Eval is safe here because of the regex above + const result = eval(`(${expression})`); + if (typeof result === "number") { + return result; + } + } catch (err) { + return undefined; + } +}; // - 2+2px // - 2*2 const evaluate = (input: string, parsedUnit: [Unit] | null) => { - const parsed = parseFloat(input); - // If its not a number, it can't be a math expression. - if (isNaN(parsed)) return parsed; + const result = evaluateMath( + parsedUnit === null ? input : input.replace(parsedUnit[0], "") + ); - // It's a math expression - if (mathRegex.test(input)) { - // Get rid of the unit - if (parsedUnit !== null) { - input = input.replace(parsedUnit[0], ""); - } - try { - return eval(`(${input})`); - } catch (err) { - return parsed; - } + if (result !== undefined) { + return result; } - return parsed; + const number = parseFloat(input); + return isNaN(number) ? undefined : number; }; // Helper to let user input: @@ -73,7 +78,7 @@ export const parseCssValue = ( // If we get a unit but there is no number - we assume its an accidental // unit match and its a keyword value. - if (isNaN(number) === true) { + if (number === undefined) { if (isValid(property, input)) { return { type: "keyword", diff --git a/packages/react-sdk/package.json b/packages/react-sdk/package.json index ceb0a5c6c5..08ec4b8c7f 100644 --- a/packages/react-sdk/package.json +++ b/packages/react-sdk/package.json @@ -1,6 +1,6 @@ { "name": "@webstudio-is/react-sdk", - "version": "0.7.1", + "version": "0.7.2", "description": "Webstudio JavaScript / TypeScript API", "author": "Webstudio ", "homepage": "https://webstudio.is", @@ -56,6 +56,7 @@ "zod": "^3.19.1" }, "dependencies": { + "detect-font": "^0.1.5", "@webstudio-is/asset-uploader": "^*", "@webstudio-is/fonts": "*", "@webstudio-is/icons": "*",