Implements jQuery.event.propHooks. Fixes #8789

This commit is contained in:
Rick Waldron 2011-09-20 12:54:34 -04:00
parent 92a80cbd05
commit 9fbed020a1

View File

@ -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",
@ -411,6 +412,10 @@ 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 ),
@ -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;
@ -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 );