Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 35504fa

Browse files
author
Marcy Sutton
committed
bug(ngAria): native controls fire a single click event
1 parent e24d968 commit 35504fa

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

src/ngAria/aria.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ function $AriaProvider() {
8383
ariaMultiline: true,
8484
ariaValue: true,
8585
tabindex: true,
86-
bindKeypress: true
86+
bindKeypress: true,
87+
keypressNodeNames: ['DIV', 'LI']
8788
};
8889

8990
/**
@@ -303,11 +304,18 @@ ngAriaModule.directive('ngShow', ['$aria', function($aria) {
303304
compile: function(elem, attr) {
304305
var fn = $parse(attr.ngClick, /* interceptorFn */ null, /* expensiveChecks */ true);
305306
return function(scope, elem, attr) {
307+
308+
function isNodeOneOf(elem, nodeTypeArray) {
309+
if (nodeTypeArray.indexOf(elem[0].nodeName) !== -1) {
310+
return true;
311+
}
312+
}
313+
306314
if ($aria.config('tabindex') && !elem.attr('tabindex')) {
307315
elem.attr('tabindex', 0);
308316
}
309317

310-
if ($aria.config('bindKeypress') && !attr.ngKeypress) {
318+
if ($aria.config('bindKeypress') && !attr.ngKeypress && isNodeOneOf(elem, $aria.config('keypressNodeNames'))) {
311319
elem.on('keypress', function(event) {
312320
if (event.keyCode === 32 || event.keyCode === 13) {
313321
scope.$apply(callback);

test/ngAria/ariaSpec.js

+42-5
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,24 @@ describe('$aria', function() {
488488

489489
it('should a trigger click from the keyboard', function() {
490490
scope.someAction = function() {};
491-
compileInput('<div ng-click="someAction()" tabindex="0"></div>');
491+
492+
var elements = $compile('<section>' +
493+
'<div class="div-click" ng-click="someAction(\'div\')" tabindex="0"></div>' +
494+
'<ul><li ng-click="someAction( \'li\')" tabindex="0"></li></ul>' +
495+
'</section>')(scope);
496+
497+
scope.$digest();
498+
492499
clickFn = spyOn(scope, 'someAction');
493500

494-
element.triggerHandler({type: 'keypress', keyCode: 32});
501+
var divElement = elements.find('div');
502+
var liElement = elements.find('li');
503+
504+
divElement.triggerHandler({type: 'keypress', keyCode: 32});
505+
liElement.triggerHandler({type: 'keypress', keyCode: 32});
495506

496-
expect(clickFn).toHaveBeenCalled();
507+
expect(clickFn).toHaveBeenCalledWith('div');
508+
expect(clickFn).toHaveBeenCalledWith('li');
497509
});
498510

499511
it('should not override existing ng-keypress', function() {
@@ -526,6 +538,31 @@ describe('$aria', function() {
526538
element.triggerHandler({ type: 'keypress', keyCode: 13 });
527539
expect(element.text()).toBe('keypress13');
528540
});
541+
542+
it('should not bind keypress to non-div elements', function() {
543+
compileInput('<button ng-click="event = $event">{{event.type}}{{event.keyCode}}</button>');
544+
expect(element.text()).toBe('');
545+
element.triggerHandler({ type: 'keypress', keyCode: 13 });
546+
expect(element.text()).toBe('');
547+
});
548+
});
549+
550+
describe('actions for different configured elements', function() {
551+
beforeEach(configAriaProvider({
552+
keypressNodeNames: ['DIV']
553+
}));
554+
beforeEach(injectScopeAndCompiler);
555+
556+
it('should not a trigger click', function() {
557+
scope.someAction = function() {};
558+
var clickFn = spyOn(scope, 'someAction');
559+
560+
element = $compile('<li ng-click="someAction()" tabindex="0"></li>')(scope);
561+
562+
element.triggerHandler({type: 'keypress', keyCode: 32});
563+
564+
expect(clickFn).not.toHaveBeenCalled();
565+
});
529566
});
530567

531568
describe('actions when bindKeypress set to false', function() {
@@ -534,11 +571,11 @@ describe('$aria', function() {
534571
}));
535572
beforeEach(injectScopeAndCompiler);
536573

537-
it('should not a trigger click from the keyboard', function() {
574+
it('should not a trigger click', function() {
538575
scope.someAction = function() {};
539576
var clickFn = spyOn(scope, 'someAction');
540577

541-
element = $compile('<div ng-click="someAction()" tabindex="0">></div>')(scope);
578+
element = $compile('<div ng-click="someAction()" tabindex="0"></div>')(scope);
542579

543580
element.triggerHandler({type: 'keypress', keyCode: 32});
544581

0 commit comments

Comments
 (0)