Skip to content

Commit

Permalink
Merge branch 'fix-8789-rwldrn-fix' of git://github.com/dmethvin/jquer…
Browse files Browse the repository at this point in the history
…y 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
  • Loading branch information
rwaldron committed Sep 26, 2011
2 parents 140bebb + c7838c3 commit 36c82a1
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 67 deletions.
142 changes: 88 additions & 54 deletions src/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ var rnamespaces = /\.(.*)$/,
rescape = /[^\w\s.|`]/g,
rtypenamespace = /^([^\.]*)?(?:\.(.+))?$/,
rhoverHack = /\bhover(\.\S+)?/,
rkeyEvent = /^key/,
rmouseEvent = /^(?:mouse|contextmenu)|click/,
rquickIs = /^([\w\-]+)?(?:#([\w\-]+))?(?:\.([\w\-]+))?(?:\[([\w+\-]+)=["']?([\w\-]*)["']?\])?$/,
quickParse = function( selector ) {
var quick = rquickIs.exec( selector );
Expand All @@ -20,8 +22,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])
);
Expand Down Expand Up @@ -99,7 +101,7 @@ jQuery.event = {
handleObj = jQuery.extend({
type: type,
origType: tns[1],
data: data,
data: data,
handler: handler,
guid: handler.guid,
selector: selector,
Expand All @@ -119,7 +121,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
Expand All @@ -146,7 +148,7 @@ jQuery.event = {
} else {
handlers.push( handleObj );
}

// Keep track of which events have ever been used, for event optimization
jQuery.event.global[ type ] = true;
}
Expand All @@ -163,7 +165,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;
}
Expand Down Expand Up @@ -245,7 +247,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: {
Expand Down Expand Up @@ -357,7 +359,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 ];
Expand Down Expand Up @@ -398,15 +400,15 @@ 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 handlers = ((jQuery._data( this, "events" ) || {})[ event.type ] || []),
delegateCount = handlers.delegateCount,
args = Array.prototype.slice.call( arguments, 0 ),
Expand Down Expand Up @@ -443,7 +445,7 @@ jQuery.event = {
}
}
}

// Copy the remaining (bound) handlers in case they're changed
handlers = handlers.slice( delegateCount );

Expand All @@ -463,66 +465,90 @@ 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(" "),
// 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;
}

return event;
}
},

mouseHooks: {
props: "button buttons clientX clientY fromElement layerX layerY offsetX offsetY pageX pageY screenX screenY toElement wheelDelta".split(" "),
filter: function( event, original ) {
var eventDoc, doc, body,
button = original.button,
fromElement = original.fromElement;

// Calculate pageX/Y if missing and clientX/Y available
if ( event.pageX == null && original.clientX != null ) {
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);
}

// Add relatedTarget, if necessary
if ( !event.relatedTarget && fromElement ) {
event.relatedTarget = fromElement === event.target ? original.toElement : fromElement;
}

// 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;
}
},

fix: function( event ) {
if ( event[ jQuery.expando ] ) {
return event;
}

// store a copy of the original event object
// and "clone" to set read-only properties
var originalEvent = event;
// 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 = jQuery.Event( originalEvent );

for ( var i = this.props.length, prop; i; ) {
prop = this.props[ --i ];
for ( var i = copy.length, prop; i; ) {
prop = copy[ --i ];
event[ prop ] = originalEvent[ prop ];
}

// Fix target property, if necessary
// Fix target property, if necessary (#1925, IE 6/7/8 & Safari2)
if ( !event.target ) {
// Fixes #1925 where srcElement might not be defined either
event.target = event.srcElement || document;
event.target = originalEvent.srcElement || document;
}

// check if target is a textnode (safari)
// Target should not be a text node (#504, Safari)
if ( event.target.nodeType === 3 ) {
event.target = event.target.parentNode;
}

// Add relatedTarget, if necessary
if ( !event.relatedTarget && event.fromElement ) {
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 ) {
// For mouse/key events; add metaKey if it's not there (#3368, IE6/7/8)
if ( event.metaKey === undefined ) {
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 ) ));
}

return event;
return propHook.filter? propHook.filter( event, originalEvent ) : event;
},

// Deprecated, use jQuery.guid instead
Expand All @@ -536,7 +562,7 @@ jQuery.event = {
// Make sure the ready event is setup
setup: jQuery.bindReady
},

focus: {
delegateType: "focusin",
trigger: useNativeMethod
Expand Down Expand Up @@ -576,7 +602,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;
Expand Down Expand Up @@ -944,7 +970,7 @@ jQuery.fn.extend({
jQuery.event.remove( this, types, fn, selector );
});
},

bind: function( types, data, fn ) {
return this.on( types, null, data, fn );
},
Expand Down Expand Up @@ -1013,7 +1039,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 ) {
Expand All @@ -1030,6 +1056,14 @@ jQuery.each( ("blur focus focusin focusout load resize scroll unload click dblcl
if ( jQuery.attrFn ) {
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 );
Expand Down
48 changes: 35 additions & 13 deletions test/unit/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
*/
Expand Down Expand Up @@ -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();
});

Expand All @@ -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" );
Expand Down Expand Up @@ -2383,6 +2383,37 @@ test("delegated events quickIs", function() {
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(){
// This code must be run before DOM ready!
Expand Down Expand Up @@ -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();
});
*/

0 comments on commit 36c82a1

Please sign in to comment.