From 31293063fa0477a0fc31526c6f57a2ed0c343c07 Mon Sep 17 00:00:00 2001 From: Ariel Flesler Date: Mon, 28 Jul 2008 18:31:25 +0000 Subject: [PATCH] jquery event: closes #3127. jQuery.event.special built-in methods were optimized. The mouse(enter|leave) special case is only added for non-IE browsers.Thanks to Mike Helgeson for his help. --- src/event.js | 99 ++++++++++++++++++++++------------------------------------ 1 file changed, 37 insertions(+), 62 deletions(-) diff --git a/src/event.js b/src/event.js index 8529d13..324024f 100644 --- a/src/event.js +++ b/src/event.js @@ -360,61 +360,47 @@ jQuery.event = { special: { ready: { - setup: function() { - // Make sure the ready event is setup - bindReady(); - return; - }, - - teardown: function() { return; } - }, - - mouseenter: { - setup: function( data ) { - if ( jQuery.browser.msie ) return false; - jQuery(this).bind("mouseover", data, jQuery.event.special.mouseenter.handler); - return true; - }, - - teardown: function() { - if ( jQuery.browser.msie ) return false; - jQuery(this).unbind("mouseover", jQuery.event.special.mouseenter.handler); - return true; - }, - - handler: function(event) { - // If we actually just moused on to a sub-element, ignore it - if ( withinElement(event, this) ) return true; - // Execute the right handlers by setting the event type to mouseenter - event.type = "mouseenter"; - return jQuery.event.handle.apply(this, arguments); - } - }, - - mouseleave: { - setup: function( data ) { - if ( jQuery.browser.msie ) return false; - jQuery(this).bind("mouseout", data, jQuery.event.special.mouseleave.handler); - return true; - }, - - teardown: function() { - if ( jQuery.browser.msie ) return false; - jQuery(this).unbind("mouseout", jQuery.event.special.mouseleave.handler); - return true; - }, - - handler: function(event) { - // If we actually just moused on to a sub-element, ignore it - if ( withinElement(event, this) ) return true; - // Execute the right handlers by setting the event type to mouseleave - event.type = "mouseleave"; - return jQuery.event.handle.apply(this, arguments); - } + // Make sure the ready event is setup + setup: bindReady, + teardown: function() {} } } }; +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 ); + } + }; + + 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 ) { return type == "unload" ? this.one(type, data, fn) : this.each(function(){ @@ -589,17 +575,6 @@ jQuery.each( ("blur,focus,load,resize,scroll,unload,click,dblclick," + }; }); -// Checks if an event happened on an element within another element -// Used in jQuery.event.special.mouseenter and mouseleave handlers -var withinElement = function(event, elem) { - // Check if mouse(over|out) are still within the same parent element - var parent = event.relatedTarget; - // Traverse up the tree - while ( parent && parent != elem ) try { parent = parent.parentNode; } catch(error) { parent = elem; } - // Return true if we actually just moused on to a sub-element - return parent == elem; -}; - // 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 -- 1.7.10.4