Corrected RegExp to match event namespaces. Fixes #5303.
[jquery.git] / src / event.js
index 8eaf44e..879f049 100644 (file)
@@ -57,7 +57,7 @@ jQuery.event = {
                        // Namespaced event handlers
                        var namespaces = type.split(".");
                        type = namespaces.shift();
-                       handler.type = namespaces.slice().sort().join(".");
+                       handler.type = namespaces.slice(0).sort().join(".");
 
                        // Get the current list of functions bound to this event
                        var handlers = events[ type ],
@@ -133,7 +133,7 @@ jQuery.event = {
                                        var namespaces = type.split(".");
                                        type = namespaces.shift();
                                        var all = !namespaces.length,
-                                               namespace = new RegExp("(^|\\.)" + namespaces.slice().sort().join(".*\\.") + "(\\.|$)"),
+                                               namespace = new RegExp("(^|\\.)" + namespaces.slice().sort().join("\\.(?:.*\\.)?") + "(\\.|$)"),
                                                special = this.special[ type ] || {};
 
                                        if ( events[ type ] ) {
@@ -291,7 +291,7 @@ jQuery.event = {
                // Cache this now, all = true means, any handler
                all = !namespaces.length && !event.exclusive;
 
-               var namespace = new RegExp("(^|\\.)" + namespaces.slice().sort().join(".*\\.") + "(\\.|$)");
+               var namespace = new RegExp("(^|\\.)" + namespaces.slice().sort().join("\\.(?:.*\\.)?") + "(\\.|$)");
 
                handlers = ( jQuery.data(this, "events") || {} )[ event.type ];
 
@@ -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();
@@ -516,28 +517,108 @@ var withinElement = function( event ) {
                // handle event if we actually just moused on to a non sub-element
                jQuery.event.handle.apply( this, arguments );
        }
+
+},
+
+// In case of event delegation, we only need to rename the event.type,
+// liveHandler will take care of the rest.
+delegate = function( event ) {
+       event.type = event.data;
+       jQuery.event.handle.apply( this, arguments );
 };
 
+// Create mouseenter and mouseleave events
 jQuery.each({
-       mouseover: 'mouseenter',
-       mouseout: 'mouseleave'
+       mouseover: "mouseenter",
+       mouseout: "mouseleave"
 }, function( orig, fix ) {
        jQuery.event.special[ fix ] = {
-               setup: function(){
-                       jQuery.event.add( this, orig, withinElement, fix );
+               setup: function(data){
+                       jQuery.event.add( this, orig, data && data.selector ? delegate : withinElement, fix );
                },
-               teardown: function(){
-                       jQuery.event.remove( this, orig, withinElement );
+               teardown: function(data){
+                       jQuery.event.remove( this, orig, data && data.selector ? delegate : withinElement );
+               }
+       };
+});
+
+(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,
+               handle = event.handle;
+       
+       function ieHandler() { 
+               arguments[0].type = orig;
+               return handle.apply(this, arguments);
+       }
+
+       event.special[orig] = {
+               setup:function() {
+                       if ( this.addEventListener )
+                               this.addEventListener( orig, handle, true );
+                       else
+                               event.add( this, fix, ieHandler );
+               }, 
+               teardown:function() { 
+                       if ( this.removeEventListener )
+                               this.removeEventListener( orig, handle, true );
+                       else
+                               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 +629,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 +653,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 );
                });
@@ -598,13 +693,14 @@ jQuery.fn.extend({
 
                return this.click( jQuery.event.proxy( fn, function( event ) {
                        // Figure out which function to execute
-                       this.lastToggle = ( this.lastToggle || 0 ) % i;
+                       var lastToggle = ( jQuery.data( this, 'lastToggle' + fn.guid ) || 0 ) % i;
+                       jQuery.data( this, 'lastToggle' + fn.guid, lastToggle + 1 );
 
                        // Make sure that clicks stop
                        event.preventDefault();
 
                        // and execute the function
-                       return args[ this.lastToggle++ ].apply( this, arguments ) || false;
+                       return args[ lastToggle ].apply( this, arguments ) || false;
                }));
        },
 
@@ -655,15 +751,23 @@ function liveHandler( event ) {
 
        jQuery.each( jQuery.data( this, "events" ).live || [], function( i, fn ) {
                if ( fn.live === event.type ) {
-                       var elem = jQuery( event.target ).closest( fn.selector )[0];
+                       var elem = jQuery( event.target ).closest( fn.selector, event.currentTarget )[0],
+                               related;
                        if ( elem ) {
-                               elems.push({ elem: elem, fn: fn });
+                               // Those two events require additional checking
+                               if ( fn.live === "mouseenter" || fn.live === "mouseleave" ) {
+                                       related = jQuery( event.relatedTarget ).closest( fn.selector )[0];
+                               }
+
+                               if ( !related || related !== elem ) {
+                                       elems.push({ elem: elem, fn: fn });
+                               }
                        }
                }
        });
 
        elems.sort(function( a, b ) {
-               return jQuery.data( a.elem, "closest" ) - jQuery.data( b.elem, "closest" );
+               return a.closer - b.closer;
        });
 
        jQuery.each(elems, function() {
@@ -688,6 +792,10 @@ jQuery.extend({
        ready: function() {
                // Make sure that the DOM is not already loaded
                if ( !jQuery.isReady ) {
+                       if ( !document.body ) {
+                               return setTimeout( jQuery.ready, 13 );
+                       }
+
                        // Remember that the DOM is ready
                        jQuery.isReady = true;
 
@@ -734,15 +842,22 @@ function bindReady() {
                // ensure firing before onload,
                // maybe late but safe also for iframes
                document.attachEvent("onreadystatechange", function() {
+                       // Make sure body exists, at least, in case IE gets a little overzealous (ticket #5443).
                        if ( document.readyState === "complete" ) {
                                document.detachEvent( "onreadystatechange", arguments.callee );
                                jQuery.ready();
                        }
                });
 
-               // If IE and not an iframe
+               // If IE and not a frame
                // continually check to see if the document is ready
-               if ( document.documentElement.doScroll && window === window.top ) (function() {
+               var toplevel = false;
+
+               try {
+                       toplevel = window.frameElement == null;
+               } catch(e){}
+
+               if ( document.documentElement.doScroll && toplevel ) (function() {
                        if ( jQuery.isReady ) {
                                return;
                        }
@@ -765,9 +880,9 @@ function bindReady() {
        jQuery.event.add( window, "load", jQuery.ready );
 }
 
-jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," +
-       "mousedown,mouseup,mousemove,mouseover,mouseout,mouseenter,mouseleave," +
-       "change,select,submit,keydown,keypress,keyup,error").split(","), function( i, name ) {
+jQuery.each( ("blur focus load resize scroll unload click dblclick " +
+       "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
+       "change select submit keydown keypress keyup error").split(" "), function( i, name ) {
 
        // Handle event binding
        jQuery.fn[ name ] = function( fn ) {
@@ -776,11 +891,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 +903,4 @@ jQuery( window ).bind( 'unload', function() {
                }
        }
 });
+@*/