3 test("expando", function(){
6 equals("expando" in jQuery, true, "jQuery is exposing the expando");
10 equals( jQuery.expando in obj, true, "jQuery.data adds an expando to the object" );
11 equals( typeof obj[jQuery.expando], "function", "jQuery.data adds an expando to the object as a function" );
14 jQuery.data(obj, 'test');
15 equals( jQuery.expando in obj, false, "jQuery.data did not add an expando to the object" );
18 jQuery.data(obj, "foo", "bar");
19 equals( jQuery.expando in obj, true, "jQuery.data added an expando to the object" );
21 var id = obj[jQuery.expando]();
22 equals( id in jQuery.cache, false, "jQuery.data did not add an entry to jQuery.cache" );
24 equals( id.foo, "bar", "jQuery.data worked correctly" );
27 test("jQuery.data", function() {
29 var div = document.createElement("div");
31 ok( jQuery.data(div, "test") === undefined, "Check for no data exists" );
33 jQuery.data(div, "test", "success");
34 equals( jQuery.data(div, "test"), "success", "Check for added data" );
36 ok( jQuery.data(div, "notexist") === undefined, "Check for no data exists" );
38 var data = jQuery.data(div);
39 same( data, { "test": "success" }, "Return complete data set" );
41 jQuery.data(div, "test", "overwritten");
42 equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );
44 jQuery.data(div, "test", undefined);
45 equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");
47 jQuery.data(div, "test", null);
48 ok( jQuery.data(div, "test") === null, "Check for null data");
50 jQuery.data(div, "test3", "orig");
51 jQuery.data(div, { "test": "in", "test2": "in2" });
52 equals( jQuery.data(div, "test"), "in", "Verify setting an object in data." );
53 equals( jQuery.data(div, "test2"), "in2", "Verify setting an object in data." );
54 equals( jQuery.data(div, "test3"), "orig", "Verify original not overwritten." );
57 jQuery.data( obj, "prop", true );
59 ok( obj[ jQuery.expando ], "Data is being stored on the object." );
60 ok( obj[ jQuery.expando ]().prop, "Data is being stored on the object." );
62 equals( jQuery.data( obj, "prop" ), true, "Make sure the right value is retrieved." );
65 test(".data()", function() {
68 var div = jQuery("#foo");
69 div.data("test", "success");
70 same( div.data(), {test: "success"}, "data() get the entire data object" );
72 var nodiv = jQuery("#unfound");
73 equals( nodiv.data(), null, "data() on empty set returns null" );
76 test(".data(String) and .data(String, Object)", function() {
78 var parent = jQuery("<div><div></div></div>"),
79 div = parent.children();
82 .bind("getData", function(){ ok( false, "getData bubbled." ) })
83 .bind("setData", function(){ ok( false, "setData bubbled." ) })
84 .bind("changeData", function(){ ok( false, "changeData bubbled." ) });
86 ok( div.data("test") === undefined, "Check for no data exists" );
88 div.data("test", "success");
89 equals( div.data("test"), "success", "Check for added data" );
91 div.data("test", "overwritten");
92 equals( div.data("test"), "overwritten", "Check for overwritten data" );
94 div.data("test", undefined);
95 equals( div.data("test"), "overwritten", "Check that data wasn't removed");
97 div.data("test", null);
98 ok( div.data("test") === null, "Check for null data");
100 ok( div.data("notexist") === undefined, "Check for no data exists" );
102 div.data("test", "overwritten");
103 var hits = {test:0}, gets = {test:0}, changes = {test:0, value:null};
106 function logChangeData(e,key,value) {
109 dataKey = dataKey + "." + e.namespace;
111 changes[key] += value;
112 changes.value = jQuery.data(e.target, dataKey);
116 .bind("setData",function(e,key,value){ hits[key] += value; })
117 .bind("setData.foo",function(e,key,value){ hits[key] += value; })
118 .bind("changeData",logChangeData)
119 .bind("changeData.foo",logChangeData)
120 .bind("getData",function(e,key){ gets[key] += 1; })
121 .bind("getData.foo",function(e,key){ gets[key] += 3; });
123 div.data("test.foo", 2);
124 equals( div.data("test"), "overwritten", "Check for original data" );
125 equals( div.data("test.foo"), 2, "Check for namespaced data" );
126 equals( div.data("test.bar"), "overwritten", "Check for unmatched namespace" );
127 equals( hits.test, 2, "Check triggered setter functions" );
128 equals( gets.test, 5, "Check triggered getter functions" );
129 equals( changes.test, 2, "Check sets raise changeData");
130 equals( changes.value, 2, "Check changeData after data has been set" );
135 changes.value = null;
138 equals( div.data("test"), 1, "Check for original data" );
139 equals( div.data("test.foo"), 2, "Check for namespaced data" );
140 equals( div.data("test.bar"), 1, "Check for unmatched namespace" );
141 equals( hits.test, 1, "Check triggered setter functions" );
142 equals( gets.test, 5, "Check triggered getter functions" );
143 equals( changes.test, 1, "Check sets raise changeData" );
144 equals( changes.value, 1, "Check changeData after data has been set" );
147 .bind("getData",function(e,key){ return key + "root"; })
148 .bind("getData.foo",function(e,key){ return key + "foo"; });
150 equals( div.data("test"), "testroot", "Check for original data" );
151 equals( div.data("test.foo"), "testfoo", "Check for namespaced data" );
152 equals( div.data("test.bar"), "testroot", "Check for unmatched namespace" );
155 var $elem = jQuery({});
156 equals( $elem.data('nothing'), undefined, "Non-existent data returns undefined");
157 equals( $elem.data('null',null).data('null'), null, "null's are preserved");
158 equals( $elem.data('emptyString','').data('emptyString'), '', "Empty strings are preserved");
159 equals( $elem.data('false',false).data('false'), false, "false's are preserved");
165 test("data-* attributes", function() {
167 var div = jQuery("<div>"),
168 child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\"></div>");
170 equals( div.data("attr"), undefined, "Check for non-existing data-attr attribute" );
172 div.attr("data-attr", "exists");
173 equals( div.data("attr"), "exists", "Check for existing data-attr attribute" );
175 div.data("attr", "internal").attr("data-attr", "external");
176 equals( div.data("attr"), "internal", "Check for .data('attr') precedence (internal > external data-* attribute)" );
178 child.appendTo('#main');
179 equals( child.data("myobj"), "old data", "Value accessed from data-* attribute");
181 child.data("myobj", "replaced");
182 equals( child.data("myobj"), "replaced", "Original data overwritten");
184 child.data("ignored", "cache");
185 equals( child.data("ignored"), "cache", "Cached data used before DOM data-* fallback");
188 .attr("data-true", "true")
189 .attr("data-false", "false")
190 .attr("data-five", "5")
191 .attr("data-point", "5.5")
192 .attr("data-pointe", "5.5E3")
193 .attr("data-pointbad", "5..5")
194 .attr("data-pointbad2", "-.")
195 .attr("data-badjson", "{123}")
196 .attr("data-badjson2", "[abc]")
197 .attr("data-empty", "")
198 .attr("data-space", " ")
199 .attr("data-null", "null")
200 .attr("data-string", "test");
202 strictEqual( child.data('true'), true, "Primitive true read from attribute");
203 strictEqual( child.data('false'), false, "Primitive false read from attribute");
204 strictEqual( child.data('five'), 5, "Primitive number read from attribute");
205 strictEqual( child.data('point'), 5.5, "Primitive number read from attribute");
206 strictEqual( child.data('pointe'), 5500, "Primitive number read from attribute");
207 strictEqual( child.data('pointbad'), "5..5", "Bad number read from attribute");
208 strictEqual( child.data('pointbad2'), "-.", "Bad number read from attribute");
209 strictEqual( child.data('badjson'), "{123}", "Bad number read from attribute");
210 strictEqual( child.data('badjson2'), "[abc]", "Bad number read from attribute");
211 strictEqual( child.data('empty'), "", "Empty string read from attribute");
212 strictEqual( child.data('space'), " ", "Empty string read from attribute");
213 strictEqual( child.data('null'), null, "Primitive null read from attribute");
214 strictEqual( child.data('string'), "test", "Typical string read from attribute");
218 // tests from metadata plugin
219 function testData(index, elem) {
222 equals(jQuery(elem).data("foo"), "bar", "Check foo property");
223 equals(jQuery(elem).data("bar"), "baz", "Check baz property");
226 equals(jQuery(elem).data("test"), "bar", "Check test property");
227 equals(jQuery(elem).data("bar"), "baz", "Check bar property");
230 equals(jQuery(elem).data("zoooo"), "bar", "Check zoooo property");
231 same(jQuery(elem).data("bar"), {"test":"baz"}, "Check bar property");
234 equals(jQuery(elem).data("number"), true, "Check number property");
235 same(jQuery(elem).data("stuff"), [2,8], "Check stuff property");
238 ok(false, ["Assertion failed on index ", index, ", with data ", data].join(''));
242 var 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>',
243 elem = jQuery(metadata).appendTo('#main');
245 elem.find("li").each(testData);
249 test(".data(Object)", function() {
252 var div = jQuery("<div/>");
254 div.data({ "test": "in", "test2": "in2" });
255 equals( div.data("test"), "in", "Verify setting an object in data." );
256 equals( div.data("test2"), "in2", "Verify setting an object in data." );
259 test("jQuery.removeData", function() {
261 var div = jQuery("#foo")[0];
262 jQuery.data(div, "test", "testing");
263 jQuery.removeData(div, "test");
264 equals( jQuery.data(div, "test"), undefined, "Check removal of data" );
267 test(".removeData()", function() {
269 var div = jQuery("#foo");
270 div.data("test", "testing");
271 div.removeData("test");
272 equals( div.data("test"), undefined, "Check removal of data" );
274 div.data("test", "testing");
275 div.data("test.foo", "testing2");
276 div.removeData("test.bar");
277 equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
278 equals( div.data("test"), "testing", "Make sure data is intact" );
280 div.removeData("test");
281 equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
282 equals( div.data("test"), undefined, "Make sure data is intact" );
284 div.removeData("test.foo");
285 equals( div.data("test.foo"), undefined, "Make sure data is intact" );