Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions packages/core/src/runtime/compositionLoader.fileMark.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { loadCompositions } from "./compositionLoader";

const load = () =>
loadCompositions({
injectedStyles: [],
injectedScripts: [],
injectedLinks: [],
parseDimensionPx: (value) => (value ? `${value}px` : null),
});

const host = (src: string, compositionId: string) => {
const el = document.createElement("div");
el.setAttribute("data-composition-src", src);
el.setAttribute("data-composition-id", compositionId);
document.body.appendChild(el);
return el;
};

beforeAll(() => {
if (typeof globalThis.CSS === "undefined") (globalThis as { CSS?: object }).CSS = {};
if (typeof CSS.escape !== "function")
CSS.escape = (value: string) => value.replace(/([^\w-])/g, "\\$1");
});

// fails without the fix: a host kept only its authored data-composition-src, which then chose where edits were written
describe("the file a mounted composition host is stamped with", () => {
beforeEach(() => {
const base = document.createElement("base");
base.href = "https://example.com/";
document.head.prepend(base);
});
afterEach(() => {
document.head.querySelectorAll("base").forEach((base) => base.remove());
document.body.innerHTML = "";
vi.restoreAllMocks();
});

it("stamps a host the runtime mounted from a fetched file with that file", async () => {
const mounted = host("compositions/scene.html", "scene-1");
vi.spyOn(globalThis, "fetch").mockResolvedValue(
new Response(`<html><body><div data-composition-id="scene-1"><p>x</p></div></body></html>`),
);
await load();
expect(mounted.querySelector("p")).toBeTruthy();
expect(mounted.getAttribute("data-composition-file")).toBe("compositions/scene.html");
});

it("stamps no file on a host whose content came from a template in this page", async () => {
document.body.innerHTML = `<template id="scene-1-template"><div data-composition-id="scene-1"><p>x</p></div></template>`;
const mounted = host("index.html", "scene-1");
const fetch = vi.spyOn(globalThis, "fetch");
await load();
expect(fetch).not.toHaveBeenCalled();
expect(mounted.querySelector("p")).toBeTruthy();
expect(mounted.hasAttribute("data-composition-file")).toBe(false);
});

it("stamps no file on a host whose file failed to load", async () => {
const mounted = host("compositions/missing.html", "scene-1");
vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response("", { status: 404 }));
await load();
expect(mounted.hasAttribute("data-composition-file")).toBe(false);
});
});
1 change: 1 addition & 0 deletions packages/core/src/runtime/compositionLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,7 @@ async function mountExternalCompositions(
throw new Error(`HTTP ${response.status}`);
}
const html = await response.text();
host.setAttribute("data-composition-file", src);
const parser = new DOMParser();
const doc = parser.parseFromString(html, "text/html");
dropAuthoredSvgReferenceAliases(doc);
Expand Down
40 changes: 40 additions & 0 deletions packages/core/src/runtime/editingElement.source.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { afterEach, describe, expect, it } from "vitest";
import { readElementSource } from "./editingElement";

const mount = (html: string) => {
document.body.innerHTML = html;
return document.getElementById("target")!;
};

// fails without the fix: an authored data-composition-src chose the file an element's edits are written to
describe("readElementSource", () => {
afterEach(() => {
document.body.innerHTML = "";
});

it("keeps the file its host was stamped with under an empty authored data-composition-src", () => {
const el = mount(
`<div data-composition-file="compositions/scene.html"><div data-composition-src=""><p id="target">x</p></div></div>`,
);
expect(readElementSource(el).file).toBe("compositions/scene.html");
});

it("keeps the stamped file under an authored data-composition-src naming another project file", () => {
const el = mount(
`<div data-composition-file="compositions/scene.html"><div data-composition-src="index.html"><p id="target">x</p></div></div>`,
);
expect(readElementSource(el).file).toBe("compositions/scene.html");
});

it("names no file for an element under only authored data-composition-src markup", () => {
const el = mount(`<div data-composition-src="index.html"><p id="target">x</p></div>`);
expect(readElementSource(el).file).toBeNull();
});

it("reads the stamped file of the nearest mounted host", () => {
const el = mount(
`<div data-composition-file="index.html"><div data-composition-file="compositions/a.html"><p id="target">x</p></div></div>`,
);
expect(readElementSource(el).file).toBe("compositions/a.html");
});
});
6 changes: 3 additions & 3 deletions packages/core/src/runtime/editingElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,12 @@ function closestWithAny(el: Element, names: readonly string[]): Element | null {
}

export function readElementSource(el: Element): EditingElementSource {
const [fileAttr, srcAttr] = SOURCE_HOST_ATTRS;
const [fileAttr] = SOURCE_HOST_ATTRS;
const [idAttr, originalIdAttr] = COMPOSITION_ROOT_ATTRS;
const host = closestWithAny(el, SOURCE_HOST_ATTRS);
const host = closestWithAny(el, [fileAttr]);
const root = closestWithAny(el, [idAttr]);
return {
file: host?.getAttribute(fileAttr) ?? host?.getAttribute(srcAttr) ?? null,
file: host?.getAttribute(fileAttr) || null,
compositionId: root?.getAttribute(idAttr) ?? null,
originalCompositionId: root?.getAttribute(originalIdAttr) ?? null,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/runtime/editingHandles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ describe("element handles over the editing port", () => {
document.body.innerHTML = `
<div class="tile"></div>
<div data-composition-file="cards.html"><div class="tile"></div><div class="tile"></div></div>
<div data-composition-src="cards.html"><div class="tile" id="t"></div></div>
<div data-composition-src="cards.html" data-composition-file="cards.html"><div class="tile" id="t"></div></div>
<div data-composition-file="cards.html"><div class="tile"></div></div>`;
const last = await describeOne(document.querySelectorAll(".tile")[4]!);
expect(last.source.file).toBe("cards.html");
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/runtime/scrubAudio.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("createScrubAudio", () => {

it("plays the music row's own track when a sub-composition repeats its id", () => {
const [inner, root] = tracks(
'<div data-composition-id="strip" data-composition-src="compositions/strip.html"><audio id="music"></audio></div>' +
'<div data-composition-id="strip" data-composition-src="compositions/strip.html" data-composition-file="compositions/strip.html"><audio id="music"></audio></div>' +
'<audio id="music" class="root"></audio>',
);

Expand Down
19 changes: 19 additions & 0 deletions packages/core/src/studioPreviewMark.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,27 @@ import {
dropServiceWorkers,
refusedPreviewMessage,
refusedPreviewStatus,
previewElementSourceFile,
} from "./studioPreviewMark";

// fails without the fix: an authored data-composition-src chose the file a timeline edit was written to
describe("previewElementSourceFile", () => {
it("keeps the stamped file under an authored host that names another file", () => {
document.body.innerHTML = `
<div data-composition-id="scene" data-composition-file="compositions/scene.html">
<div data-composition-id="wrapper" data-composition-src="index.html"><p id="t"></p></div>
</div>`;
expect(previewElementSourceFile(document.getElementById("t")!)).toBe("compositions/scene.html");
document.body.innerHTML = "";
});

it("names no file for an element under only authored markup", () => {
document.body.innerHTML = `<div data-composition-id="wrapper" data-composition-src=""><p id="t"></p></div>`;
expect(previewElementSourceFile(document.getElementById("t")!)).toBeUndefined();
document.body.innerHTML = "";
});
});

describe("refusedPreviewStatus", () => {
it.each([404, 410, 500])("reads %i back out of the refused page's message", (status) => {
expect(refusedPreviewStatus(refusedPreviewMessage(status))).toBe(status);
Expand Down
8 changes: 2 additions & 6 deletions packages/core/src/studioPreviewMark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,8 @@ export interface PreviewTarget {
}

export function previewElementSourceFile(el: Element): string | undefined {
const ownerRoot = el.parentElement?.closest("[data-composition-id]");
return (
ownerRoot?.getAttribute("data-composition-file") ??
ownerRoot?.getAttribute("data-composition-src") ??
undefined
);
const host = el.parentElement?.closest("[data-composition-file]");
return host?.getAttribute("data-composition-file") || undefined;
}

export function previewElementFinder(
Expand Down
30 changes: 30 additions & 0 deletions packages/studio-server/src/helpers/manualEditsRenderScript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,36 @@ describe("createStudioManualEditsRenderBodyScript", () => {
expect(nestedCard.style.getPropertyValue("rotate")).toContain("--hf-studio-rotation");
});

// fails without the fix: an authored data-composition-src made a saved edit to index.html apply to its own copy
it("applies an edit to index.html to no element under an authored data-composition-src naming it", () => {
const window = new Window();
window.document.body.innerHTML = `
<div data-composition-id="root">
<div data-composition-id="scene" data-composition-file="scenes/a.html">
<div data-composition-id="wrapper" data-composition-src="index.html"><div id="card"></div></div>
</div>
<div id="card"></div>
</div>
`;
const [authoredCard, ownCard] = Array.from(window.document.getElementsByTagName("*")).filter(
(element): element is HTMLElement =>
element instanceof window.HTMLElement && element.id === "card",
);
if (!authoredCard || !ownCard) throw new Error("render fixture missing");
const script = createStudioManualEditsRenderBodyScript(
JSON.stringify({
version: 1,
edits: [{ kind: "rotation", target: { sourceFile: "index.html", id: "card" }, angle: 21 }],
}),
);
if (!script) throw new Error("script fixture missing");

runScript(window, script);

expect(authoredCard.style.getPropertyValue("rotate")).toBe("");
expect(ownCard.style.getPropertyValue("rotate")).toContain("--hf-studio-rotation");
});

it("applies render edits inside composition-file hosts without composition ids", () => {
const window = new Window();
window.document.body.innerHTML = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,7 @@ function studioManualEditsRenderRuntime(
const sourceFileForElement = (element: HTMLElement): string => {
let current: HTMLElement | null = element;
while (current) {
const sourceFile =
current.getAttribute("data-composition-file") ??
current.getAttribute("data-composition-src");
const sourceFile = current.getAttribute("data-composition-file");
if (sourceFile) return sourceFile;
current = current.parentElement;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ function studioMotionRenderRuntime(
const sourceFileForElement = (element: HTMLElement): string => {
let current: HTMLElement | null = element;
while (current) {
const sourceFile =
current.getAttribute("data-composition-file") ??
current.getAttribute("data-composition-src");
const sourceFile = current.getAttribute("data-composition-file");
if (sourceFile) return sourceFile;
current = current.parentElement;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/studio/src/components/editor/domEditing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,7 @@ describe("resolveDomEditSelection", () => {
<div
id="slide-1"
data-composition-id="slide-core-conviction"
data-composition-src="compositions/slide-01-core-conviction.html"
data-composition-src="compositions/slide-01-core-conviction.html" data-composition-file="compositions/slide-01-core-conviction.html"
>
<h1>Core Conviction</h1>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ describe("liveLayerElement", () => {
it("finds a replaced layer again in its own file when a sub-composition repeats its id", () => {
document.body.innerHTML =
'<div data-composition-id="main">' +
'<div data-composition-id="strip" data-composition-src="compositions/strip.html">' +
'<div data-composition-id="strip" data-composition-src="compositions/strip.html" data-composition-file="compositions/strip.html">' +
'<div id="card-1">strip</div></div><div id="card-1">root</div></div>';
const stale = document.createElement("div");
const layer = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ it("previews on the selected element, not an earlier same-id copy in a sub-compo

it("previews on the copy in the selection's own file when a sub-composition repeats its hf-id", () => {
document.body.innerHTML =
'<div data-composition-id="strip" data-composition-src="compositions/strip.html">' +
'<div data-composition-id="strip" data-composition-src="compositions/strip.html" data-composition-file="compositions/strip.html">' +
'<div id="card" data-hf-id="hf-card"></div></div><div id="card" data-hf-id="hf-card" class="root"></div>';
const set = vi.fn();
const iframe = { contentWindow: { gsap: { set } }, contentDocument: document };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class FakeDOMMatrix {

const FIXTURE = `
<div data-composition-id="main" data-width="1920" data-height="1080">
<div id="host" data-composition-src="a.html" data-width="960" data-height="540">
<div id="host" data-composition-src="a.html" data-composition-file="a.html" data-width="960" data-height="540">
<p id="inner"></p><span id="speck"></span>
</div>
<div id="turned" style="transform: matrix(0.8, 0.6, -0.6, 0.8, 0, 0)"></div>
Expand Down
10 changes: 2 additions & 8 deletions packages/studio/src/components/editor/studioMotion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export {
} from "./studioMotionOps";

import { isHtmlElement } from "@hyperframes/core/runtime/dom-realm";
import { readElementSource } from "@hyperframes/core/runtime/editing-port";
import { readStudioMotionFromElement as readMotionAttr } from "./studioMotionOps";
import {
STUDIO_MOTION_ATTR,
Expand All @@ -53,14 +54,7 @@ import {
// ── DOM Helpers ──

function sourceFileForElement(element: HTMLElement, activeCompositionPath: string | null): string {
let current: HTMLElement | null = element;
while (current) {
const sourceFile =
current.getAttribute("data-composition-file") ?? current.getAttribute("data-composition-src");
if (sourceFile) return sourceFile;
current = current.parentElement;
}
return activeCompositionPath ?? "index.html";
return readElementSource(element).file ?? activeCompositionPath ?? "index.html";
}

function elementMatchesSourceFile(
Expand Down
2 changes: 1 addition & 1 deletion packages/studio/src/hooks/previewTimingPatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function preview(): HTMLIFrameElement {
doc.write(`<!doctype html><html><body>
<div id="root" data-composition-id="main" data-start="0" data-duration="8">
<div id="a" class="clip" data-start="0" data-duration="4"></div>
<div id="host" data-composition-id="intro" data-composition-src="intro.html"></div>
<div id="host" data-composition-id="intro" data-composition-src="intro.html" data-composition-file="intro.html"></div>
</div></body></html>`);
doc.close();
return iframe;
Expand Down
11 changes: 11 additions & 0 deletions packages/studio/src/hooks/timelineAudioGroupVolume.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@ describe("resolveGroupSourceFile", () => {
expect(resolveGroupSourceFile(doc.getElementById("root-group"))).toBeUndefined();
});

// fails without the fix: an authored data-composition-src named the file a group's volume was written to
it("ignores a data-composition-src the page's markup kept, which no mount stamped", () => {
const doc = document.implementation.createHTMLDocument("preview");
doc.body.setAttribute("data-composition-id", "root");
doc.body.innerHTML = `
<div data-composition-id="wrapper" data-composition-src="index.html">
<hf-audio-group id="authored"></hf-audio-group>
</div>`;
expect(resolveGroupSourceFile(doc.getElementById("authored"))).toBeUndefined();
});

it("is safe on a detached or missing element", () => {
expect(resolveGroupSourceFile(null)).toBeUndefined();
expect(resolveGroupSourceFile(document.createElement("hf-audio-group"))).toBeUndefined();
Expand Down
3 changes: 1 addition & 2 deletions packages/studio/src/hooks/timelineAudioGroupVolume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,7 @@ export function resolveGroupSourceFile(groupEl: Element | null): string | undefi
while (node) {
const owner: Element | null = node.closest("[data-composition-id]");
if (!owner) return undefined;
const file =
owner.getAttribute("data-composition-file") ?? owner.getAttribute("data-composition-src");
const file = owner.getAttribute("data-composition-file");
if (file) return file;
node = owner.parentElement;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/studio/src/hooks/timelineEditingHelpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe("patchIframeDomTiming", () => {
<div
id="scene-host"
data-composition-id="scene"
data-composition-src="compositions/scene.html"
data-composition-src="compositions/scene.html" data-composition-file="compositions/scene.html"
data-start="2"
></div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ describe("shadow reload store ownership and readiness", () => {
it("keeps a section the new document still hosts but its manifest misses, without dropping it once", () => {
const { getApi, gen, liveIframe, shadowIframe, root } = setup();
const film = `<div data-composition-id="main"><div id="benefit" data-composition-id="benefit"
data-composition-src="compositions/benefit.html" data-start="0" data-duration="3"></div><p id="a"></p></div>`;
data-composition-src="compositions/benefit.html" data-composition-file="compositions/benefit.html" data-start="0" data-duration="3"></div><p id="a"></p></div>`;
liveIframe.contentDocument!.body.innerHTML = film;
shadowIframe.contentDocument!.body.innerHTML = film;
const a = { id: "a", domId: "a", tag: "p", start: 0, duration: 3, track: 0 };
Expand Down
2 changes: 1 addition & 1 deletion packages/studio/src/player/lib/automationStoreSync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ describe("syncStoredAutomationFromPreview", () => {
usePlayerStore.setState({ elements: [el({ hfId: "hf-bgm" })] });
const doc = document.implementation.createHTMLDocument("preview");
doc.body.innerHTML =
'<div data-composition-id="strip" data-composition-src="compositions/strip.html">' +
'<div data-composition-id="strip" data-composition-src="compositions/strip.html" data-composition-file="compositions/strip.html">' +
'<audio id="bgm" data-hf-id="hf-bgm"></audio></div><audio id="bgm" data-hf-id="hf-bgm"></audio>';
doc.querySelectorAll("audio")[0]?.setAttribute("data-automation", TWO_POINTS);
doc.querySelectorAll("audio")[1]?.setAttribute("data-automation", RESTORED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe("buildMissingCompositionElements — hfId (R7)", () => {
it("adds a host the lookup finds in its preview, as a composition", () => {
const doc = makeDoc(`
<div data-composition-id="root">
<div id="benefit-fresh" data-composition-id="benefit-fresh" data-composition-src="compositions/benefit-fresh.html"
<div id="benefit-fresh" data-composition-id="benefit-fresh" data-composition-src="compositions/benefit-fresh.html" data-composition-file="compositions/benefit-fresh.html"
data-start="0" data-duration="3"></div>
</div>
`);
Expand Down
2 changes: 1 addition & 1 deletion packages/studio/src/player/lib/timelineIframeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ function buildMissingCompositionEntry(params: {

const preferredId = nonEmpty(element.id, compositionId);
const compositionSrc =
element.getAttribute("data-composition-src") ?? element.getAttribute("data-composition-file");
element.getAttribute("data-composition-file") ?? element.getAttribute("data-composition-src");
const selector = getTimelineElementSelector(element);
const sourceFile = getTimelineElementSourceFile(element);
const selectorIndex = getTimelineElementSelectorIndex(doc, element, selector);
Expand Down
Loading