import { acceleratedCanvasTrackerSource } from "../pageMotionBlur.js"; import { findSceneTemplate, templateContent } from "@hyperframes/parsers/sub-composition-validity"; import { parseHTML } from "linkedom"; export const RUNTIME_BOOTSTRAP_ATTR = "data-hyperframes-preview-runtime"; const RUNTIME_FILES = [ "hyperframe.runtime.iife.js", "hyperframes-runtime.modular.inline.js", "hyperframe-runtime.modular-runtime.inline.js", ]; const SIMPLE_RUNTIME_FLAG_ASSIGNMENTS = [ /^window\.__playerReady\s*=\s*(?:true|false)\s*;?$/, /^window\.__renderReady\s*=\s*(?:true|false)\s*;?$/, ]; const LEADING_COMMENTS = /^(?:\s|))*/; export interface SceneRootLookup { root: Element | null; hasTemplate: boolean; hasLiveRoot: boolean; } export function locateSceneRoot(html: string): SceneRootLookup { const { document } = parseHTML(html); const liveRoot = document.querySelector("[data-composition-id]") as Element | null; const templates = Array.from(document.querySelectorAll("template")); const scene = findSceneTemplate(templates); const sceneRoot = (scene && templateContent(scene)?.querySelector("[data-composition-id]")) ?? null; return { root: sceneRoot, hasTemplate: templates.length > 0, hasLiveRoot: liveRoot !== null, }; } export function isTemplateEntry(html: string): boolean { const { root, hasTemplate, hasLiveRoot } = locateSceneRoot(html); return hasTemplate && root !== null && !hasLiveRoot; } export function isFullHtmlDocument(html: string): boolean { return /^(?:/])/i.test(html.replace(LEADING_COMMENTS, "")); } /** * Parse a full HTML document or wrap a fragment so linkedom consistently puts * fragment content under document.body. */ export function parseHTMLContent(html: string): Document { if (isFullHtmlDocument(html)) { return parseHTML(html).document; } return parseHTML(`
${html}`).document; } /** ASCII-only chunks bound match arrays; starting at uppercase preserves unchanged spans. */ function lowerAscii(text: string): string { return text.replace(/[A-Z][\s\S]{0,65535}/g, (chunk) => chunk.replace(/[A-Z]+/g, (letters) => letters.toLowerCase()), ); } function startsWithLower(text: string, token: string, at: number): boolean { if (at < 0 || at + token.length > text.length) return false; for (let i = 0; i < token.length; i += 1) { const code = text.charCodeAt(at + i); if ((code >= 65 && code <= 90 ? code + 32 : code) !== token.charCodeAt(i)) return false; } return true; } function indexOfLower(text: string, token: string, from: number): number { for ( let at = text.indexOf(token.charAt(0), from); at !== -1; at = text.indexOf(token.charAt(0), at + 1) ) { if (startsWithLower(text, token, at)) return at; } return -1; } function lastIndexOfLower(text: string, token: string): number { for ( let at = text.lastIndexOf(token.charAt(0)); at !== -1; at = at === 0 ? -1 : text.lastIndexOf(token.charAt(0), at - 1) ) { if (startsWithLower(text, token, at)) return at; } return -1; } export function stripEmbeddedRuntimeScripts(html: string): string { if (!html) return html; let output = ""; let cursor = 0; while (cursor < html.length) { const scriptStart = findScriptStart(html, cursor); if (scriptStart === -1) { output += html.slice(cursor); break; } output += html.slice(cursor, scriptStart); const startTagEnd = findTagEnd(html, scriptStart + 1); if (startTagEnd === -1) { output += html.slice(scriptStart); break; } const closeTagEnd = findScriptCloseTagEnd(html, startTagEnd + 1); const scriptEnd = closeTagEnd === -1 ? html.length : closeTagEnd; const block = html.slice(scriptStart, scriptEnd); if (!shouldStripRuntimeScriptBlock(block, html.slice(scriptStart, startTagEnd + 1))) { output += block; } cursor = scriptEnd; } return output; } function findScriptStart(html: string, from: number): number { let index = indexOfLower(html, "" ? cursor + 1 : -1; } function shouldStripRuntimeScriptBlock(block: string, startTag: string): boolean { const script = parseHTML(`${startTag}`).document.querySelector("script"); if ( script?.hasAttribute(RUNTIME_BOOTSTRAP_ATTR) || isRuntimeFileUrl(script?.getAttribute("src")) ) { return true; } const scriptSource = getScriptSource(block).trim(); return SIMPLE_RUNTIME_FLAG_ASSIGNMENTS.some((pattern) => pattern.test(scriptSource)); } export function isRuntimeFileUrl(src: string | null | undefined): boolean { const path = lowerAscii(src?.split(/[?#]/, 1)[0] ?? ""); return RUNTIME_FILES.some((file) => path === file || path.endsWith(`/${file}`)); } function getScriptSource(block: string): string { const startTagEnd = findTagEnd(block, 1); if (startTagEnd === -1) return ""; const closeTagStart = lastIndexOfLower(block, "" || char === "/" || isHtmlWhitespace(char); } function isHtmlWhitespace(char: string): boolean { return char === " " || char === "\n" || char === "\t" || char === "\r" || char === "\f"; } export function escapeInlineScriptSource(source: string): string { return escapeCaseInsensitiveToken( escapeCaseInsensitiveToken(source, " `<\\/${match.slice(2)}`), "", open) || html.startsWith("", open)) { return html.indexOf(">", open) + 1; } COMMENT_END.lastIndex = open + 4; const end = COMMENT_END.exec(html); return end ? end.index + end[0].length : -1; } function skipRawText( html: string, open: number, tagEnd: number, unclosedRawText: Set