aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorColin Snover <github.com@zetafleet.com>2011-01-09 15:52:33 -0600
committerColin Snover <github.com@zetafleet.com>2011-01-09 15:52:33 -0600
commit8e59a99e0ade75dec434f246f52e8b3f7393f359 (patch)
treefa4368b2445650070c78122102b7c85d5293116b /test
parent1d1d4fe112c49cbd704d880b27cc646f2bfe1737 (diff)
downloadjquery-8e59a99e0ade75dec434f246f52e8b3f7393f359.tar.gz
jquery-8e59a99e0ade75dec434f246f52e8b3f7393f359.zip
Change the way jQuery.data works so that there is no longer a chance of collision between user data and internal data. Fixes #6968.
Diffstat (limited to 'test')
-rw-r--r--test/unit/attributes.js10
-rw-r--r--test/unit/data.js238
-rw-r--r--test/unit/effects.js4
-rw-r--r--test/unit/event.js20
4 files changed, 179 insertions, 93 deletions
diff --git a/test/unit/attributes.js b/test/unit/attributes.js
index a1ab58179..04f168466 100644
--- a/test/unit/attributes.js
+++ b/test/unit/attributes.js
@@ -703,12 +703,12 @@ var testToggleClass = function(valueObj) {
// toggleClass storage
e.toggleClass(true);
- ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
+ ok( e[0].className === "", "Assert class is empty (data was empty)" );
e.addClass("testD testE");
ok( e.is(".testD.testE"), "Assert class present" );
e.toggleClass();
ok( !e.is(".testD.testE"), "Assert class not present" );
- ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
+ ok( jQuery._data(e[0], '__className__') === 'testD testE', "Assert data was stored" );
e.toggleClass();
ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
e.toggleClass(false);
@@ -720,11 +720,9 @@ var testToggleClass = function(valueObj) {
e.toggleClass();
ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
-
-
// Cleanup
e.removeClass("testD");
- e.removeData('__className__');
+ jQuery.removeData(e[0], '__className__', true);
};
test("toggleClass(String|boolean|undefined[, boolean])", function() {
@@ -785,7 +783,7 @@ test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
// Cleanup
e.removeClass("test");
- e.removeData('__className__');
+ jQuery.removeData(e[0], '__className__', true);
});
test("addClass, removeClass, hasClass", function() {
diff --git a/test/unit/data.js b/test/unit/data.js
index 310cd6bc4..28e19a3e2 100644
--- a/test/unit/data.js
+++ b/test/unit/data.js
@@ -1,96 +1,177 @@
-module("data");
+module("data", { teardown: moduleTeardown });
test("expando", function(){
- expect(6);
+ expect(1);
equals("expando" in jQuery, true, "jQuery is exposing the expando");
+});
- var obj = {};
- equals( jQuery.data(obj), obj, "jQuery.data(obj) returns the object");
- equals( jQuery.expando in obj, false, "jQuery.data(obj) did not add an expando to the object" );
+function dataTests (elem) {
+ // expect(32)
- obj = {};
- jQuery.data(obj, 'test');
- equals( jQuery.expando in obj, false, "jQuery.data(obj,key) did not add an expando to the object" );
+ function getCacheLength() {
+ var cacheLength = 0;
+ for (var i in jQuery.cache) {
+ ++cacheLength;
+ }
- obj = {};
- jQuery.data(obj, "foo", "bar");
- equals( jQuery.expando in obj, false, "jQuery.data(obj,key,value) did not add an expando to the object" );
- equals( obj.foo, "bar", "jQuery.data(obj,key,value) sets fields directly on the object." );
-});
+ return cacheLength;
+ }
-test("jQuery.acceptData", function() {
- expect(7);
+ equals( jQuery.data(elem, "foo"), undefined, "No data exists initially" );
+ strictEqual( jQuery.hasData(elem), false, "jQuery.hasData agrees no data exists initially" );
- ok( jQuery.acceptData( document ), "document" );
- ok( jQuery.acceptData( document.documentElement ), "documentElement" );
- ok( jQuery.acceptData( {} ), "object" );
- ok( !jQuery.acceptData( document.createElement("embed") ), "embed" );
- ok( !jQuery.acceptData( document.createElement("applet") ), "applet" );
+ var dataObj = jQuery.data(elem);
+ equals( typeof dataObj, "object", "Calling data with no args gives us a data object reference" );
+ strictEqual( jQuery.data(elem), dataObj, "Calling jQuery.data returns the same data object when called multiple times" );
- var flash = document.createElement("object");
- flash.setAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
- ok( jQuery.acceptData( flash ), "flash" );
+ strictEqual( jQuery.hasData(elem), false, "jQuery.hasData agrees no data exists even when an empty data obj exists" );
- var applet = document.createElement("object");
- applet.setAttribute("classid", "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93");
- ok( !jQuery.acceptData( applet ), "applet" );
-});
+ dataObj.foo = "bar";
+ equals( jQuery.data(elem, "foo"), "bar", "Data is readable by jQuery.data when set directly on a returned data object" );
-test("jQuery.data", function() {
- expect(15);
- var div = document.createElement("div");
+ strictEqual( jQuery.hasData(elem), true, "jQuery.hasData agrees data exists when data exists" );
- ok( jQuery.data(div, "test") === undefined, "Check for no data exists" );
+ jQuery.data(elem, "foo", "baz");
+ equals( jQuery.data(elem, "foo"), "baz", "Data can be changed by jQuery.data" );
+ equals( dataObj.foo, "baz", "Changes made through jQuery.data propagate to referenced data object" );
- jQuery.data(div, "test", "success");
- equals( jQuery.data(div, "test"), "success", "Check for added data" );
+ jQuery.data(elem, "foo", undefined);
+ equals( jQuery.data(elem, "foo"), "baz", "Data is not unset by passing undefined to jQuery.data" );
- ok( jQuery.data(div, "notexist") === undefined, "Check for no data exists" );
+ jQuery.data(elem, "foo", null);
+ strictEqual( jQuery.data(elem, "foo"), null, "Setting null using jQuery.data works OK" );
- var data = jQuery.data(div);
- same( data, { "test": "success" }, "Return complete data set" );
+ jQuery.data(elem, "foo", "foo1");
- jQuery.data(div, "test", "overwritten");
- equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );
+ jQuery.data(elem, { "bar" : "baz", "boom" : "bloz" });
+ strictEqual( jQuery.data(elem, "foo"), "foo1", "Passing an object extends the data object instead of replacing it" );
+ equals( jQuery.data(elem, "boom"), "bloz", "Extending the data object works" );
- jQuery.data(div, "test", undefined);
- equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");
+ jQuery._data(elem, "foo", "foo2");
+ equals( jQuery._data(elem, "foo"), "foo2", "Setting internal data works" );
+ equals( jQuery.data(elem, "foo"), "foo1", "Setting internal data does not override user data" );
- jQuery.data(div, "test", null);
- ok( jQuery.data(div, "test") === null, "Check for null data");
+ var internalDataObj = jQuery.data(elem, jQuery.expando);
+ strictEqual( jQuery._data(elem), internalDataObj, "Internal data object is accessible via jQuery.expando property" );
+ notStrictEqual( dataObj, internalDataObj, "Internal data object is not the same as user data object" );
- jQuery.data(div, "test3", "orig");
- jQuery.data(div, { "test": "in", "test2": "in2" });
- equals( jQuery.data(div, "test"), "in", "Verify setting an object in data" );
- equals( jQuery.data(div, "test2"), "in2", "Verify setting an object in data" );
- equals( jQuery.data(div, "test3"), "orig", "Verify original not overwritten" );
+ strictEqual( elem.boom, undefined, "Data is never stored directly on the object" );
- var obj = {};
- jQuery.data( obj, "prop", true );
+ jQuery.removeData(elem, "foo");
+ strictEqual( jQuery.data(elem, "foo"), undefined, "jQuery.removeData removes single properties" );
- ok( obj.prop, "Data is being stored on the object" );
- equals( jQuery.data( obj, "prop" ), true, "Make sure the right value is retrieved" );
+ jQuery.removeData(elem);
+ strictEqual( jQuery.data(elem, jQuery.expando), internalDataObj, "jQuery.removeData does not remove internal data if it exists" );
- jQuery.data( window, "BAD", true );
- ok( !window[ jQuery.expando ], "Make sure there is no expando on the window object." );
- ok( !window.BAD, "And make sure that the property wasn't set directly on the window." );
- ok( jQuery.data( window, "BAD" ), "Make sure that the value was set." );
-});
+ jQuery.removeData(elem, undefined, true);
-test("jQuery.hasData", function() {
- expect(6);
+ strictEqual( jQuery.data(elem, jQuery.expando), undefined, "jQuery.removeData on internal data works" );
+ strictEqual( jQuery.hasData(elem), false, "jQuery.hasData agrees all data has been removed from object" );
+
+ jQuery._data(elem, "foo", "foo2");
+ strictEqual( jQuery.hasData(elem), true, "jQuery.hasData shows data exists even if it is only internal data" );
+
+ jQuery.data(elem, "foo", "foo1");
+ equals( jQuery._data(elem, "foo"), "foo2", "Setting user data does not override internal data" );
+
+ jQuery.removeData(elem, undefined, true);
+ equals( jQuery.data(elem, "foo"), "foo1", "jQuery.removeData for internal data does not remove user data" );
+
+ if (elem.nodeType) {
+ var oldCacheLength = getCacheLength();
+ jQuery.removeData(elem, "foo");
- function testData(obj) {
- equals( jQuery.hasData(obj), false, "No data exists" );
- jQuery.data( obj, "foo", "bar" );
- equals( jQuery.hasData(obj), true, "Data exists" );
- jQuery.removeData( obj, "foo" );
- equals( jQuery.hasData(obj), false, "Data was removed" );
+ equals( getCacheLength(), oldCacheLength - 1, "Removing the last item in the data object destroys it" );
}
+ else {
+ jQuery.removeData(elem, "foo");
+ var expected, actual;
+
+ if (jQuery.support.deleteExpando) {
+ expected = false;
+ actual = jQuery.expando in elem;
+ }
+ else {
+ expected = null;
+ actual = elem[ jQuery.expando ];
+ }
+
+ equals( actual, expected, "Removing the last item in the data object destroys it" );
+ }
+
+ jQuery.data(elem, "foo", "foo1");
+ jQuery._data(elem, "foo", "foo2");
+
+ equals( jQuery.data(elem, "foo"), "foo1", "(sanity check) Ensure data is set in user data object" );
+ equals( jQuery._data(elem, "foo"), "foo2", "(sanity check) Ensure data is set in internal data object" );
+
+ jQuery.removeData(elem, "foo", true);
+
+ strictEqual( jQuery.data(elem, jQuery.expando), undefined, "Removing the last item in internal data destroys the internal data object" );
+
+ jQuery._data(elem, "foo", "foo2");
+ equals( jQuery._data(elem, "foo"), "foo2", "(sanity check) Ensure data is set in internal data object" );
- testData(document.createElement('div'));
- testData({});
+ jQuery.removeData(elem, "foo");
+ equals( jQuery._data(elem, "foo"), "foo2", "(sanity check) jQuery.removeData for user data does not remove internal data" );
+
+ if (elem.nodeType) {
+ oldCacheLength = getCacheLength();
+ jQuery.removeData(elem, "foo", true);
+ equals( getCacheLength(), oldCacheLength - 1, "Removing the last item in the internal data object also destroys the user data object when it is empty" );
+ }
+ else {
+ jQuery.removeData(elem, "foo", true);
+
+ if (jQuery.support.deleteExpando) {
+ expected = false;
+ actual = jQuery.expando in elem;
+ }
+ else {
+ expected = null;
+ actual = elem[ jQuery.expando ];
+ }
+
+ equals( actual, expected, "Removing the last item in the internal data object also destroys the user data object when it is empty" );
+ }
+}
+
+test("jQuery.data", function() {
+ expect(128);
+
+ var div = document.createElement("div");
+
+ dataTests(div);
+ dataTests({});
+
+ // remove bound handlers from window object to stop potential false positives caused by fix for #5280 in
+ // transports/xhr.js
+ jQuery(window).unbind("unload");
+
+ dataTests(window);
+ dataTests(document);
+
+ // clean up unattached element
+ jQuery(div).remove();
+});
+
+test("jQuery.acceptData", function() {
+ expect(7);
+
+ ok( jQuery.acceptData( document ), "document" );
+ ok( jQuery.acceptData( document.documentElement ), "documentElement" );
+ ok( jQuery.acceptData( {} ), "object" );
+ ok( !jQuery.acceptData( document.createElement("embed") ), "embed" );
+ ok( !jQuery.acceptData( document.createElement("applet") ), "applet" );
+
+ var flash = document.createElement("object");
+ flash.setAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
+ ok( jQuery.acceptData( flash ), "flash" );
+
+ var applet = document.createElement("object");
+ applet.setAttribute("classid", "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93");
+ ok( !jQuery.acceptData( applet ), "applet" );
});
test(".data()", function() {
@@ -98,7 +179,6 @@ test(".data()", function() {
var div = jQuery("#foo");
strictEqual( div.data("foo"), undefined, "Make sure that missing result is undefined" );
-
div.data("test", "success");
same( div.data(), {test: "success"}, "data() get the entire data object" );
strictEqual( div.data("foo"), undefined, "Make sure that missing result is still undefined" );
@@ -107,7 +187,7 @@ test(".data()", function() {
equals( nodiv.data(), null, "data() on empty set returns null" );
var obj = { foo: "bar" };
- equals( jQuery(obj).data(), obj, "Retrieve data object from a wrapped JS object (#7524)" );
+ deepEqual( jQuery(obj).data(), {}, "Retrieve data object from a wrapped JS object (#7524)" );
})
test(".data(String) and .data(String, Object)", function() {
@@ -194,11 +274,14 @@ test(".data(String) and .data(String, Object)", function() {
equals( $elem.data('null',null).data('null'), null, "null's are preserved");
equals( $elem.data('emptyString','').data('emptyString'), '', "Empty strings are preserved");
equals( $elem.data('false',false).data('false'), false, "false's are preserved");
- equals( $elem.data('exists'), true, "Existing data is returned" );
+ equals( $elem.data('exists'), undefined, "Existing data is not returned" );
// Clean up
$elem.removeData();
- ok( jQuery.isEmptyObject( $elem[0] ), "removeData clears the object" );
+ deepEqual( $elem[0], {exists:true}, "removeData does not clear the object" );
+
+ // manually clean up detached elements
+ parent.remove();
});
test("data-* attributes", function() {
@@ -323,13 +406,17 @@ test(".data(Object)", function() {
var obj = {test:"unset"},
jqobj = jQuery(obj);
+ jqobj.data("test", "unset");
jqobj.data({ "test": "in", "test2": "in2" });
- equals( obj.test, "in", "Verify setting an object on an object extends the object" );
- equals( obj.test2, "in2", "Verify setting an object on an object extends the object" );
+ equals( jQuery.data(obj).test, "in", "Verify setting an object on an object extends the data object" );
+ equals( obj.test2, undefined, "Verify setting an object on an object does not extend the object" );
+
+ // manually clean up detached elements
+ div.remove();
});
test("jQuery.removeData", function() {
- expect(7);
+ expect(6);
var div = jQuery("#foo")[0];
jQuery.data(div, "test", "testing");
jQuery.removeData(div, "test");
@@ -342,10 +429,9 @@ test("jQuery.removeData", function() {
var obj = {};
jQuery.data(obj, "test", "testing");
- equals( obj.test, "testing", "verify data on plain object");
+ equals( jQuery(obj).data("test"), "testing", "verify data on plain object");
jQuery.removeData(obj, "test");
equals( jQuery.data(obj, "test"), undefined, "Check removal of data on plain object" );
- equals( obj.test, undefined, "Check removal of data directly from plain object" );
jQuery.data( window, "BAD", true );
jQuery.removeData( window, "BAD" );
@@ -371,4 +457,4 @@ test(".removeData()", function() {
div.removeData("test.foo");
equals( div.data("test.foo"), undefined, "Make sure data is intact" );
-});
+}); \ No newline at end of file
diff --git a/test/unit/effects.js b/test/unit/effects.js
index b7b60abbe..cf9d13109 100644
--- a/test/unit/effects.js
+++ b/test/unit/effects.js
@@ -895,7 +895,7 @@ test("hide hidden elements (bug #7141)", function() {
var div = jQuery("<div style='display:none'></div>").appendTo("#main");
equals( div.css("display"), "none", "Element is hidden by default" );
div.hide();
- ok( !div.data("olddisplay"), "olddisplay is undefined after hiding an already-hidden element" );
+ ok( !jQuery._data(div, "olddisplay"), "olddisplay is undefined after hiding an already-hidden element" );
div.show();
equals( div.css("display"), "block", "Show a double-hidden element" );
@@ -910,7 +910,7 @@ test("hide hidden elements, with animation (bug #7141)", function() {
var div = jQuery("<div style='display:none'></div>").appendTo("#main");
equals( div.css("display"), "none", "Element is hidden by default" );
div.hide(1, function () {
- ok( !div.data("olddisplay"), "olddisplay is undefined after hiding an already-hidden element" );
+ ok( !jQuery._data(div, "olddisplay"), "olddisplay is undefined after hiding an already-hidden element" );
div.show(1, function () {
equals( div.css("display"), "block", "Show a double-hidden element" );
start();
diff --git a/test/unit/event.js b/test/unit/event.js
index b4672a8b8..cae1a83fe 100644
--- a/test/unit/event.js
+++ b/test/unit/event.js
@@ -28,7 +28,7 @@ test("bind(), with data", function() {
};
jQuery("#firstp").bind("click", {foo: "bar"}, handler).click().unbind("click", handler);
- ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
+ ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
});
test("click(), with data", function() {
@@ -39,7 +39,7 @@ test("click(), with data", function() {
};
jQuery("#firstp").click({foo: "bar"}, handler).click().unbind("click", handler);
- ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
+ ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using data." );
});
test("bind(), with data, trigger with data", function() {
@@ -505,7 +505,7 @@ test("bind(), with different this object", function() {
.bind("click", jQuery.proxy(handler1, thisObject)).click().unbind("click", handler1)
.bind("click", data, jQuery.proxy(handler2, thisObject)).click().unbind("click", handler2);
- ok( !jQuery.data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
+ ok( !jQuery._data(jQuery("#firstp")[0], "events"), "Event handler unbound when using different this object and data." );
});
test("bind(name, false), unbind(name, false)", function() {
@@ -547,7 +547,7 @@ test("bind()/trigger()/unbind() on plain object", function() {
}
});
- var events = jQuery(obj).data("__events__");
+ var events = jQuery._data(obj, "events");
ok( events, "Object has events bound." );
equals( obj.events, undefined, "Events object on plain objects is not events" );
equals( typeof events, "function", "'events' expando is a function on plain objects." );
@@ -567,7 +567,9 @@ test("bind()/trigger()/unbind() on plain object", function() {
// Make sure it doesn't complain when no events are found
jQuery(obj).unbind("test");
- equals( obj.__events__, undefined, "Make sure events object is removed" );
+ equals( obj && obj[ jQuery.expando ] &&
+ obj[ jQuery.expando ][ jQuery.expando ] &&
+ obj[ jQuery.expando ][ jQuery.expando ].events, undefined, "Make sure events object is removed" );
});
test("unbind(type)", function() {
@@ -947,7 +949,7 @@ test("toggle(Function, Function, ...)", function() {
equals( turn, 2, "Trying toggle with 3 functions, attempt 5 yields 2");
$div.unbind('click',fns[0]);
- var data = jQuery.data( $div[0], 'events' );
+ var data = jQuery._data( $div[0], 'events' );
ok( !data, "Unbinding one function from toggle unbinds them all");
// Test Multi-Toggles
@@ -1065,7 +1067,7 @@ test(".live()/.die()", function() {
equals( clicked, 2, "live with a context" );
// Make sure the event is actually stored on the context
- ok( jQuery.data(container, "events").live, "live with a context" );
+ ok( jQuery._data(container, "events").live, "live with a context" );
// Test unbinding with a different context
jQuery("#foo", container).die("click");
@@ -1578,7 +1580,7 @@ test(".delegate()/.undelegate()", function() {
equals( clicked, 2, "delegate with a context" );
// Make sure the event is actually stored on the context
- ok( jQuery.data(container, "events").live, "delegate with a context" );
+ ok( jQuery._data(container, "events").live, "delegate with a context" );
// Test unbinding with a different context
jQuery("#main").undelegate("#foo", "click");
@@ -1907,7 +1909,7 @@ test("window resize", function() {
ok( true, "Resize event fired." );
}).resize().unbind("resize");
- ok( !jQuery(window).data("__events__"), "Make sure all the events are gone." );
+ ok( !jQuery._data(window, "__events__"), "Make sure all the events are gone." );
});
test("focusin bubbles", function() {