A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.
TypeScript
Show a placeholder for remote images in the draft instead of a broken icon
Esteban Suareza8dab1c5bda7
diff --git a/lib/draft-preview.ts b/lib/draft-preview.tsindex 619ebbe..44c0b39 100644--- a/lib/draft-preview.ts+++ b/lib/draft-preview.ts@@ -28,7 +28,12 @@ function rootAttributes(html: string): Map<string, string> {return roots}+// A remote image has no network in the draft; it shows this soft 16:9 block until the+// published design embeds the real one, never the browser's broken-image icon.+const DRAFT_IMAGE_PLACEHOLDER = `data:image/svg+xml,${encodeURIComponent('<svg xmlns="http://www.w3.org/2000/svg" width="1600" height="900"><rect width="100%" height="100%" fill="#808080" fill-opacity="0.15"/></svg>')}`+function clean(element: Element) {+ const image = element.localName === 'img'for (const attribute of [...element.attributes]) {const name = attribute.name.toLowerCase()const value = attribute.value.trim()@@ -38,6 +43,7 @@ function clean(element: Element) {element.removeAttribute(attribute.name)}}+ if (image && !element.hasAttribute('src')) element.setAttribute('src', DRAFT_IMAGE_PLACEHOLDER)}/** Model markup cannot execute scripts or fetch resources while being cleaned. */diff --git a/tests/design-artifact.browser.mjs b/tests/design-artifact.browser.mjsindex 74582d9..6a34254 100644--- a/tests/design-artifact.browser.mjs+++ b/tests/design-artifact.browser.mjs@@ -174,3 +174,38 @@ for (const name of ['small', 'large']) {await rm(dir, { recursive: true, force: true })}})++// The draft has no network, so a remote image must show the soft placeholder, never+// the browser's broken-image icon. The draft cleaner runs in Chrome, types stripped.+test('in Chrome, a draft shows a placeholder for remote images instead of a broken icon', async () => {+ assert.ok(existsSync(CHROME), `Chrome not found at ${CHROME}; set CHROME_PATH`)+ const { stripTypeScriptTypes } = await import('node:module')+ const source = await (await import('node:fs/promises')).readFile(new URL('../lib/draft-preview.ts', import.meta.url), 'utf8')+ const cleaner = stripTypeScriptTypes(source).replace(/^export /gm, '')+ const draft = `<!doctype html><html><head></head><body>+<img id="remote" alt="Bistro dining room" src="https://images.unsplash.com/photo-1517248135467-4c7edcad34c4?w=1600">+<img id="srcset" alt="Kitchen" srcset="https://example.com/a.png 1x, https://example.com/b.png 2x">+<img id="embedded" alt="" src="data:image/png;base64,${PIXEL}">+</body></html>`+ const dir = await mkdtemp(join(tmpdir(), 'fx0-draft-'))+ try {+ const file = join(dir, 'page.html')+ await writeFile(file, `<!doctype html><html><head><title>draft</title></head><body><pre id="out">{}</pre><script>${cleaner}+const frame = document.createElement('iframe')+frame.srcdoc = draftDocument(${JSON.stringify(draft)})+frame.onload = () => setTimeout(() => {+ const out = {}+ for (const image of frame.contentDocument.images) out[image.id] = image.complete && image.naturalWidth > 0 ? (image.src.startsWith('data:image/svg+xml') ? 'placeholder' : 'image') : 'broken'+ document.getElementById('out').textContent = JSON.stringify(out)+}, 200)+document.body.append(frame)+</script></body></html>`)+ const { stdout } = await promisify(execFile)(CHROME, [+ '--headless=new', '--disable-gpu', '--no-first-run', '--no-default-browser-check',+ `--user-data-dir=${join(dir, 'profile')}`, '--virtual-time-budget=3000', '--dump-dom', pathToFileURL(file).href,+ ], { timeout: 60_000, maxBuffer: 16 * 1024 * 1024 })+ assert.deepEqual(JSON.parse(stdout.match(/<pre id="out">([^<]*)<\/pre>/)[1]), { remote: 'placeholder', srcset: 'placeholder', embedded: 'image' })+ } finally {+ await rm(dir, { recursive: true, force: true })+ }+})