// 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 = `` // wkui bridge: lets a generated UI (artifact HTML) send one-line prompts into // the chat and make read-only data requests, without ever receiving // credentials. The parent side (document-preview.vue) only accepts messages // whose `source` is this exact iframe, so a hostile page gains nothing by // including the bridge — it can only talk about its own session's artifacts. const bridgeBootstrap = `` /** 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 + bridgeBootstrap + html.slice(prefix.length) }