mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 08:32:45 +00:00
fix(studio-server): delete, cut, group and ungroup decline a script, style or page-shell target (#8801)
* fix(studio-server): delete, cut, group and ungroup decline a script, style or page-shell target An element patch already declines a target that is code or page plumbing. The structural writers removed, cut, grouped or dissolved whatever the target resolved to, so one aimed at a script, a style sheet, head or body changed the file. They now share the patch writer's rule, which also covers html, head, body and title, and leave the file as it was. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> * fix(studio-server): an element patch may still style the page shell The first commit widened the shared patch rule to html, head, body and title. A style or attribute set on body is an ordinary edit, so the wider set now applies only to delete, cut, group and ungroup. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
6e7c35f3a9
commit
2a03de9f1f
@@ -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 = `<!doctype html><html><head><title id="name">Film</title><style id="css">.a { color: red; }</style></head><body data-hf-group="page">
|
||||
<div id="root" data-composition-id="main">
|
||||
<div id="hero" data-start="0" data-duration="4">Hello</div>
|
||||
<template id="tpl"><p>kept</p></template>
|
||||
<script id="code" data-start="0" data-duration="4">window.ready = true;</script>
|
||||
</div>
|
||||
</body></html>`;
|
||||
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(/<body[^>]*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,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -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<Element>();
|
||||
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 <div>
|
||||
// would otherwise be unwrapped — rebasing its children by the parent's origin
|
||||
|
||||
Reference in New Issue
Block a user