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