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: fix bindings crash (#5817)

Oleg Isonen953ab1eb795a
diff --git a/apps/builder/app/builder/shared/binding-popover.test.ts b/apps/builder/app/builder/shared/binding-popover.test.ts
index e4c398491a..6ea68750f0 100644
--- a/apps/builder/app/builder/shared/binding-popover.test.ts
+++ b/apps/builder/app/builder/shared/binding-popover.test.ts
@@ -13,3 +13,8 @@ test("evaluateExpressionWithinScope works", () => {
})
).toEqual(2);
});
+
+test("evaluateExpressionWithinScope treats empty expression as undefined", () => {
+ expect(evaluateExpressionWithinScope("", {})).toBeUndefined();
+ expect(evaluateExpressionWithinScope(" ", {})).toBeUndefined();
+});
diff --git a/apps/builder/app/builder/shared/binding-popover.tsx b/apps/builder/app/builder/shared/binding-popover.tsx
index c3cb1d3db4..53f0e37f78 100644
--- a/apps/builder/app/builder/shared/binding-popover.tsx
+++ b/apps/builder/app/builder/shared/binding-popover.tsx
@@ -46,6 +46,7 @@ import {
formatValuePreview,
type EditorApi,
} from "./expression-editor";
+import { normalizeEditorValue } from "~/shared/code-editor-base";
/**
* Check if a value is a primitive that can be safely stringified.
@@ -78,6 +79,10 @@ export const evaluateExpressionWithinScope = (
expression: string,
scope: Record<string, unknown>
) => {
+ if (expression.trim() === "") {
+ return;
+ }
+
const variables = new Map<string, unknown>();
for (const [name, value] of Object.entries(scope)) {
const decodedName = decodeDataSourceVariable(name);
@@ -100,15 +105,16 @@ const BindingPanel = ({
scope: Record<string, unknown>;
aliases: Map<string, string>;
valueError?: string;
- value: string;
+ value?: string;
onChange: () => void;
onSave: (value: string, invalid: boolean) => void;
}) => {
const editorApiRef = useRef<undefined | EditorApi>(undefined);
- const [expression, setExpression] = useState(value);
+ const normalizedValue = normalizeEditorValue(value);
+ const [expression, setExpression] = useState(normalizedValue);
const usedIdentifiers = useMemo(
- () => getExpressionIdentifiers(value),
- [value]
+ () => getExpressionIdentifiers(normalizedValue),
+ [normalizedValue]
);
const [errorsCount, setErrorsCount] = useState<number>(0);
const [touched, setTouched] = useState(false);
@@ -372,7 +378,7 @@ export const BindingPopover = ({
aliases: Map<string, string>;
variant: BindingVariant;
validate?: (value: unknown) => undefined | string;
- value: string;
+ value?: string;
onChange: (newValue: string) => void;
onRemove: (evaluatedValue: unknown) => void;
}) => {
@@ -385,7 +391,10 @@ export const BindingPopover = ({
return;
}
- const valueError = validate?.(evaluateExpressionWithinScope(value, scope));
+ const normalizedValue = normalizeEditorValue(value);
+ const valueError = validate?.(
+ evaluateExpressionWithinScope(normalizedValue, scope)
+ );
return (
<FloatingPanel
placement="left-start"
@@ -419,7 +428,7 @@ export const BindingPopover = ({
event.preventDefault();
// inline variables and close dialog
const evaluatedValue = evaluateExpressionWithinScope(
- value,
+ normalizedValue,
scope
);
@@ -443,7 +452,7 @@ export const BindingPopover = ({
scope={scope}
aliases={aliases}
valueError={valueError}
- value={value}
+ value={normalizedValue}
onChange={() => {
hasUnsavedChange.current = true;
}}
@@ -467,7 +476,11 @@ export const BindingPopover = ({
/>
}
>
- <BindingButton variant={variant} error={valueError} value={value} />
+ <BindingButton
+ variant={variant}
+ error={valueError}
+ value={normalizedValue}
+ />
</FloatingPanel>
);
};
diff --git a/apps/builder/app/builder/shared/expression-editor.tsx b/apps/builder/app/builder/shared/expression-editor.tsx
index 40c7799faa..f605363355 100644
--- a/apps/builder/app/builder/shared/expression-editor.tsx
+++ b/apps/builder/app/builder/shared/expression-editor.tsx
@@ -40,6 +40,7 @@ import {
EditorDialogButton,
EditorDialogControl,
type EditorApi,
+ normalizeEditorValue,
} from "~/shared/code-editor-base";
import {
decodeDataVariableName,
@@ -577,10 +578,11 @@ export const ExpressionEditor = ({
color?: "error";
autoFocus?: boolean;
readOnly?: boolean;
- value: string;
+ value?: string;
onChange: (value: string) => void;
onChangeComplete: (value: string) => void;
}) => {
+ const normalizedValue = normalizeEditorValue(value);
const { nameById, idByName } = useMemo(() => {
const nameById = new Map();
const idByName = new Map();
@@ -595,10 +597,10 @@ export const ExpressionEditor = ({
}, [aliases]);
const expressionWithUnsetVariables = useMemo(() => {
return unsetExpressionVariables({
- expression: value,
+ expression: normalizedValue,
unsetNameById: nameById,
});
- }, [value, nameById]);
+ }, [normalizedValue, nameById]);
const scopeWithUnsetVariables = useMemo(() => {
const newScope: typeof scope = {};
for (const [identifier, value] of Object.entries(scope)) {
diff --git a/apps/builder/app/shared/code-editor-base.test.ts b/apps/builder/app/shared/code-editor-base.test.ts
index 3d1ca7e2be..321b81b6dd 100644
--- a/apps/builder/app/shared/code-editor-base.test.ts
+++ b/apps/builder/app/shared/code-editor-base.test.ts
@@ -1,6 +1,6 @@
import { EditorSelection } from "@codemirror/state";
import { expect, test } from "vitest";
-import { clampEditorSelection } from "./code-editor-base";
+import { clampEditorSelection, normalizeEditorValue } from "./code-editor-base";
test("clampEditorSelection preserves selection within document length", () => {
const selection = EditorSelection.create([
@@ -35,3 +35,8 @@ test("clampEditorSelection clamps selection to document length", () => {
]);
expect(clampedSelection.mainIndex).toBe(selection.mainIndex);
});
+
+test("normalizeEditorValue defaults undefined to empty string", () => {
+ expect(normalizeEditorValue(undefined)).toBe("");
+ expect(normalizeEditorValue("value")).toBe("value");
+});
diff --git a/apps/builder/app/shared/code-editor-base.tsx b/apps/builder/app/shared/code-editor-base.tsx
index d435d09636..561a568a4f 100644
--- a/apps/builder/app/shared/code-editor-base.tsx
+++ b/apps/builder/app/shared/code-editor-base.tsx
@@ -71,6 +71,8 @@ export const clampEditorSelection = (
);
};
+export const normalizeEditorValue = (value: undefined | string) => value ?? "";
+
export const getCodeEditorCssVars = ({
minHeight,
maxHeight,
@@ -239,7 +241,7 @@ type EditorContentProps = {
autoFocus?: boolean;
invalid?: boolean;
showShortcuts?: boolean;
- value: string;
+ value?: string;
onChange: (value: string) => void;
onChangeComplete: (value: string) => void;
};
@@ -385,15 +387,16 @@ export const EditorContent = ({
if (view === undefined) {
return;
}
+ const nextValue = normalizeEditorValue(value);
// prevent updating when editor has the same state
// and can be the source of new value
- if (value === view.state.doc.toString()) {
+ if (nextValue === view.state.doc.toString()) {
return;
}
view.dispatch({
- changes: { from: 0, to: view.state.doc.length, insert: value },
- selection: clampEditorSelection(view.state.selection, value.length),
+ changes: { from: 0, to: view.state.doc.length, insert: nextValue },
+ selection: clampEditorSelection(view.state.selection, nextValue.length),
annotations: [ExternalChange.of(true)],
});
}, [value]);