]> source.dussan.org Git - jquery.git/commitdiff
Event: Optimize delegated event processing
authorRichard Gibson <richard.gibson@gmail.com>
Mon, 1 Aug 2016 18:20:38 +0000 (14:20 -0400)
committerRichard Gibson <richard.gibson@gmail.com>
Fri, 5 Aug 2016 03:52:12 +0000 (23:52 -0400)
Closes gh-3255

src/event.js

index b3fa59de562a15fc30872f697e08fe594d5d2cd2..859f99e08ab8bbcfde1e3db11a02b1ed40d073bc 100644 (file)
@@ -355,51 +355,58 @@ jQuery.event = {
        },
 
        handlers: function( event, handlers ) {
-               var i, matches, sel, handleObj,
+               var i, handleObj, sel, matchedHandlers, matchedSelectors,
                        handlerQueue = [],
                        delegateCount = handlers.delegateCount,
                        cur = event.target;
 
-               // Support: IE <=9
                // Find delegate handlers
-               // Black-hole SVG <use> instance trees (#13180)
-               //
-               // Support: Firefox <=42
-               // Avoid non-left-click in FF but don't block IE radio events (#3861, gh-2343)
-               if ( delegateCount && cur.nodeType &&
-                       ( event.type !== "click" || isNaN( event.button ) || event.button < 1 ) ) {
+               if ( delegateCount &&
+
+                       // Support: IE <=9
+                       // Black-hole SVG <use> instance trees (trac-13180)
+                       cur.nodeType &&
+
+                       // Support: Firefox <=42
+                       // Suppress spec-violating clicks indicating a non-primary pointer button (trac-3861)
+                       // https://www.w3.org/TR/DOM-Level-3-Events/#event-type-click
+                       // Support: IE 11 only
+                       // ...but not arrow key "clicks" of radio inputs, which can have `button` -1 (gh-2343)
+                       !( event.type === "click" && event.button >= 1 ) ) {
 
                        for ( ; cur !== this; cur = cur.parentNode || this ) {
 
                                // Don't check non-elements (#13208)
                                // Don't process clicks on disabled elements (#6911, #8165, #11382, #11764)
-                               if ( cur.nodeType === 1 && ( cur.disabled !== true || event.type !== "click" ) ) {
-                                       matches = [];
+                               if ( cur.nodeType === 1 && !( event.type === "click" && cur.disabled === true ) ) {
+                                       matchedHandlers = [];
+                                       matchedSelectors = {};
                                        for ( i = 0; i < delegateCount; i++ ) {
                                                handleObj = handlers[ i ];
 
                                                // Don't conflict with Object.prototype properties (#13203)
                                                sel = handleObj.selector + " ";
 
-                                               if ( matches[ sel ] === undefined ) {
-                                                       matches[ sel ] = handleObj.needsContext ?
+                                               if ( matchedSelectors[ sel ] === undefined ) {
+                                                       matchedSelectors[ sel ] = handleObj.needsContext ?
                                                                jQuery( sel, this ).index( cur ) > -1 :
                                                                jQuery.find( sel, this, null, [ cur ] ).length;
                                                }
-                                               if ( matches[ sel ] ) {
-                                                       matches.push( handleObj );
+                                               if ( matchedSelectors[ sel ] ) {
+                                                       matchedHandlers.push( handleObj );
                                                }
                                        }
-                                       if ( matches.length ) {
-                                               handlerQueue.push( { elem: cur, handlers: matches } );
+                                       if ( matchedHandlers.length ) {
+                                               handlerQueue.push( { elem: cur, handlers: matchedHandlers } );
                                        }
                                }
                        }
                }
 
                // Add the remaining (directly-bound) handlers
+               cur = this;
                if ( delegateCount < handlers.length ) {
-                       handlerQueue.push( { elem: this, handlers: handlers.slice( delegateCount ) } );
+                       handlerQueue.push( { elem: cur, handlers: handlers.slice( delegateCount ) } );
                }
 
                return handlerQueue;