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 [#7472]" );
87 // Bug #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 (#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 (#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 #6743)" );
173 QUnit.test( "attr(String) on cloned elements, #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 #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", true );
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", true );
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", true ).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( {
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", "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 #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 #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>", {
459 // Use uppercase here to ensure the type
460 // attrHook is still used
462 } ).appendTo( "#testForm" );
463 assert.equal( $radio.val(), "sup", "Value is not reset when type is set after value on a radio" );
465 // Setting attributes on svg elements (bug #3116)
467 "<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'>" +
469 "<circle cx='200' cy='200' r='150' />" +
471 ).appendTo( "body" );
472 assert.equal( $svg.attr( "cx", 100 ).attr( "cx" ), "100", "Set attribute on svg element" );
475 // undefined values are chainable
476 jQuery( "#name" ).attr( "maxlength", "5" ).removeAttr( "nonexisting" );
477 assert.equal( typeof jQuery( "#name" ).attr( "maxlength", undefined ), "object", ".attr('attribute', undefined) is chainable (#5571)" );
478 assert.equal( jQuery( "#name" ).attr( "maxlength", undefined ).attr( "maxlength" ), "5", ".attr('attribute', undefined) does not change value (#5571)" );
479 assert.equal( jQuery( "#name" ).attr( "nonexisting", undefined ).attr( "nonexisting" ), undefined, ".attr('attribute', undefined) does not create attribute (#5571)" );
482 QUnit.test( "attr(non-ASCII)", function( assert ) {
485 var $div = jQuery( "<div Ω='omega' aØc='alpha'></div>" ).appendTo( "#qunit-fixture" );
487 assert.equal( $div.attr( "Ω" ), "omega", ".attr() exclusively lowercases characters in the range A-Z (gh-2730)" );
488 assert.equal( $div.attr( "AØC" ), "alpha", ".attr() exclusively lowercases characters in the range A-Z (gh-2730)" );
491 QUnit.test( "attr(String, Object) - Loaded via XML document", function( assert ) {
493 var xml = createDashboardXML(),
495 jQuery( "tab", xml ).each( function() {
496 titles.push( jQuery( this ).attr( "title" ) );
498 assert.equal( titles[ 0 ], "Location", "attr() in XML context: Check first title" );
499 assert.equal( titles[ 1 ], "Users", "attr() in XML context: Check second title" );
502 QUnit.test( "attr(String, Object) - Loaded via XML fragment", function( assert ) {
504 var frag = createXMLFragment(),
505 $frag = jQuery( frag );
507 $frag.attr( "test", "some value" );
508 assert.equal( $frag.attr( "test" ), "some value", "set attribute" );
509 $frag.attr( "test", null );
510 assert.equal( $frag.attr( "test" ), undefined, "remove attribute" );
513 QUnit.test( "attr('tabindex')", function( assert ) {
516 // elements not natively tabbable
517 assert.equal( jQuery( "#listWithTabIndex" ).attr( "tabindex" ), "5", "not natively tabbable, with tabindex set to 0" );
518 assert.equal( jQuery( "#divWithNoTabIndex" ).attr( "tabindex" ), undefined, "not natively tabbable, no tabindex set" );
521 assert.equal( jQuery( "#linkWithNoTabIndex" ).attr( "tabindex" ), undefined, "anchor with href, no tabindex set" );
522 assert.equal( jQuery( "#linkWithTabIndex" ).attr( "tabindex" ), "2", "anchor with href, tabindex set to 2" );
523 assert.equal( jQuery( "#linkWithNegativeTabIndex" ).attr( "tabindex" ), "-1", "anchor with href, tabindex set to -1" );
525 // anchor without href
526 assert.equal( jQuery( "#linkWithNoHrefWithNoTabIndex" ).attr( "tabindex" ), undefined, "anchor without href, no tabindex set" );
527 assert.equal( jQuery( "#linkWithNoHrefWithTabIndex" ).attr( "tabindex" ), "1", "anchor without href, tabindex set to 2" );
528 assert.equal( jQuery( "#linkWithNoHrefWithNegativeTabIndex" ).attr( "tabindex" ), "-1", "anchor without href, no tabindex set" );
531 QUnit.test( "attr('tabindex', value)", function( assert ) {
534 var element = jQuery( "#divWithNoTabIndex" );
535 assert.equal( element.attr( "tabindex" ), undefined, "start with no tabindex" );
537 // set a positive string
538 element.attr( "tabindex", "1" );
539 assert.equal( element.attr( "tabindex" ), "1", "set tabindex to 1 (string)" );
542 element.attr( "tabindex", "0" );
543 assert.equal( element.attr( "tabindex" ), "0", "set tabindex to 0 (string)" );
545 // set a negative string
546 element.attr( "tabindex", "-1" );
547 assert.equal( element.attr( "tabindex" ), "-1", "set tabindex to -1 (string)" );
549 // set a positive number
550 element.attr( "tabindex", 1 );
551 assert.equal( element.attr( "tabindex" ), "1", "set tabindex to 1 (number)" );
554 element.attr( "tabindex", 0 );
555 assert.equal( element.attr( "tabindex" ), "0", "set tabindex to 0 (number)" );
557 // set a negative number
558 element.attr( "tabindex", -1 );
559 assert.equal( element.attr( "tabindex" ), "-1", "set tabindex to -1 (number)" );
561 element = jQuery( "#linkWithTabIndex" );
562 assert.equal( element.attr( "tabindex" ), "2", "start with tabindex 2" );
564 element.attr( "tabindex", -1 );
565 assert.equal( element.attr( "tabindex" ), "-1", "set negative tabindex" );
568 QUnit.test( "removeAttr(String)", function( assert ) {
572 assert.equal( jQuery( "<div class='hello'></div>" ).removeAttr( "class" ).attr( "class" ), undefined, "remove class" );
573 assert.equal( jQuery( "#form" ).removeAttr( "id" ).attr( "id" ), undefined, "Remove id" );
574 assert.equal( jQuery( "#foo" ).attr( "style", "position:absolute;" ).removeAttr( "style" ).attr( "style" ), undefined, "Check removing style attribute" );
575 assert.equal( jQuery( "#form" ).attr( "style", "position:absolute;" ).removeAttr( "style" ).attr( "style" ), undefined, "Check removing style attribute on a form" );
576 assert.equal( jQuery( "<div style='position: absolute'></div>" ).appendTo( "#foo" ).removeAttr( "style" ).prop( "style" ).cssText, "", "Check removing style attribute (#9699 Webkit)" );
577 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" );
579 jQuery( "#check1" ).removeAttr( "checked" ).prop( "checked", true ).removeAttr( "checked" );
580 assert.equal( document.getElementById( "check1" ).checked, true, "removeAttr should not set checked to false, since the checked attribute does NOT mirror the checked property" );
581 jQuery( "#text1" ).prop( "readOnly", true ).removeAttr( "readonly" );
582 assert.equal( document.getElementById( "text1" ).readOnly, false, "removeAttr sets boolean properties to false" );
584 jQuery( "#option2c" ).removeAttr( "selected" );
585 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 (#10870)" );
588 $first = jQuery( "#first" ).attr( "contenteditable", "true" ).removeAttr( "contenteditable" );
589 assert.equal( $first.attr( "contenteditable" ), undefined, "Remove the contenteditable attribute" );
591 assert.ok( false, "Removing contenteditable threw an error (#10429)" );
594 $first = jQuery( "<div Case='mixed'></div>" );
595 assert.equal( $first.attr( "Case" ), "mixed", "case of attribute doesn't matter" );
596 $first.removeAttr( "Case" );
597 assert.equal( $first.attr( "Case" ), undefined, "mixed-case attribute was removed" );
600 QUnit.test( "removeAttr(String) in XML", function( assert ) {
602 var xml = createDashboardXML(),
603 iwt = jQuery( "infowindowtab", xml );
605 assert.equal( iwt.attr( "normal" ), "ab", "Check initial value" );
606 iwt.removeAttr( "Normal" );
607 assert.equal( iwt.attr( "normal" ), "ab", "Should still be there" );
608 iwt.removeAttr( "normal" );
609 assert.equal( iwt.attr( "normal" ), undefined, "Removed" );
611 assert.equal( iwt.attr( "mixedCase" ), "yes", "Check initial value" );
612 assert.equal( iwt.attr( "mixedcase" ), undefined, "toLowerCase not work good" );
613 iwt.removeAttr( "mixedcase" );
614 assert.equal( iwt.attr( "mixedCase" ), "yes", "Should still be there" );
615 iwt.removeAttr( "mixedCase" );
616 assert.equal( iwt.attr( "mixedCase" ), undefined, "Removed" );
619 QUnit.test( "removeAttr(Multi String, variable space width)", function( assert ) {
622 var div = jQuery( "<div id='a' alt='b' title='c' rel='d'></div>" ),
630 jQuery.each( tests, function( key, val ) {
631 assert.equal( div.attr( key ), val, "Attribute `" + key + "` exists, and has a value of `" + val + "`" );
634 div.removeAttr( "id alt title rel " );
636 jQuery.each( tests, function( key ) {
637 assert.equal( div.attr( key ), undefined, "Attribute `" + key + "` was removed" );
641 QUnit.test( "removeAttr(Multi String, non-HTML whitespace is valid in attribute names (gh-3003)", function( assert ) {
644 var div = jQuery( "<div id='a' data-\xA0='b' title='c' rel='d'></div>" );
652 jQuery.each( tests, function( key, val ) {
653 assert.equal( div.attr( key ), val, "Attribute \"" + key + "\" exists, and has a value of \"" + val + "\"" );
656 div.removeAttr( "id data-\xA0 title rel " );
658 jQuery.each( tests, function( key ) {
659 assert.equal( div.attr( key ), undefined, "Attribute \"" + key + "\" was removed" );
663 QUnit.test( "prop(String, Object)", function( assert ) {
667 assert.equal( jQuery( "#text1" ).prop( "value" ), "Test", "Check for value attribute" );
668 assert.equal( jQuery( "#text1" ).prop( "value", "Test2" ).prop( "defaultValue" ), "Test", "Check for defaultValue attribute" );
669 assert.equal( jQuery( "#select2" ).prop( "selectedIndex" ), 3, "Check for selectedIndex attribute" );
670 assert.equal( jQuery( "#foo" ).prop( "nodeName" ).toUpperCase(), "DIV", "Check for nodeName attribute" );
671 assert.equal( jQuery( "#foo" ).prop( "tagName" ).toUpperCase(), "DIV", "Check for tagName attribute" );
672 assert.equal( jQuery( "<option></option>" ).prop( "selected" ), false, "Check selected attribute on disconnected element." );
674 assert.equal( jQuery( "#listWithTabIndex" ).prop( "tabindex" ), 5, "Check retrieving tabindex" );
675 jQuery( "#text1" ).prop( "readonly", true );
676 assert.equal( document.getElementById( "text1" ).readOnly, true, "Check setting readOnly property with 'readonly'" );
677 assert.equal( jQuery( "#label-for" ).prop( "for" ), "action", "Check retrieving htmlFor" );
678 jQuery( "#text1" ).prop( "class", "test" );
679 assert.equal( document.getElementById( "text1" ).className, "test", "Check setting className with 'class'" );
680 assert.equal( jQuery( "#text1" ).prop( "maxlength" ), 30, "Check retrieving maxLength" );
681 jQuery( "#table" ).prop( "cellspacing", 1 );
682 assert.equal( jQuery( "#table" ).prop( "cellSpacing" ), "1", "Check setting and retrieving cellSpacing" );
683 jQuery( "#table" ).prop( "cellpadding", 1 );
684 assert.equal( jQuery( "#table" ).prop( "cellPadding" ), "1", "Check setting and retrieving cellPadding" );
685 jQuery( "#table" ).prop( "rowspan", 1 );
686 assert.equal( jQuery( "#table" ).prop( "rowSpan" ), 1, "Check setting and retrieving rowSpan" );
687 jQuery( "#table" ).prop( "colspan", 1 );
688 assert.equal( jQuery( "#table" ).prop( "colSpan" ), 1, "Check setting and retrieving colSpan" );
689 jQuery( "#table" ).prop( "usemap", 1 );
690 assert.equal( jQuery( "#table" ).prop( "useMap" ), 1, "Check setting and retrieving useMap" );
691 jQuery( "#table" ).prop( "frameborder", 1 );
692 assert.equal( jQuery( "#table" ).prop( "frameBorder" ), 1, "Check setting and retrieving frameBorder" );
695 QUnit.test( "prop(String, Object) on null/undefined", function( assert ) {
699 var select, optgroup, option, attributeNode, commentNode, textNode, obj, $form,
700 body = document.body,
701 $body = jQuery( body );
703 assert.ok( $body.prop( "nextSibling" ) === null, "Make sure a null expando returns null" );
704 body[ "foo" ] = "bar";
705 assert.equal( $body.prop( "foo" ), "bar", "Make sure the expando is preferred over the dom attribute" );
706 body[ "foo" ] = undefined;
707 assert.ok( $body.prop( "foo" ) === undefined, "Make sure the expando is preferred over the dom attribute, even if undefined" );
709 select = document.createElement( "select" );
710 optgroup = document.createElement( "optgroup" );
711 option = document.createElement( "option" );
713 optgroup.appendChild( option );
714 select.appendChild( optgroup );
716 assert.equal( jQuery( option ).prop( "selected" ), true, "Make sure that a single option is selected, even when in an optgroup." );
717 assert.equal( jQuery( document ).prop( "nodeName" ), "#document", "prop works correctly on document nodes (bug #7451)." );
719 attributeNode = document.createAttribute( "irrelevant" );
720 commentNode = document.createComment( "some comment" );
721 textNode = document.createTextNode( "some text" );
723 jQuery.each( [ document, attributeNode, commentNode, textNode, obj, "#firstp" ], function( i, ele ) {
724 assert.strictEqual( jQuery( ele ).prop( "nonexisting" ), undefined, "prop works correctly for non existing attributes (bug #7500)." );
728 jQuery.each( [ document, obj ], function( i, ele ) {
729 var $ele = jQuery( ele );
730 $ele.prop( "nonexisting", "foo" );
731 assert.equal( $ele.prop( "nonexisting" ), "foo", "prop(name, value) works correctly for non existing attributes (bug #7500)." );
733 jQuery( document ).removeProp( "nonexisting" );
735 $form = jQuery( "#form" ).prop( "enctype", "multipart/form-data" );
736 assert.equal( $form.prop( "enctype" ), "multipart/form-data", "Set the enctype of a form (encoding in IE6/7 #6743)" );
739 QUnit.test( "prop('tabindex')", function( assert ) {
742 // inputs without tabIndex attribute
743 assert.equal( jQuery( "#inputWithoutTabIndex" ).prop( "tabindex" ), 0, "input without tabindex" );
744 assert.equal( jQuery( "#buttonWithoutTabIndex" ).prop( "tabindex" ), 0, "button without tabindex" );
745 assert.equal( jQuery( "#textareaWithoutTabIndex" ).prop( "tabindex" ), 0, "textarea without tabindex" );
747 // elements not natively tabbable
748 assert.equal( jQuery( "#listWithTabIndex" ).prop( "tabindex" ), 5, "not natively tabbable, with tabindex set to 0" );
749 assert.equal( jQuery( "#divWithNoTabIndex" ).prop( "tabindex" ), -1, "not natively tabbable, no tabindex set" );
752 assert.equal( jQuery( "#linkWithNoTabIndex" ).prop( "tabindex" ), 0, "anchor with href, no tabindex set" );
753 assert.equal( jQuery( "#linkWithTabIndex" ).prop( "tabindex" ), 2, "anchor with href, tabindex set to 2" );
754 assert.equal( jQuery( "#linkWithNegativeTabIndex" ).prop( "tabindex" ), -1, "anchor with href, tabindex set to -1" );
756 // anchor without href
757 assert.equal( jQuery( "#linkWithNoHrefWithNoTabIndex" ).prop( "tabindex" ), -1, "anchor without href, no tabindex set" );
758 assert.equal( jQuery( "#linkWithNoHrefWithTabIndex" ).prop( "tabindex" ), 1, "anchor without href, tabindex set to 2" );
759 assert.equal( jQuery( "#linkWithNoHrefWithNegativeTabIndex" ).prop( "tabindex" ), -1, "anchor without href, no tabindex set" );
762 QUnit.test( "image.prop( 'tabIndex' )", function( assert ) {
764 var image = jQuery( "<img src='" + baseURL + "1x1.jpg' />" )
765 .appendTo( "#qunit-fixture" );
766 assert.equal( image.prop( "tabIndex" ), -1, "tabIndex on image" );
769 QUnit.test( "prop('tabindex', value)", function( assert ) {
773 element = jQuery( "#divWithNoTabIndex" );
775 assert.equal( element.prop( "tabindex" ), -1, "start with no tabindex" );
777 // set a positive string
778 element.prop( "tabindex", "1" );
779 assert.equal( element.prop( "tabindex" ), 1, "set tabindex to 1 (string)" );
782 element.prop( "tabindex", "0" );
783 assert.equal( element.prop( "tabindex" ), 0, "set tabindex to 0 (string)" );
785 // set a negative string
786 element.prop( "tabindex", "-1" );
787 assert.equal( element.prop( "tabindex" ), -1, "set tabindex to -1 (string)" );
789 // set a positive number
790 element.prop( "tabindex", 1 );
791 assert.equal( element.prop( "tabindex" ), 1, "set tabindex to 1 (number)" );
794 element.prop( "tabindex", 0 );
795 assert.equal( element.prop( "tabindex" ), 0, "set tabindex to 0 (number)" );
797 // set a negative number
798 element.prop( "tabindex", -1 );
799 assert.equal( element.prop( "tabindex" ), -1, "set tabindex to -1 (number)" );
801 element = jQuery( "#linkWithTabIndex" );
802 assert.equal( element.prop( "tabindex" ), 2, "start with tabindex 2" );
804 element.prop( "tabindex", -1 );
805 assert.equal( element.prop( "tabindex" ), -1, "set negative tabindex" );
807 clone = element.clone();
808 clone.prop( "tabindex", 1 );
809 assert.equal( clone[ 0 ].getAttribute( "tabindex" ), "1", "set tabindex on cloned element" );
812 QUnit.test( "option.prop('selected', true) affects select.selectedIndex (gh-2732)", function( assert ) {
815 function addOptions( $elem ) {
817 jQuery( "<option></option>" ).val( "a" ).text( "One" ),
818 jQuery( "<option></option>" ).val( "b" ).text( "Two" ),
819 jQuery( "<option></option>" ).val( "c" ).text( "Three" )
821 .find( "[value=a]" ).prop( "selected", true ).end()
822 .find( "[value=c]" ).prop( "selected", true ).end();
826 $select = jQuery( "<select></select>" );
828 // Check select with options
829 addOptions( $select ).appendTo( "#qunit-fixture" );
830 $select.find( "[value=b]" ).prop( "selected", true );
831 assert.equal( $select[ 0 ].selectedIndex, 1, "Setting option selected affects selectedIndex" );
835 // Check select with optgroup
836 $optgroup = jQuery( "<optgroup></optgroup>" );
837 addOptions( $optgroup ).appendTo( $select );
838 $select.find( "[value=b]" ).prop( "selected", true );
840 assert.equal( $select[ 0 ].selectedIndex, 1, "Setting option in optgroup selected affects selectedIndex" );
843 QUnit.test( "removeProp(String)", function( assert ) {
845 var attributeNode = document.createAttribute( "irrelevant" ),
846 commentNode = document.createComment( "some comment" ),
847 textNode = document.createTextNode( "some text" ),
851 jQuery( "#firstp" ).prop( "nonexisting", "foo" ).removeProp( "nonexisting" )[ 0 ][ "nonexisting" ],
853 "removeprop works correctly on DOM element nodes"
856 jQuery.each( [ document, obj ], function( i, ele ) {
857 var $ele = jQuery( ele );
858 $ele.prop( "nonexisting", "foo" ).removeProp( "nonexisting" );
859 assert.strictEqual( ele[ "nonexisting" ], undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." );
861 jQuery.each( [ commentNode, textNode, attributeNode ], function( i, ele ) {
862 var $ele = jQuery( ele );
863 $ele.prop( "nonexisting", "foo" ).removeProp( "nonexisting" );
864 assert.strictEqual( ele[ "nonexisting" ], undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." );
868 QUnit.test( "val() after modification", function( assert ) {
872 document.getElementById( "text1" ).value = "bla";
873 assert.equal( jQuery( "#text1" ).val(), "bla", "Check for modified value of input element" );
876 QUnit.test( "val()", function( assert ) {
878 assert.expect( 20 + ( jQuery.fn.serialize ? 6 : 0 ) );
881 assert.equal( jQuery( "#text1" ).val(), "Test", "Check for value of input element" );
883 // ticket #1714 this caused a JS error in IE
884 assert.equal( jQuery( "#first" ).val(), "", "Check a paragraph element to see if it has a value" );
885 assert.ok( jQuery( [] ).val() === undefined, "Check an empty jQuery object will return undefined from val" );
887 assert.equal( jQuery( "#select2" ).val(), "3", "Call val() on a single='single' select" );
889 assert.deepEqual( jQuery( "#select3" ).val(), [ "1", "2" ], "Call val() on a multiple='multiple' select" );
891 assert.equal( jQuery( "#option3c" ).val(), "2", "Call val() on a option element with value" );
893 assert.equal( jQuery( "#option3a" ).val(), "", "Call val() on a option element with empty value" );
895 assert.equal( jQuery( "#option3e" ).val(), "no value", "Call val() on a option element with no value attribute" );
897 assert.equal( jQuery( "#option3a" ).val(), "", "Call val() on a option element with no value attribute" );
899 jQuery( "#select3" ).val( "" );
900 assert.deepEqual( jQuery( "#select3" ).val(), [ "" ], "Call val() on a multiple='multiple' select" );
902 assert.deepEqual( jQuery( "#select4" ).val(), [], "Call val() on multiple='multiple' select with all disabled options" );
904 jQuery( "#select4 optgroup" ).add( "#select4 > [disabled]" ).attr( "disabled", false );
905 assert.deepEqual( jQuery( "#select4" ).val(), [ "2", "3" ], "Call val() on multiple='multiple' select with some disabled options" );
907 jQuery( "#select4" ).attr( "disabled", true );
908 assert.deepEqual( jQuery( "#select4" ).val(), [ "2", "3" ], "Call val() on disabled multiple='multiple' select" );
910 assert.equal( jQuery( "#select5" ).val(), "3", "Check value on ambiguous select." );
912 jQuery( "#select5" ).val( 1 );
913 assert.equal( jQuery( "#select5" ).val(), "1", "Check value on ambiguous select." );
915 jQuery( "#select5" ).val( 3 );
916 assert.equal( jQuery( "#select5" ).val(), "3", "Check value on ambiguous select." );
919 jQuery( "<select name='select12584' id='select12584'><option value='1' disabled='disabled'>1</option></select>" ).val(),
921 "Select-one with only option disabled (#12584)"
924 if ( jQuery.fn.serialize ) {
925 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" );
927 assert.deepEqual( checks.serialize(), "", "Get unchecked values." );
929 assert.equal( checks.eq( 3 ).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
931 checks.val( [ "2" ] );
932 assert.deepEqual( checks.serialize(), "test=2", "Get a single checked value." );
934 checks.val( [ "1", "" ] );
935 assert.deepEqual( checks.serialize(), "test=1&test=", "Get multiple checked values." );
937 checks.val( [ "", "2" ] );
938 assert.deepEqual( checks.serialize(), "test=2&test=", "Get multiple checked values." );
940 checks.val( [ "1", "on" ] );
941 assert.deepEqual( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
946 $button = jQuery( "<button value='foobar'>text</button>" ).insertAfter( "#button" );
947 assert.equal( $button.val(), "foobar", "Value retrieval on a button does not return innerHTML" );
948 assert.equal( $button.val( "baz" ).html(), "text", "Setting the value does not change innerHTML" );
950 assert.equal( jQuery( "<option></option>" ).val( "test" ).attr( "value" ), "test", "Setting value sets the value attribute" );
953 QUnit.test( "val() with non-matching values on dropdown list", function( assert ) {
956 jQuery( "#select5" ).val( "" );
957 assert.equal( jQuery( "#select5" ).val(), null, "Non-matching set on select-one" );
959 var select6 = jQuery( "<select multiple id=\"select6\"><option value=\"1\">A</option><option value=\"2\">B</option></select>" ).appendTo( "#form" );
960 jQuery( select6 ).val( "nothing" );
961 assert.deepEqual( jQuery( select6 ).val(), [], "Non-matching set (single value) on select-multiple" );
963 jQuery( select6 ).val( [ "nothing1", "nothing2" ] );
964 assert.deepEqual( jQuery( select6 ).val(), [], "Non-matching set (array of values) on select-multiple" );
969 QUnit.test( "val() respects numbers without exception (Bug #9319) - progress",
974 var $progress = jQuery( "<progress max='10' value='1.5'></progress>" );
977 assert.equal( typeof $progress.val(), "number", "progress, returns a number and does not throw exception" );
978 assert.equal( $progress.val(), $progress[ 0 ].value, "progress, api matches host and does not throw exception" );
985 // IE doesn't support <meter>
986 QUnit.testUnlessIE( "val() respects numbers without exception (Bug #9319) - meter",
991 var $meter = jQuery( "<meter min='0' max='10' value='5.6'></meter>" );
994 assert.equal( typeof $meter.val(), "number", "meter, returns a number and does not throw exception" );
995 assert.equal( $meter.val(), $meter[ 0 ].value, "meter, api matches host and does not throw exception" );
1001 var testVal = function( valueObj, assert ) {
1004 jQuery( "#text1" ).val( valueObj( "test" ) );
1005 assert.equal( document.getElementById( "text1" ).value, "test", "Check for modified (via val(String)) value of input element" );
1007 jQuery( "#text1" ).val( valueObj( undefined ) );
1008 assert.equal( document.getElementById( "text1" ).value, "", "Check for modified (via val(undefined)) value of input element" );
1010 jQuery( "#text1" ).val( valueObj( 67 ) );
1011 assert.equal( document.getElementById( "text1" ).value, "67", "Check for modified (via val(Number)) value of input element" );
1013 jQuery( "#text1" ).val( valueObj( null ) );
1014 assert.equal( document.getElementById( "text1" ).value, "", "Check for modified (via val(null)) value of input element" );
1017 $select = jQuery( "<select multiple><option value='1'></option><option value='2'></option></select>" ),
1018 $select1 = jQuery( "#select1" );
1020 $select1.val( valueObj( "3" ) );
1021 assert.equal( $select1.val(), "3", "Check for modified (via val(String)) value of select element" );
1023 $select1.val( valueObj( 2 ) );
1024 assert.equal( $select1.val(), "2", "Check for modified (via val(Number)) value of select element" );
1026 $select1.append( "<option value='4'>four</option>" );
1027 $select1.val( valueObj( 4 ) );
1028 assert.equal( $select1.val(), "4", "Should be possible to set the val() to a newly created option" );
1030 // using contents will get comments regular, text, and comment nodes
1031 j = jQuery( "#nonnodes" ).contents();
1032 j.val( valueObj( "asdf" ) );
1033 assert.equal( j.val(), "asdf", "Check node,textnode,comment with val()" );
1034 j.removeAttr( "value" );
1036 $select.val( valueObj( [ "1", "2" ] ) );
1037 assert.deepEqual( $select.val(), [ "1", "2" ], "Should set array of values" );
1040 QUnit.test( "val(String/Number)", function( assert ) {
1041 testVal( bareObj, assert );
1044 QUnit.test( "val(Function)", function( assert ) {
1045 testVal( functionReturningObj, assert );
1048 QUnit.test( "val(Array of Numbers) (Bug #7123)", function( assert ) {
1050 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' />" );
1051 var elements = jQuery( "#form input[name=arrayTest]" ).val( [ 1, 2 ] );
1052 assert.ok( elements[ 0 ].checked, "First element was checked" );
1053 assert.ok( elements[ 1 ].checked, "Second element was checked" );
1054 assert.ok( !elements[ 2 ].checked, "Third element was unchecked" );
1055 assert.ok( !elements[ 3 ].checked, "Fourth element remained unchecked" );
1060 QUnit.test( "val(Function) with incoming value", function( assert ) {
1061 assert.expect( 10 );
1063 var oldVal = jQuery( "#text1" ).val();
1065 jQuery( "#text1" ).val( function( i, val ) {
1066 assert.equal( val, oldVal, "Make sure the incoming value is correct." );
1070 assert.equal( document.getElementById( "text1" ).value, "test", "Check for modified (via val(String)) value of input element" );
1072 oldVal = jQuery( "#text1" ).val();
1074 jQuery( "#text1" ).val( function( i, val ) {
1075 assert.equal( val, oldVal, "Make sure the incoming value is correct." );
1079 assert.equal( document.getElementById( "text1" ).value, "67", "Check for modified (via val(Number)) value of input element" );
1081 oldVal = jQuery( "#select1" ).val();
1083 jQuery( "#select1" ).val( function( i, val ) {
1084 assert.equal( val, oldVal, "Make sure the incoming value is correct." );
1088 assert.equal( jQuery( "#select1" ).val(), "3", "Check for modified (via val(String)) value of select element" );
1090 oldVal = jQuery( "#select1" ).val();
1092 jQuery( "#select1" ).val( function( i, val ) {
1093 assert.equal( val, oldVal, "Make sure the incoming value is correct." );
1097 assert.equal( jQuery( "#select1" ).val(), "2", "Check for modified (via val(Number)) value of select element" );
1099 jQuery( "#select1" ).append( "<option value='4'>four</option>" );
1101 oldVal = jQuery( "#select1" ).val();
1103 jQuery( "#select1" ).val( function( i, val ) {
1104 assert.equal( val, oldVal, "Make sure the incoming value is correct." );
1108 assert.equal( jQuery( "#select1" ).val(), "4", "Should be possible to set the val() to a newly created option" );
1111 // testing if a form.reset() breaks a subsequent call to a select element's .val() (in IE only)
1112 QUnit.test( "val(select) after form.reset() (Bug #2551)", function( assert ) {
1115 jQuery( "<form id='kk' name='kk'><select id='kkk'><option value='cf'>cf</option><option value='gf'>gf</option></select></form>" ).appendTo( "#qunit-fixture" );
1117 jQuery( "#kkk" ).val( "gf" );
1119 document[ "kk" ].reset();
1121 assert.equal( jQuery( "#kkk" )[ 0 ].value, "cf", "Check value of select after form reset." );
1122 assert.equal( jQuery( "#kkk" ).val(), "cf", "Check value of select after form reset." );
1124 // re-verify the multi-select is not broken (after form.reset) by our fix for single-select
1125 assert.deepEqual( jQuery( "#select3" ).val(), [ "1", "2" ], "Call val() on a multiple='multiple' select" );
1127 jQuery( "#kk" ).remove();
1130 QUnit.test( "select.val(space characters) (gh-2978)", function( assert ) {
1131 assert.expect( 37 );
1133 var $select = jQuery( "<select></select>" ).appendTo( "#qunit-fixture" ),
1149 "\\u00a0": "\u00a0",
1153 jQuery.each( spaces, function( key, obj ) {
1154 var value = obj.html || obj;
1155 html += "<option value='attr" + value + "'></option>";
1156 html += "<option value='at" + value + "tr'></option>";
1157 html += "<option value='" + value + "attr'></option>";
1159 $select.html( html );
1161 jQuery.each( spaces, function( key, obj ) {
1162 var val = obj.val || obj;
1163 $select.val( "attr" + val );
1164 assert.equal( $select.val(), "attr" + val, "Value ending with space character (" + key + ") selected (attr)" );
1166 $select.val( "at" + val + "tr" );
1167 assert.equal( $select.val(), "at" + val + "tr", "Value with space character (" + key + ") in the middle selected (attr)" );
1169 $select.val( val + "attr" );
1170 assert.equal( $select.val(), val + "attr", "Value starting with space character (" + key + ") selected (attr)" );
1173 jQuery.each( spaces, function( key, obj ) {
1174 var value = obj.html || obj,
1175 val = obj.val || obj;
1177 html += "<option>text" + value + "</option>";
1178 html += "<option>te" + value + "xt</option>";
1179 html += "<option>" + value + "text</option>";
1180 $select.html( html );
1183 if ( /^\\u/.test( key ) ) {
1184 $select.val( val + "text" );
1185 assert.equal( $select.val(), val + "text", "Value with non-HTML space character at beginning is not stripped (" + key + ") selected (" + key + "text)" );
1186 $select.val( "te" + val + "xt" );
1187 assert.equal( $select.val(), "te" + val + "xt", "Value with non-space whitespace character (" + key + ") in the middle selected (text)" );
1188 $select.val( "text" + val );
1189 assert.equal( $select.val(), "text" + val, "Value with non-HTML space character at end is not stripped (" + key + ") selected (text" + key + ")" );
1191 $select.val( "text" );
1192 assert.equal( $select.val(), "text", "Value with HTML space character at beginning or end is stripped (" + key + ") selected (text)" );
1193 $select.val( "te xt" );
1194 assert.equal( $select.val(), "te xt", "Value with space character (" + key + ") in the middle selected (text)" );
1199 QUnit.test( "radio.val(space characters)", function( assert ) {
1200 assert.expect( 42 );
1202 var radio = jQuery( "<input type='radio'/>" ).appendTo( "#qunit-fixture" ),
1218 "\\u00a0": "\u00a0",
1222 jQuery.each( spaces, function( key, obj ) {
1223 var val = obj.val || obj;
1225 radio.val( "attr" + val );
1226 assert.equal( radio.val(), "attr" + val, "Value ending with space character (" + key + ") returned (set via val())" );
1228 radio.val( "at" + val + "tr" );
1229 assert.equal( radio.val(), "at" + val + "tr", "Value with space character (" + key + ") in the middle returned (set via val())" );
1231 radio.val( val + "attr" );
1232 assert.equal( radio.val(), val + "attr", "Value starting with space character (" + key + ") returned (set via val())" );
1235 jQuery.each( spaces, function( key, obj ) {
1236 var val = obj.val || obj,
1237 htmlVal = obj.html || obj;
1239 radio = jQuery( "<input type='radio' value='attr" + htmlVal + "'/>" ).appendTo( "#qunit-fixture" );
1240 assert.equal( radio.val(), "attr" + val, "Value ending with space character (" + key + ") returned (set via HTML)" );
1242 radio = jQuery( "<input type='radio' value='at" + htmlVal + "tr'/>" ).appendTo( "#qunit-fixture" );
1243 assert.equal( radio.val(), "at" + val + "tr", "Value with space character (" + key + ") in the middle returned (set via HTML)" );
1245 radio = jQuery( "<input type='radio' value='" + htmlVal + "attr'/>" ).appendTo( "#qunit-fixture" );
1246 assert.equal( radio.val(), val + "attr", "Value starting with space character (" + key + ") returned (set via HTML)" );
1250 var testAddClass = function( valueObj, assert ) {
1254 div = jQuery( "#qunit-fixture div" );
1255 div.addClass( valueObj( "test" ) );
1257 for ( i = 0; i < div.length; i++ ) {
1258 if ( !~div.get( i ).className.indexOf( "test" ) ) {
1262 assert.ok( pass, "Add Class" );
1264 // using contents will get regular, text, and comment nodes
1265 j = jQuery( "#nonnodes" ).contents();
1266 j.addClass( valueObj( "asdf" ) );
1267 assert.ok( j.hasClass( "asdf" ), "Check node,textnode,comment for addClass" );
1269 div = jQuery( "<div></div>" );
1271 div.addClass( valueObj( "test" ) );
1272 assert.equal( div.attr( "class" ), "test", "Make sure there's no extra whitespace." );
1274 div.attr( "class", " foo" );
1275 div.addClass( valueObj( "test" ) );
1276 assert.equal( div.attr( "class" ), "foo test", "Make sure there's no extra whitespace." );
1278 div.attr( "class", "foo" );
1279 div.addClass( valueObj( "bar baz" ) );
1280 assert.equal( div.attr( "class" ), "foo bar baz", "Make sure there isn't too much trimming." );
1283 div.addClass( valueObj( "foo" ) ).addClass( valueObj( "foo" ) );
1284 assert.equal( div.attr( "class" ), "foo", "Do not add the same class twice in separate calls." );
1286 div.addClass( valueObj( "fo" ) );
1287 assert.equal( div.attr( "class" ), "foo fo", "Adding a similar class does not get interrupted." );
1288 div.removeClass().addClass( "wrap2" );
1289 assert.ok( div.addClass( "wrap" ).hasClass( "wrap" ), "Can add similarly named classes" );
1292 div.addClass( valueObj( "bar bar" ) );
1293 assert.equal( div.attr( "class" ), "bar", "Do not add the same class twice in the same call." );
1296 QUnit.test( "addClass(String)", function( assert ) {
1297 testAddClass( bareObj, assert );
1300 QUnit.test( "addClass(Function)", function( assert ) {
1301 testAddClass( functionReturningObj, assert );
1304 QUnit.test( "addClass(Array)", function( assert ) {
1305 testAddClass( arrayFromString, assert );
1308 QUnit.test( "addClass(Function) with incoming value", function( assert ) {
1309 assert.expect( 59 );
1311 div = jQuery( "#qunit-fixture div" ),
1312 old = div.map( function() {
1313 return jQuery( this ).attr( "class" ) || "";
1316 div.addClass( function( i, val ) {
1317 assert.equal( val, old[ i ], "Make sure the incoming value is correct." );
1322 for ( i = 0; i < div.length; i++ ) {
1323 if ( div.get( i ).className.indexOf( "test" ) === -1 ) {
1327 assert.ok( pass, "Add Class" );
1330 var testRemoveClass = function( valueObj, assert ) {
1333 var $set = jQuery( "#qunit-fixture div" ),
1334 div = document.createElement( "div" );
1336 $set.addClass( "test" ).removeClass( valueObj( "test" ) );
1338 assert.ok( !$set.is( ".test" ), "Remove Class" );
1340 $set.addClass( "test" ).addClass( "foo" ).addClass( "bar" );
1341 $set.removeClass( valueObj( "test" ) ).removeClass( valueObj( "bar" ) ).removeClass( valueObj( "foo" ) );
1343 assert.ok( !$set.is( ".test,.bar,.foo" ), "Remove multiple classes" );
1345 // Make sure that a null value doesn't cause problems
1346 $set.eq( 0 ).addClass( "expected" ).removeClass( valueObj( null ) );
1347 assert.ok( $set.eq( 0 ).is( ".expected" ), "Null value passed to removeClass" );
1349 $set.eq( 0 ).addClass( "expected" ).removeClass( valueObj( "" ) );
1350 assert.ok( $set.eq( 0 ).is( ".expected" ), "Empty string passed to removeClass" );
1352 // using contents will get regular, text, and comment nodes
1353 $set = jQuery( "#nonnodes" ).contents();
1354 $set.removeClass( valueObj( "asdf" ) );
1355 assert.ok( !$set.hasClass( "asdf" ), "Check node,textnode,comment for removeClass" );
1357 jQuery( div ).removeClass( valueObj( "foo" ) );
1358 assert.strictEqual( jQuery( div ).attr( "class" ), undefined, "removeClass doesn't create a class attribute" );
1360 div.className = " test foo ";
1362 jQuery( div ).removeClass( valueObj( "foo" ) );
1363 assert.equal( div.className, "test", "Make sure remaining className is trimmed." );
1365 div.className = " test ";
1367 jQuery( div ).removeClass( valueObj( "test" ) );
1368 assert.equal( div.className, "", "Make sure there is nothing left after everything is removed." );
1371 QUnit.test( "removeClass(String) - simple", function( assert ) {
1372 testRemoveClass( bareObj, assert );
1375 QUnit.test( "removeClass(Function) - simple", function( assert ) {
1376 testRemoveClass( functionReturningObj, assert );
1379 QUnit.test( "removeClass(Array) - simple", function( assert ) {
1380 testRemoveClass( arrayFromString, assert );
1383 QUnit.test( "removeClass(Function) with incoming value", function( assert ) {
1384 assert.expect( 59 );
1386 var $divs = jQuery( "#qunit-fixture div" ).addClass( "test" ), old = $divs.map( function() {
1387 return jQuery( this ).attr( "class" );
1390 $divs.removeClass( function( i, val ) {
1391 assert.equal( val, old[ i ], "Make sure the incoming value is correct." );
1395 assert.ok( !$divs.is( ".test" ), "Remove Class" );
1398 QUnit.test( "removeClass() removes duplicates", function( assert ) {
1401 var $div = jQuery( jQuery.parseHTML( "<div class='x x x'></div>" ) );
1403 $div.removeClass( "x" );
1405 assert.ok( !$div.hasClass( "x" ), "Element with multiple same classes does not escape the wrath of removeClass()" );
1408 QUnit.test( "removeClass(undefined) is a no-op", function( assert ) {
1411 var $div = jQuery( "<div class='base second'></div>" );
1412 $div.removeClass( undefined );
1414 assert.ok( $div.hasClass( "base" ) && $div.hasClass( "second" ), "Element still has classes after removeClass(undefined)" );
1417 var testToggleClass = function( valueObj, assert ) {
1418 assert.expect( 11 );
1420 var e = jQuery( "#firstp" );
1421 assert.ok( !e.is( ".test" ), "Assert class not present" );
1422 e.toggleClass( valueObj( "test" ) );
1423 assert.ok( e.is( ".test" ), "Assert class present" );
1424 e.toggleClass( valueObj( "test" ) );
1425 assert.ok( !e.is( ".test" ), "Assert class not present" );
1427 // class name with a boolean
1428 e.toggleClass( valueObj( "test" ), false );
1429 assert.ok( !e.is( ".test" ), "Assert class not present" );
1430 e.toggleClass( valueObj( "test" ), false );
1431 assert.ok( !e.is( ".test" ), "Assert class still not present" );
1432 e.toggleClass( valueObj( "test" ), true );
1433 assert.ok( e.is( ".test" ), "Assert class present" );
1434 e.toggleClass( valueObj( "test" ), true );
1435 assert.ok( e.is( ".test" ), "Assert class still present" );
1436 e.toggleClass( valueObj( "test" ), false );
1437 assert.ok( !e.is( ".test" ), "Assert class not present" );
1439 // multiple class names
1440 e.addClass( "testA testB" );
1441 assert.ok( e.is( ".testA.testB" ), "Assert 2 different classes present" );
1442 e.toggleClass( valueObj( "testB testC" ) );
1443 assert.ok( ( e.is( ".testA.testC" ) && !e.is( ".testB" ) ), "Assert 1 class added, 1 class removed, and 1 class kept" );
1444 e.toggleClass( valueObj( "testA testC" ) );
1445 assert.ok( ( !e.is( ".testA" ) && !e.is( ".testB" ) && !e.is( ".testC" ) ), "Assert no class present" );
1448 QUnit.test( "toggleClass(String|boolean|undefined[, boolean])", function( assert ) {
1449 testToggleClass( bareObj, assert );
1452 QUnit.test( "toggleClass(Function[, boolean])", function( assert ) {
1453 testToggleClass( functionReturningObj, assert );
1456 QUnit.test( "toggleClass(Array[, boolean])", function( assert ) {
1457 testToggleClass( arrayFromString, assert );
1460 QUnit.test( "toggleClass(Function[, boolean]) with incoming value", function( assert ) {
1461 assert.expect( 14 );
1463 var e = jQuery( "#firstp" ),
1464 old = e.attr( "class" ) || "";
1466 assert.ok( !e.is( ".test" ), "Assert class not present" );
1468 e.toggleClass( function( i, val ) {
1469 assert.equal( old, val, "Make sure the incoming value is correct." );
1472 assert.ok( e.is( ".test" ), "Assert class present" );
1474 old = e.attr( "class" );
1476 e.toggleClass( function( i, val ) {
1477 assert.equal( old, val, "Make sure the incoming value is correct." );
1480 assert.ok( !e.is( ".test" ), "Assert class not present" );
1482 old = e.attr( "class" ) || "";
1484 // class name with a boolean
1485 e.toggleClass( function( i, val, state ) {
1486 assert.equal( old, val, "Make sure the incoming value is correct." );
1487 assert.equal( state, false, "Make sure that the state is passed in." );
1490 assert.ok( !e.is( ".test" ), "Assert class not present" );
1492 old = e.attr( "class" ) || "";
1494 e.toggleClass( function( i, val, state ) {
1495 assert.equal( old, val, "Make sure the incoming value is correct." );
1496 assert.equal( state, true, "Make sure that the state is passed in." );
1499 assert.ok( e.is( ".test" ), "Assert class present" );
1501 old = e.attr( "class" );
1503 e.toggleClass( function( i, val, state ) {
1504 assert.equal( old, val, "Make sure the incoming value is correct." );
1505 assert.equal( state, false, "Make sure that the state is passed in." );
1508 assert.ok( !e.is( ".test" ), "Assert class not present" );
1511 QUnit.test( "addClass, removeClass, hasClass", function( assert ) {
1512 assert.expect( 17 );
1514 var jq = jQuery( "<p>Hi</p>" ), x = jq[ 0 ];
1516 jq.addClass( "hi" );
1517 assert.equal( x.className, "hi", "Check single added class" );
1519 jq.addClass( "foo bar" );
1520 assert.equal( x.className, "hi foo bar", "Check more added classes" );
1523 assert.equal( x.className, "", "Remove all classes" );
1525 jq.addClass( "hi foo bar" );
1526 jq.removeClass( "foo" );
1527 assert.equal( x.className, "hi bar", "Check removal of one class" );
1529 assert.ok( jq.hasClass( "hi" ), "Check has1" );
1530 assert.ok( jq.hasClass( "bar" ), "Check has2" );
1532 jq = jQuery( "<p class='class1\nclass2\tcla.ss3\n\rclass4'></p>" );
1534 assert.ok( jq.hasClass( "class1" ), "Check hasClass with line feed" );
1535 assert.ok( jq.is( ".class1" ), "Check is with line feed" );
1536 assert.ok( jq.hasClass( "class2" ), "Check hasClass with tab" );
1537 assert.ok( jq.is( ".class2" ), "Check is with tab" );
1538 assert.ok( jq.hasClass( "cla.ss3" ), "Check hasClass with dot" );
1539 assert.ok( jq.hasClass( "class4" ), "Check hasClass with carriage return" );
1540 assert.ok( jq.is( ".class4" ), "Check is with carriage return" );
1542 jq.removeClass( "class2" );
1543 assert.ok( jq.hasClass( "class2" ) === false, "Check the class has been properly removed" );
1544 jq.removeClass( "cla" );
1545 assert.ok( jq.hasClass( "cla.ss3" ), "Check the dotted class has not been removed" );
1546 jq.removeClass( "cla.ss3" );
1547 assert.ok( jq.hasClass( "cla.ss3" ) === false, "Check the dotted class has been removed" );
1548 jq.removeClass( "class4" );
1549 assert.ok( jq.hasClass( "class4" ) === false, "Check the class has been properly removed" );
1552 QUnit.test( "addClass, removeClass, hasClass on many elements", function( assert ) {
1553 assert.expect( 19 );
1555 var elem = jQuery( "<p>p0</p><p>p1</p><p>p2</p>" );
1557 elem.addClass( "hi" );
1558 assert.equal( elem[ 0 ].className, "hi", "Check single added class" );
1559 assert.equal( elem[ 1 ].className, "hi", "Check single added class" );
1560 assert.equal( elem[ 2 ].className, "hi", "Check single added class" );
1562 elem.addClass( "foo bar" );
1563 assert.equal( elem[ 0 ].className, "hi foo bar", "Check more added classes" );
1564 assert.equal( elem[ 1 ].className, "hi foo bar", "Check more added classes" );
1565 assert.equal( elem[ 2 ].className, "hi foo bar", "Check more added classes" );
1568 assert.equal( elem[ 0 ].className, "", "Remove all classes" );
1569 assert.equal( elem[ 1 ].className, "", "Remove all classes" );
1570 assert.equal( elem[ 2 ].className, "", "Remove all classes" );
1572 elem.addClass( "hi foo bar" );
1573 elem.removeClass( "foo" );
1574 assert.equal( elem[ 0 ].className, "hi bar", "Check removal of one class" );
1575 assert.equal( elem[ 1 ].className, "hi bar", "Check removal of one class" );
1576 assert.equal( elem[ 2 ].className, "hi bar", "Check removal of one class" );
1578 assert.ok( elem.hasClass( "hi" ), "Check has1" );
1579 assert.ok( elem.hasClass( "bar" ), "Check has2" );
1581 assert.ok( jQuery( "<p class='hi'>p0</p><p>p1</p><p>p2</p>" ).hasClass( "hi" ),
1582 "Did find a class in the first element" );
1583 assert.ok( jQuery( "<p>p0</p><p class='hi'>p1</p><p>p2</p>" ).hasClass( "hi" ),
1584 "Did find a class in the second element" );
1585 assert.ok( jQuery( "<p>p0</p><p>p1</p><p class='hi'>p2</p>" ).hasClass( "hi" ),
1586 "Did find a class in the last element" );
1588 assert.ok( jQuery( "<p class='hi'>p0</p><p class='hi'>p1</p><p class='hi'>p2</p>" ).hasClass( "hi" ),
1589 "Did find a class when present in all elements" );
1591 assert.ok( !jQuery( "<p class='hi0'>p0</p><p class='hi1'>p1</p><p class='hi2'>p2</p>" ).hasClass( "hi" ),
1592 "Did not find a class when not present" );
1595 QUnit.test( "addClass, removeClass, hasClass on many elements - Array", function( assert ) {
1596 assert.expect( 16 );
1598 var elem = jQuery( "<p>p0</p><p>p1</p><p>p2</p>" );
1600 elem.addClass( [ "hi" ] );
1601 assert.equal( elem[ 0 ].className, "hi", "Check single added class" );
1602 assert.equal( elem[ 1 ].className, "hi", "Check single added class" );
1603 assert.equal( elem[ 2 ].className, "hi", "Check single added class" );
1605 elem.addClass( [ "foo", "bar" ] );
1606 assert.equal( elem[ 0 ].className, "hi foo bar", "Check more added classes" );
1607 assert.equal( elem[ 1 ].className, "hi foo bar", "Check more added classes" );
1608 assert.equal( elem[ 2 ].className, "hi foo bar", "Check more added classes" );
1611 assert.equal( elem[ 0 ].className, "", "Remove all classes" );
1612 assert.equal( elem[ 1 ].className, "", "Remove all classes" );
1613 assert.equal( elem[ 2 ].className, "", "Remove all classes" );
1615 elem.addClass( [ "hi", "foo", "bar", "baz" ] );
1616 elem.removeClass( [ "foo" ] );
1617 assert.equal( elem[ 0 ].className, "hi bar baz", "Check removal of one class" );
1618 assert.equal( elem[ 1 ].className, "hi bar baz", "Check removal of one class" );
1619 assert.equal( elem[ 2 ].className, "hi bar baz", "Check removal of one class" );
1621 elem.removeClass( [ "bar baz" ] );
1622 assert.equal( elem[ 0 ].className, "hi", "Check removal of two classes" );
1623 assert.equal( elem[ 1 ].className, "hi", "Check removal of two classes" );
1624 assert.equal( elem[ 2 ].className, "hi", "Check removal of two classes" );
1626 assert.ok( elem.hasClass( "hi" ), "Check has1" );
1629 QUnit.test( "addClass, removeClass, hasClass on elements with classes with non-HTML whitespace (gh-3072, gh-3003)", function( assert ) {
1632 var $elem = jQuery( "<div class=' test'></div>" );
1634 function testMatches() {
1635 assert.ok( $elem.is( ".\\A0 test" ), "Element matches with collapsed space" );
1636 assert.ok( $elem.is( ".\\A0test" ), "Element matches with non-breaking space" );
1637 assert.ok( $elem.hasClass( "\xA0test" ), "Element has class with non-breaking space" );
1641 $elem.addClass( "foo" );
1643 $elem.removeClass( "foo" );
1647 QUnit.test( "contents().hasClass() returns correct values", function( assert ) {
1650 var $div = jQuery( "<div><span class='foo'></span><!-- comment -->text</div>" ),
1651 $contents = $div.contents();
1653 assert.ok( $contents.hasClass( "foo" ), "Found 'foo' in $contents" );
1654 assert.ok( !$contents.hasClass( "undefined" ), "Did not find 'undefined' in $contents (correctly)" );
1657 QUnit.test( "hasClass correctly interprets non-space separators (#13835)", function( assert ) {
1663 "line-feed": " ",
1664 "form-feed": "",
1665 "carriage-return": " "
1667 classes = jQuery.map( map, function( separator, label ) {
1668 return " " + separator + label + separator + " ";
1670 $div = jQuery( "<div class='" + classes + "'></div>" );
1672 jQuery.each( map, function( label ) {
1673 assert.ok( $div.hasClass( label ), label.replace( "-", " " ) );
1677 QUnit.test( "coords returns correct values in IE6/IE7, see #10828", function( assert ) {
1681 map = jQuery( "<map></map>" );
1683 area = map.html( "<area shape='rect' coords='0,0,0,0' href='#' alt='a'></area>" ).find( "area" );
1684 assert.equal( area.attr( "coords" ), "0,0,0,0", "did not retrieve coords correctly" );
1687 QUnit.test( "should not throw at $(option).val() (#14686)", function( assert ) {
1691 jQuery( "<option></option>" ).val();
1698 QUnit.test( "option value not trimmed when setting via parent select", function( assert ) {
1700 assert.equal( jQuery( "<select><option> 2</option></select>" ).val( "2" ).val(), "2" );
1703 QUnit.test( "Insignificant white space returned for $(option).val() (#14858, gh-2978)", function( assert ) {
1704 assert.expect( 16 );
1706 var val = jQuery( "<option></option>" ).val();
1707 assert.equal( val.length, 0, "Empty option should have no value" );
1709 jQuery.each( [ " ", "\n", "\t", "\f", "\r" ], function( i, character ) {
1710 var val = jQuery( "<option>" + character + "</option>" ).val();
1711 assert.equal( val.length, 0, "insignificant white-space returned for value" );
1713 val = jQuery( "<option>" + character + "test" + character + "</option>" ).val();
1714 assert.equal( val.length, 4, "insignificant white-space returned for value" );
1716 val = jQuery( "<option>te" + character + "st</option>" ).val();
1717 assert.equal( val, "te st", "Whitespace is collapsed in values" );
1721 QUnit.test( "SVG class manipulation (gh-2199)", function( assert ) {
1722 assert.expect( 12 );
1724 function createSVGElement( nodeName ) {
1725 return document.createElementNS( "http://www.w3.org/2000/svg", nodeName );
1733 var elem = jQuery( createSVGElement( this ) );
1735 elem.addClass( "awesome" );
1736 assert.ok( elem.hasClass( "awesome" ), "SVG element (" + this + ") has added class" );
1738 elem.removeClass( "awesome" );
1739 assert.ok( !elem.hasClass( "awesome" ), "SVG element (" + this + ") removes the class" );
1741 elem.toggleClass( "awesome" );
1742 assert.ok( elem.hasClass( "awesome" ), "SVG element (" + this + ") toggles the class on" );
1744 elem.toggleClass( "awesome" );
1745 assert.ok( !elem.hasClass( "awesome" ), "SVG element (" + this + ") toggles the class off" );
1749 QUnit.test( "non-lowercase boolean attribute getters should not crash", function( assert ) {
1752 var elem = jQuery( "<input checked required autofocus type='checkbox'>" );
1756 required: "requiRed",
1757 autofocus: "AUTOFOCUS"
1758 }, function( lowercased, original ) {
1760 assert.strictEqual( elem.attr( original ), lowercased,
1761 "The '" + this + "' attribute getter should return the lowercased name" );
1763 assert.ok( false, "The '" + this + "' attribute getter threw" );
1769 // Test trustedTypes support in browsers where they're supported (currently Chrome 83+).
1770 // Browsers with no TrustedScriptURL support still run tests on object wrappers with
1771 // a proper `toString` function.
1773 "Basic TrustedScriptURL support (gh-4948)",
1774 "mock.php?action=trustedTypesAttributes",
1775 function( assert, jQuery, window, document, test ) {
1776 var done = assert.async();
1780 test.forEach( function( result ) {
1781 assert.deepEqual( result.actual, result.expected, result.message );
1784 supportjQuery.get( baseURL + "mock.php?action=cspClean" ).then( done );