import { existsSync, readFileSync, statSync } from "node:fs";
import { join } from "node:path";
import { parseHTML } from "@hyperframes/parsers/linkedom";
import { firstHtmlTemplate } from "@hyperframes/parsers/sub-composition-validity";
import { decodedUrlPath } from "@hyperframes/parsers/asset-paths";
import {
rewriteAssetPaths,
rewriteCssAssetUrls,
rewriteInlineStyleAssetUrls,
} from "@hyperframes/core";
import {
deferScriptsUntilFonts,
dropAuthoredCompositionFiles,
RUNTIME_BOOTSTRAP_ATTR,
stripEmbeddedRuntimeScripts,
} from "@hyperframes/core/compiler";
import { findStartTags, isFullHtmlDocument } from "@hyperframes/core/compiler/html-document";
import { gsapCdnDist } from "@hyperframes/core/gsap-cdn";
import { resolveWithinProject } from "@hyperframes/core/safe-path";
import {
assertCompositionFileSize,
CompositionExpansionBudget,
} from "@hyperframes/core/compiler/composition-size-limit";
import { TIMELINE_REGISTRY_SCRIPT } from "./authoredCode.js";
import { htmlHasGsapCore, insertGsapCoreBeforeHeadPlugin } from "./gsapCoreScript.js";
import { withRootPathsRelative } from "./rootPaths.js";
import { isCompositionPath, isCompositionSource } from "./hfIdPersist.js";
/**
* Rewrite relative asset paths in a parsed DOM tree. Shared across all
* three dispatch branches (template, full-doc, fragment) to avoid drift.
*
* The preview page borrows the project-root ``, so a composition at
* `design/styleframes/frame-01.html` referencing its sibling `_shared.css` was
* requested as `/preview/_shared.css` (404). The frame then rendered unstyled,
* which the thumbnailer's transparent-body fallback painted dark navy — the
* "illegible styleframe thumbnail" bug. The `assetExists` probe re-points such
* refs at the composition's own directory; see `rewriteAssetPath`.
*/
function rewriteRelativePaths(root: ParentNode, compPath: string, projectDir: string): void {
const assetExists = (path: string) => existsSync(join(projectDir, path));
rewriteAssetPaths(
root.querySelectorAll("[src], [href], [srcset]"),
compPath,
(el: Element, attr: string) => el.getAttribute(attr),
(el: Element, attr: string, value: string) => el.setAttribute(attr, value),
assetExists,
);
rewriteInlineStyleAssetUrls(
root.querySelectorAll("[style]"),
compPath,
(el: Element) => el.getAttribute("style"),
(el: Element, value: string) => el.setAttribute("style", value),
assetExists,
);
for (const styleEl of root.querySelectorAll("style")) {
styleEl.textContent = rewriteCssAssetUrls(styleEl.textContent || "", compPath, assetExists);
}
}
/**
* Escape a CSS identifier whose first character is a digit so it is a valid
* selector. A CSS ident cannot start with a digit, so it must be written as an
* escaped code point: `01-foo` → `\30 1-foo` (leading `0` → `\30 `, rest kept).
*
* Only the leading digit needs escaping (per CSS Syntax Level 3 §4.3.11): once
* the parser consumes the `\ ` escape, the rest of the ident continues
* normally, so `123-scene` → `\31 23-scene` is valid (the `23-scene` tail is
* consumed as identifier continuation). The trailing space terminates the hex
* escape so a following hex digit isn't folded into the code point.
*/
function escapeLeadingDigitIdent(id: string): string {
return `\\${id.charCodeAt(0).toString(16)} ${id.slice(1)}`;
}
const REGEXP_SPECIALS = /[.*+?^${}()|[\]\\]/g;
/**
* Fix `#` selectors in the tree's `