test(pages): cover the canvas frame; record its unsandboxed frame in the proposal

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 15:01:16 +08:00
1 parent 7e916b77b9
commit 6b3498d123
4 files changed
+35 -6

No files matched your search

@@ -46,7 +46,7 @@ The editor mounts `<iframe>` with no `src` (about:blank, same origin), then rend
### 2. Desktop is 1280 px, scaled
- **Desktop** renders the frame 1280 px wide and scales it with a CSS transform to the width left beside the panel. The frame is one screen tall and scrolls inside, like a device (the spike found that growing it to its content loops, since a landing page is at least one screen tall: `min-h-svh`).
- **Desktop** renders the frame 1280 px wide and scales it with a CSS transform to the width left beside the panel. The frame is one screen tall and scrolls inside, like a device. A frame grown to its content risks a feedback loop with the page's `min-h-svh`, and a fixed screen keeps `svh` honest.
- **Phone** is 390 px at 1:1.
- Outlines, toolbars and "Add section" buttons are drawn inside the frame document, so they scale with the page.
@@ -46,5 +46,6 @@ None.
- `CmsBlocks` gains edit-mode hooks, which the public page does not use.
- `HtmlFrame` listens on the window of the document it is in.
- A leave guard for in-app links.
- **Security:** the editor canvas is the portal's only frame without a `sandbox`. It must be same-origin for the editor to render into it, and a sandbox would also stop scripts in the Custom HTML frames nested inside. It loads only a constant blank document with no `src`, so no stored HTML ever runs as the portal. The markup sweep test allow-lists exactly that file and shape (design Decision 1).
- **No server, API, database or SQL changes.** New labels only, with a label SQL file.
- **No new dependencies.** Tiptap is already installed; dragging uses native drag-and-drop.
@@ -0,0 +1,28 @@
import { render, waitFor, within } from "@testing-library/react";
import { describe, it, expect } from "vitest";
import { CanvasFrame } from "./CanvasFrame";
// add-page-visual-editor: the canvas renders into its own blank document, with the editor's styles and theme.
const frame = () => document.querySelector("iframe") as HTMLIFrameElement;
describe("CanvasFrame", () => {
it("renders its children inside the frame's document, at the given size, carrying the editor's styles and theme", async () => {
const style = document.head.appendChild(document.createElement("style"));
style.textContent = ".x{color:red}";
document.documentElement.classList.add("dark");
try {
render(<CanvasFrame width={390} height={700} title="Canvas"><p>On the canvas</p></CanvasFrame>);
expect(frame()).toHaveStyle({ width: "390px", height: "700px" });
const inner = () => frame().contentDocument!;
await waitFor(() => expect(within(inner().body).getByText("On the canvas")).toBeInTheDocument());
expect(document.body).not.toHaveTextContent("On the canvas");
expect(inner().documentElement).toHaveClass("dark");
expect(inner().head.querySelector("style")?.textContent).toBe(".x{color:red}");
document.documentElement.classList.remove("dark");
await waitFor(() => expect(inner().documentElement).not.toHaveClass("dark"));
} finally {
style.remove();
document.documentElement.classList.remove("dark");
}
});
});
@@ -28,11 +28,11 @@ function mirror(doc: Document): () => void {
/**
* A blank same-origin frame that its children render into by portal (add-page-visual-editor, design Decision 1).
* The frame is a device screen: its own viewport, so the page's breakpoints follow its width, and it scrolls inside.
* (Growing it to its content would loop, since the page is at least one screen tall.)
*/
export function CanvasFrame({ width, height, title, children }: { width: number; height: number; title: string; children: ReactNode }) {
// The loaded blank document's body; srcDoc plus onLoad so no browser swaps the document after the first render.
const [body, setBody] = useState<HTMLElement | null>(null);
// A container in the loaded blank document, which React removes the portal from cleanly when the frame goes.
// srcDoc plus onLoad, so no browser swaps the document out after the first render.
const [root, setRoot] = useState<HTMLElement | null>(null);
const stop = useRef<() => void>(undefined);
useEffect(() => () => stop.current?.(), []);
return (
@@ -43,9 +43,9 @@ export function CanvasFrame({ width, height, title, children }: { width: number;
if (!doc) return;
stop.current?.();
stop.current = mirror(doc);
setBody(doc.body);
setRoot(doc.body.appendChild(doc.createElement("div")));
}} />
{body ? createPortal(children, body) : null}
{root ? createPortal(children, root) : null}
</>
);
}