Navigation Menu

Skip to content

Commit

Permalink
Followup to #8753. Modify new Event constructor signature to jQuery.e…
Browse files Browse the repository at this point in the history
…vent(type, props), which can be exploited by jQuery.event.trigger as well.
  • Loading branch information
dmethvin committed Apr 12, 2011
1 parent 6d49e84 commit bebd8bc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
24 changes: 9 additions & 15 deletions src/event.js
Expand Up @@ -304,9 +304,9 @@ jQuery.event = {
// jQuery.Event object // jQuery.Event object
event[ jQuery.expando ] ? event : event[ jQuery.expando ] ? event :
// Object literal // Object literal
jQuery.extend( new jQuery.Event(type), event ) : new jQuery.Event( type, event ) :
// Just the event type (string) // Just the event type (string)
new jQuery.Event(type); new jQuery.Event( type );


event.type = type; event.type = type;
event.namespace = namespaces.join("."); event.namespace = namespaces.join(".");
Expand Down Expand Up @@ -563,26 +563,15 @@ jQuery.removeEvent = document.removeEventListener ?
} }
}; };


jQuery.Event = function( src ) { jQuery.Event = function( src, props ) {
// Allow instantiation without the 'new' keyword // Allow instantiation without the 'new' keyword
if ( !this.preventDefault ) { if ( !this.preventDefault ) {
return new jQuery.Event( src ); return new jQuery.Event( src, props );
} }


// Event object // Event object
if ( src && src.type ) { if ( src && src.type ) {
this.originalEvent = src; this.originalEvent = src;

// Push explicitly provided properties onto the event object
for ( var prop in src ) {
// Ensure we don't clobber jQuery.Event prototype
// with own properties.
if ( hasOwn.call( src, prop ) ) {
this[ prop ] = src[ prop ];
}
}

// Always ensure a type has been explicitly set
this.type = src.type; this.type = src.type;


// Events bubbling up the document may have been marked as prevented // Events bubbling up the document may have been marked as prevented
Expand All @@ -595,6 +584,11 @@ jQuery.Event = function( src ) {
this.type = src; this.type = src;
} }


// Put explicitly provided properties onto the event object
if ( props ) {
jQuery.extend( this, props );
}

// timeStamp is buggy for some events on Firefox(#3843) // timeStamp is buggy for some events on Firefox(#3843)
// So we won't rely on the native value // So we won't rely on the native value
this.timeStamp = jQuery.now(); this.timeStamp = jQuery.now();
Expand Down
4 changes: 2 additions & 2 deletions test/unit/event.js
Expand Up @@ -978,11 +978,11 @@ test("trigger(eventObject, [data], [fn])", function() {
$parent.unbind().remove(); $parent.unbind().remove();
}); });


test("jQuery.Event({ /* props */ })", function() { test("jQuery.Event( type, props )", function() {


expect(4); expect(4);


var event = jQuery.Event({ type: "keydown", keyCode: 64 }), var event = jQuery.Event( "keydown", { keyCode: 64 }),
handler = function( event ) { handler = function( event ) {
ok( "keyCode" in event, "Special property 'keyCode' exists" ); ok( "keyCode" in event, "Special property 'keyCode' exists" );
equal( event.keyCode, 64, "event.keyCode has explicit value '64'" ); equal( event.keyCode, 64, "event.keyCode has explicit value '64'" );
Expand Down

0 comments on commit bebd8bc

Please sign in to comment.