If .attr() is run on no elements undefined should be returned (as should be the case...
[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         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         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'/>").appendTo("#form")
332                 .add( jQuery("<input type='checkbox' name='test' value='2'/>").appendTo("#form") )
333                 .add( jQuery("<input type='checkbox' name='test' value=''/>").appendTo("#form") )
334                 .add( jQuery("<input type='checkbox' name='test'/>").appendTo("#form") );
335
336         same( checks.serialize(), "", "Get unchecked values." );
337
338         equals( checks.eq(3).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
339
340         checks.val([ "2" ]);
341         same( checks.serialize(), "test=2", "Get a single checked value." );
342
343         checks.val([ "1", "" ]);
344         same( checks.serialize(), "test=1&test=", "Get multiple checked values." );
345
346         checks.val([ "", "2" ]);
347         same( checks.serialize(), "test=2&test=", "Get multiple checked values." );
348
349         checks.val([ "1", "on" ]);
350         same( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
351
352         checks.remove();
353 });
354
355 var testVal = function(valueObj) {
356         expect(6);
357
358         jQuery("#text1").val(valueObj( 'test' ));
359         equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
360
361         jQuery("#text1").val(valueObj( 67 ));
362         equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
363
364         jQuery("#select1").val(valueObj( "3" ));
365         equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
366
367         jQuery("#select1").val(valueObj( 2 ));
368         equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
369
370         jQuery("#select1").append("<option value='4'>four</option>");
371         jQuery("#select1").val(valueObj( 4 ));
372         equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
373
374         // using contents will get comments regular, text, and comment nodes
375         var j = jQuery("#nonnodes").contents();
376         j.val(valueObj( "asdf" ));
377         equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
378         j.removeAttr("value");
379 }
380
381 test("val(String/Number)", function() {
382         testVal(bareObj);
383 });
384
385 test("val(Function)", function() {
386         testVal(functionReturningObj);
387 })
388
389 test("val(Function) with incoming value", function() {
390         expect(10);
391
392         var oldVal = jQuery("#text1").val();
393
394         jQuery("#text1").val(function(i, val) {
395                 equals( val, oldVal, "Make sure the incoming value is correct." );
396                 return "test";
397         });
398
399         equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
400
401         oldVal = jQuery("#text1").val();
402
403         jQuery("#text1").val(function(i, val) {
404                 equals( val, oldVal, "Make sure the incoming value is correct." );
405                 return 67;
406         });
407
408         equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
409
410         oldVal = jQuery("#select1").val();
411
412         jQuery("#select1").val(function(i, val) {
413                 equals( val, oldVal, "Make sure the incoming value is correct." );
414                 return "3";
415         });
416
417         equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
418
419         oldVal = jQuery("#select1").val();
420
421         jQuery("#select1").val(function(i, val) {
422                 equals( val, oldVal, "Make sure the incoming value is correct." );
423                 return 2;
424         });
425
426         equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
427
428         jQuery("#select1").append("<option value='4'>four</option>");
429
430         oldVal = jQuery("#select1").val();
431
432         jQuery("#select1").val(function(i, val) {
433                 equals( val, oldVal, "Make sure the incoming value is correct." );
434                 return 4;
435         });
436
437         equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
438 });
439
440 var testAddClass = function(valueObj) {
441         expect(4);
442         var div = jQuery("div");
443         div.addClass( valueObj("test") );
444         var pass = true;
445         for ( var i = 0; i < div.size(); i++ ) {
446          if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
447         }
448         ok( pass, "Add Class" );
449
450         // using contents will get regular, text, and comment nodes
451         var j = jQuery("#nonnodes").contents();
452         j.addClass( valueObj("asdf") );
453         ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
454
455         div = jQuery("<div/>");
456
457         div.addClass( valueObj("test") );
458         equals( div.attr("class"), "test", "Make sure there's no extra whitespace." );
459
460         div.attr("class", " foo");
461         div.addClass( valueObj("test") );
462         equals( div.attr("class"), "foo test", "Make sure there's no extra whitespace." );
463 };
464
465 test("addClass(String)", function() {
466         testAddClass(bareObj);
467 });
468
469 test("addClass(Function)", function() {
470         testAddClass(functionReturningObj);
471 });
472
473 test("addClass(Function) with incoming value", function() {
474         expect(39);
475
476         var div = jQuery("div"), old = div.map(function(){
477                 return jQuery(this).attr("class");
478         });
479
480         div.addClass(function(i, val) {
481                 equals( val, old[i], "Make sure the incoming value is correct." );
482                 return "test";
483         });
484
485         var pass = true;
486         for ( var i = 0; i < div.size(); i++ ) {
487          if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
488         }
489         ok( pass, "Add Class" );
490 });
491
492 var testRemoveClass = function(valueObj) {
493         expect(7);
494
495         var $divs = jQuery('div');
496
497         $divs.addClass("test").removeClass( valueObj("test") );
498
499         ok( !$divs.is('.test'), "Remove Class" );
500
501         reset();
502         $divs = jQuery('div');
503
504         $divs.addClass("test").addClass("foo").addClass("bar");
505         $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
506
507         ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
508
509         reset();
510         $divs = jQuery('div');
511
512         // Make sure that a null value doesn't cause problems
513         $divs.eq(0).addClass("test").removeClass( valueObj(null) );
514         ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
515
516         $divs.eq(0).addClass("test").removeClass( valueObj("") );
517         ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
518
519         // using contents will get regular, text, and comment nodes
520         var j = jQuery("#nonnodes").contents();
521         j.removeClass( valueObj("asdf") );
522         ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
523
524         var div = document.createElement("div");
525         div.className = " test foo ";
526
527         jQuery(div).removeClass( valueObj("foo") );
528         equals( div.className, "test", "Make sure remaining className is trimmed." );
529
530         div.className = " test ";
531
532         jQuery(div).removeClass( valueObj("test") );
533         equals( div.className, "", "Make sure there is nothing left after everything is removed." );
534 };
535
536 test("removeClass(String) - simple", function() {
537         testRemoveClass(bareObj);
538 });
539
540 test("removeClass(Function) - simple", function() {
541         testRemoveClass(functionReturningObj);
542 });
543
544 test("removeClass(Function) with incoming value", function() {
545         expect(39);
546
547         var $divs = jQuery('div').addClass("test"), old = $divs.map(function(){
548                 return jQuery(this).attr("class");
549         });
550
551         $divs.removeClass(function(i, val) {
552                 equals( val, old[i], "Make sure the incoming value is correct." );
553                 return "test";
554         });
555
556         ok( !$divs.is('.test'), "Remove Class" );
557
558         reset();        
559 });
560
561 var testToggleClass = function(valueObj) {
562         expect(17);
563
564         var e = jQuery("#firstp");
565         ok( !e.is(".test"), "Assert class not present" );
566         e.toggleClass( valueObj("test") );
567         ok( e.is(".test"), "Assert class present" );
568         e.toggleClass( valueObj("test") );
569         ok( !e.is(".test"), "Assert class not present" );
570
571         // class name with a boolean
572         e.toggleClass( valueObj("test"), false );
573         ok( !e.is(".test"), "Assert class not present" );
574         e.toggleClass( valueObj("test"), true );
575         ok( e.is(".test"), "Assert class present" );
576         e.toggleClass( valueObj("test"), false );
577         ok( !e.is(".test"), "Assert class not present" );
578
579         // multiple class names
580         e.addClass("testA testB");
581         ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
582         e.toggleClass( valueObj("testB testC") );
583         ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
584         e.toggleClass( valueObj("testA testC") );
585         ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
586
587         // toggleClass storage
588         e.toggleClass(true);
589         ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
590         e.addClass("testD testE");
591         ok( e.is(".testD.testE"), "Assert class present" );
592         e.toggleClass();
593         ok( !e.is(".testD.testE"), "Assert class not present" );
594         ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
595         e.toggleClass();
596         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
597         e.toggleClass(false);
598         ok( !e.is(".testD.testE"), "Assert class not present" );
599         e.toggleClass(true);
600         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
601         e.toggleClass();
602         e.toggleClass(false);
603         e.toggleClass();
604         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
605
606
607
608         // Cleanup
609         e.removeClass("testD");
610         e.removeData('__className__');
611 };
612
613 test("toggleClass(String|boolean|undefined[, boolean])", function() {
614         testToggleClass(bareObj);
615 });
616
617 test("toggleClass(Function[, boolean])", function() {
618         testToggleClass(functionReturningObj);
619 });
620
621 test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
622         expect(14);
623
624         var e = jQuery("#firstp"), old = e.attr("class");
625         ok( !e.is(".test"), "Assert class not present" );
626         
627         e.toggleClass(function(i, val) {
628                 equals( val, old, "Make sure the incoming value is correct." );
629                 return "test";
630         });
631         ok( e.is(".test"), "Assert class present" );
632         
633         old = e.attr("class");
634         
635         e.toggleClass(function(i, val) {
636                 equals( val, old, "Make sure the incoming value is correct." );
637                 return "test";
638         });
639         ok( !e.is(".test"), "Assert class not present" );
640         
641         old = e.attr("class");
642
643         // class name with a boolean
644         e.toggleClass(function(i, val, state) {
645                 equals( val, old, "Make sure the incoming value is correct." );
646                 equals( state, false, "Make sure that the state is passed in." );
647                 return "test";
648         }, false );
649         ok( !e.is(".test"), "Assert class not present" );
650         
651         old = e.attr("class");
652         
653         e.toggleClass(function(i, val, state) {
654                 equals( val, old, "Make sure the incoming value is correct." );
655                 equals( state, true, "Make sure that the state is passed in." );
656                 return "test";
657         }, true );
658         ok( e.is(".test"), "Assert class present" );
659         
660         old = e.attr("class");
661         
662         e.toggleClass(function(i, val, state) {
663                 equals( val, old, "Make sure the incoming value is correct." );
664                 equals( state, false, "Make sure that the state is passed in." );
665                 return "test";
666         }, false );
667         ok( !e.is(".test"), "Assert class not present" );
668
669         // Cleanup
670         e.removeClass("test");
671         e.removeData('__className__');
672 });
673
674 test("addClass, removeClass, hasClass", function() {
675         expect(14);
676  
677         var jq = jQuery("<p>Hi</p>"), x = jq[0];
678  
679         jq.addClass("hi");
680         equals( x.className, "hi", "Check single added class" );
681  
682         jq.addClass("foo bar");
683         equals( x.className, "hi foo bar", "Check more added classes" );
684  
685         jq.removeClass();
686         equals( x.className, "", "Remove all classes" );
687  
688         jq.addClass("hi foo bar");
689         jq.removeClass("foo");
690         equals( x.className, "hi bar", "Check removal of one class" );
691  
692         ok( jq.hasClass("hi"), "Check has1" );
693         ok( jq.hasClass("bar"), "Check has2" );
694  
695         var jq = jQuery("<p class='class1\nclass2\tcla.ss3\n'></p>");
696         ok( jq.hasClass("class1"), "Check hasClass with carriage return" );
697         ok( jq.is(".class1"), "Check is with carriage return" );
698         ok( jq.hasClass("class2"), "Check hasClass with tab" );
699         ok( jq.is(".class2"), "Check is with tab" );
700         ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" );
701  
702         jq.removeClass("class2");
703         ok( jq.hasClass("class2")==false, "Check the class has been properly removed" );
704         jq.removeClass("cla");
705         ok( jq.hasClass("cla.ss3"), "Check the dotted class has not been removed" );
706         jq.removeClass("cla.ss3");
707         ok( jq.hasClass("cla.ss3")==false, "Check the dotted class has been removed" );
708 });