Skip to content
This repository was archived by the owner on Aug 18, 2025. It is now read-only.

Commit 95e465e

Browse files
authored
Merge pull request #1752 from sdb1228/master
Fix uncaught DOMException error
2 parents 506cc2f + f67c198 commit 95e465e

2 files changed

Lines changed: 25 additions & 3 deletions

File tree

src/view/use-announcer/use-announcer.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ export default function useAnnouncer(contextId: ContextId): Announce {
4141
// unmounting after a timeout to let any announcements
4242
// during a mount be published
4343
setTimeout(function remove() {
44-
// not clearing the ref as it might have been set by a new effect
45-
getBodyElement().removeChild(el);
46-
44+
// checking if element exists before remove to prevent errors
45+
if (getBodyElement().contains(el)) {
46+
// not clearing the ref as it might have been set by a new effect
47+
getBodyElement().removeChild(el);
48+
}
4749
// if el was the current ref - clear it so that
4850
// we can get a warning if announce is called
4951
if (el === ref.current) {

test/unit/view/announcer.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ it('should remove the element when unmounting after a timeout', () => {
5959
expect(getElement('5')).not.toBeTruthy();
6060
});
6161

62+
it('should not remove the element when unmounting after a timeout if the element does not exist', () => {
63+
const { unmount } = render(
64+
<WithAnnouncer contextId="5">{getMock()}</WithAnnouncer>,
65+
);
66+
67+
const doc = new DOMParser().parseFromString(
68+
'<!doctype html><title>wat</title>',
69+
'text/html',
70+
);
71+
// $FlowFixMe
72+
document.write(doc);
73+
unmount();
74+
expect(() => jest.runOnlyPendingTimers()).not.toThrow(
75+
// $FlowFixMe
76+
new DOMException(
77+
"Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.",
78+
),
79+
);
80+
});
81+
6282
it('should set the text content of the announcement element', () => {
6383
// arrange
6484
const mock = getMock();

0 commit comments

Comments
 (0)