Skip to content
Permalink
Browse files

Use a key name less likely to result in collisions for events on plai…

…n JS objects. Fixes bug #7150.
  • Loading branch information...
csnover committed Oct 12, 2010
1 parent 6245ecb commit ff6ceadbfd470463e63708413eb5a55bd7e90c69
Showing with 12 additions and 8 deletions.
  1. +11 −7 src/event.js
  2. +1 −1 test/unit/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;
@@ -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." );

1 comment on commit ff6cead

@jitter

This comment has been minimized.

Copy link
Contributor

commented on ff6cead Oct 12, 2010

Maybe I got this wrong but isn't the elem.nodeType ? "events" : "__events__" test missing here #ff6cead event.js Line 289

and hasn't this #ff6cead test/unit/event.js Line 1841 to be replaced with data("__events__") too?

Please sign in to comment.
You can’t perform that action at this time.