Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix backdrop "Cannot read property 'removeChild' of null" when removed from body #34014

Merged
merged 1 commit into from
May 21, 2021
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: 5 additions & 1 deletion js/src/util/backdrop.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ class Backdrop {

EventHandler.off(this._element, EVENT_MOUSEDOWN)

this._getElement().parentNode.removeChild(this._element)
const { parentNode } = this._getElement()
GeoSot marked this conversation as resolved.
Show resolved Hide resolved
if (parentNode) {
parentNode.removeChild(this._element)
}

this._isAppended = false
}

Expand Down
19 changes: 19 additions & 0 deletions js/tests/unit/util/backdrop.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,25 @@ describe('Backdrop', () => {
})
})

it('should not error if the backdrop no longer has a parent', done => {
const instance = new Backdrop({
isVisible: true,
isAnimated: true
})
const getElements = () => document.querySelectorAll(CLASS_BACKDROP)

instance.show(() => {
instance.hide(() => {
expect(getElements().length).toEqual(0)

// replace the fixture, which was just wiped out
fixtureEl = getFixture()
done()
})
document.body.innerHTML = 'changed'
})
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - without the fix, this test causes this failure:

Chrome Headless 90.0.4430.212 (Mac OS 10.15.7) Backdrop should not error if the backdrop no longer has a parent FAILED
	Uncaught TypeError: Cannot read property 'removeChild' of null thrown

describe('click callback', () => {
it('it should execute callback on click', done => {
const spy = jasmine.createSpy('spy')
Expand Down