Added tests for attribute function setters.
[jquery.git] / test / unit / attributes.js
1 module("attributes");
2
3 var bareObj = function(value) { return value; };
4 var functionReturningObj = function(value) { return (function() { return value; }); };
5
6 test("attr(String)", function() {
7         expect(28);
8
9         // This one sometimes fails randomly ?!
10         equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
11         
12         equals( jQuery('#text1').attr('value', "Test2").attr('defaultValue'), "Test", 'Check for defaultValue attribute' );
13         equals( jQuery('#text1').attr('type'), "text", 'Check for type attribute' );
14         equals( jQuery('#radio1').attr('type'), "radio", 'Check for type attribute' );
15         equals( jQuery('#check1').attr('type'), "checkbox", 'Check for type attribute' );
16         equals( jQuery('#simon1').attr('rel'), "bookmark", 'Check for rel attribute' );
17         equals( jQuery('#google').attr('title'), "Google!", 'Check for title attribute' );
18         equals( jQuery('#mark').attr('hreflang'), "en", 'Check for hreflang attribute' );
19         equals( jQuery('#en').attr('lang'), "en", 'Check for lang attribute' );
20         equals( jQuery('#simon').attr('class'), "blog link", 'Check for class attribute' );
21         equals( jQuery('#name').attr('name'), "name", 'Check for name attribute' );
22         equals( jQuery('#text1').attr('name'), "action", 'Check for name attribute' );
23         ok( jQuery('#form').attr('action').indexOf("formaction") >= 0, 'Check for action attribute' );
24         // Temporarily disabled. See: #4299
25         // ok( jQuery('#form').attr('action','newformaction').attr('action').indexOf("newformaction") >= 0, 'Check that action attribute was changed' );
26         equals( jQuery('#text1').attr('maxlength'), '30', 'Check for maxlength attribute' );
27         equals( jQuery('#text1').attr('maxLength'), '30', 'Check for maxLength attribute' );
28         equals( jQuery('#area1').attr('maxLength'), '30', 'Check for maxLength attribute' );
29         equals( jQuery('#select2').attr('selectedIndex'), 3, 'Check for selectedIndex attribute' );
30         equals( jQuery('#foo').attr('nodeName').toUpperCase(), 'DIV', 'Check for nodeName attribute' );
31         equals( jQuery('#foo').attr('tagName').toUpperCase(), 'DIV', 'Check for tagName attribute' );
32
33         jQuery('<a id="tAnchor5"></a>').attr('href', '#5').appendTo('#main'); // using innerHTML in IE causes href attribute to be serialized to the full path
34         equals( jQuery('#tAnchor5').attr('href'), "#5", 'Check for non-absolute href (an anchor)' );
35
36         equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
37
38
39         // Related to [5574] and [5683]
40         var body = document.body, $body = jQuery(body);
41
42         ok( $body.attr('foo') === undefined, 'Make sure that a non existent attribute returns undefined' );
43         ok( $body.attr('nextSibling') === null, 'Make sure a null expando returns null' );
44
45         body.setAttribute('foo', 'baz');
46         equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
47
48         body.foo = 'bar';
49         equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
50
51         $body.attr('foo','cool');
52         equals( $body.attr('foo'), 'cool', 'Make sure that setting works well when both expando and dom attribute are available' );
53
54         body.foo = undefined;
55         ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
56
57         body.removeAttribute('foo'); // Cleanup
58
59         var select = document.createElement("select"), optgroup = document.createElement("optgroup"), option = document.createElement("option");
60         optgroup.appendChild( option );
61         select.appendChild( optgroup );
62
63         equals( jQuery(option).attr("selected"), true, "Make sure that a single option is selected, even when in an optgroup." );
64 });
65
66 if ( !isLocal ) {
67         test("attr(String) in XML Files", function() {
68                 expect(2);
69                 stop();
70                 jQuery.get("data/dashboard.xml", function(xml) {
71                         equals( jQuery("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );
72                         equals( jQuery("location", xml).attr("for"), "bar", "Check for attribute in XML document" );
73                         start();
74                 });
75         });
76 }
77
78 test("attr(String, Function)", function() {
79         expect(2);
80         equals( jQuery('#text1').attr('value', function() { return this.id ;})[0].value, "text1", "Set value from id" );
81         equals( jQuery('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");
82 });
83
84 test("attr(Hash)", function() {
85         expect(3);
86         var pass = true;
87         jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
88                 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
89         });
90         ok( pass, "Set Multiple Attributes" );
91                          equals( jQuery('#text1').attr({'value': function() { return this.id; }})[0].value, "text1", "Set attribute to computed value #1" );
92                          equals( jQuery('#text1').attr({'title': function(i) { return i; }}).attr('title'), "0", "Set attribute to computed value #2");
93
94 });
95
96 test("attr(String, Object)", function() {
97         expect(23);
98         var div = jQuery("div").attr("foo", "bar"),
99                 fail = false;
100         for ( var i = 0; i < div.size(); i++ ) {
101                 if ( div.get(i).getAttribute('foo') != "bar" ){
102                         fail = i;
103                         break;
104                 }
105         }
106         equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
107
108         // Fails on IE since recent changes to .attr()
109         // ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
110
111         jQuery("#name").attr('name', 'something');
112         equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
113         jQuery("#check2").attr('checked', true);
114         equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
115         jQuery("#check2").attr('checked', false);
116         equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
117         jQuery("#text1").attr('readonly', true);
118         equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
119         jQuery("#text1").attr('readonly', false);
120         equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
121         jQuery("#name").attr('maxlength', '5');
122         equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
123         jQuery("#name").attr('maxLength', '10');
124         equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
125
126         var table = jQuery('#table').append("<tr><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr><tr><td>cell</td><td>cell</td></tr>"),
127                 td = table.find('td:first');
128         td.attr("rowspan", "2");
129         equals( td[0].rowSpan, 2, "Check rowspan is correctly set" );
130         td.attr("colspan", "2");
131         equals( td[0].colSpan, 2, "Check colspan is correctly set" );
132         table.attr("cellspacing", "2");
133         equals( table[0].cellSpacing, 2, "Check cellspacing is correctly set" );
134
135         // for #1070
136         jQuery("#name").attr('someAttr', '0');
137         equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
138         jQuery("#name").attr('someAttr', 0);
139         equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
140         jQuery("#name").attr('someAttr', 1);
141         equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
142
143         // using contents will get comments regular, text, and comment nodes
144         var j = jQuery("#nonnodes").contents();
145
146         j.attr("name", "attrvalue");
147         equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
148         j.removeAttr("name");
149
150         reset();
151
152         var type = jQuery("#check2").attr('type');
153         var thrown = false;
154         try {
155                 jQuery("#check2").attr('type','hidden');
156         } catch(e) {
157                 thrown = true;
158         }
159         ok( thrown, "Exception thrown when trying to change type property" );
160         equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
161
162         var check = document.createElement("input");
163         var thrown = true;
164         try {
165                 jQuery(check).attr('type','checkbox');
166         } catch(e) {
167                 thrown = false;
168         }
169         ok( thrown, "Exception thrown when trying to change type property" );
170         equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
171
172         var check = jQuery("<input />");
173         var thrown = true;
174         try {
175                 check.attr('type','checkbox');
176         } catch(e) {
177                 thrown = false;
178         }
179         ok( thrown, "Exception thrown when trying to change type property" );
180         equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
181
182         var button = jQuery("#button");
183         var thrown = false;
184         try {
185                 button.attr('type','submit');
186         } catch(e) {
187                 thrown = true;
188         }
189         ok( thrown, "Exception thrown when trying to change type property" );
190         equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
191 });
192
193 test("attr(jquery_method)", function(){
194         expect(7);
195         
196         var $elem = jQuery("<div />"),
197                 elem = $elem[0];
198         
199         // one at a time        
200         $elem.attr({'html': 'foo'}, true);
201         equals( elem.innerHTML, 'foo', 'attr(html)');
202         
203         $elem.attr({'text': 'bar'}, true);
204         equals( elem.innerHTML, 'bar', 'attr(text)');
205         
206         $elem.attr({'css': {color:'red'}}, true);
207         ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
208         
209         $elem.attr({'height': 10}, true);
210         equals( elem.style.height, '10px', 'attr(height)');
211         
212         // Multiple attributes
213         
214         $elem.attr({
215                 width:10,
216                 css:{ paddingLeft:1, paddingRight:1 }
217         }, true);
218         
219         equals( elem.style.width, '10px', 'attr({...})');
220         equals( elem.style.paddingLeft, '1px', 'attr({...})');
221         equals( elem.style.paddingRight, '1px', 'attr({...})');
222 });
223
224 if ( !isLocal ) {
225         test("attr(String, Object) - Loaded via XML document", function() {
226                 expect(2);
227                 stop();
228                 jQuery.get('data/dashboard.xml', function(xml) {
229                         var titles = [];
230                         jQuery('tab', xml).each(function() {
231                                 titles.push(jQuery(this).attr('title'));
232                         });
233                         equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
234                         equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
235                         start();
236                 });
237         });
238 }
239
240 test("attr('tabindex')", function() {
241         expect(8);
242
243         // elements not natively tabbable
244         equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
245         equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
246
247         // anchor with href
248         equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
249         equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
250         equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
251
252         // anchor without href
253         equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
254         equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
255         equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
256 });
257
258 test("attr('tabindex', value)", function() {
259         expect(9);
260
261         var element = jQuery('#divWithNoTabIndex');
262         equals(element.attr('tabindex'), undefined, 'start with no tabindex');
263
264         // set a positive string
265         element.attr('tabindex', '1');
266         equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
267
268         // set a zero string
269         element.attr('tabindex', '0');
270         equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
271
272         // set a negative string
273         element.attr('tabindex', '-1');
274         equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
275
276         // set a positive number
277         element.attr('tabindex', 1);
278         equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
279
280         // set a zero number
281         element.attr('tabindex', 0);
282         equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
283
284         // set a negative number
285         element.attr('tabindex', -1);
286         equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
287
288         element = jQuery('#linkWithTabIndex');
289         equals(element.attr('tabindex'), 2, 'start with tabindex 2');
290
291         element.attr('tabindex', -1);
292         equals(element.attr('tabindex'), -1, 'set negative tabindex');
293 });
294
295 test("removeAttr(String)", function() {
296         expect(1);
297         equals( jQuery('#mark').removeAttr( "class" )[0].className, "", "remove class" );
298 });
299
300 test("val()", function() {
301         expect(17);
302
303         document.getElementById('text1').value = "bla";
304         equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
305
306         reset();
307
308         equals( jQuery("#text1").val(), "Test", "Check for value of input element" );
309         // ticket #1714 this caused a JS error in IE
310         equals( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
311         ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
312
313         equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );
314
315         same( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );
316
317         equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );
318
319         equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );
320
321         equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
322
323         equals( jQuery('#option3a').val(), '', 'Call val() on a option element with no value attribute' );
324
325         jQuery('#select3').val("");
326         same( jQuery('#select3').val(), [''], 'Call val() on a multiple="multiple" select' );
327
328         var checks = jQuery("<input type='checkbox' name='test' value='1'/>").appendTo("#form")
329                 .add( jQuery("<input type='checkbox' name='test' value='2'/>").appendTo("#form") )
330                 .add( jQuery("<input type='checkbox' name='test' value=''/>").appendTo("#form") )
331                 .add( jQuery("<input type='checkbox' name='test'/>").appendTo("#form") );
332
333         same( checks.serialize(), "", "Get unchecked values." );
334
335         equals( checks.eq(3).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
336
337         checks.val([ "2" ]);
338         same( checks.serialize(), "test=2", "Get a single checked value." );
339
340         checks.val([ "1", "" ]);
341         same( checks.serialize(), "test=1&test=", "Get multiple checked values." );
342
343         checks.val([ "", "2" ]);
344         same( checks.serialize(), "test=2&test=", "Get multiple checked values." );
345
346         checks.val([ "1", "on" ]);
347         same( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
348
349         checks.remove();
350 });
351
352 var testVal = function(valueObj) {
353         expect(6);
354
355         jQuery("#text1").val(valueObj( 'test' ));
356         equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
357
358         jQuery("#text1").val(valueObj( 67 ));
359         equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
360
361         jQuery("#select1").val(valueObj( "3" ));
362         equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
363
364         jQuery("#select1").val(valueObj( 2 ));
365         equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
366
367         jQuery("#select1").append("<option value='4'>four</option>");
368         jQuery("#select1").val(valueObj( 4 ));
369         equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
370
371         // using contents will get comments regular, text, and comment nodes
372         var j = jQuery("#nonnodes").contents();
373         j.val(valueObj( "asdf" ));
374         equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
375         j.removeAttr("value");
376 }
377
378 test("val(String/Number)", function() {
379         testVal(bareObj);
380 });
381
382 test("val(Function)", function() {
383         testVal(functionReturningObj);
384 })
385
386 test("val(Function) with incoming value", function() {
387         expect(10);
388
389         var oldVal = jQuery("#text1").val();
390
391         jQuery("#text1").val(function(i, val) {
392                 equals( val, oldVal, "Make sure the incoming value is correct." );
393                 return "test";
394         });
395
396         equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
397
398         oldVal = jQuery("#text1").val();
399
400         jQuery("#text1").val(function(i, val) {
401                 equals( val, oldVal, "Make sure the incoming value is correct." );
402                 return 67;
403         });
404
405         equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
406
407         oldVal = jQuery("#select1").val();
408
409         jQuery("#select1").val(function(i, val) {
410                 equals( val, oldVal, "Make sure the incoming value is correct." );
411                 return "3";
412         });
413
414         equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
415
416         oldVal = jQuery("#select1").val();
417
418         jQuery("#select1").val(function(i, val) {
419                 equals( val, oldVal, "Make sure the incoming value is correct." );
420                 return 2;
421         });
422
423         equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
424
425         jQuery("#select1").append("<option value='4'>four</option>");
426
427         oldVal = jQuery("#select1").val();
428
429         jQuery("#select1").val(function(i, val) {
430                 equals( val, oldVal, "Make sure the incoming value is correct." );
431                 return 4;
432         });
433
434         equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
435 });
436
437 var testAddClass = function(valueObj) {
438         expect(2);
439         var div = jQuery("div");
440         div.addClass( valueObj("test") );
441         var pass = true;
442         for ( var i = 0; i < div.size(); i++ ) {
443          if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
444         }
445         ok( pass, "Add Class" );
446
447         // using contents will get regular, text, and comment nodes
448         var j = jQuery("#nonnodes").contents();
449         j.addClass( valueObj("asdf") );
450         ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
451 };
452
453 test("addClass(String)", function() {
454         testAddClass(bareObj);
455 });
456
457 test("addClass(Function)", function() {
458         testAddClass(functionReturningObj);
459 });
460
461 test("addClass(Function) with incoming value", function() {
462         expect(39);
463
464         var div = jQuery("div"), old = div.map(function(){
465                 return jQuery(this).attr("class");
466         });
467
468         div.addClass(function(i, val) {
469                 equals( val, old[i], "Make sure the incoming value is correct." );
470                 return "test";
471         });
472
473         var pass = true;
474         for ( var i = 0; i < div.size(); i++ ) {
475          if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
476         }
477         ok( pass, "Add Class" );
478 });
479
480 var testRemoveClass = function(valueObj) {
481         expect(5);
482
483         var $divs = jQuery('div');
484
485         $divs.addClass("test").removeClass( valueObj("test") );
486
487         ok( !$divs.is('.test'), "Remove Class" );
488
489         reset();
490         $divs = jQuery('div');
491
492         $divs.addClass("test").addClass("foo").addClass("bar");
493         $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
494
495         ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
496
497         reset();
498         $divs = jQuery('div');
499
500         // Make sure that a null value doesn't cause problems
501         $divs.eq(0).addClass("test").removeClass( valueObj(null) );
502         ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
503
504         $divs.eq(0).addClass("test").removeClass( valueObj("") );
505         ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
506
507         // using contents will get regular, text, and comment nodes
508         var j = jQuery("#nonnodes").contents();
509         j.removeClass( valueObj("asdf") );
510         ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
511 };
512
513 test("removeClass(String) - simple", function() {
514         testRemoveClass(bareObj);
515 });
516
517 test("removeClass(Function) - simple", function() {
518         testRemoveClass(functionReturningObj);
519 });
520
521 test("removeClass(Function) with incoming value", function() {
522         expect(39);
523
524         var $divs = jQuery('div').addClass("test"), old = $divs.map(function(){
525                 return jQuery(this).attr("class");
526         });
527
528         $divs.removeClass(function(i, val) {
529                 equals( val, old[i], "Make sure the incoming value is correct." );
530                 return "test";
531         });
532
533         ok( !$divs.is('.test'), "Remove Class" );
534
535         reset();        
536 });
537
538 var testToggleClass = function(valueObj) {
539         expect(17);
540
541         var e = jQuery("#firstp");
542         ok( !e.is(".test"), "Assert class not present" );
543         e.toggleClass( valueObj("test") );
544         ok( e.is(".test"), "Assert class present" );
545         e.toggleClass( valueObj("test") );
546         ok( !e.is(".test"), "Assert class not present" );
547
548         // class name with a boolean
549         e.toggleClass( valueObj("test"), false );
550         ok( !e.is(".test"), "Assert class not present" );
551         e.toggleClass( valueObj("test"), true );
552         ok( e.is(".test"), "Assert class present" );
553         e.toggleClass( valueObj("test"), false );
554         ok( !e.is(".test"), "Assert class not present" );
555
556         // multiple class names
557         e.addClass("testA testB");
558         ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
559         e.toggleClass( valueObj("testB testC") );
560         ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
561         e.toggleClass( valueObj("testA testC") );
562         ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
563
564         // toggleClass storage
565         e.toggleClass(true);
566         ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
567         e.addClass("testD testE");
568         ok( e.is(".testD.testE"), "Assert class present" );
569         e.toggleClass();
570         ok( !e.is(".testD.testE"), "Assert class not present" );
571         ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
572         e.toggleClass();
573         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
574         e.toggleClass(false);
575         ok( !e.is(".testD.testE"), "Assert class not present" );
576         e.toggleClass(true);
577         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
578         e.toggleClass();
579         e.toggleClass(false);
580         e.toggleClass();
581         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
582
583
584
585         // Cleanup
586         e.removeClass("testD");
587         e.removeData('__className__');
588 };
589
590 test("toggleClass(String|boolean|undefined[, boolean])", function() {
591         testToggleClass(bareObj);
592 });
593
594 test("toggleClass(Function[, boolean])", function() {
595         testToggleClass(functionReturningObj);
596 });
597
598 test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
599         expect(14);
600
601         var e = jQuery("#firstp"), old = e.attr("class");
602         ok( !e.is(".test"), "Assert class not present" );
603         
604         e.toggleClass(function(i, val) {
605                 equals( val, old, "Make sure the incoming value is correct." );
606                 return "test";
607         });
608         ok( e.is(".test"), "Assert class present" );
609         
610         old = e.attr("class");
611         
612         e.toggleClass(function(i, val) {
613                 equals( val, old, "Make sure the incoming value is correct." );
614                 return "test";
615         });
616         ok( !e.is(".test"), "Assert class not present" );
617         
618         old = e.attr("class");
619
620         // class name with a boolean
621         e.toggleClass(function(i, val, state) {
622                 equals( val, old, "Make sure the incoming value is correct." );
623                 equals( state, false, "Make sure that the state is passed in." );
624                 return "test";
625         }, false );
626         ok( !e.is(".test"), "Assert class not present" );
627         
628         old = e.attr("class");
629         
630         e.toggleClass(function(i, val, state) {
631                 equals( val, old, "Make sure the incoming value is correct." );
632                 equals( state, true, "Make sure that the state is passed in." );
633                 return "test";
634         }, true );
635         ok( e.is(".test"), "Assert class present" );
636         
637         old = e.attr("class");
638         
639         e.toggleClass(function(i, val, state) {
640                 equals( val, old, "Make sure the incoming value is correct." );
641                 equals( state, false, "Make sure that the state is passed in." );
642                 return "test";
643         }, false );
644         ok( !e.is(".test"), "Assert class not present" );
645
646         // Cleanup
647         e.removeClass("test");
648         e.removeData('__className__');
649 });
650
651 test("addClass, removeClass, hasClass", function() {
652         expect(14);
653  
654         var jq = jQuery("<p>Hi</p>"), x = jq[0];
655  
656         jq.addClass("hi");
657         equals( x.className, "hi", "Check single added class" );
658  
659         jq.addClass("foo bar");
660         equals( x.className, "hi foo bar", "Check more added classes" );
661  
662         jq.removeClass();
663         equals( x.className, "", "Remove all classes" );
664  
665         jq.addClass("hi foo bar");
666         jq.removeClass("foo");
667         equals( x.className, "hi bar", "Check removal of one class" );
668  
669         ok( jq.hasClass("hi"), "Check has1" );
670         ok( jq.hasClass("bar"), "Check has2" );
671  
672         var jq = jQuery("<p class='class1\nclass2\tcla.ss3\n'></p>");
673         ok( jq.hasClass("class1"), "Check hasClass with carriage return" );
674         ok( jq.is(".class1"), "Check is with carriage return" );
675         ok( jq.hasClass("class2"), "Check hasClass with tab" );
676         ok( jq.is(".class2"), "Check is with tab" );
677         ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" );
678  
679         jq.removeClass("class2");
680         ok( jq.hasClass("class2")==false, "Check the class has been properly removed" );
681         jq.removeClass("cla");
682         ok( jq.hasClass("cla.ss3"), "Check the dotted class has not been removed" );
683         jq.removeClass("cla.ss3");
684         ok( jq.hasClass("cla.ss3")==false, "Check the dotted class has been removed" );
685 });