Skip to content

Commit a2b5901

Browse files
Fix bug where backdrop calls method on null if it is already removed from the body (#34014)
Co-authored-by: Rohit Sharma <[email protected]>
1 parent d64466a commit a2b5901

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

js/src/util/backdrop.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,11 @@ class Backdrop {
116116

117117
EventHandler.off(this._element, EVENT_MOUSEDOWN)
118118

119-
this._getElement().parentNode.removeChild(this._element)
119+
const { parentNode } = this._getElement()
120+
if (parentNode) {
121+
parentNode.removeChild(this._element)
122+
}
123+
120124
this._isAppended = false
121125
}
122126

js/tests/unit/util/backdrop.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,25 @@ describe('Backdrop', () => {
129129
})
130130
})
131131

132+
it('should not error if the backdrop no longer has a parent', done => {
133+
const instance = new Backdrop({
134+
isVisible: true,
135+
isAnimated: true
136+
})
137+
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)
138+
139+
instance.show(() => {
140+
instance.hide(() => {
141+
expect(getElements().length).toEqual(0)
142+
143+
// replace the fixture, which was just wiped out
144+
fixtureEl = getFixture()
145+
done()
146+
})
147+
document.body.innerHTML = 'changed'
148+
})
149+
})
150+
132151
describe('click callback', () => {
133152
it('it should execute callback on click', done => {
134153
const spy = jasmine.createSpy('spy')

0 commit comments

Comments
 (0)