Made it so that you no longer need to build jQuery in order to run the test suite...
[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(5);
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         div.attr("class", "foo");
465         div.addClass( valueObj("bar baz") );
466         equals( div.attr("class"), "foo bar baz", "Make sure there isn't too much trimming." );
467 };
468
469 test("addClass(String)", function() {
470         testAddClass(bareObj);
471 });
472
473 test("addClass(Function)", function() {
474         testAddClass(functionReturningObj);
475 });
476
477 test("addClass(Function) with incoming value", function() {
478         expect(39);
479
480         var div = jQuery("div"), old = div.map(function(){
481                 return jQuery(this).attr("class");
482         });
483
484         div.addClass(function(i, val) {
485                 if ( this.id !== "_firebugConsole" ) {
486                         equals( val, old[i], "Make sure the incoming value is correct." );
487                         return "test";
488                 }
489         });
490
491         var pass = true;
492         for ( var i = 0; i < div.size(); i++ ) {
493          if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
494         }
495         ok( pass, "Add Class" );
496 });
497
498 var testRemoveClass = function(valueObj) {
499         expect(7);
500
501         var $divs = jQuery('div');
502
503         $divs.addClass("test").removeClass( valueObj("test") );
504
505         ok( !$divs.is('.test'), "Remove Class" );
506
507         reset();
508         $divs = jQuery('div');
509
510         $divs.addClass("test").addClass("foo").addClass("bar");
511         $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
512
513         ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
514
515         reset();
516         $divs = jQuery('div');
517
518         // Make sure that a null value doesn't cause problems
519         $divs.eq(0).addClass("test").removeClass( valueObj(null) );
520         ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
521
522         $divs.eq(0).addClass("test").removeClass( valueObj("") );
523         ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
524
525         // using contents will get regular, text, and comment nodes
526         var j = jQuery("#nonnodes").contents();
527         j.removeClass( valueObj("asdf") );
528         ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
529
530         var div = document.createElement("div");
531         div.className = " test foo ";
532
533         jQuery(div).removeClass( valueObj("foo") );
534         equals( div.className, "test", "Make sure remaining className is trimmed." );
535
536         div.className = " test ";
537
538         jQuery(div).removeClass( valueObj("test") );
539         equals( div.className, "", "Make sure there is nothing left after everything is removed." );
540 };
541
542 test("removeClass(String) - simple", function() {
543         testRemoveClass(bareObj);
544 });
545
546 test("removeClass(Function) - simple", function() {
547         testRemoveClass(functionReturningObj);
548 });
549
550 test("removeClass(Function) with incoming value", function() {
551         expect(39);
552
553         var $divs = jQuery('div').addClass("test"), old = $divs.map(function(){
554                 return jQuery(this).attr("class");
555         });
556
557         $divs.removeClass(function(i, val) {
558                 if ( this.id !== "_firebugConsole" ) {
559                         equals( val, old[i], "Make sure the incoming value is correct." );
560                         return "test";
561                 }
562         });
563
564         ok( !$divs.is('.test'), "Remove Class" );
565
566         reset();        
567 });
568
569 var testToggleClass = function(valueObj) {
570         expect(17);
571
572         var e = jQuery("#firstp");
573         ok( !e.is(".test"), "Assert class not present" );
574         e.toggleClass( valueObj("test") );
575         ok( e.is(".test"), "Assert class present" );
576         e.toggleClass( valueObj("test") );
577         ok( !e.is(".test"), "Assert class not present" );
578
579         // class name with a boolean
580         e.toggleClass( valueObj("test"), false );
581         ok( !e.is(".test"), "Assert class not present" );
582         e.toggleClass( valueObj("test"), true );
583         ok( e.is(".test"), "Assert class present" );
584         e.toggleClass( valueObj("test"), false );
585         ok( !e.is(".test"), "Assert class not present" );
586
587         // multiple class names
588         e.addClass("testA testB");
589         ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
590         e.toggleClass( valueObj("testB testC") );
591         ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
592         e.toggleClass( valueObj("testA testC") );
593         ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
594
595         // toggleClass storage
596         e.toggleClass(true);
597         ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
598         e.addClass("testD testE");
599         ok( e.is(".testD.testE"), "Assert class present" );
600         e.toggleClass();
601         ok( !e.is(".testD.testE"), "Assert class not present" );
602         ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
603         e.toggleClass();
604         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
605         e.toggleClass(false);
606         ok( !e.is(".testD.testE"), "Assert class not present" );
607         e.toggleClass(true);
608         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
609         e.toggleClass();
610         e.toggleClass(false);
611         e.toggleClass();
612         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
613
614
615
616         // Cleanup
617         e.removeClass("testD");
618         e.removeData('__className__');
619 };
620
621 test("toggleClass(String|boolean|undefined[, boolean])", function() {
622         testToggleClass(bareObj);
623 });
624
625 test("toggleClass(Function[, boolean])", function() {
626         testToggleClass(functionReturningObj);
627 });
628
629 test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
630         expect(14);
631
632         var e = jQuery("#firstp"), old = e.attr("class");
633         ok( !e.is(".test"), "Assert class not present" );
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 present" );
640         
641         old = e.attr("class");
642         
643         e.toggleClass(function(i, val) {
644                 equals( val, old, "Make sure the incoming value is correct." );
645                 return "test";
646         });
647         ok( !e.is(".test"), "Assert class not present" );
648         
649         old = e.attr("class");
650
651         // class name with a boolean
652         e.toggleClass(function(i, val, state) {
653                 equals( val, old, "Make sure the incoming value is correct." );
654                 equals( state, false, "Make sure that the state is passed in." );
655                 return "test";
656         }, false );
657         ok( !e.is(".test"), "Assert class not present" );
658         
659         old = e.attr("class");
660         
661         e.toggleClass(function(i, val, state) {
662                 equals( val, old, "Make sure the incoming value is correct." );
663                 equals( state, true, "Make sure that the state is passed in." );
664                 return "test";
665         }, true );
666         ok( e.is(".test"), "Assert class present" );
667         
668         old = e.attr("class");
669         
670         e.toggleClass(function(i, val, state) {
671                 equals( val, old, "Make sure the incoming value is correct." );
672                 equals( state, false, "Make sure that the state is passed in." );
673                 return "test";
674         }, false );
675         ok( !e.is(".test"), "Assert class not present" );
676
677         // Cleanup
678         e.removeClass("test");
679         e.removeData('__className__');
680 });
681
682 test("addClass, removeClass, hasClass", function() {
683         expect(14);
684  
685         var jq = jQuery("<p>Hi</p>"), x = jq[0];
686  
687         jq.addClass("hi");
688         equals( x.className, "hi", "Check single added class" );
689  
690         jq.addClass("foo bar");
691         equals( x.className, "hi foo bar", "Check more added classes" );
692  
693         jq.removeClass();
694         equals( x.className, "", "Remove all classes" );
695  
696         jq.addClass("hi foo bar");
697         jq.removeClass("foo");
698         equals( x.className, "hi bar", "Check removal of one class" );
699  
700         ok( jq.hasClass("hi"), "Check has1" );
701         ok( jq.hasClass("bar"), "Check has2" );
702  
703         var jq = jQuery("<p class='class1\nclass2\tcla.ss3\n'></p>");
704         ok( jq.hasClass("class1"), "Check hasClass with carriage return" );
705         ok( jq.is(".class1"), "Check is with carriage return" );
706         ok( jq.hasClass("class2"), "Check hasClass with tab" );
707         ok( jq.is(".class2"), "Check is with tab" );
708         ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" );
709  
710         jq.removeClass("class2");
711         ok( jq.hasClass("class2")==false, "Check the class has been properly removed" );
712         jq.removeClass("cla");
713         ok( jq.hasClass("cla.ss3"), "Check the dotted class has not been removed" );
714         jq.removeClass("cla.ss3");
715         ok( jq.hasClass("cla.ss3")==false, "Check the dotted class has been removed" );
716 });