Skip to content

Commit

Permalink
Modal.js: data-dismiss="modal" must trigger event.preventDefault on…
Browse files Browse the repository at this point in the history
…ly if it is <a> or <area>
  • Loading branch information
GeoSot committed May 10, 2021
1 parent 90b1a69 commit d68db77
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
6 changes: 3 additions & 3 deletions js/src/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Modal extends BaseComponent {
}

hide(event) {
if (event) {
if (event && ['A', 'AREA'].includes(event.target.tagName)) {
event.preventDefault()
}

Expand Down Expand Up @@ -271,8 +271,8 @@ class Modal extends BaseComponent {
EventHandler.off(document, EVENT_FOCUSIN) // guard against infinite focus loop
EventHandler.on(document, EVENT_FOCUSIN, event => {
if (document !== event.target &&
this._element !== event.target &&
!this._element.contains(event.target)) {
this._element !== event.target &&
!this._element.contains(event.target)) {
this._element.focus()
}
})
Expand Down
54 changes: 54 additions & 0 deletions js/tests/unit/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,60 @@ describe('Modal', () => {
trigger.click()
})

it('should not prevent default default when a click occurred on data-bs-dismiss="modal" where tagName is DIFFERENT than <a> or <area>', done => {
fixtureEl.innerHTML = [
'<div class="modal">',
' <div class="modal-dialog">',
' <button type="button" data-bs-dismiss="modal"></button>',
' </div>',
'</div>'
].join('')

const modalEl = fixtureEl.querySelector('.modal')
const btnClose = fixtureEl.querySelector('button[data-bs-dismiss="modal"]')
const modal = new Modal(modalEl)

spyOn(Event.prototype, 'preventDefault').and.callThrough()

modalEl.addEventListener('shown.bs.modal', () => {
btnClose.click()
})

modalEl.addEventListener('hidden.bs.modal', () => {
expect(Event.prototype.preventDefault).not.toHaveBeenCalled()
done()
})

modal.show()
})

it('should prevent default default when a click occurred on data-bs-dismiss="modal" where tagName is <a> or <area>', done => {
fixtureEl.innerHTML = [
'<div class="modal">',
' <div class="modal-dialog">',
' <a type="button" data-bs-dismiss="modal"></a>',
' </div>',
'</div>'
].join('')

const modalEl = fixtureEl.querySelector('.modal')
const btnClose = fixtureEl.querySelector('a[data-bs-dismiss="modal"]')
const modal = new Modal(modalEl)

spyOn(Event.prototype, 'preventDefault').and.callThrough()

modalEl.addEventListener('shown.bs.modal', () => {
btnClose.click()
})

modalEl.addEventListener('hidden.bs.modal', () => {
expect(Event.prototype.preventDefault).toHaveBeenCalled()
done()
})

modal.show()
})

it('should not focus the trigger if the modal is not visible', done => {
fixtureEl.innerHTML = [
'<a data-bs-toggle="modal" href="#" data-bs-target="#exampleModal" style="display: none;"></a>',
Expand Down

0 comments on commit d68db77

Please sign in to comment.