Skip to content

Commit

Permalink
All event tests for fastfix passing
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed May 23, 2012
1 parent 8d60e32 commit 80bf069
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 79 deletions.
159 changes: 80 additions & 79 deletions event/fastfix/fastfix.js
Expand Up @@ -2,91 +2,92 @@ steal('jquery', function () {
// http://bitovi.com/blog/2012/04/faster-jquery-event-fix.html
// https://gist.github.com/2377196

if(Object.defineProperty) {
var set = function (obj, prop, val) {
if(val !== undefined) {
Object.defineProperty(obj, prop, {
value : val
});
}
return val;
},
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) {
if(!original) {
return;
}
return original.fromElement === this.target ? original.toElement : original.fromElement;
},
metaKey : function (originalEvent) {
return originalEvent.ctrlKey;
},
/* TODO this was in here twice. Probably wrong
which : function(original){
var button = original.button;
return ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) );
},
*/
which : function (original) {
return original.charCode != null ? original.charCode : original.keyCode;
}
},
oldEvent = jQuery.Event;

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) {
if(!original) {
return original;
jQuery.each(jQuery.event.keyHooks.props.concat(jQuery.event.mouseHooks.props).concat(jQuery.event.props), function (i, prop) {
if (prop !== "target") {
(function () {
Object.defineProperty(jQuery.Event.prototype, prop, {
get : function () {
// get the original value, undefined when there is no original event
var originalValue = this.originalEvent && this.originalEvent[prop];
// overwrite getter lookup
return this['_' + prop] !== undefined ? this['_' + prop] : set(this, prop,
// if we have a special function and no value
special[prop] && originalValue === undefined ?
// call the special function
special[prop].call(this, this.originalEvent) :
// use the original value
originalValue)
},
set : function (newValue) {
this['_' + prop] = newValue;
}
});
})();
}
return original.fromElement === this.target ? original.toElement : original.fromElement;
},
metaKey : function (originalEvent) {
return originalEvent.ctrlKey;
},
/* TODO this was in here twice. Probably wrong
which : function(original){
var button = original.button;
return ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) );
},
*/
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") {
(function () {
Object.defineProperty(jQuery.Event.prototype, prop, {
get : function () {
// get the original value
var originalValue = this.originalEvent ? this.originalEvent[prop] : null;
// overwrite getter lookup
return this['_' + prop] !== undefined ? this['_' + prop] : 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)
},
set : function (newValue) {
this['_' + prop] = newValue;
}
})
})();
}
})
jQuery.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;
}

$.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;
}

});
5 changes: 5 additions & 0 deletions event/fastfix/fastfix_test.js
Expand Up @@ -1289,6 +1289,11 @@ steal("jquery/event/fastfix",

jQuery("body").unbind("keydown");

// jQuery.extend(ev, { keyCode : 52 })
// // ev['keyCode'] = 42;
// var ev = jQuery.Event('click');
// ev.keyCode = 42;
// console.log(ev);
});

test("jQuery.Event.currentTarget", function () {
Expand Down

0 comments on commit 80bf069

Please sign in to comment.