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)." );
73 test("attr(String) in XML Files", function() {
76 jQuery.get("data/dashboard.xml", function(xml) {
77 equals( jQuery("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );
78 equals( jQuery("location", xml).attr("for"), "bar", "Check for attribute in XML document" );
84 test("attr(String, Function)", function() {
86 equals( jQuery('#text1').attr('value', function() { return this.id ;})[0].value, "text1", "Set value from id" );
87 equals( jQuery('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");
90 test("attr(Hash)", function() {
93 jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
94 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
96 ok( pass, "Set Multiple Attributes" );
97 equals( jQuery('#text1').attr({'value': function() { return this.id; }})[0].value, "text1", "Set attribute to computed value #1" );
98 equals( jQuery('#text1').attr({'title': function(i) { return i; }}).attr('title'), "0", "Set attribute to computed value #2");
102 test("attr(String, Object)", function() {
105 var div = jQuery("div").attr("foo", "bar"),
108 for ( var i = 0; i < div.size(); i++ ) {
109 if ( div.get(i).getAttribute('foo') != "bar" ){
115 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
117 // Fails on IE since recent changes to .attr()
118 // ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
120 jQuery("#name").attr('name', 'something');
121 equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
122 jQuery("#name").attr('name', null);
123 equals( jQuery("#name").attr('title'), '', 'Remove name attribute' );
124 jQuery("#check2").attr('checked', true);
125 equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
126 jQuery("#check2").attr('checked', false);
127 equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
128 jQuery("#text1").attr('readonly', true);
129 equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
130 jQuery("#text1").attr('readonly', false);
131 equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
132 jQuery("#name").attr('maxlength', '5');
133 equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
134 jQuery("#name").attr('maxLength', '10');
135 equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
137 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>"),
138 td = table.find('td:first');
139 td.attr("rowspan", "2");
140 equals( td[0].rowSpan, 2, "Check rowspan is correctly set" );
141 td.attr("colspan", "2");
142 equals( td[0].colSpan, 2, "Check colspan is correctly set" );
143 table.attr("cellspacing", "2");
144 equals( table[0].cellSpacing, 2, "Check cellspacing is correctly set" );
147 jQuery("#name").attr('someAttr', '0');
148 equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
149 jQuery("#name").attr('someAttr', 0);
150 equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
151 jQuery("#name").attr('someAttr', 1);
152 equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
154 // using contents will get comments regular, text, and comment nodes
155 var j = jQuery("#nonnodes").contents();
157 j.attr("name", "attrvalue");
158 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
159 j.removeAttr("name");
163 var type = jQuery("#check2").attr('type');
166 jQuery("#check2").attr('type','hidden');
170 ok( thrown, "Exception thrown when trying to change type property" );
171 equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
173 var check = document.createElement("input");
176 jQuery(check).attr('type','checkbox');
180 ok( thrown, "Exception thrown when trying to change type property" );
181 equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
183 var check = jQuery("<input />");
186 check.attr('type','checkbox');
190 ok( thrown, "Exception thrown when trying to change type property" );
191 equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
193 var button = jQuery("#button");
196 button.attr('type','submit');
200 ok( thrown, "Exception thrown when trying to change type property" );
201 equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
204 test("attr(jquery_method)", function(){
207 var $elem = jQuery("<div />"),
211 $elem.attr({'html': 'foo'}, true);
212 equals( elem.innerHTML, 'foo', 'attr(html)');
214 $elem.attr({'text': 'bar'}, true);
215 equals( elem.innerHTML, 'bar', 'attr(text)');
217 $elem.attr({'css': {color:'red'}}, true);
218 ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
220 $elem.attr({'height': 10}, true);
221 equals( elem.style.height, '10px', 'attr(height)');
223 // Multiple attributes
227 css:{ paddingLeft:1, paddingRight:1 }
230 equals( elem.style.width, '10px', 'attr({...})');
231 equals( elem.style.paddingLeft, '1px', 'attr({...})');
232 equals( elem.style.paddingRight, '1px', 'attr({...})');
236 test("attr(String, Object) - Loaded via XML document", function() {
239 jQuery.get('data/dashboard.xml', function(xml) {
241 jQuery('tab', xml).each(function() {
242 titles.push(jQuery(this).attr('title'));
244 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
245 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
251 test("attr('tabindex')", function() {
254 // elements not natively tabbable
255 equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
256 equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
259 equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
260 equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
261 equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
263 // anchor without href
264 equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
265 equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
266 equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
269 test("attr('tabindex', value)", function() {
272 var element = jQuery('#divWithNoTabIndex');
273 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
275 // set a positive string
276 element.attr('tabindex', '1');
277 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
280 element.attr('tabindex', '0');
281 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
283 // set a negative string
284 element.attr('tabindex', '-1');
285 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
287 // set a positive number
288 element.attr('tabindex', 1);
289 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
292 element.attr('tabindex', 0);
293 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
295 // set a negative number
296 element.attr('tabindex', -1);
297 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
299 element = jQuery('#linkWithTabIndex');
300 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
302 element.attr('tabindex', -1);
303 equals(element.attr('tabindex'), -1, 'set negative tabindex');
306 test("removeAttr(String)", function() {
308 equals( jQuery('#mark').removeAttr( "class" )[0].className, "", "remove class" );
311 test("val()", function() {
314 document.getElementById('text1').value = "bla";
315 equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
319 equals( jQuery("#text1").val(), "Test", "Check for value of input element" );
320 // ticket #1714 this caused a JS error in IE
321 equals( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
322 ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
324 equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );
326 same( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );
328 equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );
330 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );
332 equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
334 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with no value attribute' );
336 jQuery('#select3').val("");
337 same( jQuery('#select3').val(), [''], 'Call val() on a multiple="multiple" select' );
339 same( jQuery('#select4').val(), [], 'Call val() on multiple="multiple" select with all disabled options' );
341 jQuery('#select4 optgroup').add('#select4 > [disabled]').attr('disabled', false);
342 same( jQuery('#select4').val(), ['2', '3'], 'Call val() on multiple="multiple" select with some disabled options' );
344 jQuery('#select4').attr('disabled', true);
345 same( jQuery('#select4').val(), ['2', '3'], 'Call val() on disabled multiple="multiple" select' );
347 equals( jQuery('#select5').val(), "3", "Check value on ambiguous select." );
349 jQuery('#select5').val(1);
350 equals( jQuery('#select5').val(), "1", "Check value on ambiguous select." );
352 jQuery('#select5').val(3);
353 equals( jQuery('#select5').val(), "3", "Check value on ambiguous select." );
355 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");
357 same( checks.serialize(), "", "Get unchecked values." );
359 equals( checks.eq(3).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
362 same( checks.serialize(), "test=2", "Get a single checked value." );
364 checks.val([ "1", "" ]);
365 same( checks.serialize(), "test=1&test=", "Get multiple checked values." );
367 checks.val([ "", "2" ]);
368 same( checks.serialize(), "test=2&test=", "Get multiple checked values." );
370 checks.val([ "1", "on" ]);
371 same( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
376 var testVal = function(valueObj) {
379 jQuery("#text1").val(valueObj( 'test' ));
380 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
382 jQuery("#text1").val(valueObj( undefined ));
383 equals( document.getElementById('text1').value, "", "Check for modified (via val(undefined)) value of input element" );
385 jQuery("#text1").val(valueObj( 67 ));
386 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
388 jQuery("#text1").val(valueObj( null ));
389 equals( document.getElementById('text1').value, "", "Check for modified (via val(null)) value of input element" );
391 jQuery("#select1").val(valueObj( "3" ));
392 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
394 jQuery("#select1").val(valueObj( 2 ));
395 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
397 jQuery("#select1").append("<option value='4'>four</option>");
398 jQuery("#select1").val(valueObj( 4 ));
399 equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
401 // using contents will get comments regular, text, and comment nodes
402 var j = jQuery("#nonnodes").contents();
403 j.val(valueObj( "asdf" ));
404 equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
405 j.removeAttr("value");
408 test("val(String/Number)", function() {
412 test("val(Function)", function() {
413 testVal(functionReturningObj);
416 test( "val(Array of Numbers) (Bug #7123)", function() {
418 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" />');
419 var elements = jQuery('input[name=arrayTest]').val([ 1, 2 ]);
420 ok( elements[0].checked, "First element was checked" );
421 ok( elements[1].checked, "Second element was checked" );
422 ok( !elements[2].checked, "Third element was unchecked" );
423 ok( !elements[3].checked, "Fourth element remained unchecked" );
428 test("val(Function) with incoming value", function() {
431 var oldVal = jQuery("#text1").val();
433 jQuery("#text1").val(function(i, val) {
434 equals( val, oldVal, "Make sure the incoming value is correct." );
438 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
440 oldVal = jQuery("#text1").val();
442 jQuery("#text1").val(function(i, val) {
443 equals( val, oldVal, "Make sure the incoming value is correct." );
447 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
449 oldVal = jQuery("#select1").val();
451 jQuery("#select1").val(function(i, val) {
452 equals( val, oldVal, "Make sure the incoming value is correct." );
456 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
458 oldVal = jQuery("#select1").val();
460 jQuery("#select1").val(function(i, val) {
461 equals( val, oldVal, "Make sure the incoming value is correct." );
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>");
469 oldVal = jQuery("#select1").val();
471 jQuery("#select1").val(function(i, val) {
472 equals( val, oldVal, "Make sure the incoming value is correct." );
476 equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
479 var testAddClass = function(valueObj) {
481 var div = jQuery("div");
482 div.addClass( valueObj("test") );
484 for ( var i = 0; i < div.size(); i++ ) {
485 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
487 ok( pass, "Add Class" );
489 // using contents will get regular, text, and comment nodes
490 var j = jQuery("#nonnodes").contents();
491 j.addClass( valueObj("asdf") );
492 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
494 div = jQuery("<div/>");
496 div.addClass( valueObj("test") );
497 equals( div.attr("class"), "test", "Make sure there's no extra whitespace." );
499 div.attr("class", " foo");
500 div.addClass( valueObj("test") );
501 equals( div.attr("class"), "foo test", "Make sure there's no extra whitespace." );
503 div.attr("class", "foo");
504 div.addClass( valueObj("bar baz") );
505 equals( div.attr("class"), "foo bar baz", "Make sure there isn't too much trimming." );
508 test("addClass(String)", function() {
509 testAddClass(bareObj);
512 test("addClass(Function)", function() {
513 testAddClass(functionReturningObj);
516 test("addClass(Function) with incoming value", function() {
519 var div = jQuery("div"), old = div.map(function(){
520 return jQuery(this).attr("class");
523 div.addClass(function(i, val) {
524 if ( this.id !== "_firebugConsole" ) {
525 equals( val, old[i], "Make sure the incoming value is correct." );
531 for ( var i = 0; i < div.size(); i++ ) {
532 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
534 ok( pass, "Add Class" );
537 var testRemoveClass = function(valueObj) {
540 var $divs = jQuery('div');
542 $divs.addClass("test").removeClass( valueObj("test") );
544 ok( !$divs.is('.test'), "Remove Class" );
547 $divs = jQuery('div');
549 $divs.addClass("test").addClass("foo").addClass("bar");
550 $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
552 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
555 $divs = jQuery('div');
557 // Make sure that a null value doesn't cause problems
558 $divs.eq(0).addClass("test").removeClass( valueObj(null) );
559 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
561 $divs.eq(0).addClass("test").removeClass( valueObj("") );
562 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
564 // using contents will get regular, text, and comment nodes
565 var j = jQuery("#nonnodes").contents();
566 j.removeClass( valueObj("asdf") );
567 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
569 var div = document.createElement("div");
570 div.className = " test foo ";
572 jQuery(div).removeClass( valueObj("foo") );
573 equals( div.className, "test", "Make sure remaining className is trimmed." );
575 div.className = " test ";
577 jQuery(div).removeClass( valueObj("test") );
578 equals( div.className, "", "Make sure there is nothing left after everything is removed." );
581 test("removeClass(String) - simple", function() {
582 testRemoveClass(bareObj);
585 test("removeClass(Function) - simple", function() {
586 testRemoveClass(functionReturningObj);
589 test("removeClass(Function) with incoming value", function() {
592 var $divs = jQuery('div').addClass("test"), old = $divs.map(function(){
593 return jQuery(this).attr("class");
596 $divs.removeClass(function(i, val) {
597 if ( this.id !== "_firebugConsole" ) {
598 equals( val, old[i], "Make sure the incoming value is correct." );
603 ok( !$divs.is('.test'), "Remove Class" );
608 var testToggleClass = function(valueObj) {
611 var e = jQuery("#firstp");
612 ok( !e.is(".test"), "Assert class not present" );
613 e.toggleClass( valueObj("test") );
614 ok( e.is(".test"), "Assert class present" );
615 e.toggleClass( valueObj("test") );
616 ok( !e.is(".test"), "Assert class not present" );
618 // class name with a boolean
619 e.toggleClass( valueObj("test"), false );
620 ok( !e.is(".test"), "Assert class not present" );
621 e.toggleClass( valueObj("test"), true );
622 ok( e.is(".test"), "Assert class present" );
623 e.toggleClass( valueObj("test"), false );
624 ok( !e.is(".test"), "Assert class not present" );
626 // multiple class names
627 e.addClass("testA testB");
628 ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
629 e.toggleClass( valueObj("testB testC") );
630 ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
631 e.toggleClass( valueObj("testA testC") );
632 ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
634 // toggleClass storage
636 ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
637 e.addClass("testD testE");
638 ok( e.is(".testD.testE"), "Assert class present" );
640 ok( !e.is(".testD.testE"), "Assert class not present" );
641 ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
643 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
644 e.toggleClass(false);
645 ok( !e.is(".testD.testE"), "Assert class not present" );
647 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
649 e.toggleClass(false);
651 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
656 e.removeClass("testD");
657 e.removeData('__className__');
660 test("toggleClass(String|boolean|undefined[, boolean])", function() {
661 testToggleClass(bareObj);
664 test("toggleClass(Function[, boolean])", function() {
665 testToggleClass(functionReturningObj);
668 test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
671 var e = jQuery("#firstp"), old = e.attr("class");
672 ok( !e.is(".test"), "Assert class not present" );
674 e.toggleClass(function(i, val) {
675 equals( val, old, "Make sure the incoming value is correct." );
678 ok( e.is(".test"), "Assert class present" );
680 old = e.attr("class");
682 e.toggleClass(function(i, val) {
683 equals( val, old, "Make sure the incoming value is correct." );
686 ok( !e.is(".test"), "Assert class not present" );
688 old = e.attr("class");
690 // class name with a boolean
691 e.toggleClass(function(i, val, state) {
692 equals( val, old, "Make sure the incoming value is correct." );
693 equals( state, false, "Make sure that the state is passed in." );
696 ok( !e.is(".test"), "Assert class not present" );
698 old = e.attr("class");
700 e.toggleClass(function(i, val, state) {
701 equals( val, old, "Make sure the incoming value is correct." );
702 equals( state, true, "Make sure that the state is passed in." );
705 ok( e.is(".test"), "Assert class present" );
707 old = e.attr("class");
709 e.toggleClass(function(i, val, state) {
710 equals( val, old, "Make sure the incoming value is correct." );
711 equals( state, false, "Make sure that the state is passed in." );
714 ok( !e.is(".test"), "Assert class not present" );
717 e.removeClass("test");
718 e.removeData('__className__');
721 test("addClass, removeClass, hasClass", function() {
724 var jq = jQuery("<p>Hi</p>"), x = jq[0];
727 equals( x.className, "hi", "Check single added class" );
729 jq.addClass("foo bar");
730 equals( x.className, "hi foo bar", "Check more added classes" );
733 equals( x.className, "", "Remove all classes" );
735 jq.addClass("hi foo bar");
736 jq.removeClass("foo");
737 equals( x.className, "hi bar", "Check removal of one class" );
739 ok( jq.hasClass("hi"), "Check has1" );
740 ok( jq.hasClass("bar"), "Check has2" );
742 var jq = jQuery("<p class='class1\nclass2\tcla.ss3\n'></p>");
743 ok( jq.hasClass("class1"), "Check hasClass with carriage return" );
744 ok( jq.is(".class1"), "Check is with carriage return" );
745 ok( jq.hasClass("class2"), "Check hasClass with tab" );
746 ok( jq.is(".class2"), "Check is with tab" );
747 ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" );
749 jq.removeClass("class2");
750 ok( jq.hasClass("class2")==false, "Check the class has been properly removed" );
751 jq.removeClass("cla");
752 ok( jq.hasClass("cla.ss3"), "Check the dotted class has not been removed" );
753 jq.removeClass("cla.ss3");
754 ok( jq.hasClass("cla.ss3")==false, "Check the dotted class has been removed" );