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