mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-24 16:06:34 +00:00
* fix(core): selector alias values escape '<' so style text can't end early Attribute values that the compiler copies into selector text, such as an SVG reference alias's rewritten value or a composition or root id in a scope selector, are written inside a <style> element. A '<' in one could close that element early. These values now write '<' as the CSS escape \3c , which reads back as the same character, so each selector still matches the same elements. * test(core): name the '<' escape tests for what the escapes guarantee The selector escapes write '<' as \3c themselves, so selector text cannot end its style element even when the CSS is serialized without postcss's own </style escaping. The test names and comments now say that.
898 lines
38 KiB
TypeScript
898 lines
38 KiB
TypeScript
import {
|
|
SVG_REFERENCE_ALIASES_ATTR,
|
|
dropAuthoredSvgReferenceAliases,
|
|
readSvgReferenceAliases,
|
|
type SvgReferenceAlias,
|
|
} from "./svgSelectorAliases";
|
|
import { readExternalScriptAttributes, type ExternalScriptAttributes } from "./externalScripts";
|
|
import { parseImportMap, type ImportMap } from "./importMaps";
|
|
import {
|
|
compositionStyle,
|
|
cssStyleMergeKey,
|
|
hasSameLink,
|
|
type CompositionStyle,
|
|
} from "./scriptRuns";
|
|
/**
|
|
* Shared sub-composition inlining logic.
|
|
*
|
|
* Both the core bundler (preview) and the producer compiler (render) need to
|
|
* inline sub-composition HTML referenced via `data-composition-src`. This
|
|
* module is the single source of truth for that transformation, eliminating
|
|
* divergence that previously caused bugs (e.g. producer not setting
|
|
* `data-composition-file`).
|
|
*/
|
|
|
|
import {
|
|
rewriteAssetPath,
|
|
rewriteAssetPaths,
|
|
rewriteCssAssetUrls,
|
|
rewriteInlineStyleAssetUrls,
|
|
type AssetExists,
|
|
} from "./rewriteSubCompPaths";
|
|
import { warnUnknownEnumValues } from "../runtime/getVariables";
|
|
import {
|
|
createCompositionCssScoper,
|
|
AUTHORED_SOURCE_BEGIN,
|
|
AUTHORED_SOURCE_END,
|
|
wrapInlineScriptWithErrorBoundary,
|
|
scopedModulePrelude,
|
|
wrapScopedCompositionScript,
|
|
} from "./compositionScoping";
|
|
import {
|
|
namespaceCollidingSvgIds,
|
|
sceneClipRoots,
|
|
rewriteSvgIdReferencesInCss,
|
|
type SvgIdMap,
|
|
type SvgIdScope,
|
|
} from "./svgIdNamespacing";
|
|
import {
|
|
checkSubCompositionUsability,
|
|
resolveSubCompositionContent,
|
|
} from "@hyperframes/parsers/sub-composition-validity";
|
|
import {
|
|
enumerateNestedCompositionHosts,
|
|
planCompositionAssembly,
|
|
extractedCompositionAssets,
|
|
} from "./compositionAssembly";
|
|
import { SCENE_NO_SWAP_ATTR, SCENE_PART_ATTR } from "../sceneParts";
|
|
import { terminalText } from "../terminalText";
|
|
|
|
// Anything a scene script can leave running, pending or registered outside its timeline, or that
|
|
// throws when run again: only the timeline is torn down when a scene is swapped, so when unsure, refuse.
|
|
const SIDE_EFFECT_RE =
|
|
/\b(addEventListener|requestAnimationFrame|requestIdleCallback|setTimeout|setInterval|queueMicrotask|getContext|WebGL\w*|WebGPU\w*|gpu|Worker|WebSocket|EventSource|Audio\w*|\w*Observer|fetch|import|eval|Function|Promise|async|await|delayedCall|ScrollTrigger|Draggable|anime|customElements|registerProperty|addListener|BroadcastChannel|pushState|replaceState|adoptedStyleSheets|documentElement|getElementsByTagName|lottie|THREE|__hf[A-Z]\w*)\b|\.then\s*\(|\.ticker\b|repeat\s*:\s*-1|\.repeat\s*\(\s*-1|defineProperty\s*\(\s*(window|globalThis|self|document)\b|\bfonts\s*\.\s*add\b|\.on[a-z]+\s*=(?!=)|\bon(resize|scroll|message|key\w+|click|pointer\w+|mouse\w+|wheel|visibilitychange|hashchange|popstate|error|load)\s*=(?!=)|\[\s*["']on[a-z]+["']\s*\]|document\s*\.\s*(head|body)\b|querySelector(All)?\(\s*["'](head|body)["']|\bgsap\s*\.\s*(?:to|from|fromTo)\s*\(|\bgsap\s*\.\s*timeline\s*\((?:[^()]|\([^()]*\))*\)\s*\.\s*(?:to|from|fromTo|set|add|call)\s*\(/;
|
|
|
|
// npm packages that only define globals when loaded; lottie-web is absent because it scans the page on load.
|
|
const SWAP_SAFE_LIBRARY_URL =
|
|
/^https:\/\/(cdn\.jsdelivr\.net\/npm|unpkg\.com)\/(gsap|three|d3|d3-[a-z-]+|topojson-client|clipper-lib)(@[^/]+)?\//;
|
|
const isSwapSafeLibrary = (src: string) =>
|
|
URL.canParse(src) && SWAP_SAFE_LIBRARY_URL.test(new URL(src).href);
|
|
// GSAP's published file copied into a project, known by its license banner: it only defines globals too.
|
|
const GSAP_BANNER_RE = /^\s*\/\*!\s*\*\s*GSAP \d+\.\d+\.\d+\s*\*\s*https:\/\/gsap\.com\b/;
|
|
export const isLibraryCopy = (script: string) => GSAP_BANNER_RE.test(script);
|
|
|
|
// Waits on the page's fonts, ready or loading named faces: each settles once, leaving nothing behind.
|
|
const STRING_ARG = String.raw`(?:"(?:\\.|[^"\\\n])*"|'(?:\\.|[^'\\\n])*')`;
|
|
const FONT_LOAD = String.raw`document\s*\.\s*fonts\s*\.\s*load\s*\(\s*${STRING_ARG}\s*(?:,\s*${STRING_ARG}\s*)?\)`;
|
|
const FONT_PROMISE = String.raw`(?:document\s*\.\s*fonts\s*\.\s*ready\b|${FONT_LOAD}|Promise\s*\.\s*all\s*\(\s*\[\s*${FONT_LOAD}(?:\s*,\s*${FONT_LOAD})*\s*,?\s*\]\s*\))`;
|
|
const FONT_WAIT_RE = new RegExp(
|
|
String.raw`\b${FONT_PROMISE}\s*\.\s*then\s*\(|\bawait\s+${FONT_PROMISE}`,
|
|
"g",
|
|
);
|
|
|
|
// A canvas made only to measure text: never named, so nothing attaches it to the page or draws on it there.
|
|
const TEXT_MEASURE_CANVAS_RE =
|
|
/\bdocument\s*\.\s*createElement\s*\(\s*(["'])canvas\1\s*\)\s*\.\s*getContext\s*\(\s*(["'])2d\2\s*\)/g;
|
|
// A timeline chained as it is registered: the registry holds it, so unmounting the scene stops it.
|
|
const REGISTERED_CHAIN_RE =
|
|
/(\b(?:window\s*\.\s*)?__timelines\s*\[[^[\]]*\]\s*=\s*)gsap\s*\.\s*timeline\s*\(/g;
|
|
|
|
/** Whether a call of the browser's Element.animate may leave an animation: it gets timing, its second argument. */
|
|
function lastingAnimateCall(script: string): boolean {
|
|
for (const call of script.matchAll(/\.animate\s*\(/g)) {
|
|
if (topLevelArgs(script, call.index + call[0].length) !== 1) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
// Arguments of the call whose "(" ends before `from`; none, a spread or an unclosed call never count as one.
|
|
function topLevelArgs(script: string, from: number): number {
|
|
let depth = 0;
|
|
let args = 1;
|
|
if (/^\s*(\.\.\.|\))/.test(script.slice(from))) return Number.POSITIVE_INFINITY;
|
|
for (let i = from; i < script.length; i++) {
|
|
const ch = script[i]!;
|
|
if (ch === '"' || ch === "'" || ch === "`") {
|
|
const end = script.indexOf(ch, i + 1);
|
|
if (end < 0) return Number.POSITIVE_INFINITY;
|
|
i = end;
|
|
} else if ("([{".includes(ch)) depth++;
|
|
else if (")]}".includes(ch)) {
|
|
if (depth === 0) return args;
|
|
depth--;
|
|
} else if (ch === "," && depth === 0) args++;
|
|
}
|
|
return Number.POSITIVE_INFINITY;
|
|
}
|
|
|
|
/** Why an authored scene script cannot be swapped out cleanly, or null when it can. */
|
|
function sceneScriptSwapRefusal(script: string): string | null {
|
|
const withoutFontWaits = script.replace(FONT_WAIT_RE, "");
|
|
const awaitedFontsOnly = /\bawait\b/.test(script) && !/\bawait\b/.test(withoutFontWaits);
|
|
let checked = (
|
|
awaitedFontsOnly ? withoutFontWaits.replace(/\basync\b/g, "") : withoutFontWaits
|
|
).replace(REGISTERED_CHAIN_RE, "$1(");
|
|
// The context hands its canvas back through .canvas, which could then reach the page.
|
|
if (!/\.\s*canvas\b/.test(checked)) checked = checked.replace(TEXT_MEASURE_CANVAS_RE, "");
|
|
const match = SIDE_EFFECT_RE.exec(checked);
|
|
if (match) return `its script uses ${match[0].trim()}`;
|
|
return lastingAnimateCall(checked) ? "its script uses .animate(" : null;
|
|
}
|
|
|
|
const projectScriptRefusal = (text: string | null) =>
|
|
text === null
|
|
? "it runs a script that is not a known library"
|
|
: isLibraryCopy(text)
|
|
? null
|
|
: sceneScriptSwapRefusal(text);
|
|
|
|
// A quoted string in a script, in any of the three quote styles.
|
|
const STRING_LITERAL_RE = /(["'`])((?:\\.|(?!\1)[^\\\n])*?)\1/g;
|
|
// Text ending where createElement takes its tag: that string makes a node, it selects none.
|
|
const CREATES_ELEMENT_RE = /createElement\s*\(\s*$|createElementNS\s*\([^()]*,\s*$/;
|
|
// A "*" after a comma is a later argument, like postMessage's target origin; selector calls take it first.
|
|
const isNotASelector = (script: string, at: number, literal: string) =>
|
|
CREATES_ELEMENT_RE.test(script.slice(Math.max(0, at - 60), at)) ||
|
|
(literal.trim() === "*" && /,\s*$/.test(script.slice(Math.max(0, at - 20), at)));
|
|
|
|
/** Marks each scene whose nodes a script outside it names by selector, id or class: a swap would strand it. */
|
|
export function refuseSwapsReachedByRootScripts(document: Document, rootScripts: string[]): void {
|
|
const hosts = [...document.querySelectorAll(`[${SCENE_PART_ATTR}]`)];
|
|
const byName = new Map<string, Set<Element>>();
|
|
for (const host of hosts) {
|
|
for (const el of [host, ...host.querySelectorAll("*")]) {
|
|
for (const name of [
|
|
el.localName,
|
|
el.id && `#${el.id}`,
|
|
...[...el.classList].map((c) => `.${c}`),
|
|
]) {
|
|
if (name) byName.set(name, (byName.get(name) ?? new Set()).add(host));
|
|
}
|
|
}
|
|
}
|
|
// A selector naming a tag the document lacks cannot match, so it is not worth a query.
|
|
const namesKnown = (selector: string) =>
|
|
selector
|
|
.split(",")
|
|
.some((alt) =>
|
|
alt
|
|
.split(/[\s>+~]+/)
|
|
.every(
|
|
(part) =>
|
|
!/^[a-z][\w-]*$/i.test(part) ||
|
|
byName.has(part.toLowerCase()) ||
|
|
document.querySelector(part) !== null,
|
|
),
|
|
);
|
|
const literals = new Set(
|
|
rootScripts.flatMap((s) =>
|
|
[...s.matchAll(STRING_LITERAL_RE)]
|
|
.filter((m) => !isNotASelector(s, m.index, m[2] ?? ""))
|
|
.map((m) => m[2] ?? ""),
|
|
),
|
|
);
|
|
for (const literal of literals) {
|
|
const open = hosts.filter((host) => !host.hasAttribute(SCENE_NO_SWAP_ATTR));
|
|
if (open.length === 0) return;
|
|
// Tag names match in any case; ids and classes do not.
|
|
const reached =
|
|
literal.trim() === "*"
|
|
? open
|
|
: /^[A-Za-z_][\w-]*$/.test(literal)
|
|
? [literal.toLowerCase(), `#${literal}`, `.${literal}`].flatMap((name) => [
|
|
...(byName.get(name) ?? []),
|
|
])
|
|
: /[#.[:>]|[\w\]*] *[\s,]+ *[\w*]/.test(literal) &&
|
|
literal.length <= 120 &&
|
|
namesKnown(literal)
|
|
? open.filter((host) => reaches(host, literal))
|
|
: [];
|
|
for (const host of reached)
|
|
if (!host.hasAttribute(SCENE_NO_SWAP_ATTR))
|
|
host.setAttribute(SCENE_NO_SWAP_ATTR, `a script outside the scene selects ${literal}`);
|
|
}
|
|
}
|
|
|
|
const reaches = (host: Element, selector: string) => {
|
|
try {
|
|
return host.matches(selector) || host.querySelector(selector) !== null;
|
|
} catch {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Public interface
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export interface InlineSubCompositionsOptions {
|
|
/**
|
|
* Resolve the HTML content for a sub-composition given its `data-composition-src` value.
|
|
* Return `null` when the file cannot be found.
|
|
*/
|
|
resolveHtml: (srcPath: string) => string | null;
|
|
|
|
/** The project-relative file `resolveHtml` read for a src; asset paths rebase from its folder. */
|
|
resolveFilePath?: (srcPath: string) => string | null;
|
|
|
|
/**
|
|
* Parse an HTML string into a Document. The returned object must expose
|
|
* standard DOM APIs (querySelector, querySelectorAll, body, head, etc.).
|
|
* Both linkedom's `parseHTML(...).document` and the core bundler's
|
|
* `parseHTMLContent(...)` satisfy this contract.
|
|
*/
|
|
parseHtml: (html: string) => Document;
|
|
|
|
/**
|
|
* Identity map produced by `assignBundledRuntimeCompositionIds`.
|
|
* When provided, a host's ids are read from this map; a host it lacks (one
|
|
* inside a scene) is read from its own attributes, as without a map.
|
|
* The bundler uses this; the producer can omit it.
|
|
*/
|
|
hostIdentityMap?: Map<
|
|
Element,
|
|
{ authoredCompositionId: string | null; runtimeCompositionId: string | null }
|
|
>;
|
|
|
|
/**
|
|
* When true, rewrite `url(...)` references in inline `style` attributes
|
|
* on sub-composition elements. The bundler enables this; the producer
|
|
* can skip it.
|
|
*/
|
|
rewriteInlineStyles?: boolean;
|
|
|
|
/**
|
|
* Prepare the inner root element before injecting it into the host.
|
|
* The bundler's `prepareFlattenedInnerRoot` clones the element, strips
|
|
* timing attributes, and adds `data-hf-inner-root`. When omitted, the
|
|
* inner root's outerHTML is injected as-is.
|
|
*/
|
|
flattenInnerRoot?: (innerRoot: Element) => Element;
|
|
|
|
/**
|
|
* When true, CSS selectors targeting the authored root use a compound
|
|
* selector (`[scope][root]`) instead of a descendant (`[scope] [root]`).
|
|
* Enable this in the producer path where the inner root merges onto
|
|
* the host element via innerHTML — both attributes end up on the same
|
|
* element and a descendant selector won't match.
|
|
*/
|
|
compoundAuthoredRoot?: boolean;
|
|
|
|
/**
|
|
* Read declared variable defaults from a sub-composition's `<html>` element.
|
|
* The bundler passes `readDeclaredDefaults`; the producer can omit this.
|
|
*/
|
|
readVariableDefaults?: (docElement: Element) => Record<string, unknown>;
|
|
|
|
/**
|
|
* Parse host-level variable overrides from `data-variable-values`.
|
|
* The bundler passes `parseHostVariableValues`; the producer can omit this.
|
|
*/
|
|
parseHostVariables?: (host: Element) => Record<string, unknown>;
|
|
|
|
/**
|
|
* Build a CSS attribute selector for scoping, e.g.
|
|
* `[data-composition-id="my-comp"]`. Defaults to a simple implementation
|
|
* when not provided. The bundler passes `cssAttributeSelector` which
|
|
* handles escaping.
|
|
*/
|
|
buildScopeSelector?: (compId: string) => string;
|
|
|
|
/**
|
|
* Error label prefix used in wrapped composition scripts.
|
|
* Defaults to `"[HyperFrames] composition script error:"`.
|
|
*/
|
|
scriptErrorLabel?: string;
|
|
|
|
/**
|
|
* Probe for "does this project-root-relative path exist?". Supplied by
|
|
* callers that can see the filesystem so a sub-composition's SIBLING asset
|
|
* refs (`<link href="_shared.css">` next to the composition) resolve against
|
|
* its own directory instead of 404ing at the project root. Omit it and plain
|
|
* relative paths pass through unchanged. See `AssetExists`.
|
|
*/
|
|
assetExists?: AssetExists;
|
|
|
|
/**
|
|
* Log a warning when a composition file cannot be resolved. `reason` is a
|
|
* short, human-readable explanation (e.g. "the file is empty (0 bytes or
|
|
* whitespace-only)") from `checkSubCompositionUsability` — present for
|
|
* every skip except when `resolveHtml` returns `null` (file not found,
|
|
* which callers detect themselves before calling `resolveHtml`).
|
|
* Defaults to `console.warn`.
|
|
*/
|
|
onMissingComposition?: (srcPath: string, reason?: string) => void;
|
|
/** Tag each top-level host with `data-hf-scene` and report every part's scene, for preview swaps. */
|
|
tagScenes?: boolean;
|
|
readProjectScript?: (path: string) => string | null;
|
|
/**
|
|
* Told the size of each script and stylesheet generated for a mount: each wrapped script once
|
|
* built, and each scoped sheet rule by rule as `scopeCssToComposition` reports it. May throw.
|
|
*/
|
|
chargeEmitted?: (srcPath: string, bytes: number) => void;
|
|
}
|
|
|
|
export interface ExternalLink {
|
|
href: string;
|
|
rel: string;
|
|
crossorigin?: string;
|
|
media?: string;
|
|
title?: string;
|
|
type?: string;
|
|
disabled?: true;
|
|
}
|
|
|
|
/** Appends a hoisted link unless the document already has a live one it would duplicate. */
|
|
export function ensureExternalLinkTag(doc: Document, link: ExternalLink): void {
|
|
const el = doc.createElement("link");
|
|
el.setAttribute("rel", link.rel);
|
|
el.setAttribute("href", link.href);
|
|
for (const name of ["crossorigin", "media", "title", "type"] as const) {
|
|
const value = link[name];
|
|
if (value != null) el.setAttribute(name, value);
|
|
}
|
|
if (link.disabled) el.setAttribute("disabled", "");
|
|
if (!hasSameLink(doc, el)) doc.head.appendChild(el);
|
|
}
|
|
|
|
export interface InlineSubCompositionsResult {
|
|
styles: CompositionStyle[];
|
|
/** With `tagScenes`: the scene each entry of `styles` belongs to. */
|
|
styleScenes: string[];
|
|
scripts: string[];
|
|
externalScriptSrcs: string[];
|
|
scriptItems: Array<
|
|
| { kind: "inline"; content: string; scene?: string }
|
|
| ({ kind: "external"; src: string; scene?: string } & ExternalScriptAttributes)
|
|
>;
|
|
/** May list one link more than once; `ensureExternalLinkTag` dedupes. */
|
|
externalLinks: ExternalLink[];
|
|
variablesByComp: Record<string, Record<string, unknown>>;
|
|
/** Mounted files' import maps, addresses rebased; emit with `emitMountedModuleScripts`. */
|
|
importMaps: ImportMap[];
|
|
/** Mounted files' inline module scripts, each already carrying its scoped `__hyperframes`. */
|
|
moduleScripts: string[];
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Default helpers
|
|
// ---------------------------------------------------------------------------
|
|
|
|
const isDocument = (node: Node): node is Document => node.nodeType === 9;
|
|
|
|
function defaultBuildScopeSelector(compId: string): string {
|
|
const escaped = compId.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/</g, "\\3c ");
|
|
return `[data-composition-id="${escaped}"]`;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Core implementation
|
|
// ---------------------------------------------------------------------------
|
|
|
|
/**
|
|
* Inline sub-compositions into a document. For each host element in `hosts`:
|
|
*
|
|
* 1. Resolve the sub-composition HTML via `options.resolveHtml`
|
|
* 2. Parse it, resolve template, body, or bare-fragment content
|
|
* 3. Find the inner `[data-composition-id]` root
|
|
* 4. Extract `<style>` elements, scope CSS, collect them
|
|
* 5. Extract `<script>` elements, wrap inline scripts, collect them
|
|
* 6. Collect external script `src` URLs for deduplication
|
|
* 7. Rewrite asset paths (and optionally inline-style asset URLs)
|
|
* 8. Copy dimension attrs from inner root to host if missing
|
|
* 9. Set `data-composition-file` on host
|
|
* 10. Remove `data-composition-src` from host
|
|
* 11. Inject the content into the host element
|
|
*/
|
|
// fallow-ignore-next-line complexity
|
|
export function inlineSubCompositions(
|
|
document: Document,
|
|
hosts: Element[],
|
|
options: InlineSubCompositionsOptions,
|
|
): InlineSubCompositionsResult {
|
|
const {
|
|
resolveHtml,
|
|
resolveFilePath,
|
|
parseHtml,
|
|
hostIdentityMap,
|
|
rewriteInlineStyles = false,
|
|
flattenInnerRoot,
|
|
compoundAuthoredRoot,
|
|
readVariableDefaults,
|
|
parseHostVariables,
|
|
buildScopeSelector = defaultBuildScopeSelector,
|
|
scriptErrorLabel = "[HyperFrames] composition script error:",
|
|
onMissingComposition,
|
|
assetExists,
|
|
tagScenes = false,
|
|
readProjectScript,
|
|
chargeEmitted,
|
|
} = options;
|
|
|
|
const rootStyles = [...document.querySelectorAll("style")];
|
|
const styles: CompositionStyle[] = [];
|
|
const styleScenes: string[] = [];
|
|
const scripts: string[] = [];
|
|
const externalScriptSrcs: string[] = [];
|
|
const scriptItems: InlineSubCompositionsResult["scriptItems"] = [];
|
|
const importMaps: ImportMap[] = [];
|
|
const moduleScripts: string[] = [];
|
|
const externalLinks: ExternalLink[] = [];
|
|
const variablesByComp: Record<string, Record<string, unknown>> = {};
|
|
const svgIdScopes: Array<SvgIdScope & { src: string; styleStart: number; styleEnd: number }> = [];
|
|
// A file mounted many times reads the same; only its scope, ids and variables differ per mount.
|
|
// Each file is parsed once and only read; a mount edits its own copy.
|
|
const parsed = new Map<string, Document>();
|
|
const parseOnce = (html: string): Document => {
|
|
let doc = parsed.get(html);
|
|
if (!doc) {
|
|
doc = parseHtml(html);
|
|
parsed.set(html, doc);
|
|
}
|
|
return doc;
|
|
};
|
|
const parseCopy = (html: string): Document => {
|
|
const copy = parseOnce(html).cloneNode(true);
|
|
return isDocument(copy) ? copy : parseHtml(html);
|
|
};
|
|
const usability = new Map<string, ReturnType<typeof checkSubCompositionUsability>>();
|
|
const sheetsWithAssetUrls = new Map<string, string>();
|
|
const scopeCssToComposition = createCompositionCssScoper();
|
|
|
|
const sceneHosts = new Map<string, Element>();
|
|
|
|
const queue = hosts.map((element) => ({
|
|
element,
|
|
ancestry: [] as string[],
|
|
scene: undefined as string | undefined,
|
|
}));
|
|
for (let queueIndex = 0; queueIndex < queue.length; queueIndex += 1) {
|
|
const { element: hostEl, ancestry, scene: parentScene } = queue[queueIndex]!;
|
|
const src = hostEl.getAttribute("data-composition-src");
|
|
if (!src) continue;
|
|
|
|
const compHtml = resolveHtml(src);
|
|
// Shared with lint + render pre-flight (@hyperframes/parsers'
|
|
// subCompositionValidity.ts) so all three callers agree on what counts
|
|
// as a usable sub-composition file. This path stays intentionally
|
|
// tolerant (skip, don't throw) — preview and studio must keep bundling
|
|
// around a scene that's still being authored. Lint and the render
|
|
// pre-flight check use the same helper to fail loudly instead.
|
|
let validity = compHtml == null ? undefined : usability.get(compHtml);
|
|
if (!validity) {
|
|
validity = checkSubCompositionUsability(compHtml, parseOnce);
|
|
if (compHtml != null) usability.set(compHtml, validity);
|
|
}
|
|
if (!validity.ok) {
|
|
onMissingComposition?.(src, validity.detail);
|
|
continue;
|
|
}
|
|
if (compHtml == null) {
|
|
// Unreachable in practice — checkSubCompositionUsability's "empty"
|
|
// reason already covers null/undefined — but this lets TypeScript
|
|
// narrow compHtml to `string` below without an `as T` assertion.
|
|
onMissingComposition?.(src);
|
|
continue;
|
|
}
|
|
|
|
const compDoc = parseCopy(compHtml);
|
|
|
|
// Determine composition IDs
|
|
let compId: string | null;
|
|
let runtimeCompId: string;
|
|
const identity = hostIdentityMap?.get(hostEl);
|
|
if (identity) {
|
|
compId = identity.authoredCompositionId || null;
|
|
runtimeCompId = identity.runtimeCompositionId || compId || "";
|
|
} else {
|
|
compId = hostEl.getAttribute("data-composition-id") || null;
|
|
runtimeCompId = compId || "";
|
|
}
|
|
|
|
const { contentHtml, hasTemplate } = resolveSubCompositionContent(compHtml, compDoc);
|
|
if (!contentHtml.trim()) {
|
|
onMissingComposition?.(src);
|
|
continue;
|
|
}
|
|
const contentDoc = parseCopy(contentHtml);
|
|
if (!contentDoc.documentElement) {
|
|
onMissingComposition?.(src);
|
|
continue;
|
|
}
|
|
|
|
// Which node is the composition root, which id its CSS scopes to, which
|
|
// id its scripts scope to, where its assets come from and in what order —
|
|
// every one of those is decided by the shared assembly module, so the mount
|
|
// path in runtime/compositionLoader.ts decides them the same way.
|
|
const plan = planCompositionAssembly<Element>({
|
|
contentNode: contentDoc,
|
|
head: compDoc.head,
|
|
documentElement: compDoc.documentElement,
|
|
hasTemplate,
|
|
compositionId: compId,
|
|
});
|
|
const innerRoot = plan.innerRoot;
|
|
const authoredRootId = plan.authoredRootId;
|
|
const scopeCompId = plan.authoredCompositionId || "";
|
|
const scriptCompositionId = plan.scriptCompositionId || "";
|
|
const runtimeScope = runtimeCompId ? buildScopeSelector(runtimeCompId) : "";
|
|
const scene = tagScenes ? (parentScene ?? (runtimeCompId || src)) : undefined;
|
|
if (scene && !parentScene) {
|
|
hostEl.setAttribute(SCENE_PART_ATTR, scene);
|
|
sceneHosts.set(scene, hostEl);
|
|
}
|
|
// Lazy, so renders (no scene hosts) never run the refusal checks.
|
|
const refuseSwap = (why: () => string | null) => {
|
|
const host = scene ? sceneHosts.get(scene) : undefined;
|
|
if (!host || host.hasAttribute(SCENE_NO_SWAP_ATTR)) return;
|
|
const reason = why();
|
|
if (reason) host.setAttribute(SCENE_NO_SWAP_ATTR, reason);
|
|
};
|
|
|
|
// Variable merging (bundler feature). Read declared defaults from the
|
|
// document element (full-document sub-comps) AND the inner composition root
|
|
// (template/fragment sub-comps store their schema on the root div, not a
|
|
// synthetic <html>), then let per-instance host values override.
|
|
//
|
|
// An anonymous host has no runtime id of its own. Its wrapper keeps the
|
|
// authored composition id (restored below), so that id is the scope its
|
|
// bound media and scripts resolve variables through (runtime/variableScope.ts)
|
|
// and the key the producer's probe gate reads — a full-document sub-comp's
|
|
// `<html data-composition-variables>` is dropped by inlining, so the table
|
|
// is the only place its declared defaults survive to compile time (#7505).
|
|
// A named mount of the same file shares that scope and its explicit
|
|
// instance values win, so an anonymous mount only fills the entry while
|
|
// nothing holds it; a named mount processed later still overwrites it.
|
|
const variablesCompId = runtimeCompId || scopeCompId;
|
|
if (readVariableDefaults && parseHostVariables && variablesCompId) {
|
|
const mergedVariables: Record<string, unknown> = {};
|
|
for (const carrier of plan.variableDefaultCarriers) {
|
|
Object.assign(mergedVariables, readVariableDefaults(carrier));
|
|
}
|
|
Object.assign(mergedVariables, parseHostVariables(hostEl));
|
|
const holdsEntry = !runtimeCompId && Object.hasOwn(variablesByComp, variablesCompId);
|
|
if (Object.keys(mergedVariables).length > 0 && !holdsEntry) {
|
|
variablesByComp[variablesCompId] = mergedVariables;
|
|
}
|
|
// Compile time is the only place this defect is visible on the sub-comp
|
|
// path: the instance value is baked into `__hfVariablesByComp` right
|
|
// here, and the scoped `getVariables` shim only reads that table, so the
|
|
// runtime's identical guard never runs. Same helper, so the message and
|
|
// the per-process dedupe set are shared with the runtime path and the
|
|
// author sees one warning either way.
|
|
warnUnknownEnumValues(compDoc.documentElement, mergedVariables, variablesCompId);
|
|
warnUnknownEnumValues(innerRoot, mergedVariables, variablesCompId);
|
|
}
|
|
|
|
const filePath = resolveFilePath?.(src) ?? src;
|
|
const chargeRebase = chargeEmitted && ((bytes: number) => chargeEmitted(src, bytes));
|
|
|
|
// `<head>` <link>/<script src> are hoisted into the ROOT document, so they
|
|
// need the same directory rewrite the body's [src]/[href] pass applies —
|
|
// without it even the documented `../` form escapes the project and 404s.
|
|
const resolveSubAssetPath = (raw: string | null): string =>
|
|
rewriteAssetPath(filePath, (raw || "").trim(), assetExists, chargeRebase);
|
|
|
|
// Scope one sub-composition <style> body. scopeRootSelectors keeps the
|
|
// sub-comp's html/body/:root rules from clobbering the host document (they
|
|
// are remapped to the composition box); see compositionScoping.
|
|
const scopeSubStyle = (raw: string): string => {
|
|
const sheetKey = JSON.stringify([src, raw]);
|
|
let css = sheetsWithAssetUrls.get(sheetKey);
|
|
if (css === undefined) {
|
|
css = rewriteCssAssetUrls(raw, filePath, assetExists, chargeRebase);
|
|
sheetsWithAssetUrls.set(sheetKey, css);
|
|
}
|
|
if (!scopeCompId) {
|
|
chargeEmitted?.(src, css.length);
|
|
return css;
|
|
}
|
|
return scopeCssToComposition(css, scopeCompId, runtimeScope || undefined, authoredRootId, {
|
|
compoundAuthoredRoot: compoundAuthoredRoot === true,
|
|
scopeRootSelectors: true,
|
|
...(chargeEmitted ? { charge: (bytes: number) => chargeEmitted(src, bytes) } : {}),
|
|
});
|
|
};
|
|
|
|
// <link> hoisting is unconditional. A templated sub-composition's webfont
|
|
// link is as load-bearing as a non-templated one's, and the mount path has
|
|
// always hoisted both; gating this on `!hasTemplate` dropped a templated
|
|
// composition's font from the render while preview kept it.
|
|
for (const link of plan.linkSources) {
|
|
const href = resolveSubAssetPath(link.getAttribute("href"));
|
|
if (href) {
|
|
const rel = (link.getAttribute("rel") || "").trim();
|
|
const crossorigin = link.hasAttribute("crossorigin")
|
|
? link.getAttribute("crossorigin") || ""
|
|
: undefined;
|
|
const media = link.getAttribute("media") ?? undefined;
|
|
const title = link.getAttribute("title") ?? undefined;
|
|
const type = link.getAttribute("type") ?? undefined;
|
|
const disabled = link.hasAttribute("disabled") ? true : undefined;
|
|
externalLinks.push({ href, rel, crossorigin, media, title, type, disabled });
|
|
}
|
|
}
|
|
|
|
// Head-sourced assets come first: a non-templated sub-composition's <head>
|
|
// carries its backgrounds, positioning and fonts, and a <head> library tag
|
|
// (GSAP from a CDN) has to run before the content scripts calling into it.
|
|
const styleStart = styles.length;
|
|
for (const styleEl of plan.styleSources) {
|
|
if (cssStyleMergeKey(styleEl) === undefined) continue;
|
|
styles.push(compositionStyle(styleEl, scopeSubStyle(styleEl.textContent || "")));
|
|
if (scene) styleScenes.push(scene);
|
|
styleEl.remove();
|
|
}
|
|
const styleEnd = styles.length;
|
|
|
|
// Head- and content-sourced scripts take the same branch.
|
|
for (const scriptEl of plan.scriptSources) {
|
|
const externalSrc = resolveSubAssetPath(scriptEl.getAttribute("src"));
|
|
const type = (scriptEl.getAttribute("type") || "").trim().toLowerCase();
|
|
// A swap never re-runs external or module scripts: only a known library that just defines globals is safe.
|
|
refuseSwap(() =>
|
|
type === "importmap" || type === "module"
|
|
? "it runs a module script or import map"
|
|
: !externalSrc
|
|
? sceneScriptSwapRefusal(scriptEl.textContent || "")
|
|
: isSwapSafeLibrary(externalSrc)
|
|
? null
|
|
: projectScriptRefusal(readProjectScript?.(externalSrc) ?? null),
|
|
);
|
|
if (type === "importmap") {
|
|
const map = parseImportMap(scriptEl.textContent || "", (url) => {
|
|
// The rebase drops a leading "./" and a trailing "/"; an import map address needs both.
|
|
const authored = url.trim();
|
|
const rebased = resolveSubAssetPath(authored);
|
|
if (rebased === authored) return rebased;
|
|
const urlLike = /^(\/|\.\.?\/|[a-z][a-z\d+.-]*:)/i.test(rebased)
|
|
? rebased
|
|
: `./${rebased}`;
|
|
return authored.endsWith("/") && !urlLike.endsWith("/") ? `${urlLike}/` : urlLike;
|
|
});
|
|
if (map) importMaps.push(map);
|
|
else
|
|
console.warn(
|
|
terminalText(`[HyperFrames] ${src}: import map is not valid JSON, so it is skipped.`),
|
|
);
|
|
} else if (type === "module" && !externalSrc) {
|
|
const prelude = scriptCompositionId
|
|
? scopedModulePrelude(runtimeCompId || scopeCompId || scriptCompositionId, src)
|
|
: "";
|
|
const moduleScript = `${prelude}${AUTHORED_SOURCE_BEGIN}\n${scriptEl.textContent || ""}\n${AUTHORED_SOURCE_END}`;
|
|
chargeEmitted?.(src, moduleScript.length);
|
|
moduleScripts.push(moduleScript);
|
|
} else if (externalSrc) {
|
|
if (!externalScriptSrcs.includes(externalSrc)) {
|
|
externalScriptSrcs.push(externalSrc);
|
|
}
|
|
scriptItems.push({
|
|
kind: "external",
|
|
src: externalSrc,
|
|
...readExternalScriptAttributes(scriptEl),
|
|
...(scene ? { scene } : {}),
|
|
});
|
|
} else {
|
|
const wrappedScript = scriptCompositionId
|
|
? wrapScopedCompositionScript(
|
|
scriptEl.textContent || "",
|
|
scriptCompositionId,
|
|
scriptErrorLabel,
|
|
runtimeScope || undefined,
|
|
runtimeCompId || scopeCompId || scriptCompositionId,
|
|
authoredRootId,
|
|
src,
|
|
)
|
|
: wrapInlineScriptWithErrorBoundary(scriptEl.textContent || "", scriptErrorLabel);
|
|
chargeEmitted?.(src, wrappedScript.length);
|
|
scripts.push(wrappedScript);
|
|
scriptItems.push({ kind: "inline", content: wrappedScript, ...(scene ? { scene } : {}) });
|
|
}
|
|
scriptEl.remove();
|
|
}
|
|
|
|
// Rewrite relative asset paths before inlining so ../foo.svg from
|
|
// compositions/ resolves correctly when the content moves to root.
|
|
const assetEls = innerRoot
|
|
? innerRoot.querySelectorAll("[src], [href], [srcset]")
|
|
: contentDoc.querySelectorAll("[src], [href], [srcset]");
|
|
rewriteAssetPaths(
|
|
assetEls,
|
|
filePath,
|
|
(el: Element, attr: string) => el.getAttribute(attr),
|
|
(el: Element, attr: string, val: string) => {
|
|
el.setAttribute(attr, val);
|
|
},
|
|
assetExists,
|
|
chargeRebase,
|
|
);
|
|
|
|
if (rewriteInlineStyles) {
|
|
const styledEls = innerRoot
|
|
? innerRoot.querySelectorAll("[style]")
|
|
: contentDoc.querySelectorAll("[style]");
|
|
rewriteInlineStyleAssetUrls(
|
|
styledEls,
|
|
filePath,
|
|
(el: Element) => el.getAttribute("style"),
|
|
(el: Element, val: string) => {
|
|
el.setAttribute("style", val);
|
|
},
|
|
assetExists,
|
|
chargeRebase,
|
|
);
|
|
}
|
|
|
|
if (innerRoot?.hasAttribute("data-timeline-locked")) {
|
|
hostEl.setAttribute("data-timeline-locked", "");
|
|
}
|
|
if (innerRoot?.hasAttribute("data-no-timeline")) {
|
|
hostEl.setAttribute("data-no-timeline", "");
|
|
}
|
|
|
|
// Copy dimension attributes from inner root to host if missing
|
|
if (innerRoot) {
|
|
const innerW = innerRoot.getAttribute("data-width");
|
|
const innerH = innerRoot.getAttribute("data-height");
|
|
if (innerW && !hostEl.getAttribute("data-width")) hostEl.setAttribute("data-width", innerW);
|
|
if (innerH && !hostEl.getAttribute("data-height")) {
|
|
hostEl.setAttribute("data-height", innerH);
|
|
}
|
|
}
|
|
|
|
// Inject content into the host element
|
|
if (innerRoot) {
|
|
innerRoot.setAttribute("data-composition-file", src);
|
|
for (const child of extractedCompositionAssets(innerRoot)) child.remove();
|
|
if (flattenInnerRoot) {
|
|
const prepared = flattenInnerRoot(innerRoot);
|
|
if (!compId && scopeCompId) {
|
|
// Anonymous host: flattenInnerRoot strips data-composition-id,
|
|
// assuming the host already carries the composition's identity.
|
|
// When the host has none, nothing in the render DOM matches the
|
|
// composition's own root-styling CSS or self-referencing scripts
|
|
// (e.g. document.querySelector('[data-composition-id="X"]')).
|
|
// Restore it on the wrapper so both keep resolving, same as
|
|
// before flattening preserved it via outerHTML.
|
|
prepared.setAttribute("data-composition-id", scopeCompId);
|
|
}
|
|
hostEl.innerHTML = prepared.outerHTML || "";
|
|
} else {
|
|
hostEl.innerHTML = compId ? innerRoot.innerHTML || "" : innerRoot.outerHTML || "";
|
|
// When the producer path strips the inner root (innerHTML), the
|
|
// authored id attribute is lost. Propagate it to the host so that
|
|
// rewritten #ID selectors ([data-hf-authored-id="X"]) still resolve.
|
|
if (compId && authoredRootId) {
|
|
hostEl.setAttribute("data-hf-authored-id", authoredRootId);
|
|
}
|
|
}
|
|
} else {
|
|
for (const child of extractedCompositionAssets(contentDoc)) child.remove();
|
|
// linkedom fragment parsing: when content is `<div data-composition-id="X">...</div>`,
|
|
// the div becomes documentElement and body is empty. Fall back to documentElement.outerHTML
|
|
// to preserve the composition wrapper.
|
|
const bodyHtml = contentDoc.body?.innerHTML || "";
|
|
hostEl.innerHTML = bodyHtml || contentDoc.documentElement?.outerHTML || "";
|
|
}
|
|
for (const el of plan.inertScriptsOutsideRoot)
|
|
hostEl.insertAdjacentHTML("beforeend", el.outerHTML);
|
|
|
|
hostEl.setAttribute("data-composition-file", src);
|
|
hostEl.removeAttribute("data-composition-src");
|
|
|
|
const nestedAncestry = [...ancestry, src];
|
|
const nested = enumerateNestedCompositionHosts(hostEl, nestedAncestry);
|
|
for (const skipped of nested.skipped) {
|
|
onMissingComposition?.(skipped.src, skipped.reason);
|
|
}
|
|
for (const nestedHost of nested.hosts) {
|
|
queue.push({ element: nestedHost.host, ancestry: nestedAncestry, scene });
|
|
}
|
|
|
|
// Remember this instance for the SVG id pass below. Keyed on the
|
|
// document-unique runtime id (falling back to the authored id for an
|
|
// anonymous host with no duplicate instances) — the same identity CSS
|
|
// scoping and script scoping already key on. Nested hosts are excluded
|
|
// because each of them is recorded as its own scope when dequeued.
|
|
svgIdScopes.push({
|
|
src,
|
|
root: hostEl,
|
|
namespace: runtimeCompId || scopeCompId,
|
|
exclude: nested.hosts.map((nestedHost) => nestedHost.host),
|
|
styleStart,
|
|
styleEnd,
|
|
});
|
|
}
|
|
|
|
// SVG ids (`<clipPath id="clip">`, `<symbol id="shape">`, `<filter
|
|
// id="fx">`, …) are namespaced only once EVERY instance is in the document:
|
|
// whether an id collides is a property of the assembled document, not of one
|
|
// composition file, and an id that stays unique must stay untouched so the
|
|
// author's own `#id` lookups keep working. Each instance's extracted
|
|
// `<style>` text gets the same substitution its DOM just received. See
|
|
// svgIdNamespacing.ts for why this is a rename, unlike the sibling
|
|
// getElementById/media-id fixes.
|
|
dropAuthoredSvgReferenceAliases(document);
|
|
const hostRoots = svgIdScopes.map(({ root }) => root);
|
|
const clips = sceneClipRoots(document, hostRoots);
|
|
const svgIdMaps = namespaceCollidingSvgIds(
|
|
document,
|
|
[
|
|
...svgIdScopes.map(({ root, namespace, exclude, styleStart, styleEnd }) => ({
|
|
root,
|
|
namespace,
|
|
exclude,
|
|
cssTexts: styles.slice(styleStart, styleEnd).map((style) => style.css),
|
|
})),
|
|
{
|
|
root: document.documentElement,
|
|
namespace: "",
|
|
referenceTarget: "document",
|
|
exclude: [...hostRoots, ...clips],
|
|
cssTexts: rootStyles.map((style) => style.textContent ?? ""),
|
|
},
|
|
...clips.map((root, index) => ({
|
|
root,
|
|
namespace: `clip-${index}`,
|
|
exclude: hostRoots.filter((host) => root.contains(host)),
|
|
})),
|
|
],
|
|
undefined,
|
|
chargeEmitted &&
|
|
((index, bytes) => chargeEmitted(svgIdScopes[index]?.src ?? "the page", bytes)),
|
|
);
|
|
const rewriteCss = (
|
|
src: string,
|
|
css: string,
|
|
idMap: SvgIdMap,
|
|
references: readonly SvgReferenceAlias[],
|
|
) =>
|
|
rewriteSvgIdReferencesInCss(
|
|
css,
|
|
idMap,
|
|
references,
|
|
chargeEmitted && ((bytes) => chargeEmitted(src, bytes)),
|
|
);
|
|
svgIdMaps.slice(0, svgIdScopes.length).forEach((idMap, index) => {
|
|
const { styleStart, styleEnd } = svgIdScopes[index]!;
|
|
const references = readSvgReferenceAliases(
|
|
svgIdScopes[index]!.root,
|
|
SVG_REFERENCE_ALIASES_ATTR,
|
|
);
|
|
for (let i = styleStart; i < styleEnd; i += 1) {
|
|
styles[i]!.css = rewriteCss(svgIdScopes[index]!.src, styles[i]!.css, idMap, references);
|
|
}
|
|
});
|
|
|
|
const rootReferences = readSvgReferenceAliases(
|
|
document.documentElement,
|
|
SVG_REFERENCE_ALIASES_ATTR,
|
|
);
|
|
for (const style of rootStyles) {
|
|
const css = style.textContent ?? "";
|
|
style.textContent = rewriteCss("the page", css, svgIdMaps[svgIdScopes.length]!, rootReferences);
|
|
}
|
|
|
|
return {
|
|
styles,
|
|
styleScenes,
|
|
scripts,
|
|
externalScriptSrcs,
|
|
scriptItems,
|
|
externalLinks,
|
|
variablesByComp,
|
|
importMaps,
|
|
moduleScripts,
|
|
};
|
|
}
|