Commit 36c82a1b authored by Rick Waldron's avatar Rick Waldron

Merge branch 'fix-8789-rwldrn-fix' of git://github.com/dmethvin/jquery into fix-8789-rwldrn-fix

* 'fix-8789-rwldrn-fix' of git://github.com/dmethvin/jquery:
  Minor cleanups to code. Futile effort to get IE to pass the unit test.
  propHooks now an object with `props` array and `filter` function.
  Moves mouse properties to mouseProps
  current state
  Removes early return loop, must copy properties
  Shortcircuit fix if possible
  More ref localization
  Remove unnec. empty line
  More reference caching
  Cache reference to propHook lookup and result
  Restore this.propHooks => jQuery.event.propHooks for better gzip compression. Thanks gnarf
  Adds notes re: crash status of fix conditions
  Moves key event fixes to own even prop hook defs
  jQuery.event.propHooks => this.propHooks where possible
  Removes pageX pageY from prop list
  Updates rmouseEvent
  Implements jQuery.event.propHooks. Fixes #8789
  Adds implementation tests for jQuery.event.propHooks #8789
parents 140bebb7 c7838c36
...@@ -7,6 +7,8 @@ var rnamespaces = /\.(.*)$/, ...@@ -7,6 +7,8 @@ var rnamespaces = /\.(.*)$/,
rescape = /[^\w\s.|`]/g, rescape = /[^\w\s.|`]/g,
rtypenamespace = /^([^\.]*)?(?:\.(.+))?$/, rtypenamespace = /^([^\.]*)?(?:\.(.+))?$/,
rhoverHack = /\bhover(\.\S+)?/, rhoverHack = /\bhover(\.\S+)?/,
rkeyEvent = /^key/,
rmouseEvent = /^(?:mouse|contextmenu)|click/,
rquickIs = /^([\w\-]+)?(?:#([\w\-]+))?(?:\.([\w\-]+))?(?:\[([\w+\-]+)=["']?([\w\-]*)["']?\])?$/, rquickIs = /^([\w\-]+)?(?:#([\w\-]+))?(?:\.([\w\-]+))?(?:\[([\w+\-]+)=["']?([\w\-]*)["']?\])?$/,
quickParse = function( selector ) { quickParse = function( selector ) {
var quick = rquickIs.exec( selector ); var quick = rquickIs.exec( selector );
...@@ -463,66 +465,90 @@ jQuery.event = { ...@@ -463,66 +465,90 @@ jQuery.event = {
return event.result; 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(" "), // Includes some event props shared by KeyEvent and MouseEvent
// *** attrChange attrName relatedNode srcElement are not normalized, non-W3C, deprecated, will be removed in 1.8 ***
props: "attrChange attrName relatedNode srcElement altKey bubbles cancelable ctrlKey currentTarget eventPhase metaKey relatedTarget shiftKey target timeStamp type view which".split(" "),
propHooks: {},
keyHooks: {
props: "char charCode key keyCode".split(" "),
filter: function( event, original ) {
// Add which for key events
if ( event.which == null ) {
event.which = original.charCode != null ? original.charCode : original.keyCode;
}
fix: function( event ) {
if ( event[ jQuery.expando ] ) {
return event; return event;
} }
},
// store a copy of the original event object mouseHooks: {
// and "clone" to set read-only properties props: "button buttons clientX clientY fromElement layerX layerY offsetX offsetY pageX pageY screenX screenY toElement wheelDelta".split(" "),
var originalEvent = event; filter: function( event, original ) {
event = jQuery.Event( originalEvent ); var eventDoc, doc, body,
button = original.button,
fromElement = original.fromElement;
for ( var i = this.props.length, prop; i; ) { // Calculate pageX/Y if missing and clientX/Y available
prop = this.props[ --i ]; if ( event.pageX == null && original.clientX != null ) {
event[ prop ] = originalEvent[ prop ]; eventDoc = event.target.ownerDocument || document;
doc = eventDoc.documentElement;
body = eventDoc.body;
event.pageX = original.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0);
event.pageY = original.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
} }
// Fix target property, if necessary // Add relatedTarget, if necessary
if ( !event.target ) { if ( !event.relatedTarget && fromElement ) {
// Fixes #1925 where srcElement might not be defined either event.relatedTarget = fromElement === event.target ? original.toElement : fromElement;
event.target = event.srcElement || document;
} }
// check if target is a textnode (safari) // Add which for click: 1 === left; 2 === middle; 3 === right
if ( event.target.nodeType === 3 ) { // Note: button is not normalized, so don't use it
event.target = event.target.parentNode; if ( !event.which && button !== undefined ) {
event.which = (button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ));
} }
// Add relatedTarget, if necessary return event;
if ( !event.relatedTarget && event.fromElement ) {
event.relatedTarget = event.fromElement === event.target ? event.toElement : event.fromElement;
} }
},
// Calculate pageX/Y if missing and clientX/Y available fix: function( event ) {
if ( event.pageX == null && event.clientX != null ) { if ( event[ jQuery.expando ] ) {
var eventDocument = event.target.ownerDocument || document, return event;
doc = eventDocument.documentElement, }
body = eventDocument.body;
// Create a writable copy of the event object and normalize some properties
var originalEvent = event,
propHook = jQuery.event.propHooks[ event.type ] || {},
copy = propHook.props ? this.props.concat( propHook.props ) : this.props;
event.pageX = event.clientX + (doc && doc.scrollLeft || body && body.scrollLeft || 0) - (doc && doc.clientLeft || body && body.clientLeft || 0); event = jQuery.Event( originalEvent );
event.pageY = event.clientY + (doc && doc.scrollTop || body && body.scrollTop || 0) - (doc && doc.clientTop || body && body.clientTop || 0);
for ( var i = copy.length, prop; i; ) {
prop = copy[ --i ];
event[ prop ] = originalEvent[ prop ];
} }
// Add which for key events // Fix target property, if necessary (#1925, IE 6/7/8 & Safari2)
if ( event.which == null && (event.charCode != null || event.keyCode != null) ) { if ( !event.target ) {
event.which = event.charCode != null ? event.charCode : event.keyCode; event.target = originalEvent.srcElement || document;
} }
// Add metaKey to non-Mac browsers (use ctrl for PC's and Meta for Macs) // Target should not be a text node (#504, Safari)
if ( !event.metaKey && event.ctrlKey ) { if ( event.target.nodeType === 3 ) {
event.metaKey = event.ctrlKey; event.target = event.target.parentNode;
} }
// Add which for click: 1 === left; 2 === middle; 3 === right // For mouse/key events; add metaKey if it's not there (#3368, IE6/7/8)
// Note: button is not normalized, so don't use it if ( event.metaKey === undefined ) {
if ( !event.which && event.button !== undefined ) { event.metaKey = event.ctrlKey;
event.which = (event.button & 1 ? 1 : ( event.button & 2 ? 3 : ( event.button & 4 ? 2 : 0 ) ));
} }
return event; return propHook.filter? propHook.filter( event, originalEvent ) : event;
}, },
// Deprecated, use jQuery.guid instead // Deprecated, use jQuery.guid instead
...@@ -1013,7 +1039,7 @@ jQuery.fn.extend({ ...@@ -1013,7 +1039,7 @@ jQuery.fn.extend({
jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " + jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblclick " +
"mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " + "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 // Handle event binding
jQuery.fn[ name ] = function( data, fn ) { jQuery.fn[ name ] = function( data, fn ) {
...@@ -1030,6 +1056,14 @@ jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblcl ...@@ -1030,6 +1056,14 @@ jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblcl
if ( jQuery.attrFn ) { if ( jQuery.attrFn ) {
jQuery.attrFn[ name ] = true; jQuery.attrFn[ name ] = true;
} }
if ( rkeyEvent.test( name ) ) {
jQuery.event.propHooks[ name ] = jQuery.event.keyHooks;
}
if ( rmouseEvent.test( name ) ) {
jQuery.event.propHooks[ name ] = jQuery.event.mouseHooks;
}
}); });
})( jQuery ); })( jQuery );
......
...@@ -2383,6 +2383,37 @@ test("delegated events quickIs", function() { ...@@ -2383,6 +2383,37 @@ test("delegated events quickIs", function() {
markup.remove(); markup.remove();
}); });
test("propHooks extensions", function() {
expect( 3 );
// IE requires focusable elements to be visible, so append to body
var $fixture = jQuery( "<input type='text' id='hook-fixture' />" ).appendTo( "body" );
// Ensure the property doesn't exist
$fixture.bind( "focus", function( event ) {
ok( !("blurrinessLevel" in event), "event.blurrinessLevel does not exist" );
})[0].focus();
// Must blur the link so focus works below
$fixture.unbind( "focus" )[0].blur();
// Define a custom property for "focus" events via the filter function
ok( !jQuery.event.propHooks.focus, "We aren't clobbering an existing focus hook" );
jQuery.event.propHooks.focus = {
filter: function( event, originalEvent ) {
event.blurrinessLevel = 42;
return event;
}
};
// Trigger a native focus and ensure the property is set
$fixture.bind( "focus", function( event ) {
equals( event.blurrinessLevel, 42, "event.blurrinessLevel was set" );
})[0].focus();
delete jQuery.event.propHooks.focus;
$fixture.unbind( "focus" ).remove();
});
(function(){ (function(){
// This code must be run before DOM ready! // This code must be run before DOM ready!
...@@ -2457,13 +2488,4 @@ test("delegated events quickIs", function() { ...@@ -2457,13 +2488,4 @@ test("delegated events quickIs", function() {
})(); })();
/*
test("event properties", function() {
stop();
jQuery("#simon1").click(function(event) {
ok( event.timeStamp, "assert event.timeStamp is present" );
start();
}).click();
});
*/
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment