Permalink
Browse files

Merge branch '8789-fun-with-fix' of https://github.com/rwldrn/jquery

…into fix-8789-rwldrn-fix

Conflicts:
	src/event.js
	test/unit/event.js
  • Loading branch information...
2 parents d920ac6 + 87e1c62 commit ae27424b30df318a41ea445c296a0d5566a36467 @dmethvin dmethvin committed Sep 25, 2011
Showing with 154 additions and 48 deletions.
  1. +91 −44 src/event.js
  2. +63 −4 test/unit/event.js
View
@@ -20,8 +20,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])
);
@@ -99,7 +99,7 @@ jQuery.event = {
handleObj = jQuery.extend({
type: type,
origType: tns[1],
- data: data,
+ data: data,
handler: handler,
guid: handler.guid,
selector: selector,
@@ -119,7 +119,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
@@ -146,7 +146,7 @@ jQuery.event = {
} else {
handlers.push( handleObj );
}
-
+
// Keep track of which events have ever been used, for event optimization
jQuery.event.global[ type ] = true;
}
@@ -163,7 +163,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;
}
@@ -245,7 +245,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: {
@@ -357,7 +357,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 ];
@@ -398,15 +398,21 @@ jQuery.event = {
}
}
}
-
+
return event.result;
},
handle: function( event ) {
// Make a writable jQuery.Event from the native event object
event = jQuery.event.fix( event || window.event );
-
+
+ var propHook = jQuery.event.propHooks[ event.type ];
+
+ if ( propHook ) {
+ event = propHook( event );
+ }
+
var handlers = ((jQuery._data( this, "events" ) || {})[ event.type ] || []),
delegateCount = handlers.delegateCount,
args = Array.prototype.slice.call( arguments, 0 ),
@@ -443,7 +449,7 @@ jQuery.event = {
}
}
}
-
+
// Copy the remaining (bound) handlers in case they're changed
handlers = handlers.slice( delegateCount );
@@ -463,7 +469,9 @@ jQuery.event = {
return event.result;
},
- 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(" "),
+ props: "altKey attrName bubbles button cancelable charCode ctrlKey currentTarget data detail eventPhase fromElement handler keyCode metaKey relatedNode relatedTarget screenX screenY shiftKey srcElement target toElement view which".split(" "),
+
+ propHooks: {},
fix: function( event ) {
if ( event[ jQuery.expando ] ) {
@@ -472,21 +480,30 @@ jQuery.event = {
// store a copy of the original event object
// and "clone" to set read-only properties
- var originalEvent = event;
+ var originalEvent = event,
+ propHook = jQuery.event.propHooks[ event.type ],
+ copy = this.props;
+
event = jQuery.Event( originalEvent );
- for ( var i = this.props.length, prop; i; ) {
- prop = this.props[ --i ];
+ if ( propHook ) {
+ copy.push.apply( copy, propHook() || [] );
+ }
+
+ for ( var i = copy.length, prop; i; ) {
+ prop = copy[ --i ];
event[ prop ] = originalEvent[ prop ];
}
// Fix target property, if necessary
+ // Removal will crash IE6,7,8
if ( !event.target ) {
// Fixes #1925 where srcElement might not be defined either
event.target = event.srcElement || document;
}
// check if target is a textnode (safari)
+ // Removal will crash IE6,7,8
if ( event.target.nodeType === 3 ) {
event.target = event.target.parentNode;
}
@@ -496,30 +513,8 @@ 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;
- }
-
- // Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs)
- if ( !event.metaKey && event.ctrlKey ) {
- 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 ( propHook ) {
+ event = propHook( event, originalEvent );
}
return event;
@@ -536,7 +531,7 @@ jQuery.event = {
// Make sure the ready event is setup
setup: jQuery.bindReady
},
-
+
focus: {
delegateType: "focusin",
trigger: useNativeMethod
@@ -576,7 +571,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;
@@ -944,7 +939,7 @@ jQuery.fn.extend({
jQuery.event.remove( this, types, fn, selector );
});
},
-
+
bind: function( types, data, fn ) {
return this.on( types, null, data, fn );
},
@@ -1013,7 +1008,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 ) {
@@ -1030,7 +1025,59 @@ jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblcl
if ( jQuery.attrFn ) {
jQuery.attrFn[ name ] = true;
}
-});
+
+ // Key Event property hooks
+ if ( rkeyEvent.test( name ) ) {
+ jQuery.event.propHooks[ name ] = function( event, original ) {
+
+ var charCode = event.charCode,
+ keyCode = event.keyCode,
+ ctrlKey = event.ctrlKey;
+
+ // Add which for key events
+ if ( event.which == null && (charCode != null || keyCode != null) ) {
+ event.which = charCode != null ? charCode : keyCode;
+ }
+
+ // Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs)
+ if ( !event.metaKey && ctrlKey ) {
+ event.metaKey = ctrlKey;
+ }
+
+ return event;
+ };
+ }
+
+ // Mouse Event property hooks
+ if ( rmouseEvent.test( name ) ) {
+ jQuery.event.propHooks[ name ] = function( event, original ) {
+
+ if ( !event ) {
+ return mouseProps;
+ }
+
+ var eventDoc, doc, body,
+ button = event.button;
+
+ // Calculate pageX/Y if missing and clientX/Y available
+ if ( event.pageX == null && event.clientX != null ) {
+ eventDoc = event.target.ownerDocument || document;
+ doc = eventDoc.documentElement;
+ body = eventDoc.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 && button !== undefined ) {
+ event.which = (button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ));
+ }
+
+ return event;
+ };
+ }});
})( jQuery );
View
@@ -54,7 +54,7 @@ test("Handler changes and .trigger() order", function() {
markup.find( "b" ).trigger( "click" );
equals( path, "p div body html ", "Delivered all events" )
-
+
markup.remove();
});
*/
@@ -2332,7 +2332,7 @@ test(".on and .off", function() {
// We should have removed all the event handlers ... kinda hacky way to check this
var data = jQuery.data[ jQuery( "#onandoff" )[0].expando ] || {};
equals( data.events, undefined, "no events left" );
-
+
jQuery("#onandoff").remove();
});
@@ -2349,12 +2349,12 @@ test("delegated events quickIs", function() {
'</div>'
),
str,
- check = function(el, expect){
+ check = function(el, expect){
str = "";
markup.find( el ).trigger( "blink" );
equals( str, expect, "On " + el );
},
- func = function(e){
+ func = function(e){
var tag = this.nodeName.toLowerCase();
str += (str && " ") + tag + "|" + e.handleObj.selector;
ok( e.handleObj.quick, "Selector "+ e.handleObj.selector + " on " + tag + " is a quickIs case" );
@@ -2457,6 +2457,65 @@ test("delegated events quickIs", function() {
})();
+test("jQuery.event.propHooks", function() {
+ expect( 1 );
+ ok( jQuery.event.propHooks, "jQuery.event.propHooks exists" );
+});
+
+test("jQuery.event.propHooks as function", function() {
+
+ expect( 2 );
+
+ jQuery( "<div id='hook-fixture'></div>" ).appendTo( "#qunit-fixture" );
+
+ var $fixture = jQuery( "#hook-fixture" );
+
+ // Does not exist
+ $fixture.bind( "click", function( event ) {
+ ok( !("propC" in event), "event.propC Does not exist" );
+ }).trigger( "click" );
+
+ $fixture.unbind( "click" );
+
+ // Store as function
+ jQuery.event.propHooks[ "custom" ] = function( event ) {
+ // receives the event object for processing
+ event.propC = true;
+ return event;
+ };
+
+ $fixture.bind( "custom", function( event ) {
+ ok( event.propC, "event.propC exists" );
+ }).trigger( "custom" );
+});
+
+test("jQuery.event.propHooks usecase", function() {
+
+ expect( 3 );
+
+ jQuery( "<div id='hook-fixture'></div>" ).appendTo( "#qunit-fixture" );
+
+ var $fixture = jQuery( "#hook-fixture" );
+
+ $fixture.bind( "fakedrop", function( event ) {
+ ok( !("dataTransfer" in event), "event.dataTransfer is not available" );
+ }).trigger( "fakedrop" );
+
+ $fixture.unbind( "fakedrop" );
+
+ jQuery.event.propHooks[ "fakedrop" ] = function( event ) {
+ event.dataTransfer = "some val";
+ return event;
+ };
+
+ $fixture.bind( "fakedrop", function( event ) {
+ ok( ("dataTransfer" in event), "event.dataTransfer exists, just copied" );
+ equal( event.dataTransfer, "some val", "event.dataTransfer equal 'some val'" );
+ }).trigger( "fakedrop" );
+
+ $fixture.unbind( "fakedrop" );
+});
+
/*
test("event properties", function() {
stop();

0 comments on commit ae27424

Please sign in to comment.