Skip to content

Commit 4c89caa

Browse files
fix(core): one shared scene-template pick for the compiler, the inliner and the loader
A scene template after a card template was detected by locateSceneRoot but mounted from the first template of any kind, so it rendered blank. findSceneTemplate picks the first template holding a composition root and locateSceneRoot, resolveSubCompositionContent and the loader all use it. Co-authored-by: Jerrai <[email protected]>
1 parent 1f007a2 commit 4c89caa

5 files changed

Lines changed: 78 additions & 11 deletions

File tree

‎packages/core/src/compiler/htmlBundler.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,36 @@ describe("bundleToSingleHtml", () => {
302302
expect(bundled).not.toContain("second scene");
303303
});
304304

305+
it.each([
306+
[
307+
"a card template before the scene",
308+
`<template id="card"><div class="card">card</div></template>`,
309+
"mounted scene",
310+
"",
311+
],
312+
[
313+
"two scene templates",
314+
"",
315+
"mounted scene",
316+
`<template><div data-composition-id="other"><h1>second scene</h1></div></template>`,
317+
],
318+
])("mounts the scene template of a file with %s", async (_label, before, expected, after) => {
319+
const dir = makeTempProject({
320+
"index.html": `<!doctype html><html><body>
321+
<div data-composition-id="root" data-width="320" data-height="180" data-duration="6">
322+
<div data-composition-id="scene" data-composition-src="compositions/scene.html" data-start="0" data-duration="6"></div>
323+
</div>
324+
</body></html>`,
325+
"compositions/scene.html": `${before}<template><div data-composition-id="scene" data-duration="6"><h1>mounted scene</h1></div></template>${after}`,
326+
});
327+
328+
const bundled = await bundleToSingleHtml(dir);
329+
330+
expect(bundled).toContain(expected);
331+
expect(bundled).not.toContain("second scene");
332+
expect(bundled).not.toContain(">card<");
333+
});
334+
305335
it("rebases direct-entry authored asset paths before inlining", async () => {
306336
const spriteSvg = '<svg xmlns="http://www.w3.org/2000/svg"><circle r="4"/></svg>';
307337
const bgSvg = '<svg xmlns="http://www.w3.org/2000/svg"><rect width="8" height="8"/></svg>';

‎packages/core/src/compiler/htmlDocument.ts‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { acceleratedCanvasTrackerSource } from "../pageMotionBlur.js";
2+
import { findSceneTemplate } from "@hyperframes/parsers/sub-composition-validity";
23
import { parseHTML } from "linkedom";
34

45
export const RUNTIME_BOOTSTRAP_ATTR = "data-hyperframes-preview-runtime";
@@ -25,16 +26,11 @@ export interface SceneRootLookup {
2526
export function locateSceneRoot(html: string): SceneRootLookup {
2627
const { document } = parseHTML(html);
2728
const liveRoot = document.querySelector("[data-composition-id]") as Element | null;
28-
const templates = Array.from(document.querySelectorAll("template")) as Element[];
29+
const templates = Array.from(document.querySelectorAll("template"));
2930
const sceneRoot =
30-
templates
31-
.map(
32-
(template) =>
33-
parseHTML(template.innerHTML).document.querySelector(
34-
"[data-composition-id]",
35-
) as Element | null,
36-
)
37-
.find((root) => root !== null) ?? null;
31+
(findSceneTemplate(templates)?.content.querySelector(
32+
"[data-composition-id]",
33+
) as Element | null) ?? null;
3834
return {
3935
root: sceneRoot,
4036
hasTemplate: templates.length > 0,

‎packages/core/src/runtime/compositionLoader.test.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,27 @@ describe("loadCompositions external hosts", () => {
149149
).toBe(false);
150150
});
151151

152+
it.each([
153+
["a card template before the scene", `<template><div class="card">card</div></template>`, ""],
154+
[
155+
"two scene templates",
156+
"",
157+
`<template><div data-composition-id="other"><p>second scene</p></div></template>`,
158+
],
159+
])("mounts the scene template of a fetched file with %s", async (_label, before, after) => {
160+
const host = appendExternalHost("https://example.com/comp.html", "scene-1");
161+
const scene = `<template><div data-composition-id="scene-1"><p>Hello World</p></div></template>`;
162+
vi.spyOn(globalThis, "fetch").mockResolvedValue(
163+
new Response(`${before}${scene}${after}`, { status: 200 }),
164+
);
165+
166+
await loadFixture();
167+
168+
expect(host.textContent).toContain("Hello World");
169+
expect(host.textContent).not.toContain("card");
170+
expect(host.textContent).not.toContain("second scene");
171+
});
172+
152173
it("sizes a flattened inner root from a px-suffixed or fractional size", async () => {
153174
const host = appendExternalHost("https://example.com/comp.html", "scene-1");
154175
const compositionHtml = `

‎packages/core/src/runtime/compositionLoader.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { findSceneTemplate } from "@hyperframes/parsers/sub-composition-validity";
12
import {
23
SVG_REFERENCE_ALIASES_ATTR,
34
readSvgReferenceAliases,
@@ -185,6 +186,11 @@ function rewriteSubCompositionAssetPaths(root: ParentNode, compositionUrl: URL |
185186
}
186187
}
187188

189+
function sceneOrFirstTemplate(doc: Document): HTMLTemplateElement | null {
190+
const templates = Array.from(doc.querySelectorAll<HTMLTemplateElement>("template"));
191+
return findSceneTemplate(templates) ?? templates[0] ?? null;
192+
}
193+
188194
function uniqueCompositionId(baseId: string, index: number): string {
189195
return `${baseId}__hf${index}`;
190196
}
@@ -800,7 +806,7 @@ async function mountExternalCompositions(
800806
? doc.querySelector<HTMLTemplateElement>(
801807
`template#${CSS.escape(authoredCompositionId)}-template`,
802808
)
803-
: null) ?? doc.querySelector<HTMLTemplateElement>("template");
809+
: null) ?? sceneOrFirstTemplate(doc);
804810
const sourceNode = template ? template.content : doc.body;
805811
return await mount({
806812
host,

‎packages/parsers/src/subCompositionValidity.ts‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,25 @@ export interface ParsableDocumentLike {
5555
documentElement: { outerHTML?: string } | null;
5656
body?: { innerHTML?: string | null } | null;
5757
querySelector(selector: string): { innerHTML?: string | null } | null;
58+
querySelectorAll(selector: string): ArrayLike<{ innerHTML?: string | null } & SceneTemplateLike>;
59+
}
60+
61+
export interface SceneTemplateLike {
62+
content: { querySelector(selector: string): unknown };
63+
}
64+
65+
export function findSceneTemplate<T extends SceneTemplateLike>(
66+
templates: ArrayLike<T>,
67+
): T | undefined {
68+
return Array.from(templates).find(
69+
(template) => template.content.querySelector("[data-composition-id]") != null,
70+
);
5871
}
5972

6073
/** Resolve authored content without invoking linkedom's synthetic body getter. */
6174
export function resolveSubCompositionContent(html: string, document: ParsableDocumentLike) {
62-
const template = document.querySelector("template");
75+
const templates = Array.from(document.querySelectorAll("template"));
76+
const template = findSceneTemplate(templates) ?? templates[0];
6377
if (template) return { contentHtml: template.innerHTML ?? "", hasTemplate: true };
6478

6579
const body = document.querySelector("body");

0 commit comments

Comments
 (0)