public
Last active

Makes jQuery.event.fix fast

  • Download Gist
fastfix.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
(function(){
 
var set = function(obj, prop, val){
Object.defineProperty(obj,prop,{
value : val
})
return val;
};
var special = {
pageX : function(original){
var eventDoc = this.target.ownerDocument || document;
doc = eventDoc.documentElement;
body = eventDoc.body;
return original.clientX + ( doc && doc.scrollLeft || body && body.scrollLeft || 0 ) - ( doc && doc.clientLeft || body && body.clientLeft || 0 ) ;
},
pageY : function(original){
var eventDoc = this.target.ownerDocument || document;
doc = eventDoc.documentElement;
body = eventDoc.body;
return original.clientY + ( doc && doc.scrollTop || body && body.scrollTop || 0 ) - ( doc && doc.clientTop || body && body.clientTop || 0 ) ;
},
relatedTarget : function(original){
return original.fromElement === this.target ? original.toElement : original.fromElement;
},
which : function(original){
var button = original.button;
return ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) );
},
metaKey : function(originalEvent){
return originalEvent.ctrlKey;
},
which : function(original){
return original.charCode != null ? original.charCode : original.keyCode;
}
};
$.each($.event.keyHooks.props.concat($.event.mouseHooks.props).concat($.event.props), function(i, prop){
if(prop !== "target"){
Object.defineProperty(jQuery.Event.prototype,prop,{
get : function(){
// get the original value
var originalValue = this.originalEvent[prop]
// overwrite getter lookup
return set(this,prop,
// if we have a special function and no value
special[prop] && originalValue == null ?
// call the special function
special[prop].call(this, this.originalEvent) :
// use the original value
originalValue )
}
})
}
})
$.event.fix = function(event){
if ( event[ jQuery.expando ] ) {
return event;
}
// Create a jQuery event with at minimum a target and type set
var originalEvent = event,
event = jQuery.Event( originalEvent );
 
event.target = originalEvent.target;
// Fix target property, if necessary (#1925, IE 6/7/8 & Safari2)
if ( !event.target ) {
event.target = originalEvent.srcElement || document;
}
 
// Target should not be a text node (#504, Safari)
if ( event.target.nodeType === 3 ) {
event.target = event.target.parentNode;
}
 
return event;
}
})();

A couple of issues:

  1. You have which defined twice in the special object.
  2. You left out some of the property attributes like configurable, set (to make it writable), and enumerable so they default to false which is an implementation inconsistency cross-browser (old IE and the like).
  3. Instead of document in fix use event.currentTarget and currentTarget.ownerDocument || currentTarget so that it works cross-frame.
  4. Instead of using Object.defineProperty in your set function just delete obj[prop]; obj[prop] = val; assuming you set it initially with configurable: true.
  5. You need to add a feature check for Object.defineProperty as some browsers don't have it or it only works on DOM elements (IE8).

Thanks! I wasn't trying to write it perfect yet (wrote it on plane to India a little sleep). But if we do make this a plugin to JMVC, we will certainly fix these issues.

On 3, this was lifted from jQuery, you might want to let them know about it too.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.