From c0f10276af578effbfaa5fc456f326a6eea08b8c Mon Sep 17 00:00:00 2001 From: wawyed Date: Fri, 4 Dec 2015 09:38:58 +0000 Subject: [PATCH] fix(modal): trap tabbing regardless of config - Ensure tabbing is trapped regardless of whether `keyboard` is `false` Closes #5010 Fixes #4990 --- src/modal/modal.js | 12 ++++++----- src/modal/test/modal.spec.js | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/modal/modal.js b/src/modal/modal.js index 7bcf36f098..18b9f53d9c 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -351,13 +351,15 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap']) } var modal = openedWindows.top(); - if (modal && modal.value.keyboard) { + if (modal) { switch (evt.which) { case 27: { - evt.preventDefault(); - $rootScope.$apply(function() { - $modalStack.dismiss(modal.key, 'escape key press'); - }); + if (modal.value.keyboard) { + evt.preventDefault(); + $rootScope.$apply(function() { + $modalStack.dismiss(modal.key, 'escape key press'); + }); + } break; } case 9: { diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index 413741fcf0..f9564b2ce6 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -527,6 +527,46 @@ describe('$uibModal', function () { initialPage.remove(); }); + + it('should change focus to first element when tab key is pressed when keyboard is false', function() { + var initialPage = angular.element('Outland link'); + angular.element(document.body).append(initialPage); + initialPage.focus(); + + open({ + template:'' + + '', + keyboard: false + }); + expect($document).toHaveModalsOpen(1); + + var lastElement = angular.element(document.getElementById('tab-focus-button')); + lastElement.focus(); + triggerKeyDown(lastElement, 9); + expect(document.activeElement.getAttribute('id')).toBe('tab-focus-link'); + + initialPage.remove(); + }); + + it('should change focus to last element when shift+tab keys are pressed when keyboard is false', function() { + var initialPage = angular.element('Outland link'); + angular.element(document.body).append(initialPage); + initialPage.focus(); + + open({ + template:'' + + '', + keyboard: false + }); + expect($document).toHaveModalsOpen(1); + + var lastElement = angular.element(document.getElementById('tab-focus-link')); + lastElement.focus(); + triggerKeyDown(lastElement, 9, true); + expect(document.activeElement.getAttribute('id')).toBe('tab-focus-button'); + + initialPage.remove(); + }); }); describe('default options can be changed in a provider', function() {