diff --git a/packages/studio-server/src/helpers/sourceMutation.nonContent.test.ts b/packages/studio-server/src/helpers/sourceMutation.nonContent.test.ts
new file mode 100644
index 0000000000..f734e47780
--- /dev/null
+++ b/packages/studio-server/src/helpers/sourceMutation.nonContent.test.ts
@@ -0,0 +1,72 @@
+import { describe, expect, it } from "vitest";
+import {
+ patchElementInHtml,
+ removeElementsFromHtml,
+ splitElementInHtml,
+ unwrapElementsFromHtml,
+ wrapElementsInHtml,
+} from "./sourceMutation.js";
+
+// A regression test: each of these removed, cut, grouped or dissolved the script, style or page shell it was aimed at.
+describe("a structural edit aimed at code or the page shell, not a layer", () => {
+ const PAGE = `
Film
+
+`;
+ const NOT_LAYERS = [
+ { id: "code" },
+ { selector: "#root > script" },
+ { id: "css" },
+ { id: "tpl" },
+ { id: "name" },
+ { selector: "head" },
+ { selector: "body" },
+ { selector: "html" },
+ ];
+ const BOX = { left: 0, top: 0, width: 10, height: 10 };
+
+ for (const target of NOT_LAYERS) {
+ const aim = JSON.stringify(target);
+ it(`delete leaves the file as it was for ${aim}`, () => {
+ expect(removeElementsFromHtml(PAGE, [target])).toBe(PAGE);
+ });
+ it(`cut is refused for ${aim}`, () => {
+ const result = splitElementInHtml(PAGE, target, 2, "half", { start: 0, duration: 4 });
+ expect(result).toMatchObject({ html: PAGE, matched: false, refusal: "target-not-found" });
+ });
+ it(`group is refused for ${aim}`, () => {
+ const result = wrapElementsInHtml(PAGE, [target], "g", BOX, []);
+ expect(result).toMatchObject({ html: PAGE, matched: false });
+ });
+ it(`ungroup is refused for ${aim}`, () => {
+ expect(unwrapElementsFromHtml(PAGE, target)).toEqual({ html: PAGE, unwrapped: false });
+ });
+ }
+
+ it("delete leaves a file with no page shell as it was when aimed at its body", () => {
+ const notAPage = `export const answer = 42;\n`;
+ expect(removeElementsFromHtml(notAPage, [{ selector: "body" }])).toBe(notAPage);
+ });
+
+ it("still saves a style set on the page's body", () => {
+ const result = patchElementInHtml(PAGE, { selector: "body" }, [
+ { type: "inline-style", property: "background", value: "black" },
+ ]);
+ expect(result.matched).toBe(true);
+ expect(result.html).toMatch(/]*background:\s*black/);
+ });
+
+ it("still deletes, cuts and groups an ordinary layer on the same page", () => {
+ expect(removeElementsFromHtml(PAGE, [{ id: "hero" }, { id: "code" }])).not.toContain("Hello");
+ expect(removeElementsFromHtml(PAGE, [{ id: "hero" }, { id: "code" }])).toContain(
+ "window.ready",
+ );
+ expect(splitElementInHtml(PAGE, { id: "hero" }, 2, "half").matched).toBe(true);
+ expect(wrapElementsInHtml(PAGE, [{ id: "hero" }, { id: "code" }], "g", BOX, []).matched).toBe(
+ true,
+ );
+ });
+});
diff --git a/packages/studio-server/src/helpers/sourceMutation.ts b/packages/studio-server/src/helpers/sourceMutation.ts
index 2e919a2a1b..50914ac1c0 100644
--- a/packages/studio-server/src/helpers/sourceMutation.ts
+++ b/packages/studio-server/src/helpers/sourceMutation.ts
@@ -200,7 +200,7 @@ export function removeElementsFromHtml(
let removed = false;
for (const target of targets) {
const element = findTargetElement(document, target);
- if (!element) continue;
+ if (!element || isNotALayerToRestructure(element)) continue;
const id = element.getAttribute("id");
removeElementWithGsapCascade(document, element);
if (id && !document.getElementById(id)) dropCssRulesForId(document, id);
@@ -305,6 +305,12 @@ function stampNewChildIds(parent: Element): void {
}
}
+const PAGE_SHELL_TAGS = new Set(["html", "head", "body", "title"]);
+
+function isNotALayerToRestructure(el: Element): boolean {
+ return isNonContentElement(el) || PAGE_SHELL_TAGS.has(el.tagName.toLowerCase());
+}
+
// fallow-ignore-next-line complexity
export function patchElementInHtml(
source: string,
@@ -497,7 +503,8 @@ export function splitElementInHtml(
): SplitElementResult {
const { document, wrappedFragment } = parseSourceDocument(source);
const el = findTargetElement(document, target);
- if (!el || !isHTMLElement(el)) return refused(source, "target-not-found");
+ if (!el || !isHTMLElement(el) || isNotALayerToRestructure(el))
+ return refused(source, "target-not-found");
// An untimed wrapper whose row is its timed media's own window is a view of that
// media: cut the media and leave the wrappers untimed. A GSAP scene keeps its own window.
@@ -710,7 +717,7 @@ export function wrapElementsInHtml(
const seen = new Set();
for (const target of targets) {
const el = findTargetElement(document, target);
- if (!el || !isHTMLElement(el) || seen.has(el)) continue;
+ if (!el || !isHTMLElement(el) || isNotALayerToRestructure(el) || seen.has(el)) continue;
seen.add(el);
els.push(el);
}
@@ -842,7 +849,8 @@ export function unwrapElementsFromHtml(
): UnwrapElementsResult {
const { document, wrappedFragment } = parseSourceDocument(source);
const group = findTargetElement(document, groupTarget);
- if (!group || !isHTMLElement(group)) return { html: source, unwrapped: false };
+ if (!group || !isHTMLElement(group) || isNotALayerToRestructure(group))
+ return { html: source, unwrapped: false };
// Shape guard mirroring the wrap-side contract: only ever dissolve an actual
// group wrapper. A stale/desynced selection that resolves to a plain
// would otherwise be unwrapped — rebasing its children by the parent's origin