From a05b9c1ac59661f13e06ddefdb2845ead8c6ce8d Mon Sep 17 00:00:00 2001 From: tomasz stryjewski Date: Thu, 16 Apr 2015 10:55:42 +0200 Subject: [PATCH] fix(modal): skipping ESC handling for form inputs BREAKING CHANGE: Allow the user to hit `esc` inside an element in a modal and not exit the modal if the event has been `defaultPrevented` Closes #3551 Fixes #2544 --- package.json | 3 +++ src/modal/modal.js | 4 ++++ src/modal/test/modal.spec.js | 24 ++++++++++++++++++++++++ 3 files changed, 31 insertions(+) diff --git a/package.json b/package.json index 1eb1866d9d..50c8983bd4 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,9 @@ "version": "0.13.3-SNAPSHOT", "homepage": "http://angular-ui.github.io/bootstrap/", "dependencies": {}, + "scripts":{ + "test": "grunt" + }, "repository": { "type": "git", "url": "https://github.com/angular-ui/bootstrap.git" diff --git a/src/modal/modal.js b/src/modal/modal.js index 360f87b039..11527341ee 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -339,6 +339,10 @@ angular.module('ui.bootstrap.modal', []) } $document.bind('keydown', function (evt) { + if (evt.isDefaultPrevented()) { + return evt; + } + var modal = openedWindows.top(); if (modal && modal.value.keyboard) { switch (evt.which){ diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index e79062ae9f..8f68396db7 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -243,6 +243,30 @@ describe('$modal', function () { expect($document).toHaveModalsOpen(0); }); + it('should not close on ESC if event.preventDefault() was issued', function () { + var modal = open({template: '
' }); + expect($document).toHaveModalsOpen(1); + + var button = angular.element('button').on('keydown', preventKeyDown); + + triggerKeyDown(button, 27); + $rootScope.$digest(); + + expect($document).toHaveModalsOpen(1); + + button.off('keydown', preventKeyDown); + + triggerKeyDown(button, 27); + $animate.triggerCallbacks(); + $rootScope.$digest(); + + expect($document).toHaveModalsOpen(0); + + function preventKeyDown(evt) { + evt.preventDefault(); + } + }); + it('should support closing on backdrop click', function () { var modal = open({template: '
Content
'});