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
);
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 for ( prop
in obj2
) {
324 assert
.equal( num2
, check
.length
, "Make sure that the right number of properties came through." );
326 child
.attr( "data-other", "newvalue" );
328 assert
.equal( child
.data( "other" ), "test", "Make sure value was pulled in properly from a .data()." );
332 JSON
.parse = function() {
334 return parseJSON
.apply( this, arguments
);
338 .attr( "data-true", "true" )
339 .attr( "data-false", "false" )
340 .attr( "data-five", "5" )
341 .attr( "data-point", "5.5" )
342 .attr( "data-pointe", "5.5E3" )
343 .attr( "data-grande", "5.574E9" )
344 .attr( "data-hexadecimal", "0x42" )
345 .attr( "data-pointbad", "5..5" )
346 .attr( "data-pointbad2", "-." )
347 .attr( "data-bigassnum", "123456789123456789123456789" )
348 .attr( "data-badjson", "{123}" )
349 .attr( "data-badjson2", "[abc]" )
350 .attr( "data-notjson", " {}" )
351 .attr( "data-notjson2", "[] " )
352 .attr( "data-empty", "" )
353 .attr( "data-space", " " )
354 .attr( "data-null", "null" )
355 .attr( "data-string", "test" );
357 assert
.strictEqual( child
.data( "true" ), true, "Primitive true read from attribute" );
358 assert
.strictEqual( child
.data( "false" ), false, "Primitive false read from attribute" );
359 assert
.strictEqual( child
.data( "five" ), 5, "Integer read from attribute" );
360 assert
.strictEqual( child
.data( "point" ), 5.5, "Floating-point number read from attribute" );
361 assert
.strictEqual( child
.data( "pointe" ), "5.5E3",
362 "Exponential-notation number read from attribute as string" );
363 assert
.strictEqual( child
.data( "grande" ), "5.574E9",
364 "Big exponential-notation number read from attribute as string" );
365 assert
.strictEqual( child
.data( "hexadecimal" ), "0x42",
366 "Hexadecimal number read from attribute as string" );
367 assert
.strictEqual( child
.data( "pointbad" ), "5..5",
368 "Extra-point non-number read from attribute as string" );
369 assert
.strictEqual( child
.data( "pointbad2" ), "-.",
370 "No-digit non-number read from attribute as string" );
371 assert
.strictEqual( child
.data( "bigassnum" ), "123456789123456789123456789",
372 "Bad bigass number read from attribute as string" );
373 assert
.strictEqual( child
.data( "badjson" ), "{123}", "Bad JSON object read from attribute as string" );
374 assert
.strictEqual( child
.data( "badjson2" ), "[abc]", "Bad JSON array read from attribute as string" );
375 assert
.strictEqual( child
.data( "notjson" ), " {}",
376 "JSON object with leading non-JSON read from attribute as string" );
377 assert
.strictEqual( child
.data( "notjson2" ), "[] ",
378 "JSON array with trailing non-JSON read from attribute as string" );
379 assert
.strictEqual( child
.data( "empty" ), "", "Empty string read from attribute" );
380 assert
.strictEqual( child
.data( "space" ), " ", "Whitespace string read from attribute" );
381 assert
.strictEqual( child
.data( "null" ), null, "Primitive null read from attribute" );
382 assert
.strictEqual( child
.data( "string" ), "test", "Typical string read from attribute" );
383 assert
.equal( i
, 2, "Correct number of JSON parse attempts when reading from attributes" );
385 JSON
.parse
= parseJSON
;
388 // tests from metadata plugin
389 function testData( index
, elem
) {
392 assert
.equal( jQuery( elem
).data( "foo" ), "bar", "Check foo property" );
393 assert
.equal( jQuery( elem
).data( "bar" ), "baz", "Check baz property" );
396 assert
.equal( jQuery( elem
).data( "test" ), "bar", "Check test property" );
397 assert
.equal( jQuery( elem
).data( "bar" ), "baz", "Check bar property" );
400 assert
.equal( jQuery( elem
).data( "zoooo" ), "bar", "Check zoooo property" );
401 assert
.deepEqual( jQuery( elem
).data( "bar" ), { "test":"baz" }, "Check bar property" );
404 assert
.equal( jQuery( elem
).data( "number" ), true, "Check number property" );
405 assert
.deepEqual( jQuery( elem
).data( "stuff" ), [ 2, 8 ], "Check stuff property" );
408 assert
.ok( false, [ "Assertion failed on index ", index
, ", with data" ].join( "" ) );
412 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>";
413 elem
= jQuery( metadata
).appendTo( "#qunit-fixture" );
415 elem
.find( "li" ).each( testData
);
419 QUnit
.test( ".data(Object)", function( assert
) {
423 div
= jQuery( "<div></div>" );
425 div
.data( { "test": "in", "test2": "in2" } );
426 assert
.equal( div
.data( "test" ), "in", "Verify setting an object in data" );
427 assert
.equal( div
.data( "test2" ), "in2", "Verify setting an object in data" );
429 obj
= { test
:"unset" };
430 jqobj
= jQuery( obj
);
432 jqobj
.data( "test", "unset" );
433 jqobj
.data( { "test": "in", "test2": "in2" } );
434 assert
.equal( jQuery
.data( obj
)[ "test" ], "in", "Verify setting an object on an object extends the data object" );
435 assert
.equal( obj
[ "test2" ], undefined, "Verify setting an object on an object does not extend the object" );
437 // manually clean up detached elements
441 QUnit
.test( "jQuery.removeData", function( assert
) {
445 div
= jQuery( "#foo" )[ 0 ];
446 jQuery
.data( div
, "test", "testing" );
447 jQuery
.removeData( div
, "test" );
448 assert
.equal( jQuery
.data( div
, "test" ), undefined, "Check removal of data" );
450 jQuery
.data( div
, "test2", "testing" );
451 jQuery
.removeData( div
);
452 assert
.ok( !jQuery
.data( div
, "test2" ), "Make sure that the data property no longer exists." );
453 assert
.ok( !div
[ jQuery
.expando
], "Make sure the expando no longer exists, as well." );
459 jQuery
.removeData( div
, "test3 test4" );
460 assert
.ok( !jQuery
.data( div
, "test3" ) || jQuery
.data( div
, "test4" ), "Multiple delete with spaces." );
466 jQuery
.removeData( div
, [ "test3", "test4" ] );
467 assert
.ok( !jQuery
.data( div
, "test3" ) || jQuery
.data( div
, "test4" ), "Multiple delete by array." );
470 "test3 test4": "testing",
473 jQuery
.removeData( div
, "test3 test4" );
474 assert
.ok( !jQuery
.data( div
, "test3 test4" ), "Multiple delete with spaces deleted key with exact name" );
475 assert
.ok( jQuery
.data( div
, "test3" ), "Left the partial matched key alone" );
478 jQuery
.data( obj
, "test", "testing" );
479 assert
.equal( jQuery( obj
).data( "test" ), "testing", "verify data on plain object" );
480 jQuery
.removeData( obj
, "test" );
481 assert
.equal( jQuery
.data( obj
, "test" ), undefined, "Check removal of data on plain object" );
483 jQuery
.data( window
, "BAD", true );
484 jQuery
.removeData( window
, "BAD" );
485 assert
.ok( !jQuery
.data( window
, "BAD" ), "Make sure that the value was not still set." );
488 QUnit
.test( ".removeData()", function( assert
) {
490 var div
= jQuery( "#foo" );
491 div
.data( "test", "testing" );
492 div
.removeData( "test" );
493 assert
.equal( div
.data( "test" ), undefined, "Check removal of data" );
495 div
.data( "test", "testing" );
496 div
.data( "test.foo", "testing2" );
497 div
.removeData( "test.bar" );
498 assert
.equal( div
.data( "test.foo" ), "testing2", "Make sure data is intact" );
499 assert
.equal( div
.data( "test" ), "testing", "Make sure data is intact" );
501 div
.removeData( "test" );
502 assert
.equal( div
.data( "test.foo" ), "testing2", "Make sure data is intact" );
503 assert
.equal( div
.data( "test" ), undefined, "Make sure data is intact" );
505 div
.removeData( "test.foo" );
506 assert
.equal( div
.data( "test.foo" ), undefined, "Make sure data is intact" );
509 QUnit
.test( "JSON serialization (trac-8108)", function( assert
) {
512 var obj
= { "foo": "bar" };
513 jQuery
.data( obj
, "hidden", true );
515 assert
.equal( JSON
.stringify( obj
), "{\"foo\":\"bar\"}", "Expando is hidden from JSON.stringify" );
518 QUnit
.test( ".data should follow html5 specification regarding camel casing", function( assert
) {
521 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>" )
522 .prependTo( "body" );
524 assert
.equal( div
.data()[ "wTF" ], "ftw", "Verify single letter data-* key" );
525 assert
.equal( div
.data()[ "bigALittleA" ], "bouncing-b", "Verify single letter mixed data-* key" );
527 assert
.equal( div
.data()[ "foo" ], "a", "Verify single word data-* key" );
528 assert
.equal( div
.data()[ "fooBar" ], "b", "Verify multiple word data-* key" );
529 assert
.equal( div
.data()[ "fooBarBaz" ], "c", "Verify multiple word data-* key" );
531 assert
.equal( div
.data( "foo" ), "a", "Verify single word data-* key" );
532 assert
.equal( div
.data( "fooBar" ), "b", "Verify multiple word data-* key" );
533 assert
.equal( div
.data( "fooBarBaz" ), "c", "Verify multiple word data-* key" );
535 div
.data( "foo-bar", "d" );
537 assert
.equal( div
.data( "fooBar" ), "d", "Verify updated data-* key" );
538 assert
.equal( div
.data( "foo-bar" ), "d", "Verify updated data-* key" );
540 assert
.equal( div
.data( "fooBar" ), "d", "Verify updated data-* key (fooBar)" );
541 assert
.equal( div
.data( "foo-bar" ), "d", "Verify updated data-* key (foo-bar)" );
546 QUnit
.test( ".data should not miss preset data-* w/ hyphenated property names", function( assert
) {
550 var div
= jQuery( "<div></div>", { id
: "hyphened" } ).appendTo( "#qunit-fixture" ),
552 "camelBar": "camelBar",
553 "hyphen-foo": "hyphen-foo"
558 jQuery
.each( test
, function( i
, k
) {
559 assert
.equal( div
.data( k
), k
, "data with property '" + k
+ "' was correctly found" );
563 QUnit
.test( "jQuery.data should not miss data-* w/ hyphenated property names trac-14047", function( assert
) {
567 var div
= jQuery( "<div></div>" );
569 div
.data( "foo-bar", "baz" );
571 assert
.equal( jQuery
.data( div
[ 0 ], "foo-bar" ), "baz", "data with property 'foo-bar' was correctly found" );
574 QUnit
.test( ".data should not miss attr() set data-* with hyphenated property names", function( assert
) {
579 a
= jQuery( "<div></div>" ).appendTo( "#qunit-fixture" );
581 a
.attr( "data-long-param", "test" );
582 a
.data( "long-param", { a
: 2 } );
584 assert
.deepEqual( a
.data( "long-param" ), { a
: 2 }, "data with property long-param was found, 1" );
586 b
= jQuery( "<div></div>" ).appendTo( "#qunit-fixture" );
588 b
.attr( "data-long-param", "test" );
589 b
.data( "long-param" );
590 b
.data( "long-param", { a
: 2 } );
592 assert
.deepEqual( b
.data( "long-param" ), { a
: 2 }, "data with property long-param was found, 2" );
595 QUnit
.test( ".data always sets data with the camelCased key (gh-2257)", function( assert
) {
598 var div
= jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
634 value
: "{ \"foo\": \"bar\" }"
638 jQuery
.each( datas
, function( key
, val
) {
639 div
.data( key
, val
.value
);
640 var allData
= div
.data();
641 assert
.equal( allData
[ key
], undefined, ".data does not store with hyphenated keys" );
642 assert
.equal( allData
[ val
.key
], val
.value
, ".data stores the camelCased key" );
646 QUnit
.test( ".data should not strip more than one hyphen when camelCasing (gh-2070)", function( assert
) {
648 var div
= jQuery( "<div data-nested-single='single' data-nested--double='double' data-nested---triple='triple'></div>" ).appendTo( "#qunit-fixture" ),
649 allData
= div
.data();
651 assert
.equal( allData
.nestedSingle
, "single", "Key is correctly camelCased" );
652 assert
.equal( allData
[ "nested-Double" ], "double", "Key with double hyphens is correctly camelCased" );
653 assert
.equal( allData
[ "nested--Triple" ], "triple", "Key with triple hyphens is correctly camelCased" );
656 QUnit
.test( ".data supports interoperable hyphenated/camelCase get/set of properties with arbitrary non-null|NaN|undefined values", function( assert
) {
658 var div
= jQuery( "<div></div>", { id
: "hyphened" } ).appendTo( "#qunit-fixture" ),
694 value
: "{ \"foo\": \"bar\" }"
710 // Vendor prefixes are not treated in a special way.
731 jQuery
.each( datas
, function( key
, val
) {
732 div
.data( key
, val
.value
);
734 assert
.deepEqual( div
.data( key
), val
.value
, "get: " + key
);
735 assert
.deepEqual( div
.data( val
.key
), val
.value
, "get: " + val
.key
);
739 QUnit
.test( ".data supports interoperable removal of hyphenated/camelCase properties", function( assert
) {
740 var div
= jQuery( "<div></div>", { id
: "hyphened" } ).appendTo( "#qunit-fixture" ),
741 rdashAlpha
= /-([a-z])/g,
743 "non-empty": "a string",
751 "some-json": "{ \"foo\": \"bar\" }"
756 function fcamelCase( all
, letter
) {
757 return letter
.toUpperCase();
760 jQuery
.each( datas
, function( key
, val
) {
761 div
.data( key
, val
);
763 assert
.deepEqual( div
.data( key
), val
, "get: " + key
);
765 div
.data( key
.replace( rdashAlpha
, fcamelCase
) ),
767 "get: " + key
.replace( rdashAlpha
, fcamelCase
)
770 div
.removeData( key
);
772 assert
.equal( div
.data( key
), undefined, "get: " + key
);
777 QUnit
.test( ".data supports interoperable removal of properties SET TWICE trac-13850", function( assert
) {
778 var div
= jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
780 "non-empty": "a string",
788 "some-json": "{ \"foo\": \"bar\" }"
793 jQuery
.each( datas
, function( key
, val
) {
794 div
.data( key
, val
);
795 div
.data( key
, val
);
797 div
.removeData( key
);
799 assert
.equal( div
.data( key
), undefined, "removal: " + key
);
803 QUnit
.test( ".removeData supports removal of hyphenated properties via array (trac-12786, gh-2257)", function( assert
) {
806 var div
, plain
, compare
;
808 div
= jQuery( "<div>" ).appendTo( "#qunit-fixture" );
809 plain
= jQuery( {} );
811 // Properties should always be camelCased
814 // From batch assignment .data({ "a-a": 1 })
817 // From property, value assignment .data( "b-b", 1 )
822 div
.data( { "a-a": 1 } ).data( "b-b", 1 );
823 plain
.data( { "a-a": 1 } ).data( "b-b", 1 );
825 assert
.deepEqual( div
.data(), compare
, "Data appears as expected. (div)" );
826 assert
.deepEqual( plain
.data(), compare
, "Data appears as expected. (plain)" );
828 div
.removeData( [ "a-a", "b-b" ] );
829 plain
.removeData( [ "a-a", "b-b" ] );
831 assert
.deepEqual( div
.data(), {}, "Data is empty. (div)" );
832 assert
.deepEqual( plain
.data(), {}, "Data is empty. (plain)" );
835 // Test originally by Moschel
836 QUnit
.test( ".removeData should not throw exceptions. (trac-10080)", function( assert
) {
837 var done
= assert
.async();
839 var frame
= jQuery( "#loadediframe" );
840 jQuery( frame
[ 0 ].contentWindow
).on( "unload", function() {
841 assert
.ok( true, "called unload" );
845 // change the url to trigger unload
846 frame
.attr( "src", baseURL
+ "iframe.html?param=true" );
849 QUnit
.test( ".data only checks element attributes once. trac-8909", function( assert
) {
855 element
= jQuery( "<div data-test='testing'>" ),
858 // set an attribute using attr to ensure it
859 node
.setAttribute( "data-test2", "testing" );
860 assert
.deepEqual( element
.data(), testing
, "Sanity Check" );
862 node
.setAttribute( "data-test3", "testing" );
863 assert
.deepEqual( element
.data(), testing
, "The data didn't change even though the data-* attrs did" );
865 // clean up data cache
869 QUnit
.test( "data-* with JSON value can have newlines", function( assert
) {
872 var x
= jQuery( "<div data-some='{\n\"foo\":\n\t\"bar\"\n}'></div>" );
873 assert
.equal( x
.data( "some" ).foo
, "bar", "got a JSON data- attribute with spaces" );
877 QUnit
.test( ".data doesn't throw when calling selection is empty. trac-13551", function( assert
) {
881 jQuery( null ).data( "prop" );
882 assert
.ok( true, "jQuery(null).data('prop') does not throw" );
884 assert
.ok( false, e
.message
);
888 QUnit
.test( "acceptData", function( assert
) {
891 var flash
, pdf
, form
;
893 assert
.equal( jQuery( document
).data( "test", 42 ).data( "test" ), 42, "document" );
894 assert
.equal( jQuery( document
.documentElement
).data( "test", 42 ).data( "test" ), 42, "documentElement" );
895 assert
.equal( jQuery( {} ).data( "test", 42 ).data( "test" ), 42, "object" );
896 assert
.equal( jQuery( document
.createElement( "embed" ) ).data( "test", 42 ).data( "test" ), 42, "embed" );
898 flash
= document
.createElement( "object" );
899 flash
.setAttribute( "classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" );
900 assert
.equal( jQuery( flash
).data( "test", 42 ).data( "test" ), 42, "flash" );
902 pdf
= document
.createElement( "object" );
903 pdf
.setAttribute( "classid", "clsid:CA8A9780-280D-11CF-A24D-444553540000" );
904 assert
.equal( jQuery( pdf
).data( "test", 42 ).data( "test" ), 42, "pdf" );
906 assert
.strictEqual( jQuery( document
.createComment( "" ) ).data( "test", 42 ).data( "test" ), undefined, "comment" );
907 assert
.strictEqual( jQuery( document
.createTextNode( "" ) ).data( "test", 42 ).data( "test" ), undefined, "text" );
908 assert
.strictEqual( jQuery( document
.createDocumentFragment() ).data( "test", 42 ).data( "test" ), undefined, "documentFragment" );
910 form
= jQuery( "#form" ).append( "<input id='nodeType'/><input id='nodeName'/>" )[ 0 ];
911 assert
.equal( jQuery( form
) .data( "test", 42 ).data( "test" ), 42, "form with aliased DOM properties" );
914 QUnit
.test( "Check proper data removal of non-element descendants nodes (trac-8335)", function( assert
) {
917 var div
= jQuery( "<div>text</div>" ),
918 text
= div
.contents();
920 text
.data( "test", "test" ); // This should be a noop.
923 assert
.ok( !text
.data( "test" ), "Be sure data is not stored in non-element" );
927 "enumerate data attrs on body (trac-14894)",
928 "data/dataAttrs.html",
929 function( assert
, jQuery
, window
, document
, result
) {
931 assert
.equal( result
, "ok", "enumeration of data- attrs on body" );
935 QUnit
.test( "Check that the expando is removed when there's no more data", function( assert
) {
939 div
= jQuery( "<div></div>" );
940 div
.data( "some", "data" );
941 assert
.equal( div
.data( "some" ), "data", "Data is added" );
942 div
.removeData( "some" );
944 // Make sure the expando is gone
945 for ( key
in div
[ 0 ] ) {
946 if ( /^jQuery/.test( key
) ) {
947 assert
.strictEqual( div
[ 0 ][ key
], undefined, "Expando was not removed when there was no more data" );
952 QUnit
.test( "Check that the expando is removed when there's no more data on non-nodes", function( assert
) {
956 obj
= jQuery( { key
: 42 } );
957 obj
.data( "some", "data" );
958 assert
.equal( obj
.data( "some" ), "data", "Data is added" );
959 obj
.removeData( "some" );
961 // Make sure the expando is gone
962 for ( key
in obj
[ 0 ] ) {
963 if ( /^jQuery/.test( key
) ) {
964 assert
.ok( false, "Expando was not removed when there was no more data" );
969 QUnit
.test( ".data(prop) does not create expando", function( assert
) {
973 div
= jQuery( "<div></div>" );
976 assert
.equal( jQuery
.hasData( div
[ 0 ] ), false, "No data exists after access" );
978 // Make sure no expando has been added
979 for ( key
in div
[ 0 ] ) {
980 if ( /^jQuery/.test( key
) ) {
981 assert
.ok( false, "Expando was created on access" );
986 QUnit
.test( "keys matching Object.prototype properties (gh-3256)", function( assert
) {
989 var div
= jQuery( "<div></div>" );
991 assert
.strictEqual( div
.data( "hasOwnProperty" ), undefined,
992 "hasOwnProperty not matched (before forced data creation)" );
994 // Force the creation of a data object for this element.
995 div
.data( { foo
: "bar" } );
997 assert
.strictEqual( div
.data( "hasOwnProperty" ), undefined,
998 "hasOwnProperty not matched (after forced data creation)" );