Skip to content

Commit 897c809

Browse files
fix(web): html render images open with the rest of their page in the viewer
Clicking an image in an html_render page posted only that image, so the viewer opened without previous/next. The page script now also sends every zoomable image of the page and the clicked one's index; the reader drops unsafe entries and falls back to the single image when the index does not match, which also keeps pages bootstrapped by an older server working. Fixes #25574 AI-assisted; verified with the tests and tools listed above.
1 parent cb1e2c1 commit 897c809

3 files changed

Lines changed: 68 additions & 19 deletions

File tree

‎apps/web/src/components/files/BrowserDocumentFrame.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ export function HtmlRenderDocument(props: {
301301
}
302302
const imageRequest = readHtmlRenderImageRequest(event.data);
303303
if (imageRequest !== undefined) {
304-
setExpandedImage({ images: [{ src: imageRequest.src, name: imageRequest.name }], index: 0 });
304+
setExpandedImage({ images: [...imageRequest.images], index: imageRequest.index });
305305
}
306306
});
307307
// Attach at commit because a fast page may post its initial height before passive effects run.

‎packages/shared/src/htmlRender.test.ts‎

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,42 @@ describe("readHtmlRenderImageRequest", () => {
120120
});
121121
expect(
122122
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 });
127124
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,
130127
});
131128
const boundaryImage = readHtmlRenderImageRequest(
132129
image({ src: "https://example.com/a.png", name: "a".repeat(199) + "😀" }),
133130
);
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));
136133
expect(readHtmlRenderImageRequest(image({ src: "javascript:alert(1)" }))).toBeUndefined();
137134
expect(readHtmlRenderImageRequest(image({ src: "data:text/html,<p>" }))).toBeUndefined();
138135
expect(readHtmlRenderImageRequest(image({ src: "blob:null/1" }))).toBeUndefined();
139136
});
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+
});
140159
});
141160

142161
describe("htmlRenderFromToolItem", () => {

‎packages/shared/src/htmlRender.ts‎

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,12 @@ export function readHtmlRenderLinkRequest(data: unknown):
275275
: undefined;
276276
}
277277

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 {
287284
if (typeof src !== "string" || !/^(?:https?:\/\/|data:image\/)/i.test(src)) return undefined;
288285
const trimmed = typeof name === "string" ? name.trim() : "";
289286
// The page sends any alt text; never end the label on half an emoji.
@@ -293,6 +290,39 @@ export function readHtmlRenderImageRequest(
293290
return { src, name: label || "Image" };
294291
}
295292

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+
296326
export function readHtmlRenderKeyDown(data: unknown):
297327
| {
298328
readonly key: string;
@@ -373,7 +403,7 @@ function rootRule(theme: HtmlRenderTheme): string {
373403

374404
// Apply overridable theme CSS before paint and strip the fragment before routing; framed navigation uses the client.
375405
// 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);}})();`;
377407

378408
function bootstrapMarkup(markup: string): string {
379409
const dark = htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark");

0 commit comments

Comments
 (0)