Skip to content

Write HTML. Render video. Built for agents.

14.3 KB
Raw Blame
import { acceleratedCanvasTrackerSource } from "../pageMotionBlur.js";
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|<!--(?:>|->|[\s\S]*?-->))*/;

export function isFullHtmlDocument(html: string): boolean {
  return /^(?:<!doctype|<html[\s>/])/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(`<!DOCTYPE html><html><head></head><body>${html}</body></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()),
  );
}

export function stripEmbeddedRuntimeScripts(html: string): string {
  if (!html) return html;
  const loweredHtml = lowerAscii(html);
  let output = "";
  let cursor = 0;

  while (cursor < html.length) {
    const scriptStart = findScriptStart(loweredHtml, 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(loweredHtml, 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(loweredHtml: string, from: number): number {
  let index = loweredHtml.indexOf("<script", from);
  while (index !== -1) {
    const next = loweredHtml[index + "<script".length] ?? "";
    if (isTagBoundary(next)) return index;
    index = loweredHtml.indexOf("<script", index + 1);
  }
  return -1;
}

type TagState = "tagName" | "between" | "name" | "equals" | "value";

function findTagEnd(html: string, from: number): number {
  let quote: string | undefined;
  let state: TagState = "tagName";
  for (let index = from; index < html.length; index += 1) {
    const char = html.charAt(index);
    if (quote) {
      quote = char === quote ? undefined : quote;
      continue;
    }
    if (char === ">") return index;
    if (state === "equals" && (char === '"' || char === "'")) {
      quote = char;
      state = "between";
      continue;
    }
    state = nextTagState(state, char, index === from);
  }
  return -1;
}

function nextTagState(state: TagState, char: string, first: boolean): TagState {
  if (isHtmlWhitespace(char)) return stateAfterWhitespace(state);
  if (char === "/" && !first) return stateAfterSlash(state);
  if (char === "=" && state === "name") return "equals";
  if (state === "equals") return "value";
  return state === "between" ? "name" : state;
}

function stateAfterWhitespace(state: TagState): TagState {
  return state === "tagName" || state === "value" ? "between" : state;
}

function stateAfterSlash(state: TagState): TagState {
  return state === "equals" || state === "value" ? "value" : "between";
}

function findScriptCloseTagEnd(loweredHtml: string, from: number): number {
  let index = loweredHtml.indexOf("</script", from);
  while (index !== -1) {
    const closeTagEnd = findScriptCloseTagBoundary(loweredHtml, index + "</script".length);
    if (closeTagEnd !== -1) return closeTagEnd;
    index = loweredHtml.indexOf("</script", index + 1);
  }
  return -1;
}

function findScriptCloseTagBoundary(loweredHtml: string, from: number): number {
  let cursor = from;
  while (cursor < loweredHtml.length && isHtmlWhitespace(loweredHtml[cursor] ?? "")) {
    cursor += 1;
  }
  return loweredHtml[cursor] === ">" ? cursor + 1 : -1;
}

function shouldStripRuntimeScriptBlock(block: string, startTag: string): boolean {
  const script = parseHTML(`${startTag}</script>`).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 loweredBlock = lowerAscii(block);
  const closeTagStart = loweredBlock.lastIndexOf("</script");
  const end = closeTagStart === -1 ? block.length : closeTagStart;
  return block.slice(startTagEnd + 1, end);
}

function isTagBoundary(char: string): boolean {
  return char === "" || char === ">" || 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, "</script", (match) => `<\\/${match.slice(2)}`),
    "<!--",
    () => "\\x3C!--",
  );
}

function escapeCaseInsensitiveToken(
  source: string,
  token: string,
  replacement: (match: string) => string,
): string {
  const loweredSource = lowerAscii(source);
  const loweredToken = lowerAscii(token);
  let output = "";
  let cursor = 0;

  while (cursor < source.length) {
    const tokenStart = loweredSource.indexOf(loweredToken, cursor);
    if (tokenStart === -1) {
      output += source.slice(cursor);
      break;
    }
    output +=
      source.slice(cursor, tokenStart) +
      replacement(source.slice(tokenStart, tokenStart + token.length));
    cursor = tokenStart + token.length;
  }

  return output;
}

function inlineScriptTags(scripts: readonly string[]): string {
  return scripts
    .map((source, index) => `${source}\n//# sourceURL=hyperframes://injected/${index}`)
    .map((source) => `<script>${escapeInlineScriptSource(source)}</script>`)
    .join("\n");
}

const RAW_TEXT_TAGS = ["script", "style", "title", "textarea"] as const;

type DocumentTag = "<head" | "</head" | "<body" | "</body" | "</template";
const COMMENT_END = /--!?>/g;

function* markupStarts(lowered: string): Generator<number> {
  const unclosedRawText = new Set<string>();
  let cursor = 0;
  while (cursor !== -1) {
    const open = lowered.indexOf("<", cursor);
    if (open === -1) return;
    yield open;
    cursor = skipMarkup(lowered, open, unclosedRawText);
  }
}

function findDocumentTag(html: string, tag: DocumentTag): number {
  const lowered = lowerAscii(html);
  for (const open of markupStarts(lowered)) {
    if (isTagAt(lowered, open, tag)) return open;
  }
  return -1;
}

export function findStartTags(html: string, name: string): number[] {
  const lowered = lowerAscii(html);
  const token = `<${lowerAscii(name)}`;
  const starts: number[] = [];
  let templateDepth = 0;
  for (const open of markupStarts(lowered)) {
    if (templateDepth === 0 && isTagAt(lowered, open, token)) starts.push(open);
    if (isTagAt(lowered, open, "<template")) templateDepth++;
    else if (templateDepth > 0 && isTagAt(lowered, open, "</template")) templateDepth--;
  }
  return starts;
}

function isTagAt(lowered: string, at: number, token: string): boolean {
  return lowered.startsWith(token, at) && isTagBoundary(lowered.charAt(at + token.length));
}

/** Cursor just past the markup that starts at `open`, or -1 when the document ends inside it. */
function skipMarkup(lowered: string, open: number, unclosedRawText: Set<string>): number {
  if (lowered.startsWith("<!--", open)) return skipComment(lowered, open);
  const next = lowered.charAt(open + 1);
  if (next === "/" && !/[a-z]/.test(lowered.charAt(open + 2))) {
    const bogusEnd = lowered.indexOf(">", open);
    return bogusEnd === -1 ? -1 : bogusEnd + 1;
  }
  if (!/[a-z/]/.test(next)) return open + 1;
  const tagEnd = findTagEnd(lowered, open + 1);
  return tagEnd === -1 ? -1 : skipRawText(lowered, open, tagEnd, unclosedRawText);
}

function skipComment(lowered: string, open: number): number {
  if (lowered.startsWith("<!-->", open) || lowered.startsWith("<!--->", open)) {
    return lowered.indexOf(">", open) + 1;
  }
  COMMENT_END.lastIndex = open + 4;
  const end = COMMENT_END.exec(lowered);
  return end ? end.index + end[0].length : -1;
}

function skipRawText(
  lowered: string,
  open: number,
  tagEnd: number,
  unclosedRawText: Set<string>,
): number {
  const rawText = RAW_TEXT_TAGS.find((name) => isTagAt(lowered, open, `<${name}`));
  if (!rawText) return tagEnd + 1;
  const close = unclosedRawText.has(rawText) ? -1 : findRawTextClose(lowered, rawText, tagEnd + 1);
  if (close !== -1) return close;
  unclosedRawText.add(rawText);
  return lowered.charAt(tagEnd - 1) === "/" ? tagEnd + 1 : -1;
}

function findRawTextClose(lowered: string, name: string, from: number): number {
  const close = `</${name}`;
  for (let at = lowered.indexOf(close, from); at !== -1; at = lowered.indexOf(close, at + 1)) {
    if (isTagAt(lowered, at, close)) return at;
  }
  return -1;
}

function findOuterTemplateClose(html: string): number {
  const lowered = lowerAscii(html);
  let depth = 0;
  for (const open of markupStarts(lowered)) {
    if (isTagAt(lowered, open, "<template")) depth++;
    else if (isTagAt(lowered, open, "</template") && --depth === 0) return open;
  }
  return -1;
}

const COMPOSITION_ID_ATTR = /\sdata-composition-id\s*=/;

const isDocumentWrapper = (lowered: string, open: number): boolean =>
  isTagAt(lowered, open, "<html") || isTagAt(lowered, open, "<body");

function carriesCompositionId(lowered: string, open: number): boolean {
  const end = findTagEnd(lowered, open + 1);
  return end !== -1 && COMPOSITION_ID_ATTR.test(lowered.slice(open, end));
}

export function hasCompositionOutsideTemplates(html: string): boolean {
  const lowered = lowerAscii(html);
  let depth = 0;
  let idOnWrapper = false;
  let idInTemplate = false;
  for (const open of markupStarts(lowered)) {
    if (isTagAt(lowered, open, "<template")) {
      depth++;
      if (carriesCompositionId(lowered, open)) idInTemplate = true;
    } else if (isTagAt(lowered, open, "</template")) depth = Math.max(0, depth - 1);
    else if (/[a-z]/.test(lowered.charAt(open + 1)) && carriesCompositionId(lowered, open)) {
      if (depth > 0) idInTemplate = true;
      else if (isDocumentWrapper(lowered, open)) idOnWrapper = true;
      else return true;
    }
  }
  return idOnWrapper && !idInTemplate;
}

function insertBeforeDocumentTag(html: string, tag: DocumentTag, markup: string): string | null {
  const at = tag === "</template" ? findOuterTemplateClose(html) : findDocumentTag(html, tag);
  return at === -1 ? null : html.slice(0, at) + markup + html.slice(at);
}

/** Insert markup before the document's own `</head>`, `</body>` or `</template>`; null if it has none. */
export function insertBeforeCloseTag(
  html: string,
  name: "head" | "body" | "template",
  markup: string,
): string | null {
  return insertBeforeDocumentTag(html, `</${name}`, markup);
}

function insertTagInHead(html: string, tag: string): string {
  const withHead = insertBeforeCloseTag(html, "head", `${tag}\n`);
  if (withHead !== null) return withHead;
  const htmlOpenMatch = html.match(/<html\b[^>]*>/i);
  if (htmlOpenMatch?.index != null) {
    const insertPos = htmlOpenMatch.index + htmlOpenMatch[0].length;
    return `${html.slice(0, insertPos)}<head>${tag}</head>${html.slice(insertPos)}`;
  }
  const doctypeIdx = html.toLowerCase().indexOf("<!doctype");
  if (doctypeIdx >= 0) {
    const insertPos = html.indexOf(">", doctypeIdx) + 1;
    return html.slice(0, insertPos) + tag + html.slice(insertPos);
  }
  return tag + html;
}

const ACCEL_TRACKER_ATTR = "data-hf-accel-tracker";
const OPTS_INTO_MOTION_BLUR = /\sdata-motion-blur\s*=\s*["']?on[\s"'>]/i;

export function insertRuntimeTag(html: string, tag: string): string {
  const withRuntime = insertTagInHead(html, tag);
  if (!OPTS_INTO_MOTION_BLUR.test(html) || html.includes(ACCEL_TRACKER_ATTR)) return withRuntime;
  const tracker = `<script ${ACCEL_TRACKER_ATTR}>${acceleratedCanvasTrackerSource()}</script>`;
  return injectTagsAtHeadStart(withRuntime, tracker);
}

/**
 * Insert raw tag markup at the very start of `<head>`, ahead of every author
 * script (inline or external). Falls back to just before `<body>`, then to the
 * top of the document, for fragments that carry neither.
 */
export function injectTagsAtHeadStart(html: string, tags: string): string {
  const headOpen = findDocumentTag(html, "<head");
  const headOpenEnd = headOpen === -1 ? -1 : findTagEnd(html, headOpen + 1);
  if (headOpenEnd !== -1) {
    return `${html.slice(0, headOpenEnd + 1)}\n${tags}${html.slice(headOpenEnd + 1)}`;
  }
  return insertBeforeDocumentTag(html, "<body", `${tags}\n`) ?? `${tags}\n${html}`;
}

export function injectScriptsAtHeadStart(html: string, scripts: readonly string[]): string {
  if (scripts.length === 0) return html;
  return injectTagsAtHeadStart(html, inlineScriptTags(scripts));
}

export function injectScriptsIntoHtml(
  html: string,
  headScripts: readonly string[],
  bodyScripts: readonly string[],
  stripEmbeddedRuntime = true,
): string {
  if (stripEmbeddedRuntime) {
    html = stripEmbeddedRuntimeScripts(html);
  }

  if (headScripts.length > 0) {
    const headTags = inlineScriptTags(headScripts);
    const withHead = insertBeforeCloseTag(html, "head", `${headTags}\n`);
    html =
      withHead ?? insertBeforeDocumentTag(html, "<body", `${headTags}\n`) ?? `${headTags}\n${html}`;
  }

  if (bodyScripts.length > 0) {
    const bodyTags = inlineScriptTags(bodyScripts);
    html = insertBeforeCloseTag(html, "body", `${bodyTags}\n`) ?? `${html}\n${bodyTags}`;
  }

  return html;
}