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