// fallow-ignore-file code-duplication
import { parseHTML } from "linkedom";
import { ensureHfIds } from "@hyperframes/parsers/hf-ids";
import { describe, expect, it } from "vitest";
import {
removeElementFromHtml,
removeElementsFromHtml,
patchElementInHtml,
probeElementInSource,
probeElementsInSource,
dedupeClonedCompositionId,
} from "./sourceMutation.js";
describe("removeElementsFromHtml", () => {
const html = `
{
const { html: result, matched } = patchElementInHtml(FIXTURE, { id: "hero" }, [
{ type: "inline-style", property: "clip-path", value: "inset(10px 20px 30px 40px)" },
]);
expect(matched).toBe(true);
expect(result).toMatch(/clip-path:\s*inset\(10px 20px 30px 40px\)/);
expect(result).toContain('id="hero"');
});
it("patches inline style by class selector", () => {
const { html: result } = patchElementInHtml(FIXTURE, { selector: ".hero-heading" }, [
{ type: "inline-style", property: "font-size", value: "72px" },
]);
expect(result).toMatch(/font-size:\s*72px/);
});
it("patches data attribute", () => {
const { html: result } = patchElementInHtml(FIXTURE, { id: "hero" }, [
{ type: "attribute", property: "hf-studio-path-offset", value: "true" },
]);
expect(result).toContain('data-hf-studio-path-offset="true"');
});
it("does not double data- prefix when property already has it", () => {
const { html: result } = patchElementInHtml(FIXTURE, { id: "hero" }, [
{ type: "attribute", property: "data-hf-studio-path-offset", value: "true" },
]);
expect(result).toContain('data-hf-studio-path-offset="true"');
expect(result).not.toContain("data-data-hf-studio-path-offset");
});
it("does not double data- prefix for any studio attribute", () => {
const attrs = [
"data-hf-studio-path-offset",
"data-hf-studio-original-translate",
"data-hf-studio-original-inline-translate",
"data-hf-studio-box-size",
"data-hf-studio-rotation",
];
for (const attr of attrs) {
const { html: result } = patchElementInHtml(FIXTURE, { id: "hero" }, [
{ type: "attribute", property: attr, value: "true" },
]);
expect(result).toContain(`${attr}="true"`);
expect(result).not.toContain(`data-${attr}`);
}
});
it("removes attribute with data- prefix already present", () => {
const { html: withAttr } = patchElementInHtml(FIXTURE, { id: "hero" }, [
{ type: "attribute", property: "data-hf-studio-path-offset", value: "true" },
]);
expect(withAttr).toContain('data-hf-studio-path-offset="true"');
const { html: removed } = patchElementInHtml(withAttr, { id: "hero" }, [
{ type: "attribute", property: "data-hf-studio-path-offset", value: null },
]);
expect(removed).not.toContain("hf-studio-path-offset");
});
it("patches html attribute", () => {
const { html: result } = patchElementInHtml(FIXTURE, { id: "hero" }, [
{ type: "html-attribute", property: "title", value: "greeting" },
]);
expect(result).toContain('title="greeting"');
});
it("patches text content", () => {
const { html: result } = patchElementInHtml(FIXTURE, { id: "hero" }, [
{ type: "text-content", property: "", value: "New Title" },
]);
expect(result).toContain("New Title");
expect(result).not.toContain("Hello World");
});
it("applies child-scoped inline style without changing the parent style", () => {
const source = `
AB
`;
const { html: result, matched } = patchElementInHtml(source, { hfId: "parent" }, [
{
type: "inline-style",
property: "color",
value: "blue",
childSelector: ":scope > span",
childIndex: 1,
},
]);
expect(matched).toBe(true);
const { document } = parseHTML(result);
const parent = document.querySelector('[data-hf-id="parent"]');
const children = Array.from(document.querySelectorAll(".line"));
expect(parent?.getAttribute("style")).toContain("color: red");
expect(children[0]?.getAttribute("style")).toBeNull();
expect(children[1]?.getAttribute("style")).toContain("color: blue");
});
it("applies child-scoped text content to the child only", () => {
const source = `
AB
`;
const { html: result, matched } = patchElementInHtml(source, { hfId: "parent" }, [
{
type: "text-content",
property: "text",
value: "B < C & D",
childSelector: ":scope > span",
childIndex: 1,
},
]);
expect(matched).toBe(true);
const { document } = parseHTML(result);
const children = Array.from(document.querySelectorAll(".line"));
expect(children[0]?.textContent).toBe("A");
expect(children[1]?.textContent).toBe("B < C & D");
});
it("rejects the whole batch when a child-scoped operation cannot resolve", () => {
const source = `
AB
`;
const result = patchElementInHtml(source, { hfId: "parent" }, [
{
type: "inline-style",
property: "color",
value: "blue",
childSelector: ":scope > span",
childIndex: 0,
},
{
type: "text-content",
property: "text",
value: "missing",
childSelector: ":scope > strong",
childIndex: 0,
},
]);
expect(result.matched).toBe(false);
expect(result.html).toBe(source);
});
it("applies multiple operations in one call", () => {
const { html: result } = patchElementInHtml(FIXTURE, { id: "hero" }, [
{ type: "inline-style", property: "color", value: "blue" },
{ type: "inline-style", property: "font-size", value: "96px" },
{ type: "attribute", property: "hf-studio-path-offset", value: "true" },
]);
expect(result).toMatch(/color:\s*blue/);
expect(result).toMatch(/font-size:\s*96px/);
expect(result).toContain('data-hf-studio-path-offset="true"');
});
it("finds element by composition-id selector", () => {
const { html: result } = patchElementInHtml(
FIXTURE,
{ selector: '[data-composition-id="overlay"]' },
[{ type: "inline-style", property: "opacity", value: "0.5" }],
);
expect(result).toMatch(/opacity:\s*0\.5/);
});
it("finds element by class with selectorIndex", () => {
const html = `
A
B
`;
const { html: result } = patchElementInHtml(html, { selector: ".item", selectorIndex: 1 }, [
{ type: "text-content", property: "", value: "Changed" },
]);
expect(result).toContain("A");
expect(result).toContain("Changed");
expect(result).not.toContain(">B<");
});
// fails without the fix: each of these wrote the given text into the script, style or template.
describe("a target that is code or page plumbing, not a layer", () => {
const PAGE = `
`;
const TEXT = "window.changed = 1";
for (const type of ["text-content", "rich-text"] as const)
for (const target of [
{ selector: "script#code" },
{ id: "code" },
{ selector: "#root > script" },
{ selector: "style" },
{ id: "css" },
{ id: "tpl" },
])
it(`declines ${type} aimed at ${JSON.stringify(target)}`, () => {
const result = patchElementInHtml(PAGE, target, [{ type, property: "", value: TEXT }]);
expect(result.matched).toBe(false);
expect(result.html).toBe(PAGE);
});
it("declines every other operation on such a target too", () => {
for (const op of [
{ type: "inline-style", property: "color", value: "red" },
{ type: "html-attribute", property: "src", value: "https://example.com/a.js" },
{ type: "attribute", property: "data-start", value: "1" },
{ type: "ensure-id", property: "", value: "x" },
] as const) {
const result = patchElementInHtml(PAGE, { selector: "#root > script" }, [op]);
expect(result.matched, op.type).toBe(false);
expect(result.html, op.type).toBe(PAGE);
}
});
it("declines an operation whose child selector lands on a script", () => {
const result = patchElementInHtml(PAGE, { id: "wrap" }, [
{ type: "text-content", property: "", value: TEXT, childSelector: "script" },
]);
expect(result.matched).toBe(false);
expect(result.html).toBe(PAGE);
});
it("writes a layer's text as text when its only child is a script", () => {
const result = patchElementInHtml(PAGE, { id: "wrap" }, [
{ type: "text-content", property: "", value: TEXT },
]);
expect(result.matched).toBe(true);
const { document } = parseHTML(result.html);
expect(document.getElementById("inner")).toBeNull();
expect(document.getElementById("wrap")?.textContent).toBe(TEXT);
expect(document.querySelectorAll("script").length).toBe(1);
});
it("still saves a text edit of an ordinary layer on the same page", () => {
const result = patchElementInHtml(PAGE, { id: "hero" }, [
{ type: "text-content", property: "", value: "New" },
]);
expect(result.matched).toBe(true);
expect(result.html).toContain(">New
");
expect(result.html).toContain("window.ready = true;");
});
it("still patches a layer inside a template", () => {
const result = patchElementInHtml(PAGE, { selector: "template p" }, [
{ type: "text-content", property: "", value: "edited" },
]);
expect(result.matched).toBe(true);
expect(result.html).toContain(" {
const { html: result, matched } = patchElementInHtml(FIXTURE, { id: "nonexistent" }, [
{ type: "inline-style", property: "color", value: "red" },
]);
expect(matched).toBe(false);
expect(result).toBe(FIXTURE);
});
it("removes inline style when value is null", () => {
const { html: result } = patchElementInHtml(FIXTURE, { id: "hero" }, [
{ type: "inline-style", property: "font-size", value: null },
]);
expect(result).not.toContain("font-size");
});
// fails without the fix: either op wrote a composition source, which the runtime fetches and runs.
it("never writes a composition source, by either attribute op", () => {
for (const op of [
{
type: "html-attribute",
property: "data-composition-src",
value: "https://elsewhere.example/a.html",
},
{ type: "attribute", property: "composition-src", value: "https://elsewhere.example/a.html" },
{ type: "attribute", property: "data-composition-src", value: "scenes/b.html" },
] as const) {
const { html: result } = patchElementInHtml(FIXTURE, { id: "hero" }, [op]);
expect(result.match(/data-composition-src/g)?.length ?? 0).toBe(
FIXTURE.match(/data-composition-src/g)?.length ?? 0,
);
}
});
it("removes attribute when value is null", () => {
const { html: result } = patchElementInHtml(
FIXTURE,
{ selector: '[data-composition-id="overlay"]' },
[{ type: "html-attribute", property: "data-composition-src", value: null }],
);
expect(result).not.toContain("data-composition-src");
});
it("patches fragment html without doctype", () => {
const fragment = `