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