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)." );
98 test("attr(String) in XML Files", function() {
101 jQuery.get("data/dashboard.xml", function(xml) {
102 equals( jQuery("locations", xml).attr("class"), "foo", "Check class attribute in XML document" );
103 equals( jQuery("location", xml).attr("for"), "bar", "Check for attribute in XML document" );
109 test("attr(String, Function)", function() {
111 equals( jQuery('#text1').attr('value', function() { return this.id ;})[0].value, "text1", "Set value from id" );
112 equals( jQuery('#text1').attr('title', function(i) { return i }).attr('title'), "0", "Set value with an index");
115 test("attr(Hash)", function() {
118 jQuery("div").attr({foo: 'baz', zoo: 'ping'}).each(function(){
119 if ( this.getAttribute('foo') != "baz" && this.getAttribute('zoo') != "ping" ) pass = false;
121 ok( pass, "Set Multiple Attributes" );
122 equals( jQuery('#text1').attr({'value': function() { return this.id; }})[0].value, "text1", "Set attribute to computed value #1" );
123 equals( jQuery('#text1').attr({'title': function(i) { return i; }}).attr('title'), "0", "Set attribute to computed value #2");
127 test("attr(String, Object)", function() {
130 var div = jQuery("div").attr("foo", "bar"),
133 for ( var i = 0; i < div.size(); i++ ) {
134 if ( div.get(i).getAttribute('foo') != "bar" ){
140 equals( fail, false, "Set Attribute, the #"+fail+" element didn't get the attribute 'foo'" );
142 // Fails on IE since recent changes to .attr()
143 // ok( jQuery("#foo").attr({"width": null}), "Try to set an attribute to nothing" );
145 jQuery("#name").attr('name', 'something');
146 equals( jQuery("#name").attr('name'), 'something', 'Set name attribute' );
147 jQuery("#name").attr('name', null);
148 equals( jQuery("#name").attr('title'), '', 'Remove name attribute' );
149 jQuery("#check2").attr('checked', true);
150 equals( document.getElementById('check2').checked, true, 'Set checked attribute' );
151 jQuery("#check2").attr('checked', false);
152 equals( document.getElementById('check2').checked, false, 'Set checked attribute' );
153 jQuery("#text1").attr('readonly', true);
154 equals( document.getElementById('text1').readOnly, true, 'Set readonly attribute' );
155 jQuery("#text1").attr('readonly', false);
156 equals( document.getElementById('text1').readOnly, false, 'Set readonly attribute' );
157 jQuery("#name").attr('maxlength', '5');
158 equals( document.getElementById('name').maxLength, '5', 'Set maxlength attribute' );
159 jQuery("#name").attr('maxLength', '10');
160 equals( document.getElementById('name').maxLength, '10', 'Set maxlength attribute' );
162 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>"),
163 td = table.find('td:first');
164 td.attr("rowspan", "2");
165 equals( td[0].rowSpan, 2, "Check rowspan is correctly set" );
166 td.attr("colspan", "2");
167 equals( td[0].colSpan, 2, "Check colspan is correctly set" );
168 table.attr("cellspacing", "2");
169 equals( table[0].cellSpacing, 2, "Check cellspacing is correctly set" );
172 jQuery("#name").attr('someAttr', '0');
173 equals( jQuery("#name").attr('someAttr'), '0', 'Set attribute to a string of "0"' );
174 jQuery("#name").attr('someAttr', 0);
175 equals( jQuery("#name").attr('someAttr'), 0, 'Set attribute to the number 0' );
176 jQuery("#name").attr('someAttr', 1);
177 equals( jQuery("#name").attr('someAttr'), 1, 'Set attribute to the number 1' );
179 // using contents will get comments regular, text, and comment nodes
180 var j = jQuery("#nonnodes").contents();
182 j.attr("name", "attrvalue");
183 equals( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
184 j.removeAttr("name");
188 var type = jQuery("#check2").attr('type');
191 jQuery("#check2").attr('type','hidden');
195 ok( thrown, "Exception thrown when trying to change type property" );
196 equals( type, jQuery("#check2").attr('type'), "Verify that you can't change the type of an input element" );
198 var check = document.createElement("input");
201 jQuery(check).attr('type','checkbox');
205 ok( thrown, "Exception thrown when trying to change type property" );
206 equals( "checkbox", jQuery(check).attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
208 var check = jQuery("<input />");
211 check.attr('type','checkbox');
215 ok( thrown, "Exception thrown when trying to change type property" );
216 equals( "checkbox", check.attr('type'), "Verify that you can change the type of an input element that isn't in the DOM" );
218 var button = jQuery("#button");
221 button.attr('type','submit');
225 ok( thrown, "Exception thrown when trying to change type property" );
226 equals( "button", button.attr('type'), "Verify that you can't change the type of a button element" );
229 test("attr(jquery_method)", function(){
232 var $elem = jQuery("<div />"),
236 $elem.attr({'html': 'foo'}, true);
237 equals( elem.innerHTML, 'foo', 'attr(html)');
239 $elem.attr({'text': 'bar'}, true);
240 equals( elem.innerHTML, 'bar', 'attr(text)');
242 $elem.attr({'css': {color:'red'}}, true);
243 ok( /^(#ff0000|red)$/i.test(elem.style.color), 'attr(css)');
245 $elem.attr({'height': 10}, true);
246 equals( elem.style.height, '10px', 'attr(height)');
248 // Multiple attributes
252 css:{ paddingLeft:1, paddingRight:1 }
255 equals( elem.style.width, '10px', 'attr({...})');
256 equals( elem.style.paddingLeft, '1px', 'attr({...})');
257 equals( elem.style.paddingRight, '1px', 'attr({...})');
261 test("attr(String, Object) - Loaded via XML document", function() {
264 jQuery.get('data/dashboard.xml', function(xml) {
266 jQuery('tab', xml).each(function() {
267 titles.push(jQuery(this).attr('title'));
269 equals( titles[0], 'Location', 'attr() in XML context: Check first title' );
270 equals( titles[1], 'Users', 'attr() in XML context: Check second title' );
276 test("attr('tabindex')", function() {
279 // elements not natively tabbable
280 equals(jQuery('#listWithTabIndex').attr('tabindex'), 5, 'not natively tabbable, with tabindex set to 0');
281 equals(jQuery('#divWithNoTabIndex').attr('tabindex'), undefined, 'not natively tabbable, no tabindex set');
284 equals(jQuery('#linkWithNoTabIndex').attr('tabindex'), 0, 'anchor with href, no tabindex set');
285 equals(jQuery('#linkWithTabIndex').attr('tabindex'), 2, 'anchor with href, tabindex set to 2');
286 equals(jQuery('#linkWithNegativeTabIndex').attr('tabindex'), -1, 'anchor with href, tabindex set to -1');
288 // anchor without href
289 equals(jQuery('#linkWithNoHrefWithNoTabIndex').attr('tabindex'), undefined, 'anchor without href, no tabindex set');
290 equals(jQuery('#linkWithNoHrefWithTabIndex').attr('tabindex'), 1, 'anchor without href, tabindex set to 2');
291 equals(jQuery('#linkWithNoHrefWithNegativeTabIndex').attr('tabindex'), -1, 'anchor without href, no tabindex set');
294 test("attr('tabindex', value)", function() {
297 var element = jQuery('#divWithNoTabIndex');
298 equals(element.attr('tabindex'), undefined, 'start with no tabindex');
300 // set a positive string
301 element.attr('tabindex', '1');
302 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (string)');
305 element.attr('tabindex', '0');
306 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (string)');
308 // set a negative string
309 element.attr('tabindex', '-1');
310 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (string)');
312 // set a positive number
313 element.attr('tabindex', 1);
314 equals(element.attr('tabindex'), 1, 'set tabindex to 1 (number)');
317 element.attr('tabindex', 0);
318 equals(element.attr('tabindex'), 0, 'set tabindex to 0 (number)');
320 // set a negative number
321 element.attr('tabindex', -1);
322 equals(element.attr('tabindex'), -1, 'set tabindex to -1 (number)');
324 element = jQuery('#linkWithTabIndex');
325 equals(element.attr('tabindex'), 2, 'start with tabindex 2');
327 element.attr('tabindex', -1);
328 equals(element.attr('tabindex'), -1, 'set negative tabindex');
331 test("removeAttr(String)", function() {
333 equals( jQuery('#mark').removeAttr( "class" )[0].className, "", "remove class" );
336 test("val()", function() {
339 document.getElementById('text1').value = "bla";
340 equals( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
344 equals( jQuery("#text1").val(), "Test", "Check for value of input element" );
345 // ticket #1714 this caused a JS error in IE
346 equals( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
347 ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
349 equals( jQuery('#select2').val(), '3', 'Call val() on a single="single" select' );
351 same( jQuery('#select3').val(), ['1', '2'], 'Call val() on a multiple="multiple" select' );
353 equals( jQuery('#option3c').val(), '2', 'Call val() on a option element with value' );
355 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with empty value' );
357 equals( jQuery('#option3e').val(), 'no value', 'Call val() on a option element with no value attribute' );
359 equals( jQuery('#option3a').val(), '', 'Call val() on a option element with no value attribute' );
361 jQuery('#select3').val("");
362 same( jQuery('#select3').val(), [''], 'Call val() on a multiple="multiple" select' );
364 same( jQuery('#select4').val(), [], 'Call val() on multiple="multiple" select with all disabled options' );
366 jQuery('#select4 optgroup').add('#select4 > [disabled]').attr('disabled', false);
367 same( jQuery('#select4').val(), ['2', '3'], 'Call val() on multiple="multiple" select with some disabled options' );
369 jQuery('#select4').attr('disabled', true);
370 same( jQuery('#select4').val(), ['2', '3'], 'Call val() on disabled multiple="multiple" select' );
372 equals( jQuery('#select5').val(), "3", "Check value on ambiguous select." );
374 jQuery('#select5').val(1);
375 equals( jQuery('#select5').val(), "1", "Check value on ambiguous select." );
377 jQuery('#select5').val(3);
378 equals( jQuery('#select5').val(), "3", "Check value on ambiguous select." );
380 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");
382 same( checks.serialize(), "", "Get unchecked values." );
384 equals( checks.eq(3).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
387 same( checks.serialize(), "test=2", "Get a single checked value." );
389 checks.val([ "1", "" ]);
390 same( checks.serialize(), "test=1&test=", "Get multiple checked values." );
392 checks.val([ "", "2" ]);
393 same( checks.serialize(), "test=2&test=", "Get multiple checked values." );
395 checks.val([ "1", "on" ]);
396 same( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
401 var testVal = function(valueObj) {
404 jQuery("#text1").val(valueObj( 'test' ));
405 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
407 jQuery("#text1").val(valueObj( undefined ));
408 equals( document.getElementById('text1').value, "", "Check for modified (via val(undefined)) value of input element" );
410 jQuery("#text1").val(valueObj( 67 ));
411 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
413 jQuery("#text1").val(valueObj( null ));
414 equals( document.getElementById('text1').value, "", "Check for modified (via val(null)) value of input element" );
416 jQuery("#select1").val(valueObj( "3" ));
417 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
419 jQuery("#select1").val(valueObj( 2 ));
420 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
422 jQuery("#select1").append("<option value='4'>four</option>");
423 jQuery("#select1").val(valueObj( 4 ));
424 equals( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
426 // using contents will get comments regular, text, and comment nodes
427 var j = jQuery("#nonnodes").contents();
428 j.val(valueObj( "asdf" ));
429 equals( j.val(), "asdf", "Check node,textnode,comment with val()" );
430 j.removeAttr("value");
433 test("val(String/Number)", function() {
437 test("val(Function)", function() {
438 testVal(functionReturningObj);
441 test( "val(Array of Numbers) (Bug #7123)", function() {
443 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" />');
444 var elements = jQuery('input[name=arrayTest]').val([ 1, 2 ]);
445 ok( elements[0].checked, "First element was checked" );
446 ok( elements[1].checked, "Second element was checked" );
447 ok( !elements[2].checked, "Third element was unchecked" );
448 ok( !elements[3].checked, "Fourth element remained unchecked" );
453 test("val(Function) with incoming value", function() {
456 var oldVal = jQuery("#text1").val();
458 jQuery("#text1").val(function(i, val) {
459 equals( val, oldVal, "Make sure the incoming value is correct." );
463 equals( document.getElementById('text1').value, "test", "Check for modified (via val(String)) value of input element" );
465 oldVal = jQuery("#text1").val();
467 jQuery("#text1").val(function(i, val) {
468 equals( val, oldVal, "Make sure the incoming value is correct." );
472 equals( document.getElementById('text1').value, "67", "Check for modified (via val(Number)) value of input element" );
474 oldVal = jQuery("#select1").val();
476 jQuery("#select1").val(function(i, val) {
477 equals( val, oldVal, "Make sure the incoming value is correct." );
481 equals( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
483 oldVal = jQuery("#select1").val();
485 jQuery("#select1").val(function(i, val) {
486 equals( val, oldVal, "Make sure the incoming value is correct." );
490 equals( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
492 jQuery("#select1").append("<option value='4'>four</option>");
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(), "4", "Should be possible to set the val() to a newly created option" );
504 var testAddClass = function(valueObj) {
506 var div = jQuery("div");
507 div.addClass( valueObj("test") );
509 for ( var i = 0; i < div.size(); i++ ) {
510 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
512 ok( pass, "Add Class" );
514 // using contents will get regular, text, and comment nodes
515 var j = jQuery("#nonnodes").contents();
516 j.addClass( valueObj("asdf") );
517 ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
519 div = jQuery("<div/>");
521 div.addClass( valueObj("test") );
522 equals( div.attr("class"), "test", "Make sure there's no extra whitespace." );
524 div.attr("class", " foo");
525 div.addClass( valueObj("test") );
526 equals( div.attr("class"), "foo test", "Make sure there's no extra whitespace." );
528 div.attr("class", "foo");
529 div.addClass( valueObj("bar baz") );
530 equals( div.attr("class"), "foo bar baz", "Make sure there isn't too much trimming." );
533 test("addClass(String)", function() {
534 testAddClass(bareObj);
537 test("addClass(Function)", function() {
538 testAddClass(functionReturningObj);
541 test("addClass(Function) with incoming value", function() {
544 var div = jQuery("div"), old = div.map(function(){
545 return jQuery(this).attr("class");
548 div.addClass(function(i, val) {
549 if ( this.id !== "_firebugConsole" ) {
550 equals( val, old[i], "Make sure the incoming value is correct." );
556 for ( var i = 0; i < div.size(); i++ ) {
557 if ( div.get(i).className.indexOf("test") == -1 ) pass = false;
559 ok( pass, "Add Class" );
562 var testRemoveClass = function(valueObj) {
565 var $divs = jQuery('div');
567 $divs.addClass("test").removeClass( valueObj("test") );
569 ok( !$divs.is('.test'), "Remove Class" );
572 $divs = jQuery('div');
574 $divs.addClass("test").addClass("foo").addClass("bar");
575 $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
577 ok( !$divs.is('.test,.bar,.foo'), "Remove multiple classes" );
580 $divs = jQuery('div');
582 // Make sure that a null value doesn't cause problems
583 $divs.eq(0).addClass("test").removeClass( valueObj(null) );
584 ok( $divs.eq(0).is('.test'), "Null value passed to removeClass" );
586 $divs.eq(0).addClass("test").removeClass( valueObj("") );
587 ok( $divs.eq(0).is('.test'), "Empty string passed to removeClass" );
589 // using contents will get regular, text, and comment nodes
590 var j = jQuery("#nonnodes").contents();
591 j.removeClass( valueObj("asdf") );
592 ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
594 var div = document.createElement("div");
595 div.className = " test foo ";
597 jQuery(div).removeClass( valueObj("foo") );
598 equals( div.className, "test", "Make sure remaining className is trimmed." );
600 div.className = " test ";
602 jQuery(div).removeClass( valueObj("test") );
603 equals( div.className, "", "Make sure there is nothing left after everything is removed." );
606 test("removeClass(String) - simple", function() {
607 testRemoveClass(bareObj);
610 test("removeClass(Function) - simple", function() {
611 testRemoveClass(functionReturningObj);
614 test("removeClass(Function) with incoming value", function() {
617 var $divs = jQuery('div').addClass("test"), old = $divs.map(function(){
618 return jQuery(this).attr("class");
621 $divs.removeClass(function(i, val) {
622 if ( this.id !== "_firebugConsole" ) {
623 equals( val, old[i], "Make sure the incoming value is correct." );
628 ok( !$divs.is('.test'), "Remove Class" );
633 var testToggleClass = function(valueObj) {
636 var e = jQuery("#firstp");
637 ok( !e.is(".test"), "Assert class not present" );
638 e.toggleClass( valueObj("test") );
639 ok( e.is(".test"), "Assert class present" );
640 e.toggleClass( valueObj("test") );
641 ok( !e.is(".test"), "Assert class not present" );
643 // class name with a boolean
644 e.toggleClass( valueObj("test"), false );
645 ok( !e.is(".test"), "Assert class not present" );
646 e.toggleClass( valueObj("test"), true );
647 ok( e.is(".test"), "Assert class present" );
648 e.toggleClass( valueObj("test"), false );
649 ok( !e.is(".test"), "Assert class not present" );
651 // multiple class names
652 e.addClass("testA testB");
653 ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
654 e.toggleClass( valueObj("testB testC") );
655 ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
656 e.toggleClass( valueObj("testA testC") );
657 ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
659 // toggleClass storage
661 ok( e.get(0).className === "", "Assert class is empty (data was empty)" );
662 e.addClass("testD testE");
663 ok( e.is(".testD.testE"), "Assert class present" );
665 ok( !e.is(".testD.testE"), "Assert class not present" );
666 ok( e.data('__className__') === 'testD testE', "Assert data was stored" );
668 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
669 e.toggleClass(false);
670 ok( !e.is(".testD.testE"), "Assert class not present" );
672 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
674 e.toggleClass(false);
676 ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
681 e.removeClass("testD");
682 e.removeData('__className__');
685 test("toggleClass(String|boolean|undefined[, boolean])", function() {
686 testToggleClass(bareObj);
689 test("toggleClass(Function[, boolean])", function() {
690 testToggleClass(functionReturningObj);
693 test("toggleClass(Fucntion[, boolean]) with incoming value", function() {
696 var e = jQuery("#firstp"), old = e.attr("class");
697 ok( !e.is(".test"), "Assert class not present" );
699 e.toggleClass(function(i, val) {
700 equals( val, old, "Make sure the incoming value is correct." );
703 ok( e.is(".test"), "Assert class present" );
705 old = e.attr("class");
707 e.toggleClass(function(i, val) {
708 equals( val, old, "Make sure the incoming value is correct." );
711 ok( !e.is(".test"), "Assert class not present" );
713 old = e.attr("class");
715 // class name with a boolean
716 e.toggleClass(function(i, val, state) {
717 equals( val, old, "Make sure the incoming value is correct." );
718 equals( state, false, "Make sure that the state is passed in." );
721 ok( !e.is(".test"), "Assert class not present" );
723 old = e.attr("class");
725 e.toggleClass(function(i, val, state) {
726 equals( val, old, "Make sure the incoming value is correct." );
727 equals( state, true, "Make sure that the state is passed in." );
730 ok( e.is(".test"), "Assert class present" );
732 old = e.attr("class");
734 e.toggleClass(function(i, val, state) {
735 equals( val, old, "Make sure the incoming value is correct." );
736 equals( state, false, "Make sure that the state is passed in." );
739 ok( !e.is(".test"), "Assert class not present" );
742 e.removeClass("test");
743 e.removeData('__className__');
746 test("addClass, removeClass, hasClass", function() {
749 var jq = jQuery("<p>Hi</p>"), x = jq[0];
752 equals( x.className, "hi", "Check single added class" );
754 jq.addClass("foo bar");
755 equals( x.className, "hi foo bar", "Check more added classes" );
758 equals( x.className, "", "Remove all classes" );
760 jq.addClass("hi foo bar");
761 jq.removeClass("foo");
762 equals( x.className, "hi bar", "Check removal of one class" );
764 ok( jq.hasClass("hi"), "Check has1" );
765 ok( jq.hasClass("bar"), "Check has2" );
767 var jq = jQuery("<p class='class1\nclass2\tcla.ss3\n'></p>");
768 ok( jq.hasClass("class1"), "Check hasClass with carriage return" );
769 ok( jq.is(".class1"), "Check is with carriage return" );
770 ok( jq.hasClass("class2"), "Check hasClass with tab" );
771 ok( jq.is(".class2"), "Check is with tab" );
772 ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" );
774 jq.removeClass("class2");
775 ok( jq.hasClass("class2")==false, "Check the class has been properly removed" );
776 jq.removeClass("cla");
777 ok( jq.hasClass("cla.ss3"), "Check the dotted class has not been removed" );
778 jq.removeClass("cla.ss3");
779 ok( jq.hasClass("cla.ss3")==false, "Check the dotted class has been removed" );