1 QUnit.module( "data", { afterEach: moduleTeardown } );
3 QUnit.test( "expando", function( assert ) {
6 assert.equal( jQuery.expando !== undefined, true, "jQuery is exposing the expando" );
9 QUnit.test( "jQuery.data & removeData, expected returns", function( assert ) {
11 var elem = document.body;
14 jQuery.data( elem, "hello", "world" ), "world",
15 "jQuery.data( elem, key, value ) returns value"
18 jQuery.data( elem, "hello" ), "world",
19 "jQuery.data( elem, key ) returns value"
22 jQuery.data( elem, { goodnight: "moon" } ), { goodnight: "moon" },
23 "jQuery.data( elem, obj ) returns obj"
26 jQuery.removeData( elem, "hello" ), undefined,
27 "jQuery.removeData( elem, key, value ) returns undefined"
32 QUnit.test( "jQuery._data & _removeData, expected returns", function( assert ) {
34 var elem = document.body;
37 jQuery._data( elem, "hello", "world" ), "world",
38 "jQuery._data( elem, key, value ) returns value"
41 jQuery._data( elem, "hello" ), "world",
42 "jQuery._data( elem, key ) returns value"
45 jQuery._data( elem, { goodnight: "moon" } ), { goodnight: "moon" },
46 "jQuery._data( elem, obj ) returns obj"
49 jQuery._removeData( elem, "hello" ), undefined,
50 "jQuery._removeData( elem, key, value ) returns undefined"
54 QUnit.test( "jQuery.hasData no side effects", function( assert ) {
58 jQuery.hasData( obj );
60 assert.equal( Object.getOwnPropertyNames( obj ).length, 0,
61 "No data expandos where added when calling jQuery.hasData(o)"
65 function dataTests( elem, assert ) {
66 var dataObj, internalDataObj;
68 assert.equal( jQuery.data( elem, "foo" ), undefined, "No data exists initially" );
69 assert.strictEqual( jQuery.hasData( elem ), false, "jQuery.hasData agrees no data exists initially" );
71 dataObj = jQuery.data( elem );
72 assert.equal( typeof dataObj, "object", "Calling data with no args gives us a data object reference" );
73 assert.strictEqual( jQuery.data( elem ), dataObj, "Calling jQuery.data returns the same data object when called multiple times" );
75 assert.strictEqual( jQuery.hasData( elem ), false, "jQuery.hasData agrees no data exists even when an empty data obj exists" );
77 dataObj[ "foo" ] = "bar";
78 assert.equal( jQuery.data( elem, "foo" ), "bar", "Data is readable by jQuery.data when set directly on a returned data object" );
80 assert.strictEqual( jQuery.hasData( elem ), true, "jQuery.hasData agrees data exists when data exists" );
82 jQuery.data( elem, "foo", "baz" );
83 assert.equal( jQuery.data( elem, "foo" ), "baz", "Data can be changed by jQuery.data" );
84 assert.equal( dataObj[ "foo" ], "baz", "Changes made through jQuery.data propagate to referenced data object" );
86 jQuery.data( elem, "foo", undefined );
87 assert.equal( jQuery.data( elem, "foo" ), "baz", "Data is not unset by passing undefined to jQuery.data" );
89 jQuery.data( elem, "foo", null );
90 assert.strictEqual( jQuery.data( elem, "foo" ), null, "Setting null using jQuery.data works OK" );
92 jQuery.data( elem, "foo", "foo1" );
94 jQuery.data( elem, { "bar": "baz", "boom": "bloz" } );
95 assert.strictEqual( jQuery.data( elem, "foo" ), "foo1", "Passing an object extends the data object instead of replacing it" );
96 assert.equal( jQuery.data( elem, "boom" ), "bloz", "Extending the data object works" );
98 jQuery._data( elem, "foo", "foo2", true );
99 assert.equal( jQuery._data( elem, "foo" ), "foo2", "Setting internal data works" );
100 assert.equal( jQuery.data( elem, "foo" ), "foo1", "Setting internal data does not override user data" );
102 internalDataObj = jQuery._data( elem );
103 assert.ok( internalDataObj, "Internal data object exists" );
104 assert.notStrictEqual( dataObj, internalDataObj, "Internal data object is not the same as user data object" );
106 assert.strictEqual( elem.boom, undefined, "Data is never stored directly on the object" );
108 jQuery.removeData( elem, "foo" );
109 assert.strictEqual( jQuery.data( elem, "foo" ), undefined, "jQuery.removeData removes single properties" );
111 jQuery.removeData( elem );
112 assert.strictEqual( jQuery._data( elem ), internalDataObj, "jQuery.removeData does not remove internal data if it exists" );
114 jQuery.data( elem, "foo", "foo1" );
115 jQuery._data( elem, "foo", "foo2" );
117 assert.equal( jQuery.data( elem, "foo" ), "foo1", "(sanity check) Ensure data is set in user data object" );
118 assert.equal( jQuery._data( elem, "foo" ), "foo2", "(sanity check) Ensure data is set in internal data object" );
120 assert.strictEqual( jQuery._data( elem, jQuery.expando ), undefined, "Removing the last item in internal data destroys the internal data object" );
122 jQuery._data( elem, "foo", "foo2" );
123 assert.equal( jQuery._data( elem, "foo" ), "foo2", "(sanity check) Ensure data is set in internal data object" );
125 jQuery.removeData( elem, "foo" );
126 assert.equal( jQuery._data( elem, "foo" ), "foo2", "(sanity check) jQuery.removeData for user data does not remove internal data" );
129 QUnit.test( "jQuery.data(div)", function( assert ) {
132 var div = document.createElement( "div" );
134 dataTests( div, assert );
136 // We stored one key in the private data
137 // assert that nothing else was put in there, and that that
139 assert.expectJqData( this, div, "foo" );
142 QUnit.test( "jQuery.data({})", function( assert ) {
145 dataTests( {}, assert );
148 QUnit.test( "jQuery.data(window)", function( assert ) {
151 // remove bound handlers from window object to stop potential false positives caused by fix for #5280 in
153 jQuery( window ).off( "unload" );
155 dataTests( window, assert );
158 QUnit.test( "jQuery.data(document)", function( assert ) {
161 dataTests( document, assert );
163 assert.expectJqData( this, document, "foo" );
166 QUnit.test( "jQuery.data(<embed>)", function( assert ) {
169 dataTests( document.createElement( "embed" ), assert );
172 QUnit.test( "jQuery.data(object/flash)", function( assert ) {
175 var flash = document.createElement( "object" );
176 flash.setAttribute( "classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" );
178 dataTests( flash, assert );
181 // attempting to access the data of an undefined jQuery element should be undefined
182 QUnit.test( "jQuery().data() === undefined (#14101)", function( assert ) {
185 assert.strictEqual( jQuery().data(), undefined );
186 assert.strictEqual( jQuery().data( "key" ), undefined );
189 QUnit.test( ".data()", function( assert ) {
192 var div, dataObj, nodiv, obj;
194 div = jQuery( "#foo" );
195 assert.strictEqual( div.data( "foo" ), undefined, "Make sure that missing result is undefined" );
196 div.data( "test", "success" );
198 dataObj = div.data();
200 assert.deepEqual( dataObj, { test: "success" }, "data() returns entire data object with expected properties" );
201 assert.strictEqual( div.data( "foo" ), undefined, "Make sure that missing result is still undefined" );
203 nodiv = jQuery( "#unfound" );
204 assert.equal( nodiv.data(), null, "data() on empty set returns null" );
206 obj = { foo: "bar" };
207 jQuery( obj ).data( "foo", "baz" );
209 dataObj = jQuery.extend( true, {}, jQuery( obj ).data() );
211 assert.deepEqual( dataObj, { "foo": "baz" }, "Retrieve data object from a wrapped JS object (#7524)" );
214 function testDataTypes( $obj, assert ) {
225 "object": { foo: "bar" },
228 "function": function() {}
229 }, function( type, value ) {
230 assert.strictEqual( $obj.data( "test", value ).data( "test" ), value, "Data set to " + type );
234 QUnit.test( "jQuery(Element).data(String, Object).data(String)", function( assert ) {
236 var parent = jQuery( "<div><div></div></div>" ),
237 div = parent.children();
239 assert.strictEqual( div.data( "test" ), undefined, "No data exists initially" );
240 assert.strictEqual( div.data( "test", "success" ).data( "test" ), "success", "Data added" );
241 assert.strictEqual( div.data( "test", "overwritten" ).data( "test" ), "overwritten", "Data overwritten" );
242 assert.strictEqual( div.data( "test", undefined ).data( "test" ), "overwritten", ".data(key,undefined) does nothing but is chainable (#5571)" );
243 assert.strictEqual( div.data( "notexist" ), undefined, "No data exists for unset key" );
244 testDataTypes( div, assert );
249 QUnit.test( "jQuery(plain Object).data(String, Object).data(String)", function( assert ) {
253 var $obj = jQuery( { exists: true } );
254 assert.strictEqual( $obj.data( "nothing" ), undefined, "Non-existent data returns undefined" );
255 assert.strictEqual( $obj.data( "exists" ), undefined, "Object properties are not returned as data" );
256 testDataTypes( $obj, assert );
260 assert.deepEqual( $obj[ 0 ], { exists: true }, "removeData does not clear the object" );
263 QUnit.test( ".data(object) does not retain references. #13815", function( assert ) {
266 var $divs = jQuery( "<div></div><div></div>" ).appendTo( "#qunit-fixture" );
268 $divs.data( { "type": "foo" } );
269 $divs.eq( 0 ).data( "type", "bar" );
271 assert.equal( $divs.eq( 0 ).data( "type" ), "bar", "Correct updated value" );
272 assert.equal( $divs.eq( 1 ).data( "type" ), "foo", "Original value retained" );
275 QUnit.test( "data-* attributes", function( assert ) {
278 var prop, i, l, metadata, elem,
279 obj, obj2, check, num, num2,
280 parseJSON = JSON.parse,
281 div = jQuery( "<div>" ),
282 child = jQuery( "<div data-myobj='old data' data-ignored=\"DOM\" data-other='test' data-foo-42='boosh'></div>" ),
283 dummy = jQuery( "<div data-myobj='old data' data-ignored=\"DOM\" data-other='test' data-foo-42='boosh'></div>" );
285 assert.equal( div.data( "attr" ), undefined, "Check for non-existing data-attr attribute" );
287 div.attr( "data-attr", "exists" );
288 assert.equal( div.data( "attr" ), "exists", "Check for existing data-attr attribute" );
290 div.attr( "data-attr", "exists2" );
291 assert.equal( div.data( "attr" ), "exists", "Check that updates to data- don't update .data()" );
293 div.data( "attr", "internal" ).attr( "data-attr", "external" );
294 assert.equal( div.data( "attr" ), "internal", "Check for .data('attr') precedence (internal > external data-* attribute)" );
298 child.appendTo( "#qunit-fixture" );
299 assert.equal( child.data( "myobj" ), "old data", "Value accessed from data-* attribute" );
300 assert.equal( child.data( "foo-42" ), "boosh", "camelCasing does not affect numbers (#1751)" );
302 child.data( "myobj", "replaced" );
303 assert.equal( child.data( "myobj" ), "replaced", "Original data overwritten" );
305 child.data( "ignored", "cache" );
306 assert.equal( child.data( "ignored" ), "cache", "Cached data used before DOM data-* fallback" );
310 check = [ "myobj", "ignored", "other", "foo-42" ];
316 for ( i = 0, l = check.length; i < l; i++ ) {
317 assert.ok( obj[ check[ i ] ], "Make sure data- property exists when calling data-." );
318 assert.ok( obj2[ check[ i ] ], "Make sure data- property exists when calling data-." );
321 for ( prop in obj ) {
325 assert.equal( num, check.length, "Make sure that the right number of properties came through." );
327 for ( prop in obj2 ) {
331 assert.equal( num2, check.length, "Make sure that the right number of properties came through." );
333 child.attr( "data-other", "newvalue" );
335 assert.equal( child.data( "other" ), "test", "Make sure value was pulled in properly from a .data()." );
339 JSON.parse = function() {
341 return parseJSON.apply( this, arguments );
345 .attr( "data-true", "true" )
346 .attr( "data-false", "false" )
347 .attr( "data-five", "5" )
348 .attr( "data-point", "5.5" )
349 .attr( "data-pointe", "5.5E3" )
350 .attr( "data-grande", "5.574E9" )
351 .attr( "data-hexadecimal", "0x42" )
352 .attr( "data-pointbad", "5..5" )
353 .attr( "data-pointbad2", "-." )
354 .attr( "data-bigassnum", "123456789123456789123456789" )
355 .attr( "data-badjson", "{123}" )
356 .attr( "data-badjson2", "[abc]" )
357 .attr( "data-notjson", " {}" )
358 .attr( "data-notjson2", "[] " )
359 .attr( "data-empty", "" )
360 .attr( "data-space", " " )
361 .attr( "data-null", "null" )
362 .attr( "data-string", "test" );
364 assert.strictEqual( child.data( "true" ), true, "Primitive true read from attribute" );
365 assert.strictEqual( child.data( "false" ), false, "Primitive false read from attribute" );
366 assert.strictEqual( child.data( "five" ), 5, "Integer read from attribute" );
367 assert.strictEqual( child.data( "point" ), 5.5, "Floating-point number read from attribute" );
368 assert.strictEqual( child.data( "pointe" ), "5.5E3",
369 "Exponential-notation number read from attribute as string" );
370 assert.strictEqual( child.data( "grande" ), "5.574E9",
371 "Big exponential-notation number read from attribute as string" );
372 assert.strictEqual( child.data( "hexadecimal" ), "0x42",
373 "Hexadecimal number read from attribute as string" );
374 assert.strictEqual( child.data( "pointbad" ), "5..5",
375 "Extra-point non-number read from attribute as string" );
376 assert.strictEqual( child.data( "pointbad2" ), "-.",
377 "No-digit non-number read from attribute as string" );
378 assert.strictEqual( child.data( "bigassnum" ), "123456789123456789123456789",
379 "Bad bigass number read from attribute as string" );
380 assert.strictEqual( child.data( "badjson" ), "{123}", "Bad JSON object read from attribute as string" );
381 assert.strictEqual( child.data( "badjson2" ), "[abc]", "Bad JSON array read from attribute as string" );
382 assert.strictEqual( child.data( "notjson" ), " {}",
383 "JSON object with leading non-JSON read from attribute as string" );
384 assert.strictEqual( child.data( "notjson2" ), "[] ",
385 "JSON array with trailing non-JSON read from attribute as string" );
386 assert.strictEqual( child.data( "empty" ), "", "Empty string read from attribute" );
387 assert.strictEqual( child.data( "space" ), " ", "Whitespace string read from attribute" );
388 assert.strictEqual( child.data( "null" ), null, "Primitive null read from attribute" );
389 assert.strictEqual( child.data( "string" ), "test", "Typical string read from attribute" );
390 assert.equal( i, 2, "Correct number of JSON parse attempts when reading from attributes" );
392 JSON.parse = parseJSON;
395 // tests from metadata plugin
396 function testData( index, elem ) {
399 assert.equal( jQuery( elem ).data( "foo" ), "bar", "Check foo property" );
400 assert.equal( jQuery( elem ).data( "bar" ), "baz", "Check baz property" );
403 assert.equal( jQuery( elem ).data( "test" ), "bar", "Check test property" );
404 assert.equal( jQuery( elem ).data( "bar" ), "baz", "Check bar property" );
407 assert.equal( jQuery( elem ).data( "zoooo" ), "bar", "Check zoooo property" );
408 assert.deepEqual( jQuery( elem ).data( "bar" ), { "test":"baz" }, "Check bar property" );
411 assert.equal( jQuery( elem ).data( "number" ), true, "Check number property" );
412 assert.deepEqual( jQuery( elem ).data( "stuff" ), [ 2, 8 ], "Check stuff property" );
415 assert.ok( false, [ "Assertion failed on index ", index, ", with data" ].join( "" ) );
419 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>";
420 elem = jQuery( metadata ).appendTo( "#qunit-fixture" );
422 elem.find( "li" ).each( testData );
426 QUnit.test( ".data(Object)", function( assert ) {
430 div = jQuery( "<div/>" );
432 div.data( { "test": "in", "test2": "in2" } );
433 assert.equal( div.data( "test" ), "in", "Verify setting an object in data" );
434 assert.equal( div.data( "test2" ), "in2", "Verify setting an object in data" );
436 obj = { test:"unset" };
437 jqobj = jQuery( obj );
439 jqobj.data( "test", "unset" );
440 jqobj.data( { "test": "in", "test2": "in2" } );
441 assert.equal( jQuery.data( obj )[ "test" ], "in", "Verify setting an object on an object extends the data object" );
442 assert.equal( obj[ "test2" ], undefined, "Verify setting an object on an object does not extend the object" );
444 // manually clean up detached elements
448 QUnit.test( "jQuery.removeData", function( assert ) {
452 div = jQuery( "#foo" )[ 0 ];
453 jQuery.data( div, "test", "testing" );
454 jQuery.removeData( div, "test" );
455 assert.equal( jQuery.data( div, "test" ), undefined, "Check removal of data" );
457 jQuery.data( div, "test2", "testing" );
458 jQuery.removeData( div );
459 assert.ok( !jQuery.data( div, "test2" ), "Make sure that the data property no longer exists." );
460 assert.ok( !div[ jQuery.expando ], "Make sure the expando no longer exists, as well." );
466 jQuery.removeData( div, "test3 test4" );
467 assert.ok( !jQuery.data( div, "test3" ) || jQuery.data( div, "test4" ), "Multiple delete with spaces." );
473 jQuery.removeData( div, [ "test3", "test4" ] );
474 assert.ok( !jQuery.data( div, "test3" ) || jQuery.data( div, "test4" ), "Multiple delete by array." );
477 "test3 test4": "testing",
480 jQuery.removeData( div, "test3 test4" );
481 assert.ok( !jQuery.data( div, "test3 test4" ), "Multiple delete with spaces deleted key with exact name" );
482 assert.ok( jQuery.data( div, "test3" ), "Left the partial matched key alone" );
485 jQuery.data( obj, "test", "testing" );
486 assert.equal( jQuery( obj ).data( "test" ), "testing", "verify data on plain object" );
487 jQuery.removeData( obj, "test" );
488 assert.equal( jQuery.data( obj, "test" ), undefined, "Check removal of data on plain object" );
490 jQuery.data( window, "BAD", true );
491 jQuery.removeData( window, "BAD" );
492 assert.ok( !jQuery.data( window, "BAD" ), "Make sure that the value was not still set." );
495 QUnit.test( ".removeData()", function( assert ) {
497 var div = jQuery( "#foo" );
498 div.data( "test", "testing" );
499 div.removeData( "test" );
500 assert.equal( div.data( "test" ), undefined, "Check removal of data" );
502 div.data( "test", "testing" );
503 div.data( "test.foo", "testing2" );
504 div.removeData( "test.bar" );
505 assert.equal( div.data( "test.foo" ), "testing2", "Make sure data is intact" );
506 assert.equal( div.data( "test" ), "testing", "Make sure data is intact" );
508 div.removeData( "test" );
509 assert.equal( div.data( "test.foo" ), "testing2", "Make sure data is intact" );
510 assert.equal( div.data( "test" ), undefined, "Make sure data is intact" );
512 div.removeData( "test.foo" );
513 assert.equal( div.data( "test.foo" ), undefined, "Make sure data is intact" );
516 if ( window.JSON && window.JSON.stringify ) {
517 QUnit.test( "JSON serialization (#8108)", function( assert ) {
520 var obj = { "foo": "bar" };
521 jQuery.data( obj, "hidden", true );
523 assert.equal( JSON.stringify( obj ), "{\"foo\":\"bar\"}", "Expando is hidden from JSON.stringify" );
527 QUnit.test( ".data should follow html5 specification regarding camel casing", function( assert ) {
530 var div = jQuery( "<div id='myObject' data-w-t-f='ftw' data-big-a-little-a='bouncing-b' data-foo='a' data-foo-bar='b' data-foo-bar-baz='c'></div>" )
531 .prependTo( "body" );
533 assert.equal( div.data()[ "wTF" ], "ftw", "Verify single letter data-* key" );
534 assert.equal( div.data()[ "bigALittleA" ], "bouncing-b", "Verify single letter mixed data-* key" );
536 assert.equal( div.data()[ "foo" ], "a", "Verify single word data-* key" );
537 assert.equal( div.data()[ "fooBar" ], "b", "Verify multiple word data-* key" );
538 assert.equal( div.data()[ "fooBarBaz" ], "c", "Verify multiple word data-* key" );
540 assert.equal( div.data( "foo" ), "a", "Verify single word data-* key" );
541 assert.equal( div.data( "fooBar" ), "b", "Verify multiple word data-* key" );
542 assert.equal( div.data( "fooBarBaz" ), "c", "Verify multiple word data-* key" );
544 div.data( "foo-bar", "d" );
546 assert.equal( div.data( "fooBar" ), "d", "Verify updated data-* key" );
547 assert.equal( div.data( "foo-bar" ), "d", "Verify updated data-* key" );
549 assert.equal( div.data( "fooBar" ), "d", "Verify updated data-* key (fooBar)" );
550 assert.equal( div.data( "foo-bar" ), "d", "Verify updated data-* key (foo-bar)" );
555 QUnit.test( ".data should not miss preset data-* w/ hyphenated property names", function( assert ) {
559 var div = jQuery( "<div/>", { id: "hyphened" } ).appendTo( "#qunit-fixture" ),
561 "camelBar": "camelBar",
562 "hyphen-foo": "hyphen-foo"
567 jQuery.each( test, function( i, k ) {
568 assert.equal( div.data( k ), k, "data with property '" + k + "' was correctly found" );
572 QUnit.test( "jQuery.data should not miss data-* w/ hyphenated property names #14047", function( assert ) {
576 var div = jQuery( "<div/>" );
578 div.data( "foo-bar", "baz" );
580 assert.equal( jQuery.data( div[ 0 ], "foo-bar" ), "baz", "data with property 'foo-bar' was correctly found" );
583 QUnit.test( ".data should not miss attr() set data-* with hyphenated property names", function( assert ) {
588 a = jQuery( "<div/>" ).appendTo( "#qunit-fixture" );
590 a.attr( "data-long-param", "test" );
591 a.data( "long-param", { a: 2 } );
593 assert.deepEqual( a.data( "long-param" ), { a: 2 }, "data with property long-param was found, 1" );
595 b = jQuery( "<div/>" ).appendTo( "#qunit-fixture" );
597 b.attr( "data-long-param", "test" );
598 b.data( "long-param" );
599 b.data( "long-param", { a: 2 } );
601 assert.deepEqual( b.data( "long-param" ), { a: 2 }, "data with property long-param was found, 2" );
604 QUnit.test( ".data always sets data with the camelCased key (gh-2257)", function( assert ) {
607 var div = jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
642 // JSHint enforces double quotes,
643 // but JSON strings need double quotes to parse
644 // so we need escaped double quotes here
647 value: "{ \"foo\": \"bar\" }"
651 jQuery.each( datas, function( key, val ) {
652 div.data( key, val.value );
653 var allData = div.data();
654 assert.equal( allData[ key ], undefined, ".data does not store with hyphenated keys" );
655 assert.equal( allData[ val.key ], val.value, ".data stores the camelCased key" );
659 QUnit.test( ".data should not strip more than one hyphen when camelCasing (gh-2070)", function( assert ) {
661 var div = jQuery( "<div data-nested-single='single' data-nested--double='double' data-nested---triple='triple'></div>" ).appendTo( "#qunit-fixture" ),
662 allData = div.data();
664 assert.equal( allData.nestedSingle, "single", "Key is correctly camelCased" );
665 assert.equal( allData[ "nested-Double" ], "double", "Key with double hyphens is correctly camelCased" );
666 assert.equal( allData[ "nested--Triple" ], "triple", "Key with triple hyphens is correctly camelCased" );
669 QUnit.test( ".data supports interoperable hyphenated/camelCase get/set of properties with arbitrary non-null|NaN|undefined values", function( assert ) {
671 var div = jQuery( "<div/>", { id: "hyphened" } ).appendTo( "#qunit-fixture" ),
706 // JSHint enforces double quotes,
707 // but JSON strings need double quotes to parse
708 // so we need escaped double quotes here
711 value: "{ \"foo\": \"bar\" }"
727 // Vendor prefixes are not treated in a special way.
748 jQuery.each( datas, function( key, val ) {
749 div.data( key, val.value );
751 assert.deepEqual( div.data( key ), val.value, "get: " + key );
752 assert.deepEqual( div.data( val.key ), val.value, "get: " + val.key );
756 QUnit.test( ".data supports interoperable removal of hyphenated/camelCase properties", function( assert ) {
757 var div = jQuery( "<div/>", { id: "hyphened" } ).appendTo( "#qunit-fixture" ),
758 rdashAlpha = /-([a-z])/g,
760 "non-empty": "a string",
769 // JSHint enforces double quotes,
770 // but JSON strings need double quotes to parse
771 // so we need escaped double quotes here
772 "some-json": "{ \"foo\": \"bar\" }"
777 function fcamelCase( all, letter ) {
778 return letter.toUpperCase();
781 jQuery.each( datas, function( key, val ) {
782 div.data( key, val );
784 assert.deepEqual( div.data( key ), val, "get: " + key );
786 div.data( key.replace( rdashAlpha, fcamelCase ) ),
788 "get: " + key.replace( rdashAlpha, fcamelCase )
791 div.removeData( key );
793 assert.equal( div.data( key ), undefined, "get: " + key );
798 QUnit.test( ".data supports interoperable removal of properties SET TWICE #13850", function( assert ) {
799 var div = jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
801 "non-empty": "a string",
810 // JSHint enforces double quotes,
811 // but JSON strings need double quotes to parse
812 // so we need escaped double quotes here
813 "some-json": "{ \"foo\": \"bar\" }"
818 jQuery.each( datas, function( key, val ) {
819 div.data( key, val );
820 div.data( key, val );
822 div.removeData( key );
824 assert.equal( div.data( key ), undefined, "removal: " + key );
828 QUnit.test( ".removeData supports removal of hyphenated properties via array (#12786, gh-2257)", function( assert ) {
831 var div, plain, compare;
833 div = jQuery( "<div>" ).appendTo( "#qunit-fixture" );
834 plain = jQuery( {} );
836 // Properties should always be camelCased
839 // From batch assignment .data({ "a-a": 1 })
842 // From property, value assignment .data( "b-b", 1 )
847 div.data( { "a-a": 1 } ).data( "b-b", 1 );
848 plain.data( { "a-a": 1 } ).data( "b-b", 1 );
850 assert.deepEqual( div.data(), compare, "Data appears as expected. (div)" );
851 assert.deepEqual( plain.data(), compare, "Data appears as expected. (plain)" );
853 div.removeData( [ "a-a", "b-b" ] );
854 plain.removeData( [ "a-a", "b-b" ] );
856 assert.deepEqual( div.data(), {}, "Data is empty. (div)" );
857 assert.deepEqual( plain.data(), {}, "Data is empty. (plain)" );
860 // Test originally by Moschel
861 QUnit.test( ".removeData should not throw exceptions. (#10080)", function( assert ) {
862 var done = assert.async();
864 var frame = jQuery( "#loadediframe" );
865 jQuery( frame[ 0 ].contentWindow ).on( "unload", function() {
866 assert.ok( true, "called unload" );
870 // change the url to trigger unload
871 frame.attr( "src", baseURL + "iframe.html?param=true" );
874 QUnit.test( ".data only checks element attributes once. #8909", function( assert ) {
880 element = jQuery( "<div data-test='testing'>" ),
883 // set an attribute using attr to ensure it
884 node.setAttribute( "data-test2", "testing" );
885 assert.deepEqual( element.data(), testing, "Sanity Check" );
887 node.setAttribute( "data-test3", "testing" );
888 assert.deepEqual( element.data(), testing, "The data didn't change even though the data-* attrs did" );
890 // clean up data cache
894 QUnit.test( "data-* with JSON value can have newlines", function( assert ) {
897 var x = jQuery( "<div data-some='{\n\"foo\":\n\t\"bar\"\n}'></div>" );
898 assert.equal( x.data( "some" ).foo, "bar", "got a JSON data- attribute with spaces" );
902 QUnit.test( ".data doesn't throw when calling selection is empty. #13551", function( assert ) {
906 jQuery( null ).data( "prop" );
907 assert.ok( true, "jQuery(null).data('prop') does not throw" );
909 assert.ok( false, e.message );
913 QUnit.test( "acceptData", function( assert ) {
916 var flash, pdf, form;
918 assert.equal( jQuery( document ).data( "test", 42 ).data( "test" ), 42, "document" );
919 assert.equal( jQuery( document.documentElement ).data( "test", 42 ).data( "test" ), 42, "documentElement" );
920 assert.equal( jQuery( {} ).data( "test", 42 ).data( "test" ), 42, "object" );
921 assert.equal( jQuery( document.createElement( "embed" ) ).data( "test", 42 ).data( "test" ), 42, "embed" );
923 flash = document.createElement( "object" );
924 flash.setAttribute( "classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" );
925 assert.equal( jQuery( flash ).data( "test", 42 ).data( "test" ), 42, "flash" );
927 pdf = document.createElement( "object" );
928 pdf.setAttribute( "classid", "clsid:CA8A9780-280D-11CF-A24D-444553540000" );
929 assert.equal( jQuery( pdf ).data( "test", 42 ).data( "test" ), 42, "pdf" );
931 assert.strictEqual( jQuery( document.createComment( "" ) ).data( "test", 42 ).data( "test" ), undefined, "comment" );
932 assert.strictEqual( jQuery( document.createTextNode( "" ) ).data( "test", 42 ).data( "test" ), undefined, "text" );
933 assert.strictEqual( jQuery( document.createDocumentFragment() ).data( "test", 42 ).data( "test" ), undefined, "documentFragment" );
935 form = jQuery( "#form" ).append( "<input id='nodeType'/><input id='nodeName'/>" )[ 0 ];
936 assert.equal( jQuery( form ) .data( "test", 42 ).data( "test" ), 42, "form with aliased DOM properties" );
939 QUnit.test( "Check proper data removal of non-element descendants nodes (#8335)", function( assert ) {
942 var div = jQuery( "<div>text</div>" ),
943 text = div.contents();
945 text.data( "test", "test" ); // This should be a noop.
948 assert.ok( !text.data( "test" ), "Be sure data is not stored in non-element" );
952 "enumerate data attrs on body (#14894)",
953 "data/dataAttrs.html",
954 function( assert, jQuery, window, document, result ) {
956 assert.equal( result, "ok", "enumeration of data- attrs on body" );
960 QUnit.test( "Check that the expando is removed when there's no more data", function( assert ) {
964 div = jQuery( "<div/>" );
965 div.data( "some", "data" );
966 assert.equal( div.data( "some" ), "data", "Data is added" );
967 div.removeData( "some" );
969 // Make sure the expando is gone
970 for ( key in div[ 0 ] ) {
971 if ( /^jQuery/.test( key ) ) {
972 assert.strictEqual( div[ 0 ][ key ], undefined, "Expando was not removed when there was no more data" );
977 QUnit.test( "Check that the expando is removed when there's no more data on non-nodes", function( assert ) {
981 obj = jQuery( { key: 42 } );
982 obj.data( "some", "data" );
983 assert.equal( obj.data( "some" ), "data", "Data is added" );
984 obj.removeData( "some" );
986 // Make sure the expando is gone
987 for ( key in obj[ 0 ] ) {
988 if ( /^jQuery/.test( key ) ) {
989 assert.ok( false, "Expando was not removed when there was no more data" );
994 QUnit.test( ".data(prop) does not create expando", function( assert ) {
998 div = jQuery( "<div/>" );
1001 assert.equal( jQuery.hasData( div[ 0 ] ), false, "No data exists after access" );
1003 // Make sure no expando has been added
1004 for ( key in div[ 0 ] ) {
1005 if ( /^jQuery/.test( key ) ) {
1006 assert.ok( false, "Expando was created on access" );