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

Commit

Permalink
fix(jqLite): pass a dummy event into triggerHandler
Browse files Browse the repository at this point in the history
Previously, anchor elements could not be used with triggerHandler because
triggerHandler passes null as the event, and any anchor element with an empty
href automatically calls event.preventDefault(). Instead, pass a dummy event
when using triggerHandler, similar to what full jQuery does. Modified from
PR #2379.
  • Loading branch information
juliemr authored and IgorMinar committed May 16, 2013
1 parent 6798fec commit 7a2b95d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
* - [replaceWith()](http://api.jquery.com/replaceWith/)
* - [text()](http://api.jquery.com/text/)
* - [toggleClass()](http://api.jquery.com/toggleClass/)
* - [triggerHandler()](http://api.jquery.com/triggerHandler/) - Doesn't pass native event objects to handlers.
* - [triggerHandler()](http://api.jquery.com/triggerHandler/) - Passes a dummy event object to handlers.
* - [unbind()](http://api.jquery.com/unbind/) - Does not support namespaces
* - [val()](http://api.jquery.com/val/)
* - [wrap()](http://api.jquery.com/wrap/)
Expand Down Expand Up @@ -763,9 +763,10 @@ forEach({

triggerHandler: function(element, eventName) {
var eventFns = (JQLiteExpandoStore(element, 'events') || {})[eventName];
var event;

forEach(eventFns, function(fn) {
fn.call(element, null);
fn.call(element, {preventDefault: noop});
});
}
}, function(fn, name){
Expand Down
15 changes: 15 additions & 0 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1153,6 +1153,21 @@ describe('jqLite', function() {
expect(clickSpy1).toHaveBeenCalledOnce();
expect(clickSpy2).toHaveBeenCalledOnce();
});

it('should pass in a dummy event', function() {
// we need the event to have at least preventDefault because angular will call it on
// all anchors with no href automatically

var element = jqLite('<a>poke</a>'),
pokeSpy = jasmine.createSpy('poke'),
event;

element.bind('click', pokeSpy);

element.triggerHandler('click');
event = pokeSpy.mostRecentCall.args[0];
expect(event.preventDefault).toBe(angular.noop);
});
});


Expand Down

0 comments on commit 7a2b95d

Please sign in to comment.