diff --git a/client/src/components/Studio/printReady.ts b/client/src/components/Studio/printReady.ts index 1098dcf13..378921fb1 100644 --- a/client/src/components/Studio/printReady.ts +++ b/client/src/components/Studio/printReady.ts @@ -98,18 +98,22 @@ function settle(el: Element): Promise { * worse than a marker that prints blank. */ export async function whenReady(iframe: HTMLIFrameElement): Promise { - const win = iframe.contentWindow; const doc = iframe.contentDocument as PrintDocument | null; - if (!win || !doc) return; + if (!doc) return; const imgs = Array.from(doc.images); const svg = svgImagesToWait(doc); const images = imgs.map((img) => (img.complete ? Promise.resolve() : settle(img))); - const wait = win.setTimeout?.bind(win) ?? setTimeout; + // The sandbox blocks the frame's timer callbacks. Keep the deadline in the app. + let timeout: ReturnType | undefined; const ceiling = new Promise((resolve) => { - wait(resolve, printReadyCeilingMs(imgs.length + svg.length)); + timeout = setTimeout(resolve, printReadyCeilingMs(imgs.length + svg.length)); }); - await Promise.race([Promise.all([...images, ...svg.map(settle), doc.fonts?.ready ?? Promise.resolve()]), ceiling]); + try { + await Promise.race([Promise.all([...images, ...svg.map(settle), doc.fonts?.ready ?? Promise.resolve()]), ceiling]); + } finally { + clearTimeout(timeout); + } } diff --git a/client/tests/unit/studio/printSheets.test.ts b/client/tests/unit/studio/printSheets.test.ts index c2fcaa187..5f033fce6 100644 --- a/client/tests/unit/studio/printSheets.test.ts +++ b/client/tests/unit/studio/printSheets.test.ts @@ -1,4 +1,5 @@ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest' +import { PRINT_SRC_ATTR } from '../../../src/components/Studio/bookRender' import { printSheets } from '../../../src/components/Studio/printSheets' /** @@ -269,6 +270,44 @@ describe('the save button', () => { expect(saveButton().disabled).toBe(false) }) + it('enables saving after the last photo even when the sandbox blocks frame timers (#3309)', async () => { + vi.useFakeTimers() + const close = open() + try { + const iframe = frame() + const doc = document.implementation.createHTMLDocument('print') + doc.body.innerHTML = `` + const photo = doc.images[0] + Object.defineProperties(photo, { + complete: { get: () => photo.hasAttribute('src') }, + src: { + set: (src: string) => { + photo.setAttribute('src', src) + photo.dispatchEvent(new Event('load')) + }, + }, + }) + Object.defineProperty(iframe, 'contentDocument', { value: doc }) + // Chromium drops callbacks scheduled on the sandboxed window. + Object.defineProperty(iframe, 'contentWindow', { value: { setTimeout: () => 1 } }) + + // The eagerly loaded SVG image settles before the frame's load event. + doc.querySelector('image')?.dispatchEvent(new Event('load')) + iframe.dispatchEvent(new Event('load')) + await vi.advanceTimersByTimeAsync(0) + expect(saveButton().textContent).toBe('Preparing 1/1') + expect(saveButton().disabled).toBe(true) + + await vi.advanceTimersByTimeAsync(8_000) + expect(saveButton().disabled).toBe(false) + expect(saveButton().textContent).toBe('Save as PDF') + expect(iframe.getAttribute('sandbox')).toBe('allow-same-origin allow-modals') + } finally { + close() + vi.useRealTimers() + } + }) + it('prints the frame, not the page around it', async () => { open() const iframe = frame()