diff options
author | Dave Methvin <dave.methvin@gmail.com> | 2011-08-14 15:52:57 -0400 |
---|---|---|
committer | timmywil <timmywillisn@gmail.com> | 2011-09-19 15:42:31 -0400 |
commit | bb1081ee448b4cbdba8f6b1ae98b29a08f9bfff8 (patch) | |
tree | 526fad6f278a711fe0975e58a3e8ee097abe640b /src/event.js | |
parent | 990c094f1ceded54f93631d4ceb2c649a3b171be (diff) | |
download | jquery-bb1081ee448b4cbdba8f6b1ae98b29a08f9bfff8.tar.gz jquery-bb1081ee448b4cbdba8f6b1ae98b29a08f9bfff8.zip |
Clean up the quick selector expression parsing code.
Diffstat (limited to 'src/event.js')
-rw-r--r-- | src/event.js | 51 |
1 files changed, 27 insertions, 24 deletions
diff --git a/src/event.js b/src/event.js index ba9ef6be4..44db42e33 100644 --- a/src/event.js +++ b/src/event.js @@ -7,12 +7,32 @@ var rnamespaces = /\.(.*)$/, rescape = /[^\w\s.|`]/g, rtypenamespace = /^([^\.]*)?(?:\.(.+))?$/, rhoverHack = /\bhover(\.\S+)?/, - rquickIs = /^([\w\-]+)?(?:#([\w\-]+))?(?:\.([\w\-]+))?(?:\[([\w+\-]+)=["']?([\w\-]*)["']?\])?(:first-child|:last-child|:empty)?$/, + rquickIs = /^([\w\-]+)?(?:#([\w\-]+))?(?:\.([\w\-]+))?(?:\[([\w+\-]+)=["']?([\w\-]*)["']?\])?(?::(first-child|last-child|empty))?$/, quickPseudoMap = { - ":empty": "firstChild", - ":first-child": "previousSibling", - ":last-child": "nextSibling" - }; + "empty": "firstChild", + "first-child": "previousSibling", + "last-child": "nextSibling" + }, + quickParse = function( selector ) { + var quick = rquickIs.exec( selector ); + if ( quick ) { + // 0 1 2 3 4 5 6 + // [ _, tag, id, class, attrName, attrValue, pseudo(:empty :first-child :last-child) ] + quick[1] = ( quick[1] || "" ).toLowerCase(); + quick[3] = quick[3] && new RegExp( "\\b" + quick[3] + "\\b" ); + quick[6] = quickPseudoMap[ quick[6] ]; + } + return quick; + }, + quickIs = function( elem, m ) { + return ( + (!m[1] || elem.nodeName.toLowerCase() === m[1]) && + (!m[2] || elem.id === m[2]) && + (!m[3] || m[3].test( elem.className )) && + (!m[4] || elem.getAttribute( m[4] ) == m[5]) && + (!m[6] || !elem[ m[6] ]) + ); + } function useNativeMethod( event ) { if ( !event.isDefaultPrevented() && this[ event.type ] ) { @@ -91,14 +111,8 @@ jQuery.event = { // Delegated event setup if ( selector ) { // Pre-analyze selector so we can process it quickly on event dispatch - quick = handleObj.quick = rquickIs.exec( selector ); - if ( quick ) { - // 0 1 2 3 4 5 6 - // [ _, tag, id, class, attrName, attrValue, pseudo(:empty :first-child :last-child) ] - quick[1] = ( quick[1] || "" ).toLowerCase(); - quick[3] = quick[3] && new RegExp( "\\b" + quick[3] + "\\b" ); - quick[6] = quickPseudoMap[ quick[6] ]; - } else if ( jQuery.expr.match.POS.test( selector ) ) { + handleObj.quick = quickParse( selector ); + if ( !handleObj.quick && jQuery.expr.match.POS.test( selector ) ) { handleObj.isPositional = true; } } @@ -584,17 +598,6 @@ function dispatch( target, event, handlers, args ) { } } -function quickIs( elem, m ) { - return ( - (!m[1] || elem.nodeName.toLowerCase() === m[1]) && - (!m[2] || elem.id === m[2]) && - (!m[3] || m[3].test( elem.className )) && - (!m[4] || elem.getAttribute( m[4] ) == m[5]) && - (!m[6] || !elem[ m[6] ]) - ); -} - - jQuery.removeEvent = document.removeEventListener ? function( elem, type, handle ) { if ( elem.removeEventListener ) { |