Use a key name less likely to result in collisions for events on plain JS objects. Fixes bug #7150.

This commit is contained in:
Colin Snover 2010-10-11 23:35:18 -05:00
parent 6245ecb2b9
commit ff6ceadbfd
2 changed files with 12 additions and 8 deletions

View file

@ -54,7 +54,10 @@ jQuery.event = {
return; return;
} }
var events = elemData.events, // Use a key less likely to result in collisions for plain JS objects.
// Fixes bug #7150.
var eventKey = elem.nodeType ? "events" : "__events__",
events = elemData[ eventKey ],
eventHandle = elemData.handle; eventHandle = elemData.handle;
if ( typeof events === "function" ) { if ( typeof events === "function" ) {
@ -68,7 +71,7 @@ jQuery.event = {
if ( !elem.nodeType ) { if ( !elem.nodeType ) {
// On plain objects, create a fn that acts as the holder // On plain objects, create a fn that acts as the holder
// of the values to avoid JSON serialization of event data // of the values to avoid JSON serialization of event data
elemData.events = elemData = function(){}; elemData[ eventKey ] = elemData = function(){};
} }
elemData.events = events = {}; elemData.events = events = {};
@ -170,8 +173,9 @@ jQuery.event = {
} }
var ret, type, fn, j, i = 0, all, namespaces, namespace, special, eventType, handleObj, origType, var ret, type, fn, j, i = 0, all, namespaces, namespace, special, eventType, handleObj, origType,
eventKey = elem.nodeType ? "events" : "__events__",
elemData = jQuery.data( elem ), elemData = jQuery.data( elem ),
events = elemData && elemData.events; events = elemData && elemData[ eventKey ];
if ( !elemData || !events ) { if ( !elemData || !events ) {
return; return;
@ -278,7 +282,7 @@ jQuery.event = {
handle.elem = null; handle.elem = null;
} }
delete elemData.events; delete elemData[ eventKey ];
delete elemData.handle; delete elemData.handle;
if ( typeof elemData === "function" ) { if ( typeof elemData === "function" ) {
@ -346,7 +350,7 @@ jQuery.event = {
// Trigger the event, it is assumed that "handle" is a function // Trigger the event, it is assumed that "handle" is a function
var handle = elem.nodeType ? var handle = elem.nodeType ?
jQuery.data( elem, "handle" ) : jQuery.data( elem, "handle" ) :
(jQuery.data( elem, "events" ) || {}).handle; (jQuery.data( elem, "__events__" ) || {}).handle;
if ( handle ) { if ( handle ) {
handle.apply( elem, data ); handle.apply( elem, data );
@ -420,7 +424,7 @@ jQuery.event = {
event.namespace = event.namespace || namespace_sort.join("."); event.namespace = event.namespace || namespace_sort.join(".");
events = jQuery.data(this, "events"); events = jQuery.data(this, this.nodeType ? "events" : "__events__");
if ( typeof events === "function" ) { if ( typeof events === "function" ) {
events = events.events; events = events.events;
@ -1044,7 +1048,7 @@ jQuery.each(["live", "die"], function( i, name ) {
function liveHandler( event ) { function liveHandler( event ) {
var stop, maxLevel, elems = [], selectors = [], var stop, maxLevel, elems = [], selectors = [],
related, match, handleObj, elem, j, i, l, data, close, namespace, ret, related, match, handleObj, elem, j, i, l, data, close, namespace, ret,
events = jQuery.data( this, "events" ); events = jQuery.data( this, this.nodeType ? "events" : "__events__" );
if ( typeof events === "function" ) { if ( typeof events === "function" ) {
events = events.events; events = events.events;

View file

@ -457,7 +457,7 @@ test("bind()/trigger()/unbind() on plain object", function() {
ok( true, "Custom event run." ); ok( true, "Custom event run." );
}); });
var events = jQuery(obj).data("events"); var events = jQuery(obj).data("__events__");
ok( events, "Object has events bound." ); ok( events, "Object has events bound." );
equals( typeof events, "function", "'events' expando is a function on plain objects." ); equals( typeof events, "function", "'events' expando is a function on plain objects." );
equals( obj.test, undefined, "Make sure that test event is not on the plain object." ); equals( obj.test, undefined, "Make sure that test event is not on the plain object." );