A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.
TypeScript
Keep preview preparation linear in the design's size
Esteban Suarez4e78942d8a9a
diff --git a/lib/design-artifact.ts b/lib/design-artifact.tsindex 394d0e2..8922822 100644--- a/lib/design-artifact.ts+++ b/lib/design-artifact.ts@@ -332,6 +332,7 @@ function validateTag(tag: HtmlTag, remote: boolean): string | null {export function rewriteRemoteImages(html: string, embed: (url: string) => string): string {const edits: (SourceRange & { text: string })[] = []const image = (url: string) => embeddedImage(embed(url))+ const lower = html.toLowerCase()for (const tag of parseOpeningTags(html)) {for (const [name, range] of tag.valueRanges) {const raw = tag.attributes.get(name)!@@ -345,11 +346,11 @@ export function rewriteRemoteImages(html: string, embed: (url: string) => string} else if (name === 'style') {const quoted = html[range.start] === '"' || html[range.start] === "'"const inner = quoted ? { start: range.start + 1, end: range.end - 1 } : range- for (const edit of cssUrlEdits(html, inner, true)) edits.push({ ...edit, text: `url(${image(edit.url)})` })+ for (const edit of cssUrlEdits(html, lower, inner, true)) edits.push({ ...edit, text: `url(${image(edit.url)})` })}}if (tag.content) {- for (const edit of cssUrlEdits(html, tag.content, false)) edits.push({ ...edit, text: `url(${image(edit.url)})` })+ for (const edit of cssUrlEdits(html, lower, tag.content, false)) edits.push({ ...edit, text: `url(${image(edit.url)})` })}}// One pass, so the cost grows with the output, not with the number of edits.@@ -385,10 +386,20 @@ const STYLE_QUOTES = ['"', "'"]// The url() tokens in a CSS range whose argument is a remote image URL, skipping// comments and strings. An argument with a CSS escape is left alone (and stays blocked).-function cssUrlEdits(source: string, range: SourceRange, inAttribute: boolean): (SourceRange & { url: string })[] {+// `lower` is the whole source lowercased once by the caller; every scan stays inside `range`.+function cssUrlEdits(source: string, lower: string, range: SourceRange, inAttribute: boolean): (SourceRange & { url: string })[] {const quotes = inAttribute ? ATTRIBUTE_QUOTES : STYLE_QUOTES- const lower = source.toLowerCase()const quoteAt = (index: number) => quotes.find((quote) => lower.startsWith(quote, index))+ // The next ")" at or after a position, remembered: positions only grow, so the+ // whole range is searched once however many url( tokens never close.+ let nextClose = -1+ const closeAfter = (index: number) => {+ if (nextClose < index) {+ nextClose = source.indexOf(')', index)+ if (nextClose < 0 || nextClose >= range.end) nextClose = Number.POSITIVE_INFINITY+ }+ return nextClose+ }const edits: (SourceRange & { url: string })[] = []let index = range.startwhile (index < range.end) {@@ -403,7 +414,7 @@ function cssUrlEdits(source: string, range: SourceRange, inAttribute: boolean):continue}if (lower.startsWith('url(', index) && !/[\w-]/.test(lower[index - 1] ?? '')) {- const token = cssUrlToken(source, lower, index + 4, range.end, quoteAt)+ const token = cssUrlToken(source, lower, index + 4, range.end, quoteAt, closeAfter)if (token) {const value = (inAttribute ? decodeAttribute(token.value) : token.value).trim()const url = value.includes('\\') ? null : remoteImageUrl(value)@@ -426,7 +437,7 @@ function stringEnd(lower: string, index: number, quote: string, limit: number):return limit}-function cssUrlToken(source: string, lower: string, index: number, limit: number, quoteAt: (index: number) => string | undefined) {+function cssUrlToken(source: string, lower: string, index: number, limit: number, quoteAt: (index: number) => string | undefined, closeAfter: (index: number) => number) {while (index < limit && /\s/.test(source[index])) index++const quote = quoteAt(index)let value: string@@ -436,8 +447,8 @@ function cssUrlToken(source: string, lower: string, index: number, limit: numberindex = closewhile (index < limit && /\s/.test(source[index])) index++} else {- const close = source.indexOf(')', index)- if (close < 0 || close >= limit) return null+ const close = closeAfter(index)+ if (close >= limit) return nullvalue = source.slice(index, close)index = close}diff --git a/tests/remote-image.test.mjs b/tests/remote-image.test.mjsindex bdf2dab..80fc3e7 100644--- a/tests/remote-image.test.mjs+++ b/tests/remote-image.test.mjs@@ -4,7 +4,7 @@ import https from 'node:https'import dns from 'node:dns/promises'import { Readable } from 'node:stream'import { EventEmitter } from 'node:events'-import { BLANK_IMAGE, DESIGN_ARTIFACT_EMBEDDED_CSP, remoteImageUrl, rewriteRemoteImages, validateDesignArtifactHtml } from '../lib/design-artifact.ts'+import { BLANK_IMAGE, DESIGN_ARTIFACT_EMBEDDED_CSP, MAX_DESIGN_ARTIFACT_BYTES, remoteImageUrl, rewriteRemoteImages, validateDesignArtifactHtml } from '../lib/design-artifact.ts'import { embedRemoteImages, loadRemoteImage, MAX_REMOTE_IMAGES, MAX_REMOTE_IMAGES_TOTAL_BYTES } from '../lib/remote-image.ts'import { sniffImageType } from '../lib/image-sniff.ts'import { MAX_IMAGE_FETCH_BYTES } from '../lib/public-fetch.ts'@@ -109,6 +109,21 @@ test('the size budget counts every place an image is written, so repeating one cassert.ok(performance.now() - started < 2_000, 'rewriting grows with the output, not with the number of edits')})+test('preparing a design stays linear in its size, however its CSS is shaped', () => {+ const fill = (unit) => unit.repeat(Math.floor((MAX_DESIGN_ARTIFACT_BYTES - 200) / unit.length))+ for (const [name, html] of [+ ['many style attributes', doc(fill('<b style="a">x</b>'))],+ ['unclosed url( in style attributes', doc(fill('<b style="url(">'))],+ ['unclosed url( in a style block', doc('', `<style>${fill('url(')}</style>`)],+ ['unclosed quoted url( in a style block', doc('', `<style>${fill('url("')}</style>`)],+ ]) {+ assert.equal(validateDesignArtifactHtml(html).ok, true, name)+ const started = performance.now()+ rewriteRemoteImages(html, () => PNG_URL)+ assert.ok(performance.now() - started < 500, `${name}: ${Math.round(performance.now() - started)} ms`)+ }+})+test('a failed image is blank and the copy is marked incomplete; a design without remote images is untouched', async () => {const html = doc('<img src="https://example.com/ok.png"><img src="https://example.com/down.png">')const result = await embedRemoteImages(html, async (url) => { if (url.endsWith('down.png')) throw new Error('down'); return PNG_URL })