]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
Fix bug where backdrop calls method on null if it is already removed from the body...
authorRyan Weaver <weaverryan+github@gmail.com>
Fri, 21 May 2021 22:16:05 +0000 (18:16 -0400)
committerGitHub <noreply@github.com>
Fri, 21 May 2021 22:16:05 +0000 (01:16 +0300)
Co-authored-by: Rohit Sharma <rohit2sharma95@gmail.com>
js/src/util/backdrop.js
js/tests/unit/util/backdrop.spec.js

index 4c18e99c085b49d5a16ac94985b65180f46f8dd3..c05c221ddfe5cfd8e8ee5d96c126433982c197cb 100644 (file)
@@ -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
   }
 
index ae342b09290a6c785d171a9a7d5ebd2d77a256cb..02dea5a25b6038cb16c8581c22963d4f79c9746a 100644 (file)
@@ -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')