fix(pages): a short Custom HTML block shrinks to its content height

scrollHeight inside the frame never drops below the frame's own 600 px start height,
so a top bar left a large blank gap. The frame script now reports the content box height.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-03 20:06:57 +08:00
1 parent e70051f912
commit 3d89eb3118
2 files changed
+13 -2

No files matched your search

@@ -37,3 +37,14 @@ describe("frame script: same-page links", () => {
expect(post.mock.calls.filter(([d]) => (d as { type: string }).type === "colite-frame-anchor")).toHaveLength(0);
});
});
describe("frame script: height report", () => {
it("reports the content's height, not the frame's own, so a short block can shrink below its start height", () => {
const post = vi.spyOn(window.parent, "postMessage").mockImplementation(() => {});
const root = document.documentElement;
vi.spyOn(root, "scrollHeight", "get").mockReturnValue(600);
vi.spyOn(root, "getBoundingClientRect").mockReturnValue({ height: 69.4 } as DOMRect);
runFrameScript("<header>Top bar</header>");
expect(post).toHaveBeenCalledWith({ type: "colite-frame-height", height: 70 }, "*");
});
});
+2 -2
View File
@@ -15,10 +15,10 @@ export const FRAME_ANCHOR_MESSAGE = "colite-frame-anchor";
const ANCHOR_SOURCE = "^(?!reg-)[a-z0-9][a-z0-9-]{0,39}$";
const ANCHOR = new RegExp(ANCHOR_SOURCE);
// Posts the document height to the parent page on load and whenever it changes. A click on a #name link
// Posts the content height (not scrollHeight, which never drops below the frame itself) to the parent page on load and whenever it changes. A click on a #name link
// (add-page-anchor-links) asks the page to scroll instead: the frame is as tall as its content and never scrolls.
const FRAME_SCRIPT =
`<script>(function(){function s(){parent.postMessage({type:"${FRAME_MESSAGE}",height:Math.ceil(document.documentElement.scrollHeight)},"*")}` +
`<script>(function(){function s(){parent.postMessage({type:"${FRAME_MESSAGE}",height:Math.ceil(document.documentElement.getBoundingClientRect().height)},"*")}` +
`addEventListener("load",s);if(window.ResizeObserver)new ResizeObserver(s).observe(document.documentElement);s();` +
`document.addEventListener("click",function(e){var a=e.target&&e.target.closest&&e.target.closest('a[href^="#"]');if(!a)return;` +
`var n=a.getAttribute("href").slice(1);if(!/${ANCHOR_SOURCE}/.test(n))return;e.preventDefault();var t=document.getElementById(n);` +