From 9fbed020a15b0bfe33ce622b6f2fbccc1e2b4739 Mon Sep 17 00:00:00 2001 From: Rick Waldron Date: Tue, 20 Sep 2011 12:54:34 -0400 Subject: [PATCH] Implements jQuery.event.propHooks. Fixes #8789 --- src/event.js | 81 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/src/event.js b/src/event.js index da787ea85..7fdcba5ad 100644 --- a/src/event.js +++ b/src/event.js @@ -7,6 +7,7 @@ var rnamespaces = /\.(.*)$/, rescape = /[^\w\s.|`]/g, rtypenamespace = /^([^\.]*)?(?:\.(.+))?$/, rhoverHack = /\bhover(\.\S+)?/, + rmouseEvent = /^(?:mouse)|(?:click)|(contextmenu)/, rquickIs = /^([\w\-]+)?(?:#([\w\-]+))?(?:\.([\w\-]+))?(?:\[([\w+\-]+)=["']?([\w\-]*)["']?\])?(?::(first-child|last-child|empty))?$/, quickPseudoMap = { "empty": "firstChild", @@ -26,8 +27,8 @@ var rnamespaces = /\.(.*)$/, }, quickIs = function( elem, m ) { return ( - (!m[1] || elem.nodeName.toLowerCase() === m[1]) && - (!m[2] || elem.id === m[2]) && + (!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] ]) @@ -106,7 +107,7 @@ jQuery.event = { handleObj = jQuery.extend({ type: type, origType: tns[1], - data: data, + data: data, handler: handler, guid: handler.guid, selector: selector, @@ -126,7 +127,7 @@ jQuery.event = { if ( !handlers ) { handlers = events[ type ] = []; handlers.delegateCount = 0; - + // Only use addEventListener/attachEvent if the special events handler returns false if ( !special.setup || special.setup.call( elem, data, namespaces, eventHandle ) === false ) { // Bind the global event handler to the element @@ -153,7 +154,7 @@ jQuery.event = { } else { handlers.push( handleObj ); } - + // Keep track of which events have ever been used, for event optimization jQuery.event.global[ type ] = true; } @@ -170,7 +171,7 @@ jQuery.event = { var elemData = jQuery.hasData( elem ) && jQuery._data( elem ), t, tns, type, namespaces, origCount, j, events, special, handle, eventType, handleObj; - + if ( !elemData || !(events = elemData.events) ) { return; } @@ -252,7 +253,7 @@ jQuery.event = { jQuery.removeData( elem, [ "events", "handle" ], true ); } }, - + // Events that are safe to short-circuit if no handlers are attached. // Native DOM events should not be added, they may have inline handlers. customEvent: { @@ -361,7 +362,7 @@ jQuery.event = { } addHandlers( doc.defaultView || doc.parentWindow || window, bubbleType ); } - + // Bubble up the DOM tree for ( i = 0; i < eventPath.length; i++ ) { cur = eventPath[ i ]; @@ -402,7 +403,7 @@ jQuery.event = { } } } - + return event.result; }, @@ -410,7 +411,11 @@ jQuery.event = { // Make a writable jQuery.Event from the native event object event = jQuery.event.fix( event || window.event ); - + + if ( jQuery.event.propHooks[ event.type ] ) { + event = jQuery.event.propHooks[ event.type ]( event ); + } + var handlers = ((jQuery._data( this, "events" ) || {})[ event.type ] || []), delegateCount = handlers.delegateCount, args = Array.prototype.slice.call( arguments, 0 ), @@ -447,7 +452,7 @@ jQuery.event = { } } } - + // Copy the remaining (bound) handlers in case they're changed handlers = handlers.slice( delegateCount ); @@ -469,6 +474,8 @@ jQuery.event = { props: "altKey attrChange attrName bubbles button cancelable charCode clientX clientY ctrlKey currentTarget data detail eventPhase fromElement handler keyCode layerX layerY metaKey newValue offsetX offsetY pageX pageY prevValue relatedNode relatedTarget screenX screenY shiftKey srcElement target toElement view wheelDelta which".split(" "), + propHooks: {}, + fix: function( event ) { if ( event[ jQuery.expando ] ) { return event; @@ -500,16 +507,6 @@ jQuery.event = { event.relatedTarget = event.fromElement === event.target ? event.toElement : event.fromElement; } - // Calculate pageX/Y if missing and clientX/Y available - if ( event.pageX == null && event.clientX != null ) { - var eventDocument = event.target.ownerDocument || document, - doc = eventDocument.documentElement, - body = eventDocument.body; - - event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0); - event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0); - } - // Add which for key events if ( event.which == null && (event.charCode != null || event.keyCode != null) ) { event.which = event.charCode != null ? event.charCode : event.keyCode; @@ -520,10 +517,8 @@ jQuery.event = { event.metaKey = event.ctrlKey; } - // Add which for click: 1 === left; 2 === middle; 3 === right - // Note: button is not normalized, so don't use it - if ( !event.which && event.button !== undefined ) { - event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) )); + if ( jQuery.event.propHooks[ event.type ] ) { + event = jQuery.event.propHooks[ event.type ]( event, originalEvent ); } return event; @@ -540,7 +535,7 @@ jQuery.event = { // Make sure the ready event is setup setup: jQuery.bindReady }, - + focus: { delegateType: "focusin", trigger: useNativeMethod @@ -580,7 +575,7 @@ function dispatch( target, event, handlers, args ) { // Triggered event must either 1) be non-exclusive and have no namespace, or // 2) have namespace(s) a subset or equal to those in the bound event (both can have no namespace). if ( run_all || (!event.namespace && !handleObj.namespace) || event.namespace_re && event.namespace_re.test( handleObj.namespace ) ) { - + // Pass in a reference to the handler function itself // So that we can later remove it event.handler = handleObj.handler; @@ -740,7 +735,7 @@ if ( !jQuery.support.submitBubbles ) { type = jQuery.nodeName( elem, "input" ) || jQuery.nodeName( elem, "button" ) ? elem.type : ""; // Do the elem.form check after type to avoid VML-related crash in IE (#9807) - if ( (e.type === "click" && (type === "submit" || type === "image") && elem.form) || + if ( (e.type === "click" && (type === "submit" || type === "image") && elem.form) || (e.type === "keypress" && e.keyCode === 13 && (type === "text" || type === "password") && elem.form) ) { simulate( "submit", this, e ); } @@ -987,7 +982,7 @@ jQuery.fn.extend({ jQuery.event.remove( this, types, fn, selector ); }); }, - + bind: function( types, data, fn ) { return this.on( types, null, data, fn ); }, @@ -1056,7 +1051,7 @@ jQuery.fn.extend({ jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " + "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " + - "change select submit keydown keypress keyup error").split(" "), function( i, name ) { + "change select submit keydown keypress keyup error contextmenu").split(" "), function( i, name ) { // Handle event binding jQuery.fn[ name ] = function( data, fn ) { @@ -1073,6 +1068,32 @@ jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblcl if ( jQuery.attrFn ) { jQuery.attrFn[ name ] = true; } + + // Add internal event property hooks to mouse events + if ( rmouseEvent.test( name ) ) { + + jQuery.event.propHooks[ name ] = function( event, original ) { + + var eventDocument, doc, body; + + // Calculate pageX/Y if missing and clientX/Y available + if ( event.pageX == null && event.clientX != null ) { + eventDocument = event.target.ownerDocument || document; + doc = eventDocument.documentElement; + body = eventDocument.body; + + event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0); + event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0); + } + + // Add which for click: 1 === left; 2 === middle; 3 === right + // Note: button is not normalized, so don't use it + if ( !event.which && event.button !== undefined ) { + event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) )); + } + return event; + }; + } }); })( jQuery ); -- 2.39.5