From 653fece6ab7de7bc161047dbf4ae98a941f07b05 Mon Sep 17 00:00:00 2001 From: Joey Farina <17398284+josephfarina@users.noreply.github.com> Date: Wed, 18 Nov 2026 07:50:22 -0800 Subject: [PATCH 1/3] [experimental] export: the page's script reads each carried image from the first paint A file carried each image its first paint shows twice: as a data: URI in the markup and again in the page's script. The markup now numbers each value it carries (data-astryx-carried, written once per img, source, input or video element after its last attribute), and the script reads those values from the first paint before the page mounts. A local path the script holds as a whole string becomes that read; an https address stays as the page wrote it, and the image sinks read its data from the first paint instead of holding a second copy. Only elements a browser builds carry anything the script reads. The HTML scanner reads raw text, comments, CDATA, tag names, self-closing tags, and svg and MathML content as the HTML tokenizer and tree builder do; noscript, template and select content marks nothing; and the first markup shape a serializer never writes (a mis-nested end tag, CDATA, a bogus comment, a comment holding --!>, a non-void self-closing tag, plaintext, frameset, or a stray tag in a select) stops the marks for the rest of the text, so every later image keeps its own copy. [skip ci] --- .changeset/render-image-once-markup.md | 7 + .../api/render/render.interactive.test.mjs | 373 ++++++++++++++- .../doc-compiler/doc-loads.test.mjs | 4 +- .../cli/foundation/render/client-bundle.mjs | 24 +- .../cli/foundation/render/html-reader.mjs | 264 +++++++++-- .../foundation/render/remote-images.test.mjs | 147 +++++- .../cli/foundation/render/render-page.mjs | 439 +++++++++++++++++- .../cli/foundation/render/render.test.mjs | 120 ++++- 8 files changed, 1314 insertions(+), 64 deletions(-) create mode 100644 .changeset/render-image-once-markup.md diff --git a/.changeset/render-image-once-markup.md b/.changeset/render-image-once-markup.md new file mode 100644 index 0000000000000..d5b86efe08908 --- /dev/null +++ b/.changeset/render-image-once-markup.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/cli': patch +--- + +[experimental] `astryx experimental export`: an exported page's script reads from the first paint an image the first paint shows through `src`, `srcset` or `poster` when the page's code names it as a whole string by that exact path or https address. The file holds each such image one time fewer: a page with one 450 KB local photo goes from about 2.05 MB to 1.45 MB, and an https image the page shows through `src`, `srcset` or `poster` is held once (the same photo by an https address: about 1.46 MB to 0.86 MB). The first paint is the same, also where scripts are blocked. + +@josephfarina diff --git a/packages/cli/api/render/render.interactive.test.mjs b/packages/cli/api/render/render.interactive.test.mjs index b1178f2a50bce..cf6bdede8861c 100644 --- a/packages/cli/api/render/render.interactive.test.mjs +++ b/packages/cli/api/render/render.interactive.test.mjs @@ -25,7 +25,7 @@ import { ESBUILD_MISSING, findEsbuild, } from '../../foundation/render/esbuild-dependency.mjs'; -import {RenderError, renderPage} from '../../foundation/render/render-page.mjs'; +import {CARRY_ATTR, RenderError, renderPage} from '../../foundation/render/render-page.mjs'; import {cliVersion} from '../../foundation/integrations/cli-peer-version.mjs'; import {runCli} from '../../test-utils/run-cli.mjs'; import { @@ -574,7 +574,14 @@ import '@astryxdesign/core/astryx.css'; `, ); await render('src/pictured.tsx', {cwd: dir, out: 'pictured.html'}); - const dom = new JSDOM(read('pictured.html'), { + // The script reads the image from the first paint: the file holds its + // data: URI only where the markup shows it; the embedded source keeps + // its own copy for --reverse. + const html = read('pictured.html'); + const pageScript = html.slice(html.lastIndexOf('\`, + \`tip\`, + \` a > b \${IMG} \`, + \`name\`, + \` a > \${IMG} \`, + '', + \`\`, + \`\`, + \`\`, + \`\${IMG}\`, + \`\`, + \`\`, + \`
\${IMG} ]]>
\`, + \`\`, + \`\`, + \`\`, + \`\`, + \`\`, + \`

\${IMG}\`, + \`
\`, + \`\`, + \`\`, + \`\`, +]; +export default function Page() { + return ( +

+ {SHAPES.map(html =>
)} +

{typeof document === 'undefined' ? '' : Drawn}

+
+ ); +} +import '@astryxdesign/core/astryx.css'; +`, + ); + // Other bytes, so other.png's data differs from photo.png's. + put('src/shapes/other.png', Buffer.concat([fs.readFileSync(path.join(dir, 'src/shapes/photo.png')), Buffer.from([0])])); + put( + 'src/shapes/two-src.tsx', + `const OTHER = './other.png'; +export default function Page() { + return ( +
+
'}} /> +

{typeof document === 'undefined' ? '' : Drawn}

+
+ ); +} +import '@astryxdesign/core/astryx.css'; +`, + ); + put( + 'src/shapes/alt-gt.tsx', + `const PHOTO = './photo.png'; +export default function Page() { + return ( +
+
'}} /> +

{typeof document === 'undefined' ? '' : Drawn}

+
+ ); +} +import '@astryxdesign/core/astryx.css'; +`, + ); + put( + 'src/shapes/only-noscript.tsx', + `const PATH = './photo.png'; +export default function Page() { + return ( +
+ +

{typeof document === 'undefined' ? '' : Drawn}

+
+ ); +} +import '@astryxdesign/core/astryx.css'; +`, + ); + for (const [page, out] of [['src/shapes/page.tsx', 'shapes.html'], ['src/shapes/only-noscript.tsx', 'only-noscript.html'], ['src/shapes/nested-template.tsx', 'nested-template.html'], ['src/shapes/comment-textarea.tsx', 'comment-textarea.html'], ['src/shapes/raw-elements.tsx', 'raw-elements.html'], ['src/shapes/two-src.tsx', 'two-src.html'], ['src/shapes/alt-gt.tsx', 'alt-gt.html']]) { + await render(page, {cwd: dir, out}); + } + /** @param {string} file */ + const drawn = async file => { + const dom = new JSDOM(read(file), {runScripts: 'dangerously', pretendToBeVisual: true}); + await new Promise(resolve => dom.window.setTimeout(resolve, 100)); + const root = /** @type {Element} */ (dom.window.document.getElementById(ROOT_ID)); + const out = { + plain: root.querySelector('img[alt="Plain"]')?.getAttribute('src'), + inner: root.querySelector('[data-k="inner"] img')?.getAttribute('src'), + css: root.querySelector('[data-k="css"]')?.getAttribute('style'), + bare: root.querySelector('[data-k="bare"]')?.getAttribute('style'), + drawn: root.querySelector('img[alt="Drawn"]')?.getAttribute('src'), + }; + dom.window.close(); + return out; + }; + const shapes = await drawn('shapes.html'); + expect(shapes.plain).toMatch(/^data:image\/png;base64,/); + expect(shapes.inner).toMatch(/^data:image\/png;base64,/); + expect(shapes.css).toMatch(/url\("?data:image\/png;base64,/); + expect(shapes.bare).toMatch(/url\("?data:image\/png;base64,/); + // Only the whole string "./photo.png" is read from the first paint. + const script = read('shapes.html').slice(read('shapes.html').lastIndexOf('-->\n` + * inside it ends nothing; `-->` returns to plain script data. + * @param {string} html + * @param {number} from + * @returns {number} + */ +function scriptEnd(html, from) { + const piece = /|<\/?script(?=[\t\n\f\r />])/gi; + piece.lastIndex = from; + /** @type {'data' | 'escaped' | 'double'} */ + let state = 'data'; + for (let m = piece.exec(html); m; m = piece.exec(html)) { + const found = m[0].toLowerCase(); + if (found === '` and `` close at once. + if (state === 'escaped' && /^-?>/.test(html.slice(m.index + 4, m.index + 6))) state = 'data'; + } else if (found === '-->') { + if (state !== 'data') state = 'data'; + } else if (found === '` with a color, face or size. + * @param {string} tag + * @param {Array<[string, string, number, number]>} attrs + */ +const breaksOut = (tag, attrs) => + BREAKOUT.has(tag) || (tag === 'font' && attrs.some(([name]) => name === 'color' || name === 'face' || name === 'size')); + +/** + * The element a start tag opens in svg or MathML content, with the HTML + * integration points (svg foreignObject, desc and title; a MathML + * annotation-xml whose encoding is text/html or application/xhtml+xml) and + * the MathML text integration points marked (HTML §13.2.6). + * @param {'svg' | 'math'} ns + * @param {string} tag + * @param {Array<[string, string, number, number]>} attrs + * @returns {OpenElement} + */ +function foreignElement(ns, tag, attrs) { + const encoding = (attrs.find(([name]) => name === 'encoding')?.[1] ?? '').toLowerCase(); + const htmlPoint = + ns === 'svg' + ? SVG_HTML_POINTS.has(tag) + : tag === 'annotation-xml' && (encoding === 'text/html' || encoding === 'application/xhtml+xml'); + return {ns, tag, htmlPoint, textPoint: ns === 'math' && MATH_TEXT_POINTS.has(tag)}; +} + +/** + * Whether the tree construction dispatcher (HTML §13.2.6) hands a start tag + * to the HTML insertion mode rather than to the rules for foreign content. + * @param {OpenElement | undefined} node the current node + * @param {string} tag + */ +const startIsHtml = (node, tag) => + !node || + node.ns === 'html' || + (node.textPoint && tag !== 'mglyph' && tag !== 'malignmark') || + (node.ns === 'math' && node.tag === 'annotation-xml' && tag === 'svg') || + node.htmlPoint; + +/** + * Close what an end tag closes in svg or MathML content, as the tree builder + * does. In foreign content (HTML §13.2.6.5, "any other end tag"): walk the + * stack from the current node, pop up to a node whose lowercased name is the + * tag's, and at the first HTML element hand the token to the HTML insertion + * mode. There ("any other end tag" in body, §13.2.6.4.7): pop up to a node + * of that name, and stop at a special element, which the tag cannot close. + * @param {OpenElement[]} open + * @param {string} tag + */ +function closeEnd(open, tag) { + let k = open.length - 1; + if (k >= 0 && open[k].ns !== 'html') { + for (; k >= 0 && open[k].ns !== 'html'; k--) { + if (open[k].tag === tag) { + open.length = k; + return; + } + } + } + for (; k >= 0; k--) { + if (open[k].tag === tag) { + open.length = k; + return; + } + const node = open[k]; + if (node.ns === 'html' ? SPECIAL.has(node.tag) : node.htmlPoint || node.textPoint) return; + } +} + /** * Split an HTML document into its pieces, in order, as a browser does for * the markup a render writes. A comment ends at the first `-->` after its - * `` and `` are whole comments), or at the end. The - * content of a raw-text element up to its end tag is one `raw` piece, so a - * tag or a comment in a stylesheet is text. A `<` that starts no tag is + * `` and `` are whole comments), or at the end. A bogus + * comment (``. Inside svg + * or MathML content, ``, `/>` closes a + * tag and no element holds raw text; an integration point (``, + * ``, svg ``, an HTML `<annotation-xml>`, `<mi>`, `<mo>`, `<mn>`, + * `<ms>`, `<mtext>`) and a breakout tag (`<p>`, `<div>`…) return to HTML, + * and end tags close what the tree builder closes. The content + * of a raw-text element up to its end tag is one `raw` piece, so a tag or a + * comment in a stylesheet is text; `<plaintext>` makes the rest of the + * document one `raw` piece. A `<` that starts no tag is * text, and a tag that no `>` ends makes the rest text. Each character is * read a fixed number of times, so the time grows with the document's length. * @param {string} html @@ -117,28 +275,62 @@ export function* scanHtml(html) { const n = html.length; let at = 0; let text = 0; + // The open elements of svg or MathML content, as the tree builder keeps + // them (HTML §13.2.6): HTML elements are kept only inside it. A start tag + // the dispatcher gives to foreign content is foreign, `/>` closes it, and + // no element holds raw text; `<![CDATA[` is CDATA while the current node is + // foreign (§13.2.5.42). + /** @type {OpenElement[]} */ + const open = []; + const current = () => open[open.length - 1]; + const breakOut = () => { + for (let node = current(); node && node.ns !== 'html' && !node.htmlPoint && !node.textPoint; node = current()) open.pop(); + }; while (at < n) { const lt = html.indexOf('<', at); if (lt < 0) break; const next = html[lt + 1] ?? ''; /** @type {HtmlToken | null} */ let token = null; + /** Whether the HTML insertion mode reads this start tag. */ + let asHtml = true; DOCTYPE.lastIndex = lt; if (html.startsWith('<!--', lt)) { const close = html.indexOf('-->', lt + 2); token = {kind: 'comment', start: lt, end: close < 0 ? n : close + 3}; + } else if (current() && current().ns !== 'html' && html.startsWith('<![CDATA[', lt)) { + const close = html.indexOf(']]>', lt + 9); + token = {kind: 'cdata', start: lt, end: close < 0 ? n : close + 3}; } else if (DOCTYPE.test(html)) { const close = html.indexOf('>', lt); if (close < 0) break; token = {kind: 'doctype', start: lt, end: close + 1}; + } else if (next === '?' || next === '!' || (next === '/' && !LETTER.test(html[lt + 2] ?? '') && lt + 2 < n)) { + const close = html.indexOf('>', lt + 2); + token = {kind: 'comment', start: lt, end: close < 0 ? n : close + 1}; } else if (LETTER.test(next) || (next === '/' && LETTER.test(html[lt + 2] ?? ''))) { const closing = next === '/'; const read = readTag(html, lt + (closing ? 2 : 1)); if (read === 'open') break; - if (read !== 'text') { - token = closing - ? {kind: 'end', tag: read.tag, start: lt, end: read.end} - : {kind: 'start', tag: read.tag, attrs: read.attrs, selfClosing: read.selfClosing, start: lt, end: read.end}; + if (closing) { + if (current() && current().ns !== 'html' && (read.tag === 'p' || read.tag === 'br')) breakOut(); + else closeEnd(open, read.tag); + token = {kind: 'end', tag: read.tag, start: lt, end: read.end}; + } else { + asHtml = startIsHtml(current(), read.tag); + if (!asHtml && breaksOut(read.tag, read.attrs)) { + breakOut(); + asHtml = true; + } + token = { + kind: 'start', + tag: read.tag, + attrs: read.attrs, + // A browser honours `/>` only on an svg or MathML element. + selfClosing: read.selfClosing && (!asHtml || read.tag === 'svg' || read.tag === 'math'), + start: lt, + end: read.end, + }; } } if (!token) { @@ -148,11 +340,20 @@ export function* scanHtml(html) { if (lt > text) yield {kind: 'text', start: text, end: lt}; yield token; at = text = token.end; - // A self-closing raw-text element (an SVG `<title />`) holds nothing. - if (token.kind === 'start' && RAW_TEXT.has(token.tag) && !token.selfClosing) { - const closing = new RegExp(`</${token.tag}`, 'gi'); - closing.lastIndex = at; - const stop = closing.exec(html)?.index ?? n; + if (token.kind !== 'start') continue; + if (!asHtml || token.tag === 'svg' || token.tag === 'math') { + const node = current(); + const ns = asHtml ? /** @type {'svg' | 'math'} */ (token.tag) : node?.ns === 'math' ? 'math' : 'svg'; + if (!token.selfClosing) open.push(foreignElement(ns, token.tag, token.attrs)); + continue; + } + if (open.length > 0 && !VOID.has(token.tag)) open.push({ns: 'html', tag: token.tag, htmlPoint: false, textPoint: false}); + if (token.tag === 'plaintext') { + if (at < n) yield {kind: 'raw', start: at, end: n}; + return; + } + if (RAW_TEXT.has(token.tag)) { + const stop = token.tag === 'script' ? scriptEnd(html, at) : endTagAt(html, token.tag, at); yield {kind: 'raw', start: at, end: stop}; at = text = stop; } @@ -161,7 +362,8 @@ export function* scanHtml(html) { } /** - * A small, forgiving HTML parser: enough for rendered markup. + * A small, forgiving HTML parser: it builds the elements the scanner reads, + * with each CDATA section in svg or MathML as text. * @param {string} html * @returns {ElementNode} */ @@ -176,6 +378,10 @@ export function parseHtml(html) { top.children.push({text: decode(html.slice(token.start, token.end))}); } else if (token.kind === 'raw') { top.children.push({text: html.slice(token.start, token.end)}); + } else if (token.kind === 'cdata') { + // Inside svg or MathML a CDATA section is text, as a browser shows it. + const closed = html.startsWith(']]>', token.end - 3) && token.end - 3 >= token.start + 9; + top.children.push({text: html.slice(token.start + 9, closed ? token.end - 3 : token.end)}); } else if (token.kind === 'end') { const at = stack.map(n => n.tag).lastIndexOf(token.tag); if (at > 0) stack.length = at; diff --git a/packages/cli/foundation/render/remote-images.test.mjs b/packages/cli/foundation/render/remote-images.test.mjs index 0e45ee4acae9e..c29f2fd71d96d 100644 --- a/packages/cli/foundation/render/remote-images.test.mjs +++ b/packages/cli/foundation/render/remote-images.test.mjs @@ -558,11 +558,14 @@ describe('a render with images from the web', () => { expect(markup).toContain(`src="${data}"`); expect(markup).toContain('src="http://example.com/plain.png"'); expect(asked.map(a => a.url)).toEqual(['https://example.com/shoe.png']); - // The script keeps the page's address, and carries the same data for + // The script keeps the page's address; the image sinks give the data // where the browser takes an image's address. const script = page.html.slice(page.html.indexOf('<script>')); expect(script).toContain('"https://example.com/shoe.png"'); - expect(script).toContain(`["https://example.com/shoe.png","${data}"]`); + // The first paint shows it, so the image sinks read its data there: + // the file holds it once. + expect(script).toContain('[["https://example.com/shoe.png",0]]'); + expect(script).not.toContain(data); expect(page.remoteImages).toEqual({ fetched: ['https://example.com/shoe.png'], failed: [ @@ -1006,6 +1009,146 @@ describe('a render with images from the web', () => { } }, 120_000); + it('reads an https image a first-paint srcset lists from that srcset, so the file holds it once', async () => { + const dir = makeLinkedFixture('astryx-render-srcset-read-', ['react', 'react-dom']); + try { + const file = path.join(dir, 'page.jsx'); + fs.writeFileSync( + file, + [ + "import {useState} from 'react';", + "const b = 'https://cdn.example.com/b.png';", + "const a1 = 'https://cdn.example.com/a1.png';", + "const a2 = 'https://cdn.example.com/a2.png';", + 'export default function Page() {', + ' const [open, setOpen] = useState(false);', + ' return (', + ' <main>', + ' <img srcSet="https://cdn.example.com/b.png 2x" alt="B" />', + ' <img srcSet={`${a1} 1x, ${a2} 2x`} alt="A" />', + ' <button onClick={() => setOpen(true)}>Show</button>', + ' {open && <img src={b} alt="LaterB" />}', + ' {open && <img src={a2} alt="LaterA2" />}', + ' </main>', + ' );', + '}', + "import '@astryxdesign/core/astryx.css';", + '', + ].join('\n'), + ); + /** @param {number} n */ + const image = n => respond(Buffer.concat([PNG, Buffer.from([n])]), {headers: {'content-type': 'image/png'}}); + const {fetch} = fetchFrom({ + 'https://cdn.example.com/b.png': image(1), + 'https://cdn.example.com/a1.png': image(2), + 'https://cdn.example.com/a2.png': image(3), + }); + const page = await renderPage(file, {cwd: dir, remoteImages: {fetch}}); + const dataOf = (/** @type {number} */ n) => `data:image/png;base64,${Buffer.concat([PNG, Buffer.from([n])]).toString('base64')}`; + // Each image's data is in the file once: in the first paint's srcset. + for (const n of [1, 2, 3]) expect(page.html.split(dataOf(n)).length - 1, String(n)).toBe(1); + const dom = new JSDOM(page.html, {runScripts: 'dangerously', pretendToBeVisual: true}); + const settle = () => new Promise(resolve => dom.window.setTimeout(resolve, 100)); + await settle(); + const doc = dom.window.document; + expect(doc.querySelector(`#${ROOT_ID} img[alt="B"]`)?.getAttribute('srcset')).toBe(`${dataOf(1)} 2x`); + expect(doc.querySelector(`#${ROOT_ID} img[alt="A"]`)?.getAttribute('srcset')).toBe(`${dataOf(2)} 1x, ${dataOf(3)} 2x`); + /** @type {HTMLButtonElement} */ (doc.querySelector(`#${ROOT_ID} button`)).click(); + await settle(); + expect(doc.querySelector(`#${ROOT_ID} img[alt="LaterB"]`)?.getAttribute('src')).toBe(dataOf(1)); + expect(doc.querySelector(`#${ROOT_ID} img[alt="LaterA2"]`)?.getAttribute('src')).toBe(dataOf(3)); + dom.window.close(); + } finally { + removeLinkedFixture(dir); + } + }, 120_000); + + it('keeps a srcSet literal that lists an https candidate and a local one as written in the script, so the code reads the address it wrote', async () => { + const dir = makeLinkedFixture('astryx-render-mixed-srcset-', ['react', 'react-dom']); + try { + fs.writeFileSync(path.join(dir, 'b.png'), Buffer.concat([PNG, Buffer.from([5])])); + const file = path.join(dir, 'page.jsx'); + fs.writeFileSync( + file, + [ + "import {useState} from 'react';", + "const SET = 'https://cdn.example.com/a.png 1x, ./b.png 2x';", + 'export default function Page() {', + " const [first, setFirst] = useState('');", + ' return (', + ' <main>', + ' <img srcSet={SET} alt="M" />', + " <button onClick={() => setFirst(SET.split(' ')[0])}>Read</button>", + ' <p>{first}</p>', + ' </main>', + ' );', + '}', + "import '@astryxdesign/core/astryx.css';", + '', + ].join('\n'), + ); + const {fetch} = fetchFrom({ + 'https://cdn.example.com/a.png': respond(Buffer.concat([PNG, Buffer.from([4])]), {headers: {'content-type': 'image/png'}}), + }); + const page = await renderPage(file, {cwd: dir, remoteImages: {fetch}}); + const dataOf = (/** @type {number} */ n) => `data:image/png;base64,${Buffer.concat([PNG, Buffer.from([n])]).toString('base64')}`; + const dom = new JSDOM(page.html, {runScripts: 'dangerously', pretendToBeVisual: true}); + const settle = () => new Promise(resolve => dom.window.setTimeout(resolve, 100)); + await settle(); + const doc = dom.window.document; + expect(doc.querySelector(`#${ROOT_ID} img[alt="M"]`)?.getAttribute('srcset')).toBe(`${dataOf(4)} 1x, ${dataOf(5)} 2x`); + /** @type {HTMLButtonElement} */ (doc.querySelector(`#${ROOT_ID} button`)).click(); + await settle(); + expect(doc.querySelector(`#${ROOT_ID} p`)?.textContent).toBe('https://cdn.example.com/a.png'); + dom.window.close(); + } finally { + removeLinkedFixture(dir); + } + }, 120_000); + + it('gives a first-paint https image its data from the first paint, with no second copy, and leaves an address the code extends as written', async () => { + const dir = makeLinkedFixture('astryx-render-extend-carried-', [ + 'react', + 'react-dom', + ]); + try { + const file = path.join(dir, 'page.jsx'); + fs.writeFileSync( + file, + [ + "import {useState} from 'react';", + "const hero = 'https://cdn.example.com/hero.png';", + 'export default function Page() {', + ' const [open, setOpen] = useState(false);', + ' return <main><img src={hero} alt="Now" /><button onClick={() => setOpen(true)}>Show</button>{open && <img src={`${hero}?w=640`} alt="Later" />}</main>;', + '}', + "import '@astryxdesign/core/astryx.css';", + '', + ].join('\n'), + ); + const {fetch} = fetchFrom({'https://cdn.example.com/hero.png': png()}); + const page = await renderPage(file, {cwd: dir, remoteImages: {fetch}}); + const data = `data:image/png;base64,${PNG.toString('base64')}`; + const script = page.html.slice(page.html.indexOf('<script>', page.html.indexOf('application/x-astryx-source'))); + // The script holds no copy: the image sinks read the first paint's. + expect(script).not.toContain(data); + expect(script).toContain('__astryxCarried[i]'); + const dom = new JSDOM(page.html, {runScripts: 'dangerously', pretendToBeVisual: true}); + const settle = () => new Promise(resolve => dom.window.setTimeout(resolve, 100)); + await settle(); + const doc = dom.window.document; + expect(doc.querySelector(`#${ROOT_ID} img[alt="Now"]`)?.getAttribute('src')).toBe(data); + /** @type {HTMLButtonElement} */ (doc.querySelector(`#${ROOT_ID} button`)).click(); + await settle(); + // An address the code extends with a query keeps it, as the sinks read + // only the exact address or one with a fragment. + expect(doc.querySelector(`#${ROOT_ID} img[alt="Later"]`)?.getAttribute('src')).toBe('https://cdn.example.com/hero.png?w=640'); + dom.window.close(); + } finally { + removeLinkedFixture(dir); + } + }, 120_000); + it('reads a srcSet address that holds commas whole, and leaves a srcSet it carried nothing from as written', async () => { const dir = makeLinkedFixture('astryx-render-srcset-commas-', [ 'react', diff --git a/packages/cli/foundation/render/render-page.mjs b/packages/cli/foundation/render/render-page.mjs index 30f2322c03c44..fb021d8392382 100644 --- a/packages/cli/foundation/render/render-page.mjs +++ b/packages/cli/foundation/render/render-page.mjs @@ -26,6 +26,7 @@ import {builtinModules} from 'node:module'; import {fileURLToPath, pathToFileURL} from 'node:url'; import {NOT_AN_IMAGE, showsImage} from './image-attributes.mjs'; import {formatSrcset, parseSrcset} from './srcset.mjs'; +import * as vm from 'node:vm'; import postcss from 'postcss'; import {importUrl} from '../fs/import-url.mjs'; import { @@ -37,13 +38,14 @@ import { } from './assets.mjs'; import {classesIn, cutCss, cutFontFaces, layerOrderOf} from './css-cut.mjs'; import {asParsed, formatFor, sha256, sourceBlock} from './source-block.mjs'; -import {LINK_OUT, ROOT_ID, imageSinks} from './client-bundle.mjs'; +import {LINK_OUT, ROOT_ID, compactSource, imageSinks} from './client-bundle.mjs'; import {remoteImages} from './remote-images.mjs'; import {ESBUILD_MISSING, findEsbuild} from './esbuild-dependency.mjs'; import {carriedNameFor, embeddedPathProblem} from './embedded-files.mjs'; -import {scanHtml} from './html-reader.mjs'; +import {scanHtml, VOID} from './html-reader.mjs'; import {removeOnStop} from '../fs/stop-cleanup.mjs'; import {aliasOnly, assetRefsOf, fileNamed} from './embedded-imports.mjs'; +import {babelParser} from '../runtime/lazy-dependencies.mjs'; import {isServedPackage, packageOf as specPackageOf, resolveServed} from './served-packages.mjs'; import {CHART_CSS, smallSvgText} from './charts.mjs'; import {HOST_EXIT} from './host-exit.mjs'; @@ -923,23 +925,167 @@ export function withoutAutofocus(markup) { return at === 0 ? markup : out + markup.slice(at); } +/** + * The render's own attribute on a first-paint element whose `src`, `srcset` + * or `poster` it carries: each attribute with the number of its value + * (`src:0 srcset:1`), so the page's script reads the value there + * ({@link withCarriedPaths}). Only the file's markup has it, never the page's + * source. + */ +export const CARRY_ATTR = 'data-astryx-carried'; + +/** The name the script reads the markup's carried values from. */ +const CARRIED_VALUES = '__astryxCarried'; + +/** The script's reader of one candidate of a carried `srcset`, by its place. */ +const CARRIED_CANDIDATE = '__astryxCandidate'; + +/** + * The script's first statement: each value the markup carries, read from the + * first paint by its number before the script draws the page again. + */ +const CARRIED_PRELUDE = + `var ${CARRIED_VALUES}=(()=>{const v=[];for(const e of document.querySelectorAll('[${CARRY_ATTR}]'))` + + `for(const c of e.getAttribute('${CARRY_ATTR}').split(' ')){const [a,i]=c.split(':');v[+i]??=e.getAttribute(a)}return v})();`; + /** * The page's script with each string the markup's rewrite replaced (an - * image's `src`, `poster` or `srcset`, as the page wrote it) written as the - * same carried data, so the page the script draws shows what the first paint - * showed. Only a quoted string that is exactly that value changes, never a - * longer string that holds it. + * image's `src`, `poster` or `srcset`, as the page wrote it) read from the + * markup, which carries the same data, so the page the script draws shows + * what the first paint showed and the file holds that data once. A read + * replaces only a whole string token of the script whose value is exactly + * that path, found with a JavaScript parser. Every other place the path + * appears keeps the data itself: a longer string that holds it (inner HTML, + * a CSS `url()`), a value no first-paint element carries (an image the page + * shows only later), an object key, or a template a tag reads. A script the + * parser cannot read, or that would not parse with the reads, keeps the data + * everywhere. + * @param {string} script + * @param {Map<string, string>} rewrites + * @param {Map<string, number>} carried each value the first paint carries, with + * the number its mark gives it + * @param {Set<string>} [srcsets] the carried values a `srcset` marks: only + * their candidates are read by place + * @param {Map<string, string>} [painted] what each value became in the first + * paint (default `rewrites`): a whole value is read only where the script's + * value is the same, so a srcset that lists an https candidate, which the + * script keeps as written, is never read whole + * @returns {string} + */ +export function withCarriedPaths(script, rewrites, carried, srcsets = new Set(), painted = rewrites) { + const inline = inlineCarried(script, rewrites); + const tokens = carriedTokens(script, rewrites, carried, srcsets, painted); + if (tokens.length === 0) return inline; + // Each token to read becomes a mark no script holds (the script is built + // ASCII-only, so it never holds a raw private-use character), so the text + // pass below writes the data everywhere else; then each mark becomes its read. + let marked = ''; + let at = 0; + for (const token of tokens) { + marked += `${script.slice(at, token.start)}\uE000${Array.isArray(token.index) ? token.index.join('.') : token.index}\uE001`; + at = token.end; + } + marked += script.slice(at); + const candidates = tokens.some(token => Array.isArray(token.index)); + const read = + CARRIED_PRELUDE + + (candidates ? `var ${CARRIED_CANDIDATE}=(i,k)=>(${compactSource(parseSrcset)})(${CARRIED_VALUES}[i]??'')[k]?.url;` : '') + + inlineCarried(marked, rewrites).replace(/\uE000(\d+)(?:\.(\d+))?\uE001/g, (_, i, k, offset, all) => + `${/[\w$]/.test(all[offset - 1] ?? '') ? ' ' : ''}${k === undefined ? `${CARRIED_VALUES}[${i}]` : `${CARRIED_CANDIDATE}(${i},${k})`}`, + ); + try { + new vm.Script(read); + return read; + } catch { + return inline; + } +} + +/** + * The string tokens of the script whose value is exactly a path the first + * paint carries, where a read can stand: not an object key, not a template a + * tag reads, not text the page shows (`children`). In script order. + * @param {string} script + * @param {Map<string, string>} rewrites + * @param {Map<string, number>} carried + * @param {Set<string>} srcsets the carried values a `srcset` marks + * @param {Map<string, string>} painted what each value became in the first paint + * @returns {{start: number, end: number, index: number | [number, number]}[]} + */ +function carriedTokens(script, rewrites, carried, srcsets, painted) { + /** @type {Map<string, number | [number, number]>} */ + const indexOf = new Map(); + /** @param {string} value */ + const quoted = value => !/["'`\\\n]/.test(value) && ['"', "'", '`'].some(q => script.includes(`${q}${value}${q}`)); + for (const [was] of rewrites) { + const index = carried.get(was); + if (index !== undefined && painted.get(was) === rewrites.get(was) && quoted(was)) indexOf.set(was, index); + } + // A local path the script names that a carried srcset lists + // (`${a} 1x, ${b} 2x` built in code): read that candidate. + for (const [, i, written] of carriedSrcsets(carried, srcsets, rewrites)) { + written.forEach(({url}, k) => { + if (!indexOf.has(url) && rewrites.has(url) && !/^(https?:)?\/\//i.test(url) && quoted(url)) indexOf.set(url, [i, k]); + }); + } + if (indexOf.size === 0) return []; + /** @type {any} */ + let ast; + try { + ast = babelParser().parse(script, {sourceType: 'script', errorRecovery: false}); + } catch { + return []; + } + /** @type {{start: number, end: number, index: number | [number, number]}[]} */ + const found = []; + /** @param {any} node @param {any} parent */ + const visit = (node, parent) => { + if (node === null || typeof node !== 'object') return; + if (Array.isArray(node)) { + for (const child of node) visit(child, parent); + return; + } + if (typeof node.type !== 'string') return; + /** @type {string | undefined} */ + let value; + if (node.type === 'StringLiteral') value = node.value; + else if (node.type === 'TemplateLiteral' && node.expressions.length === 0 && parent?.type !== 'TaggedTemplateExpression') value = node.quasis[0]?.value?.cooked; + if (value !== undefined && indexOf.has(value)) { + const key = KEYED.has(parent?.type) && parent.key === node; + const shown = parent?.type === 'ObjectProperty' && parent.value === node && !parent.computed && (parent.key?.name ?? parent.key?.value) === 'children'; + if (!key && !shown) found.push({start: node.start, end: node.end, index: /** @type {number | [number, number]} */ (indexOf.get(value))}); + } + for (const k of Object.keys(node)) { + if (k === 'loc' || k === 'start' || k === 'end' || k === 'extra' || k === 'leadingComments' || k === 'trailingComments') continue; + visit(node[k], node); + } + }; + try { + visit(ast.program, null); + } catch { + // A script nested too deep to walk keeps its inline data. + return []; + } + return found.sort((a, b) => a.start - b.start); +} + +/** The nodes whose `key` names a member: a string there is a name, not a value. */ +const KEYED = new Set(['ObjectProperty', 'ObjectMethod', 'ClassProperty', 'ClassMethod', 'ClassPrivateProperty', 'ClassAccessorProperty']); + +/** + * The script with each string that is exactly a path the markup's rewrite + * replaced written as the same data, and each CSS url() that names it too. + * Text the page shows (`children: "./photo.png"`) stays as written. * @param {string} script * @param {Map<string, string>} rewrites * @returns {string} */ -export function withCarriedPaths(script, rewrites) { +function inlineCarried(script, rewrites) { let out = script; for (const [was, now] of rewrites) { if (/["'`\\\n<]/.test(now) || /["'`\\\n]/.test(was)) continue; for (const quote of ['"', "'", '`']) { const literal = `${quote}${was}${quote}`; - // Text the page shows (`children: "./photo.png"`) stays as written. out = out .split(literal) .map((part, i, parts) => @@ -1098,11 +1244,12 @@ function publicFolderOf(at) { * link (`href`) loads nothing, so it is left alone. * @param {string} text markup or CSS * @param {'html' | 'css'} kind - * @param {{dir: string, pageDir?: string, root: string, warnings: RenderWarning[], counted: Set<string>, localAssets: Set<string>, leaveLargeStyleAssets?: boolean, rewrites?: Map<string, string>, remote?: Map<string, import('./remote-images.mjs').RemoteImage>}} ctx + * @param {{dir: string, pageDir?: string, root: string, warnings: RenderWarning[], counted: Set<string>, localAssets: Set<string>, leaveLargeStyleAssets?: boolean, rewrites?: Map<string, string>, carried?: Map<string, number>, srcsets?: Set<string>, remote?: Map<string, import('./remote-images.mjs').RemoteImage>}} ctx * `leaveLargeStyleAssets`: a stylesheet's file too large to inline is left * as it is and named, as a missing one is, rather than refused; * `rewrites`: each attribute value the markup's rewrite replaced, as the - * page wrote it, with what it became; `remote`: the remote images fetched + * page wrote it, with what it became; `carried`: gets each one the markup + * numbers for the page's script (none in a markup-only render); `remote`: the remote images fetched * for this render, by the address the page wrote */ function inlineAssets(text, kind, ctx) { @@ -1170,7 +1317,7 @@ function inlineAssets(text, kind, ctx) { ctx.localAssets.add(found); return assetDataUri(found); }; - return rewriteRefs(text, kind, resolveRef, ctx.rewrites); + return rewriteRefs(text, kind, resolveRef, ctx.rewrites, ctx.carried, ctx.srcsets); } /** @@ -1179,6 +1326,167 @@ function inlineAssets(text, kind, ctx) { * @typedef {{tag: string, attr: string}} AssetPlace */ +/** + * Where the first paint holds an https image's data, for the image sinks to + * read: the marked value that is the address, or else a marked `srcset` that + * lists it, with the candidate's place there. + * @param {string} url + * @param {Map<string, number> | undefined} carried + * @param {Map<string, [number, number]>} listed each address a marked + * `srcset` lists, with its mark and place ({@link listedCandidates}) + * @returns {[string, number] | [string, number, number] | null} + */ +function firstPaintRead(url, carried, listed) { + if (!carried) return null; + const mark = carried.get(url); + if (mark !== undefined) return [url, mark]; + const place = listed.get(url); + return place ? [url, ...place] : null; +} + +/** + * Each address a marked `srcset` lists, with that srcset's mark and the + * candidate's place in it: the first such place, read once per render. + * @param {Map<string, number> | undefined} carried + * @param {Set<string>} srcsets the carried values a `srcset` marks + * @param {Map<string, string>} rewrites + * @returns {Map<string, [number, number]>} + */ +function listedCandidates(carried, srcsets, rewrites) { + /** @type {Map<string, [number, number]>} */ + const listed = new Map(); + if (!carried) return listed; + for (const [, i, written] of carriedSrcsets(carried, srcsets, rewrites)) { + written.forEach(({url}, k) => { + if (!listed.has(url)) listed.set(url, [i, k]); + }); + } + return listed; +} + +/** + * Each marked `srcset` value whose data lists the same number of candidates + * as the page wrote, with its mark: only such a value can give one + * candidate's data by its place. A `src` or `poster` that holds a comma and + * a space is one address, never a list. + * @param {Map<string, number>} carried + * @param {Set<string>} srcsets the carried values a `srcset` marks + * @param {Map<string, string>} rewrites + * @returns {Array<[string, number, import('./srcset.mjs').SrcsetCandidate[]]>} + */ +function carriedSrcsets(carried, srcsets, rewrites) { + /** @type {Array<[string, number, import('./srcset.mjs').SrcsetCandidate[]]>} */ + const lists = []; + for (const [was, i] of carried) { + const now = rewrites.get(was); + if (!srcsets.has(was) || now === undefined) continue; + const written = parseSrcset(was); + if (parseSrcset(now).length === written.length) lists.push([was, i, written]); + } + return lists; +} + +/** Elements whose content a browser running scripts builds no image of. */ +const UNBUILT = new Set(['noscript', 'template', 'select']); + +/** + * Where `text` holds markup a browser running scripts never makes elements + * of, as [start, end] pairs: a comment (a bogus one too), a CDATA section, + * the text of a raw-text element (`<textarea>`, `<iframe>`, `<xmp>` and the + * others the scanner names, everything after `<plaintext>`), and each + * outermost `<noscript>`, `<template>` or `<select>` (whose content Firefox's + * parser drops), which runs to its own close tag past any nested in it. The + * pieces are the HTML scanner's own. + * @param {string} text + * @returns {[number, number][]} + */ +function unread(text) { + /** @type {[number, number][]} */ + const ranges = []; + /** @type {string[]} */ + const open = []; + let start = 0; + for (const piece of scanHtml(text)) { + if (open.length === 0 && (piece.kind === 'comment' || piece.kind === 'cdata' || piece.kind === 'raw')) { + // A `<![CDATA[` the scanner reads as a bogus comment (outside a foreign + // root it tracks) can still be CDATA in a browser: range it to `]]>`. + const cdata = piece.kind === 'comment' && text.startsWith('<![CDATA[', piece.start); + const close = cdata ? text.indexOf(']]>', piece.start + 9) : -1; + ranges.push([piece.start, cdata ? (close < 0 ? text.length : close + 3) : piece.end]); + continue; + } + if ((piece.kind !== 'start' && piece.kind !== 'end') || !UNBUILT.has(piece.tag)) continue; + if (piece.kind === 'start') { + if (piece.selfClosing) continue; + if (open.length === 0) start = piece.start; + open.push(piece.tag); + continue; + } + const at = open.lastIndexOf(piece.tag); + if (at === -1) continue; + open.length = at; + if (open.length === 0) ranges.push([start, piece.end]); + } + if (open.length > 0) ranges.push([start, text.length]); + const off = offScript(text); + if (off >= 0) ranges.push([off, text.length]); + return ranges; +} + +/** The tags a serializer writes inside a `<select>`. */ +const SELECT_CONTENT = new Set(['option', 'optgroup', 'hr']); + +/** + * Where `text` first holds a shape a serializer such as React's never writes, + * or -1: an end tag that does not close the element open last, CDATA, a + * bogus comment, a comment holding `--!>` (which also ends one), a + * self-closing HTML tag that is not void, `<plaintext>`, `<frameset>`, a + * start tag inside a `<select>` other than option, optgroup or hr, or a + * `<noscript>` that a browser running scripts ends elsewhere: it reads the + * content as text up to the first `</noscript`, where the scanner reads + * markup. Up to that point the scanner and a browser agree on the + * elements; past it they may not, so nothing after it marks a carried + * value, and each image there keeps its own copy in the script. A gap in + * the scanner then costs bytes, never a missing picture. + * @param {string} text + * @returns {number} + */ +function offScript(text) { + /** @type {string[]} */ + const open = []; + /** Each open `<noscript>`: where it starts, and where a browser ends it. @type {{start: number, end: number}[]} */ + const noscripts = []; + const NOSCRIPT_END = /<\/noscript[\t\n\f\r />]/gi; + for (const piece of scanHtml(text)) { + const outer = noscripts[0]; + if (outer && piece.start > outer.end) return outer.start; + const written = text.slice(piece.start, piece.end); + if (piece.kind === 'cdata' || (piece.kind === 'comment' && (!written.startsWith('<!--') || written.includes('--!>')))) return piece.start; + if (piece.kind === 'start') { + if (piece.tag === 'plaintext' || piece.tag === 'frameset') return piece.start; + if (open.includes('select') && !SELECT_CONTENT.has(piece.tag)) return piece.start; + if (VOID.has(piece.tag)) continue; + if (written.endsWith('/>') && !piece.selfClosing) return piece.start; + if (!piece.selfClosing) open.push(piece.tag); + if (piece.tag === 'noscript' && !piece.selfClosing) { + NOSCRIPT_END.lastIndex = piece.end; + const close = NOSCRIPT_END.exec(text); + noscripts.push({start: piece.start, end: close ? close.index : text.length}); + } + } else if (piece.kind === 'end') { + if (open[open.length - 1] !== piece.tag) return piece.start; + open.pop(); + if (piece.tag === 'noscript') { + const own = /** @type {{start: number, end: number}} */ (noscripts.pop()); + // A browser running scripts ends the outermost one at its first `</noscript`. + if (noscripts.length === 0 && piece.start !== own.end) return own.start; + } + } + } + return -1; +} + + /** * Each reference a page's markup or stylesheet loads, read once for every * caller: the render inlines them, and the checks on a written file name @@ -1191,9 +1499,30 @@ function inlineAssets(text, kind, ctx) { * @param {(ref: string, place?: AssetPlace) => string | null} resolve * @param {Map<string, string>} [rewrites] gets each attribute value replaced, * as the page wrote it, with what it became + * @param {Map<string, number>} [marked] number each replaced value in the + * markup ({@link CARRY_ATTR}), so the page's script reads it there, and + * add it to this map with its number + * @param {Set<string>} [srcsets] gets each marked value a `srcset` holds * @returns {string} the text with the replacements made */ -export function rewriteRefs(text, kind, resolve, rewrites) { +export function rewriteRefs(text, kind, resolve, rewrites, marked, srcsets) { + /** + * What each replaced value became, for the marks the first paint gets. + * @type {Map<string, string[]>} + */ + const became = new Map(); + /** + * Record a replaced value. + * @param {string} was + * @param {string} now + */ + const carry = (was, now) => { + if (!rewrites) return; + // The last rewrite of a value is the one the script reads, as without + // marks; an element that shows an earlier one is not marked. + rewrites.set(was, now); + became.set(now, [...(became.get(now) ?? []), was]); + }; /** * @param {string} value * @param {boolean} [record] keep each rewrite, for a style the script sets @@ -1216,7 +1545,7 @@ export function rewriteRefs(text, kind, resolve, rewrites) { // names in any case. .replace(/\s(src|poster)="([^"]*)"/gi, (whole, attr, ref) => { const uri = resolve(ref, {tag, attr: attr.toLowerCase()}); - if (uri) rewrites?.set(decodeAttr(ref), uri); + if (uri) carry(decodeAttr(ref), uri); return uri ? ` ${attr}="${uri}"` : whole; }) .replace(/\s(srcset)="([^"]*)"/gi, (whole, attr, set) => { @@ -1229,7 +1558,7 @@ export function rewriteRefs(text, kind, resolve, rewrites) { }); if (!carried) return whole; const rewritten = formatSrcset(candidates); - rewrites?.set(written, rewritten); + carry(written, rewritten); return ` ${attr}="${rewritten.replace(/"/g, '"')}"`; }) .replace(/\sstyle="([^"]*)"/g, (whole, style) => { @@ -1240,10 +1569,62 @@ export function rewriteRefs(text, kind, resolve, rewrites) { }); // Each element by itself, so a reference knows the element it is in. A // quoted value may hold a `>`, which does not end the tag. - return text.replace(/<([a-zA-Z][\w-]*)\b((?:[^>"']|"[^"]*"|'[^']*')*)>/g, (tagText, name, attrs) => { + const written = text.replace(/<([a-zA-Z][\w-]*)\b((?:[^>"']|"[^"]*"|'[^']*')*)>/g, (tagText, name, attrs) => { const mapped = mapAttributes(attrs, name.toLowerCase()); return mapped === attrs ? tagText : `<${name}${mapped}>`; }); + return marked && rewrites && became.size > 0 ? withCarryMarks(written, became, rewrites, marked, srcsets ?? new Set()) : written; +} + +/** The elements whose `src`, `srcset` or `poster` is an image the page shows. */ +const IMAGE_TAGS = new Set(['img', 'source', 'input', 'video']); + +/** + * The markup with each element a browser builds that shows a replaced value + * marked ({@link CARRY_ATTR}) with that value's number, once per element, + * after its last attribute. Only the first attribute of each name on a real + * `img`, `source`, `input` or `video` start tag counts, outside the ranges + * {@link unread} names; the pieces are the HTML scanner's own. + * @param {string} text + * @param {Map<string, string[]>} became what each value became, and the + * values written that became it + * @param {Map<string, string>} rewrites + * @param {Map<string, number>} marked gets each marked value, with its number + * @param {Set<string>} srcsets gets each marked value a `srcset` holds + * @returns {string} + */ +function withCarryMarks(text, became, rewrites, marked, srcsets) { + const ranges = unread(text); + /** @type {Array<[number, string]>} */ + const inserts = []; + for (const piece of scanHtml(text)) { + if (piece.kind !== 'start' || !IMAGE_TAGS.has(piece.tag) || piece.attrs.length === 0) continue; + if (ranges.some(([from, to]) => piece.start > from && piece.start < to)) continue; + /** @type {string[]} */ + const marks = []; + const seen = new Set(); + for (const [name, value] of piece.attrs) { + if (seen.has(name)) continue; + seen.add(name); + if (name !== 'src' && name !== 'srcset' && name !== 'poster') continue; + const now = decodeAttr(value); + for (const was of became.get(now) ?? []) { + if (rewrites.get(was) !== now) continue; + // The number stays the value's own, whatever later leaves the rewrites. + if (!marked.has(was)) marked.set(was, [...rewrites.keys()].indexOf(was)); + if (name === 'srcset') srcsets.add(was); + marks.push(`${name}:${marked.get(was)}`); + } + } + if (marks.length > 0) inserts.push([/** @type {[string, string, number, number]} */ (piece.attrs.at(-1))[3], ` ${CARRY_ATTR}="${[...new Set(marks)].join(' ')}"`]); + } + let out = ''; + let at = 0; + for (const [where, mark] of inserts) { + out += text.slice(at, where) + mark; + at = where; + } + return out + text.slice(at); } /** @@ -1749,6 +2130,10 @@ export async function renderPage(entry, options = {}) { localAssets: /** @type {Set<string>} */ (new Set()), leaveLargeStyleAssets: options.leaveLargeStyleAssets === true, rewrites: /** @type {Map<string, string>} */ (new Map()), + // The page's script reads the data the markup carries; a markup-only + // render has no script. + carried: options.markupOnly ? undefined : /** @type {Map<string, number>} */ (new Map()), + srcsets: /** @type {Set<string>} */ (new Set()), remote: remote?.results, }; /** @@ -2233,11 +2618,31 @@ export async function renderPage(entry, options = {}) { const mixed = written.map((candidate, i) => (remoteAddress(candidate.url) ? candidate : carried[i])); if (mixed.some((candidate, i) => candidate !== written[i])) localRewrites.set(was, formatSrcset(mixed)); } + // An https image the first paint shows and marks: the sink reads its data + // from that element, so the script holds no second copy of it. /** @type {[string, string][]} */ const sinks = []; - for (const [url, fetched] of remote?.results ?? []) if ('uri' in fetched) sinks.push([url, fetched.uri]); + /** @type {Array<[string, number] | [string, number, number]>} */ + const reads = []; + const listed = listedCandidates(ctx.carried, ctx.srcsets, ctx.rewrites); + for (const [url, fetched] of remote?.results ?? []) { + if (!('uri' in fetched)) continue; + const read = firstPaintRead(url, ctx.carried, listed); + if (read) reads.push(read); + else sinks.push([url, fetched.uri]); + } + let pageScriptText = script === null ? null : withCarriedPaths(script, localRewrites, ctx.carried ?? new Map(), ctx.srcsets, ctx.rewrites); + // The values the first paint carries are read once, before the sinks and + // the page's script that use them. + let prelude = reads.length > 0 ? CARRIED_PRELUDE : ''; + if (pageScriptText?.startsWith(CARRIED_PRELUDE)) { + prelude = CARRIED_PRELUDE; + pageScriptText = pageScriptText.slice(CARRIED_PRELUDE.length); + } const drawn = - script === null ? null : (sinks.length > 0 ? imageSinks(sinks) : '') + withCarriedPaths(script, localRewrites); + pageScriptText === null + ? null + : prelude + (sinks.length + reads.length > 0 ? imageSinks(sinks, reads, CARRIED_VALUES) : '') + pageScriptText; // Paths are kept relative to the folder that holds every file: the page's // folder, or an ancestor when the page imports from above it, but never one // from which the page's own path holds a name a reverse refuses (a diff --git a/packages/cli/foundation/render/render.test.mjs b/packages/cli/foundation/render/render.test.mjs index 30353808e08d8..77d9558a9188e 100644 --- a/packages/cli/foundation/render/render.test.mjs +++ b/packages/cli/foundation/render/render.test.mjs @@ -12,8 +12,8 @@ import {performance} from 'node:perf_hooks'; import {describe, expect, it} from 'vitest'; import {classesIn, cutCss, cutFontFaces, layerOrderOf, requiredClasses} from './css-cut.mjs'; import {RENDER_FORMAT, readSourceBlock, renderedHeadOf, renderedMarkupOf, sourceBlock} from './source-block.mjs'; -import {isAstryxMarkup, parseHtml} from './html-reader.mjs'; -import {oneColorModeOf, renderPage, withCarriedPaths, withUncarriedImages} from './render-page.mjs'; +import {isAstryxMarkup, parseHtml, scanHtml} from './html-reader.mjs'; +import {CARRY_ATTR, oneColorModeOf, renderPage, withCarriedPaths, withUncarriedImages} from './render-page.mjs'; import {carriedNameFor, embeddedPathProblem} from './embedded-files.mjs'; import {makeLinkedFixture, removeLinkedFixture} from '../../test-utils/linked-fixture.mjs'; @@ -327,6 +327,13 @@ describe('the HTML reader', () => { expect(p.children[0].text).toBe('after'); }); + it('reads a CDATA section in svg as its text, closed or not', () => { + const closed = /** @type {any} */ (parseHtml('<svg><text><![CDATA[Hello]]></text></svg>').children[0]); + expect(closed.children[0]).toEqual({tag: 'text', attrs: {}, children: [{text: 'Hello'}]}); + const open = /** @type {any} */ (parseHtml('<svg><text><![CDATA[Hi').children[0]); + expect(open.children[0].children).toEqual([{text: 'Hi'}]); + }); + it('tells Astryx markup from other HTML', () => { expect(isAstryxMarkup('<div class="x astryx-button"></div>')).toBe(true); expect(isAstryxMarkup('<div class="button"></div>')).toBe(false); @@ -391,13 +398,33 @@ export default function Page() { }); describe('withCarriedPaths', () => { - it('rewrites an image path the script names, and leaves text the page shows as written', () => { + it('reads an image path the script names from the markup, and leaves text the page shows as written', () => { const rewrites = new Map([['./photo.png', 'data:image/png;base64,AAAA']]); - expect( - withCarriedPaths(`a({src:"./photo.png",children:"./photo.png"});b('./photo.png');c({children: \`./photo.png\`})`, rewrites), - ).toBe( - `a({src:"data:image/png;base64,AAAA",children:"./photo.png"});b('data:image/png;base64,AAAA');c({children: \`./photo.png\`})`, + const script = withCarriedPaths(`a({src:"./photo.png",children:"./photo.png"});b('./photo.png');c({children: \`./photo.png\`})`, rewrites, new Map([['./photo.png', 0]])); + expect(script).not.toContain('data:image/png'); + expect(script.endsWith(`a({src:__astryxCarried[0],children:"./photo.png"});b(__astryxCarried[0]);c({children: \`./photo.png\`})`)).toBe(true); + expect(script).toContain(`'[${CARRY_ATTR}]'`); + }); + + it('writes the data itself where a read cannot go: an object key, a tagged template, a value the first paint does not carry, or a script that would not parse', () => { + const rewrites = new Map([['./photo.png', 'data:image/png;base64,AAAA']]); + const carried = new Map([['./photo.png', 0]]); + expect(withCarriedPaths('f("./photo.png")', rewrites, new Map())).toBe('f("data:image/png;base64,AAAA")'); + expect(withCarriedPaths(`m={"./photo.png":1};f("./photo.png")`, rewrites, carried)).toMatch( + /m=\{"data:image\/png;base64,AAAA":1\};f\(__astryxCarried\[0\]\)$/, ); + expect(withCarriedPaths('f=()=>tag`./photo.png`', rewrites, carried)).toMatch(/f=\(\)=>tag`data:image\/png;base64,AAAA`$/); + expect(withCarriedPaths('f=()=>{return"./photo.png"}', rewrites, carried)).toMatch(/f=\(\)=>\{return __astryxCarried\[0\]\}$/); + expect(withCarriedPaths('f("./photo.png"', rewrites, carried)).toBe('f("data:image/png;base64,AAAA"'); + }); +}); + +describe('withCarriedPaths numbering', () => { + it('reads each value by the number its first-paint mark gives it, even after an earlier value left the rewrites', () => { + // ./a.png was 0 when the markup was marked, then left (kept as written). + const rewrites = new Map([['./b.png', 'data:image/png;base64,BBBB']]); + const script = withCarriedPaths('f("./a.png");g("./b.png")', rewrites, new Map([['./b.png', 1]])); + expect(script.endsWith('f("./a.png");g(__astryxCarried[1])')).toBe(true); }); }); @@ -564,3 +591,82 @@ describe('oneColorModeOf: the modes a page\'s root theme draws in', () => { expect(oneColorModeOf(root, null, ['.page{--color-background-body:#fff}'])).toBeNull(); }); }); + +describe('scanHtml', () => { + /** @param {string} html */ + const pieces = html => [...scanHtml(html)].map(t => `${t.kind}:${html.slice(t.start, t.end)}`); + + it('reads the content of iframe, xmp, noembed and noframes as raw text, and everything after plaintext', () => { + for (const tag of ['iframe', 'xmp', 'noembed', 'noframes']) { + expect(pieces(`<${tag}><img src="a.png"></${tag}><b>`)).toEqual([`start:<${tag}>`, 'raw:<img src="a.png">', `end:</${tag}>`, 'start:<b>']); + } + expect(pieces('<plaintext><img src="a.png"></plaintext><b>')).toEqual(['start:<plaintext>', 'raw:<img src="a.png"></plaintext><b>']); + }); + + it('reads a bogus comment to the next >', () => { + expect(pieces('<?php x ?><b>')).toEqual(['comment:<?php x ?>', 'start:<b>']); + expect(pieces('<!x <img src="a.png"><b>')).toEqual(['comment:<!x <img src="a.png">', 'start:<b>']); + expect(pieces('</ <img src="a.png"><b>')).toEqual(['comment:</ <img src="a.png">', 'start:<b>']); + expect(pieces('a < b')).toEqual(['text:a < b']); + }); + + it('ends raw text only at its own end tag, and a script only where the script data states end it', () => { + expect(pieces('<textarea>a</textareas><img src="a.png"></textarea><b>')).toEqual(['start:<textarea>', 'raw:a</textareas><img src="a.png">', 'end:</textarea>', 'start:<b>']); + expect(pieces('<title>ab')).toEqual(['start:', 'raw:a', 'end:', 'text:b']); + expect(pieces('')).toEqual(['start:', 'start:']); + expect(pieces('')).toEqual(['start:', 'start:']); + expect(pieces('')).toEqual(['start:', 'start:']); + }); + + it('reads a CDATA section inside svg or math to ]]>, and outside them as a bogus comment', () => { + expect(pieces(' a > <img src="a.png"> ')).toEqual(['start:', 'cdata: a > <img src="a.png"> ', 'end:', 'start:']); + expect(pieces(' b ]]>')).toEqual(['comment:', 'text: b ]]>']); + }); + + it('reads a tag name up to white space, / or >, and closes only an open foreign root of its own name', () => { + expect(pieces(`b`)).toEqual([`start:`, 'text:b']); + expect(pieces(' a > <img src="a.png"> ')).toEqual(['start:', 'end:', 'cdata: a > <img src="a.png"> ', 'end:']); + }); + + it('honours a self-closing tag only in svg or math', () => { + expect(pieces('')).toEqual(['start:', 'start:']); + const closed = [...scanHtml('
')].filter(t => t.kind === 'start').map(t => /** @type {any} */ (t).selfClosing); + expect(closed).toEqual([false, false, true]); + }); + + it('reads HTML again in an svg or MathML integration point and after a breakout tag', () => { + expect(pieces('')).toContain('raw:'); + expect(pieces('')).toContain('raw:'); + expect(pieces('

<img src="a.png">')).toContain('raw:'); + // In svg itself, /> closes the tag and svg's holds markup. + expect(pieces('<svg><path/><title>t')).toEqual(['start:', 'start:', 'start:', 'text:t', 'end:', 'end:']); + }); + + it('closes svg and MathML elements as the tree builder does (HTML 13.2.6)', () => { + const raw = (/** @type {string} */ html) => pieces(html).includes('raw:'); + const IMG = ''; + // Foreign "any other end tag": walk up to the node of that name. + expect(raw(`${IMG}`)).toBe(true); + expect(raw(`${IMG}`)).toBe(true); + // ...and at the first HTML element, the HTML mode reads it: a special + // element there stops it, so the integration point stays open. + expect(raw(`
${IMG}
`)).toBe(true); + expect(raw(`
${IMG}
`)).toBe(true); + // An annotation-xml with an HTML encoding is an HTML integration point; + // one without is not, so /> closes and no raw text opens there. + expect(raw(`${IMG}`)).toBe(true); + expect(raw(`${IMG}`)).toBe(false); + // In a MathML text integration point, mglyph and malignmark stay MathML. + expect(pieces('x')).toContain('start:'); + // The dispatcher gives in annotation-xml to HTML: an svg root. + expect(pieces('')).toContain('start:'); + // CDATA while the current node is foreign, an integration point too. + expect(pieces(' a > b ')).toContain('cdata: a > b '); + }); + + it('says where each attribute is', () => { + const html = ``; + const [tag] = /** @type {any[]} */ ([...scanHtml(html)]); + expect(tag.attrs.map((/** @type {[string, string, number, number]} */ [, , from, to]) => html.slice(from, to))).toEqual([`data-tip=''`, 'id=b']); + }); +}); From e304ec631b1d01940e51e910e5e0c66ea41440ce Mon Sep 17 00:00:00 2001 From: Joey Farina <17398284+josephfarina@users.noreply.github.com> Date: Tue, 17 Nov 2026 08:22:20 -0800 Subject: [PATCH 2/3] [experimental] export: the first paint names each repeated list of Astryx's StyleX classes by one alias A page that repeats an element repeats its StyleX class list each time, and those lists are most of a large page's markup. Each repeated list of the StyleX classes Astryx's packages write becomes one short alias class in the first paint (the page's own classes keep their names), and every rule that names one of its classes also names the alias in the same place, so the alias matches exactly where the classes match. Only the first paint changes; the page's script draws its own elements with the classes StyleX wrote, and the page comes back exactly. [skip ci] --- .changeset/render-class-aliases.md | 7 + .../generate-landing-template-previews.mjs | 9 +- .../landing-template-previews.test.ts | 34 ++ packages/cli/api/render/_adapter.mjs | 9 +- packages/cli/api/render/render.doc.mjs | 9 +- .../api/render/render.interactive.test.mjs | 68 ++++ packages/cli/api/render/render.mjs | 7 +- packages/cli/api/render/render.type.mjs | 1 + .../cli/foundation/render/class-alias.mjs | 333 ++++++++++++++++++ .../foundation/render/class-alias.test.mjs | 138 ++++++++ .../cli/foundation/render/render-page.mjs | 21 +- 11 files changed, 626 insertions(+), 10 deletions(-) create mode 100644 .changeset/render-class-aliases.md create mode 100644 packages/cli/foundation/render/class-alias.mjs create mode 100644 packages/cli/foundation/render/class-alias.test.mjs diff --git a/.changeset/render-class-aliases.md b/.changeset/render-class-aliases.md new file mode 100644 index 0000000000000..a2b62ddb96a27 --- /dev/null +++ b/.changeset/render-class-aliases.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/cli': patch +--- + +[experimental] `astryx experimental export`: a large exported page that repeats an element names each repeated list of the StyleX classes Astryx's packages write (the `x…` classes) by one short class in its first paint, when those repeated lists add up to at least 16 KB; the page's own StyleX classes keep their names, so a large table or list writes a much smaller file: a 10,000-row grid goes from 52 MB to 18 MB. The page looks the same, also where scripts are blocked; its script draws with StyleX's own classes, and `--reverse` gives the page back exactly. The experimental `render()` takes `keepClassNames: true` for a caller that styles the file's markup with its own copy of the StyleX styles. + +@josephfarina diff --git a/apps/docsite/scripts/generate-landing-template-previews.mjs b/apps/docsite/scripts/generate-landing-template-previews.mjs index ba74abb10b079..7eb90027b1022 100644 --- a/apps/docsite/scripts/generate-landing-template-previews.mjs +++ b/apps/docsite/scripts/generate-landing-template-previews.mjs @@ -74,6 +74,13 @@ const templatesDir = path.resolve( // high-density screen. It must be a width the image optimizer allows. export const PREVIEW_IMAGE_WIDTH = 640; +/** + * How a preview whose markup the site takes is rendered: the site's own + * StyleX styles style it, so it keeps StyleX's class names instead of the + * short aliases whose rules only the file's own stylesheet holds. + */ +export const PREVIEW_MARKUP_RENDER = {keepClassNames: true}; + // The page fills the frame and never scrolls, as a live template preview // fills its tile: without this, a template taller than the frame shows a // scrollbar where scrollbars are always visible. The card cannot run script @@ -275,7 +282,7 @@ async function main() { /** @type {Record} */ const previews = {}; const renderPage = async (id, pageDir) => { - await render('page.tsx', {cwd: pageDir, out: 'page.html'}); + await render('page.tsx', {cwd: pageDir, out: 'page.html', ...PREVIEW_MARKUP_RENDER}); previews[id] = previewMarkup( fs.readFileSync(path.join(pageDir, 'page.html'), 'utf8'), ); diff --git a/apps/docsite/src/__tests__/landing-template-previews.test.ts b/apps/docsite/src/__tests__/landing-template-previews.test.ts index 0fd2f268925bd..100d06a443de4 100644 --- a/apps/docsite/src/__tests__/landing-template-previews.test.ts +++ b/apps/docsite/src/__tests__/landing-template-previews.test.ts @@ -4,8 +4,10 @@ import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; +import {render} from '@astryxdesign/cli/api/experimental'; import { PREVIEW_IMAGE_WIDTH, + PREVIEW_MARKUP_RENDER, previewDocument, previewMarkup, previewPageSource, @@ -143,6 +145,38 @@ describe('previewMarkup', () => { expect(markup).not.toContain('x-astryx-source'); }); + it("carries StyleX's own class names, which the site's styles match, for a page that repeats an element", async () => { + const dir = fs.mkdtempSync(path.join(docsiteDir, '.preview-classes-')); + try { + fs.writeFileSync( + path.join(dir, 'page.tsx'), + `import {Button} from '@astryxdesign/core/Button'; +export default function Page() { + return

{Array.from({length: 400}, (_, i) =>
; +} +`, + ); + await render('page.tsx', { + cwd: dir, + out: 'page.html', + ...PREVIEW_MARKUP_RENDER, + }); + const out = previewMarkup( + fs.readFileSync(path.join(dir, 'page.html'), 'utf8'), + ); + const lists = [...out.matchAll(/]*\sclass="([^"]*)"/g)].map( + m => m[1], + ); + expect(lists).toHaveLength(400); + for (const list of lists) { + expect(list).toMatch(/\sx[0-9a-z]{5,}/); + expect(list).not.toMatch(/(^|\s)_a[0-9a-z]+(\s|$)/); + } + } finally { + fs.rmSync(dir, {recursive: true, force: true}); + } + }, 120_000); + it('keeps the image URLs of the rendered page and loads them at low priority', () => { expect(markup).toContain( '', diff --git a/packages/cli/api/render/_adapter.mjs b/packages/cli/api/render/_adapter.mjs index ac0e717b360bb..f543ba1c0fad2 100644 --- a/packages/cli/api/render/_adapter.mjs +++ b/packages/cli/api/render/_adapter.mjs @@ -567,11 +567,12 @@ async function propValueWarnings(values, page) { /** * Render a page module to one HTML file and write it. - * @param {{cwd: string, input: string, output: string, title?: string, absolute?: boolean}} args absolute paths; - * `absolute`: the caller named the output by an absolute path + * @param {{cwd: string, input: string, output: string, title?: string, keepClassNames?: boolean, absolute?: boolean}} args absolute paths; + * `absolute`: the caller named the output by an absolute path; + * `keepClassNames`: keep every class of the first paint as written * @returns {Promise} */ -export async function renderToFile({cwd, input, output, title, absolute = false}) { +export async function renderToFile({cwd, input, output, title, keepClassNames = false, absolute = false}) { readInput(input, cwd); // An absolute --out is the caller's choice of place, also outside the // current folder (cli-surface DEC-16). The file there is still never one a @@ -583,7 +584,7 @@ export async function renderToFile({cwd, input, output, title, absolute = false} try { const set = servedSet(input); try { - page = await renderPage(input, {title, version: cliVersion(), served: set && 'dir' in set ? set : null, output, cwd, cutFontFaces: true}); + page = await renderPage(input, {title, version: cliVersion(), served: set && 'dir' in set ? set : null, output, cwd, cutFontFaces: true, keepClassNames}); } catch (e) { // A page that needs the Core the CLI could not obtain says why. const detail = /** @type {any} */ (e)?.code === 'ERR_RENDER_MISSING_DEPENDENCY' ? /** @type {any} */ (e).detail : null; diff --git a/packages/cli/api/render/render.doc.mjs b/packages/cli/api/render/render.doc.mjs index c8a9a293dbc13..d45532b9d8dfb 100644 --- a/packages/cli/api/render/render.doc.mjs +++ b/packages/cli/api/render/render.doc.mjs @@ -72,6 +72,13 @@ export const doc = { type: 'string', description: 'Folder relative paths resolve from (default: the current directory).', }, + { + name: 'options.keepClassNames', + type: 'boolean', + description: + "Keep every class of the first paint as written, for a caller that takes the file's markup and styles it with its own copy of the StyleX styles. By default, once the repeated lists of the StyleX classes Astryx's packages write (the `x…` classes) add up to 16 KB, each such list becomes one short alias class, whose rules only the file's own stylesheet holds; the page's own StyleX classes keep their names. Rendering only; refused with `reverse`.", + default: 'false', + }, ], returns: [ { @@ -89,7 +96,7 @@ export const doc = { {code: 'ERR_MISSING_ARGUMENT', when: 'no file is given'}, { code: 'ERR_INVALID_ARGUMENT', - when: 'the file or `out` has the wrong kind of extension, `out` is the input itself, `title` is given with `reverse`, or `overwrite` without it; with `reverse`, the page would land under a configuration name (`vite.config.ts`) or where a test, story or benchmark runner would run it (judged from the repository root)', + when: 'the file or `out` has the wrong kind of extension, `out` is the input itself, `title` or `keepClassNames` is given with `reverse`, or `overwrite` without it; with `reverse`, the page would land under a configuration name (`vite.config.ts`) or where a test, story or benchmark runner would run it (judged from the repository root)', }, {code: 'ERR_FILE_NOT_FOUND', when: 'the file, or a local file the page imports, does not exist'}, {code: 'ERR_READ_FAILED', when: 'the file is there but cannot be read: no read permission, a folder or a pipe given as the file, or another file-system error; the message names the reason'}, diff --git a/packages/cli/api/render/render.interactive.test.mjs b/packages/cli/api/render/render.interactive.test.mjs index cf6bdede8861c..6970a595a024e 100644 --- a/packages/cli/api/render/render.interactive.test.mjs +++ b/packages/cli/api/render/render.interactive.test.mjs @@ -1164,6 +1164,74 @@ import '@astryxdesign/core/astryx.css'; expect(fs.readFileSync(path.join(dir, 'two-folders-back/b/photo.png'))).toEqual(own(2)); }, 120_000); + it('keeps every class as written and adds no alias rule with keepClassNames, which reverse refuses', async () => { + put( + 'src/plain/page.tsx', + `import {Button} from '@astryxdesign/core/Button'; +export default function Page() { + return
{Array.from({length: 400}, (_, i) =>
; +} +`, + ); + await render('src/plain/page.tsx', {cwd: dir, out: 'plain.html', keepClassNames: true}); + const html = read('plain.html'); + const style = html.slice(html.indexOf('')); + expect(style).not.toMatch(/\._a[0-9a-z]+/); + const root = html.slice(html.indexOf(`
`), html.indexOf('`, + ); + const out = previewMarkup(html); + expect(out, tag).not.toContain(' { expect(markup).toContain( '', diff --git a/packages/cli/api/render/render.interactive.test.mjs b/packages/cli/api/render/render.interactive.test.mjs index 6970a595a024e..7376ec446745b 100644 --- a/packages/cli/api/render/render.interactive.test.mjs +++ b/packages/cli/api/render/render.interactive.test.mjs @@ -12,7 +12,7 @@ import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; import {JSDOM, VirtualConsole} from 'jsdom'; -import {afterAll, beforeAll, describe, expect, it} from 'vitest'; +import {afterAll, beforeAll, describe, expect, it, vi} from 'vitest'; import {render} from './render.mjs'; import {doctor} from '../doctor/doctor.mjs'; import { @@ -28,6 +28,7 @@ import { import {CARRY_ATTR, RenderError, renderPage} from '../../foundation/render/render-page.mjs'; import {cliVersion} from '../../foundation/integrations/cli-peer-version.mjs'; import {runCli} from '../../test-utils/run-cli.mjs'; +import {pageDom, pageDrawn} from '../../test-utils/page-dom.mjs'; import { makeLinkedFixture, removeLinkedFixture, @@ -80,7 +81,7 @@ const LABEL = `export const label = (n: number): string => \`Clicked \${n} times /** The page's script: the last script element, after the embedded source. @param {string} file */ const scriptOf = file => file.slice( - file.lastIndexOf('-->\n'), ); @@ -119,7 +120,7 @@ describe("a render, which carries the page's script", () => { expect(html).toContain(`
`); expect(html).toContain(`#${ROOT_ID}{display:contents}`); const block = html.indexOf(`\n\n')).toBe(true); @@ -190,18 +191,90 @@ describe("a render, which carries the page's script", () => { expect(stdout).toMatch(/^Export it again: .* --out counter\.html$/m); }, 120_000); + it('keeps a classic script for a page that bundles a CommonJS module that does not ask for strict mode, and the page still works', async () => { + put( + 'src/sloppy/count.js', + `// Written for sloppy mode: it assigns a name it never declares. +module.exports = function next(n) { + counted = n + 1; + return counted; +}; +`, + ); + put( + 'src/sloppy/page.tsx', + `import {useState} from 'react'; +import next from './count.js'; +export default function Page() { + const [n, setN] = useState(0); + return ; +} +import '@astryxdesign/core/astryx.css'; +`, + ); + await render('src/sloppy/page.tsx', {cwd: dir, out: 'sloppy.html'}); + const file = read('sloppy.html'); + // A module script would run the module in strict mode, where the write throws. + expect(file).not.toContain('`; + const file = read('viewer-cancel.html').replace('', `${viewer}`); + const dom = await pageDom(file, {url: 'about:srcdoc'}); + await pageDrawn(dom); + const link = [...dom.window.document.querySelectorAll('a')].find(a => a.textContent === 'Members'); + link?.dispatchEvent(new dom.window.MouseEvent('click', {bubbles: true, cancelable: true, button: 0})); + await new Promise(resolve => dom.window.setTimeout(resolve, 20)); + expect(dom.window.location.hash).toBe('#members'); + dom.window.close(); + }, 120_000); + it('keeps a link to a place in the page inside the file, in a frame whose address is the viewer\u2019s', async () => { put( 'src/places.tsx', @@ -259,12 +363,8 @@ import '@astryxdesign/core/astryx.css'; '', '', ); - const dom = new JSDOM(file, { - url: 'about:srcdoc', - runScripts: 'dangerously', - pretendToBeVisual: true, - }); - await new Promise(resolve => dom.window.setTimeout(resolve, 50)); + const dom = await pageDom(file, {url: 'about:srcdoc'}); + await pageDrawn(dom); const doc = dom.window.document; const click = (/** @type {string} */ text) => { const link = [...doc.querySelectorAll('a')].find( @@ -405,11 +505,8 @@ import '@astryxdesign/core/astryx.css'; ); const painted = new JSDOM(markup).window.document.querySelector('input'); expect(painted?.hasAttribute('autofocus')).toBe(false); - const dom = new JSDOM(file, { - runScripts: 'dangerously', - pretendToBeVisual: true, - }); - await new Promise(resolve => dom.window.setTimeout(resolve, 50)); + const dom = await pageDom(file); + await pageDrawn(dom); const input = dom.window.document.querySelector( 'input[aria-label="Search"]', ); @@ -454,13 +551,10 @@ import '@astryxdesign/core/astryx.css'; expect(markup).toContain( 'Self', ); - const dom = new JSDOM(file, { - runScripts: 'dangerously', - pretendToBeVisual: true, - }); + const dom = await pageDom(file); const settle = () => new Promise(resolve => dom.window.setTimeout(resolve, 50)); - await settle(); + await pageDrawn(dom); const doc = dom.window.document; const sent = () => doc.getElementById('sent')?.textContent; const docs = /** @type {HTMLAnchorElement} */ ( @@ -523,23 +617,21 @@ import '@astryxdesign/core/astryx.css'; ); await render('src/shots.tsx', {cwd: dir, out: 'shots.html'}); const file = read('shots.html'); - const script = file.slice(file.lastIndexOf('', start) + ''.length); expect(after.startsWith( - `\n\n\n\n')).toBe(true); }); diff --git a/packages/cli/api/render/render.test.mjs b/packages/cli/api/render/render.test.mjs index 53374f3f119f1..9991457c7fba8 100644 --- a/packages/cli/api/render/render.test.mjs +++ b/packages/cli/api/render/render.test.mjs @@ -260,7 +260,7 @@ describe('render(): page to one HTML file', () => { expect(renderedMarkupOf(html)).not.toMatch(/')).toHaveLength(2); // The page's script follows the note after the embedded source. - expect(html).toMatch(/ -->\n\n` : ''; + // A module script runs once the document is parsed, so a browser that + // reaches it before its first paint paints the markup first. A classic + // script at the end of the body would run before that paint on a page small + // enough to arrive at once. A script that would not run the same in strict + // mode (a CommonJS package that does not ask for it) stays classic. + const scriptTag = pageScript ? `${pageScript}\n` : ''; let html; let block; if (options.output) { diff --git a/packages/cli/test-utils/page-dom.mjs b/packages/cli/test-utils/page-dom.mjs new file mode 100644 index 0000000000000..08249737096f7 --- /dev/null +++ b/packages/cli/test-utils/page-dom.mjs @@ -0,0 +1,72 @@ +// Copyright (c) Meta Platforms, Inc. and affiliates. + +/** + * @file A rendered page opened in JSDOM with its scripts run as a browser + * runs them. JSDOM runs classic scripts but not module scripts, and a + * rendered page's script is an inline module: this runs each inline module + * once the document is parsed (readyState interactive, before + * DOMContentLoaded), as a function body with no `this` at its top level, + * and reports what it throws as the window's `error` event, as a browser + * does. It does not give a module's strict-mode semantics: JSDOM's eval lets + * through what strict mode refuses, such as a write to an undeclared name. + * Tests only; a rendered file never holds this. + * + * @position test-utils — used by the render tests that run a page's script + */ + +import {JSDOM} from 'jsdom'; +import {vi} from 'vitest'; + +/** + * The page in JSDOM, once its module scripts have run. + * @param {string} html + * @param {import('jsdom').ConstructorOptions} [options] + * @returns {Promise} + */ +export function pageDom(html, options = {}) { + const dom = new JSDOM(html, {runScripts: 'dangerously', pretendToBeVisual: true, ...options}); + const {window} = dom; + const {document} = window; + const runModules = () => { + for (const script of document.querySelectorAll('script[type="module"]:not([src])')) { + try { + window.eval(`(function () {'use strict';\n${script.textContent}\n}).call(undefined);`); + } catch (error) { + window.dispatchEvent(new window.ErrorEvent('error', {error, message: String(/** @type {any} */ (error)?.message ?? error)})); + } + } + }; + // A browser runs a page's module scripts once the document is parsed: + // readyState is 'interactive' and DOMContentLoaded has not fired yet. + if (document.readyState !== 'loading') { + runModules(); + return Promise.resolve(dom); + } + return new Promise(resolve => { + document.addEventListener('readystatechange', function ready() { + if (document.readyState !== 'interactive') return; + document.removeEventListener('readystatechange', ready); + runModules(); + resolve(dom); + }); + }); +} + +/** + * Resolves once React has drawn the page: an element in it carries React's + * props. React draws after the module script runs, so a test that acts on + * the page waits for this. + * @param {JSDOM} dom + * @returns {Promise} + */ +export async function pageDrawn(dom) { + await vi.waitFor( + () => { + const drawn = [...dom.window.document.querySelectorAll('body *')].some(el => + Object.keys(el).some(key => key.startsWith('__reactProps$')), + ); + if (!drawn) throw new Error('React has not drawn the page'); + }, + {timeout: 10_000, interval: 10}, + ); +}