1 module("data", { teardown: moduleTeardown });
3 test("expando", function(){
6 equals("expando" in jQuery, true, "jQuery is exposing the expando");
9 function dataTests (elem) {
12 function getCacheLength() {
14 for (var i in jQuery.cache) {
21 equals( jQuery.data(elem, "foo"), undefined, "No data exists initially" );
22 strictEqual( jQuery.hasData(elem), false, "jQuery.hasData agrees no data exists initially" );
24 var dataObj = jQuery.data(elem);
25 equals( typeof dataObj, "object", "Calling data with no args gives us a data object reference" );
26 strictEqual( jQuery.data(elem), dataObj, "Calling jQuery.data returns the same data object when called multiple times" );
28 strictEqual( jQuery.hasData(elem), false, "jQuery.hasData agrees no data exists even when an empty data obj exists" );
31 equals( jQuery.data(elem, "foo"), "bar", "Data is readable by jQuery.data when set directly on a returned data object" );
33 strictEqual( jQuery.hasData(elem), true, "jQuery.hasData agrees data exists when data exists" );
35 jQuery.data(elem, "foo", "baz");
36 equals( jQuery.data(elem, "foo"), "baz", "Data can be changed by jQuery.data" );
37 equals( dataObj.foo, "baz", "Changes made through jQuery.data propagate to referenced data object" );
39 jQuery.data(elem, "foo", undefined);
40 equals( jQuery.data(elem, "foo"), "baz", "Data is not unset by passing undefined to jQuery.data" );
42 jQuery.data(elem, "foo", null);
43 strictEqual( jQuery.data(elem, "foo"), null, "Setting null using jQuery.data works OK" );
45 jQuery.data(elem, "foo", "foo1");
47 jQuery.data(elem, { "bar" : "baz", "boom" : "bloz" });
48 strictEqual( jQuery.data(elem, "foo"), "foo1", "Passing an object extends the data object instead of replacing it" );
49 equals( jQuery.data(elem, "boom"), "bloz", "Extending the data object works" );
51 jQuery._data(elem, "foo", "foo2");
52 equals( jQuery._data(elem, "foo"), "foo2", "Setting internal data works" );
53 equals( jQuery.data(elem, "foo"), "foo1", "Setting internal data does not override user data" );
55 var internalDataObj = jQuery.data(elem, jQuery.expando);
56 strictEqual( jQuery._data(elem), internalDataObj, "Internal data object is accessible via jQuery.expando property" );
57 notStrictEqual( dataObj, internalDataObj, "Internal data object is not the same as user data object" );
59 strictEqual( elem.boom, undefined, "Data is never stored directly on the object" );
61 jQuery.removeData(elem, "foo");
62 strictEqual( jQuery.data(elem, "foo"), undefined, "jQuery.removeData removes single properties" );
64 jQuery.removeData(elem);
65 strictEqual( jQuery.data(elem, jQuery.expando), internalDataObj, "jQuery.removeData does not remove internal data if it exists" );
67 jQuery.removeData(elem, undefined, true);
69 strictEqual( jQuery.data(elem, jQuery.expando), undefined, "jQuery.removeData on internal data works" );
70 strictEqual( jQuery.hasData(elem), false, "jQuery.hasData agrees all data has been removed from object" );
72 jQuery._data(elem, "foo", "foo2");
73 strictEqual( jQuery.hasData(elem), true, "jQuery.hasData shows data exists even if it is only internal data" );
75 jQuery.data(elem, "foo", "foo1");
76 equals( jQuery._data(elem, "foo"), "foo2", "Setting user data does not override internal data" );
78 jQuery.removeData(elem, undefined, true);
79 equals( jQuery.data(elem, "foo"), "foo1", "jQuery.removeData for internal data does not remove user data" );
82 var oldCacheLength = getCacheLength();
83 jQuery.removeData(elem, "foo");
85 equals( getCacheLength(), oldCacheLength - 1, "Removing the last item in the data object destroys it" );
88 jQuery.removeData(elem, "foo");
91 if (jQuery.support.deleteExpando) {
93 actual = jQuery.expando in elem;
97 actual = elem[ jQuery.expando ];
100 equals( actual, expected, "Removing the last item in the data object destroys it" );
103 jQuery.data(elem, "foo", "foo1");
104 jQuery._data(elem, "foo", "foo2");
106 equals( jQuery.data(elem, "foo"), "foo1", "(sanity check) Ensure data is set in user data object" );
107 equals( jQuery._data(elem, "foo"), "foo2", "(sanity check) Ensure data is set in internal data object" );
109 jQuery.removeData(elem, "foo", true);
111 strictEqual( jQuery.data(elem, jQuery.expando), undefined, "Removing the last item in internal data destroys the internal data object" );
113 jQuery._data(elem, "foo", "foo2");
114 equals( jQuery._data(elem, "foo"), "foo2", "(sanity check) Ensure data is set in internal data object" );
116 jQuery.removeData(elem, "foo");
117 equals( jQuery._data(elem, "foo"), "foo2", "(sanity check) jQuery.removeData for user data does not remove internal data" );
120 oldCacheLength = getCacheLength();
121 jQuery.removeData(elem, "foo", true);
122 equals( getCacheLength(), oldCacheLength - 1, "Removing the last item in the internal data object also destroys the user data object when it is empty" );
125 jQuery.removeData(elem, "foo", true);
127 if (jQuery.support.deleteExpando) {
129 actual = jQuery.expando in elem;
133 actual = elem[ jQuery.expando ];
136 equals( actual, expected, "Removing the last item in the internal data object also destroys the user data object when it is empty" );
140 test("jQuery.data", function() {
143 var div = document.createElement("div");
148 // remove bound handlers from window object to stop potential false positives caused by fix for #5280 in
150 jQuery(window).unbind("unload");
155 // clean up unattached element
156 jQuery(div).remove();
159 test("jQuery.acceptData", function() {
162 ok( jQuery.acceptData( document ), "document" );
163 ok( jQuery.acceptData( document.documentElement ), "documentElement" );
164 ok( jQuery.acceptData( {} ), "object" );
165 ok( !jQuery.acceptData( document.createElement("embed") ), "embed" );
166 ok( !jQuery.acceptData( document.createElement("applet") ), "applet" );
168 var flash = document.createElement("object");
169 flash.setAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
170 ok( jQuery.acceptData( flash ), "flash" );
172 var applet = document.createElement("object");
173 applet.setAttribute("classid", "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93");
174 ok( !jQuery.acceptData( applet ), "applet" );
177 test(".data()", function() {
180 var div = jQuery("#foo");
181 strictEqual( div.data("foo"), undefined, "Make sure that missing result is undefined" );
182 div.data("test", "success");
183 same( div.data(), {test: "success"}, "data() get the entire data object" );
184 strictEqual( div.data("foo"), undefined, "Make sure that missing result is still undefined" );
186 var nodiv = jQuery("#unfound");
187 equals( nodiv.data(), null, "data() on empty set returns null" );
189 var obj = { foo: "bar" };
190 deepEqual( jQuery(obj).data(), {}, "Retrieve data object from a wrapped JS object (#7524)" );
193 test(".data(String) and .data(String, Object)", function() {
195 var parent = jQuery("<div><div></div></div>"),
196 div = parent.children();
199 .bind("getData", function(){ ok( false, "getData bubbled." ) })
200 .bind("setData", function(){ ok( false, "setData bubbled." ) })
201 .bind("changeData", function(){ ok( false, "changeData bubbled." ) });
203 ok( div.data("test") === undefined, "Check for no data exists" );
205 div.data("test", "success");
206 equals( div.data("test"), "success", "Check for added data" );
208 div.data("test", "overwritten");
209 equals( div.data("test"), "overwritten", "Check for overwritten data" );
211 div.data("test", undefined);
212 equals( div.data("test"), "overwritten", "Check that data wasn't removed");
214 div.data("test", null);
215 ok( div.data("test") === null, "Check for null data");
217 ok( div.data("notexist") === undefined, "Check for no data exists" );
219 div.data("test", "overwritten");
220 var hits = {test:0}, gets = {test:0}, changes = {test:0, value:null};
223 function logChangeData(e,key,value) {
226 dataKey = dataKey + "." + e.namespace;
228 changes[key] += value;
229 changes.value = jQuery.data(e.target, dataKey);
233 .bind("setData",function(e,key,value){ hits[key] += value; })
234 .bind("setData.foo",function(e,key,value){ hits[key] += value; })
235 .bind("changeData",logChangeData)
236 .bind("changeData.foo",logChangeData)
237 .bind("getData",function(e,key){ gets[key] += 1; })
238 .bind("getData.foo",function(e,key){ gets[key] += 3; });
240 div.data("test.foo", 2);
241 equals( div.data("test"), "overwritten", "Check for original data" );
242 equals( div.data("test.foo"), 2, "Check for namespaced data" );
243 equals( div.data("test.bar"), "overwritten", "Check for unmatched namespace" );
244 equals( hits.test, 2, "Check triggered setter functions" );
245 equals( gets.test, 5, "Check triggered getter functions" );
246 equals( changes.test, 2, "Check sets raise changeData");
247 equals( changes.value, 2, "Check changeData after data has been set" );
252 changes.value = null;
255 equals( div.data("test"), 1, "Check for original data" );
256 equals( div.data("test.foo"), 2, "Check for namespaced data" );
257 equals( div.data("test.bar"), 1, "Check for unmatched namespace" );
258 equals( hits.test, 1, "Check triggered setter functions" );
259 equals( gets.test, 5, "Check triggered getter functions" );
260 equals( changes.test, 1, "Check sets raise changeData" );
261 equals( changes.value, 1, "Check changeData after data has been set" );
264 .bind("getData",function(e,key){ return key + "root"; })
265 .bind("getData.foo",function(e,key){ return key + "foo"; });
267 equals( div.data("test"), "testroot", "Check for original data" );
268 equals( div.data("test.foo"), "testfoo", "Check for namespaced data" );
269 equals( div.data("test.bar"), "testroot", "Check for unmatched namespace" );
272 var $elem = jQuery({exists:true});
273 equals( $elem.data('nothing'), undefined, "Non-existent data returns undefined");
274 equals( $elem.data('null',null).data('null'), null, "null's are preserved");
275 equals( $elem.data('emptyString','').data('emptyString'), '', "Empty strings are preserved");
276 equals( $elem.data('false',false).data('false'), false, "false's are preserved");
277 equals( $elem.data('exists'), undefined, "Existing data is not returned" );
281 deepEqual( $elem[0], {exists:true}, "removeData does not clear the object" );
283 // manually clean up detached elements
287 test("data-* attributes", function() {
289 var div = jQuery("<div>"),
290 child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>"),
291 dummy = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>");
293 equals( div.data("attr"), undefined, "Check for non-existing data-attr attribute" );
295 div.attr("data-attr", "exists");
296 equals( div.data("attr"), "exists", "Check for existing data-attr attribute" );
298 div.attr("data-attr", "exists2");
299 equals( div.data("attr"), "exists", "Check that updates to data- don't update .data()" );
301 div.data("attr", "internal").attr("data-attr", "external");
302 equals( div.data("attr"), "internal", "Check for .data('attr') precedence (internal > external data-* attribute)" );
306 child.appendTo('#main');
307 equals( child.data("myobj"), "old data", "Value accessed from data-* attribute");
309 child.data("myobj", "replaced");
310 equals( child.data("myobj"), "replaced", "Original data overwritten");
312 child.data("ignored", "cache");
313 equals( child.data("ignored"), "cache", "Cached data used before DOM data-* fallback");
315 var obj = child.data(), obj2 = dummy.data(), check = [ "myobj", "ignored", "other" ], num = 0, num2 = 0;
319 for ( var i = 0, l = check.length; i < l; i++ ) {
320 ok( obj[ check[i] ], "Make sure data- property exists when calling data-." );
321 ok( obj2[ check[i] ], "Make sure data- property exists when calling data-." );
324 for ( var prop in obj ) {
328 equals( num, check.length, "Make sure that the right number of properties came through." );
330 for ( var prop in obj2 ) {
334 equals( num2, check.length, "Make sure that the right number of properties came through." );
336 child.attr("data-other", "newvalue");
338 equals( child.data("other"), "test", "Make sure value was pulled in properly from a .data()." );
341 .attr("data-true", "true")
342 .attr("data-false", "false")
343 .attr("data-five", "5")
344 .attr("data-point", "5.5")
345 .attr("data-pointe", "5.5E3")
346 .attr("data-pointbad", "5..5")
347 .attr("data-pointbad2", "-.")
348 .attr("data-badjson", "{123}")
349 .attr("data-badjson2", "[abc]")
350 .attr("data-empty", "")
351 .attr("data-space", " ")
352 .attr("data-null", "null")
353 .attr("data-string", "test");
355 strictEqual( child.data('true'), true, "Primitive true read from attribute");
356 strictEqual( child.data('false'), false, "Primitive false read from attribute");
357 strictEqual( child.data('five'), 5, "Primitive number read from attribute");
358 strictEqual( child.data('point'), 5.5, "Primitive number read from attribute");
359 strictEqual( child.data('pointe'), 5500, "Primitive number read from attribute");
360 strictEqual( child.data('pointbad'), "5..5", "Bad number read from attribute");
361 strictEqual( child.data('pointbad2'), "-.", "Bad number read from attribute");
362 strictEqual( child.data('badjson'), "{123}", "Bad number read from attribute");
363 strictEqual( child.data('badjson2'), "[abc]", "Bad number read from attribute");
364 strictEqual( child.data('empty'), "", "Empty string read from attribute");
365 strictEqual( child.data('space'), " ", "Empty string read from attribute");
366 strictEqual( child.data('null'), null, "Primitive null read from attribute");
367 strictEqual( child.data('string'), "test", "Typical string read from attribute");
371 // tests from metadata plugin
372 function testData(index, elem) {
375 equals(jQuery(elem).data("foo"), "bar", "Check foo property");
376 equals(jQuery(elem).data("bar"), "baz", "Check baz property");
379 equals(jQuery(elem).data("test"), "bar", "Check test property");
380 equals(jQuery(elem).data("bar"), "baz", "Check bar property");
383 equals(jQuery(elem).data("zoooo"), "bar", "Check zoooo property");
384 same(jQuery(elem).data("bar"), {"test":"baz"}, "Check bar property");
387 equals(jQuery(elem).data("number"), true, "Check number property");
388 same(jQuery(elem).data("stuff"), [2,8], "Check stuff property");
391 ok(false, ["Assertion failed on index ", index, ", with data ", data].join(''));
395 var metadata = '<ol><li class="test test2" data-foo="bar" data-bar="baz" data-arr="[1,2]">Some stuff</li><li class="test test2" data-test="bar" data-bar="baz">Some stuff</li><li class="test test2" data-zoooo="bar" data-bar=\'{"test":"baz"}\'>Some stuff</li><li class="test test2" data-number=true data-stuff="[2,8]">Some stuff</li></ol>',
396 elem = jQuery(metadata).appendTo('#main');
398 elem.find("li").each(testData);
402 test(".data(Object)", function() {
405 var div = jQuery("<div/>");
407 div.data({ "test": "in", "test2": "in2" });
408 equals( div.data("test"), "in", "Verify setting an object in data" );
409 equals( div.data("test2"), "in2", "Verify setting an object in data" );
411 var obj = {test:"unset"},
413 jqobj.data("test", "unset");
414 jqobj.data({ "test": "in", "test2": "in2" });
415 equals( jQuery.data(obj).test, "in", "Verify setting an object on an object extends the data object" );
416 equals( obj.test2, undefined, "Verify setting an object on an object does not extend the object" );
418 // manually clean up detached elements
422 test("jQuery.removeData", function() {
424 var div = jQuery("#foo")[0];
425 jQuery.data(div, "test", "testing");
426 jQuery.removeData(div, "test");
427 equals( jQuery.data(div, "test"), undefined, "Check removal of data" );
429 jQuery.data(div, "test2", "testing");
430 jQuery.removeData( div );
431 ok( !jQuery.data(div, "test2"), "Make sure that the data property no longer exists." );
432 ok( !div[ jQuery.expando ], "Make sure the expando no longer exists, as well." );
435 jQuery.data(obj, "test", "testing");
436 equals( jQuery(obj).data("test"), "testing", "verify data on plain object");
437 jQuery.removeData(obj, "test");
438 equals( jQuery.data(obj, "test"), undefined, "Check removal of data on plain object" );
440 jQuery.data( window, "BAD", true );
441 jQuery.removeData( window, "BAD" );
442 ok( !jQuery.data( window, "BAD" ), "Make sure that the value was not still set." );
445 test(".removeData()", function() {
447 var div = jQuery("#foo");
448 div.data("test", "testing");
449 div.removeData("test");
450 equals( div.data("test"), undefined, "Check removal of data" );
452 div.data("test", "testing");
453 div.data("test.foo", "testing2");
454 div.removeData("test.bar");
455 equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
456 equals( div.data("test"), "testing", "Make sure data is intact" );
458 div.removeData("test");
459 equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
460 equals( div.data("test"), undefined, "Make sure data is intact" );
462 div.removeData("test.foo");
463 equals( div.data("test.foo"), undefined, "Make sure data is intact" );