1 module("attributes", { teardown: moduleTeardown });
3 var bareObj = function(value) { return value; };
4 var functionReturningObj = function(value) { return (function() { return value; }); };
6 test("jQuery.props: itegrity test", function() {
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.
18 maxlength: "maxLength",
19 cellspacing: "cellSpacing",
24 frameborder: "frameBorder"
27 same(propsShouldBe, jQuery.props, "jQuery.props passes integrity check");
31 test("attr(String)", function() {
34 // This one sometimes fails randomly ?!
35 equals( jQuery('#text1').attr('value'), "Test", 'Check for value attribute' );
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' );
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)' );
62 equals( jQuery("<option/>").attr("selected"), false, "Check selected attribute on disconnected element." );
65 // Related to [5574] and [5683]
66 var body = document.body, $body = jQuery(body);
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' );
71 body.setAttribute('foo', 'baz');
72 equals( $body.attr('foo'), 'baz', 'Make sure the dom attribute is retrieved when no expando is found' );
75 equals( $body.attr('foo'), 'bar', 'Make sure the expando is preferred over the dom attribute' );
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' );
81 ok( $body.attr('foo') === undefined, 'Make sure the expando is preferred over the dom attribute, even if undefined' );
83 body.removeAttribute('foo'); // Cleanup
85 var select = document.createElement("select"), optgroup = document.createElement("optgroup"), option = document.createElement("option");
86 optgroup.appendChild( option );
87 select.appendChild( optgroup );
89 equals( jQuery(option).attr("selected"), true, "Make sure that a single option is selected, even when in an optgroup." );
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." );
94 equals( jQuery(document).attr("nodeName"), "#document", "attr works correctly on document nodes (bug #7451)." );
96 var attributeNode = document.createAttribute("irrelevant"),
97 commentNode = document.createComment("some comment"),
98 textNode = document.createTextNode("some text"),
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)." );
106 test("attr(String) in XML Files", function() {
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" );
117 test("attr(String, Function)", function() {
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");
123 test("attr(Hash)", function() {
126 jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
127 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
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");
135 test("attr(String, Object)", function() {
138 var div = jQuery("div").attr("foo", "bar"),
141 for ( var i = 0; i < div.size(); i++ ) {
142 if ( div.get(i).getAttribute('foo') != "bar" ){
148 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
150 // Fails on IE since recent changes to .attr()
151 // ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
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' );
170 var attributeNode = document.createAttribute("irrelevant"),
171 commentNode = document.createComment("some comment"),
172 textNode = document.createTextNode("some text"),
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)." );
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)." );
185 jQuery.each( [document, "#firstp"], function( i, ele ) {
186 jQuery( ele ).removeAttr("nonexisting");
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" );
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' );
206 // using contents will get comments regular, text, and comment nodes
207 var j = jQuery("#nonnodes").contents();
209 j.attr("name", "attrvalue");
210 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
211 j.removeAttr("name");
215 var type = jQuery("#check2").attr('type');
218 jQuery("#check2").attr('type','hidden');
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" );
225 var check = document.createElement("input");
228 jQuery(check).attr('type','checkbox');
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" );
235 var check = jQuery("<input />");
238 check.attr('type','checkbox');
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" );
245 var button = jQuery("#button");
248 button.attr('type','submit');
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" );
256 test("attr(jquery_method)", function(){
259 var $elem = jQuery("<div />"),
263 $elem.attr({'html': 'foo'}, true);
264 equals( elem.innerHTML, 'foo', 'attr(html)');
266 $elem.attr({'text': 'bar'}, true);
267 equals( elem.innerHTML, 'bar', 'attr(text)');
269 $elem.attr({'css': {color:'red'}}, true);
270 ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
272 $elem.attr({'height': 10}, true);
273 equals( elem.style.height, '10px', 'attr(height)');
275 // Multiple attributes
279 css:{ paddingLeft:1, paddingRight:1 }
282 equals( elem.style.width, '10px', 'attr({...})');
283 equals( elem.style.paddingLeft, '1px', 'attr({...})');
284 equals( elem.style.paddingRight, '1px', 'attr({...})');
288 test("attr(String, Object) - Loaded via XML document", function() {
291 jQuery.get('data/dashboard.xml', function(xml) {
293 jQuery('tab', xml).each(function() {
294 titles.push(jQuery(this).attr('title'));
296 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
297 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
303 test("attr('tabindex')", function() {
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');
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');
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');
321 test("attr('tabindex', value)", function() {
324 var element = jQuery('#divWithNoTabIndex');
325 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
327 // set a positive string
328 element.attr('tabindex', '1');
329 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
332 element.attr('tabindex', '0');
333 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
335 // set a negative string
336 element.attr('tabindex', '-1');
337 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
339 // set a positive number
340 element.attr('tabindex', 1);
341 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
344 element.attr('tabindex', 0);
345 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
347 // set a negative number
348 element.attr('tabindex', -1);
349 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
351 element = jQuery('#linkWithTabIndex');
352 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
354 element.attr('tabindex', -1);
355 equals(element.attr('tabindex'), -1, 'set negative tabindex');
358 test("removeAttr(String)", function() {
360 equals( jQuery('#mark').removeAttr( "class" )[0].className, "", "remove class" );
362 var attributeNode = document.createAttribute("irrelevant"),
363 commentNode = document.createComment("some comment"),
364 textNode = document.createTextNode("some text"),
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" );
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)." );
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)." );
381 test("val()", function() {
384 document.getElementById('text1').value = "bla";
385 equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
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" );
394 equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );
396 same( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );
398 equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );
400 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );
402 equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
404 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with no value attribute' );
406 jQuery('#select3').val("");
407 same( jQuery('#select3').val(), [''], 'Call val() on a multiple="multiple" select' );
409 same( jQuery('#select4').val(), [], 'Call val() on multiple="multiple" select with all disabled options' );
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' );
414 jQuery('#select4').attr('disabled', true);
415 same( jQuery('#select4').val(), ['2', '3'], 'Call val() on disabled multiple="multiple" select' );
417 equals( jQuery('#select5').val(), "3", "Check value on ambiguous select." );
419 jQuery('#select5').val(1);
420 equals( jQuery('#select5').val(), "1", "Check value on ambiguous select." );
422 jQuery('#select5').val(3);
423 equals( jQuery('#select5').val(), "3", "Check value on ambiguous select." );
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");
427 same( checks.serialize(), "", "Get unchecked values." );
429 equals( checks.eq(3).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
432 same( checks.serialize(), "test=2", "Get a single checked value." );
434 checks.val([ "1", "" ]);
435 same( checks.serialize(), "test=1&test=", "Get multiple checked values." );
437 checks.val([ "", "2" ]);
438 same( checks.serialize(), "test=2&test=", "Get multiple checked values." );
440 checks.val([ "1", "on" ]);
441 same( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
446 var testVal = function(valueObj) {
449 jQuery("#text1").val(valueObj( 'test' ));
450 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
452 jQuery("#text1").val(valueObj( undefined ));
453 equals( document.getElementById('text1').value, "", "Check for modified (via val(undefined)) value of input element" );
455 jQuery("#text1").val(valueObj( 67 ));
456 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
458 jQuery("#text1").val(valueObj( null ));
459 equals( document.getElementById('text1').value, "", "Check for modified (via val(null)) value of input element" );
461 jQuery("#select1").val(valueObj( "3" ));
462 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
464 jQuery("#select1").val(valueObj( 2 ));
465 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
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" );
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");
478 test("val(String/Number)", function() {
482 test("val(Function)", function() {
483 testVal(functionReturningObj);
486 test( "val(Array of Numbers) (Bug #7123)", function() {
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" );
498 test("val(Function) with incoming value", function() {
501 var oldVal = jQuery("#text1").val();
503 jQuery("#text1").val(function(i, val) {
504 equals( val, oldVal, "Make sure the incoming value is correct." );
508 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
510 oldVal = jQuery("#text1").val();
512 jQuery("#text1").val(function(i, val) {
513 equals( val, oldVal, "Make sure the incoming value is correct." );
517 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
519 oldVal = jQuery("#select1").val();
521 jQuery("#select1").val(function(i, val) {
522 equals( val, oldVal, "Make sure the incoming value is correct." );
526 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
528 oldVal = jQuery("#select1").val();
530 jQuery("#select1").val(function(i, val) {
531 equals( val, oldVal, "Make sure the incoming value is correct." );
535 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
537 jQuery("#select1").append("<option value='4'>four</option>");
539 oldVal = jQuery("#select1").val();
541 jQuery("#select1").val(function(i, val) {
542 equals( val, oldVal, "Make sure the incoming value is correct." );
546 equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
549 var testAddClass = function(valueObj) {
551 var div = jQuery("div");
552 div.addClass( valueObj("test") );
554 for ( var i = 0; i < div.size(); i++ ) {
555 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
557 ok( pass, "Add Class" );
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" );
564 div = jQuery("<div/>");
566 div.addClass( valueObj("test") );
567 equals( div.attr("class"), "test", "Make sure there's no extra whitespace." );
569 div.attr("class", " foo");
570 div.addClass( valueObj("test") );
571 equals( div.attr("class"), "foo test", "Make sure there's no extra whitespace." );
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." );
578 test("addClass(String)", function() {
579 testAddClass(bareObj);
582 test("addClass(Function)", function() {
583 testAddClass(functionReturningObj);
586 test("addClass(Function) with incoming value", function() {
589 var div = jQuery("div"), old = div.map(function(){
590 return jQuery(this).attr("class");
593 div.addClass(function(i, val) {
594 if ( this.id !== "_firebugConsole" ) {
595 equals( val, old[i], "Make sure the incoming value is correct." );
601 for ( var i = 0; i < div.size(); i++ ) {
602 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
604 ok( pass, "Add Class" );
607 var testRemoveClass = function(valueObj) {
610 var $divs = jQuery('div');
612 $divs.addClass("test").removeClass( valueObj("test") );
614 ok( !$divs.is('.test'), "Remove Class" );
617 $divs = jQuery('div');
619 $divs.addClass("test").addClass("foo").addClass("bar");
620 $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
622 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
625 $divs = jQuery('div');
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" );
631 $divs.eq(0).addClass("test").removeClass( valueObj("") );
632 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
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" );
639 var div = document.createElement("div");
640 div.className = " test foo ";
642 jQuery(div).removeClass( valueObj("foo") );
643 equals( div.className, "test", "Make sure remaining className is trimmed." );
645 div.className = " test ";
647 jQuery(div).removeClass( valueObj("test") );
648 equals( div.className, "", "Make sure there is nothing left after everything is removed." );
651 test("removeClass(String) - simple", function() {
652 testRemoveClass(bareObj);
655 test("removeClass(Function) - simple", function() {
656 testRemoveClass(functionReturningObj);
659 test("removeClass(Function) with incoming value", function() {
662 var $divs = jQuery('div').addClass("test"), old = $divs.map(function(){
663 return jQuery(this).attr("class");
666 $divs.removeClass(function(i, val) {
667 if ( this.id !== "_firebugConsole" ) {
668 equals( val, old[i], "Make sure the incoming value is correct." );
673 ok( !$divs.is('.test'), "Remove Class" );
678 var testToggleClass = function(valueObj) {
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" );
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" );
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" );
704 // toggleClass storage
706 ok( e[0].className === "", "Assert class is empty (data was empty)" );
707 e.addClass("testD testE");
708 ok( e.is(".testD.testE"), "Assert class present" );
710 ok( !e.is(".testD.testE"), "Assert class not present" );
711 ok( jQuery._data(e[0], '__className__') === 'testD testE', "Assert data was stored" );
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" );
717 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
719 e.toggleClass(false);
721 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
724 e.removeClass("testD");
725 jQuery.removeData(e[0], '__className__', true);
728 test("toggleClass(String|boolean|undefined[, boolean])", function() {
729 testToggleClass(bareObj);
732 test("toggleClass(Function[, boolean])", function() {
733 testToggleClass(functionReturningObj);
736 test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
739 var e = jQuery("#firstp"), old = e.attr("class");
740 ok( !e.is(".test"), "Assert class not present" );
742 e.toggleClass(function(i, val) {
743 equals( val, old, "Make sure the incoming value is correct." );
746 ok( e.is(".test"), "Assert class present" );
748 old = e.attr("class");
750 e.toggleClass(function(i, val) {
751 equals( val, old, "Make sure the incoming value is correct." );
754 ok( !e.is(".test"), "Assert class not present" );
756 old = e.attr("class");
758 // class name with a boolean
759 e.toggleClass(function(i, val, state) {
760 equals( val, old, "Make sure the incoming value is correct." );
761 equals( state, false, "Make sure that the state is passed in." );
764 ok( !e.is(".test"), "Assert class not present" );
766 old = e.attr("class");
768 e.toggleClass(function(i, val, state) {
769 equals( val, old, "Make sure the incoming value is correct." );
770 equals( state, true, "Make sure that the state is passed in." );
773 ok( e.is(".test"), "Assert class present" );
775 old = e.attr("class");
777 e.toggleClass(function(i, val, state) {
778 equals( val, old, "Make sure the incoming value is correct." );
779 equals( state, false, "Make sure that the state is passed in." );
782 ok( !e.is(".test"), "Assert class not present" );
785 e.removeClass("test");
786 jQuery.removeData(e[0], '__className__', true);
789 test("addClass, removeClass, hasClass", function() {
792 var jq = jQuery("<p>Hi</p>"), x = jq[0];
795 equals( x.className, "hi", "Check single added class" );
797 jq.addClass("foo bar");
798 equals( x.className, "hi foo bar", "Check more added classes" );
801 equals( x.className, "", "Remove all classes" );
803 jq.addClass("hi foo bar");
804 jq.removeClass("foo");
805 equals( x.className, "hi bar", "Check removal of one class" );
807 ok( jq.hasClass("hi"), "Check has1" );
808 ok( jq.hasClass("bar"), "Check has2" );
810 var jq = jQuery("<p class='class1\nclass2\tcla.ss3\n\rclass4'></p>");
811 ok( jq.hasClass("class1"), "Check hasClass with line feed" );
812 ok( jq.is(".class1"), "Check is with line feed" );
813 ok( jq.hasClass("class2"), "Check hasClass with tab" );
814 ok( jq.is(".class2"), "Check is with tab" );
815 ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" );
816 ok( jq.hasClass("class4"), "Check hasClass with carriage return" );
817 ok( jq.is(".class4"), "Check is with carriage return" );
819 jq.removeClass("class2");
820 ok( jq.hasClass("class2")==false, "Check the class has been properly removed" );
821 jq.removeClass("cla");
822 ok( jq.hasClass("cla.ss3"), "Check the dotted class has not been removed" );
823 jq.removeClass("cla.ss3");
824 ok( jq.hasClass("cla.ss3")==false, "Check the dotted class has been removed" );
825 jq.removeClass("class4");
826 ok( jq.hasClass("class4")==false, "Check the class has been properly removed" );