Skip to content

Commit

Permalink
prototype: Make sure event handlers and their wrappers are removed fr…
Browse files Browse the repository at this point in the history
…om the cache by Event.stopObserving.
  • Loading branch information
sstephenson committed Aug 9, 2007
1 parent f3d9cb2 commit 2fbb490
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG
@@ -1,5 +1,7 @@
*SVN*

* Make sure event handlers and their wrappers are removed from the cache by Event.stopObserving. [sam, Severin Heiniger]

* Add line numbers to failures when unit testing in Firefox. Closes #9231. [John Resig]

* Fix Function#argumentNames for Opera and IE. [Thomas Fuchs]
Expand Down
12 changes: 8 additions & 4 deletions src/event.js
Expand Up @@ -24,6 +24,8 @@ Object.extend(Event, {
'DOMSubtreeModified', 'DOMNodeInserted',
'NodeInsertedIntoDocument', 'DOMAttrModified',
'DOMCharacterDataModified'],

cache: { },

relatedTarget: function(event) {
var element;
Expand Down Expand Up @@ -106,7 +108,7 @@ Event.extend = (function() {
})();

Object.extend(Event, (function() {
var cache = { };
var cache = Event.cache;

function getEventID(element) {
if (element._eventID) return element._eventID;
Expand Down Expand Up @@ -151,9 +153,9 @@ Object.extend(Event, (function() {
}

function destroyWrapper(id, eventName, handler) {
var c = getCacheForID(id), name = getDOMEventName(eventName);
if (!c[name]) return false;
c[name] = c[name].without(findWrapper(id, eventName, handler));
var c = getCacheForID(id);
if (!c[eventName]) return false;
c[eventName] = c[eventName].without(findWrapper(id, eventName, handler));
}

function destroyCache() {
Expand Down Expand Up @@ -204,6 +206,8 @@ Object.extend(Event, (function() {
} else {
element.detachEvent("on" + name, wrapper);
}

destroyWrapper(id, eventName, handler);
},

fire: function(element, eventName, memo) {
Expand Down
16 changes: 16 additions & 0 deletions test/unit/event.html
Expand Up @@ -166,6 +166,22 @@ <h1>Prototype Unit test file</h1>
assertEqual(1, count);
}},

testStopObservingRemovesHandlerFromCache: function() { with(this) {
var span = $("span"), observer = function() { }, eventID;

span.observe("somethingHappened", observer);
eventID = span._eventID;

assert(Event.cache[eventID]);
assert(Object.isArray(Event.cache[eventID].somethingHappened));
assertEqual(1, Event.cache[eventID].somethingHappened.length);

span.stopObserving("somethingHappened", observer);
assert(Event.cache[eventID]);
assert(Object.isArray(Event.cache[eventID].somethingHappened));
assertEqual(0, Event.cache[eventID].somethingHappened.length);
}},

testDocumentContentLoadedEventFiresBeforeWindowLoad: function() { with(this) {
assert(eventResults.contentLoaded, "contentLoaded");
assert(eventResults.contentLoaded.endOfDocument, "contentLoaded.endOfDocument");
Expand Down

0 comments on commit 2fbb490

Please sign in to comment.