mirror of
https://github.com/twbs/bootstrap.git
synced 2025-02-20 17:54:23 +01:00
Merge pull request #15930 from twbs/modal-restore-inline-padding
Modal: Apply any preexisting body padding again after closing
This commit is contained in:
commit
442d2dd458
16
js/modal.js
16
js/modal.js
@ -14,12 +14,13 @@
|
|||||||
// ======================
|
// ======================
|
||||||
|
|
||||||
var Modal = function (element, options) {
|
var Modal = function (element, options) {
|
||||||
this.options = options
|
this.options = options
|
||||||
this.$body = $(document.body)
|
this.$body = $(document.body)
|
||||||
this.$element = $(element)
|
this.$element = $(element)
|
||||||
this.$backdrop = null
|
this.$backdrop = null
|
||||||
this.isShown = null
|
this.isShown = null
|
||||||
this.scrollbarWidth = 0
|
this.originalBodyPad = null
|
||||||
|
this.scrollbarWidth = 0
|
||||||
|
|
||||||
if (this.options.remote) {
|
if (this.options.remote) {
|
||||||
this.$element
|
this.$element
|
||||||
@ -259,11 +260,12 @@
|
|||||||
|
|
||||||
Modal.prototype.setScrollbar = function () {
|
Modal.prototype.setScrollbar = function () {
|
||||||
var bodyPad = parseInt((this.$body.css('padding-right') || 0), 10)
|
var bodyPad = parseInt((this.$body.css('padding-right') || 0), 10)
|
||||||
|
this.originalBodyPad = document.body.style.paddingRight || ''
|
||||||
if (this.bodyIsOverflowing) this.$body.css('padding-right', bodyPad + this.scrollbarWidth)
|
if (this.bodyIsOverflowing) this.$body.css('padding-right', bodyPad + this.scrollbarWidth)
|
||||||
}
|
}
|
||||||
|
|
||||||
Modal.prototype.resetScrollbar = function () {
|
Modal.prototype.resetScrollbar = function () {
|
||||||
this.$body.css('padding-right', '')
|
this.$body.css('padding-right', this.originalBodyPad)
|
||||||
}
|
}
|
||||||
|
|
||||||
Modal.prototype.measureScrollbar = function () { // thx walsh
|
Modal.prototype.measureScrollbar = function () { // thx walsh
|
||||||
|
@ -301,4 +301,81 @@ $(function () {
|
|||||||
|
|
||||||
$toggleBtn.click()
|
$toggleBtn.click()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
QUnit.test('should restore inline body padding after closing', function (assert) {
|
||||||
|
var done = assert.async()
|
||||||
|
var originalBodyPad = 0
|
||||||
|
var $body = $(document.body)
|
||||||
|
|
||||||
|
$body.css('padding-right', originalBodyPad)
|
||||||
|
|
||||||
|
$('<div id="modal-test"/>')
|
||||||
|
.on('hidden.bs.modal', function () {
|
||||||
|
var currentBodyPad = parseInt($body.css('padding-right'), 10)
|
||||||
|
assert.notStrictEqual($body.attr('style'), '', 'body has non-empty style attribute')
|
||||||
|
assert.strictEqual(currentBodyPad, originalBodyPad, 'original body padding was not changed')
|
||||||
|
$body.removeAttr('style')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
.on('shown.bs.modal', function () {
|
||||||
|
$(this).bootstrapModal('hide')
|
||||||
|
})
|
||||||
|
.bootstrapModal('show')
|
||||||
|
})
|
||||||
|
|
||||||
|
QUnit.test('should ignore values set via CSS when trying to restore body padding after closing', function (assert) {
|
||||||
|
var done = assert.async()
|
||||||
|
var $body = $(document.body)
|
||||||
|
var $style = $('<style>body { padding-right: 42px; }</style>').appendTo('head')
|
||||||
|
|
||||||
|
$('<div id="modal-test"/>')
|
||||||
|
.on('hidden.bs.modal', function () {
|
||||||
|
assert.ok(!$body.attr('style'), 'body does not have inline padding set')
|
||||||
|
$style.remove()
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
.on('shown.bs.modal', function () {
|
||||||
|
$(this).bootstrapModal('hide')
|
||||||
|
})
|
||||||
|
.bootstrapModal('show')
|
||||||
|
})
|
||||||
|
|
||||||
|
QUnit.test('should ignore other inline styles when trying to restore body padding after closing', function (assert) {
|
||||||
|
var done = assert.async()
|
||||||
|
var $body = $(document.body)
|
||||||
|
var $style = $('<style>body { padding-right: 42px; }</style>').appendTo('head')
|
||||||
|
|
||||||
|
$body.css('color', 'red')
|
||||||
|
|
||||||
|
$('<div id="modal-test"/>')
|
||||||
|
.on('hidden.bs.modal', function () {
|
||||||
|
assert.strictEqual($body[0].style.paddingRight, '', 'body does not have inline padding set')
|
||||||
|
assert.strictEqual($body[0].style.color, 'red', 'body still has other inline styles set')
|
||||||
|
$body.removeAttr('style')
|
||||||
|
$style.remove()
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
.on('shown.bs.modal', function () {
|
||||||
|
$(this).bootstrapModal('hide')
|
||||||
|
})
|
||||||
|
.bootstrapModal('show')
|
||||||
|
})
|
||||||
|
|
||||||
|
QUnit.test('should properly restore non-pixel inline body padding after closing', function (assert) {
|
||||||
|
var done = assert.async()
|
||||||
|
var $body = $(document.body)
|
||||||
|
|
||||||
|
$body.css('padding-right', '5%')
|
||||||
|
|
||||||
|
$('<div id="modal-test"/>')
|
||||||
|
.on('hidden.bs.modal', function () {
|
||||||
|
assert.strictEqual($body[0].style.paddingRight, '5%', 'body does not have inline padding set')
|
||||||
|
$body.removeAttr('style')
|
||||||
|
done()
|
||||||
|
})
|
||||||
|
.on('shown.bs.modal', function () {
|
||||||
|
$(this).bootstrapModal('hide')
|
||||||
|
})
|
||||||
|
.bootstrapModal('show')
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user