From e4d4c5d0bc9f5560e730ec3df06a538400e6ff6d Mon Sep 17 00:00:00 2001 From: Johann-S Date: Sun, 28 Jul 2019 12:19:00 +0200 Subject: [PATCH] Backport #29155 allow dynamic modal body for scrollable modals --- js/src/modal.js | 5 +++-- js/tests/unit/modal.js | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/js/src/modal.js b/js/src/modal.js index 99fe1bf2d0..d6abfdec87 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -236,6 +236,7 @@ class Modal { _showElement(relatedTarget) { const transition = $(this._element).hasClass(ClassName.FADE) + const modalBody = this._dialog ? this._dialog.querySelector(Selector.MODAL_BODY) : null if (!this._element.parentNode || this._element.parentNode.nodeType !== Node.ELEMENT_NODE) { @@ -247,8 +248,8 @@ class Modal { this._element.removeAttribute('aria-hidden') this._element.setAttribute('aria-modal', true) - if ($(this._dialog).hasClass(ClassName.SCROLLABLE)) { - this._dialog.querySelector(Selector.MODAL_BODY).scrollTop = 0 + if ($(this._dialog).hasClass(ClassName.SCROLLABLE) && modalBody) { + modalBody.scrollTop = 0 } else { this._element.scrollTop = 0 } diff --git a/js/tests/unit/modal.js b/js/tests/unit/modal.js index f14ef3290a..d22d8a1de7 100644 --- a/js/tests/unit/modal.js +++ b/js/tests/unit/modal.js @@ -812,4 +812,25 @@ $(function () { }) .bootstrapModal('show') }) + + QUnit.test('should set .modal\'s scroll top to 0 if .modal-dialog-scrollable and modal body do not exists', function (assert) { + assert.expect(1) + var done = assert.async() + + var $modal = $([ + '' + ].join('')).appendTo('#qunit-fixture') + + + $modal.on('shown.bs.modal', function () { + assert.strictEqual($modal.scrollTop(), 0) + done() + }) + .bootstrapModal('show') + }) })