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

fix(jqLite): pass in a dummy event with triggerHandler #2408

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 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 @@ -743,9 +743,18 @@ forEach({

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

if (document.createEventObject) {
event = document.createEventObject();
event.type = eventName;
} else {
event = document.createEvent('HTMLEvents');
event.initEvent(eventName, false, true);
}

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

it('should pass a dummy event', function() {
var element = jqLite('<a>poke</a>'),
pokeSpy = jasmine.createSpy('poke'),
event;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation is off


element.bind('poke', pokeSpy);

element.triggerHandler('poke');
event = pokeSpy.mostRecentCall.args[0];
expect(event.type).toEqual('poke');
expect(event.preventDefault).toBeDefined();
});
});


Expand Down