Since we fixed .is(POS) let's use it and save bytes.

Also, creates a single jQuery object and reuses it in the delegation-test loop to make it more performancy.
This commit is contained in:
Dave Methvin 2011-11-06 20:12:00 -05:00
parent f5250e1594
commit a7e911b7fe

View File

@ -99,17 +99,10 @@ jQuery.event = {
handler: handler, handler: handler,
guid: handler.guid, guid: handler.guid,
selector: selector, selector: selector,
quick: quickParse( selector ),
namespace: namespaces.join(".") namespace: namespaces.join(".")
}, handleObjIn ); }, handleObjIn );
// Delegated event; pre-analyze selector so it's processed quickly on event dispatch
if ( selector ) {
handleObj.quick = quickParse( selector );
if ( !handleObj.quick && jQuery.expr.match.POS.test( selector ) ) {
handleObj.isPositional = true;
}
}
// Init the event handler queue if we're the first // Init the event handler queue if we're the first
handlers = events[ type ]; handlers = events[ type ];
if ( !handlers ) { if ( !handlers ) {
@ -406,7 +399,7 @@ jQuery.event = {
run_all = !event.exclusive && !event.namespace, run_all = !event.exclusive && !event.namespace,
specialHandle = ( jQuery.event.special[ event.type ] || {} ).handle, specialHandle = ( jQuery.event.special[ event.type ] || {} ).handle,
handlerQueue = [], handlerQueue = [],
i, j, cur, ret, selMatch, matched, matches, handleObj, sel, hit, related; i, j, cur, jqcur, ret, selMatch, matched, matches, handleObj, sel, related;
// Use the fix-ed jQuery.Event rather than the (read-only) native event // Use the fix-ed jQuery.Event rather than the (read-only) native event
args[0] = event; args[0] = event;
@ -416,21 +409,24 @@ jQuery.event = {
// Avoid disabled elements in IE (#6911) and non-left-click bubbling in Firefox (#3861) // Avoid disabled elements in IE (#6911) and non-left-click bubbling in Firefox (#3861)
if ( delegateCount && !event.target.disabled && !(event.button && event.type === "click") ) { if ( delegateCount && !event.target.disabled && !(event.button && event.type === "click") ) {
// Pregenerate a single jQuery object for reuse with .is()
jqcur = jQuery(this);
jqcur.context = this.ownerDocument || this;
for ( cur = event.target; cur != this; cur = cur.parentNode || this ) { for ( cur = event.target; cur != this; cur = cur.parentNode || this ) {
selMatch = {}; selMatch = {};
matches = []; matches = [];
jqcur[0] = cur;
for ( i = 0; i < delegateCount; i++ ) { for ( i = 0; i < delegateCount; i++ ) {
handleObj = handlers[ i ]; handleObj = handlers[ i ];
sel = handleObj.selector; sel = handleObj.selector;
hit = selMatch[ sel ];
if ( handleObj.isPositional ) { if ( selMatch[ sel ] === undefined ) {
// Since .is() does not work for positionals; see http://jsfiddle.net/eJ4yd/3/ selMatch[ sel ] = (
hit = ( hit || (selMatch[ sel ] = jQuery( sel )) ).index( cur ) >= 0; handleObj.quick ? quickIs( cur, handleObj.quick ) : jqcur.is( sel )
} else if ( hit === undefined ) { );
hit = selMatch[ sel ] = ( handleObj.quick ? quickIs( cur, handleObj.quick ) : jQuery( cur ).is( sel ) );
} }
if ( hit ) { if ( selMatch[ sel ] ) {
matches.push( handleObj ); matches.push( handleObj );
} }
} }