// This is a standalone script: it runs inside the opaque-origin iframe, never // in the application window. Keep it independent of imports and bundler helpers. // Each frame owns two ephemeral stores, discarded when the frame is reloaded. const storageBootstrap = `` /** Add compatibility only to the rendered copy, preserving the original source. */ export function buildHtmlPreview(html: string): string { // Keep the doctype first (standards mode), and use an existing head when // present. Only inspect the document prefix: text resembling tags inside // comments, scripts or the body must never become an insertion point. const trivia = String.raw`(?:\s|)*` const attributes = String.raw`(?:[^<>"']|"[^"]*"|'[^']*')*` const prefix = new RegExp( `^${trivia}(?:${trivia})?(?:${trivia})?(?:)?`, 'i', ).exec(html)?.[0] ?? '' return prefix + storageBootstrap + html.slice(prefix.length) }