From d2621e3f985b8ca3e2d12a3c9a0f24562000bf9c Mon Sep 17 00:00:00 2001 From: AndriIushchuk Date: Mon, 18 Jan 2016 13:51:03 +0200 Subject: [PATCH] fix(modal): ensure shift+tab is trapped in modal - Fixes where shift+tab keystrokes are not causing focus to remain trapped in modal Closes #5294 Fixes #5229 --- src/modal/modal.js | 12 +++++++++++- src/modal/test/modal.spec.js | 12 ++++++++++-- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/modal/modal.js b/src/modal/modal.js index c88840562a..67101c74d2 100644 --- a/src/modal/modal.js +++ b/src/modal/modal.js @@ -404,7 +404,7 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap']) $modalStack.loadFocusElementList(modal); var focusChanged = false; if (evt.shiftKey) { - if ($modalStack.isFocusInFirstItem(evt)) { + if ($modalStack.isFocusInFirstItem(evt) || $modalStack.isModalFocused(evt, modal)) { focusChanged = $modalStack.focusLastFocusableElement(); } } else { @@ -547,6 +547,16 @@ angular.module('ui.bootstrap.modal', ['ui.bootstrap.stackedMap']) return false; }; + $modalStack.isModalFocused = function(evt, modalWindow) { + if (evt && modalWindow) { + var modalDomEl = modalWindow.value.modalDomEl; + if (modalDomEl && modalDomEl.length) { + return (evt.target || evt.srcElement) === modalDomEl[0]; + } + } + return false; + }; + $modalStack.isFocusInFirstItem = function(evt) { if (focusableElementList.length > 0) { return (evt.target || evt.srcElement) === focusableElementList[0]; diff --git a/src/modal/test/modal.spec.js b/src/modal/test/modal.spec.js index 3695927dff..676c0d30be 100644 --- a/src/modal/test/modal.spec.js +++ b/src/modal/test/modal.spec.js @@ -620,11 +620,15 @@ describe('$uibModal', function() { template:'' + '' }); + $rootScope.$digest(); expect($document).toHaveModalsOpen(1); + triggerKeyDown(angular.element(document.activeElement), 9, true); + expect(document.activeElement.getAttribute('id')).toBe('tab-focus-button'); + var lastElement = angular.element(document.getElementById('tab-focus-link')); lastElement.focus(); - triggerKeyDown(lastElement, 9, true); + triggerKeyDown(angular.element(document.activeElement), 9, true); expect(document.activeElement.getAttribute('id')).toBe('tab-focus-button'); initialPage.remove(); @@ -660,11 +664,15 @@ describe('$uibModal', function() { '', keyboard: false }); + $rootScope.$digest(); expect($document).toHaveModalsOpen(1); + triggerKeyDown(angular.element(document.activeElement), 9, true); + expect(document.activeElement.getAttribute('id')).toBe('tab-focus-button'); + var lastElement = angular.element(document.getElementById('tab-focus-link')); lastElement.focus(); - triggerKeyDown(lastElement, 9, true); + triggerKeyDown(angular.element(document.activeElement), 9, true); expect(document.activeElement.getAttribute('id')).toBe('tab-focus-button'); initialPage.remove();