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