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

Commit 7a2b95d

Browse files
juliemrIgorMinar
authored andcommitted
fix(jqLite): pass a dummy event into triggerHandler
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.
1 parent 6798fec commit 7a2b95d

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/jqLite.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
* - [replaceWith()](http://api.jquery.com/replaceWith/)
5555
* - [text()](http://api.jquery.com/text/)
5656
* - [toggleClass()](http://api.jquery.com/toggleClass/)
57-
* - [triggerHandler()](http://api.jquery.com/triggerHandler/) - Doesn't pass native event objects to handlers.
57+
* - [triggerHandler()](http://api.jquery.com/triggerHandler/) - Passes a dummy event object to handlers.
5858
* - [unbind()](http://api.jquery.com/unbind/) - Does not support namespaces
5959
* - [val()](http://api.jquery.com/val/)
6060
* - [wrap()](http://api.jquery.com/wrap/)
@@ -763,9 +763,10 @@ forEach({
763763

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

767768
forEach(eventFns, function(fn) {
768-
fn.call(element, null);
769+
fn.call(element, {preventDefault: noop});
769770
});
770771
}
771772
}, function(fn, name){

test/jqLiteSpec.js

+15
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,21 @@ describe('jqLite', function() {
11531153
expect(clickSpy1).toHaveBeenCalledOnce();
11541154
expect(clickSpy2).toHaveBeenCalledOnce();
11551155
});
1156+
1157+
it('should pass in a dummy event', function() {
1158+
// we need the event to have at least preventDefault because angular will call it on
1159+
// all anchors with no href automatically
1160+
1161+
var element = jqLite('<a>poke</a>'),
1162+
pokeSpy = jasmine.createSpy('poke'),
1163+
event;
1164+
1165+
element.bind('click', pokeSpy);
1166+
1167+
element.triggerHandler('click');
1168+
event = pokeSpy.mostRecentCall.args[0];
1169+
expect(event.preventDefault).toBe(angular.noop);
1170+
});
11561171
});
11571172

11581173

0 commit comments

Comments
 (0)