aboutsummaryrefslogtreecommitdiffstats
path: root/src/data.js
diff options
context:
space:
mode:
authorColin Snover <github.com@zetafleet.com>2011-02-14 16:22:23 -0600
committerColin Snover <github.com@zetafleet.com>2011-02-14 16:22:23 -0600
commit2ed81708bdacfd4b97b77baef67ad8b75205dd20 (patch)
tree25a6f1dfbf0aa6d9703ada0c79853d76ec84f9d5 /src/data.js
parent217a9919c3adcde198774301aa082e5be8a6489d (diff)
downloadjquery-2ed81708bdacfd4b97b77baef67ad8b75205dd20.tar.gz
jquery-2ed81708bdacfd4b97b77baef67ad8b75205dd20.zip
Hide metadata when serializing JS objects using JSON.stringify via a toJSON hack. Fixes #8108.
Diffstat (limited to 'src/data.js')
-rw-r--r--src/data.js26
1 files changed, 23 insertions, 3 deletions
diff --git a/src/data.js b/src/data.js
index 9fee459a2..66d229256 100644
--- a/src/data.js
+++ b/src/data.js
@@ -24,7 +24,7 @@ jQuery.extend({
hasData: function( elem ) {
elem = elem.nodeType ? jQuery.cache[ elem[jQuery.expando] ] : elem[ jQuery.expando ];
- return !!elem && !jQuery.isEmptyObject(elem);
+ return !!elem && !isEmptyDataObject( elem );
},
data: function( elem, name, data, pvt /* Internal Use Only */ ) {
@@ -64,6 +64,13 @@ jQuery.extend({
if ( !cache[ id ] ) {
cache[ id ] = {};
+
+ // TODO: This is a hack for 1.5 ONLY. Avoids exposing jQuery
+ // metadata on plain JS objects when the object is serialized using
+ // JSON.stringify
+ cache[ id ].toJSON = function () {
+ return undefined;
+ };
}
// An object can be passed to jQuery.data instead of a key/value pair; this gets
@@ -130,7 +137,7 @@ jQuery.extend({
// If there is no data left in the cache, we want to continue
// and let the cache object itself get destroyed
- if ( !jQuery.isEmptyObject(thisCache) ) {
+ if ( !isEmptyDataObject(thisCache) ) {
return;
}
}
@@ -142,7 +149,7 @@ jQuery.extend({
// Don't destroy the parent cache unless the internal data object
// had been the only thing left in it
- if ( !jQuery.isEmptyObject(cache[ id ]) ) {
+ if ( !isEmptyDataObject(cache[ id ]) ) {
return;
}
}
@@ -291,4 +298,17 @@ function dataAttr( elem, key, data ) {
return data;
}
+// TODO: This is a hack for 1.5 ONLY to allow objects with a single toJSON
+// property to be considered empty objects; this property always exists in
+// order to make sure JSON.stringify does not expose internal metadata
+function isEmptyDataObject( obj ) {
+ for ( var name in obj ) {
+ if ( name !== "toJSON" ) {
+ return false;
+ }
+ }
+
+ return true;
+}
+
})( jQuery );