Skip to content

Commit d5dd364

Browse files
t3dotggclaude
andcommitted
fix(web): HTML renders fit their full content height
The web frame capped the client's measured page height at the agent's height, so pages that wrapped taller under client fonts were cropped and scrolled inside the thread. The measured height now wins, up to 20,000px. html_preview and html_render reject viewport height units, which would make a fitted frame grow without end. Mobile is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 826ef55 commit d5dd364

7 files changed

Lines changed: 129 additions & 14 deletions

File tree

‎apps/server/src/htmlRender/HtmlRender.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,19 @@ describe("HtmlRender", () => {
121121
}).pipe(Effect.provide(layerTest)),
122122
);
123123

124+
it.effect("rejects viewport height units, which make the fitted frame grow without end", () =>
125+
Effect.gen(function* () {
126+
const htmlRender = yield* HtmlRender.HtmlRender;
127+
for (const css of ["min-height:100vh", "height: calc(100dvh - 2rem)", "top:.5svh"]) {
128+
const error = yield* htmlRender.prepare(`<style>main{${css}}</style>`).pipe(Effect.flip);
129+
expect(error).toBeInstanceOf(HtmlRender.HtmlRenderViewportHeightError);
130+
}
131+
yield* htmlRender.prepare(
132+
'<style>main{width:100vw;height:320px}</style><img src="data:image/png;base64,AB1vh+2vh/3vhC">',
133+
);
134+
}).pipe(Effect.provide(layerTest)),
135+
);
136+
124137
it.effect("inlines an SVG behind processing instructions and a doctype subset", () =>
125138
Effect.gen(function* () {
126139
const fileSystem = yield* FileSystem.FileSystem;

‎apps/server/src/htmlRender/HtmlRender.ts‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ export class HtmlRenderPageTooLargeError extends Schema.TaggedError<HtmlRenderPa
6767
}
6868
}
6969

70+
export class HtmlRenderViewportHeightError extends Schema.TaggedError<HtmlRenderViewportHeightError>()(
71+
"HtmlRenderViewportHeightError",
72+
{ found: Schema.String },
73+
) {
74+
override get message(): string {
75+
return `The page uses a viewport height unit (${this.found}). The frame grows to fit the page, so a size that follows the viewport makes it grow again and again. Let content set the height, or use fixed pixel heights.`;
76+
}
77+
}
78+
79+
// The lookbehind skips base64 runs, such as an inlined image that happens to contain "1vh".
80+
const VIEWPORT_HEIGHT_UNIT = /(?<![\w.+/])\d*\.?\d+[sld]?vh(?![\w-])/i;
81+
82+
const rejectViewportHeight = (html: string) => {
83+
const found = VIEWPORT_HEIGHT_UNIT.exec(html)?.[0];
84+
return found === undefined
85+
? Effect.void
86+
: Effect.fail(new HtmlRenderViewportHeightError({ found }));
87+
};
88+
7089
export class HtmlRenderStoreError extends Schema.TaggedError<HtmlRenderStoreError>()(
7190
"HtmlRenderStoreError",
7291
{ cause: Schema.Defect() },
@@ -79,7 +98,8 @@ export class HtmlRenderStoreError extends Schema.TaggedError<HtmlRenderStoreErro
7998
export type HtmlRenderPrepareError =
8099
| HtmlRenderImagesNotFoundError
81100
| HtmlRenderImageTooLargeError
82-
| HtmlRenderPageTooLargeError;
101+
| HtmlRenderPageTooLargeError
102+
| HtmlRenderViewportHeightError;
83103

84104
export interface HtmlPreview {
85105
readonly png: string;
@@ -378,6 +398,7 @@ const make = Effect.gen(function* () {
378398
inlineLocalImages(injectHtmlRenderBootstrap(html)).pipe(Effect.provideContext(services));
379399

380400
const prepare = Effect.fn("HtmlRender.prepare")(function* (html: string) {
401+
yield* rejectViewportHeight(html);
381402
const inlined = yield* inline(html);
382403
if (inlined.missing.length > 0) {
383404
return yield* new HtmlRenderImagesNotFoundError({ paths: inlined.missing });
@@ -429,6 +450,7 @@ const make = Effect.gen(function* () {
429450
Math.max(MIN_PREVIEW_WIDTH, Math.round(input.width ?? HTML_RENDER_COLUMN_WIDTH)),
430451
);
431452
const appearance = input.appearance ?? "dark";
453+
yield* rejectViewportHeight(input.html);
432454
const inlined = yield* inline(input.html);
433455
const executable = yield* previewBrowser.executable;
434456
const theme = htmlRenderTheme(

‎apps/server/src/mcp/toolkits/html/handlers.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const INVALID_PAGE_ERRORS = new Set([
1010
"HtmlRenderImagesNotFoundError",
1111
"HtmlRenderImageTooLargeError",
1212
"HtmlRenderPageTooLargeError",
13+
"HtmlRenderViewportHeightError",
1314
]);
1415

1516
const toFailure = (error: { readonly _tag: string; readonly message: string }) =>

‎apps/server/src/mcp/toolkits/html/tools.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { OrchestratorMcpFailure } from "@t3tools/contracts";
22
import {
33
HTML_RENDER_COLUMN_WIDTH,
44
HTML_RENDER_LAYOUT_GUIDE,
5+
HTML_RENDER_MAX_FIT_HEIGHT,
56
HTML_RENDER_MAX_HEIGHT,
67
HTML_RENDER_MAX_TITLE_LENGTH,
78
HTML_RENDER_MIN_HEIGHT,
@@ -71,15 +72,15 @@ export const HtmlPreviewTool = Tool.make("html_preview", {
7172
// Read-only in the MCP sense: it shows a page in the caller's own thread and
7273
// touches no workspace, so plan mode and read-only sandboxes can use it.
7374
const HtmlRenderTool = Tool.make(HTML_RENDER_TOOL_NAME, {
74-
description: `Show a finished HTML page (chart, table, diagram, collage, mockup) inline in this thread, above your final text reply; call it before writing that reply. The reader already sees the page, so the reply should not announce it, say where it is, or restate it: add only what the page doesn't say. Preview with html_preview first. T3 fits the frame to the page's height at each reader's width. A height below the page's contentHeight caps the frame there, and the rest scrolls inside it. ${PAGE_RULES} ${HTML_RENDER_LAYOUT_GUIDE} ${HTML_RENDER_THEME_GUIDE}`,
75+
description: `Show a finished HTML page (chart, table, diagram, collage, mockup) inline in this thread, above your final text reply; call it before writing that reply. The reader already sees the page, so the reply should not announce it, say where it is, or restate it: add only what the page doesn't say. Preview with html_preview first. T3 fits the frame to the page's height at each reader's width. On mobile, a height below the page's contentHeight caps the frame there, and the rest scrolls inside it; web and desktop fit the whole page, up to ${HTML_RENDER_MAX_FIT_HEIGHT}px. ${PAGE_RULES} ${HTML_RENDER_LAYOUT_GUIDE} ${HTML_RENDER_THEME_GUIDE}`,
7576
parameters: Schema.Struct({
7677
html: Html,
7778
title: Schema.String.check(
7879
Schema.isMinLength(1),
7980
Schema.isMaxLength(HTML_RENDER_MAX_TITLE_LENGTH),
8081
).annotate({ description: "Short name for the page." }),
8182
height: Schema.Int.annotate({
82-
description: `The frame height in CSS pixels, ${HTML_RENDER_MIN_HEIGHT}-${HTML_RENDER_MAX_HEIGHT}. Use html_preview's contentHeight, or less to make long content scroll inside the frame.`,
83+
description: `The frame height in CSS pixels, ${HTML_RENDER_MIN_HEIGHT}-${HTML_RENDER_MAX_HEIGHT}. Use html_preview's contentHeight. A lower height makes long content scroll inside the frame on mobile only.`,
8384
}),
8485
}),
8586
success: Schema.Struct({
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { act } from "react";
2+
import { createRoot, type Root } from "react-dom/client";
3+
import { afterEach, expect, it, vi } from "vite-plus/test";
4+
import { EnvironmentId } from "@t3tools/contracts";
5+
import {
6+
htmlRenderTheme,
7+
HTML_RENDER_DEFAULT_FONTS,
8+
injectHtmlRenderBootstrap,
9+
} from "@t3tools/shared/htmlRender";
10+
import { T3_CODE_DARK_THEME_COLORS } from "@t3tools/shared/themePalettes";
11+
12+
import "~/index.css";
13+
14+
import { HtmlRenderFrame } from "./HtmlRenderFrame";
15+
16+
const page = vi.hoisted(() => ({ url: "" }));
17+
vi.mock(import("~/assets/assetUrls"), async (original) => ({
18+
...(await original()),
19+
useAssetUrlState: () => ({
20+
_tag: "Success",
21+
url: page.url,
22+
expiresAt: Date.now() + 60 * 60_000,
23+
}),
24+
useAssetUrlRefresh: () => vi.fn(),
25+
}));
26+
vi.mock(import("~/hooks/useHtmlRenderTheme"), async (original) => ({
27+
...(await original()),
28+
useHtmlRenderTheme: () =>
29+
htmlRenderTheme(T3_CODE_DARK_THEME_COLORS, "dark", HTML_RENDER_DEFAULT_FONTS),
30+
}));
31+
32+
let root: Root | undefined;
33+
let container: HTMLDivElement | undefined;
34+
35+
afterEach(async () => {
36+
await act(() => root?.unmount());
37+
container?.remove();
38+
URL.revokeObjectURL(page.url);
39+
});
40+
41+
it("grows past the agent's height to fit the page the client lays out", async () => {
42+
page.url = URL.createObjectURL(
43+
new Blob([injectHtmlRenderBootstrap('<div style="height:2345px"></div>')], {
44+
type: "text/html",
45+
}),
46+
);
47+
container = document.createElement("div");
48+
document.body.append(container);
49+
root = createRoot(container);
50+
await act(() =>
51+
root!.render(
52+
<HtmlRenderFrame
53+
environmentId={EnvironmentId.make("environment-1")}
54+
htmlRender={{ attachmentId: "html", title: "Tall page", height: 400 }}
55+
threadRef={undefined}
56+
/>,
57+
),
58+
);
59+
60+
const box = container.firstElementChild as HTMLElement;
61+
await expect.poll(() => box.style.height).toBe("2345px");
62+
expect(box.querySelector("iframe")?.clientHeight).toBe(2345);
63+
});

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it } from "vite-plus/test";
22

33
import {
4-
HTML_RENDER_MAX_HEIGHT,
4+
HTML_RENDER_MAX_FIT_HEIGHT,
55
htmlRenderFrameHeight,
66
htmlRenderReferencesEqual,
77
htmlRenderTheme,
@@ -267,18 +267,22 @@ describe("htmlRenderFrameHeight", () => {
267267
const fitted = { ...measured, height: 1403 };
268268
expect(htmlRenderFrameHeight(fitted, 728, 1415)).toBe(1415);
269269
expect(htmlRenderFrameHeight(fitted, 1400)).toBe(1660);
270-
expect(htmlRenderFrameHeight(fitted, 728, 5000)).toBe(HTML_RENDER_MAX_HEIGHT);
270+
expect(htmlRenderFrameHeight(fitted, 728, 5000)).toBe(5000);
271+
expect(htmlRenderFrameHeight(fitted, 728, 99_999)).toBe(HTML_RENDER_MAX_FIT_HEIGHT);
271272
});
272273

273-
it("keeps the agent's height when it asked for a scrolling frame or the page is unmeasured", () => {
274+
it("fits the client's measured content over a shorter agent height", () => {
274275
const scrolling = { ...measured, height: 600 };
275-
expect(htmlRenderFrameHeight(scrolling, 728, 1415)).toBe(600);
276-
expect(htmlRenderFrameHeight(scrolling, 1400)).toBe(600);
277-
expect(htmlRenderFrameHeight(reference, 728)).toBe(reference.height);
278-
expect(htmlRenderFrameHeight(reference, 728, 900)).toBe(reference.height);
276+
expect(htmlRenderFrameHeight(scrolling, 728, 1415)).toBe(1415);
277+
expect(htmlRenderFrameHeight(reference, 728, 900)).toBe(900);
279278
expect(htmlRenderFrameHeight(reference, 728, 300)).toBe(300);
280279
});
281280

281+
it("keeps the agent's height without a client measurement", () => {
282+
expect(htmlRenderFrameHeight({ ...measured, height: 600 }, 1400)).toBe(600);
283+
expect(htmlRenderFrameHeight(reference, 728)).toBe(reference.height);
284+
});
285+
282286
it("drops a malformed table and compares tables by value", () => {
283287
expect(readHtmlRenderReference({ ...reference, heights: [[728, "x"]] })?.heights).toBe(
284288
undefined,

‎packages/shared/src/htmlRender.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import { EVENT_CODE_SHORTCUT_KEYS } from "./keybindings.ts";
99
export const HTML_RENDER_TOOL_NAME = "html_render";
1010
export const HTML_RENDER_MIN_HEIGHT = 80;
1111
export const HTML_RENDER_MAX_HEIGHT = 2000;
12+
// A client fits the frame to the live page up to this height. It only stops a
13+
// page that grows with its frame, since vh units are rejected at publish.
14+
export const HTML_RENDER_MAX_FIT_HEIGHT = 20_000;
1215
export const HTML_RENDER_MAX_TITLE_LENGTH = 200;
1316

1417
export interface HtmlRenderReference {
@@ -90,22 +93,30 @@ function measuredHeight(heights: NonNullable<HtmlRenderReference["heights"]>, wi
9093

9194
/**
9295
* A page even a few pixels taller than its frame scrolls inside it and takes
93-
* the reader's scroll, so the frame fits the page.
96+
* the reader's scroll, so the frame fits the page. A client that measured the
97+
* live page passes `contentHeight`, which wins over the agent's height: client
98+
* fonts often wrap the page taller than the server or agent measured it.
9499
*/
95100
export function htmlRenderFrameHeight(
96101
reference: HtmlRenderReference,
97102
width: number,
98103
contentHeight?: number,
99104
) {
105+
if (contentHeight !== undefined) {
106+
return Math.min(
107+
HTML_RENDER_MAX_FIT_HEIGHT,
108+
Math.max(HTML_RENDER_MIN_HEIGHT, Math.ceil(contentHeight)),
109+
);
110+
}
100111
const heights = reference.heights;
101112
if (heights === undefined || heights.length === 0) {
102-
return clampHtmlRenderHeight(Math.min(reference.height, contentHeight ?? reference.height));
113+
return clampHtmlRenderHeight(reference.height);
103114
}
104115
const cap =
105116
measuredHeight(heights, HTML_RENDER_COLUMN_WIDTH) > reference.height
106117
? reference.height
107118
: HTML_RENDER_MAX_HEIGHT;
108-
return clampHtmlRenderHeight(Math.min(cap, contentHeight ?? measuredHeight(heights, width)));
119+
return clampHtmlRenderHeight(Math.min(cap, measuredHeight(heights, width)));
109120
}
110121

111122
export function htmlRenderFileName(title: string) {
@@ -234,7 +245,7 @@ export const HTML_RENDER_LAYOUT_GUIDE = [
234245
"Use a fluid width with no horizontal padding on the outermost element, and no outer card, border, or banner title: the page is part of your reply.",
235246
"If a box needs its own background (a mock of a specific screen, a panel that must stand apart), give it at least 16px of padding on every side and var(--radius) corners, so content never touches its edge.",
236247
"Give charts fixed pixel heights rather than heights that scale with width.",
237-
"Let content set the page's height. Avoid viewport-based heights such as 100vh or height:100% on html or body; the frame grows to fit the page, so they can make it grow again and again.",
248+
"Let content set the page's height. The frame grows to fit the page, so a height that follows the viewport makes it grow again and again: html_preview and html_render reject vh, svh, lvh, and dvh units, and height:100% on html or body has the same problem.",
238249
].join(" ");
239250

240251
const HOST_CONTEXT_CHANGED_METHOD = "ui/notifications/host-context-changed";

0 commit comments

Comments
 (0)