From 77b9a2614a90bd758bf0cface597019587103e68 Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Sun, 14 Dec 2008 01:35:12 -0600 Subject: [PATCH] Alter element storage API to handle the `window` object gracefully. --- CHANGELOG | 2 +- src/dom/dom.js | 17 ++++++++++------- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 71f7cea..6933707 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,4 @@ -* Added Element#store and Element#retrieve for safe, hash-backed storage of element metadata (no memory leaks). Also added Element#getStorage for working with the element's storage hash directly. Hat tip: Mootools. (ZenCocoon, Andrew Dupont) +* Add Element#store and Element#retrieve for safe, hash-backed storage of element metadata (no memory leaks). Also add Element#getStorage for working with the element's storage hash directly. Hat tip: Mootools. (ZenCocoon, Andrew Dupont) * Fix issue where certain versions of Safari treat class names case-insensitively in Selector/$$ queries. (Andrew Dupont, kangax, Brice) diff --git a/src/dom/dom.js b/src/dom/dom.js index e90766f..4cfbe20 100644 --- a/src/dom/dom.js +++ b/src/dom/dom.js @@ -1214,11 +1214,15 @@ Element.addMethods({ getStorage: function(element) { if (!(element = $(element))) return; - if (Object.isUndefined(element._prototypeUID)) - element._prototypeUID = [Element.Storage.UID++]; - - var uid = element._prototypeUID[0]; - + var uid; + if (element === window) { + uid = 0; + } else { + if (Object.isUndefined(element._prototypeUID)) + element._prototypeUID = [Element.Storage.UID++]; + uid = element._prototypeUID[0]; + } + if (!Element.Storage[uid]) Element.Storage[uid] = $H(); @@ -1232,8 +1236,7 @@ Element.addMethods({ retrieve: function(element, key, defaultValue) { if (!(element = $(element))) return; - - var hash = element.getStorage(), value = hash.get(key); + var hash = Element.getStorage(element), value = hash.get(key); if (Object.isUndefined(value)) { hash.set(key, defaultValue);