Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/host/core/browser/host.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ test('open requests go to the host, which decides', async ({page}) => {
test('a renderer gets built-in renderer documents by name only, and can run one in a frame of its own', async ({page}) => {
await open(page, 'renderer=hostile&probe=documents')
const alert = page.getByRole('alert')
// The probe reports what the host answered, then again once the built-in document runs nested, under the nesting
// frame's policy, and speaks the protocol to it: each report carries both, so neither can replace the other.
await expect(alert).toContainText(
/documents graph=html; hostile=refused \(hostile is a custom renderer; hosts supply only built-in renderers\); no-such-renderer=refused \(no-such-renderer is not a built-in renderer\)/,
/documents graph=html; hostile=refused \(hostile is a custom renderer; hosts supply only built-in renderers\); no-such-renderer=refused \(no-such-renderer is not a built-in renderer\); nested graph ready/,
)
// The built-in document runs nested, under the nesting frame's policy, and speaks the protocol to it.
await expect(alert).toContainText('nested graph ready')
// The request naming a path was dropped as invalid, never answered.
await expect(alert).not.toContainText('../renderers')
})
Expand Down
21 changes: 15 additions & 6 deletions tools/storybook/test-renderers/hostile/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -220,26 +220,35 @@
if (probe === 'documents') {
const answers = {}
const names = ['graph', 'hostile', 'no-such-renderer']
// One report, sent again whenever it changes: the answers, then whether the nested renderer is ready. A
// second report replaces the first, so each carries everything known so far, whichever arrives first.
let nested = 'loading'
const documents = () => {
if (Object.keys(answers).length === names.length)
report(`documents ${names.map(name => `${name}=${answers[name]}`).join('; ')}; nested graph ${nested}`)
}
addEventListener('message', event => {
if (event.source === parent && event.data?.channel === channel && event.data.type === 'document') {
const {renderer, html, error} = event.data
answers[renderer] = html !== undefined ? (/^<!doctype html>/i.test(html) ? 'html' : 'other') : `refused (${error})`
if (renderer === 'graph' && html) nest(html)
if (Object.keys(answers).length === names.length)
report(`documents ${names.map(name => `${name}=${answers[name]}`).join('; ')}`)
if (renderer === 'graph' && html)
nest(html, () => {
nested = 'ready'
documents()
})
documents()
}
})
// A URL or path is not a renderer name: the host drops the request.
post({type: 'document-request', renderer: '../renderers/graph/index.html'})
for (const name of names) post({type: 'document-request', renderer: name})
}
function nest(html) {
function nest(html, ready) {
const nested = 'nested-channel-0123456789'
const frame = document.createElement('iframe')
frame.setAttribute('sandbox', 'allow-scripts')
addEventListener('message', event => {
if (event.source === frame.contentWindow && event.data?.channel === nested && event.data.type === 'ready')
report('nested graph ready')
if (event.source === frame.contentWindow && event.data?.channel === nested && event.data.type === 'ready') ready()
})
const fragment = `<script>location.hash = ${JSON.stringify(`channel=${nested}&origin=null`)}<\/script>`
frame.srcdoc = html.replace(/<head>/i, `<head>${fragment}`)
Expand Down
Loading