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.
This commit is contained in:
Julie 2013-04-15 15:52:56 -07:00 committed by Igor Minar
parent 6798fec439
commit 0401a7f598
2 changed files with 19 additions and 3 deletions

View file

@ -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/)
@ -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){

View file

@ -792,7 +792,7 @@ describe('jqLite', function() {
if (msie < 9){
var evnt = document.createEventObject();
evnt.srcElement = element;
evnt.relatedTarget = relatedTarget;
evnt.relatedTarget = relatedTarget;
element.fireEvent('on' + type, evnt);
return;
};
@ -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).toBeDefined();
});
});