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: prevent editing body (#2833)

Bogdan Chadkind8922a0dd40c
diff --git a/apps/builder/app/shared/instance-utils.test.ts b/apps/builder/app/shared/instance-utils.test.ts
index 5f6cb0b302..0250d09ec8 100644
--- a/apps/builder/app/shared/instance-utils.test.ts
+++ b/apps/builder/app/shared/instance-utils.test.ts
@@ -166,15 +166,13 @@ const emptyInsertConstraints: InsertConstraints = {
describe("find closest editable instance selector", () => {
test("searches closest container", () => {
- const instances: Instances = new Map([
- createInstancePair("body", "Body", [{ type: "id", value: "box" }]),
- createInstancePair("box", "Box", [
+ const instances: Instances = toMap([
+ createInstance("body", "Body", [{ type: "id", value: "box" }]),
+ createInstance("box", "Box", [
{ type: "text", value: "some text" },
{ type: "id", value: "bold" },
]),
- createInstancePair("bold", "Bold", [
- { type: "text", value: "some-bold" },
- ]),
+ createInstance("bold", "Bold", [{ type: "text", value: "some-bold" }]),
]);
expect(
findClosestEditableInstanceSelector(
@@ -193,17 +191,15 @@ describe("find closest editable instance selector", () => {
});
test("skips when container has anything except rich-text-child or text", () => {
- const instances: Instances = new Map([
- createInstancePair("body", "Body", [{ type: "id", value: "box" }]),
- createInstancePair("box", "Box", [
+ const instances: Instances = toMap([
+ createInstance("body", "Body", [{ type: "id", value: "box" }]),
+ createInstance("box", "Box", [
{ type: "text", value: "some text" },
{ type: "id", value: "bold" },
{ type: "id", value: "child-box" },
]),
- createInstancePair("bold", "Bold", [
- { type: "text", value: "some-bold" },
- ]),
- createInstancePair("child-box", "Box", [
+ createInstance("bold", "Bold", [{ type: "text", value: "some-bold" }]),
+ createInstance("child-box", "Box", [
{ type: "text", value: "child-box" },
]),
]);
@@ -217,16 +213,28 @@ describe("find closest editable instance selector", () => {
});
test("considers empty container as editable", () => {
- const instances: Instances = new Map([
- createInstancePair("body", "Body", []),
+ const instances: Instances = toMap([
+ createInstance("body", "Body", [{ type: "id", value: "body" }]),
+ createInstance("box", "Box", []),
]);
+ expect(
+ findClosestEditableInstanceSelector(
+ ["box", "body"],
+ instances,
+ createFakeComponentMetas({})
+ )
+ ).toEqual(["box", "body"]);
+ });
+
+ test("prevent editing Body instance", () => {
+ const instances: Instances = toMap([createInstance("body", "Body", [])]);
expect(
findClosestEditableInstanceSelector(
["body"],
instances,
createFakeComponentMetas({})
)
- ).toEqual(["body"]);
+ ).toEqual(undefined);
});
});
diff --git a/apps/builder/app/shared/instance-utils.ts b/apps/builder/app/shared/instance-utils.ts
index 0845c1a225..797b001307 100644
--- a/apps/builder/app/shared/instance-utils.ts
+++ b/apps/builder/app/shared/instance-utils.ts
@@ -137,6 +137,12 @@ export const findClosestEditableInstanceSelector = (
if (instance === undefined) {
return;
}
+ // when start editing empty body all text content
+ // including style and scripts appear in editor
+ // assume body is root and stop checking further
+ if (instance.component === "Body") {
+ return;
+ }
const meta = metas.get(instance.component);
if (meta === undefined) {
return;