The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(webapp): put the srcdoc base inside the page's own head so it keeps its attributes
unsupportedpastels61d7cb24ec21
diff --git a/apps/desktop/src/lib/browser-desktop-bridge.test.ts b/apps/desktop/src/lib/browser-desktop-bridge.test.tsindex ecd811910ae..850d796d70d 100644--- a/apps/desktop/src/lib/browser-desktop-bridge.test.ts+++ b/apps/desktop/src/lib/browser-desktop-bridge.test.ts@@ -841,6 +841,55 @@ describe('browser-hosted Desktop bridge', () => {expect(await srcdocOf(based)).toBe(based)})+ it('puts the about:srcdoc base inside the page\'s own <head>, so the head keeps its attributes', async () => {+ const win = mutableWindow()+ seedWebappSession('served-token')+ const blobs: Blob[] = []++ vi.spyOn(URL, 'createObjectURL').mockImplementation(blob => {+ blobs.push(blob as Blob)++ return `blob:http://127.0.0.1:9119/page-${blobs.length}`+ })++ expect(installBrowserDesktopBridge()).toBe(true)++ const parsedSrcdocOf = async (page: string) => {+ await win.hermesDesktop!.saveImageBuffer(new TextEncoder().encode(page), '.html')+ const wrapper = new DOMParser().parseFromString(await blobs.at(-1)!.text(), 'text/html')+ const srcdoc = wrapper.querySelector('iframe')!.getAttribute('srcdoc')!++ return { doc: new DOMParser().parseFromString(srcdoc, 'text/html'), srcdoc }+ }++ // A base before <head> would open an implicit head, and the authored tag's attributes would be lost.+ const report = '<!DOCTYPE html><HTML lang="en"><Head id="report-assets" data-x="1"><title>r</title></Head><body><a href="#sec">x</a></body></HTML>'+ const withHead = await parsedSrcdocOf(report)+ expect(withHead.srcdoc).toBe(report.replace('data-x="1">', 'data-x="1"><base href="about:srcdoc">'))+ expect(withHead.doc.compatMode).toBe('CSS1Compat')+ expect(withHead.doc.head.id).toBe('report-assets')+ expect(withHead.doc.head.dataset.x).toBe('1')+ expect(withHead.doc.querySelectorAll('base')).toHaveLength(1)+ expect(withHead.doc.querySelector('base')!.parentElement).toBe(withHead.doc.head)+ expect(withHead.doc.querySelector('base')!.getAttribute('href')).toBe('about:srcdoc')++ // Without a <head>, the base goes right after <html>, which keeps its attributes too.+ const headless = '<!doctype html><html lang="fr" data-theme="dark"><body><p>x</p></body></html>'+ const withHtml = await parsedSrcdocOf(headless)+ expect(withHtml.srcdoc).toBe(headless.replace('data-theme="dark">', 'data-theme="dark"><base href="about:srcdoc">'))+ expect(withHtml.doc.documentElement.lang).toBe('fr')+ expect(withHtml.doc.documentElement.dataset.theme).toBe('dark')+ expect(withHtml.doc.querySelectorAll('base')).toHaveLength(1)+ expect(withHtml.doc.querySelector('base')!.parentElement).toBe(withHtml.doc.head)++ // <header> is not a <head>: the base stays after the doctype.+ const banner = '<!doctype html><header id="top"><h1>t</h1></header><p>x</p>'+ const withHeader = await parsedSrcdocOf(banner)+ expect(withHeader.srcdoc).toBe(banner.replace('<!doctype html>', '<!doctype html><base href="about:srcdoc">'))+ expect(withHeader.doc.querySelectorAll('base')).toHaveLength(1)+ expect(withHeader.doc.querySelector('header')!.id).toBe('top')+ })+it('stages non-image buffers as browser-local object URLs', async () => {const win = mutableWindow()seedWebappSession('served-token')diff --git a/apps/desktop/src/lib/browser-uploads.ts b/apps/desktop/src/lib/browser-uploads.tsindex 458fdcce1be..e6325db8c9c 100644--- a/apps/desktop/src/lib/browser-uploads.ts+++ b/apps/desktop/src/lib/browser-uploads.ts@@ -51,20 +51,26 @@ function rememberBounded<K, V>(cache: Map<K, V>, key: K, value: V): void {// Decoded as the data URL's charset=utf-8 did, and escaped for a double-quoted attribute (`&`// first) so no byte of the page parses as markup of this same-origin wrapper.// A srcdoc document inherits the wrapper's blob: base URL, so `#anchor` links would resolve to-// blob:…#anchor and do nothing. A page without its own <base> gets about:srcdoc, placed after-// any doctype so the page keeps its rendering mode.+// blob:…#anchor and do nothing. A page without its own <base> gets about:srcdoc, placed inside+// the page's own <head> start tag (else its <html> tag, else after any doctype): a base before+// <head> opens an implicit head, and the parser then drops the authored tag and its attributes.+// After the doctype, the page keeps its rendering mode.const SRCDOC_BASE = '<base href="about:srcdoc">'const LEADING_DOCTYPE = /^\s*<!doctype[^>]*>/iconst BASE_ELEMENT = /<base[\s/>]/i+const START_TAG_REST = String.raw`(?=[\s/>])(?:"[^"]*"|'[^']*'|[^'">])*>`+const HEAD_START = new RegExp(`<head${START_TAG_REST}`, 'i')+const HTML_START = new RegExp(`<html${START_TAG_REST}`, 'i')function withSrcdocBase(page: string): string {if (BASE_ELEMENT.test(page)) {return page}- const doctype = LEADING_DOCTYPE.exec(page)?.[0] ?? ''+ const anchor = HEAD_START.exec(page) ?? HTML_START.exec(page) ?? LEADING_DOCTYPE.exec(page)+ const at = anchor ? anchor.index + anchor[0].length : 0- return doctype + SRCDOC_BASE + page.slice(doctype.length)+ return page.slice(0, at) + SRCDOC_BASE + page.slice(at)}function sandboxedHtmlBlob(bytes: Uint8Array): Blob {