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" );
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
);
137 QUnit
.test( "jQuery.data({})", function( assert
) {
140 dataTests( {}, assert
);
143 QUnit
.test( "jQuery.data(window)", function( assert
) {
146 // remove bound handlers from window object to stop potential false positives caused by fix for trac-5280 in
148 jQuery( window
).off( "unload" );
150 dataTests( window
, assert
);
153 QUnit
.test( "jQuery.data(document)", function( assert
) {
156 dataTests( document
, assert
);
159 QUnit
.test( "jQuery.data(<embed>)", function( assert
) {
162 dataTests( document
.createElement( "embed" ), assert
);
165 QUnit
.test( "jQuery.data(object/flash)", function( assert
) {
168 var flash
= document
.createElement( "object" );
169 flash
.setAttribute( "classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" );
171 dataTests( flash
, assert
);
174 // attempting to access the data of an undefined jQuery element should be undefined
175 QUnit
.test( "jQuery().data() === undefined (trac-14101)", function( assert
) {
178 assert
.strictEqual( jQuery().data(), undefined );
179 assert
.strictEqual( jQuery().data( "key" ), undefined );
182 QUnit
.test( ".data()", function( assert
) {
185 var div
, dataObj
, nodiv
, obj
;
187 div
= jQuery( "#foo" );
188 assert
.strictEqual( div
.data( "foo" ), undefined, "Make sure that missing result is undefined" );
189 div
.data( "test", "success" );
191 dataObj
= div
.data();
193 assert
.deepEqual( dataObj
, { test
: "success" }, "data() returns entire data object with expected properties" );
194 assert
.strictEqual( div
.data( "foo" ), undefined, "Make sure that missing result is still undefined" );
196 nodiv
= jQuery( "#unfound" );
197 assert
.equal( nodiv
.data(), null, "data() on empty set returns null" );
199 obj
= { foo
: "bar" };
200 jQuery( obj
).data( "foo", "baz" );
202 dataObj
= jQuery
.extend( true, {}, jQuery( obj
).data() );
204 assert
.deepEqual( dataObj
, { "foo": "baz" }, "Retrieve data object from a wrapped JS object (trac-7524)" );
207 function testDataTypes( $obj
, assert
) {
218 "object": { foo
: "bar" },
221 "function": function() {}
222 }, function( type
, value
) {
223 assert
.strictEqual( $obj
.data( "test", value
).data( "test" ), value
, "Data set to " + type
);
227 QUnit
.test( "jQuery(Element).data(String, Object).data(String)", function( assert
) {
229 var parent
= jQuery( "<div><div></div></div>" ),
230 div
= parent
.children();
232 assert
.strictEqual( div
.data( "test" ), undefined, "No data exists initially" );
233 assert
.strictEqual( div
.data( "test", "success" ).data( "test" ), "success", "Data added" );
234 assert
.strictEqual( div
.data( "test", "overwritten" ).data( "test" ), "overwritten", "Data overwritten" );
235 assert
.strictEqual( div
.data( "test", undefined ).data( "test" ), "overwritten", ".data(key,undefined) does nothing but is chainable (trac-5571)" );
236 assert
.strictEqual( div
.data( "notexist" ), undefined, "No data exists for unset key" );
237 testDataTypes( div
, assert
);
242 QUnit
.test( "jQuery(plain Object).data(String, Object).data(String)", function( assert
) {
246 var $obj
= jQuery( { exists
: true } );
247 assert
.strictEqual( $obj
.data( "nothing" ), undefined, "Non-existent data returns undefined" );
248 assert
.strictEqual( $obj
.data( "exists" ), undefined, "Object properties are not returned as data" );
249 testDataTypes( $obj
, assert
);
253 assert
.deepEqual( $obj
[ 0 ], { exists
: true }, "removeData does not clear the object" );
256 QUnit
.test( ".data(object) does not retain references. trac-13815", function( assert
) {
259 var $divs
= jQuery( "<div></div><div></div>" ).appendTo( "#qunit-fixture" );
261 $divs
.data( { "type": "foo" } );
262 $divs
.eq( 0 ).data( "type", "bar" );
264 assert
.equal( $divs
.eq( 0 ).data( "type" ), "bar", "Correct updated value" );
265 assert
.equal( $divs
.eq( 1 ).data( "type" ), "foo", "Original value retained" );
268 QUnit
.test( "data-* attributes", function( assert
) {
271 var prop
, i
, l
, metadata
, elem
,
272 obj
, obj2
, check
, num
, num2
,
273 parseJSON
= JSON
.parse
,
274 div
= jQuery( "<div>" ),
275 child
= jQuery( "<div data-myobj='old data' data-ignored=\"DOM\" data-other='test' data-foo-42='boosh'></div>" ),
276 dummy
= jQuery( "<div data-myobj='old data' data-ignored=\"DOM\" data-other='test' data-foo-42='boosh'></div>" );
278 assert
.equal( div
.data( "attr" ), undefined, "Check for non-existing data-attr attribute" );
280 div
.attr( "data-attr", "exists" );
281 assert
.equal( div
.data( "attr" ), "exists", "Check for existing data-attr attribute" );
283 div
.attr( "data-attr", "exists2" );
284 assert
.equal( div
.data( "attr" ), "exists", "Check that updates to data- don't update .data()" );
286 div
.data( "attr", "internal" ).attr( "data-attr", "external" );
287 assert
.equal( div
.data( "attr" ), "internal", "Check for .data('attr') precedence (internal > external data-* attribute)" );
291 child
.appendTo( "#qunit-fixture" );
292 assert
.equal( child
.data( "myobj" ), "old data", "Value accessed from data-* attribute" );
293 assert
.equal( child
.data( "foo-42" ), "boosh", "camelCasing does not affect numbers (gh-1751)" );
295 child
.data( "myobj", "replaced" );
296 assert
.equal( child
.data( "myobj" ), "replaced", "Original data overwritten" );
298 child
.data( "ignored", "cache" );
299 assert
.equal( child
.data( "ignored" ), "cache", "Cached data used before DOM data-* fallback" );
303 check
= [ "myobj", "ignored", "other", "foo-42" ];
309 for ( i
= 0, l
= check
.length
; i
< l
; i
++ ) {
310 assert
.ok( obj
[ check
[ i
] ], "Make sure data- property exists when calling data-." );
311 assert
.ok( obj2
[ check
[ i
] ], "Make sure data- property exists when calling data-." );
314 for ( prop
in obj
) {
318 assert
.equal( num
, check
.length
, "Make sure that the right number of properties came through." );
320 /* eslint-disable-next-line no-unused-vars */
321 for ( prop
in obj2
) {
325 assert
.equal( num2
, check
.length
, "Make sure that the right number of properties came through." );
327 child
.attr( "data-other", "newvalue" );
329 assert
.equal( child
.data( "other" ), "test", "Make sure value was pulled in properly from a .data()." );
333 JSON
.parse = function() {
335 return parseJSON
.apply( this, arguments
);
339 .attr( "data-true", "true" )
340 .attr( "data-false", "false" )
341 .attr( "data-five", "5" )
342 .attr( "data-point", "5.5" )
343 .attr( "data-pointe", "5.5E3" )
344 .attr( "data-grande", "5.574E9" )
345 .attr( "data-hexadecimal", "0x42" )
346 .attr( "data-pointbad", "5..5" )
347 .attr( "data-pointbad2", "-." )
348 .attr( "data-bigassnum", "123456789123456789123456789" )
349 .attr( "data-badjson", "{123}" )
350 .attr( "data-badjson2", "[abc]" )
351 .attr( "data-notjson", " {}" )
352 .attr( "data-notjson2", "[] " )
353 .attr( "data-empty", "" )
354 .attr( "data-space", " " )
355 .attr( "data-null", "null" )
356 .attr( "data-string", "test" );
358 assert
.strictEqual( child
.data( "true" ), true, "Primitive true read from attribute" );
359 assert
.strictEqual( child
.data( "false" ), false, "Primitive false read from attribute" );
360 assert
.strictEqual( child
.data( "five" ), 5, "Integer read from attribute" );
361 assert
.strictEqual( child
.data( "point" ), 5.5, "Floating-point number read from attribute" );
362 assert
.strictEqual( child
.data( "pointe" ), "5.5E3",
363 "Exponential-notation number read from attribute as string" );
364 assert
.strictEqual( child
.data( "grande" ), "5.574E9",
365 "Big exponential-notation number read from attribute as string" );
366 assert
.strictEqual( child
.data( "hexadecimal" ), "0x42",
367 "Hexadecimal number read from attribute as string" );
368 assert
.strictEqual( child
.data( "pointbad" ), "5..5",
369 "Extra-point non-number read from attribute as string" );
370 assert
.strictEqual( child
.data( "pointbad2" ), "-.",
371 "No-digit non-number read from attribute as string" );
372 assert
.strictEqual( child
.data( "bigassnum" ), "123456789123456789123456789",
373 "Bad bigass number read from attribute as string" );
374 assert
.strictEqual( child
.data( "badjson" ), "{123}", "Bad JSON object read from attribute as string" );
375 assert
.strictEqual( child
.data( "badjson2" ), "[abc]", "Bad JSON array read from attribute as string" );
376 assert
.strictEqual( child
.data( "notjson" ), " {}",
377 "JSON object with leading non-JSON read from attribute as string" );
378 assert
.strictEqual( child
.data( "notjson2" ), "[] ",
379 "JSON array with trailing non-JSON read from attribute as string" );
380 assert
.strictEqual( child
.data( "empty" ), "", "Empty string read from attribute" );
381 assert
.strictEqual( child
.data( "space" ), " ", "Whitespace string read from attribute" );
382 assert
.strictEqual( child
.data( "null" ), null, "Primitive null read from attribute" );
383 assert
.strictEqual( child
.data( "string" ), "test", "Typical string read from attribute" );
384 assert
.equal( i
, 2, "Correct number of JSON parse attempts when reading from attributes" );
386 JSON
.parse
= parseJSON
;
389 // tests from metadata plugin
390 function testData( index
, elem
) {
393 assert
.equal( jQuery( elem
).data( "foo" ), "bar", "Check foo property" );
394 assert
.equal( jQuery( elem
).data( "bar" ), "baz", "Check baz property" );
397 assert
.equal( jQuery( elem
).data( "test" ), "bar", "Check test property" );
398 assert
.equal( jQuery( elem
).data( "bar" ), "baz", "Check bar property" );
401 assert
.equal( jQuery( elem
).data( "zoooo" ), "bar", "Check zoooo property" );
402 assert
.deepEqual( jQuery( elem
).data( "bar" ), { "test": "baz" }, "Check bar property" );
405 assert
.equal( jQuery( elem
).data( "number" ), true, "Check number property" );
406 assert
.deepEqual( jQuery( elem
).data( "stuff" ), [ 2, 8 ], "Check stuff property" );
409 assert
.ok( false, [ "Assertion failed on index ", index
, ", with data" ].join( "" ) );
413 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>";
414 elem
= jQuery( metadata
).appendTo( "#qunit-fixture" );
416 elem
.find( "li" ).each( testData
);
420 QUnit
.test( ".data(Object)", function( assert
) {
424 div
= jQuery( "<div></div>" );
426 div
.data( { "test": "in", "test2": "in2" } );
427 assert
.equal( div
.data( "test" ), "in", "Verify setting an object in data" );
428 assert
.equal( div
.data( "test2" ), "in2", "Verify setting an object in data" );
430 obj
= { test
: "unset" };
431 jqobj
= jQuery( obj
);
433 jqobj
.data( "test", "unset" );
434 jqobj
.data( { "test": "in", "test2": "in2" } );
435 assert
.equal( jQuery
.data( obj
).test
, "in", "Verify setting an object on an object extends the data object" );
436 assert
.equal( obj
.test2
, undefined, "Verify setting an object on an object does not extend the object" );
438 // manually clean up detached elements
442 QUnit
.test( "jQuery.removeData", function( assert
) {
446 div
= jQuery( "#foo" )[ 0 ];
447 jQuery
.data( div
, "test", "testing" );
448 jQuery
.removeData( div
, "test" );
449 assert
.equal( jQuery
.data( div
, "test" ), undefined, "Check removal of data" );
451 jQuery
.data( div
, "test2", "testing" );
452 jQuery
.removeData( div
);
453 assert
.ok( !jQuery
.data( div
, "test2" ), "Make sure that the data property no longer exists." );
454 assert
.ok( !div
[ jQuery
.expando
], "Make sure the expando no longer exists, as well." );
460 jQuery
.removeData( div
, "test3 test4" );
461 assert
.ok( !jQuery
.data( div
, "test3" ) || jQuery
.data( div
, "test4" ), "Multiple delete with spaces." );
467 jQuery
.removeData( div
, [ "test3", "test4" ] );
468 assert
.ok( !jQuery
.data( div
, "test3" ) || jQuery
.data( div
, "test4" ), "Multiple delete by array." );
471 "test3 test4": "testing",
474 jQuery
.removeData( div
, "test3 test4" );
475 assert
.ok( !jQuery
.data( div
, "test3 test4" ), "Multiple delete with spaces deleted key with exact name" );
476 assert
.ok( jQuery
.data( div
, "test3" ), "Left the partial matched key alone" );
479 jQuery
.data( obj
, "test", "testing" );
480 assert
.equal( jQuery( obj
).data( "test" ), "testing", "verify data on plain object" );
481 jQuery
.removeData( obj
, "test" );
482 assert
.equal( jQuery
.data( obj
, "test" ), undefined, "Check removal of data on plain object" );
484 jQuery
.data( window
, "BAD", true );
485 jQuery
.removeData( window
, "BAD" );
486 assert
.ok( !jQuery
.data( window
, "BAD" ), "Make sure that the value was not still set." );
489 QUnit
.test( ".removeData()", function( assert
) {
491 var div
= jQuery( "#foo" );
492 div
.data( "test", "testing" );
493 div
.removeData( "test" );
494 assert
.equal( div
.data( "test" ), undefined, "Check removal of data" );
496 div
.data( "test", "testing" );
497 div
.data( "test.foo", "testing2" );
498 div
.removeData( "test.bar" );
499 assert
.equal( div
.data( "test.foo" ), "testing2", "Make sure data is intact" );
500 assert
.equal( div
.data( "test" ), "testing", "Make sure data is intact" );
502 div
.removeData( "test" );
503 assert
.equal( div
.data( "test.foo" ), "testing2", "Make sure data is intact" );
504 assert
.equal( div
.data( "test" ), undefined, "Make sure data is intact" );
506 div
.removeData( "test.foo" );
507 assert
.equal( div
.data( "test.foo" ), undefined, "Make sure data is intact" );
510 QUnit
.test( "JSON serialization (trac-8108)", function( assert
) {
513 var obj
= { "foo": "bar" };
514 jQuery
.data( obj
, "hidden", true );
516 assert
.equal( JSON
.stringify( obj
), "{\"foo\":\"bar\"}", "Expando is hidden from JSON.stringify" );
519 QUnit
.test( ".data should follow html5 specification regarding camel casing", function( assert
) {
522 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>" )
523 .prependTo( "body" );
525 assert
.equal( div
.data().wTF
, "ftw", "Verify single letter data-* key" );
526 assert
.equal( div
.data().bigALittleA
, "bouncing-b", "Verify single letter mixed data-* key" );
528 assert
.equal( div
.data().foo
, "a", "Verify single word data-* key" );
529 assert
.equal( div
.data().fooBar
, "b", "Verify multiple word data-* key" );
530 assert
.equal( div
.data().fooBarBaz
, "c", "Verify multiple word data-* key" );
532 assert
.equal( div
.data( "foo" ), "a", "Verify single word data-* key" );
533 assert
.equal( div
.data( "fooBar" ), "b", "Verify multiple word data-* key" );
534 assert
.equal( div
.data( "fooBarBaz" ), "c", "Verify multiple word data-* key" );
536 div
.data( "foo-bar", "d" );
538 assert
.equal( div
.data( "fooBar" ), "d", "Verify updated data-* key" );
539 assert
.equal( div
.data( "foo-bar" ), "d", "Verify updated data-* key" );
541 assert
.equal( div
.data( "fooBar" ), "d", "Verify updated data-* key (fooBar)" );
542 assert
.equal( div
.data( "foo-bar" ), "d", "Verify updated data-* key (foo-bar)" );
547 QUnit
.test( ".data should not miss preset data-* w/ hyphenated property names", function( assert
) {
551 var div
= jQuery( "<div></div>", { id
: "hyphened" } ).appendTo( "#qunit-fixture" ),
553 "camelBar": "camelBar",
554 "hyphen-foo": "hyphen-foo"
559 jQuery
.each( test
, function( i
, k
) {
560 assert
.equal( div
.data( k
), k
, "data with property '" + k
+ "' was correctly found" );
564 QUnit
.test( "jQuery.data should not miss data-* w/ hyphenated property names trac-14047", function( assert
) {
568 var div
= jQuery( "<div></div>" );
570 div
.data( "foo-bar", "baz" );
572 assert
.equal( jQuery
.data( div
[ 0 ], "foo-bar" ), "baz", "data with property 'foo-bar' was correctly found" );
575 QUnit
.test( ".data should not miss attr() set data-* with hyphenated property names", function( assert
) {
580 a
= jQuery( "<div></div>" ).appendTo( "#qunit-fixture" );
582 a
.attr( "data-long-param", "test" );
583 a
.data( "long-param", { a
: 2 } );
585 assert
.deepEqual( a
.data( "long-param" ), { a
: 2 }, "data with property long-param was found, 1" );
587 b
= jQuery( "<div></div>" ).appendTo( "#qunit-fixture" );
589 b
.attr( "data-long-param", "test" );
590 b
.data( "long-param" );
591 b
.data( "long-param", { a
: 2 } );
593 assert
.deepEqual( b
.data( "long-param" ), { a
: 2 }, "data with property long-param was found, 2" );
596 QUnit
.test( ".data always sets data with the camelCased key (gh-2257)", function( assert
) {
599 var div
= jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
635 value
: "{ \"foo\": \"bar\" }"
639 jQuery
.each( datas
, function( key
, val
) {
640 div
.data( key
, val
.value
);
641 var allData
= div
.data();
642 assert
.equal( allData
[ key
], undefined, ".data does not store with hyphenated keys" );
643 assert
.equal( allData
[ val
.key
], val
.value
, ".data stores the camelCased key" );
647 QUnit
.test( ".data should not strip more than one hyphen when camelCasing (gh-2070)", function( assert
) {
649 var div
= jQuery( "<div data-nested-single='single' data-nested--double='double' data-nested---triple='triple'></div>" ).appendTo( "#qunit-fixture" ),
650 allData
= div
.data();
652 assert
.equal( allData
.nestedSingle
, "single", "Key is correctly camelCased" );
653 assert
.equal( allData
[ "nested-Double" ], "double", "Key with double hyphens is correctly camelCased" );
654 assert
.equal( allData
[ "nested--Triple" ], "triple", "Key with triple hyphens is correctly camelCased" );
657 QUnit
.test( ".data supports interoperable hyphenated/camelCase get/set of properties with arbitrary non-null|NaN|undefined values", function( assert
) {
659 var div
= jQuery( "<div></div>", { id
: "hyphened" } ).appendTo( "#qunit-fixture" ),
695 value
: "{ \"foo\": \"bar\" }"
711 // Vendor prefixes are not treated in a special way.
732 jQuery
.each( datas
, function( key
, val
) {
733 div
.data( key
, val
.value
);
735 assert
.deepEqual( div
.data( key
), val
.value
, "get: " + key
);
736 assert
.deepEqual( div
.data( val
.key
), val
.value
, "get: " + val
.key
);
740 QUnit
.test( ".data supports interoperable removal of hyphenated/camelCase properties", function( assert
) {
741 var div
= jQuery( "<div></div>", { id
: "hyphened" } ).appendTo( "#qunit-fixture" ),
742 rdashAlpha
= /-([a-z])/g,
744 "non-empty": "a string",
752 "some-json": "{ \"foo\": \"bar\" }"
757 function fcamelCase( all
, letter
) {
758 return letter
.toUpperCase();
761 jQuery
.each( datas
, function( key
, val
) {
762 div
.data( key
, val
);
764 assert
.deepEqual( div
.data( key
), val
, "get: " + key
);
766 div
.data( key
.replace( rdashAlpha
, fcamelCase
) ),
768 "get: " + key
.replace( rdashAlpha
, fcamelCase
)
771 div
.removeData( key
);
773 assert
.equal( div
.data( key
), undefined, "get: " + key
);
778 QUnit
.test( ".data supports interoperable removal of properties SET TWICE trac-13850", function( assert
) {
779 var div
= jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
781 "non-empty": "a string",
789 "some-json": "{ \"foo\": \"bar\" }"
794 jQuery
.each( datas
, function( key
, val
) {
795 div
.data( key
, val
);
796 div
.data( key
, val
);
798 div
.removeData( key
);
800 assert
.equal( div
.data( key
), undefined, "removal: " + key
);
804 QUnit
.test( ".removeData supports removal of hyphenated properties via array (trac-12786, gh-2257)", function( assert
) {
807 var div
, plain
, compare
;
809 div
= jQuery( "<div>" ).appendTo( "#qunit-fixture" );
810 plain
= jQuery( {} );
812 // Properties should always be camelCased
815 // From batch assignment .data({ "a-a": 1 })
818 // From property, value assignment .data( "b-b", 1 )
823 div
.data( { "a-a": 1 } ).data( "b-b", 1 );
824 plain
.data( { "a-a": 1 } ).data( "b-b", 1 );
826 assert
.deepEqual( div
.data(), compare
, "Data appears as expected. (div)" );
827 assert
.deepEqual( plain
.data(), compare
, "Data appears as expected. (plain)" );
829 div
.removeData( [ "a-a", "b-b" ] );
830 plain
.removeData( [ "a-a", "b-b" ] );
832 assert
.deepEqual( div
.data(), {}, "Data is empty. (div)" );
833 assert
.deepEqual( plain
.data(), {}, "Data is empty. (plain)" );
836 // Test originally by Moschel
837 QUnit
.test( ".removeData should not throw exceptions. (trac-10080)", function( assert
) {
838 var done
= assert
.async();
840 var frame
= jQuery( "#loadediframe" );
841 jQuery( frame
[ 0 ].contentWindow
).on( "unload", function() {
842 assert
.ok( true, "called unload" );
846 // change the url to trigger unload
847 frame
.attr( "src", baseURL
+ "iframe.html?param=true" );
850 QUnit
.test( ".data only checks element attributes once. trac-8909", function( assert
) {
856 element
= jQuery( "<div data-test='testing'>" ),
859 // set an attribute using attr to ensure it
860 node
.setAttribute( "data-test2", "testing" );
861 assert
.deepEqual( element
.data(), testing
, "Sanity Check" );
863 node
.setAttribute( "data-test3", "testing" );
864 assert
.deepEqual( element
.data(), testing
, "The data didn't change even though the data-* attrs did" );
866 // clean up data cache
870 QUnit
.test( "data-* with JSON value can have newlines", function( assert
) {
873 var x
= jQuery( "<div data-some='{\n\"foo\":\n\t\"bar\"\n}'></div>" );
874 assert
.equal( x
.data( "some" ).foo
, "bar", "got a JSON data- attribute with spaces" );
878 QUnit
.test( ".data doesn't throw when calling selection is empty. trac-13551", function( assert
) {
882 jQuery( null ).data( "prop" );
883 assert
.ok( true, "jQuery(null).data('prop') does not throw" );
885 assert
.ok( false, e
.message
);
889 QUnit
.test( "acceptData", function( assert
) {
892 var flash
, pdf
, form
;
894 assert
.equal( jQuery( document
).data( "test", 42 ).data( "test" ), 42, "document" );
895 assert
.equal( jQuery( document
.documentElement
).data( "test", 42 ).data( "test" ), 42, "documentElement" );
896 assert
.equal( jQuery( {} ).data( "test", 42 ).data( "test" ), 42, "object" );
897 assert
.equal( jQuery( document
.createElement( "embed" ) ).data( "test", 42 ).data( "test" ), 42, "embed" );
899 flash
= document
.createElement( "object" );
900 flash
.setAttribute( "classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" );
901 assert
.equal( jQuery( flash
).data( "test", 42 ).data( "test" ), 42, "flash" );
903 pdf
= document
.createElement( "object" );
904 pdf
.setAttribute( "classid", "clsid:CA8A9780-280D-11CF-A24D-444553540000" );
905 assert
.equal( jQuery( pdf
).data( "test", 42 ).data( "test" ), 42, "pdf" );
907 assert
.strictEqual( jQuery( document
.createComment( "" ) ).data( "test", 42 ).data( "test" ), undefined, "comment" );
908 assert
.strictEqual( jQuery( document
.createTextNode( "" ) ).data( "test", 42 ).data( "test" ), undefined, "text" );
909 assert
.strictEqual( jQuery( document
.createDocumentFragment() ).data( "test", 42 ).data( "test" ), undefined, "documentFragment" );
911 form
= jQuery( "#form" ).append( "<input id='nodeType'/><input id='nodeName'/>" )[ 0 ];
912 assert
.equal( jQuery( form
) .data( "test", 42 ).data( "test" ), 42, "form with aliased DOM properties" );
915 QUnit
.test( "Check proper data removal of non-element descendants nodes (trac-8335)", function( assert
) {
918 var div
= jQuery( "<div>text</div>" ),
919 text
= div
.contents();
921 text
.data( "test", "test" ); // This should be a noop.
924 assert
.ok( !text
.data( "test" ), "Be sure data is not stored in non-element" );
928 "enumerate data attrs on body (trac-14894)",
929 "data/dataAttrs.html",
930 function( assert
, jQuery
, window
, document
, result
) {
932 assert
.equal( result
, "ok", "enumeration of data- attrs on body" );
936 QUnit
.test( "Check that the expando is removed when there's no more data", function( assert
) {
940 div
= jQuery( "<div></div>" );
941 div
.data( "some", "data" );
942 assert
.equal( div
.data( "some" ), "data", "Data is added" );
943 div
.removeData( "some" );
945 // Make sure the expando is gone
946 for ( key
in div
[ 0 ] ) {
947 if ( /^jQuery/.test( key
) ) {
948 assert
.strictEqual( div
[ 0 ][ key
], undefined, "Expando was not removed when there was no more data" );
953 QUnit
.test( "Check that the expando is removed when there's no more data on non-nodes", function( assert
) {
957 obj
= jQuery( { key
: 42 } );
958 obj
.data( "some", "data" );
959 assert
.equal( obj
.data( "some" ), "data", "Data is added" );
960 obj
.removeData( "some" );
962 // Make sure the expando is gone
963 for ( key
in obj
[ 0 ] ) {
964 if ( /^jQuery/.test( key
) ) {
965 assert
.ok( false, "Expando was not removed when there was no more data" );
970 QUnit
.test( ".data(prop) does not create expando", function( assert
) {
974 div
= jQuery( "<div></div>" );
977 assert
.equal( jQuery
.hasData( div
[ 0 ] ), false, "No data exists after access" );
979 // Make sure no expando has been added
980 for ( key
in div
[ 0 ] ) {
981 if ( /^jQuery/.test( key
) ) {
982 assert
.ok( false, "Expando was created on access" );
987 QUnit
.test( "keys matching Object.prototype properties (gh-3256)", function( assert
) {
990 var div
= jQuery( "<div></div>" );
992 assert
.strictEqual( div
.data( "hasOwnProperty" ), undefined,
993 "hasOwnProperty not matched (before forced data creation)" );
995 // Force the creation of a data object for this element.
996 div
.data( { foo
: "bar" } );
998 assert
.strictEqual( div
.data( "hasOwnProperty" ), undefined,
999 "hasOwnProperty not matched (after forced data creation)" );