Added the new jQuery.support object and removed all uses of jQuery.browser from withi...
[jquery.git] / src / event.js
index 4bf96fc..9d06457 100644 (file)
@@ -13,7 +13,7 @@ jQuery.event = {
 
                // For whatever reason, IE has trouble passing the window object
                // around, causing it to be cloned in the process
-               if ( jQuery.browser.msie && elem.setInterval )
+               if ( elem.setInterval && elem != window )
                        elem = window;
 
                // Make sure that the function being executed has a unique ID
@@ -40,8 +40,9 @@ jQuery.event = {
                        handle = jQuery.data(elem, "handle") || jQuery.data(elem, "handle", function(){
                                // Handle the second event of a trigger and when
                                // an event is called after a page has unloaded
-                               if ( typeof jQuery !== "undefined" && !jQuery.event.triggered )
-                                       return jQuery.event.handle.apply(arguments.callee.elem, arguments);
+                               return typeof jQuery !== "undefined" && !jQuery.event.triggered ?
+                                       jQuery.event.handle.apply(arguments.callee.elem, arguments) :
+                                       undefined;
                        });
                // Add elem as a property of the handle function
                // This is to prevent a memory leak with non-native
@@ -53,8 +54,8 @@ jQuery.event = {
                jQuery.each(types.split(/\s+/), function(index, type) {
                        // Namespaced event handlers
                        var parts = type.split(".");
-                       type = parts[0];
-                       handler.type = parts[1];
+                       type = parts.shift();
+                       handler.type = parts.sort().join(".");
 
                        // Get the current list of functions bound to this event
                        var handlers = events[type];
@@ -113,8 +114,9 @@ jQuery.event = {
                                // jQuery(...).unbind("mouseover mouseout", fn);
                                jQuery.each(types.split(/\s+/), function(index, type){
                                        // Namespaced event handlers
-                                       var parts = type.split(".");
-                                       type = parts[0];
+                                       var namespace = type.split(".");
+                                       type = namespace.shift();
+                                       namespace = RegExp("(^|\\.)" + namespace.sort().join(".*\\.") + "(\\.|$)");
 
                                        if ( events[type] ) {
                                                // remove the given handler for the given type
@@ -125,7 +127,7 @@ jQuery.event = {
                                                else
                                                        for ( handler in events[type] )
                                                                // Handle the removal of namespaced events
-                                                               if ( !parts[1] || events[type][handler].type == parts[1] )
+                                                               if ( namespace.test(events[type][handler].type) )
                                                                        delete events[type][handler];
 
                                                // remove generic event handler if no more handlers exist
@@ -246,10 +248,12 @@ jQuery.event = {
 
                // Namespaced event handlers
                namespace = event.type.split(".");
-               event.type = namespace[0];
-               namespace = namespace[1];
+               event.type = namespace.shift();
+
                // Cache this now, all = true means, any handler
-               all = !namespace && !event.exclusive;
+               all = !namespace.length && !event.exclusive;
+               
+               namespace = RegExp("(^|\\.)" + namespace.sort().join(".*\\.") + "(\\.|$)");
 
                handlers = ( jQuery.data(this, "events") || {} )[event.type];
 
@@ -257,7 +261,7 @@ jQuery.event = {
                        var handler = handlers[j];
 
                        // Filter the functions by class
-                       if ( all || handler.type == namespace ) {
+                       if ( all || namespace.test(handler.type) ) {
                                // Pass in a reference to the handler function itself
                                // So that we can later remove it
                                event.handler = handler;
@@ -379,39 +383,37 @@ function stopImmediatePropagation(){
        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
-       var withinElement = function(event) {
-               // Check if mouse(over|out) are still within the same parent element
-               var parent = event.relatedTarget;
-               // Traverse up the tree
-               while ( parent && parent != this )
-                       try { parent = parent.parentNode; }
-                       catch(e) { parent = this; }
-               
-               if( parent != this ){
-                       // set the correct event type
-                       event.type = event.data;
-                       // handle event if we actually just moused on to a non sub-element
-                       jQuery.event.handle.apply( this, arguments );
-               }
-       };
+// Checks if an event happened on an element within another element
+// Used in jQuery.event.special.mouseenter and mouseleave handlers
+var withinElement = function(event) {
+       // Check if mouse(over|out) are still within the same parent element
+       var parent = event.relatedTarget;
+       // Traverse up the tree
+       while ( parent && parent != this )
+               try { parent = parent.parentNode; }
+               catch(e) { parent = this; }
        
-       jQuery.each({ 
-               mouseover: 'mouseenter', 
-               mouseout: 'mouseleave'
-       }, function( orig, fix ){
-               jQuery.event.special[ fix ] = {
-                       setup: function(){
-                               jQuery.event.add( this, orig, withinElement, fix );
-                       },
-                       teardown: function(){
-                               jQuery.event.remove( this, orig, withinElement );
-                       }
-               };                         
-       });
-}
+       if( parent != this ){
+               // set the correct event type
+               event.type = event.data;
+               // handle event if we actually just moused on to a non sub-element
+               jQuery.event.handle.apply( this, arguments );
+       }
+};
+       
+jQuery.each({ 
+       mouseover: 'mouseenter', 
+       mouseout: 'mouseleave'
+}, function( orig, fix ){
+       jQuery.event.special[ fix ] = {
+               setup: function(){
+                       jQuery.event.add( this, orig, withinElement, fix );
+               },
+               teardown: function(){
+                       jQuery.event.remove( this, orig, withinElement );
+               }
+       };                         
+});
 
 jQuery.fn.extend({
        bind: function( type, data, fn ) {
@@ -521,27 +523,30 @@ function bindReady(){
        if ( readyBound ) return;
        readyBound = true;
 
-       // Mozilla, Opera (see further below for it) and webkit nightlies currently support this event
-       if ( document.addEventListener && !jQuery.browser.opera)
+       // Mozilla, Opera and webkit nightlies currently support this event
+       if ( document.addEventListener ) {
                // Use the handy event callback
-               document.addEventListener( "DOMContentLoaded", jQuery.ready, false );
+               document.addEventListener( "DOMContentLoaded", function(){
+                       document.removeEventListener( "DOMContentLoaded", arguments.callee, false );
+                       jQuery.ready();
+               }, false );
 
        // If IE event model is used
-       if ( document.attachEvent )
+       } else if ( document.attachEvent ) {
                // ensure firing before onload,
                // maybe late but safe also for iframes
-               document.attachEvent("onreadystatechange", function(e) {
-                       if ( document.readyState == "complete" ) {
-                               document.detachEvent("onreadystatechange", arguments.callee );
+               document.attachEvent("onreadystatechange", function(){
+                       if ( document.readyState === "complete" ) {
+                               document.detachEvent( "onreadystatechange", arguments.callee );
                                jQuery.ready();
                        }
                });
 
-       // If IE and not an iframe
-       if ( document.documentElement.doScroll && !window.frameElement )
+               // If IE and not an iframe
                // continually check to see if the document is ready
-               (function(){
-                       if (jQuery.isReady) return;
+               if ( document.documentElement.doScroll && !window.frameElement ) (function(){
+                       if ( jQuery.isReady ) return;
+
                        try {
                                // If IE is used, use the trick by Diego Perini
                                // http://javascript.nwbox.com/IEContentLoaded/
@@ -550,36 +555,7 @@ function bindReady(){
                                setTimeout( arguments.callee, 0 );
                                return;
                        }
-                       // and execute any waiting functions
-                       jQuery.ready();
-               })();
-
-       if ( jQuery.browser.opera )
-               document.addEventListener( "DOMContentLoaded", function () {
-                       if (jQuery.isReady) return;
-                       for (var i = 0; i < document.styleSheets.length; i++)
-                               if (document.styleSheets[i].disabled) {
-                                       setTimeout( arguments.callee, 0 );
-                                       return;
-                               }
-                       // and execute any waiting functions
-                       jQuery.ready();
-               }, false);
 
-       if ( jQuery.browser.safari ) {
-               var numStyles;
-               (function(){
-                       if (jQuery.isReady) return;
-                       if ( document.readyState != "loaded" && document.readyState != "complete" ) {
-                               setTimeout( arguments.callee, 0 );
-                               return;
-                       }
-                       if ( numStyles === undefined )
-                               numStyles = jQuery("style, link[rel=stylesheet]").length;
-                       if ( document.styleSheets.length != numStyles ) {
-                               setTimeout( arguments.callee, 0 );
-                               return;
-                       }
                        // and execute any waiting functions
                        jQuery.ready();
                })();