The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 21:00 UTC
fix(web): html render images open with the rest of their page in the viewer (#34883)
Julius Marminge committed
Nov 11, 2026 at 23:52 UTC
415e1cabe6e43a6b44dcdb1973e834fea0431b2d
4 files changed
+91
-19
apps/web/src/components/files/BrowserDocumentFrame.browser.test.tsx
+23
@@ -1,5 +1,6 @@
1
import { injectHtmlRenderBootstrap } from "@t3tools/shared/htmlRender";
2
import { describe, expect, it, vi } from "vite-plus/test";
3
+import { page } from "vite-plus/test/browser";
4
5
import "~/index.css";
6
import { render } from "../../../test/render";
@@ -34,6 +35,28 @@ describe("HtmlRenderDocument", () => {
35
await delivered;
36
expect(onContentHeight).toHaveBeenCalledTimes(1);
37
});
38
+
39
+ it("opens a clicked image with the other images of its block in the viewer", async () => {
40
+ const swatch = (color: string) =>
41
+ `data:image/svg+xml,${encodeURIComponent(
42
+ `<svg xmlns="http://www.w3.org/2000/svg" width="80" height="50"><rect width="80" height="50" fill="${color}"/></svg>`,
43
+ )}`;
44
+ const html = injectHtmlRenderBootstrap(
45
+ `<!doctype html><html><body><div>${["Red", "Green", "Blue"]
46
+ .map((name) => `<img alt="${name}" src="${swatch(name.toLowerCase())}">`)
47
+ .join("")}</div></body></html>`,
48
+ );
49
+ const src = URL.createObjectURL(new Blob([html], { type: "text/html" }));
50
+ using _url = { [Symbol.dispose]: () => URL.revokeObjectURL(src) };
51
+ await using screen = await render(<HtmlRenderDocument src={src} title="Swatches" />);
52
+
53
+ await page.frameLocator(screen.getByTitle("Swatches")).getByAltText("Green").click();
54
+
55
+ const viewer = page.getByRole("dialog", { name: "Expanded image preview" });
56
+ await expect.element(viewer.getByAltText("Green")).toBeVisible();
57
+ await expect.element(viewer.getByRole("button", { name: "Next media" })).toBeVisible();
58
+ await expect.element(viewer.getByRole("button", { name: "Previous media" })).toBeVisible();
59
+ });
60
});
61
62
it("keeps the first and last selected card rings inside the fitted renderer frame", async () => {
apps/web/src/components/files/BrowserDocumentFrame.tsx
+1
-1
@@ -301,7 +301,7 @@ export function HtmlRenderDocument(props: {
301
}
302
const imageRequest = readHtmlRenderImageRequest(event.data);
303
if (imageRequest !== undefined) {
304
- setExpandedImage({ images: [{ src: imageRequest.src, name: imageRequest.name }], index: 0 });
304
+ setExpandedImage({ images: [...imageRequest.images], index: imageRequest.index });
305
}
306
});
307
// Attach at commit because a fast page may post its initial height before passive effects run.
packages/shared/src/htmlRender.test.ts
+27
-8
@@ -120,23 +120,42 @@ describe("readHtmlRenderImageRequest", () => {
120
});
121
expect(
122
readHtmlRenderImageRequest(image({ src: "data:image/png;base64,AA", name: " Chart " })),
123
- ).toEqual({
124
- src: "data:image/png;base64,AA",
125
- name: "Chart",
126
- });
123
+ ).toEqual({ images: [{ src: "data:image/png;base64,AA", name: "Chart" }], index: 0 });
124
expect(readHtmlRenderImageRequest(image({ src: "https://example.com/a.png" }))).toEqual({
128
- src: "https://example.com/a.png",
129
- name: "Image",
125
+ images: [{ src: "https://example.com/a.png", name: "Image" }],
126
+ index: 0,
127
});
128
const boundaryImage = readHtmlRenderImageRequest(
129
image({ src: "https://example.com/a.png", name: "a".repeat(199) + "😀" }),
130
);
134
- expect(boundaryImage?.name.isWellFormed()).toBe(true);
135
- expect(boundaryImage?.name).toBe("a".repeat(199));
131
+ expect(boundaryImage?.images[0]?.name.isWellFormed()).toBe(true);
132
+ expect(boundaryImage?.images[0]?.name).toBe("a".repeat(199));
133
expect(readHtmlRenderImageRequest(image({ src: "javascript:alert(1)" }))).toBeUndefined();
134
expect(readHtmlRenderImageRequest(image({ src: "data:text/html,<p>" }))).toBeUndefined();
135
expect(readHtmlRenderImageRequest(image({ src: "blob:null/1" }))).toBeUndefined();
136
});
137
+
138
+ it("keeps the page's other images, dropping unsafe ones, around the clicked image", () => {
139
+ const b = { src: "https://example.com/b.png", name: "B" };
140
+ const params = {
141
+ ...b,
142
+ images: [{ src: "https://example.com/a.png", name: "A" }, { src: "blob:null/1" }, b, null],
143
+ index: 2,
144
+ };
145
+ const request = (overrides: object) =>
146
+ readHtmlRenderImageRequest({
147
+ jsonrpc: "2.0",
148
+ method: "t3/notifications/open-image",
149
+ params: { ...params, ...overrides },
150
+ });
151
+ expect(request({})).toEqual({
152
+ images: [{ src: "https://example.com/a.png", name: "A" }, b],
153
+ index: 1,
154
+ });
155
+ // An index that does not point at the clicked image falls back to that image alone.
156
+ expect(request({ index: 0 })).toEqual({ images: [b], index: 0 });
157
+ expect(request({ index: -1 })).toEqual({ images: [b], index: 0 });
158
+ });
159
});
160
161
describe("htmlRenderFromToolItem", () => {
packages/shared/src/htmlRender.ts
+40
-10
@@ -275,15 +275,12 @@ export function readHtmlRenderLinkRequest(data: unknown):
275
: undefined;
276
}
277
278
-/** Published local images arrive as `data:image/` URLs. */
279
-export function readHtmlRenderImageRequest(
280
- data: unknown,
281
-): { readonly src: string; readonly name: string } | undefined {
282
- if (typeof data !== "object" || data === null) return undefined;
283
- const { jsonrpc, method, params } = data as Record<string, unknown>;
284
- if (jsonrpc !== "2.0" || method !== OPEN_IMAGE_METHOD) return undefined;
285
- if (typeof params !== "object" || params === null) return undefined;
286
- const { src, name } = params as Record<string, unknown>;
278
+interface HtmlRenderImage {
279
+ readonly src: string;
280
+ readonly name: string;
281
+}
282
+
283
+function readHtmlRenderImage(src: unknown, name: unknown): HtmlRenderImage | undefined {
284
if (typeof src !== "string" || !/^(?:https?:\/\/|data:image\/)/i.test(src)) return undefined;
285
const trimmed = typeof name === "string" ? name.trim() : "";
286
// The page sends any alt text; never end the label on half an emoji.
@@ -293,6 +290,39 @@ export function readHtmlRenderImageRequest(
290
return { src, name: label || "Image" };
291
}
292
293
+/**
294
+ * Published local images arrive as `data:image/` URLs. `images` and `index` carry the
295
+ * page's other images; a page bootstrapped by an older server sends only `src` and `name`.
296
+ */
297
+export function readHtmlRenderImageRequest(
298
+ data: unknown,
299
+): { readonly images: ReadonlyArray<HtmlRenderImage>; readonly index: number } | undefined {
300
+ if (typeof data !== "object" || data === null) return undefined;
301
+ const { jsonrpc, method, params } = data as Record<string, unknown>;
302
+ if (jsonrpc !== "2.0" || method !== OPEN_IMAGE_METHOD) return undefined;
303
+ if (typeof params !== "object" || params === null) return undefined;
304
+ const { src, name, images, index } = params as Record<string, unknown>;
305
+ const clicked = readHtmlRenderImage(src, name);
306
+ if (clicked === undefined) return undefined;
307
+ const gallery: HtmlRenderImage[] = [];
308
+ let at = -1;
309
+ if (Array.isArray(images)) {
310
+ images.forEach((entry: unknown, position) => {
311
+ if (typeof entry !== "object" || entry === null) return;
312
+ const image = readHtmlRenderImage(
313
+ (entry as Record<string, unknown>).src,
314
+ (entry as Record<string, unknown>).name,
315
+ );
316
+ if (image === undefined) return;
317
+ if (position === index) at = gallery.length;
318
+ gallery.push(image);
319
+ });
320
+ }
321
+ return at >= 0 && gallery[at]?.src === clicked.src
322
+ ? { images: gallery, index: at }
323
+ : { images: [clicked], index: 0 };
324
+}
325
+
326
export function readHtmlRenderKeyDown(data: unknown):
327
| {
328
readonly key: string;
@@ -373,7 +403,7 @@ function rootRule(theme: HtmlRenderTheme): string {
403
404
// Apply overridable theme CSS before paint and strip the fragment before routing; framed navigation uses the client.
405
// Forward shortcuts after page handlers; text fields retain their keys.
376
-const BOOTSTRAP_SCRIPT = `(function(){var s=document.getElementById("t3-theme"),n=0;if(!s)return;var b=${JSON.stringify(BASE_CSS)};function a(t){if(!t||typeof t!=="object"||!t.variables||typeof t.variables!=="object")return;var c=":root{color-scheme:"+(t.appearance==="light"?"light":"dark")+";";for(var k in t.variables){if(/^--[a-z0-9-]+$/.test(k))c+=k+":"+String(t.variables[k]).replace(/[;{}<>]/g,"")+";";}s.textContent=c+"}"+b;}try{var m=/[#&]${THEME_FRAGMENT_KEY}=([^&]*)/.exec(location.hash);if(m){a(JSON.parse(decodeURIComponent(m[1])));history.replaceState(history.state,"",location.pathname+location.search);}}catch(e){}window.addEventListener("message",function(e){var d=e.data,p=d&&d.params;if(d&&d.jsonrpc==="2.0"&&d.method===${JSON.stringify(HOST_CONTEXT_CHANGED_METHOD)}&&p&&p.styles)a({appearance:p.theme,variables:p.styles.variables});});document.addEventListener("click",function(e){var l=e.isTrusted?e.composedPath().find(function(t){return t&&t.matches&&t.matches("a[href]");}):null,u;if(!l)return;try{u=new URL(l.getAttribute("href"),document.baseURI);}catch(x){return;}if(!/^https?:$/.test(u.protocol)||u.href.split("#")[0]===location.href.split("#")[0])return;if(window.parent!==window){e.preventDefault();window.parent.postMessage({jsonrpc:"2.0",id:"t3-link-"+(++n),method:${JSON.stringify(OPEN_LINK_METHOD)},params:{url:u.href,metaKey:e.metaKey,ctrlKey:e.ctrlKey}},"*");}else{l.setAttribute("target","_blank");l.setAttribute("rel","noopener");}},true);if(window.parent!==window){var h,o,z=function(){var r=document.documentElement,v=Math.ceil(r.scrollHeight>r.clientHeight?r.scrollHeight:r.getBoundingClientRect().height);if(v===h)return;h=v;window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(SIZE_CHANGED_METHOD)},params:{height:v}},"*");};if(window.ResizeObserver){o=new ResizeObserver(z);o.observe(document.documentElement);}document.addEventListener("DOMContentLoaded",function(){if(o&&document.body)o.observe(document.body);z();});window.addEventListener("load",z);window.addEventListener("click",function(e){var t=e.composedPath()[0],u;if(!e.isTrusted||e.defaultPrevented||e.button!==0||e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||!t||t.tagName!=="IMG"||t.closest("a[href],button"))return;u=t.currentSrc||t.src;if(u)setTimeout(function(){if(!e.defaultPrevented)window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(OPEN_IMAGE_METHOD)},params:{src:u,name:t.alt||t.title||""}},"*");});});var K=[],B=[],C=${JSON.stringify(EVENT_CODE_SHORTCUT_KEYS)};window.addEventListener("message",function(e){var d=e.data,p=d&&d.params;if(e.source===window.parent&&d&&d.jsonrpc==="2.0"&&d.method===${JSON.stringify(SHORTCUTS_METHOD)}&&p&&Array.isArray(p.shortcuts)&&Array.isArray(p.blocked)){K=p.shortcuts;B=p.blocked;}});window.addEventListener("keydown",function(e){var t=e.composedPath()[0],c=e.code||"",l=e.key.toLowerCase(),x=/^[a-z]$/.test(l)?[l]:[l,/^Key[A-Z]$/.test(c)?c.slice(3).toLowerCase():"",l==="+"&&!e.shiftKey&&!e.altKey&&!/^Digit/.test(c)?"":C.hasOwnProperty(c)?C[c]:""],f,m=function(s){return s&&s.metaKey===e.metaKey&&s.ctrlKey===e.ctrlKey&&s.shiftKey===e.shiftKey&&s.altKey===e.altKey&&x.indexOf(s.key)>-1;};if(!e.isTrusted||e.isComposing||e.getModifierState("AltGraph")&&!/^[a-z0-9]$/i.test(e.key)||t&&(t.isContentEditable||/^(?:INPUT|TEXTAREA|SELECT)$/.test(t.tagName))||!K.some(m)||B.some(m))return;f=function(v){window.removeEventListener("keydown",f);if(v!==e||e.defaultPrevented)return;e.preventDefault();window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(KEY_DOWN_METHOD)},params:{key:e.key,code:e.code,metaKey:e.metaKey,ctrlKey:e.ctrlKey,shiftKey:e.shiftKey,altKey:e.altKey,repeat:e.repeat}},"*");};window.addEventListener("keydown",f);setTimeout(function(){window.removeEventListener("keydown",f);});},true);}})();`;
406
+const BOOTSTRAP_SCRIPT = `(function(){var s=document.getElementById("t3-theme"),n=0;if(!s)return;var b=${JSON.stringify(BASE_CSS)};function a(t){if(!t||typeof t!=="object"||!t.variables||typeof t.variables!=="object")return;var c=":root{color-scheme:"+(t.appearance==="light"?"light":"dark")+";";for(var k in t.variables){if(/^--[a-z0-9-]+$/.test(k))c+=k+":"+String(t.variables[k]).replace(/[;{}<>]/g,"")+";";}s.textContent=c+"}"+b;}try{var m=/[#&]${THEME_FRAGMENT_KEY}=([^&]*)/.exec(location.hash);if(m){a(JSON.parse(decodeURIComponent(m[1])));history.replaceState(history.state,"",location.pathname+location.search);}}catch(e){}window.addEventListener("message",function(e){var d=e.data,p=d&&d.params;if(d&&d.jsonrpc==="2.0"&&d.method===${JSON.stringify(HOST_CONTEXT_CHANGED_METHOD)}&&p&&p.styles)a({appearance:p.theme,variables:p.styles.variables});});document.addEventListener("click",function(e){var l=e.isTrusted?e.composedPath().find(function(t){return t&&t.matches&&t.matches("a[href]");}):null,u;if(!l)return;try{u=new URL(l.getAttribute("href"),document.baseURI);}catch(x){return;}if(!/^https?:$/.test(u.protocol)||u.href.split("#")[0]===location.href.split("#")[0])return;if(window.parent!==window){e.preventDefault();window.parent.postMessage({jsonrpc:"2.0",id:"t3-link-"+(++n),method:${JSON.stringify(OPEN_LINK_METHOD)},params:{url:u.href,metaKey:e.metaKey,ctrlKey:e.ctrlKey}},"*");}else{l.setAttribute("target","_blank");l.setAttribute("rel","noopener");}},true);if(window.parent!==window){var h,o,z=function(){var r=document.documentElement,v=Math.ceil(r.scrollHeight>r.clientHeight?r.scrollHeight:r.getBoundingClientRect().height);if(v===h)return;h=v;window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(SIZE_CHANGED_METHOD)},params:{height:v}},"*");};if(window.ResizeObserver){o=new ResizeObserver(z);o.observe(document.documentElement);}document.addEventListener("DOMContentLoaded",function(){if(o&&document.body)o.observe(document.body);z();});window.addEventListener("load",z);window.addEventListener("click",function(e){var t=e.composedPath()[0],u;if(!e.isTrusted||e.defaultPrevented||e.button!==0||e.metaKey||e.ctrlKey||e.shiftKey||e.altKey||!t||t.tagName!=="IMG"||t.closest("a[href],button"))return;u=t.currentSrc||t.src;if(u)setTimeout(function(){var g=[],i=-1;if(e.defaultPrevented)return;Array.prototype.forEach.call(document.images,function(m){var v=m.currentSrc||m.src;if(!v||m.closest("a[href],button"))return;if(m===t)i=g.length;g.push({src:v,name:m.alt||m.title||""});});window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(OPEN_IMAGE_METHOD)},params:{src:u,name:t.alt||t.title||"",images:g,index:i}},"*");});});var K=[],B=[],C=${JSON.stringify(EVENT_CODE_SHORTCUT_KEYS)};window.addEventListener("message",function(e){var d=e.data,p=d&&d.params;if(e.source===window.parent&&d&&d.jsonrpc==="2.0"&&d.method===${JSON.stringify(SHORTCUTS_METHOD)}&&p&&Array.isArray(p.shortcuts)&&Array.isArray(p.blocked)){K=p.shortcuts;B=p.blocked;}});window.addEventListener("keydown",function(e){var t=e.composedPath()[0],c=e.code||"",l=e.key.toLowerCase(),x=/^[a-z]$/.test(l)?[l]:[l,/^Key[A-Z]$/.test(c)?c.slice(3).toLowerCase():"",l==="+"&&!e.shiftKey&&!e.altKey&&!/^Digit/.test(c)?"":C.hasOwnProperty(c)?C[c]:""],f,m=function(s){return s&&s.metaKey===e.metaKey&&s.ctrlKey===e.ctrlKey&&s.shiftKey===e.shiftKey&&s.altKey===e.altKey&&x.indexOf(s.key)>-1;};if(!e.isTrusted||e.isComposing||e.getModifierState("AltGraph")&&!/^[a-z0-9]$/i.test(e.key)||t&&(t.isContentEditable||/^(?:INPUT|TEXTAREA|SELECT)$/.test(t.tagName))||!K.some(m)||B.some(m))return;f=function(v){window.removeEventListener("keydown",f);if(v!==e||e.defaultPrevented)return;e.preventDefault();window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(KEY_DOWN_METHOD)},params:{key:e.key,code:e.code,metaKey:e.metaKey,ctrlKey:e.ctrlKey,shiftKey:e.shiftKey,altKey:e.altKey,repeat:e.repeat}},"*");};window.addEventListener("keydown",f);setTimeout(function(){window.removeEventListener("keydown",f);});},true);}})();`;
407
408
function bootstrapMarkup(markup: string): string {
409
const dark = htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark");