Convert all jQuery.removeData(foo, bar, true) calls to jQuery._remove(foo, bar)
[jquery.git] / test / unit / attributes.js
blob1c878f37a8fa44c5cab09f3264799e9701471609
1 module( "attributes", {
2         teardown: moduleTeardown
3 });
5 var bareObj = function( value ) {
6         return value;
7 };
9 var functionReturningObj = function( value ) {
10         return (function() {
11                 return value;
12         });
16         ======== local reference =======
17         bareObj and functionReturningObj can be used to test passing functions to setters
18         See testVal below for an example
20         bareObj( value );
21                 This function returns whatever value is passed in
23         functionReturningObj( value );
24                 Returns a function that returns the value
27 test( "jQuery.propFix integrity test", function() {
28         expect( 1 );
30         //  This must be maintained and equal jQuery.attrFix when appropriate
31         //  Ensure that accidental or erroneous property
32         //  overwrites don't occur
33         //  This is simply for better code coverage and future proofing.
34         var props = {
35                 "tabindex": "tabIndex",
36                 "readonly": "readOnly",
37                 "for": "htmlFor",
38                 "class": "className",
39                 "maxlength": "maxLength",
40                 "cellspacing": "cellSpacing",
41                 "cellpadding": "cellPadding",
42                 "rowspan": "rowSpan",
43                 "colspan": "colSpan",
44                 "usemap": "useMap",
45                 "frameborder": "frameBorder",
46                 "contenteditable": "contentEditable"
47         };
49         if ( !jQuery.support.enctype ) {
50                 props.enctype = "encoding";
51         }
53         deepEqual( props, jQuery.propFix, "jQuery.propFix passes integrity check" );
54 });
56 test( "attr(String)", function() {
57         expect( 46 );
59         equal( jQuery("#text1").attr("type"), "text", "Check for type attribute" );
60         equal( jQuery("#radio1").attr("type"), "radio", "Check for type attribute" );
61         equal( jQuery("#check1").attr("type"), "checkbox", "Check for type attribute" );
62         equal( jQuery("#simon1").attr("rel"), "bookmark", "Check for rel attribute" );
63         equal( jQuery("#google").attr("title"), "Google!", "Check for title attribute" );
64         equal( jQuery("#mark").attr("hreflang"), "en", "Check for hreflang attribute" );
65         equal( jQuery("#en").attr("lang"), "en", "Check for lang attribute" );
66         equal( jQuery("#simon").attr("class"), "blog link", "Check for class attribute" );
67         equal( jQuery("#name").attr("name"), "name", "Check for name attribute" );
68         equal( jQuery("#text1").attr("name"), "action", "Check for name attribute" );
69         ok( jQuery("#form").attr("action").indexOf("formaction") >= 0, "Check for action attribute" );
70         equal( jQuery("#text1").attr("value", "t").attr("value"), "t", "Check setting the value attribute" );
71         equal( jQuery("<div value='t'></div>").attr("value"), "t", "Check setting custom attr named 'value' on a div" );
72         equal( jQuery("#form").attr("blah", "blah").attr("blah"), "blah", "Set non-existant attribute on a form" );
73         equal( jQuery("#foo").attr("height"), undefined, "Non existent height attribute should return undefined" );
75         // [7472] & [3113] (form contains an input with name="action" or name="id")
76         var extras = jQuery("<input name='id' name='name' /><input id='target' name='target' />").appendTo("#testForm");
77         equal( jQuery("#form").attr("action","newformaction").attr("action"), "newformaction", "Check that action attribute was changed" );
78         equal( jQuery("#testForm").attr("target"), undefined, "Retrieving target does not equal the input with name=target" );
79         equal( jQuery("#testForm").attr("target", "newTarget").attr("target"), "newTarget", "Set target successfully on a form" );
80         equal( jQuery("#testForm").removeAttr("id").attr("id"), undefined, "Retrieving id does not equal the input with name=id after id is removed [#7472]" );
81         // Bug #3685 (form contains input with name="name")
82         equal( jQuery("#testForm").attr("name"), undefined, "Retrieving name does not retrieve input with name=name" );
83         extras.remove();
85         equal( jQuery("#text1").attr("maxlength"), "30", "Check for maxlength attribute" );
86         equal( jQuery("#text1").attr("maxLength"), "30", "Check for maxLength attribute" );
87         equal( jQuery("#area1").attr("maxLength"), "30", "Check for maxLength attribute" );
89         // using innerHTML in IE causes href attribute to be serialized to the full path
90         jQuery("<a/>").attr({
91                 "id": "tAnchor5",
92                 "href": "#5"
93         }).appendTo("#qunit-fixture");
94         equal( jQuery("#tAnchor5").attr("href"), "#5", "Check for non-absolute href (an anchor)" );
96         // list attribute is readonly by default in browsers that support it
97         jQuery("#list-test").attr( "list", "datalist" );
98         equal( jQuery("#list-test").attr("list"), "datalist", "Check setting list attribute" );
100         // Related to [5574] and [5683]
101         var body = document.body, $body = jQuery( body );
103         strictEqual( $body.attr("foo"), undefined, "Make sure that a non existent attribute returns undefined" );
105         body.setAttribute( "foo", "baz" );
106         equal( $body.attr("foo"), "baz", "Make sure the dom attribute is retrieved when no expando is found" );
108         $body.attr( "foo","cool" );
109         equal( $body.attr("foo"), "cool", "Make sure that setting works well when both expando and dom attribute are available" );
111         body.removeAttribute("foo"); // Cleanup
113         var select = document.createElement("select"),
114                 optgroup = document.createElement("optgroup"),
115                 option = document.createElement("option");
117         optgroup.appendChild( option );
118         select.appendChild( optgroup );
120         equal( jQuery( option ).attr("selected"), "selected", "Make sure that a single option is selected, even when in an optgroup." );
122         var $img = jQuery("<img style='display:none' width='215' height='53' src='http://static.jquery.com/files/rocker/images/logo_jquery_215x53.gif'/>").appendTo("body");
123         equal( $img.attr("width"), "215", "Retrieve width attribute an an element with display:none." );
124         equal( $img.attr("height"), "53", "Retrieve height attribute an an element with display:none." );
126         // Check for style support
127         ok( !!~jQuery("#dl").attr("style").indexOf("position"), "Check style attribute getter, also normalize css props to lowercase" );
128         ok( !!~jQuery("#foo").attr("style", "position:absolute;").attr("style").indexOf("position"), "Check style setter" );
130         // Check value on button element (#1954)
131         var $button = jQuery("<button value='foobar'>text</button>").insertAfter("#button");
132         equal( $button.attr("value"), "foobar", "Value retrieval on a button does not return innerHTML" );
133         equal( $button.attr("value", "baz").html(), "text", "Setting the value does not change innerHTML" );
135         // Attributes with a colon on a table element (#1591)
136         equal( jQuery("#table").attr("test:attrib"), undefined, "Retrieving a non-existent attribute on a table with a colon does not throw an error." );
137         equal( jQuery("#table").attr( "test:attrib", "foobar" ).attr("test:attrib"), "foobar", "Setting an attribute on a table with a colon does not throw an error." );
139         var $form = jQuery("<form class='something'></form>").appendTo("#qunit-fixture");
140         equal( $form.attr("class"), "something", "Retrieve the class attribute on a form." );
142         var $a = jQuery("<a href='#' onclick='something()'>Click</a>").appendTo("#qunit-fixture");
143         equal( $a.attr("onclick"), "something()", "Retrieve ^on attribute without anonymous function wrapper." );
145         ok( jQuery("<div/>").attr("doesntexist") === undefined, "Make sure undefined is returned when no attribute is found." );
146         ok( jQuery("<div/>").attr("title") === undefined, "Make sure undefined is returned when no attribute is found." );
147         equal( jQuery("<div/>").attr( "title", "something" ).attr("title"), "something", "Set the title attribute." );
148         ok( jQuery().attr("doesntexist") === undefined, "Make sure undefined is returned when no element is there." );
149         equal( jQuery("<div/>").attr("value"), undefined, "An unset value on a div returns undefined." );
150         equal( jQuery("<input/>").attr("value"), "", "An unset value on an input returns current value." );
152         $form = jQuery("#form").attr( "enctype", "multipart/form-data" );
153         equal( $form.prop("enctype"), "multipart/form-data", "Set the enctype of a form (encoding in IE6/7 #6743)" );
156 test( "attr(String) in XML Files", function() {
157         expect( 3 );
158         var xml = createDashboardXML();
159         equal( jQuery( "locations", xml ).attr("class"), "foo", "Check class attribute in XML document" );
160         equal( jQuery( "location", xml ).attr("for"), "bar", "Check for attribute in XML document" );
161         equal( jQuery( "location", xml ).attr("checked"), "different", "Check that hooks are not attached in XML document" );
164 test( "attr(String, Function)", function() {
165         expect( 2 );
167         equal(
168                 jQuery("#text1").attr( "value", function() {
169                         return this.id;
170                 })[0].value,
171                 "text1",
172                 "Set value from id"
173         );
175         equal(
176                 jQuery("#text1").attr( "title", function(i) {
177                         return i;
178                 }).attr("title"),
179                 "0",
180                 "Set value with an index"
181         );
184 test( "attr(Hash)", function() {
185         expect( 3 );
186         var pass = true;
187         jQuery("div").attr({
188                 "foo": "baz",
189                 "zoo": "ping"
190         }).each(function() {
191                 if ( this.getAttribute("foo") != "baz" && this.getAttribute("zoo") != "ping" ) {
192                         pass = false;
193                 }
194         });
196         ok( pass, "Set Multiple Attributes" );
198         equal(
199                 jQuery("#text1").attr({
200                         "value": function() {
201                                 return this["id"];
202                         }})[0].value,
203                 "text1",
204                 "Set attribute to computed value #1"
205         );
207         equal(
208                 jQuery("#text1").attr({
209                         "title": function(i) {
210                                 return i;
211                         }
212                 }).attr("title"),
213                 "0",
214                 "Set attribute to computed value #2"
215         );
218 test( "attr(String, Object)", function() {
219         expect( 81 );
221         var div = jQuery("div").attr("foo", "bar"),
222                 fail = false;
224         for ( var i = 0; i < div.size(); i++ ) {
225                 if ( div.get( i ).getAttribute("foo") != "bar" ) {
226                         fail = i;
227                         break;
228                 }
229         }
231         equal( fail, false, "Set Attribute, the #" + fail + " element didn't get the attribute 'foo'" );
233         ok(
234                 jQuery("#foo").attr({
235                         "width": null
236                 }),
237                 "Try to set an attribute to nothing"
238         );
240         jQuery("#name").attr( "name", "something" );
241         equal( jQuery("#name").attr("name"), "something", "Set name attribute" );
242         jQuery("#name").attr( "name", null );
243         equal( jQuery("#name").attr("name"), undefined, "Remove name attribute" );
244         var $input = jQuery( "<input>", {
245                 name: "something",
246                 id: "specified"
247         });
248         equal( $input.attr("name"), "something", "Check element creation gets/sets the name attribute." );
249         equal( $input.attr("id"), "specified", "Check element creation gets/sets the id attribute." );
251         jQuery("#check2").prop( "checked", true ).prop( "checked", false ).attr( "checked", true );
252         equal( document.getElementById("check2").checked, true, "Set checked attribute" );
253         equal( jQuery("#check2").prop("checked"), true, "Set checked attribute" );
254         equal( jQuery("#check2").attr("checked"), "checked", "Set checked attribute" );
255         jQuery("#check2").attr( "checked", false );
256         equal( document.getElementById("check2").checked, false, "Set checked attribute" );
257         equal( jQuery("#check2").prop("checked"), false, "Set checked attribute" );
258         equal( jQuery("#check2").attr("checked"), undefined, "Set checked attribute" );
259         jQuery("#text1").attr( "readonly", true );
260         equal( document.getElementById("text1").readOnly, true, "Set readonly attribute" );
261         equal( jQuery("#text1").prop("readOnly"), true, "Set readonly attribute" );
262         equal( jQuery("#text1").attr("readonly"), "readonly", "Set readonly attribute" );
263         jQuery("#text1").attr( "readonly", false );
264         equal( document.getElementById("text1").readOnly, false, "Set readonly attribute" );
265         equal( jQuery("#text1").prop("readOnly"), false, "Set readonly attribute" );
266         equal( jQuery("#text1").attr("readonly"), undefined, "Set readonly attribute" );
268         jQuery("#check2").prop( "checked", true );
269         equal( document.getElementById("check2").checked, true, "Set checked attribute" );
270         equal( jQuery("#check2").prop("checked"), true, "Set checked attribute" );
271         equal( jQuery("#check2").attr("checked"), "checked", "Set checked attribute" );
272         jQuery("#check2").prop( "checked", false );
273         equal( document.getElementById("check2").checked, false, "Set checked attribute" );
274         equal( jQuery("#check2").prop("checked"), false, "Set checked attribute" );
275         equal( jQuery("#check2").attr("checked"), undefined, "Set checked attribute" );
277         jQuery("#check2").attr("checked", "checked");
278         equal( document.getElementById("check2").checked, true, "Set checked attribute with 'checked'" );
279         equal( jQuery("#check2").prop("checked"), true, "Set checked attribute" );
280         equal( jQuery("#check2").attr("checked"), "checked", "Set checked attribute" );
282         QUnit.reset();
284         var $radios = jQuery("#checkedtest").find("input[type='radio']");
285         $radios.eq( 1 ).click();
286         equal( $radios.eq( 1 ).prop("checked"), true, "Second radio was checked when clicked" );
287         equal( $radios.attr("checked"), $radios[ 0 ].checked ? "checked" : undefined, "Known booleans do not fall back to attribute presence (#10278)" );
289         jQuery("#text1").prop( "readOnly", true );
290         equal( document.getElementById("text1").readOnly, true, "Set readonly attribute" );
291         equal( jQuery("#text1").prop("readOnly"), true, "Set readonly attribute" );
292         equal( jQuery("#text1").attr("readonly"), "readonly", "Set readonly attribute" );
293         jQuery("#text1").prop( "readOnly", false );
294         equal( document.getElementById("text1").readOnly, false, "Set readonly attribute" );
295         equal( jQuery("#text1").prop("readOnly"), false, "Set readonly attribute" );
296         equal( jQuery("#text1").attr("readonly"), undefined, "Set readonly attribute" );
298         jQuery("#name").attr( "maxlength", "5" );
299         equal( document.getElementById("name").maxLength, 5, "Set maxlength attribute" );
300         jQuery("#name").attr( "maxLength", "10" );
301         equal( document.getElementById("name").maxLength, 10, "Set maxlength attribute" );
303         // HTML5 boolean attributes
304         var $text = jQuery("#text1").attr({
305                 "autofocus": true,
306                 "required": true
307         });
308         equal( $text.attr("autofocus"), "autofocus", "Set boolean attributes to the same name" );
309         equal( $text.attr( "autofocus", false ).attr("autofocus"), undefined, "Setting autofocus attribute to false removes it" );
310         equal( $text.attr("required"), "required", "Set boolean attributes to the same name" );
311         equal( $text.attr( "required", false ).attr("required"), undefined, "Setting required attribute to false removes it" );
313         var $details = jQuery("<details open></details>").appendTo("#qunit-fixture");
314         equal( $details.attr("open"), "open", "open attribute presense indicates true" );
315         equal( $details.attr( "open", false ).attr("open"), undefined, "Setting open attribute to false removes it" );
317         $text.attr( "data-something", true );
318         equal( $text.attr("data-something"), "true", "Set data attributes");
319         equal( $text.data("something"), true, "Setting data attributes are not affected by boolean settings");
320         $text.attr( "data-another", false );
321         equal( $text.attr("data-another"), "false", "Set data attributes");
322         equal( $text.data("another"), false, "Setting data attributes are not affected by boolean settings" );
323         equal( $text.attr( "aria-disabled", false ).attr("aria-disabled"), "false", "Setting aria attributes are not affected by boolean settings" );
324         $text.removeData("something").removeData("another").removeAttr("aria-disabled");
326         jQuery("#foo").attr("contenteditable", true);
327         equal( jQuery("#foo").attr("contenteditable"), "true", "Enumerated attributes are set properly" );
329         var attributeNode = document.createAttribute("irrelevant"),
330                 commentNode = document.createComment("some comment"),
331                 textNode = document.createTextNode("some text"),
332                 obj = {};
334         jQuery.each( [ commentNode, textNode, attributeNode ], function( i, elem ) {
335                 var $elem = jQuery( elem );
336                 $elem.attr( "nonexisting", "foo" );
337                 strictEqual( $elem.attr("nonexisting"), undefined, "attr(name, value) works correctly on comment and text nodes (bug #7500)." );
338         });
340         jQuery.each( [ window, document, obj, "#firstp" ], function( i, elem ) {
341                 var $elem = jQuery( elem );
342                 strictEqual( $elem.attr("nonexisting"), undefined, "attr works correctly for non existing attributes (bug #7500)." );
343                 equal( $elem.attr( "something", "foo" ).attr("something"), "foo", "attr falls back to prop on unsupported arguments" );
344         });
346         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>"),
347                 td = table.find("td:first");
348         td.attr( "rowspan", "2" );
349         equal( td[ 0 ]["rowSpan"], 2, "Check rowspan is correctly set" );
350         td.attr( "colspan", "2" );
351         equal( td[ 0 ]["colSpan"], 2, "Check colspan is correctly set" );
352         table.attr("cellspacing", "2");
353         equal( table[ 0 ]["cellSpacing"], "2", "Check cellspacing is correctly set" );
355         equal( jQuery("#area1").attr("value"), "foobar", "Value attribute retrieves the property for backwards compatibility." );
357         // for #1070
358         jQuery("#name").attr( "someAttr", "0" );
359         equal( jQuery("#name").attr("someAttr"), "0", "Set attribute to a string of '0'" );
360         jQuery("#name").attr( "someAttr", 0 );
361         equal( jQuery("#name").attr("someAttr"), "0", "Set attribute to the number 0" );
362         jQuery("#name").attr( "someAttr", 1 );
363         equal( jQuery("#name").attr("someAttr"), "1", "Set attribute to the number 1" );
365         // using contents will get comments regular, text, and comment nodes
366         var j = jQuery("#nonnodes").contents();
368         j.attr( "name", "attrvalue" );
369         equal( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
370         j.removeAttr("name");
372         // Type
373         var type = jQuery("#check2").attr("type");
374         var thrown = false;
375         try {
376                 jQuery("#check2").attr( "type", "hidden" );
377         } catch( e ) {
378                 thrown = true;
379         }
380         ok( thrown, "Exception thrown when trying to change type property" );
381         equal( type, jQuery("#check2").attr("type"), "Verify that you can't change the type of an input element" );
383         var check = document.createElement("input");
384         thrown = true;
385         try {
386                 jQuery( check ).attr( "type", "checkbox" );
387         } catch( e ) {
388                 thrown = false;
389         }
390         ok( thrown, "Exception thrown when trying to change type property" );
391         equal( "checkbox", jQuery( check ).attr("type"), "Verify that you can change the type of an input element that isn't in the DOM" );
393         check = jQuery("<input />");
394         thrown = true;
395         try {
396                 check.attr( "type", "checkbox" );
397         } catch( e ) {
398                 thrown = false;
399         }
400         ok( thrown, "Exception thrown when trying to change type property" );
401         equal( "checkbox", check.attr("type"), "Verify that you can change the type of an input element that isn't in the DOM" );
403         var button = jQuery("#button");
404         thrown = false;
405         try {
406                 button.attr( "type", "submit" );
407         } catch( e ) {
408                 thrown = true;
409         }
410         ok( thrown, "Exception thrown when trying to change type property" );
411         equal( "button", button.attr("type"), "Verify that you can't change the type of a button element" );
413         var $radio = jQuery( "<input>", {
414                 "value": "sup",
415                 "type": "radio"
416         }).appendTo("#testForm");
417         equal( $radio.val(), "sup", "Value is not reset when type is set after value on a radio" );
419         // Setting attributes on svg elements (bug #3116)
420         var $svg = jQuery(
421                 "<svg xmlns='http://www.w3.org/2000/svg'   xmlns:xlink='http://www.w3.org/1999/xlink' version='1.1'  baseProfile='full' width='200' height='200'>" +
423                         "<circle cx='200' cy='200' r='150' />" +
424                         "</svg>"
425                 ).appendTo("body");
426         equal( $svg.attr( "cx", 100 ).attr("cx"), "100", "Set attribute on svg element" );
427         $svg.remove();
429         // undefined values are chainable
430         jQuery("#name").attr( "maxlength", "5" ).removeAttr("nonexisting");
431         equal( typeof jQuery("#name").attr( "maxlength", undefined ), "object", ".attr('attribute', undefined) is chainable (#5571)" );
432         equal( jQuery("#name").attr( "maxlength", undefined ).attr("maxlength"), "5", ".attr('attribute', undefined) does not change value (#5571)" );
433         equal( jQuery("#name").attr( "nonexisting", undefined ).attr("nonexisting"), undefined, ".attr('attribute', undefined) does not create attribute (#5571)" );
436 test( "attr(jquery_method)", function() {
438         var $elem = jQuery("<div />"),
439                 elem = $elem[ 0 ],
440                 expected = 2,
441                 attrObj = {};
443         if ( jQuery.fn.width ) {
444                 expected += 2;
445                 attrObj["width"] = 10;
446         }
448         if ( jQuery.fn.offset ) {
449                 expected += 2;
450                 attrObj["offset"] = {
451                         "top": 1,
452                         "left": 0
453                 };
454         }
456         if ( jQuery.css ) {
457                 expected += 3;
458                 attrObj["css"] = {
459                         "paddingLeft": 1,
460                         "paddingRight": 1
461                 };
462         }
464         expect( expected );
466         // one at a time
467         $elem.attr({
468                 "html": "foo"
469         }, true );
470         equal( elem.innerHTML, "foo", "attr(html)" );
472         $elem.attr({
473                 "text": "bar"
474         }, true );
475         equal( elem.innerHTML, "bar", "attr(text)" );
477         // Multiple attributes
478         $elem.attr( attrObj, true );
480         if ( jQuery.fn.width ) {
481                 equal( elem.style.width, "10px", "attr({width:})" );
483                 $elem.attr( {
484                         "height": 10
485                 }, true );
486                 equal( elem.style.height, "10px", "attr(height)" );
487         }
489         if ( jQuery.fn.offset ) {
490                 equal( elem.style.top, "1px", "attr({offset:})" );
492                 $elem.attr({
493                         offset: {
494                                 top: 1,
495                                 left: 1
496                         }
497                 }, true );
498                 equal( elem.style.left, "1px", "attr(offset)" );
499         }
501         if ( jQuery.css ) {
502                 equal( elem.style.paddingLeft, "1px", "attr({css:})" );
503                 equal( elem.style.paddingRight, "1px", "attr({css:})" );
505                 $elem.attr({
506                         "css": {
507                                 "color": "red"
508                         }
509                 }, true );
510                 ok( /^(#ff0000|red)$/i.test( elem.style.color ), "attr(css)" );
511         }
514 test( "attr(String, Object) - Loaded via XML document", function() {
515         expect( 2 );
516         var xml = createDashboardXML();
517         var titles = [];
518         jQuery( "tab", xml ).each(function() {
519                 titles.push( jQuery( this ).attr("title") );
520         });
521         equal( titles[ 0 ], "Location", "attr() in XML context: Check first title" );
522         equal( titles[ 1 ], "Users", "attr() in XML context: Check second title" );
525 test( "attr('tabindex')", function() {
526         expect( 8 );
528         // elements not natively tabbable
529         equal( jQuery("#listWithTabIndex").attr("tabindex"), "5", "not natively tabbable, with tabindex set to 0" );
530         equal( jQuery("#divWithNoTabIndex").attr("tabindex"), undefined, "not natively tabbable, no tabindex set" );
532         // anchor with href
533         equal( jQuery("#linkWithNoTabIndex").attr("tabindex"), undefined, "anchor with href, no tabindex set" );
534         equal( jQuery("#linkWithTabIndex").attr("tabindex"), "2", "anchor with href, tabindex set to 2" );
535         equal( jQuery("#linkWithNegativeTabIndex").attr("tabindex"), "-1", "anchor with href, tabindex set to -1" );
537         // anchor without href
538         equal( jQuery("#linkWithNoHrefWithNoTabIndex").attr("tabindex"), undefined, "anchor without href, no tabindex set" );
539         equal( jQuery("#linkWithNoHrefWithTabIndex").attr("tabindex"), "1", "anchor without href, tabindex set to 2" );
540         equal( jQuery("#linkWithNoHrefWithNegativeTabIndex").attr("tabindex"), "-1", "anchor without href, no tabindex set" );
543 test( "attr('tabindex', value)", function() {
544         expect( 9 );
546         var element = jQuery("#divWithNoTabIndex");
547         equal( element.attr("tabindex"), undefined, "start with no tabindex" );
549         // set a positive string
550         element.attr( "tabindex", "1" );
551         equal( element.attr("tabindex"), "1", "set tabindex to 1 (string)" );
553         // set a zero string
554         element.attr( "tabindex", "0" );
555         equal( element.attr("tabindex"), "0", "set tabindex to 0 (string)" );
557         // set a negative string
558         element.attr( "tabindex", "-1" );
559         equal( element.attr("tabindex"), "-1", "set tabindex to -1 (string)" );
561         // set a positive number
562         element.attr( "tabindex", 1 );
563         equal( element.attr("tabindex"), "1", "set tabindex to 1 (number)" );
565         // set a zero number
566         element.attr( "tabindex", 0 );
567         equal(element.attr("tabindex"), "0", "set tabindex to 0 (number)");
569         // set a negative number
570         element.attr( "tabindex", -1 );
571         equal( element.attr("tabindex"), "-1", "set tabindex to -1 (number)" );
573         element = jQuery("#linkWithTabIndex");
574         equal( element.attr("tabindex"), "2", "start with tabindex 2" );
576         element.attr( "tabindex", -1 );
577         equal( element.attr("tabindex"), "-1", "set negative tabindex" );
580 test( "removeAttr(String)", function() {
581         expect( 12 );
582         var $first;
584         equal( jQuery("#mark").removeAttr("class").attr("class"), undefined, "remove class" );
585         equal( jQuery("#form").removeAttr("id").attr("id"), undefined, "Remove id" );
586         equal( jQuery("#foo").attr( "style", "position:absolute;" ).removeAttr("style").attr("style"), undefined, "Check removing style attribute" );
587         equal( jQuery("#form").attr( "style", "position:absolute;" ).removeAttr("style").attr("style"), undefined, "Check removing style attribute on a form" );
588         equal( jQuery("<div style='position: absolute'></div>").appendTo("#foo").removeAttr("style").prop("style").cssText, "", "Check removing style attribute (#9699 Webkit)" );
589         equal( jQuery("#fx-test-group").attr( "height", "3px" ).removeAttr("height").get( 0 ).style.height, "1px", "Removing height attribute has no effect on height set with style attribute" );
591         jQuery("#check1").removeAttr("checked").prop( "checked", true ).removeAttr("checked");
592         equal( document.getElementById("check1").checked, false, "removeAttr sets boolean properties to false" );
593         jQuery("#text1").prop( "readOnly", true ).removeAttr("readonly");
594         equal( document.getElementById("text1").readOnly, false, "removeAttr sets boolean properties to false" );
596         jQuery("#option2c").removeAttr("selected");
597         equal( jQuery("#option2d").attr("selected"), "selected", "Removing `selected` from an option that is not selected does not remove selected from the currently selected option (#10870)" );
599         try {
600                 $first = jQuery("#first").attr( "contenteditable", "true" ).removeAttr("contenteditable");
601                 equal( $first.attr("contenteditable"), undefined, "Remove the contenteditable attribute" );
602         } catch( e ) {
603                 ok( false, "Removing contenteditable threw an error (#10429)" );
604         }
606         $first = jQuery("<div Case='mixed'></div>");
607         equal( $first.attr("Case"), "mixed", "case of attribute doesn't matter" );
608         $first.removeAttr("Case");
609         // IE 6/7 return empty string here, not undefined
610         ok( !$first.attr("Case"), "mixed-case attribute was removed" );
613 test( "removeAttr(String) in XML", function() {
614         expect( 7 );
615         var xml = createDashboardXML(),
616                 iwt = jQuery( "infowindowtab", xml );
618         equal( iwt.attr("normal"), "ab", "Check initial value" );
619         iwt.removeAttr("Normal");
620         equal( iwt.attr("normal"), "ab", "Should still be there" );
621         iwt.removeAttr("normal");
622         equal( iwt.attr("normal"), undefined, "Removed" );
624         equal( iwt.attr("mixedCase"), "yes", "Check initial value" );
625         equal( iwt.attr("mixedcase"), undefined, "toLowerCase not work good" );
626         iwt.removeAttr("mixedcase");
627         equal( iwt.attr("mixedCase"), "yes", "Should still be there" );
628         iwt.removeAttr("mixedCase");
629         equal( iwt.attr("mixedCase"), undefined, "Removed" );
632 test( "removeAttr(Multi String, variable space width)", function() {
633         expect( 8 );
635         var div = jQuery("<div id='a' alt='b' title='c' rel='d'></div>"),
636                 tests = {
637                         id: "a",
638                         alt: "b",
639                         title: "c",
640                         rel: "d"
641                 };
643         jQuery.each( tests, function( key, val ) {
644                 equal( div.attr( key ), val, "Attribute `" + key + "` exists, and has a value of `" + val + "`" );
645         });
647         div.removeAttr( "id   alt title  rel  " );
649         jQuery.each( tests, function( key, val ) {
650                 equal( div.attr( key ), undefined, "Attribute `" + key + "` was removed" );
651         });
654 test( "prop(String, Object)", function() {
655         expect( 31 );
657         equal( jQuery("#text1").prop("value"), "Test", "Check for value attribute" );
658         equal( jQuery("#text1").prop( "value", "Test2" ).prop("defaultValue"), "Test", "Check for defaultValue attribute" );
659         equal( jQuery("#select2").prop("selectedIndex"), 3, "Check for selectedIndex attribute" );
660         equal( jQuery("#foo").prop("nodeName").toUpperCase(), "DIV", "Check for nodeName attribute" );
661         equal( jQuery("#foo").prop("tagName").toUpperCase(), "DIV", "Check for tagName attribute" );
662         equal( jQuery("<option/>").prop("selected"), false, "Check selected attribute on disconnected element." );
664         equal( jQuery("#listWithTabIndex").prop("tabindex"), 5, "Check retrieving tabindex" );
665         jQuery("#text1").prop( "readonly", true );
666         equal( document.getElementById("text1").readOnly, true, "Check setting readOnly property with 'readonly'" );
667         equal( jQuery("#label-for").prop("for"), "action", "Check retrieving htmlFor" );
668         jQuery("#text1").prop("class", "test");
669         equal( document.getElementById("text1").className, "test", "Check setting className with 'class'" );
670         equal( jQuery("#text1").prop("maxlength"), 30, "Check retrieving maxLength" );
671         jQuery("#table").prop( "cellspacing", 1 );
672         equal( jQuery("#table").prop("cellSpacing"), "1", "Check setting and retrieving cellSpacing" );
673         jQuery("#table").prop( "cellpadding", 1 );
674         equal( jQuery("#table").prop("cellPadding"), "1", "Check setting and retrieving cellPadding" );
675         jQuery("#table").prop( "rowspan", 1 );
676         equal( jQuery("#table").prop("rowSpan"), 1, "Check setting and retrieving rowSpan" );
677         jQuery("#table").prop( "colspan", 1 );
678         equal( jQuery("#table").prop("colSpan"), 1, "Check setting and retrieving colSpan" );
679         jQuery("#table").prop( "usemap", 1 );
680         equal( jQuery("#table").prop("useMap"), 1, "Check setting and retrieving useMap" );
681         jQuery("#table").prop( "frameborder", 1 );
682         equal( jQuery("#table").prop("frameBorder"), 1, "Check setting and retrieving frameBorder" );
683         QUnit.reset();
685         var body = document.body,
686                 $body = jQuery( body );
688         ok( $body.prop("nextSibling") === null, "Make sure a null expando returns null" );
689         body["foo"] = "bar";
690         equal( $body.prop("foo"), "bar", "Make sure the expando is preferred over the dom attribute" );
691         body["foo"] = undefined;
692         ok( $body.prop("foo") === undefined, "Make sure the expando is preferred over the dom attribute, even if undefined" );
694         var select = document.createElement("select"),
695                 optgroup = document.createElement("optgroup"),
696                 option = document.createElement("option");
698         optgroup.appendChild( option );
699         select.appendChild( optgroup );
701         equal( jQuery( option ).prop("selected"), true, "Make sure that a single option is selected, even when in an optgroup." );
702         equal( jQuery( document ).prop("nodeName"), "#document", "prop works correctly on document nodes (bug #7451)." );
704         var attributeNode = document.createAttribute("irrelevant"),
705                 commentNode = document.createComment("some comment"),
706                 textNode = document.createTextNode("some text"),
707                 obj = {};
708         jQuery.each( [ document, attributeNode, commentNode, textNode, obj, "#firstp" ], function( i, ele ) {
709                 strictEqual( jQuery( ele ).prop("nonexisting"), undefined, "prop works correctly for non existing attributes (bug #7500)." );
710         });
712         obj = {};
713         jQuery.each( [ document, obj ], function( i, ele ) {
714                 var $ele = jQuery( ele );
715                 $ele.prop( "nonexisting", "foo" );
716                 equal( $ele.prop("nonexisting"), "foo", "prop(name, value) works correctly for non existing attributes (bug #7500)." );
717         });
718         jQuery( document ).removeProp("nonexisting");
720         var $form = jQuery("#form").prop( "enctype", "multipart/form-data" );
721         equal( $form.prop("enctype"), "multipart/form-data", "Set the enctype of a form (encoding in IE6/7 #6743)" );
724 test( "prop('tabindex')", function() {
725         expect( 8 );
727         // elements not natively tabbable
728         equal( jQuery("#listWithTabIndex").prop("tabindex"), 5, "not natively tabbable, with tabindex set to 0" );
729         equal( jQuery("#divWithNoTabIndex").prop("tabindex"), undefined, "not natively tabbable, no tabindex set" );
731         // anchor with href
732         equal( jQuery("#linkWithNoTabIndex").prop("tabindex"), 0, "anchor with href, no tabindex set" );
733         equal( jQuery("#linkWithTabIndex").prop("tabindex"), 2, "anchor with href, tabindex set to 2" );
734         equal( jQuery("#linkWithNegativeTabIndex").prop("tabindex"), -1, "anchor with href, tabindex set to -1" );
736         // anchor without href
737         equal( jQuery("#linkWithNoHrefWithNoTabIndex").prop("tabindex"), undefined, "anchor without href, no tabindex set" );
738         equal( jQuery("#linkWithNoHrefWithTabIndex").prop("tabindex"), 1, "anchor without href, tabindex set to 2" );
739         equal( jQuery("#linkWithNoHrefWithNegativeTabIndex").prop("tabindex"), -1, "anchor without href, no tabindex set" );
742 test( "prop('tabindex', value)", 10, function() {
744         var clone,
745                 element = jQuery("#divWithNoTabIndex");
747         equal( element.prop("tabindex"), undefined, "start with no tabindex" );
749         // set a positive string
750         element.prop( "tabindex", "1" );
751         equal( element.prop("tabindex"), 1, "set tabindex to 1 (string)" );
753         // set a zero string
754         element.prop( "tabindex", "0" );
755         equal( element.prop("tabindex"), 0, "set tabindex to 0 (string)" );
757         // set a negative string
758         element.prop( "tabindex", "-1" );
759         equal( element.prop("tabindex"), -1, "set tabindex to -1 (string)" );
761         // set a positive number
762         element.prop( "tabindex", 1 );
763         equal( element.prop("tabindex"), 1, "set tabindex to 1 (number)" );
765         // set a zero number
766         element.prop( "tabindex", 0 );
767         equal( element.prop("tabindex"), 0, "set tabindex to 0 (number)" );
769         // set a negative number
770         element.prop( "tabindex", -1 );
771         equal( element.prop("tabindex"), -1, "set tabindex to -1 (number)" );
773         element = jQuery("#linkWithTabIndex");
774         equal( element.prop("tabindex"), 2, "start with tabindex 2" );
776         element.prop( "tabindex", -1 );
777         equal( element.prop("tabindex"), -1, "set negative tabindex" );
779         clone = element.clone();
780         clone.prop( "tabindex", 1 );
781         equal( clone[ 0 ].getAttribute("tabindex"), "1", "set tabindex on cloned element" );
784 test( "removeProp(String)", function() {
785         expect( 6 );
786         var attributeNode = document.createAttribute("irrelevant"),
787                 commentNode = document.createComment("some comment"),
788                 textNode = document.createTextNode("some text"),
789                 obj = {};
791         strictEqual(
792                 jQuery( "#firstp" ).prop( "nonexisting", "foo" ).removeProp( "nonexisting" )[ 0 ]["nonexisting"],
793                 undefined,
794                 "removeprop works correctly on DOM element nodes"
795         );
797         jQuery.each( [ document, obj ], function( i, ele ) {
798                 var $ele = jQuery( ele );
799                 $ele.prop( "nonexisting", "foo" ).removeProp("nonexisting");
800                 strictEqual( ele["nonexisting"], undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." );
801         });
802         jQuery.each( [ commentNode, textNode, attributeNode ], function( i, ele ) {
803                 var $ele = jQuery( ele );
804                 $ele.prop( "nonexisting", "foo" ).removeProp("nonexisting");
805                 strictEqual( ele["nonexisting"], undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." );
806         });
809 test( "val()", function() {
810         expect( 21 + ( jQuery.fn.serialize ? 6 : 0 ) );
812         document.getElementById("text1").value = "bla";
813         equal( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
815         QUnit.reset();
817         equal( jQuery("#text1").val(), "Test", "Check for value of input element" );
818         // ticket #1714 this caused a JS error in IE
819         equal( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
820         ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
822         equal( jQuery("#select2").val(), "3", "Call val() on a single='single' select" );
824         deepEqual( jQuery("#select3").val(), [ "1", "2" ], "Call val() on a multiple='multiple' select" );
826         equal( jQuery("#option3c").val(), "2", "Call val() on a option element with value" );
828         equal( jQuery("#option3a").val(), "", "Call val() on a option element with empty value" );
830         equal( jQuery("#option3e").val(), "no value", "Call val() on a option element with no value attribute" );
832         equal( jQuery("#option3a").val(), "", "Call val() on a option element with no value attribute" );
834         jQuery("#select3").val("");
835         deepEqual( jQuery("#select3").val(), [""], "Call val() on a multiple='multiple' select" );
837         deepEqual( jQuery("#select4").val(), [], "Call val() on multiple='multiple' select with all disabled options" );
839         jQuery("#select4 optgroup").add("#select4 > [disabled]").attr( "disabled", false );
840         deepEqual( jQuery("#select4").val(), [ "2", "3" ], "Call val() on multiple='multiple' select with some disabled options" );
842         jQuery("#select4").attr( "disabled", true );
843         deepEqual( jQuery("#select4").val(), [ "2", "3" ], "Call val() on disabled multiple='multiple' select" );
845         equal( jQuery("#select5").val(), "3", "Check value on ambiguous select." );
847         jQuery("#select5").val( 1 );
848         equal( jQuery("#select5").val(), "1", "Check value on ambiguous select." );
850         jQuery("#select5").val( 3 );
851         equal( jQuery("#select5").val(), "3", "Check value on ambiguous select." );
853         strictEqual(
854                 jQuery("<select name='select12584' id='select12584'><option value='1' disabled='disabled'>1</option></select>").val(),
855                 null,
856                 "Select-one with only option disabled (#12584)"
857         );
859         if ( jQuery.fn.serialize ) {
860                 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");
862                 deepEqual( checks.serialize(), "", "Get unchecked values." );
864                 equal( checks.eq( 3 ).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
866                 checks.val([ "2" ]);
867                 deepEqual( checks.serialize(), "test=2", "Get a single checked value." );
869                 checks.val([ "1", "" ]);
870                 deepEqual( checks.serialize(), "test=1&test=", "Get multiple checked values." );
872                 checks.val([ "", "2" ]);
873                 deepEqual( checks.serialize(), "test=2&test=", "Get multiple checked values." );
875                 checks.val([ "1", "on" ]);
876                 deepEqual( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
878                 checks.remove();
879         }
881         var $button = jQuery("<button value='foobar'>text</button>").insertAfter("#button");
882         equal( $button.val(), "foobar", "Value retrieval on a button does not return innerHTML" );
883         equal( $button.val("baz").html(), "text", "Setting the value does not change innerHTML" );
885         equal( jQuery("<option/>").val("test").attr("value"), "test", "Setting value sets the value attribute" );
888 if ( "value" in document.createElement("meter") &&
889                         "value" in document.createElement("progress") ) {
891         test( "val() respects numbers without exception (Bug #9319)", function() {
893                 expect( 4 );
895                 var $meter = jQuery("<meter min='0' max='10' value='5.6'></meter>"),
896                         $progress = jQuery("<progress max='10' value='1.5'></progress>");
898                 try {
899                         equal( typeof $meter.val(), "number", "meter, returns a number and does not throw exception" );
900                         equal( $meter.val(), $meter[ 0 ].value, "meter, api matches host and does not throw exception" );
902                         equal( typeof $progress.val(), "number", "progress, returns a number and does not throw exception" );
903                         equal( $progress.val(), $progress[ 0 ].value, "progress, api matches host and does not throw exception" );
905                 } catch( e ) {}
907                 $meter.remove();
908                 $progress.remove();
909         });
912 var testVal = function( valueObj ) {
913         expect( 8 );
915         QUnit.reset();
916         jQuery("#text1").val( valueObj("test") );
917         equal( document.getElementById("text1").value, "test", "Check for modified (via val(String)) value of input element" );
919         jQuery("#text1").val( valueObj( undefined ) );
920         equal( document.getElementById("text1").value, "", "Check for modified (via val(undefined)) value of input element" );
922         jQuery("#text1").val( valueObj( 67 ) );
923         equal( document.getElementById("text1").value, "67", "Check for modified (via val(Number)) value of input element" );
925         jQuery("#text1").val( valueObj( null ) );
926         equal( document.getElementById("text1").value, "", "Check for modified (via val(null)) value of input element" );
928         var $select1 = jQuery("#select1");
929         $select1.val( valueObj("3") );
930         equal( $select1.val(), "3", "Check for modified (via val(String)) value of select element" );
932         $select1.val( valueObj( 2 ) );
933         equal( $select1.val(), "2", "Check for modified (via val(Number)) value of select element" );
935         $select1.append("<option value='4'>four</option>");
936         $select1.val( valueObj( 4 ) );
937         equal( $select1.val(), "4", "Should be possible to set the val() to a newly created option" );
939         // using contents will get comments regular, text, and comment nodes
940         var j = jQuery("#nonnodes").contents();
941         j.val( valueObj( "asdf" ) );
942         equal( j.val(), "asdf", "Check node,textnode,comment with val()" );
943         j.removeAttr("value");
946 test( "val(String/Number)", function() {
947         testVal( bareObj );
950 test( "val(Function)", function() {
951         testVal( functionReturningObj );
954 test( "val(Array of Numbers) (Bug #7123)", function() {
955         expect( 4 );
956         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' />");
957         var elements = jQuery("input[name=arrayTest]").val([ 1, 2 ]);
958         ok( elements[ 0 ].checked, "First element was checked" );
959         ok( elements[ 1 ].checked, "Second element was checked" );
960         ok( !elements[ 2 ].checked, "Third element was unchecked" );
961         ok( !elements[ 3 ].checked, "Fourth element remained unchecked" );
963         elements.remove();
966 test( "val(Function) with incoming value", function() {
967         expect( 10 );
969         QUnit.reset();
970         var oldVal = jQuery("#text1").val();
972         jQuery("#text1").val(function( i, val ) {
973                 equal( val, oldVal, "Make sure the incoming value is correct." );
974                 return "test";
975         });
977         equal( document.getElementById("text1").value, "test", "Check for modified (via val(String)) value of input element" );
979         oldVal = jQuery("#text1").val();
981         jQuery("#text1").val(function( i, val ) {
982                 equal( val, oldVal, "Make sure the incoming value is correct." );
983                 return 67;
984         });
986         equal( document.getElementById("text1").value, "67", "Check for modified (via val(Number)) value of input element" );
988         oldVal = jQuery("#select1").val();
990         jQuery("#select1").val(function( i, val ) {
991                 equal( val, oldVal, "Make sure the incoming value is correct." );
992                 return "3";
993         });
995         equal( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
997         oldVal = jQuery("#select1").val();
999         jQuery("#select1").val(function( i, val ) {
1000                 equal( val, oldVal, "Make sure the incoming value is correct." );
1001                 return 2;
1002         });
1004         equal( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
1006         jQuery("#select1").append("<option value='4'>four</option>");
1008         oldVal = jQuery("#select1").val();
1010         jQuery("#select1").val(function( i, val ) {
1011                 equal( val, oldVal, "Make sure the incoming value is correct." );
1012                 return 4;
1013         });
1015         equal( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
1018 // testing if a form.reset() breaks a subsequent call to a select element's .val() (in IE only)
1019 test( "val(select) after form.reset() (Bug #2551)", function() {
1020         expect( 3 );
1022         jQuery("<form id='kk' name='kk'><select id='kkk'><option value='cf'>cf</option><option value='gf'>gf</option></select></form>").appendTo("#qunit-fixture");
1024         jQuery("#kkk").val("gf");
1026         document["kk"].reset();
1028         equal( jQuery("#kkk")[ 0 ].value, "cf", "Check value of select after form reset." );
1029         equal( jQuery("#kkk").val(), "cf", "Check value of select after form reset." );
1031         // re-verify the multi-select is not broken (after form.reset) by our fix for single-select
1032         deepEqual( jQuery("#select3").val(), ["1", "2"], "Call val() on a multiple='multiple' select" );
1034         jQuery("#kk").remove();
1037 var testAddClass = function( valueObj ) {
1038         expect( 9 );
1040         var div = jQuery("div");
1041         div.addClass( valueObj("test") );
1042         var pass = true;
1043         for ( var i = 0; i < div.size(); i++ ) {
1044                 if ( !~div.get( i ).className.indexOf("test") ) {
1045                         pass = false;
1046                 }
1047         }
1048         ok( pass, "Add Class" );
1050         // using contents will get regular, text, and comment nodes
1051         var j = jQuery("#nonnodes").contents();
1052         j.addClass( valueObj("asdf") );
1053         ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
1055         div = jQuery("<div/>");
1057         div.addClass( valueObj("test") );
1058         equal( div.attr("class"), "test", "Make sure there's no extra whitespace." );
1060         div.attr( "class", " foo" );
1061         div.addClass( valueObj("test") );
1062         equal( div.attr("class"), "foo test", "Make sure there's no extra whitespace." );
1064         div.attr( "class", "foo" );
1065         div.addClass( valueObj("bar baz") );
1066         equal( div.attr("class"), "foo bar baz", "Make sure there isn't too much trimming." );
1068         div.removeClass();
1069         div.addClass( valueObj("foo") ).addClass( valueObj("foo") );
1070         equal( div.attr("class"), "foo", "Do not add the same class twice in separate calls." );
1072         div.addClass( valueObj("fo") );
1073         equal( div.attr("class"), "foo fo", "Adding a similar class does not get interrupted." );
1074         div.removeClass().addClass("wrap2");
1075         ok( div.addClass("wrap").hasClass("wrap"), "Can add similarly named classes");
1077         div.removeClass();
1078         div.addClass( valueObj("bar bar") );
1079         equal( div.attr("class"), "bar", "Do not add the same class twice in the same call." );
1082 test( "addClass(String)", function() {
1083         testAddClass( bareObj );
1086 test( "addClass(Function)", function() {
1087         testAddClass( functionReturningObj );
1090 test( "addClass(Function) with incoming value", function() {
1091         expect( 48 );
1092         var div = jQuery("div"),
1093                 old = div.map(function() {
1094                         return jQuery(this).attr("class") || "";
1095                 });
1097         div.addClass(function( i, val ) {
1098                 if ( this.id !== "_firebugConsole" ) {
1099                         equal( val, old[ i ], "Make sure the incoming value is correct." );
1100                         return "test";
1101                 }
1102         });
1104         var pass = true;
1105         for ( var i = 0; i < div.length; i++ ) {
1106                 if ( div.get(i).className.indexOf("test") == -1 ) {
1107                         pass = false;
1108                 }
1109         }
1110         ok( pass, "Add Class" );
1113 var testRemoveClass = function(valueObj) {
1114         expect( 7 );
1116         var $divs = jQuery("div");
1118         $divs.addClass("test").removeClass( valueObj("test") );
1120         ok( !$divs.is(".test"), "Remove Class" );
1122         QUnit.reset();
1123         $divs = jQuery("div");
1125         $divs.addClass("test").addClass("foo").addClass("bar");
1126         $divs.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
1128         ok( !$divs.is(".test,.bar,.foo"), "Remove multiple classes" );
1130         QUnit.reset();
1131         $divs = jQuery("div");
1133         // Make sure that a null value doesn't cause problems
1134         $divs.eq( 0 ).addClass("test").removeClass( valueObj( null ) );
1135         ok( $divs.eq( 0 ).is(".test"), "Null value passed to removeClass" );
1137         $divs.eq( 0 ).addClass("test").removeClass( valueObj("") );
1138         ok( $divs.eq( 0 ).is(".test"), "Empty string passed to removeClass" );
1140         // using contents will get regular, text, and comment nodes
1141         var j = jQuery("#nonnodes").contents();
1142         j.removeClass( valueObj("asdf") );
1143         ok( !j.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
1145         var div = document.createElement("div");
1146         div.className = " test foo ";
1148         jQuery( div ).removeClass( valueObj("foo") );
1149         equal( div.className, "test", "Make sure remaining className is trimmed." );
1151         div.className = " test ";
1153         jQuery( div ).removeClass( valueObj("test") );
1154         equal( div.className, "", "Make sure there is nothing left after everything is removed." );
1157 test( "removeClass(String) - simple", function() {
1158         testRemoveClass( bareObj );
1161 test( "removeClass(Function) - simple", function() {
1162         testRemoveClass( functionReturningObj );
1165 test( "removeClass(Function) with incoming value", function() {
1166         expect( 48 );
1168         var $divs = jQuery("div").addClass("test"), old = $divs.map(function() {
1169                 return jQuery( this ).attr("class");
1170         });
1172         $divs.removeClass(function( i, val ) {
1173                 if ( this.id !== "_firebugConsole" ) {
1174                         equal( val, old[ i ], "Make sure the incoming value is correct." );
1175                         return "test";
1176                 }
1177         });
1179         ok( !$divs.is(".test"), "Remove Class" );
1181         QUnit.reset();
1184 test( "removeClass() removes duplicates", function() {
1185         expect( 1 );
1187         var $div = jQuery( jQuery.parseHTML("<div class='x x x'></div>") );
1189         $div.removeClass("x");
1191         ok( !$div.hasClass("x"), "Element with multiple same classes does not escape the wrath of removeClass()" );
1194 var testToggleClass = function(valueObj) {
1195         expect( 17 );
1197         var e = jQuery("#firstp");
1198         ok( !e.is(".test"), "Assert class not present" );
1199         e.toggleClass( valueObj("test") );
1200         ok( e.is(".test"), "Assert class present" );
1201         e.toggleClass( valueObj("test") );
1202         ok( !e.is(".test"), "Assert class not present" );
1204         // class name with a boolean
1205         e.toggleClass( valueObj("test"), false );
1206         ok( !e.is(".test"), "Assert class not present" );
1207         e.toggleClass( valueObj("test"), true );
1208         ok( e.is(".test"), "Assert class present" );
1209         e.toggleClass( valueObj("test"), false );
1210         ok( !e.is(".test"), "Assert class not present" );
1212         // multiple class names
1213         e.addClass("testA testB");
1214         ok( (e.is(".testA.testB")), "Assert 2 different classes present" );
1215         e.toggleClass( valueObj("testB testC") );
1216         ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
1217         e.toggleClass( valueObj("testA testC") );
1218         ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
1220         // toggleClass storage
1221         e.toggleClass( true );
1222         ok( e[ 0 ].className === "", "Assert class is empty (data was empty)" );
1223         e.addClass("testD testE");
1224         ok( e.is(".testD.testE"), "Assert class present" );
1225         e.toggleClass();
1226         ok( !e.is(".testD.testE"), "Assert class not present" );
1227         ok( jQuery._data(e[ 0 ], "__className__") === "testD testE", "Assert data was stored" );
1228         e.toggleClass();
1229         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
1230         e.toggleClass( false );
1231         ok( !e.is(".testD.testE"), "Assert class not present" );
1232         e.toggleClass( true );
1233         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
1234         e.toggleClass();
1235         e.toggleClass( false );
1236         e.toggleClass();
1237         ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
1239         // Cleanup
1240         e.removeClass("testD");
1241         jQuery._removeData( e[ 0 ], "__className__" );
1244 test( "toggleClass(String|boolean|undefined[, boolean])", function() {
1245         testToggleClass( bareObj );
1248 test( "toggleClass(Function[, boolean])", function() {
1249         testToggleClass( functionReturningObj );
1252 test( "toggleClass(Fucntion[, boolean]) with incoming value", function() {
1253         expect( 14 );
1255         var e = jQuery("#firstp"), old = e.attr("class") || "";
1256         ok( !e.is(".test"), "Assert class not present" );
1258         e.toggleClass(function( i, val ) {
1259                 equal( old, val, "Make sure the incoming value is correct." );
1260                 return "test";
1261         });
1262         ok( e.is(".test"), "Assert class present" );
1264         old = e.attr("class");
1266         e.toggleClass(function( i, val ) {
1267                 equal( old, val, "Make sure the incoming value is correct." );
1268                 return "test";
1269         });
1270         ok( !e.is(".test"), "Assert class not present" );
1272         old = e.attr("class") || "";
1274         // class name with a boolean
1275         e.toggleClass(function( i, val, state ) {
1276                 equal( old, val, "Make sure the incoming value is correct." );
1277                 equal( state, false, "Make sure that the state is passed in." );
1278                 return "test";
1279         }, false );
1280         ok( !e.is(".test"), "Assert class not present" );
1282         old = e.attr("class") || "";
1284         e.toggleClass(function( i, val, state ) {
1285                 equal( old, val, "Make sure the incoming value is correct." );
1286                 equal( state, true, "Make sure that the state is passed in." );
1287                 return "test";
1288         }, true );
1289         ok( e.is(".test"), "Assert class present" );
1291         old = e.attr("class");
1293         e.toggleClass(function( i, val, state ) {
1294                 equal( old, val, "Make sure the incoming value is correct." );
1295                 equal( state, false, "Make sure that the state is passed in." );
1296                 return "test";
1297         }, false );
1298         ok( !e.is(".test"), "Assert class not present" );
1300         // Cleanup
1301         e.removeClass("test");
1302         jQuery._removeData( e[ 0 ], "__className__" );
1305 test( "addClass, removeClass, hasClass", function() {
1306         expect( 17 );
1308         var jq = jQuery("<p>Hi</p>"), x = jq[ 0 ];
1310         jq.addClass("hi");
1311         equal( x.className, "hi", "Check single added class" );
1313         jq.addClass("foo bar");
1314         equal( x.className, "hi foo bar", "Check more added classes" );
1316         jq.removeClass();
1317         equal( x.className, "", "Remove all classes" );
1319         jq.addClass("hi foo bar");
1320         jq.removeClass("foo");
1321         equal( x.className, "hi bar", "Check removal of one class" );
1323         ok( jq.hasClass("hi"), "Check has1" );
1324         ok( jq.hasClass("bar"), "Check has2" );
1326         jq = jQuery("<p class='class1\nclass2\tcla.ss3\n\rclass4'></p>");
1328         ok( jq.hasClass("class1"), "Check hasClass with line feed" );
1329         ok( jq.is(".class1"), "Check is with line feed" );
1330         ok( jq.hasClass("class2"), "Check hasClass with tab" );
1331         ok( jq.is(".class2"), "Check is with tab" );
1332         ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" );
1333         ok( jq.hasClass("class4"), "Check hasClass with carriage return" );
1334         ok( jq.is(".class4"), "Check is with carriage return" );
1336         jq.removeClass("class2");
1337         ok( jq.hasClass("class2") === false, "Check the class has been properly removed" );
1338         jq.removeClass("cla");
1339         ok( jq.hasClass("cla.ss3"), "Check the dotted class has not been removed" );
1340         jq.removeClass("cla.ss3");
1341         ok( jq.hasClass("cla.ss3") === false, "Check the dotted class has been removed" );
1342         jq.removeClass("class4");
1343         ok( jq.hasClass("class4") === false, "Check the class has been properly removed" );
1346 test( "contents().hasClass() returns correct values", function() {
1347         expect( 2 );
1349         var $div = jQuery("<div><span class='foo'></span><!-- comment -->text</div>"),
1350         $contents = $div.contents();
1352         ok( $contents.hasClass("foo"), "Found 'foo' in $contents" );
1353         ok( !$contents.hasClass("undefined"), "Did not find 'undefined' in $contents (correctly)" );
1356 test( "coords returns correct values in IE6/IE7, see #10828", function() {
1357         expect( 2 );
1359         var area,
1360                 map = jQuery("<map />");
1362         area = map.html("<area shape='rect' coords='0,0,0,0' href='#' alt='a' />").find("area");
1363         equal( area.attr("coords"), "0,0,0,0", "did not retrieve coords correctly" );
1365         area = map.html("<area shape='rect' href='#' alt='a' /></map>").find("area");
1366         equal( area.attr("coords"), undefined, "did not retrieve coords correctly" );