diff --git a/packages/producer/src/services/htmlCompiler.ts b/packages/producer/src/services/htmlCompiler.ts index 77f444502b..951b0a82a5 100644 --- a/packages/producer/src/services/htmlCompiler.ts +++ b/packages/producer/src/services/htmlCompiler.ts @@ -29,6 +29,14 @@ import { createReadStream, existsSync, mkdirSync, readFileSync } from "fs"; import { join, dirname, resolve, basename, relative, sep } from "path"; import { parseHTML } from "linkedom"; import { bundledScript } from "./bundledScripts.js"; +import { + inlineScriptBlocks, + remoteSrcUrls, + removeDelimitedRuns, + replaceGsapDistScripts, + stripCrossoriginAttr, + stripPreloadNone, +} from "./htmlTagScans.js"; import { COMPOSITION_SOURCE_URL, compileTimingAttrs, @@ -393,22 +401,15 @@ export interface RenderModeHints { reasons: RenderModeHint[]; } -const INLINE_SCRIPT_PATTERN = /]*)>([\s\S]*?)<\/script>/gi; const COMPILER_MOUNT_BLOCK_START = "/* __HF_COMPILER_MOUNT_START__ */"; const COMPILER_MOUNT_BLOCK_END = "/* __HF_COMPILER_MOUNT_END__ */"; function stripJsComments(source: string): string { - return source.replace(/\/\/.*$/gm, "").replace(/\/\*[\s\S]*?\*\//g, ""); + return removeDelimitedRuns(source.replace(/\/\/.*$/gm, ""), "/*", "*/"); } function stripCompilerMountBootstrap(source: string): string { - return source.replace( - new RegExp( - `${COMPILER_MOUNT_BLOCK_START.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}[\\s\\S]*?${COMPILER_MOUNT_BLOCK_END.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`, - "g", - ), - "", - ); + return removeDelimitedRuns(source, COMPILER_MOUNT_BLOCK_START, COMPILER_MOUNT_BLOCK_END); } /** The composition the runtime reads data-motion-blur from: `data-root="true"`, else the first. */ @@ -449,12 +450,9 @@ export function detectRenderModeHints(html: string, pageMotionBlur?: "off"): Ren }); } - let scriptMatch: RegExpExecArray | null; - const scriptPattern = new RegExp(INLINE_SCRIPT_PATTERN.source, INLINE_SCRIPT_PATTERN.flags); - while ((scriptMatch = scriptPattern.exec(html)) !== null) { - const attrs = scriptMatch[1] || ""; + for (const { attrs, body } of inlineScriptBlocks(html)) { if (/\bsrc\s*=/i.test(attrs)) continue; - const content = stripJsComments(stripCompilerMountBootstrap(scriptMatch[2] || "")); + const content = stripJsComments(stripCompilerMountBootstrap(body)); if (!/requestAnimationFrame\s*\(/.test(content)) continue; reasons.push({ code: "requestAnimationFrame", @@ -551,12 +549,9 @@ const SHADER_TRANSITION_USAGE_PATTERN = /\b(?:(?:window|globalThis)\s*\.\s*)?HyperShader\s*\.\s*init\s*\(|\b__hf\s*\.\s*transitions\s*=/; export function detectShaderTransitionUsage(html: string): boolean { - let scriptMatch: RegExpExecArray | null; - const scriptPattern = new RegExp(INLINE_SCRIPT_PATTERN.source, INLINE_SCRIPT_PATTERN.flags); - while ((scriptMatch = scriptPattern.exec(html)) !== null) { - const attrs = scriptMatch[1] || ""; + for (const { attrs, body } of inlineScriptBlocks(html)) { if (/\bsrc\s*=/i.test(attrs)) continue; - const content = stripJsComments(stripCompilerMountBootstrap(scriptMatch[2] || "")); + const content = stripJsComments(stripCompilerMountBootstrap(body)); if (SHADER_TRANSITION_USAGE_PATTERN.test(content)) return true; } @@ -809,13 +804,13 @@ async function compileHtmlFile( // injected frame images, so the browser never needs to load the source. // Without this, videos with crossorigin="anonymous" targeting CORS-restricted // origins (e.g. S3 without CORS headers) keep readyState=0, blocking page setup. - compiledHtml = compiledHtml.replace(/(]*)\s+crossorigin(?:=["'][^"']*["'])?/gi, "$1"); + compiledHtml = stripCrossoriginAttr(compiledHtml, "video"); // Strip crossorigin from img elements. The renderer captures DOM frames visually — // no canvas readback — so CORS compliance is unnecessary. External images from // CORS-restricted origins (e.g. S3) render blank when crossorigin forces a failed // CORS request against the renderer's localhost file server. - compiledHtml = compiledHtml.replace(/(]*)\s+crossorigin(?:=["'][^"']*["'])?/gi, "$1"); + compiledHtml = stripCrossoriginAttr(compiledHtml, "img"); // Strip crossorigin from audio elements. Audio is processed out-of-band via // FFmpeg; the browser's CORS policy for audio elements is irrelevant to @@ -824,7 +819,7 @@ async function compileHtmlFile( // headers reject — leaving audio elements in a failed network state. The // FFmpeg audio path reads the src URL directly and is unaffected by browser // CORS, so stripping the attribute has no side effects. - compiledHtml = compiledHtml.replace(/(]*)\s+crossorigin(?:=["'][^"']*["'])?/gi, "$1"); + compiledHtml = stripCrossoriginAttr(compiledHtml, "audio"); return { html: compiledHtml, unresolvedCompositions }; } @@ -1540,22 +1535,6 @@ export function collectExternalAssets( } export const REMOTE_MEDIA_SUBDIR = "_remote_media"; -// Match opening tags of