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