Probably not the optimal solution, but tests pass.
[jquery.git] / src / event.js
index e911f29..0f976a0 100644 (file)
@@ -405,7 +405,7 @@ jQuery.event = {
                        add: function( proxy, data, namespaces ) {
                                jQuery.extend( proxy, data || {} );
                                proxy.guid += data.selector + data.live;
-                               jQuery.event.add( this, data.live, liveHandler );
+                               jQuery.event.add( this, data.live, liveHandler, data );
                        },
 
                        remove: function( namespaces ) {
@@ -467,6 +467,7 @@ jQuery.Event.prototype = {
                if ( !e ) {
                        return;
                }
+               
                // if preventDefault exists run it on the original event
                if ( e.preventDefault ) {
                        e.preventDefault();
@@ -518,9 +519,10 @@ var withinElement = function( event ) {
        }
 };
 
+// Create mouseenter and mouseleave events
 jQuery.each({
-       mouseover: 'mouseenter',
-       mouseout: 'mouseleave'
+       mouseover: "mouseenter",
+       mouseout: "mouseleave"
 }, function( orig, fix ) {
        jQuery.event.special[ fix ] = {
                setup: function(){
@@ -532,12 +534,84 @@ jQuery.each({
        };
 });
 
+(function() {
+       
+       var event = jQuery.event,
+               special = event.special,
+               handle  = event.handle;
+
+       special.submit = {
+               setup: function(data, namespaces) {
+                       if(data.selector) {
+                               event.add(this, 'click.specialSubmit', function(e, eventData) {
+                                       if(jQuery(e.target).filter(":submit, :image").closest(data.selector).length) {
+                                               e.type = "submit";
+                                               return handle.call( this, e, eventData );
+                                       }
+                               });
+                               
+                               event.add(this, 'keypress.specialSubmit', function( e, eventData ) {
+                                       if(jQuery(e.target).filter(":text, :password").closest(data.selector).length) {
+                                               e.type = "submit";
+                                               return handle.call( this, e, eventData );
+                                       }
+                               });
+                       } else {
+                               return false;
+                       }
+               },
+               
+               remove: function(namespaces) {
+                       event.remove(this, 'click.specialSubmit');
+                       event.remove(this, 'keypress.specialSubmit');
+               }
+       };
+       
+})();
+
+// Create "bubbling" focus and blur events
+jQuery.each({
+       focus: "focusin",
+       blur: "focusout"
+}, function( orig, fix ){
+       var event = jQuery.event,
+               special = event.special,
+               handle = event.handle;
+       
+       function ieHandler() { 
+               arguments[0].type = orig;
+               return handle.apply(this, arguments);
+       }
+
+       special[orig] = {
+               setup:function() {
+                       if ( this.addEventListener )
+                               this.addEventListener( orig, handle, true );
+                       else
+                               jQuery.event.add( this, fix, ieHandler );
+               }, 
+               teardown:function() { 
+                       if ( this.removeEventListener )
+                               this.removeEventListener( orig, handle, true );
+                       else
+                               jQuery.event.remove( this, fix, ieHandler );
+               }
+       };
+});
+
 jQuery.fn.extend({
+       // TODO: make bind(), unbind() and one() DRY!
        bind: function( type, data, fn, thisObject ) {
-               if ( jQuery.isFunction( data ) ) {
-                       if ( fn !== undefined ) {
-                               thisObject = fn;
+               // Handle object literals
+               if ( typeof type === "object" ) {
+                       for ( var key in type ) {
+                               this.bind(key, data, type[key], fn);
                        }
+                       return this;
+               }
+               
+               if ( jQuery.isFunction( data ) ) {
+                       thisObject = fn;
                        fn = data;
                        data = undefined;
                }
@@ -548,10 +622,16 @@ jQuery.fn.extend({
        },
 
        one: function( type, data, fn, thisObject ) {
-               if ( jQuery.isFunction( data ) ) {
-                       if ( fn !== undefined ) {
-                               thisObject = fn;
+               // Handle object literals
+               if ( typeof type === "object" ) {
+                       for ( var key in type ) {
+                               this.one(key, data, type[key], fn);
                        }
+                       return this;
+               }
+               
+               if ( jQuery.isFunction( data ) ) {
+                       thisObject = fn;
                        fn = data;
                        data = undefined;
                }
@@ -566,6 +646,14 @@ jQuery.fn.extend({
        },
 
        unbind: function( type, fn ) {
+               // Handle object literals
+               if ( typeof type === "object" && !type.preventDefault ) {
+                       for ( var key in type ) {
+                               this.unbind(key, type[key]);
+                       }
+                       return this;
+               }
+               
                return this.each(function() {
                        jQuery.event.remove( this, type, fn );
                });
@@ -776,11 +864,10 @@ jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
 });
 
 // Prevent memory leaks in IE
-// And prevent errors on refresh with events like mouseover in other browsers
 // Window isn't included so as not to unbind existing unload events
 // More info:
 //  - http://isaacschlueter.com/2006/10/msie-memory-leaks/
-//  - https://bugzilla.mozilla.org/show_bug.cgi?id=252542
+/*@cc_on
 jQuery( window ).bind( 'unload', function() {
        for ( var id in jQuery.cache ) {
                // Skip the window
@@ -789,3 +876,4 @@ jQuery( window ).bind( 'unload', function() {
                }
        }
 });
+@*/