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('x\`,
+ \`tip\`,
+ \`\`,
+ \`name\`,
+ \`\`,
+ '',
+ \`\`,
+ \`\${IMG}\`,
+ \`\${IMG}\`,
+ \`
+
+ );
+}
+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 (`…`, a ``. Inside svg
+ * or MathML content, ``, `/>` closes a
+ * tag and no element holds raw text; an integration point (``,
+ * ``, svg ``, an HTML ``, ``, ``, ``,
+ * ``, ``) and a breakout tag (`
`, `
`…) 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; `` 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; ` 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 + 2);
token = {kind: 'comment', start: lt, end: close < 0 ? n : close + 3};
+ } else if (current() && current().ns !== 'html' && html.startsWith('', 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 ``) 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('z')).toEqual(['start:z', 'end:', '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('')).toEqual(['start:', '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('')).toEqual(['start:']);
+ });
+
+ it('honours a self-closing tag only in svg or math', () => {
+ expect(pieces('')).toEqual(['start:', 'raw:', 'end:', '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('')).toContain('raw:');
+ // In svg itself, /> closes the tag and svg's holds markup.
+ expect(pieces('')).toEqual(['start:']);
+ });
+
+ 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(``)).toBe(true);
+ expect(raw(``)).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(``)).toBe(true);
+ expect(raw(``)).toBe(false);
+ // In a MathML text integration point, mglyph and malignmark stay MathML.
+ expect(pieces('')).toContain('start:');
+ // The dispatcher gives