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

Commit 0401a7f

Browse files
juliemrIgorMinar
authored andcommittedMay 16, 2013
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 0401a7f

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
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

+16-1
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,7 @@ describe('jqLite', function() {
792792
if (msie < 9){
793793
var evnt = document.createEventObject();
794794
evnt.srcElement = element;
795-
evnt.relatedTarget = relatedTarget;
795+
evnt.relatedTarget = relatedTarget;
796796
element.fireEvent('on' + type, evnt);
797797
return;
798798
};
@@ -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).toBeDefined();
1170+
});
11561171
});
11571172

11581173

0 commit comments

Comments
 (0)
This repository has been archived.