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