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.acceptData", function() {
30 ok( jQuery.acceptData( document ), "document" );
31 ok( jQuery.acceptData( document.documentElement ), "documentElement" );
32 ok( jQuery.acceptData( {} ), "object" );
33 ok( !jQuery.acceptData( document.createElement("embed") ), "embed" );
34 ok( !jQuery.acceptData( document.createElement("applet") ), "applet" );
36 var flash = document.createElement("object");
37 flash.setAttribute("classid", "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000");
38 ok( jQuery.acceptData( flash ), "flash" );
40 var applet = document.createElement("object");
41 applet.setAttribute("classid", "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93");
42 ok( !jQuery.acceptData( applet ), "applet" );
45 test("jQuery.data", function() {
47 var div = document.createElement("div");
49 ok( jQuery.data(div, "test") === undefined, "Check for no data exists" );
51 jQuery.data(div, "test", "success");
52 equals( jQuery.data(div, "test"), "success", "Check for added data" );
54 ok( jQuery.data(div, "notexist") === undefined, "Check for no data exists" );
56 var data = jQuery.data(div);
57 same( data, { "test": "success" }, "Return complete data set" );
59 jQuery.data(div, "test", "overwritten");
60 equals( jQuery.data(div, "test"), "overwritten", "Check for overwritten data" );
62 jQuery.data(div, "test", undefined);
63 equals( jQuery.data(div, "test"), "overwritten", "Check that data wasn't removed");
65 jQuery.data(div, "test", null);
66 ok( jQuery.data(div, "test") === null, "Check for null data");
68 jQuery.data(div, "test3", "orig");
69 jQuery.data(div, { "test": "in", "test2": "in2" });
70 equals( jQuery.data(div, "test"), "in", "Verify setting an object in data." );
71 equals( jQuery.data(div, "test2"), "in2", "Verify setting an object in data." );
72 equals( jQuery.data(div, "test3"), "orig", "Verify original not overwritten." );
75 jQuery.data( obj, "prop", true );
77 ok( obj[ jQuery.expando ], "Data is being stored on the object." );
78 ok( obj[ jQuery.expando ]().prop, "Data is being stored on the object." );
80 equals( jQuery.data( obj, "prop" ), true, "Make sure the right value is retrieved." );
83 test(".data()", function() {
86 var div = jQuery("#foo");
87 strictEqual( div.data("foo"), undefined, "Make sure that missing result is undefined" );
89 div.data("test", "success");
90 same( div.data(), {test: "success"}, "data() get the entire data object" );
91 strictEqual( div.data("foo"), undefined, "Make sure that missing result is still undefined" );
93 var nodiv = jQuery("#unfound");
94 equals( nodiv.data(), null, "data() on empty set returns null" );
97 test(".data(String) and .data(String, Object)", function() {
99 var parent = jQuery("<div><div></div></div>"),
100 div = parent.children();
103 .bind("getData", function(){ ok( false, "getData bubbled." ) })
104 .bind("setData", function(){ ok( false, "setData bubbled." ) })
105 .bind("changeData", function(){ ok( false, "changeData bubbled." ) });
107 ok( div.data("test") === undefined, "Check for no data exists" );
109 div.data("test", "success");
110 equals( div.data("test"), "success", "Check for added data" );
112 div.data("test", "overwritten");
113 equals( div.data("test"), "overwritten", "Check for overwritten data" );
115 div.data("test", undefined);
116 equals( div.data("test"), "overwritten", "Check that data wasn't removed");
118 div.data("test", null);
119 ok( div.data("test") === null, "Check for null data");
121 ok( div.data("notexist") === undefined, "Check for no data exists" );
123 div.data("test", "overwritten");
124 var hits = {test:0}, gets = {test:0}, changes = {test:0, value:null};
127 function logChangeData(e,key,value) {
130 dataKey = dataKey + "." + e.namespace;
132 changes[key] += value;
133 changes.value = jQuery.data(e.target, dataKey);
137 .bind("setData",function(e,key,value){ hits[key] += value; })
138 .bind("setData.foo",function(e,key,value){ hits[key] += value; })
139 .bind("changeData",logChangeData)
140 .bind("changeData.foo",logChangeData)
141 .bind("getData",function(e,key){ gets[key] += 1; })
142 .bind("getData.foo",function(e,key){ gets[key] += 3; });
144 div.data("test.foo", 2);
145 equals( div.data("test"), "overwritten", "Check for original data" );
146 equals( div.data("test.foo"), 2, "Check for namespaced data" );
147 equals( div.data("test.bar"), "overwritten", "Check for unmatched namespace" );
148 equals( hits.test, 2, "Check triggered setter functions" );
149 equals( gets.test, 5, "Check triggered getter functions" );
150 equals( changes.test, 2, "Check sets raise changeData");
151 equals( changes.value, 2, "Check changeData after data has been set" );
156 changes.value = null;
159 equals( div.data("test"), 1, "Check for original data" );
160 equals( div.data("test.foo"), 2, "Check for namespaced data" );
161 equals( div.data("test.bar"), 1, "Check for unmatched namespace" );
162 equals( hits.test, 1, "Check triggered setter functions" );
163 equals( gets.test, 5, "Check triggered getter functions" );
164 equals( changes.test, 1, "Check sets raise changeData" );
165 equals( changes.value, 1, "Check changeData after data has been set" );
168 .bind("getData",function(e,key){ return key + "root"; })
169 .bind("getData.foo",function(e,key){ return key + "foo"; });
171 equals( div.data("test"), "testroot", "Check for original data" );
172 equals( div.data("test.foo"), "testfoo", "Check for namespaced data" );
173 equals( div.data("test.bar"), "testroot", "Check for unmatched namespace" );
176 var $elem = jQuery({});
177 equals( $elem.data('nothing'), undefined, "Non-existent data returns undefined");
178 equals( $elem.data('null',null).data('null'), null, "null's are preserved");
179 equals( $elem.data('emptyString','').data('emptyString'), '', "Empty strings are preserved");
180 equals( $elem.data('false',false).data('false'), false, "false's are preserved");
186 test("data-* attributes", function() {
188 var div = jQuery("<div>"),
189 child = jQuery("<div data-myobj='old data' data-ignored=\"DOM\"></div>");
191 equals( div.data("attr"), undefined, "Check for non-existing data-attr attribute" );
193 div.attr("data-attr", "exists");
194 equals( div.data("attr"), "exists", "Check for existing data-attr attribute" );
196 div.data("attr", "internal").attr("data-attr", "external");
197 equals( div.data("attr"), "internal", "Check for .data('attr') precedence (internal > external data-* attribute)" );
199 child.appendTo('#main');
200 equals( child.data("myobj"), "old data", "Value accessed from data-* attribute");
202 child.data("myobj", "replaced");
203 equals( child.data("myobj"), "replaced", "Original data overwritten");
205 child.data("ignored", "cache");
206 equals( child.data("ignored"), "cache", "Cached data used before DOM data-* fallback");
209 .attr("data-true", "true")
210 .attr("data-false", "false")
211 .attr("data-five", "5")
212 .attr("data-point", "5.5")
213 .attr("data-pointe", "5.5E3")
214 .attr("data-pointbad", "5..5")
215 .attr("data-pointbad2", "-.")
216 .attr("data-badjson", "{123}")
217 .attr("data-badjson2", "[abc]")
218 .attr("data-empty", "")
219 .attr("data-space", " ")
220 .attr("data-null", "null")
221 .attr("data-string", "test");
223 strictEqual( child.data('true'), true, "Primitive true read from attribute");
224 strictEqual( child.data('false'), false, "Primitive false read from attribute");
225 strictEqual( child.data('five'), 5, "Primitive number read from attribute");
226 strictEqual( child.data('point'), 5.5, "Primitive number read from attribute");
227 strictEqual( child.data('pointe'), 5500, "Primitive number read from attribute");
228 strictEqual( child.data('pointbad'), "5..5", "Bad number read from attribute");
229 strictEqual( child.data('pointbad2'), "-.", "Bad number read from attribute");
230 strictEqual( child.data('badjson'), "{123}", "Bad number read from attribute");
231 strictEqual( child.data('badjson2'), "[abc]", "Bad number read from attribute");
232 strictEqual( child.data('empty'), "", "Empty string read from attribute");
233 strictEqual( child.data('space'), " ", "Empty string read from attribute");
234 strictEqual( child.data('null'), null, "Primitive null read from attribute");
235 strictEqual( child.data('string'), "test", "Typical string read from attribute");
239 // tests from metadata plugin
240 function testData(index, elem) {
243 equals(jQuery(elem).data("foo"), "bar", "Check foo property");
244 equals(jQuery(elem).data("bar"), "baz", "Check baz property");
247 equals(jQuery(elem).data("test"), "bar", "Check test property");
248 equals(jQuery(elem).data("bar"), "baz", "Check bar property");
251 equals(jQuery(elem).data("zoooo"), "bar", "Check zoooo property");
252 same(jQuery(elem).data("bar"), {"test":"baz"}, "Check bar property");
255 equals(jQuery(elem).data("number"), true, "Check number property");
256 same(jQuery(elem).data("stuff"), [2,8], "Check stuff property");
259 ok(false, ["Assertion failed on index ", index, ", with data ", data].join(''));
263 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>',
264 elem = jQuery(metadata).appendTo('#main');
266 elem.find("li").each(testData);
270 test(".data(Object)", function() {
273 var div = jQuery("<div/>");
275 div.data({ "test": "in", "test2": "in2" });
276 equals( div.data("test"), "in", "Verify setting an object in data." );
277 equals( div.data("test2"), "in2", "Verify setting an object in data." );
280 test("jQuery.removeData", function() {
282 var div = jQuery("#foo")[0];
283 jQuery.data(div, "test", "testing");
284 jQuery.removeData(div, "test");
285 equals( jQuery.data(div, "test"), undefined, "Check removal of data" );
288 test(".removeData()", function() {
290 var div = jQuery("#foo");
291 div.data("test", "testing");
292 div.removeData("test");
293 equals( div.data("test"), undefined, "Check removal of data" );
295 div.data("test", "testing");
296 div.data("test.foo", "testing2");
297 div.removeData("test.bar");
298 equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
299 equals( div.data("test"), "testing", "Make sure data is intact" );
301 div.removeData("test");
302 equals( div.data("test.foo"), "testing2", "Make sure data is intact" );
303 equals( div.data("test"), undefined, "Make sure data is intact" );
305 div.removeData("test.foo");
306 equals( div.data("test.foo"), undefined, "Make sure data is intact" );