Skip to content

Commit

Permalink
fix(ngAria): change accessibility keypress event to use event.which i…
Browse files Browse the repository at this point in the history
…f it is provided

In Firefox, keyboard events for printable characters (e.g. space) do not use event.keyCode.
Use event.which if it is provided before falling back to event.keyCode.

Closes angular#11340
  • Loading branch information
andrewaustin authored and Narretz committed Apr 20, 2015
1 parent 992114f commit 249f9b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/ngAria/aria.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,8 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {

if ($aria.config('bindKeypress') && !attr.ngKeypress && !isNodeOneOf(elem, nodeBlackList)) {
elem.on('keypress', function(event) {
if (event.keyCode === 32 || event.keyCode === 13) {
var keyCode = event.which || event.keyCode;
if (keyCode === 32 || keyCode === 13) {
scope.$apply(callback);
}

Expand Down
24 changes: 23 additions & 1 deletion test/ngAria/ariaSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ describe('$aria', function() {

var clickFn;

it('should a trigger click from the keyboard', function() {
it('should trigger a click from the keyboard', function() {
scope.someAction = function() {};

var elements = $compile('<section>' +
Expand All @@ -620,6 +620,28 @@ describe('$aria', function() {
expect(clickFn).toHaveBeenCalledWith('li');
});

it('should trigger a click in browsers that provide event.which instead of event.keyCode', function() {
scope.someAction = function() {};

var elements = $compile('<section>' +
'<div class="div-click" ng-click="someAction(\'div\')" tabindex="0"></div>' +
'<ul><li ng-click="someAction( \'li\')" tabindex="0"></li></ul>' +
'</section>')(scope);

scope.$digest();

clickFn = spyOn(scope, 'someAction');

var divElement = elements.find('div');
var liElement = elements.find('li');

divElement.triggerHandler({type: 'keypress', which: 32});
liElement.triggerHandler({type: 'keypress', which: 32});

expect(clickFn).toHaveBeenCalledWith('div');
expect(clickFn).toHaveBeenCalledWith('li');
});

it('should not override existing ng-keypress', function() {
scope.someOtherAction = function() {};
var keypressFn = spyOn(scope, 'someOtherAction');
Expand Down

0 comments on commit 249f9b8

Please sign in to comment.