1
0

data.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758
  1. module("data", { teardown: moduleTeardown });
  2. test("expando", function(){
  3. expect(1);
  4. equal(jQuery.expando !== undefined, true, "jQuery is exposing the expando");
  5. });
  6. test( "jQuery.data & removeData, expected returns", function() {
  7. expect(4);
  8. var elem = document.body;
  9. equal(
  10. jQuery.data( elem, "hello", "world" ), "world",
  11. "jQuery.data( elem, key, value ) returns value"
  12. );
  13. equal(
  14. jQuery.data( elem, "hello" ), "world",
  15. "jQuery.data( elem, key ) returns value"
  16. );
  17. deepEqual(
  18. jQuery.data( elem, { goodnight: "moon" }), { goodnight: "moon" },
  19. "jQuery.data( elem, obj ) returns obj"
  20. );
  21. equal(
  22. jQuery.removeData( elem, "hello" ), undefined,
  23. "jQuery.removeData( elem, key, value ) returns undefined"
  24. );
  25. });
  26. test( "jQuery._data & _removeData, expected returns", function() {
  27. expect(4);
  28. var elem = document.body;
  29. equal(
  30. jQuery._data( elem, "hello", "world" ), "world",
  31. "jQuery._data( elem, key, value ) returns value"
  32. );
  33. equal(
  34. jQuery._data( elem, "hello" ), "world",
  35. "jQuery._data( elem, key ) returns value"
  36. );
  37. deepEqual(
  38. jQuery._data( elem, { goodnight: "moon" }), { goodnight: "moon" },
  39. "jQuery._data( elem, obj ) returns obj"
  40. );
  41. equal(
  42. jQuery._removeData( elem, "hello" ), undefined,
  43. "jQuery._removeData( elem, key, value ) returns undefined"
  44. );
  45. });
  46. test( "jQuery.hasData no side effects", function() {
  47. expect(1);
  48. var obj = {};
  49. jQuery.hasData( obj );
  50. equal( Object.getOwnPropertyNames( obj ).length, 0,
  51. "No data expandos where added when calling jQuery.hasData(o)"
  52. );
  53. });
  54. function dataTests (elem) {
  55. var dataObj, internalDataObj;
  56. equal( jQuery.data(elem, "foo"), undefined, "No data exists initially" );
  57. strictEqual( jQuery.hasData(elem), false, "jQuery.hasData agrees no data exists initially" );
  58. dataObj = jQuery.data(elem);
  59. equal( typeof dataObj, "object", "Calling data with no args gives us a data object reference" );
  60. strictEqual( jQuery.data(elem), dataObj, "Calling jQuery.data returns the same data object when called multiple times" );
  61. strictEqual( jQuery.hasData(elem), false, "jQuery.hasData agrees no data exists even when an empty data obj exists" );
  62. dataObj["foo"] = "bar";
  63. equal( jQuery.data(elem, "foo"), "bar", "Data is readable by jQuery.data when set directly on a returned data object" );
  64. strictEqual( jQuery.hasData(elem), true, "jQuery.hasData agrees data exists when data exists" );
  65. jQuery.data(elem, "foo", "baz");
  66. equal( jQuery.data(elem, "foo"), "baz", "Data can be changed by jQuery.data" );
  67. equal( dataObj["foo"], "baz", "Changes made through jQuery.data propagate to referenced data object" );
  68. jQuery.data(elem, "foo", undefined);
  69. equal( jQuery.data(elem, "foo"), "baz", "Data is not unset by passing undefined to jQuery.data" );
  70. jQuery.data(elem, "foo", null);
  71. strictEqual( jQuery.data(elem, "foo"), null, "Setting null using jQuery.data works OK" );
  72. jQuery.data(elem, "foo", "foo1");
  73. jQuery.data(elem, { "bar" : "baz", "boom" : "bloz" });
  74. strictEqual( jQuery.data(elem, "foo"), "foo1", "Passing an object extends the data object instead of replacing it" );
  75. equal( jQuery.data(elem, "boom"), "bloz", "Extending the data object works" );
  76. jQuery._data(elem, "foo", "foo2", true);
  77. equal( jQuery._data(elem, "foo"), "foo2", "Setting internal data works" );
  78. equal( jQuery.data(elem, "foo"), "foo1", "Setting internal data does not override user data" );
  79. internalDataObj = jQuery._data( elem );
  80. ok( internalDataObj, "Internal data object exists" );
  81. notStrictEqual( dataObj, internalDataObj, "Internal data object is not the same as user data object" );
  82. strictEqual( elem.boom, undefined, "Data is never stored directly on the object" );
  83. jQuery.removeData(elem, "foo");
  84. strictEqual( jQuery.data(elem, "foo"), undefined, "jQuery.removeData removes single properties" );
  85. jQuery.removeData(elem);
  86. strictEqual( jQuery._data(elem), internalDataObj, "jQuery.removeData does not remove internal data if it exists" );
  87. jQuery.data(elem, "foo", "foo1");
  88. jQuery._data(elem, "foo", "foo2");
  89. equal( jQuery.data(elem, "foo"), "foo1", "(sanity check) Ensure data is set in user data object" );
  90. equal( jQuery._data(elem, "foo"), "foo2", "(sanity check) Ensure data is set in internal data object" );
  91. strictEqual( jQuery._data(elem, jQuery.expando), undefined, "Removing the last item in internal data destroys the internal data object" );
  92. jQuery._data(elem, "foo", "foo2");
  93. equal( jQuery._data(elem, "foo"), "foo2", "(sanity check) Ensure data is set in internal data object" );
  94. jQuery.removeData(elem, "foo");
  95. equal( jQuery._data(elem, "foo"), "foo2", "(sanity check) jQuery.removeData for user data does not remove internal data" );
  96. }
  97. test("jQuery.data(div)", 25, function() {
  98. var div = document.createElement("div");
  99. dataTests(div);
  100. // We stored one key in the private data
  101. // assert that nothing else was put in there, and that that
  102. // one stayed there.
  103. QUnit.expectJqData(div, "foo");
  104. });
  105. test("jQuery.data({})", 25, function() {
  106. dataTests({});
  107. });
  108. test("jQuery.data(window)", 25, function() {
  109. // remove bound handlers from window object to stop potential false positives caused by fix for #5280 in
  110. // transports/xhr.js
  111. jQuery(window).off("unload");
  112. dataTests(window);
  113. });
  114. test("jQuery.data(document)", 25, function() {
  115. dataTests(document);
  116. QUnit.expectJqData(document, "foo");
  117. });
  118. test("jQuery.data(<embed>)", 25, function() {
  119. dataTests( document.createElement("embed") );
  120. });
  121. test("jQuery.data(<applet>)", 25, function() {
  122. dataTests( document.createElement("applet") );
  123. });
  124. test("jQuery.data(object/flash)", 25, function() {
  125. var flash = document.createElement("object");
  126. flash.setAttribute( "classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" );
  127. dataTests( flash );
  128. });
  129. test(".data()", function() {
  130. expect(5);
  131. var div, dataObj, nodiv, obj;
  132. div = jQuery("#foo");
  133. strictEqual( div.data("foo"), undefined, "Make sure that missing result is undefined" );
  134. div.data("test", "success");
  135. dataObj = div.data();
  136. deepEqual( dataObj, {test: "success"}, "data() returns entire data object with expected properties" );
  137. strictEqual( div.data("foo"), undefined, "Make sure that missing result is still undefined" );
  138. nodiv = jQuery("#unfound");
  139. equal( nodiv.data(), null, "data() on empty set returns null" );
  140. obj = { foo: "bar" };
  141. jQuery(obj).data("foo", "baz");
  142. dataObj = jQuery.extend(true, {}, jQuery(obj).data());
  143. deepEqual( dataObj, { "foo": "baz" }, "Retrieve data object from a wrapped JS object (#7524)" );
  144. });
  145. function testDataTypes( $obj ) {
  146. jQuery.each({
  147. "null": null,
  148. "true": true,
  149. "false": false,
  150. "zero": 0,
  151. "one": 1,
  152. "empty string": "",
  153. "empty array": [],
  154. "array": [1],
  155. "empty object": {},
  156. "object": { foo: "bar" },
  157. "date": new Date(),
  158. "regex": /test/,
  159. "function": function() {}
  160. }, function( type, value ) {
  161. strictEqual( $obj.data( "test", value ).data("test"), value, "Data set to " + type );
  162. });
  163. }
  164. test("jQuery(Element).data(String, Object).data(String)", function() {
  165. expect( 18 );
  166. var parent = jQuery("<div><div></div></div>"),
  167. div = parent.children();
  168. strictEqual( div.data("test"), undefined, "No data exists initially" );
  169. strictEqual( div.data("test", "success").data("test"), "success", "Data added" );
  170. strictEqual( div.data("test", "overwritten").data("test"), "overwritten", "Data overwritten" );
  171. strictEqual( div.data("test", undefined).data("test"), "overwritten", ".data(key,undefined) does nothing but is chainable (#5571)");
  172. strictEqual( div.data("notexist"), undefined, "No data exists for unset key" );
  173. testDataTypes( div );
  174. parent.remove();
  175. });
  176. test("jQuery(plain Object).data(String, Object).data(String)", function() {
  177. expect( 16 );
  178. // #3748
  179. var $obj = jQuery({ exists: true });
  180. strictEqual( $obj.data("nothing"), undefined, "Non-existent data returns undefined");
  181. strictEqual( $obj.data("exists"), undefined, "Object properties are not returned as data" );
  182. testDataTypes( $obj );
  183. // Clean up
  184. $obj.removeData();
  185. deepEqual( $obj[0], { exists: true }, "removeData does not clear the object" );
  186. });
  187. test(".data(object) does not retain references. #13815", function() {
  188. expect( 2 );
  189. var $divs = jQuery("<div></div><div></div>").appendTo("#qunit-fixture");
  190. $divs.data({ "type": "foo" });
  191. $divs.eq( 0 ).data( "type", "bar" );
  192. equal( $divs.eq( 0 ).data("type"), "bar", "Correct updated value" );
  193. equal( $divs.eq( 1 ).data("type"), "foo", "Original value retained" );
  194. });
  195. test("data-* attributes", function() {
  196. expect(40);
  197. var prop, i, l, metadata, elem,
  198. obj, obj2, check, num, num2,
  199. div = jQuery("<div>"),
  200. child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>"),
  201. dummy = jQuery("<div data-myobj='old data' data-ignored=\"DOM\" data-other='test'></div>");
  202. equal( div.data("attr"), undefined, "Check for non-existing data-attr attribute" );
  203. div.attr("data-attr", "exists");
  204. equal( div.data("attr"), "exists", "Check for existing data-attr attribute" );
  205. div.attr("data-attr", "exists2");
  206. equal( div.data("attr"), "exists", "Check that updates to data- don't update .data()" );
  207. div.data("attr", "internal").attr("data-attr", "external");
  208. equal( div.data("attr"), "internal", "Check for .data('attr') precedence (internal > external data-* attribute)" );
  209. div.remove();
  210. child.appendTo("#qunit-fixture");
  211. equal( child.data("myobj"), "old data", "Value accessed from data-* attribute");
  212. child.data("myobj", "replaced");
  213. equal( child.data("myobj"), "replaced", "Original data overwritten");
  214. child.data("ignored", "cache");
  215. equal( child.data("ignored"), "cache", "Cached data used before DOM data-* fallback");
  216. obj = child.data();
  217. obj2 = dummy.data();
  218. check = [ "myobj", "ignored", "other" ];
  219. num = 0;
  220. num2 = 0;
  221. dummy.remove();
  222. for ( i = 0, l = check.length; i < l; i++ ) {
  223. ok( obj[ check[i] ], "Make sure data- property exists when calling data-." );
  224. ok( obj2[ check[i] ], "Make sure data- property exists when calling data-." );
  225. }
  226. for ( prop in obj ) {
  227. num++;
  228. }
  229. equal( num, check.length, "Make sure that the right number of properties came through." );
  230. for ( prop in obj2 ) {
  231. num2++;
  232. }
  233. equal( num2, check.length, "Make sure that the right number of properties came through." );
  234. child.attr("data-other", "newvalue");
  235. equal( child.data("other"), "test", "Make sure value was pulled in properly from a .data()." );
  236. child
  237. .attr("data-true", "true")
  238. .attr("data-false", "false")
  239. .attr("data-five", "5")
  240. .attr("data-point", "5.5")
  241. .attr("data-pointe", "5.5E3")
  242. .attr("data-grande", "5.574E9")
  243. .attr("data-hexadecimal", "0x42")
  244. .attr("data-pointbad", "5..5")
  245. .attr("data-pointbad2", "-.")
  246. .attr("data-bigassnum", "123456789123456789123456789")
  247. .attr("data-badjson", "{123}")
  248. .attr("data-badjson2", "[abc]")
  249. .attr("data-empty", "")
  250. .attr("data-space", " ")
  251. .attr("data-null", "null")
  252. .attr("data-string", "test");
  253. strictEqual( child.data("true"), true, "Primitive true read from attribute");
  254. strictEqual( child.data("false"), false, "Primitive false read from attribute");
  255. strictEqual( child.data("five"), 5, "Primitive number read from attribute");
  256. strictEqual( child.data("point"), 5.5, "Primitive number read from attribute");
  257. strictEqual( child.data("pointe"), "5.5E3", "Floating point exponential number read from attribute");
  258. strictEqual( child.data("grande"), "5.574E9", "Big exponential number read from attribute");
  259. strictEqual( child.data("hexadecimal"), "0x42", "Hexadecimal number read from attribute");
  260. strictEqual( child.data("pointbad"), "5..5", "Bad number read from attribute");
  261. strictEqual( child.data("pointbad2"), "-.", "Bad number read from attribute");
  262. strictEqual( child.data("bigassnum"), "123456789123456789123456789", "Bad bigass number read from attribute");
  263. strictEqual( child.data("badjson"), "{123}", "Bad number read from attribute");
  264. strictEqual( child.data("badjson2"), "[abc]", "Bad number read from attribute");
  265. strictEqual( child.data("empty"), "", "Empty string read from attribute");
  266. strictEqual( child.data("space"), " ", "Empty string read from attribute");
  267. strictEqual( child.data("null"), null, "Primitive null read from attribute");
  268. strictEqual( child.data("string"), "test", "Typical string read from attribute");
  269. child.remove();
  270. // tests from metadata plugin
  271. function testData(index, elem) {
  272. switch (index) {
  273. case 0:
  274. equal(jQuery(elem).data("foo"), "bar", "Check foo property");
  275. equal(jQuery(elem).data("bar"), "baz", "Check baz property");
  276. break;
  277. case 1:
  278. equal(jQuery(elem).data("test"), "bar", "Check test property");
  279. equal(jQuery(elem).data("bar"), "baz", "Check bar property");
  280. break;
  281. case 2:
  282. equal(jQuery(elem).data("zoooo"), "bar", "Check zoooo property");
  283. deepEqual(jQuery(elem).data("bar"), {"test":"baz"}, "Check bar property");
  284. break;
  285. case 3:
  286. equal(jQuery(elem).data("number"), true, "Check number property");
  287. deepEqual(jQuery(elem).data("stuff"), [2,8], "Check stuff property");
  288. break;
  289. default:
  290. ok(false, ["Assertion failed on index ", index, ", with data"].join(""));
  291. }
  292. }
  293. 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>";
  294. elem = jQuery(metadata).appendTo("#qunit-fixture");
  295. elem.find("li").each(testData);
  296. elem.remove();
  297. });
  298. test(".data(Object)", function() {
  299. expect(4);
  300. var obj, jqobj,
  301. div = jQuery("<div/>");
  302. div.data({ "test": "in", "test2": "in2" });
  303. equal( div.data("test"), "in", "Verify setting an object in data" );
  304. equal( div.data("test2"), "in2", "Verify setting an object in data" );
  305. obj = {test:"unset"};
  306. jqobj = jQuery(obj);
  307. jqobj.data("test", "unset");
  308. jqobj.data({ "test": "in", "test2": "in2" });
  309. equal( jQuery.data(obj)["test"], "in", "Verify setting an object on an object extends the data object" );
  310. equal( obj["test2"], undefined, "Verify setting an object on an object does not extend the object" );
  311. // manually clean up detached elements
  312. div.remove();
  313. });
  314. test("jQuery.removeData", function() {
  315. expect(10);
  316. var obj,
  317. div = jQuery("#foo")[0];
  318. jQuery.data(div, "test", "testing");
  319. jQuery.removeData(div, "test");
  320. equal( jQuery.data(div, "test"), undefined, "Check removal of data" );
  321. jQuery.data(div, "test2", "testing");
  322. jQuery.removeData( div );
  323. ok( !jQuery.data(div, "test2"), "Make sure that the data property no longer exists." );
  324. ok( !div[ jQuery.expando ], "Make sure the expando no longer exists, as well." );
  325. jQuery.data(div, {
  326. test3: "testing",
  327. test4: "testing"
  328. });
  329. jQuery.removeData( div, "test3 test4" );
  330. ok( !jQuery.data(div, "test3") || jQuery.data(div, "test4"), "Multiple delete with spaces." );
  331. jQuery.data(div, {
  332. test3: "testing",
  333. test4: "testing"
  334. });
  335. jQuery.removeData( div, [ "test3", "test4" ] );
  336. ok( !jQuery.data(div, "test3") || jQuery.data(div, "test4"), "Multiple delete by array." );
  337. jQuery.data(div, {
  338. "test3 test4": "testing",
  339. "test3": "testing"
  340. });
  341. jQuery.removeData( div, "test3 test4" );
  342. ok( !jQuery.data(div, "test3 test4"), "Multiple delete with spaces deleted key with exact name" );
  343. ok( jQuery.data(div, "test3"), "Left the partial matched key alone" );
  344. obj = {};
  345. jQuery.data(obj, "test", "testing");
  346. equal( jQuery(obj).data("test"), "testing", "verify data on plain object");
  347. jQuery.removeData(obj, "test");
  348. equal( jQuery.data(obj, "test"), undefined, "Check removal of data on plain object" );
  349. jQuery.data( window, "BAD", true );
  350. jQuery.removeData( window, "BAD" );
  351. ok( !jQuery.data( window, "BAD" ), "Make sure that the value was not still set." );
  352. });
  353. test(".removeData()", function() {
  354. expect(6);
  355. var div = jQuery("#foo");
  356. div.data("test", "testing");
  357. div.removeData("test");
  358. equal( div.data("test"), undefined, "Check removal of data" );
  359. div.data("test", "testing");
  360. div.data("test.foo", "testing2");
  361. div.removeData("test.bar");
  362. equal( div.data("test.foo"), "testing2", "Make sure data is intact" );
  363. equal( div.data("test"), "testing", "Make sure data is intact" );
  364. div.removeData("test");
  365. equal( div.data("test.foo"), "testing2", "Make sure data is intact" );
  366. equal( div.data("test"), undefined, "Make sure data is intact" );
  367. div.removeData("test.foo");
  368. equal( div.data("test.foo"), undefined, "Make sure data is intact" );
  369. });
  370. if (window.JSON && window.JSON.stringify) {
  371. test("JSON serialization (#8108)", function () {
  372. expect(1);
  373. var obj = { "foo": "bar" };
  374. jQuery.data(obj, "hidden", true);
  375. equal( JSON.stringify(obj), "{\"foo\":\"bar\"}", "Expando is hidden from JSON.stringify" );
  376. });
  377. }
  378. test(".data should follow html5 specification regarding camel casing", function() {
  379. expect(12);
  380. 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>")
  381. .prependTo("body");
  382. equal( div.data()["wTF"], "ftw", "Verify single letter data-* key" );
  383. equal( div.data()["bigALittleA"], "bouncing-b", "Verify single letter mixed data-* key" );
  384. equal( div.data()["foo"], "a", "Verify single word data-* key" );
  385. equal( div.data()["fooBar"], "b", "Verify multiple word data-* key" );
  386. equal( div.data()["fooBarBaz"], "c", "Verify multiple word data-* key" );
  387. equal( div.data("foo"), "a", "Verify single word data-* key" );
  388. equal( div.data("fooBar"), "b", "Verify multiple word data-* key" );
  389. equal( div.data("fooBarBaz"), "c", "Verify multiple word data-* key" );
  390. div.data("foo-bar", "d");
  391. equal( div.data("fooBar"), "d", "Verify updated data-* key" );
  392. equal( div.data("foo-bar"), "d", "Verify updated data-* key" );
  393. equal( div.data("fooBar"), "d", "Verify updated data-* key (fooBar)" );
  394. equal( div.data("foo-bar"), "d", "Verify updated data-* key (foo-bar)" );
  395. div.remove();
  396. });
  397. test(".data should not miss preset data-* w/ hyphenated property names", function() {
  398. expect(2);
  399. var div = jQuery("<div/>", { id: "hyphened" }).appendTo("#qunit-fixture"),
  400. test = {
  401. "camelBar": "camelBar",
  402. "hyphen-foo": "hyphen-foo"
  403. };
  404. div.data( test );
  405. jQuery.each( test , function(i, k) {
  406. equal( div.data(k), k, "data with property '"+k+"' was correctly found");
  407. });
  408. });
  409. test("jQuery.data should not miss data-* w/ hyphenated property names #14047", function() {
  410. expect(1);
  411. var div = jQuery("<div/>");
  412. div.data( "foo-bar", "baz" );
  413. equal( jQuery.data(div[0], "foo-bar"), "baz", "data with property 'foo-bar' was correctly found");
  414. });
  415. test(".data should not miss attr() set data-* with hyphenated property names", function() {
  416. expect(2);
  417. var a, b;
  418. a = jQuery("<div/>").appendTo("#qunit-fixture");
  419. a.attr( "data-long-param", "test" );
  420. a.data( "long-param", { a: 2 });
  421. deepEqual( a.data("long-param"), { a: 2 }, "data with property long-param was found, 1" );
  422. b = jQuery("<div/>").appendTo("#qunit-fixture");
  423. b.attr( "data-long-param", "test" );
  424. b.data( "long-param" );
  425. b.data( "long-param", { a: 2 });
  426. deepEqual( b.data("long-param"), { a: 2 }, "data with property long-param was found, 2" );
  427. });
  428. test(".data supports interoperable hyphenated/camelCase get/set of properties with arbitrary non-null|NaN|undefined values", function() {
  429. var div = jQuery("<div/>", { id: "hyphened" }).appendTo("#qunit-fixture"),
  430. datas = {
  431. "non-empty": "a string",
  432. "empty-string": "",
  433. "one-value": 1,
  434. "zero-value": 0,
  435. "an-array": [],
  436. "an-object": {},
  437. "bool-true": true,
  438. "bool-false": false,
  439. // JSHint enforces double quotes,
  440. // but JSON strings need double quotes to parse
  441. // so we need escaped double quotes here
  442. "some-json": "{ \"foo\": \"bar\" }",
  443. "num-1-middle": true,
  444. "num-end-2": true,
  445. "2-num-start": true
  446. };
  447. expect( 24 );
  448. jQuery.each( datas, function( key, val ) {
  449. div.data( key, val );
  450. deepEqual( div.data( key ), val, "get: " + key );
  451. deepEqual( div.data( jQuery.camelCase( key ) ), val, "get: " + jQuery.camelCase( key ) );
  452. });
  453. });
  454. test(".data supports interoperable removal of hyphenated/camelCase properties", function() {
  455. var div = jQuery("<div/>", { id: "hyphened" }).appendTo("#qunit-fixture"),
  456. datas = {
  457. "non-empty": "a string",
  458. "empty-string": "",
  459. "one-value": 1,
  460. "zero-value": 0,
  461. "an-array": [],
  462. "an-object": {},
  463. "bool-true": true,
  464. "bool-false": false,
  465. // JSHint enforces double quotes,
  466. // but JSON strings need double quotes to parse
  467. // so we need escaped double quotes here
  468. "some-json": "{ \"foo\": \"bar\" }"
  469. };
  470. expect( 27 );
  471. jQuery.each( datas, function( key, val ) {
  472. div.data( key, val );
  473. deepEqual( div.data( key ), val, "get: " + key );
  474. deepEqual( div.data( jQuery.camelCase( key ) ), val, "get: " + jQuery.camelCase( key ) );
  475. div.removeData( key );
  476. equal( div.data( key ), undefined, "get: " + key );
  477. });
  478. });
  479. test(".data supports interoperable removal of properties SET TWICE #13850", function() {
  480. var div = jQuery("<div>").appendTo("#qunit-fixture"),
  481. datas = {
  482. "non-empty": "a string",
  483. "empty-string": "",
  484. "one-value": 1,
  485. "zero-value": 0,
  486. "an-array": [],
  487. "an-object": {},
  488. "bool-true": true,
  489. "bool-false": false,
  490. // JSHint enforces double quotes,
  491. // but JSON strings need double quotes to parse
  492. // so we need escaped double quotes here
  493. "some-json": "{ \"foo\": \"bar\" }"
  494. };
  495. expect( 9 );
  496. jQuery.each( datas, function( key, val ) {
  497. div.data( key, val );
  498. div.data( key, val );
  499. div.removeData( key );
  500. equal( div.data( key ), undefined, "removal: " + key );
  501. });
  502. });
  503. test( ".removeData supports removal of hyphenated properties via array (#12786)", function() {
  504. expect( 4 );
  505. var div, plain, compare;
  506. div = jQuery("<div>").appendTo("#qunit-fixture");
  507. plain = jQuery({});
  508. // When data is batch assigned (via plain object), the properties
  509. // are not camel cased as they are with (property, value) calls
  510. compare = {
  511. // From batch assignment .data({ "a-a": 1 })
  512. "a-a": 1,
  513. // From property, value assignment .data( "b-b", 1 )
  514. "bB": 1
  515. };
  516. // Mixed assignment
  517. div.data({ "a-a": 1 }).data( "b-b", 1 );
  518. plain.data({ "a-a": 1 }).data( "b-b", 1 );
  519. deepEqual( div.data(), compare, "Data appears as expected. (div)" );
  520. deepEqual( plain.data(), compare, "Data appears as expected. (plain)" );
  521. div.removeData([ "a-a", "b-b" ]);
  522. plain.removeData([ "a-a", "b-b" ]);
  523. // NOTE: Timo's proposal for "propEqual" (or similar) would be nice here
  524. deepEqual( div.data(), {}, "Data is empty. (div)" );
  525. deepEqual( plain.data(), {}, "Data is empty. (plain)" );
  526. });
  527. // Test originally by Moschel
  528. test(".removeData should not throw exceptions. (#10080)", function() {
  529. expect(1);
  530. stop();
  531. var frame = jQuery("#loadediframe");
  532. jQuery(frame[0].contentWindow).on("unload", function() {
  533. ok(true, "called unload");
  534. start();
  535. });
  536. // change the url to trigger unload
  537. frame.attr("src", "data/iframe.html?param=true");
  538. });
  539. test( ".data only checks element attributes once. #8909", function() {
  540. expect( 2 );
  541. var testing = {
  542. "test": "testing",
  543. "test2": "testing"
  544. },
  545. element = jQuery( "<div data-test='testing'>" ),
  546. node = element[ 0 ];
  547. // set an attribute using attr to ensure it
  548. node.setAttribute( "data-test2", "testing" );
  549. deepEqual( element.data(), testing, "Sanity Check" );
  550. node.setAttribute( "data-test3", "testing" );
  551. deepEqual( element.data(), testing, "The data didn't change even though the data-* attrs did" );
  552. // clean up data cache
  553. element.remove();
  554. });
  555. test( "data-* with JSON value can have newlines", function() {
  556. expect(1);
  557. var x = jQuery("<div data-some='{\n\"foo\":\n\t\"bar\"\n}'></div>");
  558. equal( x.data("some").foo, "bar", "got a JSON data- attribute with spaces" );
  559. x.remove();
  560. });
  561. test(".data doesn't throw when calling selection is empty. #13551", function() {
  562. expect(1);
  563. try {
  564. jQuery( null ).data( "prop" );
  565. ok( true, "jQuery(null).data('prop') does not throw" );
  566. } catch ( e ) {
  567. ok( false, e.message );
  568. }
  569. });
  570. test("jQuery.acceptData", 6, function() {
  571. ok( jQuery.acceptData( document ), "document" );
  572. ok( jQuery.acceptData( document.documentElement ), "documentElement" );
  573. ok( jQuery.acceptData( {} ), "object" );
  574. ok( !jQuery.acceptData( document.createComment("") ), "comment" );
  575. ok( !jQuery.acceptData( document.createTextNode("") ), "text" );
  576. ok( !jQuery.acceptData( document.createDocumentFragment() ), "documentFragment" );
  577. });
  578. test("Check proper data removal of non-element descendants nodes (#8335)", 1, function() {
  579. var div = jQuery("<div>text</div>"),
  580. text = div.contents();
  581. text.data( "test", "test" ); // This should be a noop.
  582. div.remove();
  583. ok( !text.data("test"), "Be sure data is not stored in non-element" );
  584. });