From de7b79e0770e289efb51f435e6d16e2f2bc26ecc Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Mon, 17 May 2021 14:33:54 -0400 Subject: [PATCH] Fixing bug where backdrop calls method on null if it is already removed from the body Co-authored-by: Rohit Sharma --- js/src/util/backdrop.js | 6 +++++- js/tests/unit/util/backdrop.spec.js | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/js/src/util/backdrop.js b/js/src/util/backdrop.js index 4c18e99c085b..c05c221ddfe5 100644 --- a/js/src/util/backdrop.js +++ b/js/src/util/backdrop.js @@ -116,7 +116,11 @@ class Backdrop { EventHandler.off(this._element, EVENT_MOUSEDOWN) - this._getElement().parentNode.removeChild(this._element) + const { parentNode } = this._getElement() + if (parentNode) { + parentNode.removeChild(this._element) + } + this._isAppended = false } diff --git a/js/tests/unit/util/backdrop.spec.js b/js/tests/unit/util/backdrop.spec.js index ae342b09290a..02dea5a25b60 100644 --- a/js/tests/unit/util/backdrop.spec.js +++ b/js/tests/unit/util/backdrop.spec.js @@ -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' + }) + }) + describe('click callback', () => { it('it should execute callback on click', done => { const spy = jasmine.createSpy('spy')