Build: replace CRLF with LF during minify
[jquery.git] / test / unit / data.js
blob2a7db81469ef209a4269106a0eddeb06abe2b191
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 for ( prop in obj2 ) {
321 num2++;
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()." );
330 // attribute parsing
331 i = 0;
332 JSON.parse = function() {
333 i++;
334 return parseJSON.apply( this, arguments );
337 child
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;
386 child.remove();
388 // tests from metadata plugin
389 function testData( index, elem ) {
390 switch ( index ) {
391 case 0:
392 assert.equal( jQuery( elem ).data( "foo" ), "bar", "Check foo property" );
393 assert.equal( jQuery( elem ).data( "bar" ), "baz", "Check baz property" );
394 break;
395 case 1:
396 assert.equal( jQuery( elem ).data( "test" ), "bar", "Check test property" );
397 assert.equal( jQuery( elem ).data( "bar" ), "baz", "Check bar property" );
398 break;
399 case 2:
400 assert.equal( jQuery( elem ).data( "zoooo" ), "bar", "Check zoooo property" );
401 assert.deepEqual( jQuery( elem ).data( "bar" ), { "test":"baz" }, "Check bar property" );
402 break;
403 case 3:
404 assert.equal( jQuery( elem ).data( "number" ), true, "Check number property" );
405 assert.deepEqual( jQuery( elem ).data( "stuff" ), [ 2, 8 ], "Check stuff property" );
406 break;
407 default:
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 );
416 elem.remove();
417 } );
419 QUnit.test( ".data(Object)", function( assert ) {
420 assert.expect( 4 );
422 var obj, jqobj,
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
438 div.remove();
439 } );
441 QUnit.test( "jQuery.removeData", function( assert ) {
442 assert.expect( 10 );
444 var obj,
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." );
455 jQuery.data( div, {
456 test3: "testing",
457 test4: "testing"
458 } );
459 jQuery.removeData( div, "test3 test4" );
460 assert.ok( !jQuery.data( div, "test3" ) || jQuery.data( div, "test4" ), "Multiple delete with spaces." );
462 jQuery.data( div, {
463 test3: "testing",
464 test4: "testing"
465 } );
466 jQuery.removeData( div, [ "test3", "test4" ] );
467 assert.ok( !jQuery.data( div, "test3" ) || jQuery.data( div, "test4" ), "Multiple delete by array." );
469 jQuery.data( div, {
470 "test3 test4": "testing",
471 "test3": "testing"
472 } );
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" );
477 obj = {};
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." );
486 } );
488 QUnit.test( ".removeData()", function( assert ) {
489 assert.expect( 6 );
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" );
507 } );
509 QUnit.test( "JSON serialization (trac-8108)", function( assert ) {
510 assert.expect( 1 );
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" );
516 } );
518 QUnit.test( ".data should follow html5 specification regarding camel casing", function( assert ) {
519 assert.expect( 12 );
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)" );
543 div.remove();
544 } );
546 QUnit.test( ".data should not miss preset data-* w/ hyphenated property names", function( assert ) {
548 assert.expect( 2 );
550 var div = jQuery( "<div></div>", { id: "hyphened" } ).appendTo( "#qunit-fixture" ),
551 test = {
552 "camelBar": "camelBar",
553 "hyphen-foo": "hyphen-foo"
556 div.data( test );
558 jQuery.each( test, function( i, k ) {
559 assert.equal( div.data( k ), k, "data with property '" + k + "' was correctly found" );
560 } );
561 } );
563 QUnit.test( "jQuery.data should not miss data-* w/ hyphenated property names trac-14047", function( assert ) {
565 assert.expect( 1 );
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" );
572 } );
574 QUnit.test( ".data should not miss attr() set data-* with hyphenated property names", function( assert ) {
575 assert.expect( 2 );
577 var a, b;
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" );
593 } );
595 QUnit.test( ".data always sets data with the camelCased key (gh-2257)", function( assert ) {
596 assert.expect( 18 );
598 var div = jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
599 datas = {
600 "non-empty": {
601 key: "nonEmpty",
602 value: "a string"
604 "empty-string": {
605 key: "emptyString",
606 value: ""
608 "one-value": {
609 key: "oneValue",
610 value: 1
612 "zero-value": {
613 key: "zeroValue",
614 value: 0
616 "an-array": {
617 key: "anArray",
618 value: []
620 "an-object": {
621 key: "anObject",
622 value: {}
624 "bool-true": {
625 key: "boolTrue",
626 value: true
628 "bool-false": {
629 key: "boolFalse",
630 value: false
632 "some-json": {
633 key: "someJson",
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" );
643 } );
644 } );
646 QUnit.test( ".data should not strip more than one hyphen when camelCasing (gh-2070)", function( assert ) {
647 assert.expect( 3 );
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" );
654 } );
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" ),
659 datas = {
660 "non-empty": {
661 key: "nonEmpty",
662 value: "a string"
664 "empty-string": {
665 key: "emptyString",
666 value: ""
668 "one-value": {
669 key: "oneValue",
670 value: 1
672 "zero-value": {
673 key: "zeroValue",
674 value: 0
676 "an-array": {
677 key: "anArray",
678 value: []
680 "an-object": {
681 key: "anObject",
682 value: {}
684 "bool-true": {
685 key: "boolTrue",
686 value: true
688 "bool-false": {
689 key: "boolFalse",
690 value: false
692 "some-json": {
693 key: "someJson",
694 value: "{ \"foo\": \"bar\" }"
697 "num-1-middle": {
698 key: "num-1Middle",
699 value: true
701 "num-end-2": {
702 key: "numEnd-2",
703 value: true
705 "2-num-start": {
706 key: "2NumStart",
707 value: true
710 // Vendor prefixes are not treated in a special way.
711 "-ms-foo": {
712 key: "MsFoo",
713 value: true
715 "-moz-foo": {
716 key: "MozFoo",
717 value: true
719 "-webkit-foo": {
720 key: "WebkitFoo",
721 value: true
723 "-fake-foo": {
724 key: "FakeFoo",
725 value: true
729 assert.expect( 32 );
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 );
736 } );
737 } );
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,
742 datas = {
743 "non-empty": "a string",
744 "empty-string": "",
745 "one-value": 1,
746 "zero-value": 0,
747 "an-array": [],
748 "an-object": {},
749 "bool-true": true,
750 "bool-false": false,
751 "some-json": "{ \"foo\": \"bar\" }"
754 assert.expect( 27 );
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 );
764 assert.deepEqual(
765 div.data( key.replace( rdashAlpha, fcamelCase ) ),
766 val,
767 "get: " + key.replace( rdashAlpha, fcamelCase )
770 div.removeData( key );
772 assert.equal( div.data( key ), undefined, "get: " + key );
774 } );
775 } );
777 QUnit.test( ".data supports interoperable removal of properties SET TWICE trac-13850", function( assert ) {
778 var div = jQuery( "<div>" ).appendTo( "#qunit-fixture" ),
779 datas = {
780 "non-empty": "a string",
781 "empty-string": "",
782 "one-value": 1,
783 "zero-value": 0,
784 "an-array": [],
785 "an-object": {},
786 "bool-true": true,
787 "bool-false": false,
788 "some-json": "{ \"foo\": \"bar\" }"
791 assert.expect( 9 );
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 );
800 } );
801 } );
803 QUnit.test( ".removeData supports removal of hyphenated properties via array (trac-12786, gh-2257)", function( assert ) {
804 assert.expect( 4 );
806 var div, plain, compare;
808 div = jQuery( "<div>" ).appendTo( "#qunit-fixture" );
809 plain = jQuery( {} );
811 // Properties should always be camelCased
812 compare = {
814 // From batch assignment .data({ "a-a": 1 })
815 "aA": 1,
817 // From property, value assignment .data( "b-b", 1 )
818 "bB": 1
821 // Mixed assignment
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)" );
833 } );
835 // Test originally by Moschel
836 QUnit.test( ".removeData should not throw exceptions. (trac-10080)", function( assert ) {
837 var done = assert.async();
838 assert.expect( 1 );
839 var frame = jQuery( "#loadediframe" );
840 jQuery( frame[ 0 ].contentWindow ).on( "unload", function() {
841 assert.ok( true, "called unload" );
842 done();
843 } );
845 // change the url to trigger unload
846 frame.attr( "src", baseURL + "iframe.html?param=true" );
847 } );
849 QUnit.test( ".data only checks element attributes once. trac-8909", function( assert ) {
850 assert.expect( 2 );
851 var testing = {
852 "test": "testing",
853 "test2": "testing"
855 element = jQuery( "<div data-test='testing'>" ),
856 node = element[ 0 ];
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
866 element.remove();
867 } );
869 QUnit.test( "data-* with JSON value can have newlines", function( assert ) {
870 assert.expect( 1 );
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" );
874 x.remove();
875 } );
877 QUnit.test( ".data doesn't throw when calling selection is empty. trac-13551", function( assert ) {
878 assert.expect( 1 );
880 try {
881 jQuery( null ).data( "prop" );
882 assert.ok( true, "jQuery(null).data('prop') does not throw" );
883 } catch ( e ) {
884 assert.ok( false, e.message );
886 } );
888 QUnit.test( "acceptData", function( assert ) {
889 assert.expect( 10 );
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" );
912 } );
914 QUnit.test( "Check proper data removal of non-element descendants nodes (trac-8335)", function( assert ) {
915 assert.expect( 1 );
917 var div = jQuery( "<div>text</div>" ),
918 text = div.contents();
920 text.data( "test", "test" ); // This should be a noop.
921 div.remove();
923 assert.ok( !text.data( "test" ), "Be sure data is not stored in non-element" );
924 } );
926 testIframe(
927 "enumerate data attrs on body (trac-14894)",
928 "data/dataAttrs.html",
929 function( assert, jQuery, window, document, result ) {
930 assert.expect( 1 );
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 ) {
936 assert.expect( 2 );
938 var key,
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" );
950 } );
952 QUnit.test( "Check that the expando is removed when there's no more data on non-nodes", function( assert ) {
953 assert.expect( 1 );
955 var key,
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" );
967 } );
969 QUnit.test( ".data(prop) does not create expando", function( assert ) {
970 assert.expect( 1 );
972 var key,
973 div = jQuery( "<div></div>" );
975 div.data( "foo" );
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" );
984 } );
986 QUnit.test( "keys matching Object.prototype properties (gh-3256)", function( assert ) {
987 assert.expect( 2 );
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)" );
999 } );