Build: Bump the github-actions group across 1 directory with 2 updates
[jquery.git] / test / unit / data.js
blobd4a0f7b12fdaf8ab3d78b3dd44e83225aeeb1701
1 QUnit.module( "data", { afterEach: moduleTeardown } );
3 QUnit.test( "expando", function( assert ) {
4 assert.expect( 1 );
6 assert.equal( jQuery.expando !== undefined, true, "jQuery is exposing the expando" );
7 } );
9 QUnit.test( "jQuery.data & removeData, expected returns", function( assert ) {
10 assert.expect( 4 );
11 var elem = document.body;
13 assert.equal(
14 jQuery.data( elem, "hello", "world" ), "world",
15 "jQuery.data( elem, key, value ) returns value"
17 assert.equal(
18 jQuery.data( elem, "hello" ), "world",
19 "jQuery.data( elem, key ) returns value"
21 assert.deepEqual(
22 jQuery.data( elem, { goodnight: "moon" } ), { goodnight: "moon" },
23 "jQuery.data( elem, obj ) returns obj"
25 assert.equal(
26 jQuery.removeData( elem, "hello" ), undefined,
27 "jQuery.removeData( elem, key, value ) returns undefined"
30 } );
32 QUnit.test( "jQuery._data & _removeData, expected returns", function( assert ) {
33 assert.expect( 4 );
34 var elem = document.body;
36 assert.equal(
37 jQuery._data( elem, "hello", "world" ), "world",
38 "jQuery._data( elem, key, value ) returns value"
40 assert.equal(
41 jQuery._data( elem, "hello" ), "world",
42 "jQuery._data( elem, key ) returns value"
44 assert.deepEqual(
45 jQuery._data( elem, { goodnight: "moon" } ), { goodnight: "moon" },
46 "jQuery._data( elem, obj ) returns obj"
48 assert.equal(
49 jQuery._removeData( elem, "hello" ), undefined,
50 "jQuery._removeData( elem, key, value ) returns undefined"
52 } );
54 QUnit.test( "jQuery.hasData no side effects", function( assert ) {
55 assert.expect( 1 );
56 var obj = {};
58 jQuery.hasData( obj );
60 assert.equal( Object.getOwnPropertyNames( obj ).length, 0,
61 "No data expandos where added when calling jQuery.hasData(o)"
63 } );
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 ) {
130 assert.expect( 25 );
132 var div = document.createElement( "div" );
134 dataTests( div, assert );
135 } );
137 QUnit.test( "jQuery.data({})", function( assert ) {
138 assert.expect( 25 );
140 dataTests( {}, assert );
141 } );
143 QUnit.test( "jQuery.data(window)", function( assert ) {
144 assert.expect( 25 );
146 // remove bound handlers from window object to stop potential false positives caused by fix for trac-5280 in
147 // transports/xhr.js
148 jQuery( window ).off( "unload" );
150 dataTests( window, assert );
151 } );
153 QUnit.test( "jQuery.data(document)", function( assert ) {
154 assert.expect( 25 );
156 dataTests( document, assert );
157 } );
159 QUnit.test( "jQuery.data(<embed>)", function( assert ) {
160 assert.expect( 25 );
162 dataTests( document.createElement( "embed" ), assert );
163 } );
165 QUnit.test( "jQuery.data(object/flash)", function( assert ) {
166 assert.expect( 25 );
168 var flash = document.createElement( "object" );
169 flash.setAttribute( "classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" );
171 dataTests( flash, assert );
172 } );
174 // attempting to access the data of an undefined jQuery element should be undefined
175 QUnit.test( "jQuery().data() === undefined (trac-14101)", function( assert ) {
176 assert.expect( 2 );
178 assert.strictEqual( jQuery().data(), undefined );
179 assert.strictEqual( jQuery().data( "key" ), undefined );
180 } );
182 QUnit.test( ".data()", function( assert ) {
183 assert.expect( 5 );
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)" );
205 } );
207 function testDataTypes( $obj, assert ) {
208 jQuery.each( {
209 "null": null,
210 "true": true,
211 "false": false,
212 "zero": 0,
213 "one": 1,
214 "empty string": "",
215 "empty array": [],
216 "array": [ 1 ],
217 "empty object": {},
218 "object": { foo: "bar" },
219 "date": new Date(),
220 "regex": /test/,
221 "function": function() {}
222 }, function( type, value ) {
223 assert.strictEqual( $obj.data( "test", value ).data( "test" ), value, "Data set to " + type );
224 } );
227 QUnit.test( "jQuery(Element).data(String, Object).data(String)", function( assert ) {
228 assert.expect( 18 );
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 );
239 parent.remove();
240 } );
242 QUnit.test( "jQuery(plain Object).data(String, Object).data(String)", function( assert ) {
243 assert.expect( 16 );
245 // trac-3748
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 );
251 // Clean up
252 $obj.removeData();
253 assert.deepEqual( $obj[ 0 ], { exists: true }, "removeData does not clear the object" );
254 } );
256 QUnit.test( ".data(object) does not retain references. trac-13815", function( assert ) {
257 assert.expect( 2 );
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" );
266 } );
268 QUnit.test( "data-* attributes", function( assert ) {
269 assert.expect( 46 );
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)" );
289 div.remove();
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" );
301 obj = child.data();
302 obj2 = dummy.data();
303 check = [ "myobj", "ignored", "other", "foo-42" ];
304 num = 0;
305 num2 = 0;
307 dummy.remove();
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 ) {
315 num++;
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 ) {
322 num2++;
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()." );
331 // attribute parsing
332 i = 0;
333 JSON.parse = function() {
334 i++;
335 return parseJSON.apply( this, arguments );
338 child
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;
387 child.remove();
389 // tests from metadata plugin
390 function testData( index, elem ) {
391 switch ( index ) {
392 case 0:
393 assert.equal( jQuery( elem ).data( "foo" ), "bar", "Check foo property" );
394 assert.equal( jQuery( elem ).data( "bar" ), "baz", "Check baz property" );
395 break;
396 case 1:
397 assert.equal( jQuery( elem ).data( "test" ), "bar", "Check test property" );
398 assert.equal( jQuery( elem ).data( "bar" ), "baz", "Check bar property" );
399 break;
400 case 2:
401 assert.equal( jQuery( elem ).data( "zoooo" ), "bar", "Check zoooo property" );
402 assert.deepEqual( jQuery( elem ).data( "bar" ), { "test": "baz" }, "Check bar property" );
403 break;
404 case 3:
405 assert.equal( jQuery( elem ).data( "number" ), true, "Check number property" );
406 assert.deepEqual( jQuery( elem ).data( "stuff" ), [ 2, 8 ], "Check stuff property" );
407 break;
408 default:
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 );
417 elem.remove();
418 } );
420 QUnit.test( ".data(Object)", function( assert ) {
421 assert.expect( 4 );
423 var obj, jqobj,
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
439 div.remove();
440 } );
442 QUnit.test( "jQuery.removeData", function( assert ) {
443 assert.expect( 10 );
445 var obj,
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." );
456 jQuery.data( div, {
457 test3: "testing",
458 test4: "testing"
459 } );
460 jQuery.removeData( div, "test3 test4" );
461 assert.ok( !jQuery.data( div, "test3" ) || jQuery.data( div, "test4" ), "Multiple delete with spaces." );
463 jQuery.data( div, {
464 test3: "testing",
465 test4: "testing"
466 } );
467 jQuery.removeData( div, [ "test3", "test4" ] );
468 assert.ok( !jQuery.data( div, "test3" ) || jQuery.data( div, "test4" ), "Multiple delete by array." );
470 jQuery.data( div, {
471 "test3 test4": "testing",
472 "test3": "testing"
473 } );
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" );
478 obj = {};
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." );
487 } );
489 QUnit.test( ".removeData()", function( assert ) {
490 assert.expect( 6 );
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" );
508 } );
510 QUnit.test( "JSON serialization (trac-8108)", function( assert ) {
511 assert.expect( 1 );
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" );
517 } );
519 QUnit.test( ".data should follow html5 specification regarding camel casing", function( assert ) {
520 assert.expect( 12 );
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)" );
544 div.remove();
545 } );
547 QUnit.test( ".data should not miss preset data-* w/ hyphenated property names", function( assert ) {
549 assert.expect( 2 );
551 var div = jQuery( "<div></div>", { id: "hyphened" } ).appendTo( "#qunit-fixture" ),
552 test = {
553 "camelBar": "camelBar",
554 "hyphen-foo": "hyphen-foo"
557 div.data( test );
559 jQuery.each( test, function( i, k ) {
560 assert.equal( div.data( k ), k, "data with property '" + k + "' was correctly found" );
561 } );
562 } );
564 QUnit.test( "jQuery.data should not miss data-* w/ hyphenated property names trac-14047", function( assert ) {
566 assert.expect( 1 );
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" );
573 } );
575 QUnit.test( ".data should not miss attr() set data-* with hyphenated property names", function( assert ) {
576 assert.expect( 2 );
578 var a, b;
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" );
594 } );
596 QUnit.test( ".data always sets data with the camelCased key (gh-2257)", function( assert ) {
597 assert.expect( 18 );
599 var div = jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
600 datas = {
601 "non-empty": {
602 key: "nonEmpty",
603 value: "a string"
605 "empty-string": {
606 key: "emptyString",
607 value: ""
609 "one-value": {
610 key: "oneValue",
611 value: 1
613 "zero-value": {
614 key: "zeroValue",
615 value: 0
617 "an-array": {
618 key: "anArray",
619 value: []
621 "an-object": {
622 key: "anObject",
623 value: {}
625 "bool-true": {
626 key: "boolTrue",
627 value: true
629 "bool-false": {
630 key: "boolFalse",
631 value: false
633 "some-json": {
634 key: "someJson",
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" );
644 } );
645 } );
647 QUnit.test( ".data should not strip more than one hyphen when camelCasing (gh-2070)", function( assert ) {
648 assert.expect( 3 );
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" );
655 } );
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" ),
660 datas = {
661 "non-empty": {
662 key: "nonEmpty",
663 value: "a string"
665 "empty-string": {
666 key: "emptyString",
667 value: ""
669 "one-value": {
670 key: "oneValue",
671 value: 1
673 "zero-value": {
674 key: "zeroValue",
675 value: 0
677 "an-array": {
678 key: "anArray",
679 value: []
681 "an-object": {
682 key: "anObject",
683 value: {}
685 "bool-true": {
686 key: "boolTrue",
687 value: true
689 "bool-false": {
690 key: "boolFalse",
691 value: false
693 "some-json": {
694 key: "someJson",
695 value: "{ \"foo\": \"bar\" }"
698 "num-1-middle": {
699 key: "num-1Middle",
700 value: true
702 "num-end-2": {
703 key: "numEnd-2",
704 value: true
706 "2-num-start": {
707 key: "2NumStart",
708 value: true
711 // Vendor prefixes are not treated in a special way.
712 "-ms-foo": {
713 key: "MsFoo",
714 value: true
716 "-moz-foo": {
717 key: "MozFoo",
718 value: true
720 "-webkit-foo": {
721 key: "WebkitFoo",
722 value: true
724 "-fake-foo": {
725 key: "FakeFoo",
726 value: true
730 assert.expect( 32 );
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 );
737 } );
738 } );
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,
743 datas = {
744 "non-empty": "a string",
745 "empty-string": "",
746 "one-value": 1,
747 "zero-value": 0,
748 "an-array": [],
749 "an-object": {},
750 "bool-true": true,
751 "bool-false": false,
752 "some-json": "{ \"foo\": \"bar\" }"
755 assert.expect( 27 );
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 );
765 assert.deepEqual(
766 div.data( key.replace( rdashAlpha, fcamelCase ) ),
767 val,
768 "get: " + key.replace( rdashAlpha, fcamelCase )
771 div.removeData( key );
773 assert.equal( div.data( key ), undefined, "get: " + key );
775 } );
776 } );
778 QUnit.test( ".data supports interoperable removal of properties SET TWICE trac-13850", function( assert ) {
779 var div = jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
780 datas = {
781 "non-empty": "a string",
782 "empty-string": "",
783 "one-value": 1,
784 "zero-value": 0,
785 "an-array": [],
786 "an-object": {},
787 "bool-true": true,
788 "bool-false": false,
789 "some-json": "{ \"foo\": \"bar\" }"
792 assert.expect( 9 );
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 );
801 } );
802 } );
804 QUnit.test( ".removeData supports removal of hyphenated properties via array (trac-12786, gh-2257)", function( assert ) {
805 assert.expect( 4 );
807 var div, plain, compare;
809 div = jQuery( "<div>" ).appendTo( "#qunit-fixture" );
810 plain = jQuery( {} );
812 // Properties should always be camelCased
813 compare = {
815 // From batch assignment .data({ "a-a": 1 })
816 "aA": 1,
818 // From property, value assignment .data( "b-b", 1 )
819 "bB": 1
822 // Mixed assignment
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)" );
834 } );
836 // Test originally by Moschel
837 QUnit.test( ".removeData should not throw exceptions. (trac-10080)", function( assert ) {
838 var done = assert.async();
839 assert.expect( 1 );
840 var frame = jQuery( "#loadediframe" );
841 jQuery( frame[ 0 ].contentWindow ).on( "unload", function() {
842 assert.ok( true, "called unload" );
843 done();
844 } );
846 // change the url to trigger unload
847 frame.attr( "src", baseURL + "iframe.html?param=true" );
848 } );
850 QUnit.test( ".data only checks element attributes once. trac-8909", function( assert ) {
851 assert.expect( 2 );
852 var testing = {
853 "test": "testing",
854 "test2": "testing"
856 element = jQuery( "<div data-test='testing'>" ),
857 node = element[ 0 ];
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
867 element.remove();
868 } );
870 QUnit.test( "data-* with JSON value can have newlines", function( assert ) {
871 assert.expect( 1 );
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" );
875 x.remove();
876 } );
878 QUnit.test( ".data doesn't throw when calling selection is empty. trac-13551", function( assert ) {
879 assert.expect( 1 );
881 try {
882 jQuery( null ).data( "prop" );
883 assert.ok( true, "jQuery(null).data('prop') does not throw" );
884 } catch ( e ) {
885 assert.ok( false, e.message );
887 } );
889 QUnit.test( "acceptData", function( assert ) {
890 assert.expect( 10 );
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" );
913 } );
915 QUnit.test( "Check proper data removal of non-element descendants nodes (trac-8335)", function( assert ) {
916 assert.expect( 1 );
918 var div = jQuery( "<div>text</div>" ),
919 text = div.contents();
921 text.data( "test", "test" ); // This should be a noop.
922 div.remove();
924 assert.ok( !text.data( "test" ), "Be sure data is not stored in non-element" );
925 } );
927 testIframe(
928 "enumerate data attrs on body (trac-14894)",
929 "data/dataAttrs.html",
930 function( assert, jQuery, window, document, result ) {
931 assert.expect( 1 );
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 ) {
937 assert.expect( 2 );
939 var key,
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" );
951 } );
953 QUnit.test( "Check that the expando is removed when there's no more data on non-nodes", function( assert ) {
954 assert.expect( 1 );
956 var key,
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" );
968 } );
970 QUnit.test( ".data(prop) does not create expando", function( assert ) {
971 assert.expect( 1 );
973 var key,
974 div = jQuery( "<div></div>" );
976 div.data( "foo" );
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" );
985 } );
987 QUnit.test( "keys matching Object.prototype properties (gh-3256)", function( assert ) {
988 assert.expect( 2 );
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)" );
1000 } );