fix: avoid focusing already removed iframe(closes #2178) #2750
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose
In some cases, a focus event can be raised for the element in the iframe at the moment when the iframe is removed from the document. For example in React application:
If you call the parent.postMessage from the iframe page and update the parent component state to remove the iframe element, the reference to the element in the "iframe" will be alive at the time when the "iframe" is fully removed from the page. React internal mechanism tries to focus this element. HammerHead's internal mechanism catches this focus event and tries to focus a non-existent element, which results in an error.
Approach
Check the "defaultView" value before making any manipulation in the focus method.
References
Error: "TypeError: window.location.toString is not a function"