3 var bareObj = function(value) { return value; };
4 var functionReturningObj = function(value) { return (function() { return value; }); };
6 test("attr(String)", function() {
9 // This one sometimes fails randomly ?!
10 equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
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' );
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)' );
37 equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
40 // Related to [5574] and [5683]
41 var body = document.body, $body = jQuery(body);
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' );
46 body.setAttribute('foo', 'baz');
47 equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
50 equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
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' );
56 ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
58 body.removeAttribute('foo'); // Cleanup
60 var select = document.createElement("select"), optgroup = document.createElement("optgroup"), option = document.createElement("option");
61 optgroup.appendChild( option );
62 select.appendChild( optgroup );
64 equals( jQuery(option).attr("selected"), true, "Make sure that a single option is selected, even when in an optgroup." );
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." );
69 equals( jQuery(document).attr("nodeName"), "#document", "attr works correctly on document nodes (bug #7451)." );
71 var attributeNode = document.createAttribute("irrelevant"),
72 commentNode = document.createComment("some comment"),
73 textNode = document.createTextNode("some text"),
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)." );
81 test("attr(String) in XML Files", function() {
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" );
92 test("attr(String, Function)", function() {
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");
98 test("attr(Hash)", function() {
101 jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
102 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
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");
110 test("attr(String, Object)", function() {
113 var div = jQuery("div").attr("foo", "bar"),
116 for ( var i = 0; i < div.size(); i++ ) {
117 if ( div.get(i).getAttribute('foo') != "bar" ){
123 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
125 // Fails on IE since recent changes to .attr()
126 // ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
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' );
145 var attributeNode = document.createAttribute("irrelevant"),
146 commentNode = document.createComment("some comment"),
147 textNode = document.createTextNode("some text"),
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)." );
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)." );
160 jQuery.each( [document, "#firstp"], function( i, ele ) {
161 jQuery( ele ).removeAttr("nonexisting");
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" );
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' );
181 // using contents will get comments regular, text, and comment nodes
182 var j = jQuery("#nonnodes").contents();
184 j.attr("name", "attrvalue");
185 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
186 j.removeAttr("name");
190 var type = jQuery("#check2").attr('type');
193 jQuery("#check2").attr('type','hidden');
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" );
200 var check = document.createElement("input");
203 jQuery(check).attr('type','checkbox');
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" );
210 var check = jQuery("<input />");
213 check.attr('type','checkbox');
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" );
220 var button = jQuery("#button");
223 button.attr('type','submit');
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" );
231 test("attr(jquery_method)", function(){
234 var $elem = jQuery("<div />"),
238 $elem.attr({'html': 'foo'}, true);
239 equals( elem.innerHTML, 'foo', 'attr(html)');
241 $elem.attr({'text': 'bar'}, true);
242 equals( elem.innerHTML, 'bar', 'attr(text)');
244 $elem.attr({'css': {color:'red'}}, true);
245 ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
247 $elem.attr({'height': 10}, true);
248 equals( elem.style.height, '10px', 'attr(height)');
250 // Multiple attributes
254 css:{ paddingLeft:1, paddingRight:1 }
257 equals( elem.style.width, '10px', 'attr({...})');
258 equals( elem.style.paddingLeft, '1px', 'attr({...})');
259 equals( elem.style.paddingRight, '1px', 'attr({...})');
263 test("attr(String, Object) - Loaded via XML document", function() {
266 jQuery.get('data/dashboard.xml', function(xml) {
268 jQuery('tab', xml).each(function() {
269 titles.push(jQuery(this).attr('title'));
271 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
272 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
278 test("attr('tabindex')", function() {
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');
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');
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');
296 test("attr('tabindex', value)", function() {
299 var element = jQuery('#divWithNoTabIndex');
300 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
302 // set a positive string
303 element.attr('tabindex', '1');
304 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
307 element.attr('tabindex', '0');
308 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
310 // set a negative string
311 element.attr('tabindex', '-1');
312 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
314 // set a positive number
315 element.attr('tabindex', 1);
316 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
319 element.attr('tabindex', 0);
320 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
322 // set a negative number
323 element.attr('tabindex', -1);
324 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
326 element = jQuery('#linkWithTabIndex');
327 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
329 element.attr('tabindex', -1);
330 equals(element.attr('tabindex'), -1, 'set negative tabindex');
333 test("removeAttr(String)", function() {
335 equals( jQuery('#mark').removeAttr( "class" )[0].className, "", "remove class" );
337 var attributeNode = document.createAttribute("irrelevant"),
338 commentNode = document.createComment("some comment"),
339 textNode = document.createTextNode("some text"),
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" );
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)." );
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)." );
356 test("val()", function() {
359 document.getElementById('text1').value = "bla";
360 equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
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" );
369 equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );
371 same( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );
373 equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );
375 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );
377 equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
379 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with no value attribute' );
381 jQuery('#select3').val("");
382 same( jQuery('#select3').val(), [''], 'Call val() on a multiple="multiple" select' );
384 same( jQuery('#select4').val(), [], 'Call val() on multiple="multiple" select with all disabled options' );
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' );
389 jQuery('#select4').attr('disabled', true);
390 same( jQuery('#select4').val(), ['2', '3'], 'Call val() on disabled multiple="multiple" select' );
392 equals( jQuery('#select5').val(), "3", "Check value on ambiguous select." );
394 jQuery('#select5').val(1);
395 equals( jQuery('#select5').val(), "1", "Check value on ambiguous select." );
397 jQuery('#select5').val(3);
398 equals( jQuery('#select5').val(), "3", "Check value on ambiguous select." );
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");
402 same( checks.serialize(), "", "Get unchecked values." );
404 equals( checks.eq(3).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
407 same( checks.serialize(), "test=2", "Get a single checked value." );
409 checks.val([ "1", "" ]);
410 same( checks.serialize(), "test=1&test=", "Get multiple checked values." );
412 checks.val([ "", "2" ]);
413 same( checks.serialize(), "test=2&test=", "Get multiple checked values." );
415 checks.val([ "1", "on" ]);
416 same( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
421 var testVal = function(valueObj) {
424 jQuery("#text1").val(valueObj( 'test' ));
425 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
427 jQuery("#text1").val(valueObj( undefined ));
428 equals( document.getElementById('text1').value, "", "Check for modified (via val(undefined)) value of input element" );
430 jQuery("#text1").val(valueObj( 67 ));
431 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
433 jQuery("#text1").val(valueObj( null ));
434 equals( document.getElementById('text1').value, "", "Check for modified (via val(null)) value of input element" );
436 jQuery("#select1").val(valueObj( "3" ));
437 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
439 jQuery("#select1").val(valueObj( 2 ));
440 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
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" );
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");
453 test("val(String/Number)", function() {
457 test("val(Function)", function() {
458 testVal(functionReturningObj);
461 test( "val(Array of Numbers) (Bug #7123)", function() {
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" );
473 test("val(Function) with incoming value", function() {
476 var oldVal = jQuery("#text1").val();
478 jQuery("#text1").val(function(i, val) {
479 equals( val, oldVal, "Make sure the incoming value is correct." );
483 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
485 oldVal = jQuery("#text1").val();
487 jQuery("#text1").val(function(i, val) {
488 equals( val, oldVal, "Make sure the incoming value is correct." );
492 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
494 oldVal = jQuery("#select1").val();
496 jQuery("#select1").val(function(i, val) {
497 equals( val, oldVal, "Make sure the incoming value is correct." );
501 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
503 oldVal = jQuery("#select1").val();
505 jQuery("#select1").val(function(i, val) {
506 equals( val, oldVal, "Make sure the incoming value is correct." );
510 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
512 jQuery("#select1").append("<option value='4'>four</option>");
514 oldVal = jQuery("#select1").val();
516 jQuery("#select1").val(function(i, val) {
517 equals( val, oldVal, "Make sure the incoming value is correct." );
521 equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
524 var testAddClass = function(valueObj) {
526 var div = jQuery("div");
527 div.addClass( valueObj("test") );
529 for ( var i = 0; i < div.size(); i++ ) {
530 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
532 ok( pass, "Add Class" );
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" );
539 div = jQuery("<div/>");
541 div.addClass( valueObj("test") );
542 equals( div.attr("class"), "test", "Make sure there's no extra whitespace." );
544 div.attr("class", " foo");
545 div.addClass( valueObj("test") );
546 equals( div.attr("class"), "foo test", "Make sure there's no extra whitespace." );
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." );
553 test("addClass(String)", function() {
554 testAddClass(bareObj);
557 test("addClass(Function)", function() {
558 testAddClass(functionReturningObj);
561 test("addClass(Function) with incoming value", function() {
564 var div = jQuery("div"), old = div.map(function(){
565 return jQuery(this).attr("class");
568 div.addClass(function(i, val) {
569 if ( this.id !== "_firebugConsole" ) {
570 equals( val, old[i], "Make sure the incoming value is correct." );
576 for ( var i = 0; i < div.size(); i++ ) {
577 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
579 ok( pass, "Add Class" );
582 var testRemoveClass = function(valueObj) {
585 var $divs = jQuery('div');
587 $divs.addClass("test").removeClass( valueObj("test") );
589 ok( !$divs.is('.test'), "Remove Class" );
592 $divs = jQuery('div');
594 $divs.addClass("test").addClass("foo").addClass("bar");
595 $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
597 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
600 $divs = jQuery('div');
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" );
606 $divs.eq(0).addClass("test").removeClass( valueObj("") );
607 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
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" );
614 var div = document.createElement("div");
615 div.className = " test foo ";
617 jQuery(div).removeClass( valueObj("foo") );
618 equals( div.className, "test", "Make sure remaining className is trimmed." );
620 div.className = " test ";
622 jQuery(div).removeClass( valueObj("test") );
623 equals( div.className, "", "Make sure there is nothing left after everything is removed." );
626 test("removeClass(String) - simple", function() {
627 testRemoveClass(bareObj);
630 test("removeClass(Function) - simple", function() {
631 testRemoveClass(functionReturningObj);
634 test("removeClass(Function) with incoming value", function() {
637 var $divs = jQuery('div').addClass("test"), old = $divs.map(function(){
638 return jQuery(this).attr("class");
641 $divs.removeClass(function(i, val) {
642 if ( this.id !== "_firebugConsole" ) {
643 equals( val, old[i], "Make sure the incoming value is correct." );
648 ok( !$divs.is('.test'), "Remove Class" );
653 var testToggleClass = function(valueObj) {
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" );
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" );
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" );
679 // toggleClass storage
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" );
685 ok( !e.is(".testD.testE"), "Assert class not present" );
686 ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
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" );
692 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
694 e.toggleClass(false);
696 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
701 e.removeClass("testD");
702 e.removeData('__className__');
705 test("toggleClass(String|boolean|undefined[, boolean])", function() {
706 testToggleClass(bareObj);
709 test("toggleClass(Function[, boolean])", function() {
710 testToggleClass(functionReturningObj);
713 test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
716 var e = jQuery("#firstp"), old = e.attr("class");
717 ok( !e.is(".test"), "Assert class not present" );
719 e.toggleClass(function(i, val) {
720 equals( val, old, "Make sure the incoming value is correct." );
723 ok( e.is(".test"), "Assert class present" );
725 old = e.attr("class");
727 e.toggleClass(function(i, val) {
728 equals( val, old, "Make sure the incoming value is correct." );
731 ok( !e.is(".test"), "Assert class not present" );
733 old = e.attr("class");
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." );
741 ok( !e.is(".test"), "Assert class not present" );
743 old = e.attr("class");
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." );
750 ok( e.is(".test"), "Assert class present" );
752 old = e.attr("class");
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." );
759 ok( !e.is(".test"), "Assert class not present" );
762 e.removeClass("test");
763 e.removeData('__className__');
766 test("addClass, removeClass, hasClass", function() {
769 var jq = jQuery("<p>Hi</p>"), x = jq[0];
772 equals( x.className, "hi", "Check single added class" );
774 jq.addClass("foo bar");
775 equals( x.className, "hi foo bar", "Check more added classes" );
778 equals( x.className, "", "Remove all classes" );
780 jq.addClass("hi foo bar");
781 jq.removeClass("foo");
782 equals( x.className, "hi bar", "Check removal of one class" );
784 ok( jq.hasClass("hi"), "Check has1" );
785 ok( jq.hasClass("bar"), "Check has2" );
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" );
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" );