1 QUnit.module( "attributes", {
2 afterEach: moduleTeardown
5 function bareObj( value ) {
9 function functionReturningObj( value ) {
15 function arrayFromString( value ) {
16 return value ? value.split( " " ) : [];
20 ======== local reference =======
21 bareObj and functionReturningObj can be used to test passing functions to setters
22 See testVal below for an example
25 This function returns whatever value is passed in
27 functionReturningObj( value );
28 Returns a function that returns the value
31 QUnit.test( "jQuery.propFix integrity test", function( assert ) {
34 // This must be maintained and equal jQuery.attrFix when appropriate
35 // Ensure that accidental or erroneous property
36 // overwrites don't occur
37 // This is simply for better code coverage and future proofing.
39 "tabindex": "tabIndex",
40 "readonly": "readOnly",
43 "maxlength": "maxLength",
44 "cellspacing": "cellSpacing",
45 "cellpadding": "cellPadding",
49 "frameborder": "frameBorder",
50 "contenteditable": "contentEditable"
53 assert.deepEqual( props, jQuery.propFix, "jQuery.propFix passes integrity check" );
56 QUnit.test( "attr(String)", function( assert ) {
59 var extras, body, $body,
60 select, optgroup, option, $img, styleElem,
63 assert.equal( jQuery( "#text1" ).attr( "type" ), "text", "Check for type attribute" );
64 assert.equal( jQuery( "#radio1" ).attr( "type" ), "radio", "Check for type attribute" );
65 assert.equal( jQuery( "#check1" ).attr( "type" ), "checkbox", "Check for type attribute" );
66 assert.equal( jQuery( "#simon1" ).attr( "rel" ), "bookmark", "Check for rel attribute" );
67 assert.equal( jQuery( "#google" ).attr( "title" ), "Google!", "Check for title attribute" );
68 assert.equal( jQuery( "#mark" ).attr( "hreflang" ), "en", "Check for hreflang attribute" );
69 assert.equal( jQuery( "#en" ).attr( "lang" ), "en", "Check for lang attribute" );
70 assert.equal( jQuery( "#simon" ).attr( "class" ), "blog link", "Check for class attribute" );
71 assert.equal( jQuery( "#name" ).attr( "name" ), "name", "Check for name attribute" );
72 assert.equal( jQuery( "#text1" ).attr( "name" ), "action", "Check for name attribute" );
73 assert.ok( jQuery( "#form" ).attr( "action" ).indexOf( "formaction" ) >= 0, "Check for action attribute" );
74 assert.equal( jQuery( "#text1" ).attr( "value", "t" ).attr( "value" ), "t", "Check setting the value attribute" );
75 assert.equal( jQuery( "#text1" ).attr( "value", "" ).attr( "value" ), "", "Check setting the value attribute to empty string" );
76 assert.equal( jQuery( "<div value='t'></div>" ).attr( "value" ), "t", "Check setting custom attr named 'value' on a div" );
77 assert.equal( jQuery( "#form" ).attr( "blah", "blah" ).attr( "blah" ), "blah", "Set non-existent attribute on a form" );
78 assert.equal( jQuery( "#foo" ).attr( "height" ), undefined, "Non existent height attribute should return undefined" );
80 // [7472] & [3113] (form contains an input with name="action" or name="id")
81 extras = jQuery( "<input id='id' name='id' /><input id='name' name='name' /><input id='target' name='target' />" ).appendTo( "#testForm" );
82 assert.equal( jQuery( "#form" ).attr( "action", "newformaction" ).attr( "action" ), "newformaction", "Check that action attribute was changed" );
83 assert.equal( jQuery( "#testForm" ).attr( "target" ), undefined, "Retrieving target does not equal the input with name=target" );
84 assert.equal( jQuery( "#testForm" ).attr( "target", "newTarget" ).attr( "target" ), "newTarget", "Set target successfully on a form" );
85 assert.equal( jQuery( "#testForm" ).removeAttr( "id" ).attr( "id" ), undefined, "Retrieving id does not equal the input with name=id after id is removed [trac-7472]" );
87 // Bug trac-3685 (form contains input with name="name")
88 assert.equal( jQuery( "#testForm" ).attr( "name" ), undefined, "Retrieving name does not retrieve input with name=name" );
91 assert.equal( jQuery( "#text1" ).attr( "maxlength" ), "30", "Check for maxlength attribute" );
92 assert.equal( jQuery( "#text1" ).attr( "maxLength" ), "30", "Check for maxLength attribute" );
93 assert.equal( jQuery( "#area1" ).attr( "maxLength" ), "30", "Check for maxLength attribute" );
95 // using innerHTML in IE causes href attribute to be serialized to the full path
96 jQuery( "<a></a>" ).attr( {
99 } ).appendTo( "#qunit-fixture" );
100 assert.equal( jQuery( "#tAnchor5" ).attr( "href" ), "#5", "Check for non-absolute href (an anchor)" );
101 jQuery( "<a id='tAnchor6' href='#5'></a>" ).appendTo( "#qunit-fixture" );
102 assert.equal( jQuery( "#tAnchor5" ).prop( "href" ), jQuery( "#tAnchor6" ).prop( "href" ), "Check for absolute href prop on an anchor" );
104 jQuery( "<script type='jquery/test' src='#5' id='scriptSrc'></script>" ).appendTo( "#qunit-fixture" );
105 assert.equal( jQuery( "#tAnchor5" ).prop( "href" ), jQuery( "#scriptSrc" ).prop( "src" ), "Check for absolute src prop on a script" );
107 // list attribute is readonly by default in browsers that support it
108 jQuery( "#list-test" ).attr( "list", "datalist" );
109 assert.equal( jQuery( "#list-test" ).attr( "list" ), "datalist", "Check setting list attribute" );
111 // Related to [5574] and [5683]
112 body = document.body;
113 $body = jQuery( body );
115 assert.strictEqual( $body.attr( "foo" ), undefined, "Make sure that a non existent attribute returns undefined" );
117 body.setAttribute( "foo", "baz" );
118 assert.equal( $body.attr( "foo" ), "baz", "Make sure the dom attribute is retrieved when no expando is found" );
120 $body.attr( "foo", "cool" );
121 assert.equal( $body.attr( "foo" ), "cool", "Make sure that setting works well when both expando and dom attribute are available" );
123 body.removeAttribute( "foo" ); // Cleanup
125 select = document.createElement( "select" );
126 optgroup = document.createElement( "optgroup" );
127 option = document.createElement( "option" );
129 optgroup.appendChild( option );
130 select.appendChild( optgroup );
132 assert.equal( jQuery( option ).prop( "selected" ), true, "Make sure that a single option is selected, even when in an optgroup." );
134 $img = jQuery( "<img style='display:none' width='215' height='53' src='" + baseURL + "1x1.jpg'/>" ).appendTo( "body" );
135 assert.equal( $img.attr( "width" ), "215", "Retrieve width attribute on an element with display:none." );
136 assert.equal( $img.attr( "height" ), "53", "Retrieve height attribute on an element with display:none." );
138 // Check for style support
139 styleElem = jQuery( "<div></div>" ).appendTo( "#qunit-fixture" ).css( {
140 background: "url(UPPERlower.gif)"
142 assert.ok( !!~styleElem.attr( "style" ).indexOf( "UPPERlower.gif" ), "Check style attribute getter" );
143 assert.ok( !!~styleElem.attr( "style", "position:absolute;" ).attr( "style" ).indexOf( "absolute" ), "Check style setter" );
145 // Check value on button element (trac-1954)
146 $button = jQuery( "<button>text</button>" ).insertAfter( "#button" );
147 assert.strictEqual( $button.attr( "value" ), undefined, "Absence of value attribute on a button" );
148 assert.equal( $button.attr( "value", "foobar" ).attr( "value" ), "foobar", "Value attribute on a button does not return innerHTML" );
149 assert.equal( $button.attr( "value", "baz" ).html(), "text", "Setting the value attribute does not change innerHTML" );
151 // Attributes with a colon on a table element (trac-1591)
152 assert.equal( jQuery( "#table" ).attr( "test:attrib" ), undefined, "Retrieving a non-existent attribute on a table with a colon does not throw an error." );
153 assert.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." );
155 $form = jQuery( "<form class='something'></form>" ).appendTo( "#qunit-fixture" );
156 assert.equal( $form.attr( "class" ), "something", "Retrieve the class attribute on a form." );
158 $a = jQuery( "<a href='#' onclick='something()'>Click</a>" ).appendTo( "#qunit-fixture" );
159 assert.equal( $a.attr( "onclick" ), "something()", "Retrieve ^on attribute without anonymous function wrapper." );
161 assert.ok( jQuery( "<div></div>" ).attr( "doesntexist" ) === undefined, "Make sure undefined is returned when no attribute is found." );
162 assert.ok( jQuery( "<div></div>" ).attr( "title" ) === undefined, "Make sure undefined is returned when no attribute is found." );
163 assert.equal( jQuery( "<div></div>" ).attr( "title", "something" ).attr( "title" ), "something", "Set the title attribute." );
164 assert.ok( jQuery().attr( "doesntexist" ) === undefined, "Make sure undefined is returned when no element is there." );
165 assert.equal( jQuery( "<div></div>" ).attr( "value" ), undefined, "An unset value on a div returns undefined." );
166 assert.strictEqual( jQuery( "<select><option value='property'></option></select>" ).attr( "value" ), undefined, "An unset value on a select returns undefined." );
168 $form = jQuery( "#form" ).attr( "enctype", "multipart/form-data" );
169 assert.equal( $form.prop( "enctype" ), "multipart/form-data", "Set the enctype of a form (encoding in IE6/7 trac-6743)" );
173 QUnit.test( "attr(String) on cloned elements, trac-9646", function( assert ) {
177 input = jQuery( "<input name='tester' />" );
179 input.attr( "name" );
181 assert.strictEqual( input.clone( true ).attr( "name", "test" )[ 0 ].name, "test", "Name attribute should be changed on cloned element" );
183 div = jQuery( "<div id='tester'></div>" );
186 assert.strictEqual( div.clone( true ).attr( "id", "test" )[ 0 ].id, "test", "Id attribute should be changed on cloned element" );
188 input = jQuery( "<input value='tester' />" );
189 input.attr( "value" );
191 assert.strictEqual( input.clone( true ).attr( "value", "test" )[ 0 ].value, "test", "Value attribute should be changed on cloned element" );
193 assert.strictEqual( input.clone( true ).attr( "value", 42 )[ 0 ].value, "42", "Value attribute should be changed on cloned element" );
196 QUnit.test( "attr(String) in XML Files", function( assert ) {
198 var xml = createDashboardXML();
199 assert.equal( jQuery( "locations", xml ).attr( "class" ), "foo", "Check class attribute in XML document" );
200 assert.equal( jQuery( "location", xml ).attr( "for" ), "bar", "Check for attribute in XML document" );
201 assert.equal( jQuery( "location", xml ).attr( "checked" ), "different", "Check that hooks are not attached in XML document" );
204 QUnit.test( "attr(String, Function)", function( assert ) {
208 jQuery( "#text1" ).attr( "value", function() {
216 jQuery( "#text1" ).attr( "title", function( i ) {
220 "Set value with an index"
224 QUnit.test( "attr(Hash)", function( assert ) {
228 jQuery( "#qunit-fixture div" ).attr( {
231 } ).each( function() {
232 if ( this.getAttribute( "foo" ) !== "baz" && this.getAttribute( "zoo" ) !== "ping" ) {
237 assert.ok( pass, "Set Multiple Attributes" );
240 jQuery( "#text1" ).attr( {
241 "value": function() {
243 } } ).attr( "value" ),
245 "Set attribute to computed value #1"
249 jQuery( "#text1" ).attr( {
250 "title": function( i ) {
255 "Set attribute to computed value #2"
259 QUnit.test( "attr(String, Object)", function( assert ) {
262 var $input, $text, $details,
263 attributeNode, commentNode, textNode, obj,
265 check, thrown, button, $radio, $radios, $svg,
266 div = jQuery( "#qunit-fixture div" ).attr( "foo", "bar" ),
270 for ( ; i < div.length; i++ ) {
271 if ( div[ i ].getAttribute( "foo" ) !== "bar" ) {
277 assert.equal( fail, false, "Set Attribute, the #" + fail + " element didn't get the attribute 'foo'" );
280 jQuery( "#foo" ).attr( {
283 "Try to set an attribute to nothing"
286 jQuery( "#name" ).attr( "name", "something" );
287 assert.equal( jQuery( "#name" ).attr( "name" ), "something", "Set name attribute" );
288 jQuery( "#name" ).attr( "name", null );
289 assert.equal( jQuery( "#name" ).attr( "name" ), undefined, "Remove name attribute" );
291 $input = jQuery( "<input>", {
295 assert.equal( $input.attr( "name" ), "something", "Check element creation gets/sets the name attribute." );
296 assert.equal( $input.attr( "id" ), "specified", "Check element creation gets/sets the id attribute." );
298 // As of fixing trac-11115, we only guarantee boolean property update for checked and selected
299 $input = jQuery( "<input type='checkbox'/>" ).attr( "checked", true );
300 assert.equal( $input.prop( "checked" ), true, "Setting checked updates property (verified by .prop)" );
301 assert.equal( $input[ 0 ].checked, true, "Setting checked updates property (verified by native property)" );
302 $input = jQuery( "<option></option>" ).attr( "selected", true );
303 assert.equal( $input.prop( "selected" ), true, "Setting selected updates property (verified by .prop)" );
304 assert.equal( $input[ 0 ].selected, true, "Setting selected updates property (verified by native property)" );
306 $input = jQuery( "#check2" );
307 $input.prop( "checked", true ).prop( "checked", false ).attr( "checked", "checked" );
308 assert.equal( $input.attr( "checked" ), "checked", "Set checked (verified by .attr)" );
309 $input.prop( "checked", false ).prop( "checked", true ).attr( "checked", false );
310 assert.equal( $input.attr( "checked" ), undefined, "Remove checked (verified by .attr)" );
312 $input = jQuery( "#text1" ).prop( "readOnly", true ).prop( "readOnly", false ).attr( "readonly", "readonly" );
313 assert.equal( $input.attr( "readonly" ), "readonly", "Set readonly (verified by .attr)" );
314 $input.prop( "readOnly", false ).prop( "readOnly", true ).attr( "readonly", false );
315 assert.equal( $input.attr( "readonly" ), undefined, "Remove readonly (verified by .attr)" );
317 $input = jQuery( "#check2" ).attr( "checked", true ).attr( "checked", false ).prop( "checked", true );
318 assert.equal( $input[ 0 ].checked, true, "Set checked property (verified by native property)" );
319 assert.equal( $input.prop( "checked" ), true, "Set checked property (verified by .prop)" );
320 assert.equal( $input.attr( "checked" ), undefined, "Setting checked property doesn't affect checked attribute" );
321 $input.attr( "checked", false ).attr( "checked", "checked" ).prop( "checked", false );
322 assert.equal( $input[ 0 ].checked, false, "Clear checked property (verified by native property)" );
323 assert.equal( $input.prop( "checked" ), false, "Clear checked property (verified by .prop)" );
324 assert.equal( $input.attr( "checked" ), "checked", "Clearing checked property doesn't affect checked attribute" );
326 $input = jQuery( "#check2" ).attr( "checked", false ).attr( "checked", "checked" );
327 assert.equal( $input.attr( "checked" ), "checked", "Set checked to 'checked' (verified by .attr)" );
329 $radios = jQuery( "#checkedtest" ).find( "input[type='radio']" );
330 $radios.eq( 1 ).trigger( "click" );
331 assert.equal( $radios.eq( 1 ).prop( "checked" ), true, "Second radio was checked when clicked" );
332 assert.equal( $radios.eq( 0 ).attr( "checked" ), "checked", "First radio is still [checked]" );
334 $input = jQuery( "#text1" ).attr( "readonly", false ).prop( "readOnly", true );
335 assert.equal( $input[ 0 ].readOnly, true, "Set readonly property (verified by native property)" );
336 assert.equal( $input.prop( "readOnly" ), true, "Set readonly property (verified by .prop)" );
337 $input.attr( "readonly", true ).prop( "readOnly", false );
338 assert.equal( $input[ 0 ].readOnly, false, "Clear readonly property (verified by native property)" );
339 assert.equal( $input.prop( "readOnly" ), false, "Clear readonly property (verified by .prop)" );
341 $input = jQuery( "#name" ).attr( "maxlength", "5" );
342 assert.equal( $input[ 0 ].maxLength, 5, "Set maxlength (verified by native property)" );
343 $input.attr( "maxLength", "10" );
344 assert.equal( $input[ 0 ].maxLength, 10, "Set maxlength (verified by native property)" );
346 // HTML5 boolean attributes
347 $text = jQuery( "#text1" ).attr( {
348 "autofocus": "autofocus",
349 "required": "required"
351 assert.equal( $text.attr( "autofocus" ), "autofocus", "Reading autofocus attribute yields 'autofocus'" );
352 assert.equal( $text.attr( "autofocus", false ).attr( "autofocus" ), undefined, "Setting autofocus to false removes it" );
353 assert.equal( $text.attr( "required" ), "required", "Reading required attribute yields 'required'" );
354 assert.equal( $text.attr( "required", false ).attr( "required" ), undefined, "Setting required attribute to false removes it" );
356 $details = jQuery( "<details open></details>" ).appendTo( "#qunit-fixture" );
357 assert.equal( $details.attr( "open" ), "", "open attribute presence indicates true" );
358 assert.equal( $details.attr( "open", false ).attr( "open" ), undefined, "Setting open attribute to false removes it" );
360 $text.attr( "data-something", true );
361 assert.equal( $text.attr( "data-something" ), "true", "Set data attributes" );
362 assert.equal( $text.data( "something" ), true, "Setting data attributes are not affected by boolean settings" );
363 $text.attr( "data-another", "false" );
364 assert.equal( $text.attr( "data-another" ), "false", "Set data attributes" );
365 assert.equal( $text.data( "another" ), false, "Setting data attributes are not affected by boolean settings" );
366 assert.equal( $text.attr( "aria-disabled", false ).attr( "aria-disabled" ), "false", "Setting aria attributes are not affected by boolean settings" );
367 $text.removeData( "something" ).removeData( "another" ).removeAttr( "aria-disabled" );
369 jQuery( "#foo" ).attr( "contenteditable", true );
370 assert.equal( jQuery( "#foo" ).attr( "contenteditable" ), "true", "Enumerated attributes are set properly" );
372 attributeNode = document.createAttribute( "irrelevant" );
373 commentNode = document.createComment( "some comment" );
374 textNode = document.createTextNode( "some text" );
377 jQuery.each( [ commentNode, textNode, attributeNode ], function( i, elem ) {
378 var $elem = jQuery( elem );
379 $elem.attr( "nonexisting", "foo" );
380 assert.strictEqual( $elem.attr( "nonexisting" ), undefined, "attr(name, value) works correctly on comment and text nodes (bug trac-7500)." );
383 jQuery.each( [ window, document, obj, "#firstp" ], function( i, elem ) {
384 var oldVal = elem.nonexisting,
385 $elem = jQuery( elem );
386 assert.strictEqual( $elem.attr( "nonexisting" ), undefined, "attr works correctly for non existing attributes (bug trac-7500)." );
387 assert.equal( $elem.attr( "nonexisting", "foo" ).attr( "nonexisting" ), "foo", "attr falls back to prop on unsupported arguments" );
388 elem.nonexisting = oldVal;
391 // Register the property on the window for the previous assertion so it will be clean up
392 Globals.register( "nonexisting" );
394 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>" );
395 td = table.find( "td" ).eq( 0 );
396 td.attr( "rowspan", "2" );
397 assert.equal( td[ 0 ].rowSpan, 2, "Check rowspan is correctly set" );
398 td.attr( "colspan", "2" );
399 assert.equal( td[ 0 ].colSpan, 2, "Check colspan is correctly set" );
400 table.attr( "cellspacing", "2" );
401 assert.equal( table[ 0 ].cellSpacing, "2", "Check cellspacing is correctly set" );
403 assert.equal( jQuery( "#area1" ).attr( "value" ), undefined, "Value attribute is distinct from value property." );
406 jQuery( "#name" ).attr( "someAttr", "0" );
407 assert.equal( jQuery( "#name" ).attr( "someAttr" ), "0", "Set attribute to a string of '0'" );
408 jQuery( "#name" ).attr( "someAttr", 0 );
409 assert.equal( jQuery( "#name" ).attr( "someAttr" ), "0", "Set attribute to the number 0" );
410 jQuery( "#name" ).attr( "someAttr", 1 );
411 assert.equal( jQuery( "#name" ).attr( "someAttr" ), "1", "Set attribute to the number 1" );
413 // using contents will get comments regular, text, and comment nodes
414 j = jQuery( "#nonnodes" ).contents();
416 j.attr( "name", "attrvalue" );
417 assert.equal( j.attr( "name" ), "attrvalue", "Check node,textnode,comment for attr" );
418 j.removeAttr( "name" );
421 type = jQuery( "#check2" ).attr( "type" );
423 jQuery( "#check2" ).attr( "type", "hidden" );
424 assert.ok( true, "No exception thrown on input type change" );
426 assert.ok( true, "Exception thrown on input type change: " + e );
429 check = document.createElement( "input" );
432 jQuery( check ).attr( "type", "checkbox" );
436 assert.ok( thrown, "Exception thrown when trying to change type property" );
437 assert.equal( "checkbox", jQuery( check ).attr( "type" ), "Verify that you can change the type of an input element that isn't in the DOM" );
439 check = jQuery( "<input />" );
442 check.attr( "type", "checkbox" );
446 assert.ok( thrown, "Exception thrown when trying to change type property" );
447 assert.equal( "checkbox", check.attr( "type" ), "Verify that you can change the type of an input element that isn't in the DOM" );
449 button = jQuery( "#button" );
451 button.attr( "type", "submit" );
452 assert.ok( true, "No exception thrown on button type change" );
454 assert.ok( true, "Exception thrown on button type change: " + e );
457 $radio = jQuery( "<input>", {
460 // Use uppercase here to ensure the type
461 // attrHook is still used
463 } ).appendTo( "#testForm" );
464 assert.equal( $radio.val(), "sup", "Value is not reset when type is set after value on a radio" );
466 // Setting attributes on svg elements (bug trac-3116)
468 "<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'>" +
470 "<circle cx='200' cy='200' r='150' />" +
472 ).appendTo( "body" );
473 assert.equal( $svg.attr( "cx", 100 ).attr( "cx" ), "100", "Set attribute on svg element" );
476 // undefined values are chainable
477 jQuery( "#name" ).attr( "maxlength", "5" ).removeAttr( "nonexisting" );
478 assert.equal( typeof jQuery( "#name" ).attr( "maxlength", undefined ), "object", ".attr('attribute', undefined) is chainable (trac-5571)" );
479 assert.equal( jQuery( "#name" ).attr( "maxlength", undefined ).attr( "maxlength" ), "5", ".attr('attribute', undefined) does not change value (trac-5571)" );
480 assert.equal( jQuery( "#name" ).attr( "nonexisting", undefined ).attr( "nonexisting" ), undefined, ".attr('attribute', undefined) does not create attribute (trac-5571)" );
483 QUnit.test( "attr(non-ASCII)", function( assert ) {
486 var $div = jQuery( "<div Ω='omega' aØc='alpha'></div>" ).appendTo( "#qunit-fixture" );
488 assert.equal( $div.attr( "Ω" ), "omega", ".attr() exclusively lowercases characters in the range A-Z (gh-2730)" );
489 assert.equal( $div.attr( "AØC" ), "alpha", ".attr() exclusively lowercases characters in the range A-Z (gh-2730)" );
492 QUnit.test( "attr(String, Object) - Loaded via XML document", function( assert ) {
494 var xml = createDashboardXML(),
496 jQuery( "tab", xml ).each( function() {
497 titles.push( jQuery( this ).attr( "title" ) );
499 assert.equal( titles[ 0 ], "Location", "attr() in XML context: Check first title" );
500 assert.equal( titles[ 1 ], "Users", "attr() in XML context: Check second title" );
503 QUnit.test( "attr(String, Object) - Loaded via XML fragment", function( assert ) {
505 var frag = createXMLFragment(),
506 $frag = jQuery( frag );
508 $frag.attr( "test", "some value" );
509 assert.equal( $frag.attr( "test" ), "some value", "set attribute" );
510 $frag.attr( "test", null );
511 assert.equal( $frag.attr( "test" ), undefined, "remove attribute" );
514 QUnit.test( "attr('tabindex')", function( assert ) {
517 // elements not natively tabbable
518 assert.equal( jQuery( "#listWithTabIndex" ).attr( "tabindex" ), "5", "not natively tabbable, with tabindex set to 0" );
519 assert.equal( jQuery( "#divWithNoTabIndex" ).attr( "tabindex" ), undefined, "not natively tabbable, no tabindex set" );
522 assert.equal( jQuery( "#linkWithNoTabIndex" ).attr( "tabindex" ), undefined, "anchor with href, no tabindex set" );
523 assert.equal( jQuery( "#linkWithTabIndex" ).attr( "tabindex" ), "2", "anchor with href, tabindex set to 2" );
524 assert.equal( jQuery( "#linkWithNegativeTabIndex" ).attr( "tabindex" ), "-1", "anchor with href, tabindex set to -1" );
526 // anchor without href
527 assert.equal( jQuery( "#linkWithNoHrefWithNoTabIndex" ).attr( "tabindex" ), undefined, "anchor without href, no tabindex set" );
528 assert.equal( jQuery( "#linkWithNoHrefWithTabIndex" ).attr( "tabindex" ), "1", "anchor without href, tabindex set to 2" );
529 assert.equal( jQuery( "#linkWithNoHrefWithNegativeTabIndex" ).attr( "tabindex" ), "-1", "anchor without href, no tabindex set" );
532 QUnit.test( "attr('tabindex', value)", function( assert ) {
535 var element = jQuery( "#divWithNoTabIndex" );
536 assert.equal( element.attr( "tabindex" ), undefined, "start with no tabindex" );
538 // set a positive string
539 element.attr( "tabindex", "1" );
540 assert.equal( element.attr( "tabindex" ), "1", "set tabindex to 1 (string)" );
543 element.attr( "tabindex", "0" );
544 assert.equal( element.attr( "tabindex" ), "0", "set tabindex to 0 (string)" );
546 // set a negative string
547 element.attr( "tabindex", "-1" );
548 assert.equal( element.attr( "tabindex" ), "-1", "set tabindex to -1 (string)" );
550 // set a positive number
551 element.attr( "tabindex", 1 );
552 assert.equal( element.attr( "tabindex" ), "1", "set tabindex to 1 (number)" );
555 element.attr( "tabindex", 0 );
556 assert.equal( element.attr( "tabindex" ), "0", "set tabindex to 0 (number)" );
558 // set a negative number
559 element.attr( "tabindex", -1 );
560 assert.equal( element.attr( "tabindex" ), "-1", "set tabindex to -1 (number)" );
562 element = jQuery( "#linkWithTabIndex" );
563 assert.equal( element.attr( "tabindex" ), "2", "start with tabindex 2" );
565 element.attr( "tabindex", -1 );
566 assert.equal( element.attr( "tabindex" ), "-1", "set negative tabindex" );
569 QUnit.test( "removeAttr(String)", function( assert ) {
573 assert.equal( jQuery( "<div class='hello'></div>" ).removeAttr( "class" ).attr( "class" ), undefined, "remove class" );
574 assert.equal( jQuery( "#form" ).removeAttr( "id" ).attr( "id" ), undefined, "Remove id" );
575 assert.equal( jQuery( "#foo" ).attr( "style", "position:absolute;" ).removeAttr( "style" ).attr( "style" ), undefined, "Check removing style attribute" );
576 assert.equal( jQuery( "#form" ).attr( "style", "position:absolute;" ).removeAttr( "style" ).attr( "style" ), undefined, "Check removing style attribute on a form" );
577 assert.equal( jQuery( "<div style='position: absolute'></div>" ).appendTo( "#foo" ).removeAttr( "style" ).prop( "style" ).cssText, "", "Check removing style attribute (trac-9699 Webkit)" );
578 assert.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" );
580 jQuery( "#check1" ).removeAttr( "checked" ).prop( "checked", true ).removeAttr( "checked" );
581 assert.equal( document.getElementById( "check1" ).checked, true, "removeAttr should not set checked to false, since the checked attribute does NOT mirror the checked property" );
582 jQuery( "#text1" ).prop( "readOnly", true ).removeAttr( "readonly" );
583 assert.equal( document.getElementById( "text1" ).readOnly, false, "removeAttr sets boolean properties to false" );
585 jQuery( "#option2c" ).removeAttr( "selected" );
586 assert.equal( jQuery( "#option2d" ).attr( "selected" ), "selected", "Removing `selected` from an option that is not selected does not remove selected from the currently selected option (trac-10870)" );
589 $first = jQuery( "#first" ).attr( "contenteditable", "true" ).removeAttr( "contenteditable" );
590 assert.equal( $first.attr( "contenteditable" ), undefined, "Remove the contenteditable attribute" );
592 assert.ok( false, "Removing contenteditable threw an error (trac-10429)" );
595 $first = jQuery( "<div Case='mixed'></div>" );
596 assert.equal( $first.attr( "Case" ), "mixed", "case of attribute doesn't matter" );
597 $first.removeAttr( "Case" );
598 assert.equal( $first.attr( "Case" ), undefined, "mixed-case attribute was removed" );
601 QUnit.test( "removeAttr(String) in XML", function( assert ) {
603 var xml = createDashboardXML(),
604 iwt = jQuery( "infowindowtab", xml );
606 assert.equal( iwt.attr( "normal" ), "ab", "Check initial value" );
607 iwt.removeAttr( "Normal" );
608 assert.equal( iwt.attr( "normal" ), "ab", "Should still be there" );
609 iwt.removeAttr( "normal" );
610 assert.equal( iwt.attr( "normal" ), undefined, "Removed" );
612 assert.equal( iwt.attr( "mixedCase" ), "yes", "Check initial value" );
613 assert.equal( iwt.attr( "mixedcase" ), undefined, "toLowerCase not work good" );
614 iwt.removeAttr( "mixedcase" );
615 assert.equal( iwt.attr( "mixedCase" ), "yes", "Should still be there" );
616 iwt.removeAttr( "mixedCase" );
617 assert.equal( iwt.attr( "mixedCase" ), undefined, "Removed" );
620 QUnit.test( "removeAttr(Multi String, variable space width)", function( assert ) {
623 var div = jQuery( "<div id='a' alt='b' title='c' rel='d'></div>" ),
631 jQuery.each( tests, function( key, val ) {
632 assert.equal( div.attr( key ), val, "Attribute `" + key + "` exists, and has a value of `" + val + "`" );
635 div.removeAttr( "id alt title rel " );
637 jQuery.each( tests, function( key ) {
638 assert.equal( div.attr( key ), undefined, "Attribute `" + key + "` was removed" );
642 QUnit.test( "removeAttr(Multi String, non-HTML whitespace is valid in attribute names (gh-3003)", function( assert ) {
645 var div = jQuery( "<div id='a' data-\xA0='b' title='c' rel='d'></div>" );
653 jQuery.each( tests, function( key, val ) {
654 assert.equal( div.attr( key ), val, "Attribute \"" + key + "\" exists, and has a value of \"" + val + "\"" );
657 div.removeAttr( "id data-\xA0 title rel " );
659 jQuery.each( tests, function( key ) {
660 assert.equal( div.attr( key ), undefined, "Attribute \"" + key + "\" was removed" );
664 QUnit.test( "prop(String, Object)", function( assert ) {
668 assert.equal( jQuery( "#text1" ).prop( "value" ), "Test", "Check for value attribute" );
669 assert.equal( jQuery( "#text1" ).prop( "value", "Test2" ).prop( "defaultValue" ), "Test", "Check for defaultValue attribute" );
670 assert.equal( jQuery( "#select2" ).prop( "selectedIndex" ), 3, "Check for selectedIndex attribute" );
671 assert.equal( jQuery( "#foo" ).prop( "nodeName" ).toUpperCase(), "DIV", "Check for nodeName attribute" );
672 assert.equal( jQuery( "#foo" ).prop( "tagName" ).toUpperCase(), "DIV", "Check for tagName attribute" );
673 assert.equal( jQuery( "<option></option>" ).prop( "selected" ), false, "Check selected attribute on disconnected element." );
675 assert.equal( jQuery( "#listWithTabIndex" ).prop( "tabindex" ), 5, "Check retrieving tabindex" );
676 jQuery( "#text1" ).prop( "readonly", true );
677 assert.equal( document.getElementById( "text1" ).readOnly, true, "Check setting readOnly property with 'readonly'" );
678 assert.equal( jQuery( "#label-for" ).prop( "for" ), "action", "Check retrieving htmlFor" );
679 jQuery( "#text1" ).prop( "class", "test" );
680 assert.equal( document.getElementById( "text1" ).className, "test", "Check setting className with 'class'" );
681 assert.equal( jQuery( "#text1" ).prop( "maxlength" ), 30, "Check retrieving maxLength" );
682 jQuery( "#table" ).prop( "cellspacing", 1 );
683 assert.equal( jQuery( "#table" ).prop( "cellSpacing" ), "1", "Check setting and retrieving cellSpacing" );
684 jQuery( "#table" ).prop( "cellpadding", 1 );
685 assert.equal( jQuery( "#table" ).prop( "cellPadding" ), "1", "Check setting and retrieving cellPadding" );
686 jQuery( "#table" ).prop( "rowspan", 1 );
687 assert.equal( jQuery( "#table" ).prop( "rowSpan" ), 1, "Check setting and retrieving rowSpan" );
688 jQuery( "#table" ).prop( "colspan", 1 );
689 assert.equal( jQuery( "#table" ).prop( "colSpan" ), 1, "Check setting and retrieving colSpan" );
690 jQuery( "#table" ).prop( "usemap", 1 );
691 assert.equal( jQuery( "#table" ).prop( "useMap" ), 1, "Check setting and retrieving useMap" );
692 jQuery( "#table" ).prop( "frameborder", 1 );
693 assert.equal( jQuery( "#table" ).prop( "frameBorder" ), 1, "Check setting and retrieving frameBorder" );
696 QUnit.test( "prop(String, Object) on null/undefined", function( assert ) {
700 var select, optgroup, option, attributeNode, commentNode, textNode, obj, $form,
701 body = document.body,
702 $body = jQuery( body );
704 assert.ok( $body.prop( "nextSibling" ) === null, "Make sure a null expando returns null" );
706 assert.equal( $body.prop( "foo" ), "bar", "Make sure the expando is preferred over the dom attribute" );
707 body.foo = undefined;
708 assert.ok( $body.prop( "foo" ) === undefined, "Make sure the expando is preferred over the dom attribute, even if undefined" );
710 select = document.createElement( "select" );
711 optgroup = document.createElement( "optgroup" );
712 option = document.createElement( "option" );
714 optgroup.appendChild( option );
715 select.appendChild( optgroup );
717 assert.equal( jQuery( option ).prop( "selected" ), true, "Make sure that a single option is selected, even when in an optgroup." );
718 assert.equal( jQuery( document ).prop( "nodeName" ), "#document", "prop works correctly on document nodes (bug trac-7451)." );
720 attributeNode = document.createAttribute( "irrelevant" );
721 commentNode = document.createComment( "some comment" );
722 textNode = document.createTextNode( "some text" );
724 jQuery.each( [ document, attributeNode, commentNode, textNode, obj, "#firstp" ], function( i, ele ) {
725 assert.strictEqual( jQuery( ele ).prop( "nonexisting" ), undefined, "prop works correctly for non existing attributes (bug trac-7500)." );
729 jQuery.each( [ document, obj ], function( i, ele ) {
730 var $ele = jQuery( ele );
731 $ele.prop( "nonexisting", "foo" );
732 assert.equal( $ele.prop( "nonexisting" ), "foo", "prop(name, value) works correctly for non existing attributes (bug trac-7500)." );
734 jQuery( document ).removeProp( "nonexisting" );
736 $form = jQuery( "#form" ).prop( "enctype", "multipart/form-data" );
737 assert.equal( $form.prop( "enctype" ), "multipart/form-data", "Set the enctype of a form (encoding in IE6/7 trac-6743)" );
740 QUnit.test( "prop('tabindex')", function( assert ) {
743 // inputs without tabIndex attribute
744 assert.equal( jQuery( "#inputWithoutTabIndex" ).prop( "tabindex" ), 0, "input without tabindex" );
745 assert.equal( jQuery( "#buttonWithoutTabIndex" ).prop( "tabindex" ), 0, "button without tabindex" );
746 assert.equal( jQuery( "#textareaWithoutTabIndex" ).prop( "tabindex" ), 0, "textarea without tabindex" );
748 // elements not natively tabbable
749 assert.equal( jQuery( "#listWithTabIndex" ).prop( "tabindex" ), 5, "not natively tabbable, with tabindex set to 0" );
750 assert.equal( jQuery( "#divWithNoTabIndex" ).prop( "tabindex" ), -1, "not natively tabbable, no tabindex set" );
753 assert.equal( jQuery( "#linkWithNoTabIndex" ).prop( "tabindex" ), 0, "anchor with href, no tabindex set" );
754 assert.equal( jQuery( "#linkWithTabIndex" ).prop( "tabindex" ), 2, "anchor with href, tabindex set to 2" );
755 assert.equal( jQuery( "#linkWithNegativeTabIndex" ).prop( "tabindex" ), -1, "anchor with href, tabindex set to -1" );
757 // anchor without href
758 assert.equal( jQuery( "#linkWithNoHrefWithNoTabIndex" ).prop( "tabindex" ), -1, "anchor without href, no tabindex set" );
759 assert.equal( jQuery( "#linkWithNoHrefWithTabIndex" ).prop( "tabindex" ), 1, "anchor without href, tabindex set to 2" );
760 assert.equal( jQuery( "#linkWithNoHrefWithNegativeTabIndex" ).prop( "tabindex" ), -1, "anchor without href, no tabindex set" );
763 QUnit.test( "image.prop( 'tabIndex' )", function( assert ) {
765 var image = jQuery( "<img src='" + baseURL + "1x1.jpg' />" )
766 .appendTo( "#qunit-fixture" );
767 assert.equal( image.prop( "tabIndex" ), -1, "tabIndex on image" );
770 QUnit.test( "prop('tabindex', value)", function( assert ) {
774 element = jQuery( "#divWithNoTabIndex" );
776 assert.equal( element.prop( "tabindex" ), -1, "start with no tabindex" );
778 // set a positive string
779 element.prop( "tabindex", "1" );
780 assert.equal( element.prop( "tabindex" ), 1, "set tabindex to 1 (string)" );
783 element.prop( "tabindex", "0" );
784 assert.equal( element.prop( "tabindex" ), 0, "set tabindex to 0 (string)" );
786 // set a negative string
787 element.prop( "tabindex", "-1" );
788 assert.equal( element.prop( "tabindex" ), -1, "set tabindex to -1 (string)" );
790 // set a positive number
791 element.prop( "tabindex", 1 );
792 assert.equal( element.prop( "tabindex" ), 1, "set tabindex to 1 (number)" );
795 element.prop( "tabindex", 0 );
796 assert.equal( element.prop( "tabindex" ), 0, "set tabindex to 0 (number)" );
798 // set a negative number
799 element.prop( "tabindex", -1 );
800 assert.equal( element.prop( "tabindex" ), -1, "set tabindex to -1 (number)" );
802 element = jQuery( "#linkWithTabIndex" );
803 assert.equal( element.prop( "tabindex" ), 2, "start with tabindex 2" );
805 element.prop( "tabindex", -1 );
806 assert.equal( element.prop( "tabindex" ), -1, "set negative tabindex" );
808 clone = element.clone();
809 clone.prop( "tabindex", 1 );
810 assert.equal( clone[ 0 ].getAttribute( "tabindex" ), "1", "set tabindex on cloned element" );
813 QUnit.test( "option.prop('selected', true) affects select.selectedIndex (gh-2732)", function( assert ) {
816 function addOptions( $elem ) {
818 jQuery( "<option></option>" ).val( "a" ).text( "One" ),
819 jQuery( "<option></option>" ).val( "b" ).text( "Two" ),
820 jQuery( "<option></option>" ).val( "c" ).text( "Three" )
822 .find( "[value=a]" ).prop( "selected", true ).end()
823 .find( "[value=c]" ).prop( "selected", true ).end();
827 $select = jQuery( "<select></select>" );
829 // Check select with options
830 addOptions( $select ).appendTo( "#qunit-fixture" );
831 $select.find( "[value=b]" ).prop( "selected", true );
832 assert.equal( $select[ 0 ].selectedIndex, 1, "Setting option selected affects selectedIndex" );
836 // Check select with optgroup
837 $optgroup = jQuery( "<optgroup></optgroup>" );
838 addOptions( $optgroup ).appendTo( $select );
839 $select.find( "[value=b]" ).prop( "selected", true );
841 assert.equal( $select[ 0 ].selectedIndex, 1, "Setting option in optgroup selected affects selectedIndex" );
844 QUnit.test( "removeProp(String)", function( assert ) {
846 var attributeNode = document.createAttribute( "irrelevant" ),
847 commentNode = document.createComment( "some comment" ),
848 textNode = document.createTextNode( "some text" ),
852 jQuery( "#firstp" ).prop( "nonexisting", "foo" ).removeProp( "nonexisting" )[ 0 ].nonexisting,
854 "removeprop works correctly on DOM element nodes"
857 jQuery.each( [ document, obj ], function( i, ele ) {
858 var $ele = jQuery( ele );
859 $ele.prop( "nonexisting", "foo" ).removeProp( "nonexisting" );
860 assert.strictEqual( ele.nonexisting, undefined, "removeProp works correctly on non DOM element nodes (bug trac-7500)." );
862 jQuery.each( [ commentNode, textNode, attributeNode ], function( i, ele ) {
863 var $ele = jQuery( ele );
864 $ele.prop( "nonexisting", "foo" ).removeProp( "nonexisting" );
865 assert.strictEqual( ele.nonexisting, undefined, "removeProp works correctly on non DOM element nodes (bug trac-7500)." );
869 QUnit.test( "val() after modification", function( assert ) {
873 document.getElementById( "text1" ).value = "bla";
874 assert.equal( jQuery( "#text1" ).val(), "bla", "Check for modified value of input element" );
877 QUnit.test( "val()", function( assert ) {
879 assert.expect( 20 + ( jQuery.fn.serialize ? 6 : 0 ) );
882 assert.equal( jQuery( "#text1" ).val(), "Test", "Check for value of input element" );
884 // ticket trac-1714 this caused a JS error in IE
885 assert.equal( jQuery( "#first" ).val(), "", "Check a paragraph element to see if it has a value" );
886 assert.ok( jQuery( [] ).val() === undefined, "Check an empty jQuery object will return undefined from val" );
888 assert.equal( jQuery( "#select2" ).val(), "3", "Call val() on a single='single' select" );
890 assert.deepEqual( jQuery( "#select3" ).val(), [ "1", "2" ], "Call val() on a multiple='multiple' select" );
892 assert.equal( jQuery( "#option3c" ).val(), "2", "Call val() on a option element with value" );
894 assert.equal( jQuery( "#option3a" ).val(), "", "Call val() on a option element with empty value" );
896 assert.equal( jQuery( "#option3e" ).val(), "no value", "Call val() on a option element with no value attribute" );
898 assert.equal( jQuery( "#option3a" ).val(), "", "Call val() on a option element with no value attribute" );
900 jQuery( "#select3" ).val( "" );
901 assert.deepEqual( jQuery( "#select3" ).val(), [ "" ], "Call val() on a multiple='multiple' select" );
903 assert.deepEqual( jQuery( "#select4" ).val(), [], "Call val() on multiple='multiple' select with all disabled options" );
905 jQuery( "#select4 optgroup" ).add( "#select4 > [disabled]" ).attr( "disabled", false );
906 assert.deepEqual( jQuery( "#select4" ).val(), [ "2", "3" ], "Call val() on multiple='multiple' select with some disabled options" );
908 jQuery( "#select4" ).attr( "disabled", true );
909 assert.deepEqual( jQuery( "#select4" ).val(), [ "2", "3" ], "Call val() on disabled multiple='multiple' select" );
911 assert.equal( jQuery( "#select5" ).val(), "3", "Check value on ambiguous select." );
913 jQuery( "#select5" ).val( 1 );
914 assert.equal( jQuery( "#select5" ).val(), "1", "Check value on ambiguous select." );
916 jQuery( "#select5" ).val( 3 );
917 assert.equal( jQuery( "#select5" ).val(), "3", "Check value on ambiguous select." );
920 jQuery( "<select name='select12584' id='select12584'><option value='1' disabled='disabled'>1</option></select>" ).val(),
922 "Select-one with only option disabled (trac-12584)"
925 if ( includesModule( "serialize" ) ) {
926 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" );
928 assert.deepEqual( checks.serialize(), "", "Get unchecked values." );
930 assert.equal( checks.eq( 3 ).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
932 checks.val( [ "2" ] );
933 assert.deepEqual( checks.serialize(), "test=2", "Get a single checked value." );
935 checks.val( [ "1", "" ] );
936 assert.deepEqual( checks.serialize(), "test=1&test=", "Get multiple checked values." );
938 checks.val( [ "", "2" ] );
939 assert.deepEqual( checks.serialize(), "test=2&test=", "Get multiple checked values." );
941 checks.val( [ "1", "on" ] );
942 assert.deepEqual( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
947 $button = jQuery( "<button value='foobar'>text</button>" ).insertAfter( "#button" );
948 assert.equal( $button.val(), "foobar", "Value retrieval on a button does not return innerHTML" );
949 assert.equal( $button.val( "baz" ).html(), "text", "Setting the value does not change innerHTML" );
951 assert.equal( jQuery( "<option></option>" ).val( "test" ).attr( "value" ), "test", "Setting value sets the value attribute" );
954 QUnit.test( "val() with non-matching values on dropdown list", function( assert ) {
957 jQuery( "#select5" ).val( "" );
958 assert.equal( jQuery( "#select5" ).val(), null, "Non-matching set on select-one" );
960 var select6 = jQuery( "<select multiple id=\"select6\"><option value=\"1\">A</option><option value=\"2\">B</option></select>" ).appendTo( "#form" );
961 jQuery( select6 ).val( "nothing" );
962 assert.deepEqual( jQuery( select6 ).val(), [], "Non-matching set (single value) on select-multiple" );
964 jQuery( select6 ).val( [ "nothing1", "nothing2" ] );
965 assert.deepEqual( jQuery( select6 ).val(), [], "Non-matching set (array of values) on select-multiple" );
970 QUnit.test( "val() respects numbers without exception (Bug trac-9319) - progress",
975 var $progress = jQuery( "<progress max='10' value='1.5'></progress>" );
978 assert.equal( typeof $progress.val(), "number", "progress, returns a number and does not throw exception" );
979 assert.equal( $progress.val(), $progress[ 0 ].value, "progress, api matches host and does not throw exception" );
986 // IE doesn't support <meter>
987 QUnit.testUnlessIE( "val() respects numbers without exception (Bug trac-9319) - meter",
992 var $meter = jQuery( "<meter min='0' max='10' value='5.6'></meter>" );
995 assert.equal( typeof $meter.val(), "number", "meter, returns a number and does not throw exception" );
996 assert.equal( $meter.val(), $meter[ 0 ].value, "meter, api matches host and does not throw exception" );
1002 var testVal = function( valueObj, assert ) {
1005 jQuery( "#text1" ).val( valueObj( "test" ) );
1006 assert.equal( document.getElementById( "text1" ).value, "test", "Check for modified (via val(String)) value of input element" );
1008 jQuery( "#text1" ).val( valueObj( undefined ) );
1009 assert.equal( document.getElementById( "text1" ).value, "", "Check for modified (via val(undefined)) value of input element" );
1011 jQuery( "#text1" ).val( valueObj( 67 ) );
1012 assert.equal( document.getElementById( "text1" ).value, "67", "Check for modified (via val(Number)) value of input element" );
1014 jQuery( "#text1" ).val( valueObj( null ) );
1015 assert.equal( document.getElementById( "text1" ).value, "", "Check for modified (via val(null)) value of input element" );
1018 $select = jQuery( "<select multiple><option value='1'></option><option value='2'></option></select>" ),
1019 $select1 = jQuery( "#select1" );
1021 $select1.val( valueObj( "3" ) );
1022 assert.equal( $select1.val(), "3", "Check for modified (via val(String)) value of select element" );
1024 $select1.val( valueObj( 2 ) );
1025 assert.equal( $select1.val(), "2", "Check for modified (via val(Number)) value of select element" );
1027 $select1.append( "<option value='4'>four</option>" );
1028 $select1.val( valueObj( 4 ) );
1029 assert.equal( $select1.val(), "4", "Should be possible to set the val() to a newly created option" );
1031 // using contents will get comments regular, text, and comment nodes
1032 j = jQuery( "#nonnodes" ).contents();
1033 j.val( valueObj( "asdf" ) );
1034 assert.equal( j.val(), "asdf", "Check node,textnode,comment with val()" );
1035 j.removeAttr( "value" );
1037 $select.val( valueObj( [ "1", "2" ] ) );
1038 assert.deepEqual( $select.val(), [ "1", "2" ], "Should set array of values" );
1041 QUnit.test( "val(String/Number)", function( assert ) {
1042 testVal( bareObj, assert );
1045 QUnit.test( "val(Function)", function( assert ) {
1046 testVal( functionReturningObj, assert );
1049 QUnit.test( "val(Array of Numbers) (Bug trac-7123)", function( assert ) {
1051 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' />" );
1052 var elements = jQuery( "#form input[name=arrayTest]" ).val( [ 1, 2 ] );
1053 assert.ok( elements[ 0 ].checked, "First element was checked" );
1054 assert.ok( elements[ 1 ].checked, "Second element was checked" );
1055 assert.ok( !elements[ 2 ].checked, "Third element was unchecked" );
1056 assert.ok( !elements[ 3 ].checked, "Fourth element remained unchecked" );
1061 QUnit.test( "val(Function) with incoming value", function( assert ) {
1062 assert.expect( 10 );
1064 var oldVal = jQuery( "#text1" ).val();
1066 jQuery( "#text1" ).val( function( i, val ) {
1067 assert.equal( val, oldVal, "Make sure the incoming value is correct." );
1071 assert.equal( document.getElementById( "text1" ).value, "test", "Check for modified (via val(String)) value of input element" );
1073 oldVal = jQuery( "#text1" ).val();
1075 jQuery( "#text1" ).val( function( i, val ) {
1076 assert.equal( val, oldVal, "Make sure the incoming value is correct." );
1080 assert.equal( document.getElementById( "text1" ).value, "67", "Check for modified (via val(Number)) value of input element" );
1082 oldVal = jQuery( "#select1" ).val();
1084 jQuery( "#select1" ).val( function( i, val ) {
1085 assert.equal( val, oldVal, "Make sure the incoming value is correct." );
1089 assert.equal( jQuery( "#select1" ).val(), "3", "Check for modified (via val(String)) value of select element" );
1091 oldVal = jQuery( "#select1" ).val();
1093 jQuery( "#select1" ).val( function( i, val ) {
1094 assert.equal( val, oldVal, "Make sure the incoming value is correct." );
1098 assert.equal( jQuery( "#select1" ).val(), "2", "Check for modified (via val(Number)) value of select element" );
1100 jQuery( "#select1" ).append( "<option value='4'>four</option>" );
1102 oldVal = jQuery( "#select1" ).val();
1104 jQuery( "#select1" ).val( function( i, val ) {
1105 assert.equal( val, oldVal, "Make sure the incoming value is correct." );
1109 assert.equal( jQuery( "#select1" ).val(), "4", "Should be possible to set the val() to a newly created option" );
1112 // testing if a form.reset() breaks a subsequent call to a select element's .val() (in IE only)
1113 QUnit.test( "val(select) after form.reset() (Bug trac-2551)", function( assert ) {
1116 jQuery( "<form id='kk' name='kk'><select id='kkk'><option value='cf'>cf</option><option value='gf'>gf</option></select></form>" ).appendTo( "#qunit-fixture" );
1118 jQuery( "#kkk" ).val( "gf" );
1120 document.kk.reset();
1122 assert.equal( jQuery( "#kkk" )[ 0 ].value, "cf", "Check value of select after form reset." );
1123 assert.equal( jQuery( "#kkk" ).val(), "cf", "Check value of select after form reset." );
1125 // re-verify the multi-select is not broken (after form.reset) by our fix for single-select
1126 assert.deepEqual( jQuery( "#select3" ).val(), [ "1", "2" ], "Call val() on a multiple='multiple' select" );
1128 jQuery( "#kk" ).remove();
1131 QUnit.test( "select.val(space characters) (gh-2978)", function( assert ) {
1132 assert.expect( 37 );
1134 var $select = jQuery( "<select></select>" ).appendTo( "#qunit-fixture" ),
1150 "\\u00a0": "\u00a0",
1154 jQuery.each( spaces, function( key, obj ) {
1155 var value = obj.html || obj;
1156 html += "<option value='attr" + value + "'></option>";
1157 html += "<option value='at" + value + "tr'></option>";
1158 html += "<option value='" + value + "attr'></option>";
1160 $select.html( html );
1162 jQuery.each( spaces, function( key, obj ) {
1163 var val = obj.val || obj;
1164 $select.val( "attr" + val );
1165 assert.equal( $select.val(), "attr" + val, "Value ending with space character (" + key + ") selected (attr)" );
1167 $select.val( "at" + val + "tr" );
1168 assert.equal( $select.val(), "at" + val + "tr", "Value with space character (" + key + ") in the middle selected (attr)" );
1170 $select.val( val + "attr" );
1171 assert.equal( $select.val(), val + "attr", "Value starting with space character (" + key + ") selected (attr)" );
1174 jQuery.each( spaces, function( key, obj ) {
1175 var value = obj.html || obj,
1176 val = obj.val || obj;
1178 html += "<option>text" + value + "</option>";
1179 html += "<option>te" + value + "xt</option>";
1180 html += "<option>" + value + "text</option>";
1181 $select.html( html );
1184 if ( /^\\u/.test( key ) ) {
1185 $select.val( val + "text" );
1186 assert.equal( $select.val(), val + "text", "Value with non-HTML space character at beginning is not stripped (" + key + ") selected (" + key + "text)" );
1187 $select.val( "te" + val + "xt" );
1188 assert.equal( $select.val(), "te" + val + "xt", "Value with non-space whitespace character (" + key + ") in the middle selected (text)" );
1189 $select.val( "text" + val );
1190 assert.equal( $select.val(), "text" + val, "Value with non-HTML space character at end is not stripped (" + key + ") selected (text" + key + ")" );
1192 $select.val( "text" );
1193 assert.equal( $select.val(), "text", "Value with HTML space character at beginning or end is stripped (" + key + ") selected (text)" );
1194 $select.val( "te xt" );
1195 assert.equal( $select.val(), "te xt", "Value with space character (" + key + ") in the middle selected (text)" );
1200 QUnit.test( "radio.val(space characters)", function( assert ) {
1201 assert.expect( 42 );
1203 var radio = jQuery( "<input type='radio'/>" ).appendTo( "#qunit-fixture" ),
1219 "\\u00a0": "\u00a0",
1223 jQuery.each( spaces, function( key, obj ) {
1224 var val = obj.val || obj;
1226 radio.val( "attr" + val );
1227 assert.equal( radio.val(), "attr" + val, "Value ending with space character (" + key + ") returned (set via val())" );
1229 radio.val( "at" + val + "tr" );
1230 assert.equal( radio.val(), "at" + val + "tr", "Value with space character (" + key + ") in the middle returned (set via val())" );
1232 radio.val( val + "attr" );
1233 assert.equal( radio.val(), val + "attr", "Value starting with space character (" + key + ") returned (set via val())" );
1236 jQuery.each( spaces, function( key, obj ) {
1237 var val = obj.val || obj,
1238 htmlVal = obj.html || obj;
1240 radio = jQuery( "<input type='radio' value='attr" + htmlVal + "'/>" ).appendTo( "#qunit-fixture" );
1241 assert.equal( radio.val(), "attr" + val, "Value ending with space character (" + key + ") returned (set via HTML)" );
1243 radio = jQuery( "<input type='radio' value='at" + htmlVal + "tr'/>" ).appendTo( "#qunit-fixture" );
1244 assert.equal( radio.val(), "at" + val + "tr", "Value with space character (" + key + ") in the middle returned (set via HTML)" );
1246 radio = jQuery( "<input type='radio' value='" + htmlVal + "attr'/>" ).appendTo( "#qunit-fixture" );
1247 assert.equal( radio.val(), val + "attr", "Value starting with space character (" + key + ") returned (set via HTML)" );
1251 var testAddClass = function( valueObj, assert ) {
1255 div = jQuery( "#qunit-fixture div" );
1256 div.addClass( valueObj( "test" ) );
1258 for ( i = 0; i < div.length; i++ ) {
1259 if ( !~div.get( i ).className.indexOf( "test" ) ) {
1263 assert.ok( pass, "Add Class" );
1265 // using contents will get regular, text, and comment nodes
1266 j = jQuery( "#nonnodes" ).contents();
1267 j.addClass( valueObj( "asdf" ) );
1268 assert.ok( j.hasClass( "asdf" ), "Check node,textnode,comment for addClass" );
1270 div = jQuery( "<div></div>" );
1272 div.addClass( valueObj( "test" ) );
1273 assert.equal( div.attr( "class" ), "test", "Make sure there's no extra whitespace." );
1275 div.attr( "class", " foo" );
1276 div.addClass( valueObj( "test" ) );
1277 assert.equal( div.attr( "class" ), "foo test", "Make sure there's no extra whitespace." );
1279 div.attr( "class", "foo" );
1280 div.addClass( valueObj( "bar baz" ) );
1281 assert.equal( div.attr( "class" ), "foo bar baz", "Make sure there isn't too much trimming." );
1284 div.addClass( valueObj( "foo" ) ).addClass( valueObj( "foo" ) );
1285 assert.equal( div.attr( "class" ), "foo", "Do not add the same class twice in separate calls." );
1287 div.addClass( valueObj( "fo" ) );
1288 assert.equal( div.attr( "class" ), "foo fo", "Adding a similar class does not get interrupted." );
1289 div.removeClass().addClass( "wrap2" );
1290 assert.ok( div.addClass( "wrap" ).hasClass( "wrap" ), "Can add similarly named classes" );
1293 div.addClass( valueObj( "bar bar" ) );
1294 assert.equal( div.attr( "class" ), "bar", "Do not add the same class twice in the same call." );
1297 QUnit.test( "addClass(String)", function( assert ) {
1298 testAddClass( bareObj, assert );
1301 QUnit.test( "addClass(Function)", function( assert ) {
1302 testAddClass( functionReturningObj, assert );
1305 QUnit.test( "addClass(Array)", function( assert ) {
1306 testAddClass( arrayFromString, assert );
1309 QUnit.test( "addClass(Function) with incoming value", function( assert ) {
1310 assert.expect( 59 );
1312 div = jQuery( "#qunit-fixture div" ),
1313 old = div.map( function() {
1314 return jQuery( this ).attr( "class" ) || "";
1317 div.addClass( function( i, val ) {
1318 assert.equal( val, old[ i ], "Make sure the incoming value is correct." );
1323 for ( i = 0; i < div.length; i++ ) {
1324 if ( div.get( i ).className.indexOf( "test" ) === -1 ) {
1328 assert.ok( pass, "Add Class" );
1331 var testRemoveClass = function( valueObj, assert ) {
1334 var $set = jQuery( "#qunit-fixture div" ),
1335 div = document.createElement( "div" );
1337 $set.addClass( "test" ).removeClass( valueObj( "test" ) );
1339 assert.ok( !$set.is( ".test" ), "Remove Class" );
1341 $set.addClass( "test" ).addClass( "foo" ).addClass( "bar" );
1342 $set.removeClass( valueObj( "test" ) ).removeClass( valueObj( "bar" ) ).removeClass( valueObj( "foo" ) );
1344 assert.ok( !$set.is( ".test,.bar,.foo" ), "Remove multiple classes" );
1346 // Make sure that a null value doesn't cause problems
1347 $set.eq( 0 ).addClass( "expected" ).removeClass( valueObj( null ) );
1348 assert.ok( $set.eq( 0 ).is( ".expected" ), "Null value passed to removeClass" );
1350 $set.eq( 0 ).addClass( "expected" ).removeClass( valueObj( "" ) );
1351 assert.ok( $set.eq( 0 ).is( ".expected" ), "Empty string passed to removeClass" );
1353 // using contents will get regular, text, and comment nodes
1354 $set = jQuery( "#nonnodes" ).contents();
1355 $set.removeClass( valueObj( "asdf" ) );
1356 assert.ok( !$set.hasClass( "asdf" ), "Check node,textnode,comment for removeClass" );
1358 jQuery( div ).removeClass( valueObj( "foo" ) );
1359 assert.strictEqual( jQuery( div ).attr( "class" ), undefined, "removeClass doesn't create a class attribute" );
1361 div.className = " test foo ";
1363 jQuery( div ).removeClass( valueObj( "foo" ) );
1364 assert.equal( div.className, "test", "Make sure remaining className is trimmed." );
1366 div.className = " test ";
1368 jQuery( div ).removeClass( valueObj( "test" ) );
1369 assert.equal( div.className, "", "Make sure there is nothing left after everything is removed." );
1372 QUnit.test( "removeClass(String) - simple", function( assert ) {
1373 testRemoveClass( bareObj, assert );
1376 QUnit.test( "removeClass(Function) - simple", function( assert ) {
1377 testRemoveClass( functionReturningObj, assert );
1380 QUnit.test( "removeClass(Array) - simple", function( assert ) {
1381 testRemoveClass( arrayFromString, assert );
1384 QUnit.test( "removeClass(Function) with incoming value", function( assert ) {
1385 assert.expect( 59 );
1387 var $divs = jQuery( "#qunit-fixture div" ).addClass( "test" ), old = $divs.map( function() {
1388 return jQuery( this ).attr( "class" );
1391 $divs.removeClass( function( i, val ) {
1392 assert.equal( val, old[ i ], "Make sure the incoming value is correct." );
1396 assert.ok( !$divs.is( ".test" ), "Remove Class" );
1399 QUnit.test( "removeClass() removes duplicates", function( assert ) {
1402 var $div = jQuery( jQuery.parseHTML( "<div class='x x x'></div>" ) );
1404 $div.removeClass( "x" );
1406 assert.ok( !$div.hasClass( "x" ), "Element with multiple same classes does not escape the wrath of removeClass()" );
1409 QUnit.test( "removeClass(undefined) is a no-op", function( assert ) {
1412 var $div = jQuery( "<div class='base second'></div>" );
1413 $div.removeClass( undefined );
1415 assert.ok( $div.hasClass( "base" ) && $div.hasClass( "second" ), "Element still has classes after removeClass(undefined)" );
1418 var testToggleClass = function( valueObj, assert ) {
1419 assert.expect( 11 );
1421 var e = jQuery( "#firstp" );
1422 assert.ok( !e.is( ".test" ), "Assert class not present" );
1423 e.toggleClass( valueObj( "test" ) );
1424 assert.ok( e.is( ".test" ), "Assert class present" );
1425 e.toggleClass( valueObj( "test" ) );
1426 assert.ok( !e.is( ".test" ), "Assert class not present" );
1428 // class name with a boolean
1429 e.toggleClass( valueObj( "test" ), false );
1430 assert.ok( !e.is( ".test" ), "Assert class not present" );
1431 e.toggleClass( valueObj( "test" ), false );
1432 assert.ok( !e.is( ".test" ), "Assert class still not present" );
1433 e.toggleClass( valueObj( "test" ), true );
1434 assert.ok( e.is( ".test" ), "Assert class present" );
1435 e.toggleClass( valueObj( "test" ), true );
1436 assert.ok( e.is( ".test" ), "Assert class still present" );
1437 e.toggleClass( valueObj( "test" ), false );
1438 assert.ok( !e.is( ".test" ), "Assert class not present" );
1440 // multiple class names
1441 e.addClass( "testA testB" );
1442 assert.ok( e.is( ".testA.testB" ), "Assert 2 different classes present" );
1443 e.toggleClass( valueObj( "testB testC" ) );
1444 assert.ok( ( e.is( ".testA.testC" ) && !e.is( ".testB" ) ), "Assert 1 class added, 1 class removed, and 1 class kept" );
1445 e.toggleClass( valueObj( "testA testC" ) );
1446 assert.ok( ( !e.is( ".testA" ) && !e.is( ".testB" ) && !e.is( ".testC" ) ), "Assert no class present" );
1449 QUnit.test( "toggleClass(String|boolean|undefined[, boolean])", function( assert ) {
1450 testToggleClass( bareObj, assert );
1453 QUnit.test( "toggleClass(Function[, boolean])", function( assert ) {
1454 testToggleClass( functionReturningObj, assert );
1457 QUnit.test( "toggleClass(Array[, boolean])", function( assert ) {
1458 testToggleClass( arrayFromString, assert );
1461 QUnit.test( "toggleClass(Function[, boolean]) with incoming value", function( assert ) {
1462 assert.expect( 14 );
1464 var e = jQuery( "#firstp" ),
1465 old = e.attr( "class" ) || "";
1467 assert.ok( !e.is( ".test" ), "Assert class not present" );
1469 e.toggleClass( function( i, val ) {
1470 assert.equal( old, val, "Make sure the incoming value is correct." );
1473 assert.ok( e.is( ".test" ), "Assert class present" );
1475 old = e.attr( "class" );
1477 e.toggleClass( function( i, val ) {
1478 assert.equal( old, val, "Make sure the incoming value is correct." );
1481 assert.ok( !e.is( ".test" ), "Assert class not present" );
1483 old = e.attr( "class" ) || "";
1485 // class name with a boolean
1486 e.toggleClass( function( i, val, state ) {
1487 assert.equal( old, val, "Make sure the incoming value is correct." );
1488 assert.equal( state, false, "Make sure that the state is passed in." );
1491 assert.ok( !e.is( ".test" ), "Assert class not present" );
1493 old = e.attr( "class" ) || "";
1495 e.toggleClass( function( i, val, state ) {
1496 assert.equal( old, val, "Make sure the incoming value is correct." );
1497 assert.equal( state, true, "Make sure that the state is passed in." );
1500 assert.ok( e.is( ".test" ), "Assert class present" );
1502 old = e.attr( "class" );
1504 e.toggleClass( function( i, val, state ) {
1505 assert.equal( old, val, "Make sure the incoming value is correct." );
1506 assert.equal( state, false, "Make sure that the state is passed in." );
1509 assert.ok( !e.is( ".test" ), "Assert class not present" );
1512 QUnit.test( "addClass, removeClass, hasClass", function( assert ) {
1513 assert.expect( 17 );
1515 var jq = jQuery( "<p>Hi</p>" ), x = jq[ 0 ];
1517 jq.addClass( "hi" );
1518 assert.equal( x.className, "hi", "Check single added class" );
1520 jq.addClass( "foo bar" );
1521 assert.equal( x.className, "hi foo bar", "Check more added classes" );
1524 assert.equal( x.className, "", "Remove all classes" );
1526 jq.addClass( "hi foo bar" );
1527 jq.removeClass( "foo" );
1528 assert.equal( x.className, "hi bar", "Check removal of one class" );
1530 assert.ok( jq.hasClass( "hi" ), "Check has1" );
1531 assert.ok( jq.hasClass( "bar" ), "Check has2" );
1533 jq = jQuery( "<p class='class1\nclass2\tcla.ss3\n\rclass4'></p>" );
1535 assert.ok( jq.hasClass( "class1" ), "Check hasClass with line feed" );
1536 assert.ok( jq.is( ".class1" ), "Check is with line feed" );
1537 assert.ok( jq.hasClass( "class2" ), "Check hasClass with tab" );
1538 assert.ok( jq.is( ".class2" ), "Check is with tab" );
1539 assert.ok( jq.hasClass( "cla.ss3" ), "Check hasClass with dot" );
1540 assert.ok( jq.hasClass( "class4" ), "Check hasClass with carriage return" );
1541 assert.ok( jq.is( ".class4" ), "Check is with carriage return" );
1543 jq.removeClass( "class2" );
1544 assert.ok( jq.hasClass( "class2" ) === false, "Check the class has been properly removed" );
1545 jq.removeClass( "cla" );
1546 assert.ok( jq.hasClass( "cla.ss3" ), "Check the dotted class has not been removed" );
1547 jq.removeClass( "cla.ss3" );
1548 assert.ok( jq.hasClass( "cla.ss3" ) === false, "Check the dotted class has been removed" );
1549 jq.removeClass( "class4" );
1550 assert.ok( jq.hasClass( "class4" ) === false, "Check the class has been properly removed" );
1553 QUnit.test( "addClass, removeClass, hasClass on many elements", function( assert ) {
1554 assert.expect( 19 );
1556 var elem = jQuery( "<p>p0</p><p>p1</p><p>p2</p>" );
1558 elem.addClass( "hi" );
1559 assert.equal( elem[ 0 ].className, "hi", "Check single added class" );
1560 assert.equal( elem[ 1 ].className, "hi", "Check single added class" );
1561 assert.equal( elem[ 2 ].className, "hi", "Check single added class" );
1563 elem.addClass( "foo bar" );
1564 assert.equal( elem[ 0 ].className, "hi foo bar", "Check more added classes" );
1565 assert.equal( elem[ 1 ].className, "hi foo bar", "Check more added classes" );
1566 assert.equal( elem[ 2 ].className, "hi foo bar", "Check more added classes" );
1569 assert.equal( elem[ 0 ].className, "", "Remove all classes" );
1570 assert.equal( elem[ 1 ].className, "", "Remove all classes" );
1571 assert.equal( elem[ 2 ].className, "", "Remove all classes" );
1573 elem.addClass( "hi foo bar" );
1574 elem.removeClass( "foo" );
1575 assert.equal( elem[ 0 ].className, "hi bar", "Check removal of one class" );
1576 assert.equal( elem[ 1 ].className, "hi bar", "Check removal of one class" );
1577 assert.equal( elem[ 2 ].className, "hi bar", "Check removal of one class" );
1579 assert.ok( elem.hasClass( "hi" ), "Check has1" );
1580 assert.ok( elem.hasClass( "bar" ), "Check has2" );
1582 assert.ok( jQuery( "<p class='hi'>p0</p><p>p1</p><p>p2</p>" ).hasClass( "hi" ),
1583 "Did find a class in the first element" );
1584 assert.ok( jQuery( "<p>p0</p><p class='hi'>p1</p><p>p2</p>" ).hasClass( "hi" ),
1585 "Did find a class in the second element" );
1586 assert.ok( jQuery( "<p>p0</p><p>p1</p><p class='hi'>p2</p>" ).hasClass( "hi" ),
1587 "Did find a class in the last element" );
1589 assert.ok( jQuery( "<p class='hi'>p0</p><p class='hi'>p1</p><p class='hi'>p2</p>" ).hasClass( "hi" ),
1590 "Did find a class when present in all elements" );
1592 assert.ok( !jQuery( "<p class='hi0'>p0</p><p class='hi1'>p1</p><p class='hi2'>p2</p>" ).hasClass( "hi" ),
1593 "Did not find a class when not present" );
1596 QUnit.test( "addClass, removeClass, hasClass on many elements - Array", function( assert ) {
1597 assert.expect( 16 );
1599 var elem = jQuery( "<p>p0</p><p>p1</p><p>p2</p>" );
1601 elem.addClass( [ "hi" ] );
1602 assert.equal( elem[ 0 ].className, "hi", "Check single added class" );
1603 assert.equal( elem[ 1 ].className, "hi", "Check single added class" );
1604 assert.equal( elem[ 2 ].className, "hi", "Check single added class" );
1606 elem.addClass( [ "foo", "bar" ] );
1607 assert.equal( elem[ 0 ].className, "hi foo bar", "Check more added classes" );
1608 assert.equal( elem[ 1 ].className, "hi foo bar", "Check more added classes" );
1609 assert.equal( elem[ 2 ].className, "hi foo bar", "Check more added classes" );
1612 assert.equal( elem[ 0 ].className, "", "Remove all classes" );
1613 assert.equal( elem[ 1 ].className, "", "Remove all classes" );
1614 assert.equal( elem[ 2 ].className, "", "Remove all classes" );
1616 elem.addClass( [ "hi", "foo", "bar", "baz" ] );
1617 elem.removeClass( [ "foo" ] );
1618 assert.equal( elem[ 0 ].className, "hi bar baz", "Check removal of one class" );
1619 assert.equal( elem[ 1 ].className, "hi bar baz", "Check removal of one class" );
1620 assert.equal( elem[ 2 ].className, "hi bar baz", "Check removal of one class" );
1622 elem.removeClass( [ "bar baz" ] );
1623 assert.equal( elem[ 0 ].className, "hi", "Check removal of two classes" );
1624 assert.equal( elem[ 1 ].className, "hi", "Check removal of two classes" );
1625 assert.equal( elem[ 2 ].className, "hi", "Check removal of two classes" );
1627 assert.ok( elem.hasClass( "hi" ), "Check has1" );
1630 QUnit.test( "addClass, removeClass, hasClass on elements with classes with non-HTML whitespace (gh-3072, gh-3003)", function( assert ) {
1633 var $elem = jQuery( "<div class=' test'></div>" );
1635 function testMatches() {
1636 assert.ok( $elem.is( ".\\A0 test" ), "Element matches with collapsed space" );
1637 assert.ok( $elem.is( ".\\A0test" ), "Element matches with non-breaking space" );
1638 assert.ok( $elem.hasClass( "\xA0test" ), "Element has class with non-breaking space" );
1642 $elem.addClass( "foo" );
1644 $elem.removeClass( "foo" );
1649 var rnothtmlwhite = /[^\x20\t\r\n\f]+/g;
1651 function expectClasses( assert, elem, classes ) {
1652 var actualClassesSorted = ( elem.attr( "class" ).match( rnothtmlwhite ) || [] )
1653 .sort().join( " " );
1654 var classesSorted = classes.slice()
1655 .sort().join( " " );
1656 assert.equal( actualClassesSorted, classesSorted, "Expected classes present" );
1659 QUnit.test( "addClass on arrays with falsy elements (gh-4998)", function( assert ) {
1662 var elem = jQuery( "<div class='a'></div>" );
1664 elem.addClass( [ "b", "", "c" ] );
1665 expectClasses( assert, elem, [ "a", "b", "c" ] );
1666 elem.addClass( [ "", "d" ] );
1667 expectClasses( assert, elem, [ "a", "b", "c", "d" ] );
1668 elem.addClass( [ "e", "" ] );
1669 expectClasses( assert, elem, [ "a", "b", "c", "d", "e" ] );
1672 QUnit.test( "removeClass on arrays with falsy elements (gh-4998)", function( assert ) {
1675 var elem = jQuery( "<div class='a b c d e'></div>" );
1677 elem.removeClass( [ "e", "" ] );
1678 expectClasses( assert, elem, [ "a", "b", "c", "d" ] );
1679 elem.removeClass( [ "", "d" ] );
1680 expectClasses( assert, elem, [ "a", "b", "c" ] );
1681 elem.removeClass( [ "b", "", "c" ] );
1682 expectClasses( assert, elem, [ "a" ] );
1686 QUnit.test( "contents().hasClass() returns correct values", function( assert ) {
1689 var $div = jQuery( "<div><span class='foo'></span><!-- comment -->text</div>" ),
1690 $contents = $div.contents();
1692 assert.ok( $contents.hasClass( "foo" ), "Found 'foo' in $contents" );
1693 assert.ok( !$contents.hasClass( "undefined" ), "Did not find 'undefined' in $contents (correctly)" );
1696 QUnit.test( "hasClass correctly interprets non-space separators (trac-13835)", function( assert ) {
1702 "line-feed": " ",
1703 "form-feed": "",
1704 "carriage-return": " "
1706 classes = jQuery.map( map, function( separator, label ) {
1707 return " " + separator + label + separator + " ";
1709 $div = jQuery( "<div class='" + classes + "'></div>" );
1711 jQuery.each( map, function( label ) {
1712 assert.ok( $div.hasClass( label ), label.replace( "-", " " ) );
1716 QUnit.test( "coords returns correct values in IE6/IE7, see trac-10828", function( assert ) {
1720 map = jQuery( "<map></map>" );
1722 area = map.html( "<area shape='rect' coords='0,0,0,0' href='#' alt='a'></area>" ).find( "area" );
1723 assert.equal( area.attr( "coords" ), "0,0,0,0", "did not retrieve coords correctly" );
1726 QUnit.test( "should not throw at $(option).val() (trac-14686)", function( assert ) {
1730 jQuery( "<option></option>" ).val();
1737 QUnit.test( "option value not trimmed when setting via parent select", function( assert ) {
1739 assert.equal( jQuery( "<select><option> 2</option></select>" ).val( "2" ).val(), "2" );
1742 QUnit.test( "Insignificant white space returned for $(option).val() (trac-14858, gh-2978)", function( assert ) {
1743 assert.expect( 16 );
1745 var val = jQuery( "<option></option>" ).val();
1746 assert.equal( val.length, 0, "Empty option should have no value" );
1748 jQuery.each( [ " ", "\n", "\t", "\f", "\r" ], function( i, character ) {
1749 var val = jQuery( "<option>" + character + "</option>" ).val();
1750 assert.equal( val.length, 0, "insignificant white-space returned for value" );
1752 val = jQuery( "<option>" + character + "test" + character + "</option>" ).val();
1753 assert.equal( val.length, 4, "insignificant white-space returned for value" );
1755 val = jQuery( "<option>te" + character + "st</option>" ).val();
1756 assert.equal( val, "te st", "Whitespace is collapsed in values" );
1760 QUnit.test( "SVG class manipulation (gh-2199)", function( assert ) {
1761 assert.expect( 12 );
1763 function createSVGElement( nodeName ) {
1764 return document.createElementNS( "http://www.w3.org/2000/svg", nodeName );
1772 var elem = jQuery( createSVGElement( this ) );
1774 elem.addClass( "awesome" );
1775 assert.ok( elem.hasClass( "awesome" ), "SVG element (" + this + ") has added class" );
1777 elem.removeClass( "awesome" );
1778 assert.ok( !elem.hasClass( "awesome" ), "SVG element (" + this + ") removes the class" );
1780 elem.toggleClass( "awesome" );
1781 assert.ok( elem.hasClass( "awesome" ), "SVG element (" + this + ") toggles the class on" );
1783 elem.toggleClass( "awesome" );
1784 assert.ok( !elem.hasClass( "awesome" ), "SVG element (" + this + ") toggles the class off" );
1788 QUnit.test( "non-lowercase boolean attribute getters should not crash", function( assert ) {
1791 var elem = jQuery( "<input checked required autofocus type='checkbox'>" );
1794 "Checked", "requiRed", "AUTOFOCUS"
1795 ].forEach( function( inconsistentlyCased ) {
1797 assert.strictEqual( elem.attr( inconsistentlyCased ), "",
1798 "The '" + this + "' attribute getter should return an empty string" );
1800 assert.ok( false, "The '" + this + "' attribute getter threw" );
1806 QUnit.test( "false setter removes non-ARIA attrs (gh-5388)", function( assert ) {
1807 assert.expect( 24 );
1809 var elem = jQuery( "<input" +
1810 " checked required autofocus" +
1811 " type='checkbox'" +
1812 " title='Example title'" +
1813 " class='test-class'" +
1814 " style='color: brown'" +
1815 " aria-hidden='true'" +
1816 " aria-checked='true'" +
1817 " aria-label='Example ARIA label'" +
1818 " data-prop='Example data value'" +
1819 " data-title='Example data title'" +
1820 " data-true='true'" +
1823 function testFalseSetter( attributes, options ) {
1824 var removal = options.removal;
1826 attributes.forEach( function( attrName ) {
1827 assert.ok( elem.attr( attrName ) != null,
1828 "Attribute '" + attrName + "': initial defined value" );
1829 elem.attr( attrName, false );
1832 assert.strictEqual( elem.attr( attrName ), undefined,
1833 "Attribute '" + attrName + "' removed" );
1835 assert.strictEqual( elem.attr( attrName ), "false",
1836 "Attribute '" + attrName + "' set to 'false'" );
1841 // Boolean attributes
1843 [ "checked", "required", "autofocus" ],
1847 // Regular attributes
1849 [ "title", "class", "style" ],
1853 // `aria-*` attributes
1855 [ "aria-hidden", "aria-checked", "aria-label" ],
1859 // `data-*` attributes
1861 [ "data-prop", "data-title", "data-true" ],
1866 // Test trustedTypes support in browsers where they're supported (currently Chrome 83+).
1867 // Browsers with no TrustedScriptURL support still run tests on object wrappers with
1868 // a proper `toString` function.
1870 "Basic TrustedScriptURL support (gh-4948)",
1871 "mock.php?action=trustedTypesAttributes",
1872 function( assert, jQuery, window, document, test ) {
1873 var done = assert.async();
1877 test.forEach( function( result ) {
1878 assert.deepEqual( result.actual, result.expected, result.message );
1881 supportjQuery.get( baseURL + "mock.php?action=cspClean" ).then( done );