From a4968ebde14b71c8fd471ea87c00af1d56a8d59b Mon Sep 17 00:00:00 2001 From: tomasz stryjewski Date: Thu, 16 Apr 2015 10:55:42 +0200 Subject: [PATCH 1/4] fix(modal): skipping ESC handling for form inputs; fixes #2544 --- package.json | 3 +++ src/modal/modal.js | 4 ++++ src/modal/test/modal.spec.js | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/package.json b/package.json index a02bc584b4..03af5b8601 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,9 @@ "version": "0.13.0-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 b2a3e3f4d0..f1e4a31b63 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -253,6 +253,10 @@ angular.module('ui.bootstrap.modal', []) $document.bind('keydown', function (evt) { var modal; + if ( /^(input|textarea|select)$/i.test( evt.target.nodeName ) ) { + return evt; + } + if (evt.which === 27) { modal = openedWindows.top(); if (modal && modal.value.keyboard) { diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index c1d5fe4c29..5b7fc1011c 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -238,6 +238,28 @@ describe('$modal', function () { expect($document).toHaveModalsOpen(0); }); + [ 'input', 'textarea', 'select' ].forEach( function ( input ) { + it('should not close on ESC if event target is <' + input + '>', function () { + var template = input === 'input' ? '' : '<' + input + '>'; + + var modal = open({template: '
' + template + '
' }); + expect($document).toHaveModalsOpen(1); + + triggerKeyDown( angular.element( input ), 27); + $timeout.flush(); + $rootScope.$digest(); + + expect($document).toHaveModalsOpen(1); + + triggerKeyDown($document, 27); + $timeout.flush(); + $rootScope.$digest(); + + expect($document).toHaveModalsOpen(0); + }); + }); + + it('should support closing on backdrop click', function () { var modal = open({template: '
Content
'}); From 0f98ba9a5c947bdde53b65795982aa5fce4ee46d Mon Sep 17 00:00:00 2001 From: tomasz stryjewski Date: Thu, 16 Apr 2015 11:34:40 +0200 Subject: [PATCH 2/4] fix(modal): cleaner regexp for matching inputs; fixes #2544 --- src/modal/modal.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/modal/modal.js b/src/modal/modal.js index f1e4a31b63..3aa0234601 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -253,7 +253,7 @@ angular.module('ui.bootstrap.modal', []) $document.bind('keydown', function (evt) { var modal; - if ( /^(input|textarea|select)$/i.test( evt.target.nodeName ) ) { + if ( /^(?:input|textarea|select)$/i.test( evt.target.nodeName ) ) { return evt; } From 9cfd82c45f35da94df2b9a6d5914fa9b3fc66d68 Mon Sep 17 00:00:00 2001 From: tomasz stryjewski Date: Tue, 4 Aug 2015 17:36:43 +0200 Subject: [PATCH 3/4] fix(modal): ammended the PR as per request --- src/modal/modal.js | 6 +++--- src/modal/test/modal.spec.js | 30 +++++++++++++++--------------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/modal/modal.js b/src/modal/modal.js index 3aa0234601..ab800613fa 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -251,11 +251,11 @@ angular.module('ui.bootstrap.modal', []) } $document.bind('keydown', function (evt) { - var modal; - - if ( /^(?:input|textarea|select)$/i.test( evt.target.nodeName ) ) { + if (evt.isPropagationStopped() || /^(?:input|textarea|select)$/i.test(evt.target.nodeName)) { return evt; } + + var modal; if (evt.which === 27) { modal = openedWindows.top(); diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index 5b7fc1011c..ddcfb6fbe0 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -238,28 +238,28 @@ describe('$modal', function () { expect($document).toHaveModalsOpen(0); }); - [ 'input', 'textarea', 'select' ].forEach( function ( input ) { - it('should not close on ESC if event target is <' + input + '>', function () { - var template = input === 'input' ? '' : '<' + input + '>'; + it('should not close on ESC if event.preventDefault() was issued', function () { + var modal = open({template: '
' }); + expect($document).toHaveModalsOpen(1); - var modal = open({template: '
' + template + '
' }); - expect($document).toHaveModalsOpen(1); + var input = angular.element( 'input' ) + .on( 'keydown', function ( evt ) { + evt.preventDefault(); + }); - triggerKeyDown( angular.element( input ), 27); - $timeout.flush(); - $rootScope.$digest(); + triggerKeyDown( input, 27); + $timeout.flush(); + $rootScope.$digest(); - expect($document).toHaveModalsOpen(1); + expect($document).toHaveModalsOpen(1); - triggerKeyDown($document, 27); - $timeout.flush(); - $rootScope.$digest(); + triggerKeyDown($document, 27); + $timeout.flush(); + $rootScope.$digest(); - expect($document).toHaveModalsOpen(0); - }); + expect($document).toHaveModalsOpen(0); }); - it('should support closing on backdrop click', function () { var modal = open({template: '
Content
'}); From b44f25024d7212813780e9ba6678d4d4c161770e Mon Sep 17 00:00:00 2001 From: tomasz stryjewski Date: Thu, 6 Aug 2015 11:26:02 +0200 Subject: [PATCH 4/4] fix(modal): ammended the PR as per request, this time for real --- src/modal/modal.js | 5 +++-- src/modal/test/modal.spec.js | 18 +++++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/modal/modal.js b/src/modal/modal.js index ab800613fa..a4a9cab756 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -251,10 +251,11 @@ angular.module('ui.bootstrap.modal', []) } $document.bind('keydown', function (evt) { - if (evt.isPropagationStopped() || /^(?:input|textarea|select)$/i.test(evt.target.nodeName)) { + + if (evt.isDefaultPrevented()) { return evt; } - + var modal; if (evt.which === 27) { diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index ddcfb6fbe0..c85be2e33f 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -239,25 +239,29 @@ describe('$modal', function () { }); it('should not close on ESC if event.preventDefault() was issued', function () { - var modal = open({template: '
' }); + var modal = open({template: '
' }); expect($document).toHaveModalsOpen(1); - var input = angular.element( 'input' ) - .on( 'keydown', function ( evt ) { - evt.preventDefault(); - }); + var button = angular.element('button').bind('keydown', preventKeyDown); - triggerKeyDown( input, 27); + triggerKeyDown(button, 27); $timeout.flush(); $rootScope.$digest(); expect($document).toHaveModalsOpen(1); - triggerKeyDown($document, 27); + button.unbind('keydown', preventKeyDown); + + triggerKeyDown(button, 27); $timeout.flush(); $rootScope.$digest(); expect($document).toHaveModalsOpen(0); + + + function preventKeyDown(evt) { + evt.preventDefault(); + } }); it('should support closing on backdrop click', function () {