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