From ff6ceadbfd470463e63708413eb5a55bd7e90c69 Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Mon, 11 Oct 2010 23:35:18 -0500 Subject: [PATCH] Use a key name less likely to result in collisions for events on plain JS objects. Fixes bug #7150. --- src/event.js | 18 +++++++++++------- test/unit/event.js | 2 +- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/event.js b/src/event.js index d949d0f0f..1bbf1348d 100644 --- a/src/event.js +++ b/src/event.js @@ -54,7 +54,10 @@ jQuery.event = { 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; if ( typeof events === "function" ) { @@ -68,7 +71,7 @@ jQuery.event = { if ( !elem.nodeType ) { // On plain objects, create a fn that acts as the holder // of the values to avoid JSON serialization of event data - elemData.events = elemData = function(){}; + elemData[ eventKey ] = elemData = function(){}; } elemData.events = events = {}; @@ -170,8 +173,9 @@ jQuery.event = { } var ret, type, fn, j, i = 0, all, namespaces, namespace, special, eventType, handleObj, origType, + eventKey = elem.nodeType ? "events" : "__events__", elemData = jQuery.data( elem ), - events = elemData && elemData.events; + events = elemData && elemData[ eventKey ]; if ( !elemData || !events ) { return; @@ -278,7 +282,7 @@ jQuery.event = { handle.elem = null; } - delete elemData.events; + delete elemData[ eventKey ]; delete elemData.handle; if ( typeof elemData === "function" ) { @@ -346,7 +350,7 @@ jQuery.event = { // Trigger the event, it is assumed that "handle" is a function var handle = elem.nodeType ? jQuery.data( elem, "handle" ) : - (jQuery.data( elem, "events" ) || {}).handle; + (jQuery.data( elem, "__events__" ) || {}).handle; if ( handle ) { handle.apply( elem, data ); @@ -420,7 +424,7 @@ jQuery.event = { event.namespace = event.namespace || namespace_sort.join("."); - events = jQuery.data(this, "events"); + events = jQuery.data(this, this.nodeType ? "events" : "__events__"); if ( typeof events === "function" ) { events = events.events; @@ -1044,7 +1048,7 @@ jQuery.each(["live", "die"], function( i, name ) { function liveHandler( event ) { var stop, maxLevel, elems = [], selectors = [], 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" ) { events = events.events; diff --git a/test/unit/event.js b/test/unit/event.js index b093e8e71..b37b253a8 100644 --- a/test/unit/event.js +++ b/test/unit/event.js @@ -457,7 +457,7 @@ test("bind()/trigger()/unbind() on plain object", function() { ok( true, "Custom event run." ); }); - var events = jQuery(obj).data("events"); + var events = jQuery(obj).data("__events__"); ok( events, "Object has events bound." ); 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." ); -- 2.39.5