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: 6 additions & 0 deletions src/utils/focus/getActiveElement.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {isDisabled} from '../misc/isDisabled'
import {isElementType} from '../misc/isElementType'

export function getActiveElement(
document: Document | ShadowRoot,
Expand All @@ -10,6 +11,11 @@ export function getActiveElement(
if (activeElementInShadowTree) {
return activeElementInShadowTree
}
} else if (activeElement && isElementType(activeElement, 'iframe')) {
const contentDocument = (activeElement as HTMLIFrameElement).contentDocument
if (contentDocument) {
return getActiveElement(contentDocument)
}
}
// Browser does not yield disabled elements as document.activeElement - jsdom does
if (isDisabled(activeElement)) {
Expand Down
23 changes: 23 additions & 0 deletions tests/keyboard/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,26 @@ customElements.define(
}
},
)

test('typing on focused element with iframe', async () => {
let iframe: HTMLIFrameElement
let iframeButton: HTMLElement
const {user} = setup(
'<div id="iframe-container"></div>',
)
iframe = document.createElement('iframe')
window.document.body.appendChild(iframe)
iframeButton = iframe.contentWindow!.document.createElement('button')
iframe.contentWindow!.document.body.appendChild(iframeButton)
let keydown = jest.fn()
let keyup = jest.fn()
iframeButton.addEventListener('keydown', keydown)
iframeButton.addEventListener('keyup', keyup)

iframeButton.focus()
await user.keyboard('[Space]')
expect(keydown).toHaveBeenCalled()
expect(keyup).toHaveBeenCalled()

iframe.remove()
})
Loading