Skip to main content

Bug Tracker

Side navigation

Ticket #3662: event-object[5953].diff


File event-object[5953].diff, 12.1 KB (added by flesler, November 28, 2008 11:58PM UTC)
Index: src/event.js
===================================================================
--- src/event.js	(revision 5953)
+++ src/event.js	(working copy)
@@ -155,69 +155,59 @@
 		}
 	},
 
-	trigger: function(type, data, elem, donative, extra) {
-		// Clone the incoming data, if any
-		data = jQuery.makeArray(data);
-
-		if ( type.indexOf("!") >= 0 ) {
-			type = type.slice(0, -1);
-			var exclusive = true;
-		}
-
+	trigger: function( e, args, elem, donative, extra) {
+		// Event object or event type
+		var type = e.type || e;
+		
 		// Handle a global trigger
 		if ( !elem ) {
 			// Only trigger if we've ever bound an event for it
 			if ( this.global[type] )
 				jQuery.each( jQuery.cache, function(){
 					if ( this.events && this.events[type] )
-						jQuery.event.trigger( type, data, this.handle.elem );
+						jQuery.event.trigger( type, args, this.handle.elem );
 				});
 
 		// Handle triggering a single element
 		} else {
+
 			// don't do events on text and comment nodes
 			if ( elem.nodeType == 3 || elem.nodeType == 8 )
-				return undefined;
+				return;
 
-			var val, ret, fn = jQuery.isFunction( elem[ type ] || null ),
-				// Check to see if we need to provide a fake event, or not
-				event = !data[0] || !data[0].preventDefault;
-
-			// Pass along a fake event
-			if ( event ) {
-				data.unshift({
-					type: type,
-					target: elem,
-					preventDefault: function(){},
-					stopPropagation: function(){},
-					stopImmediatePropagation:stopImmediatePropagation,
-					timeStamp: now()
-				});
-				data[0][expando] = true; // no need to fix fake event
+			// Clone the incoming args, if any
+			args = jQuery.makeArray(args);
+			
+			if ( type.indexOf("!") >= 0 ) {
+				type = type.slice(0, -1);
+				var exclusive = true;
 			}
+			
+			if( !e.type )
+				e = new jQuery.Event(type);
+			e.target = elem;
+			e.exclusive = exclusive;
+			
+			args.unshift( e );
 
-			// Enforce the right trigger type
-			data[0].type = type;
-			if ( exclusive )
-				data[0].exclusive = true;
+			var val, ret, fn = jQuery.isFunction( elem[ type ] );
 
 			// Trigger the event, it is assumed that "handle" is a function
 			var handle = jQuery.data(elem, "handle");
 			if ( handle )
-				val = handle.apply( elem, data );
+				val = handle.apply( elem, args );
 
 			// Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
-			if ( (!fn || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
+			if ( (!fn || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, args ) === false )
 				val = false;
 
 			// Extra functions don't get the custom event object
-			if ( event )
-				data.shift();
+			args.shift();
 
 			// Handle triggering of extra function
 			if ( extra && jQuery.isFunction( extra ) ) {
 				// call the extra function and tack the current return value on the end for possible inspection
-				ret = extra.apply( elem, val == null ? data : data.concat( val ) );
+				ret = extra.apply( elem, val == null ? args : args.concat( val ) );
 				// if anything is returned, give it precedence and have it overwrite the previous value
 				if ( ret !== undefined )
 					val = ret;
@@ -239,31 +229,29 @@
 	},
 
 	handle: function(event) {
-		// returned undefined or false
-		var val, ret, namespace, all, handlers;
-
 		event = arguments[0] = jQuery.event.fix( event || window.event );
 
+		var val, 
+			ns = event.type.split("."),
+			// Cache this now, all = true means, any handler
+			all = !ns[1] && !event.exclusive, 
+			handlers = ( jQuery.data(this, "events") || {} )[ns[0]];
+
 		// Namespaced event handlers
-		namespace = event.type.split(".");
-		event.type = namespace[0];
-		namespace = namespace[1];
-		// Cache this now, all = true means, any handler
-		all = !namespace && !event.exclusive;
+		event.type = ns[0];
+		ns = ns[1];
 
-		handlers = ( jQuery.data(this, "events") || {} )[event.type];
-
 		for ( var j in handlers ) {
 			var handler = handlers[j];
 
 			// Filter the functions by class
-			if ( all || handler.type == namespace ) {
+			if ( all || handler.type == ns ) {
 				// Pass in a reference to the handler function itself
 				// So that we can later remove it
 				event.handler = handler;
 				event.data = handler.data;
 
-				ret = handler.apply( this, arguments );
+				var ret = handler.apply( this, arguments );
 
 				if ( val !== false )
 					val = ret;
@@ -282,7 +270,7 @@
 		return val;
 	},
 
-	props: "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode metaKey newValue originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target timeStamp toElement type view wheelDelta which".split(" "),
+	props: "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode metaKey newValue originalTarget pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target toElement type view wheelDelta which".split(" "),
 
 	fix: function(event) {
 		if ( event[expando] )
@@ -291,38 +279,13 @@
 		// store a copy of the original event object
 		// and "clone" to set read-only properties
 		var originalEvent = event;
-		event = { originalEvent: originalEvent };
+		event = new jQuery.Event( originalEvent );
 
 		for ( var i = this.props.length, prop; i; ){
 			prop = this.props[ --i ];
 			event[ prop ] = originalEvent[ prop ];
 		}
 
-		// Mark it as fixed
-		event[expando] = true;
-
-		// add preventDefault and stopPropagation since
-		// they will not work on the clone
-		event.preventDefault = function() {
-			// if preventDefault exists run it on the original event
-			if (originalEvent.preventDefault)
-				originalEvent.preventDefault();
-			// otherwise set the returnValue property of the original event to false (IE)
-			originalEvent.returnValue = false;
-		};
-		event.stopPropagation = function() {
-			// if stopPropagation exists run it on the original event
-			if (originalEvent.stopPropagation)
-				originalEvent.stopPropagation();
-			// otherwise set the cancelBubble property of the original event to true (IE)
-			originalEvent.cancelBubble = true;
-		};
-
-		event.stopImmediatePropagation = stopImmediatePropagation;
-
-		// Fix timeStamp
-		event.timeStamp = event.timeStamp || now();
-
 		// Fix target property, if necessary
 		if ( !event.target )
 			event.target = event.srcElement || document; // Fixes #1925 where srcElement might not be defined either
@@ -374,11 +337,49 @@
 	}
 };
 
-function stopImmediatePropagation(){
-	this._sip = 1;
-	this.stopPropagation();
-}
+jQuery.Event = function( src ){
+	// Event object
+	if( src && src.type ){
+		this.originalEvent = src;
+		this.type = src.type;
+		
+		// Fix timeStamp
+		this.timeStamp = src.timeStamp || now();
+	// Event type
+	}else
+		this.type = src;
 
+	// Mark it as fixed
+	this[expando] = true;
+};
+
+jQuery.Event.prototype = {
+	// add preventDefault and stopPropagation since
+	// they will not work on the clone
+	preventDefault: function() {
+		if( !this.originalEvent )
+			return;
+		// if preventDefault exists run it on the original event
+		if (this.originalEvent.preventDefault)
+			this.originalEvent.preventDefault();
+		// otherwise set the returnValue property of the original event to false (IE)
+		this.originalEvent.returnValue = false;
+	},
+	stopPropagation: function() {
+		if( !this.originalEvent )
+			return;
+		// if stopPropagation exists run it on the original event
+		if (this.originalEvent.stopPropagation)
+			this.originalEvent.stopPropagation();
+		// otherwise set the cancelBubble property of the original event to true (IE)
+		this.originalEvent.cancelBubble = true;
+	},
+	stopImmediatePropagation:function(){
+		this._sip = 1;
+		this.stopPropagation();
+	}
+};
+
 if ( !jQuery.browser.msie ){	
 	// Checks if an event happened on an element within another element
 	// Used in jQuery.event.special.mouseenter and mouseleave handlers
Index: test/unit/event.js
===================================================================
--- test/unit/event.js	(revision 5953)
+++ test/unit/event.js	(working copy)
@@ -182,7 +182,7 @@
 });
 
 test("trigger(event, [data], [fn])", function() {
-	expect(67);
+	expect(62);
 
 	var handler = function(event, a, b, c) {
 		equals( event.type, "click", "check passed data" );
@@ -213,48 +213,46 @@
 		equals( c, "abc", "check passed data" );
 		equals( v, "test", "check current value" );
 	};
+	
+	var $elem = jQuery("#firstp");
 
 	// Simulate a "native" click
-	jQuery("#firstp")[0].click = function(){
+	$elem[0].click = function(){
 		ok( true, "Native call was triggered" );
 	};
 
 	// Triggers handlrs and native
 	// Trigger 5
-	jQuery("#firstp").bind("click", handler).trigger("click", [1, "2", "abc"]);
+	$elem.bind("click", handler).trigger("click", [1, "2", "abc"]);
 
 	// Triggers handlers, native, and extra fn
 	// Triggers 9
-	jQuery("#firstp").trigger("click", [1, "2", "abc"], handler4);
+	$elem.trigger("click", [1, "2", "abc"], handler4);
 
 	// Simulate a "native" click
-	jQuery("#firstp")[0].click = function(){
+	$elem[0].click = function(){
 		ok( false, "Native call was triggered" );
 	};
 
-	// Triggers handlers, native, and extra fn
-	// Triggers 7
-	jQuery("#firstp").trigger("click", [1, "2", "abc"], handler2);
-
 	// Trigger only the handlers (no native)
 	// Triggers 5
-	equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
+	equals( $elem.triggerHandler("click", [1, "2", "abc"]), "test", "Verify handler response" );
 
 	// Trigger only the handlers (no native) and extra fn
 	// Triggers 8
-	equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
+	equals( $elem.triggerHandler("click", [1, "2", "abc"], handler2), false, "Verify handler response" );
 
 	// Build fake click event to pass in
-	var eventObj = jQuery.event.fix({ type: "foo", target: document.body });
+	var eventObj = new jQuery.Event("click");
 
 	// Trigger only the handlers (no native), with external event obj
 	// Triggers 5
-	equals( jQuery("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"]), "test", "Verify handler response" );
+	equals( $elem.triggerHandler(eventObj, [1, "2", "abc"]), "test", "Verify handler response" );
 
 	// Trigger only the handlers (no native) and extra fn, with external event obj
 	// Triggers 9
-	eventObj = jQuery.event.fix({ type: "foo", target: document.body });
-	equals( jQuery("#firstp").triggerHandler("click", [eventObj, 1, "2", "abc"], handler), "test", "Verify handler response" );
+	eventObj = new jQuery.Event("click");
+	equals( $elem.triggerHandler(eventObj, [1, "2", "abc"], handler2), false, "Verify handler response" );
 	
 	var pass = true;
 	try {
@@ -268,11 +266,23 @@
 
 	// have the extra handler override the return
 	// Triggers 9
-	equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
+	equals( $elem.triggerHandler("click", [1, "2", "abc"], handler3), "newVal", "Verify triggerHandler return is overwritten by extra function" );
 
 	// have the extra handler leave the return value alone
 	// Triggers 9
-	equals( jQuery("#firstp").triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
+	equals( $elem.triggerHandler("click", [1, "2", "abc"], handler4), "test", "Verify triggerHandler return is not overwritten by extra function" );
+	
+	eventObj = new jQuery.Event('foo');
+	eventObj.secret = 'boo!';
+	
+	$elem.unbind('click').bind('foo',function(e){
+		equals( e.type, 'foo', 'Verify event type when passed passing an event object' );
+		equals( e.target.id, 'firstp', 'Verify event.target when passed passing an event object' );
+		equals( e.secret, 'boo!', 'Verify event object\'s custom attribute when passed passing an event object' );
+	});
+	
+	$elem.triggerHandler( eventObj );
+	$elem.unbind('foo');
 });
 
 test("toggle(Function, Function, ...)", function() {

Download in other formats:

Original Format