fix(sdk): align template script traversal

This commit is contained in:
Vance Ingalls
2026-07-10 17:36:09 -07:00
parent 46602d75f4
commit a61f7de8d7
7 changed files with 186 additions and 72 deletions
+14 -22
View File
@@ -6,7 +6,11 @@
*/
import { parseHTML } from "linkedom";
import { ensureHfIds, isCompositionTemplate } from "@hyperframes/core/hf-ids";
import {
ensureHfIds,
isCompositionTemplate,
walkCompositionDescendants,
} from "@hyperframes/core/hf-ids";
export interface ParsedDocument {
document: Document;
@@ -374,38 +378,26 @@ export function setStyleSheet(document: Document, css: string): void {
function findScriptElementsDeep(document: Document): Element[] {
const scripts: Element[] = [];
const walk = (parent: Element): void => {
for (const child of Array.from(parent.children)) {
const tag = child.tagName.toLowerCase();
if (tag === "script") {
scripts.push(child);
continue;
}
if (tag === "template") {
if (isCompositionTemplate(child)) walk(child);
continue;
}
walk(child);
}
};
if (document.documentElement) walk(document.documentElement);
walkCompositionDescendants(document, (child) => {
if (child.tagName.toLowerCase() === "script") scripts.push(child);
});
return scripts;
}
function isGsapScriptText(text: string): boolean {
return text.includes("gsap") || text.includes("__timelines") || text.includes("ScrollTrigger");
}
export function getGsapScripts(document: Document): string[] {
return findScriptElementsDeep(document)
.map((script) => script.textContent ?? "")
.filter(
(text) =>
text.includes("gsap") || text.includes("__timelines") || text.includes("ScrollTrigger"),
);
.filter(isGsapScriptText);
}
function findGsapScriptElement(document: Document): Element | null {
for (const script of findScriptElementsDeep(document)) {
const text = script.textContent ?? "";
if (text.includes("gsap") || text.includes("__timelines") || text.includes("ScrollTrigger"))
return script as unknown as Element;
if (isGsapScriptText(text)) return script;
}
return null;
}