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