attributes.js 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. module( "attributes", {
  2. teardown: moduleTeardown
  3. });
  4. function bareObj( value ) {
  5. return value;
  6. }
  7. function functionReturningObj( value ) {
  8. return function() {
  9. return value;
  10. };
  11. }
  12. /*
  13. ======== local reference =======
  14. bareObj and functionReturningObj can be used to test passing functions to setters
  15. See testVal below for an example
  16. bareObj( value );
  17. This function returns whatever value is passed in
  18. functionReturningObj( value );
  19. Returns a function that returns the value
  20. */
  21. test( "jQuery.propFix integrity test", function() {
  22. expect( 1 );
  23. // This must be maintained and equal jQuery.attrFix when appropriate
  24. // Ensure that accidental or erroneous property
  25. // overwrites don't occur
  26. // This is simply for better code coverage and future proofing.
  27. var props = {
  28. "tabindex": "tabIndex",
  29. "readonly": "readOnly",
  30. "for": "htmlFor",
  31. "class": "className",
  32. "maxlength": "maxLength",
  33. "cellspacing": "cellSpacing",
  34. "cellpadding": "cellPadding",
  35. "rowspan": "rowSpan",
  36. "colspan": "colSpan",
  37. "usemap": "useMap",
  38. "frameborder": "frameBorder",
  39. "contenteditable": "contentEditable"
  40. };
  41. deepEqual( props, jQuery.propFix, "jQuery.propFix passes integrity check" );
  42. });
  43. test( "attr(String)", function() {
  44. expect( 50 );
  45. var extras, body, $body,
  46. select, optgroup, option, $img, styleElem,
  47. $button, $form, $a;
  48. equal( jQuery("#text1").attr("type"), "text", "Check for type attribute" );
  49. equal( jQuery("#radio1").attr("type"), "radio", "Check for type attribute" );
  50. equal( jQuery("#check1").attr("type"), "checkbox", "Check for type attribute" );
  51. equal( jQuery("#simon1").attr("rel"), "bookmark", "Check for rel attribute" );
  52. equal( jQuery("#google").attr("title"), "Google!", "Check for title attribute" );
  53. equal( jQuery("#mark").attr("hreflang"), "en", "Check for hreflang attribute" );
  54. equal( jQuery("#en").attr("lang"), "en", "Check for lang attribute" );
  55. equal( jQuery("#simon").attr("class"), "blog link", "Check for class attribute" );
  56. equal( jQuery("#name").attr("name"), "name", "Check for name attribute" );
  57. equal( jQuery("#text1").attr("name"), "action", "Check for name attribute" );
  58. ok( jQuery("#form").attr("action").indexOf("formaction") >= 0, "Check for action attribute" );
  59. equal( jQuery("#text1").attr("value", "t").attr("value"), "t", "Check setting the value attribute" );
  60. equal( jQuery("#text1").attr("value", "").attr("value"), "", "Check setting the value attribute to empty string" );
  61. equal( jQuery("<div value='t'></div>").attr("value"), "t", "Check setting custom attr named 'value' on a div" );
  62. equal( jQuery("#form").attr("blah", "blah").attr("blah"), "blah", "Set non-existent attribute on a form" );
  63. equal( jQuery("#foo").attr("height"), undefined, "Non existent height attribute should return undefined" );
  64. // [7472] & [3113] (form contains an input with name="action" or name="id")
  65. extras = jQuery("<input id='id' name='id' /><input id='name' name='name' /><input id='target' name='target' />").appendTo("#testForm");
  66. equal( jQuery("#form").attr("action","newformaction").attr("action"), "newformaction", "Check that action attribute was changed" );
  67. equal( jQuery("#testForm").attr("target"), undefined, "Retrieving target does not equal the input with name=target" );
  68. equal( jQuery("#testForm").attr("target", "newTarget").attr("target"), "newTarget", "Set target successfully on a form" );
  69. equal( jQuery("#testForm").removeAttr("id").attr("id"), undefined, "Retrieving id does not equal the input with name=id after id is removed [#7472]" );
  70. // Bug #3685 (form contains input with name="name")
  71. equal( jQuery("#testForm").attr("name"), undefined, "Retrieving name does not retrieve input with name=name" );
  72. extras.remove();
  73. equal( jQuery("#text1").attr("maxlength"), "30", "Check for maxlength attribute" );
  74. equal( jQuery("#text1").attr("maxLength"), "30", "Check for maxLength attribute" );
  75. equal( jQuery("#area1").attr("maxLength"), "30", "Check for maxLength attribute" );
  76. // using innerHTML in IE causes href attribute to be serialized to the full path
  77. jQuery("<a/>").attr({
  78. "id": "tAnchor5",
  79. "href": "#5"
  80. }).appendTo("#qunit-fixture");
  81. equal( jQuery("#tAnchor5").attr("href"), "#5", "Check for non-absolute href (an anchor)" );
  82. jQuery("<a id='tAnchor6' href='#5' />").appendTo("#qunit-fixture");
  83. equal( jQuery("#tAnchor5").prop("href"), jQuery("#tAnchor6").prop("href"), "Check for absolute href prop on an anchor" );
  84. $("<script type='jquery/test' src='#5' id='scriptSrc'></script>").appendTo("#qunit-fixture");
  85. equal( jQuery("#tAnchor5").prop("href"), jQuery("#scriptSrc").prop("src"), "Check for absolute src prop on a script" );
  86. // list attribute is readonly by default in browsers that support it
  87. jQuery("#list-test").attr( "list", "datalist" );
  88. equal( jQuery("#list-test").attr("list"), "datalist", "Check setting list attribute" );
  89. // Related to [5574] and [5683]
  90. body = document.body;
  91. $body = jQuery( body );
  92. strictEqual( $body.attr("foo"), undefined, "Make sure that a non existent attribute returns undefined" );
  93. body.setAttribute( "foo", "baz" );
  94. equal( $body.attr("foo"), "baz", "Make sure the dom attribute is retrieved when no expando is found" );
  95. $body.attr( "foo","cool" );
  96. equal( $body.attr("foo"), "cool", "Make sure that setting works well when both expando and dom attribute are available" );
  97. body.removeAttribute("foo"); // Cleanup
  98. select = document.createElement("select");
  99. optgroup = document.createElement("optgroup");
  100. option = document.createElement("option");
  101. optgroup.appendChild( option );
  102. select.appendChild( optgroup );
  103. equal( jQuery( option ).prop("selected"), true, "Make sure that a single option is selected, even when in an optgroup." );
  104. $img = jQuery("<img style='display:none' width='215' height='53' src='data/1x1.jpg'/>").appendTo("body");
  105. equal( $img.attr("width"), "215", "Retrieve width attribute an an element with display:none." );
  106. equal( $img.attr("height"), "53", "Retrieve height attribute an an element with display:none." );
  107. // Check for style support
  108. styleElem = jQuery("<div/>").appendTo("#qunit-fixture").css({
  109. background: "url(UPPERlower.gif)"
  110. });
  111. ok( !!~styleElem.attr("style").indexOf("UPPERlower.gif"), "Check style attribute getter" );
  112. ok( !!~styleElem.attr("style", "position:absolute;").attr("style").indexOf("absolute"), "Check style setter" );
  113. // Check value on button element (#1954)
  114. $button = jQuery("<button>text</button>").insertAfter("#button");
  115. strictEqual( $button.attr("value"), undefined, "Absence of value attribute on a button" );
  116. equal( $button.attr( "value", "foobar" ).attr("value"), "foobar", "Value attribute on a button does not return innerHTML" );
  117. equal( $button.attr("value", "baz").html(), "text", "Setting the value attribute does not change innerHTML" );
  118. // Attributes with a colon on a table element (#1591)
  119. equal( jQuery("#table").attr("test:attrib"), undefined, "Retrieving a non-existent attribute on a table with a colon does not throw an error." );
  120. 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." );
  121. $form = jQuery("<form class='something'></form>").appendTo("#qunit-fixture");
  122. equal( $form.attr("class"), "something", "Retrieve the class attribute on a form." );
  123. $a = jQuery("<a href='#' onclick='something()'>Click</a>").appendTo("#qunit-fixture");
  124. equal( $a.attr("onclick"), "something()", "Retrieve ^on attribute without anonymous function wrapper." );
  125. ok( jQuery("<div/>").attr("doesntexist") === undefined, "Make sure undefined is returned when no attribute is found." );
  126. ok( jQuery("<div/>").attr("title") === undefined, "Make sure undefined is returned when no attribute is found." );
  127. equal( jQuery("<div/>").attr( "title", "something" ).attr("title"), "something", "Set the title attribute." );
  128. ok( jQuery().attr("doesntexist") === undefined, "Make sure undefined is returned when no element is there." );
  129. equal( jQuery("<div/>").attr("value"), undefined, "An unset value on a div returns undefined." );
  130. strictEqual( jQuery("<select><option value='property'></option></select>").attr("value"), undefined, "An unset value on a select returns undefined." );
  131. $form = jQuery("#form").attr( "enctype", "multipart/form-data" );
  132. equal( $form.prop("enctype"), "multipart/form-data", "Set the enctype of a form (encoding in IE6/7 #6743)" );
  133. });
  134. test( "attr(String) on cloned elements, #9646", function() {
  135. expect( 4 );
  136. var div,
  137. input = jQuery("<input name='tester' />");
  138. input.attr("name");
  139. strictEqual( input.clone( true ).attr( "name", "test" )[ 0 ].name, "test", "Name attribute should be changed on cloned element" );
  140. div = jQuery("<div id='tester' />");
  141. div.attr("id");
  142. strictEqual( div.clone( true ).attr( "id", "test" )[ 0 ].id, "test", "Id attribute should be changed on cloned element" );
  143. input = jQuery("<input value='tester' />");
  144. input.attr("value");
  145. strictEqual( input.clone( true ).attr( "value", "test" )[ 0 ].value, "test", "Value attribute should be changed on cloned element" );
  146. strictEqual( input.clone( true ).attr( "value", 42 )[ 0 ].value, "42", "Value attribute should be changed on cloned element" );
  147. });
  148. test( "attr(String) in XML Files", function() {
  149. expect( 3 );
  150. var xml = createDashboardXML();
  151. equal( jQuery( "locations", xml ).attr("class"), "foo", "Check class attribute in XML document" );
  152. equal( jQuery( "location", xml ).attr("for"), "bar", "Check for attribute in XML document" );
  153. equal( jQuery( "location", xml ).attr("checked"), "different", "Check that hooks are not attached in XML document" );
  154. });
  155. test( "attr(String, Function)", function() {
  156. expect( 2 );
  157. equal(
  158. jQuery("#text1").attr( "value", function() {
  159. return this.id;
  160. }).attr("value"),
  161. "text1",
  162. "Set value from id"
  163. );
  164. equal(
  165. jQuery("#text1").attr( "title", function(i) {
  166. return i;
  167. }).attr("title"),
  168. "0",
  169. "Set value with an index"
  170. );
  171. });
  172. test( "attr(Hash)", function() {
  173. expect( 3 );
  174. var pass = true;
  175. jQuery("div").attr({
  176. "foo": "baz",
  177. "zoo": "ping"
  178. }).each(function() {
  179. if ( this.getAttribute("foo") !== "baz" && this.getAttribute("zoo") !== "ping" ) {
  180. pass = false;
  181. }
  182. });
  183. ok( pass, "Set Multiple Attributes" );
  184. equal(
  185. jQuery("#text1").attr({
  186. "value": function() {
  187. return this["id"];
  188. }}).attr("value"),
  189. "text1",
  190. "Set attribute to computed value #1"
  191. );
  192. equal(
  193. jQuery("#text1").attr({
  194. "title": function(i) {
  195. return i;
  196. }
  197. }).attr("title"),
  198. "0",
  199. "Set attribute to computed value #2"
  200. );
  201. });
  202. test( "attr(String, Object)", function() {
  203. expect( 71 );
  204. var $input, $text, $details,
  205. attributeNode, commentNode, textNode, obj,
  206. table, td, j, type,
  207. check, thrown, button, $radio, $radios, $svg,
  208. div = jQuery("div").attr("foo", "bar"),
  209. i = 0,
  210. fail = false;
  211. for ( ; i < div.length; i++ ) {
  212. if ( div[ i ].getAttribute("foo") !== "bar" ) {
  213. fail = i;
  214. break;
  215. }
  216. }
  217. equal( fail, false, "Set Attribute, the #" + fail + " element didn't get the attribute 'foo'" );
  218. ok(
  219. jQuery("#foo").attr({
  220. "width": null
  221. }),
  222. "Try to set an attribute to nothing"
  223. );
  224. jQuery("#name").attr( "name", "something" );
  225. equal( jQuery("#name").attr("name"), "something", "Set name attribute" );
  226. jQuery("#name").attr( "name", null );
  227. equal( jQuery("#name").attr("name"), undefined, "Remove name attribute" );
  228. $input = jQuery( "<input>", {
  229. name: "something",
  230. id: "specified"
  231. });
  232. equal( $input.attr("name"), "something", "Check element creation gets/sets the name attribute." );
  233. equal( $input.attr("id"), "specified", "Check element creation gets/sets the id attribute." );
  234. // As of fixing #11115, we only guarantee boolean property update for checked and selected
  235. $input = jQuery("<input type='checkbox'/>").attr( "checked", true );
  236. equal( $input.prop("checked"), true, "Setting checked updates property (verified by .prop)" );
  237. equal( $input[0].checked, true, "Setting checked updates property (verified by native property)" );
  238. $input = jQuery("<option/>").attr( "selected", true );
  239. equal( $input.prop("selected"), true, "Setting selected updates property (verified by .prop)" );
  240. equal( $input[0].selected, true, "Setting selected updates property (verified by native property)" );
  241. $input = jQuery("#check2");
  242. $input.prop( "checked", true ).prop( "checked", false ).attr( "checked", true );
  243. equal( $input.attr("checked"), "checked", "Set checked (verified by .attr)" );
  244. $input.prop( "checked", false ).prop( "checked", true ).attr( "checked", false );
  245. equal( $input.attr("checked"), undefined, "Remove checked (verified by .attr)" );
  246. $input = jQuery("#text1").prop( "readOnly", true ).prop( "readOnly", false ).attr( "readonly", true );
  247. equal( $input.attr("readonly"), "readonly", "Set readonly (verified by .attr)" );
  248. $input.prop( "readOnly", false ).prop( "readOnly", true ).attr( "readonly", false );
  249. equal( $input.attr("readonly"), undefined, "Remove readonly (verified by .attr)" );
  250. $input = jQuery("#check2").attr( "checked", true ).attr( "checked", false ).prop( "checked", true );
  251. equal( $input[0].checked, true, "Set checked property (verified by native property)" );
  252. equal( $input.prop("checked"), true, "Set checked property (verified by .prop)" );
  253. equal( $input.attr("checked"), undefined, "Setting checked property doesn't affect checked attribute" );
  254. $input.attr( "checked", false ).attr( "checked", true ).prop( "checked", false );
  255. equal( $input[0].checked, false, "Clear checked property (verified by native property)" );
  256. equal( $input.prop("checked"), false, "Clear checked property (verified by .prop)" );
  257. equal( $input.attr("checked"), "checked", "Clearing checked property doesn't affect checked attribute" );
  258. $input = jQuery("#check2").attr( "checked", false ).attr( "checked", "checked" );
  259. equal( $input.attr("checked"), "checked", "Set checked to 'checked' (verified by .attr)" );
  260. $radios = jQuery("#checkedtest").find("input[type='radio']");
  261. $radios.eq( 1 ).trigger("click");
  262. equal( $radios.eq( 1 ).prop("checked"), true, "Second radio was checked when clicked" );
  263. equal( $radios.eq( 0 ).attr("checked"), "checked", "First radio is still [checked]" );
  264. $input = jQuery("#text1").attr( "readonly", false ).prop( "readOnly", true );
  265. equal( $input[0].readOnly, true, "Set readonly property (verified by native property)" );
  266. equal( $input.prop("readOnly"), true, "Set readonly property (verified by .prop)" );
  267. $input.attr( "readonly", true ).prop( "readOnly", false );
  268. equal( $input[0].readOnly, false, "Clear readonly property (verified by native property)" );
  269. equal( $input.prop("readOnly"), false, "Clear readonly property (verified by .prop)" );
  270. $input = jQuery("#name").attr( "maxlength", "5" );
  271. equal( $input[0].maxLength, 5, "Set maxlength (verified by native property)" );
  272. $input.attr( "maxLength", "10" );
  273. equal( $input[0].maxLength, 10, "Set maxlength (verified by native property)" );
  274. // HTML5 boolean attributes
  275. $text = jQuery("#text1").attr({
  276. "autofocus": true,
  277. "required": true
  278. });
  279. equal( $text.attr("autofocus"), "autofocus", "Reading autofocus attribute yields 'autofocus'" );
  280. equal( $text.attr( "autofocus", false ).attr("autofocus"), undefined, "Setting autofocus to false removes it" );
  281. equal( $text.attr("required"), "required", "Reading required attribute yields 'required'" );
  282. equal( $text.attr( "required", false ).attr("required"), undefined, "Setting required attribute to false removes it" );
  283. $details = jQuery("<details open></details>").appendTo("#qunit-fixture");
  284. equal( $details.attr("open"), "open", "open attribute presence indicates true" );
  285. equal( $details.attr( "open", false ).attr("open"), undefined, "Setting open attribute to false removes it" );
  286. $text.attr( "data-something", true );
  287. equal( $text.attr("data-something"), "true", "Set data attributes");
  288. equal( $text.data("something"), true, "Setting data attributes are not affected by boolean settings");
  289. $text.attr( "data-another", false );
  290. equal( $text.attr("data-another"), "false", "Set data attributes");
  291. equal( $text.data("another"), false, "Setting data attributes are not affected by boolean settings" );
  292. equal( $text.attr( "aria-disabled", false ).attr("aria-disabled"), "false", "Setting aria attributes are not affected by boolean settings" );
  293. $text.removeData("something").removeData("another").removeAttr("aria-disabled");
  294. jQuery("#foo").attr("contenteditable", true);
  295. equal( jQuery("#foo").attr("contenteditable"), "true", "Enumerated attributes are set properly" );
  296. attributeNode = document.createAttribute("irrelevant");
  297. commentNode = document.createComment("some comment");
  298. textNode = document.createTextNode("some text");
  299. obj = {};
  300. jQuery.each( [ commentNode, textNode, attributeNode ], function( i, elem ) {
  301. var $elem = jQuery( elem );
  302. $elem.attr( "nonexisting", "foo" );
  303. strictEqual( $elem.attr("nonexisting"), undefined, "attr(name, value) works correctly on comment and text nodes (bug #7500)." );
  304. });
  305. // Register the property name to avoid generating a new global when testing window
  306. Globals.register("nonexisting");
  307. jQuery.each( [ window, document, obj, "#firstp" ], function( i, elem ) {
  308. var oldVal = elem.nonexisting,
  309. $elem = jQuery( elem );
  310. strictEqual( $elem.attr("nonexisting"), undefined, "attr works correctly for non existing attributes (bug #7500)." );
  311. equal( $elem.attr( "nonexisting", "foo" ).attr("nonexisting"), "foo", "attr falls back to prop on unsupported arguments" );
  312. elem.nonexisting = oldVal;
  313. });
  314. 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>");
  315. td = table.find("td").eq(0);
  316. td.attr( "rowspan", "2" );
  317. equal( td[ 0 ]["rowSpan"], 2, "Check rowspan is correctly set" );
  318. td.attr( "colspan", "2" );
  319. equal( td[ 0 ]["colSpan"], 2, "Check colspan is correctly set" );
  320. table.attr("cellspacing", "2");
  321. equal( table[ 0 ]["cellSpacing"], "2", "Check cellspacing is correctly set" );
  322. equal( jQuery("#area1").attr("value"), undefined, "Value attribute is distinct from value property." );
  323. // for #1070
  324. jQuery("#name").attr( "someAttr", "0" );
  325. equal( jQuery("#name").attr("someAttr"), "0", "Set attribute to a string of '0'" );
  326. jQuery("#name").attr( "someAttr", 0 );
  327. equal( jQuery("#name").attr("someAttr"), "0", "Set attribute to the number 0" );
  328. jQuery("#name").attr( "someAttr", 1 );
  329. equal( jQuery("#name").attr("someAttr"), "1", "Set attribute to the number 1" );
  330. // using contents will get comments regular, text, and comment nodes
  331. j = jQuery("#nonnodes").contents();
  332. j.attr( "name", "attrvalue" );
  333. equal( j.attr("name"), "attrvalue", "Check node,textnode,comment for attr" );
  334. j.removeAttr("name");
  335. // Type
  336. type = jQuery("#check2").attr("type");
  337. try {
  338. jQuery("#check2").attr( "type", "hidden" );
  339. ok( true, "No exception thrown on input type change" );
  340. } catch( e ) {
  341. ok( true, "Exception thrown on input type change: " + e );
  342. }
  343. check = document.createElement("input");
  344. thrown = true;
  345. try {
  346. jQuery( check ).attr( "type", "checkbox" );
  347. } catch( e ) {
  348. thrown = false;
  349. }
  350. ok( thrown, "Exception thrown when trying to change type property" );
  351. equal( "checkbox", jQuery( check ).attr("type"), "Verify that you can change the type of an input element that isn't in the DOM" );
  352. check = jQuery("<input />");
  353. thrown = true;
  354. try {
  355. check.attr( "type", "checkbox" );
  356. } catch( e ) {
  357. thrown = false;
  358. }
  359. ok( thrown, "Exception thrown when trying to change type property" );
  360. equal( "checkbox", check.attr("type"), "Verify that you can change the type of an input element that isn't in the DOM" );
  361. button = jQuery("#button");
  362. try {
  363. button.attr( "type", "submit" );
  364. ok( true, "No exception thrown on button type change" );
  365. } catch( e ) {
  366. ok( true, "Exception thrown on button type change: " + e );
  367. }
  368. $radio = jQuery( "<input>", {
  369. "value": "sup",
  370. "type": "radio"
  371. }).appendTo("#testForm");
  372. equal( $radio.val(), "sup", "Value is not reset when type is set after value on a radio" );
  373. // Setting attributes on svg elements (bug #3116)
  374. $svg = jQuery(
  375. "<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'>" +
  376. "<circle cx='200' cy='200' r='150' />" +
  377. "</svg>"
  378. ).appendTo("body");
  379. equal( $svg.attr( "cx", 100 ).attr("cx"), "100", "Set attribute on svg element" );
  380. $svg.remove();
  381. // undefined values are chainable
  382. jQuery("#name").attr( "maxlength", "5" ).removeAttr("nonexisting");
  383. equal( typeof jQuery("#name").attr( "maxlength", undefined ), "object", ".attr('attribute', undefined) is chainable (#5571)" );
  384. equal( jQuery("#name").attr( "maxlength", undefined ).attr("maxlength"), "5", ".attr('attribute', undefined) does not change value (#5571)" );
  385. equal( jQuery("#name").attr( "nonexisting", undefined ).attr("nonexisting"), undefined, ".attr('attribute', undefined) does not create attribute (#5571)" );
  386. });
  387. test( "attr(String, Object) - Loaded via XML document", function() {
  388. expect( 2 );
  389. var xml = createDashboardXML(),
  390. titles = [];
  391. jQuery( "tab", xml ).each(function() {
  392. titles.push( jQuery( this ).attr("title") );
  393. });
  394. equal( titles[ 0 ], "Location", "attr() in XML context: Check first title" );
  395. equal( titles[ 1 ], "Users", "attr() in XML context: Check second title" );
  396. });
  397. test( "attr(String, Object) - Loaded via XML fragment", function() {
  398. expect( 2 );
  399. var frag = createXMLFragment(),
  400. $frag = jQuery( frag );
  401. $frag.attr( "test", "some value" );
  402. equal( $frag.attr("test"), "some value", "set attribute" );
  403. $frag.attr( "test", null );
  404. equal( $frag.attr("test"), undefined, "remove attribute" );
  405. });
  406. test( "attr('tabindex')", function() {
  407. expect( 8 );
  408. // elements not natively tabbable
  409. equal( jQuery("#listWithTabIndex").attr("tabindex"), "5", "not natively tabbable, with tabindex set to 0" );
  410. equal( jQuery("#divWithNoTabIndex").attr("tabindex"), undefined, "not natively tabbable, no tabindex set" );
  411. // anchor with href
  412. equal( jQuery("#linkWithNoTabIndex").attr("tabindex"), undefined, "anchor with href, no tabindex set" );
  413. equal( jQuery("#linkWithTabIndex").attr("tabindex"), "2", "anchor with href, tabindex set to 2" );
  414. equal( jQuery("#linkWithNegativeTabIndex").attr("tabindex"), "-1", "anchor with href, tabindex set to -1" );
  415. // anchor without href
  416. equal( jQuery("#linkWithNoHrefWithNoTabIndex").attr("tabindex"), undefined, "anchor without href, no tabindex set" );
  417. equal( jQuery("#linkWithNoHrefWithTabIndex").attr("tabindex"), "1", "anchor without href, tabindex set to 2" );
  418. equal( jQuery("#linkWithNoHrefWithNegativeTabIndex").attr("tabindex"), "-1", "anchor without href, no tabindex set" );
  419. });
  420. test( "attr('tabindex', value)", function() {
  421. expect( 9 );
  422. var element = jQuery("#divWithNoTabIndex");
  423. equal( element.attr("tabindex"), undefined, "start with no tabindex" );
  424. // set a positive string
  425. element.attr( "tabindex", "1" );
  426. equal( element.attr("tabindex"), "1", "set tabindex to 1 (string)" );
  427. // set a zero string
  428. element.attr( "tabindex", "0" );
  429. equal( element.attr("tabindex"), "0", "set tabindex to 0 (string)" );
  430. // set a negative string
  431. element.attr( "tabindex", "-1" );
  432. equal( element.attr("tabindex"), "-1", "set tabindex to -1 (string)" );
  433. // set a positive number
  434. element.attr( "tabindex", 1 );
  435. equal( element.attr("tabindex"), "1", "set tabindex to 1 (number)" );
  436. // set a zero number
  437. element.attr( "tabindex", 0 );
  438. equal(element.attr("tabindex"), "0", "set tabindex to 0 (number)");
  439. // set a negative number
  440. element.attr( "tabindex", -1 );
  441. equal( element.attr("tabindex"), "-1", "set tabindex to -1 (number)" );
  442. element = jQuery("#linkWithTabIndex");
  443. equal( element.attr("tabindex"), "2", "start with tabindex 2" );
  444. element.attr( "tabindex", -1 );
  445. equal( element.attr("tabindex"), "-1", "set negative tabindex" );
  446. });
  447. test( "removeAttr(String)", function() {
  448. expect( 12 );
  449. var $first;
  450. equal( jQuery("#mark").removeAttr("class").attr("class"), undefined, "remove class" );
  451. equal( jQuery("#form").removeAttr("id").attr("id"), undefined, "Remove id" );
  452. equal( jQuery("#foo").attr( "style", "position:absolute;" ).removeAttr("style").attr("style"), undefined, "Check removing style attribute" );
  453. equal( jQuery("#form").attr( "style", "position:absolute;" ).removeAttr("style").attr("style"), undefined, "Check removing style attribute on a form" );
  454. equal( jQuery("<div style='position: absolute'></div>").appendTo("#foo").removeAttr("style").prop("style").cssText, "", "Check removing style attribute (#9699 Webkit)" );
  455. 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" );
  456. jQuery("#check1").removeAttr("checked").prop( "checked", true ).removeAttr("checked");
  457. equal( document.getElementById("check1").checked, false, "removeAttr sets boolean properties to false" );
  458. jQuery("#text1").prop( "readOnly", true ).removeAttr("readonly");
  459. equal( document.getElementById("text1").readOnly, false, "removeAttr sets boolean properties to false" );
  460. jQuery("#option2c").removeAttr("selected");
  461. 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)" );
  462. try {
  463. $first = jQuery("#first").attr( "contenteditable", "true" ).removeAttr("contenteditable");
  464. equal( $first.attr("contenteditable"), undefined, "Remove the contenteditable attribute" );
  465. } catch( e ) {
  466. ok( false, "Removing contenteditable threw an error (#10429)" );
  467. }
  468. $first = jQuery("<div Case='mixed'></div>");
  469. equal( $first.attr("Case"), "mixed", "case of attribute doesn't matter" );
  470. $first.removeAttr("Case");
  471. // IE 6/7 return empty string here, not undefined
  472. ok( !$first.attr("Case"), "mixed-case attribute was removed" );
  473. });
  474. test( "removeAttr(String) in XML", function() {
  475. expect( 7 );
  476. var xml = createDashboardXML(),
  477. iwt = jQuery( "infowindowtab", xml );
  478. equal( iwt.attr("normal"), "ab", "Check initial value" );
  479. iwt.removeAttr("Normal");
  480. equal( iwt.attr("normal"), "ab", "Should still be there" );
  481. iwt.removeAttr("normal");
  482. equal( iwt.attr("normal"), undefined, "Removed" );
  483. equal( iwt.attr("mixedCase"), "yes", "Check initial value" );
  484. equal( iwt.attr("mixedcase"), undefined, "toLowerCase not work good" );
  485. iwt.removeAttr("mixedcase");
  486. equal( iwt.attr("mixedCase"), "yes", "Should still be there" );
  487. iwt.removeAttr("mixedCase");
  488. equal( iwt.attr("mixedCase"), undefined, "Removed" );
  489. });
  490. test( "removeAttr(Multi String, variable space width)", function() {
  491. expect( 8 );
  492. var div = jQuery("<div id='a' alt='b' title='c' rel='d'></div>"),
  493. tests = {
  494. id: "a",
  495. alt: "b",
  496. title: "c",
  497. rel: "d"
  498. };
  499. jQuery.each( tests, function( key, val ) {
  500. equal( div.attr( key ), val, "Attribute `" + key + "` exists, and has a value of `" + val + "`" );
  501. });
  502. div.removeAttr( "id alt title rel " );
  503. jQuery.each( tests, function( key ) {
  504. equal( div.attr( key ), undefined, "Attribute `" + key + "` was removed" );
  505. });
  506. });
  507. test( "prop(String, Object)", function() {
  508. expect( 31 );
  509. equal( jQuery("#text1").prop("value"), "Test", "Check for value attribute" );
  510. equal( jQuery("#text1").prop( "value", "Test2" ).prop("defaultValue"), "Test", "Check for defaultValue attribute" );
  511. equal( jQuery("#select2").prop("selectedIndex"), 3, "Check for selectedIndex attribute" );
  512. equal( jQuery("#foo").prop("nodeName").toUpperCase(), "DIV", "Check for nodeName attribute" );
  513. equal( jQuery("#foo").prop("tagName").toUpperCase(), "DIV", "Check for tagName attribute" );
  514. equal( jQuery("<option/>").prop("selected"), false, "Check selected attribute on disconnected element." );
  515. equal( jQuery("#listWithTabIndex").prop("tabindex"), 5, "Check retrieving tabindex" );
  516. jQuery("#text1").prop( "readonly", true );
  517. equal( document.getElementById("text1").readOnly, true, "Check setting readOnly property with 'readonly'" );
  518. equal( jQuery("#label-for").prop("for"), "action", "Check retrieving htmlFor" );
  519. jQuery("#text1").prop("class", "test");
  520. equal( document.getElementById("text1").className, "test", "Check setting className with 'class'" );
  521. equal( jQuery("#text1").prop("maxlength"), 30, "Check retrieving maxLength" );
  522. jQuery("#table").prop( "cellspacing", 1 );
  523. equal( jQuery("#table").prop("cellSpacing"), "1", "Check setting and retrieving cellSpacing" );
  524. jQuery("#table").prop( "cellpadding", 1 );
  525. equal( jQuery("#table").prop("cellPadding"), "1", "Check setting and retrieving cellPadding" );
  526. jQuery("#table").prop( "rowspan", 1 );
  527. equal( jQuery("#table").prop("rowSpan"), 1, "Check setting and retrieving rowSpan" );
  528. jQuery("#table").prop( "colspan", 1 );
  529. equal( jQuery("#table").prop("colSpan"), 1, "Check setting and retrieving colSpan" );
  530. jQuery("#table").prop( "usemap", 1 );
  531. equal( jQuery("#table").prop("useMap"), 1, "Check setting and retrieving useMap" );
  532. jQuery("#table").prop( "frameborder", 1 );
  533. equal( jQuery("#table").prop("frameBorder"), 1, "Check setting and retrieving frameBorder" );
  534. QUnit.reset();
  535. var select, optgroup, option, attributeNode, commentNode, textNode, obj, $form,
  536. body = document.body,
  537. $body = jQuery( body );
  538. ok( $body.prop("nextSibling") === null, "Make sure a null expando returns null" );
  539. body["foo"] = "bar";
  540. equal( $body.prop("foo"), "bar", "Make sure the expando is preferred over the dom attribute" );
  541. body["foo"] = undefined;
  542. ok( $body.prop("foo") === undefined, "Make sure the expando is preferred over the dom attribute, even if undefined" );
  543. select = document.createElement("select");
  544. optgroup = document.createElement("optgroup");
  545. option = document.createElement("option");
  546. optgroup.appendChild( option );
  547. select.appendChild( optgroup );
  548. equal( jQuery( option ).prop("selected"), true, "Make sure that a single option is selected, even when in an optgroup." );
  549. equal( jQuery( document ).prop("nodeName"), "#document", "prop works correctly on document nodes (bug #7451)." );
  550. attributeNode = document.createAttribute("irrelevant");
  551. commentNode = document.createComment("some comment");
  552. textNode = document.createTextNode("some text");
  553. obj = {};
  554. jQuery.each( [ document, attributeNode, commentNode, textNode, obj, "#firstp" ], function( i, ele ) {
  555. strictEqual( jQuery( ele ).prop("nonexisting"), undefined, "prop works correctly for non existing attributes (bug #7500)." );
  556. });
  557. obj = {};
  558. jQuery.each( [ document, obj ], function( i, ele ) {
  559. var $ele = jQuery( ele );
  560. $ele.prop( "nonexisting", "foo" );
  561. equal( $ele.prop("nonexisting"), "foo", "prop(name, value) works correctly for non existing attributes (bug #7500)." );
  562. });
  563. jQuery( document ).removeProp("nonexisting");
  564. $form = jQuery("#form").prop( "enctype", "multipart/form-data" );
  565. equal( $form.prop("enctype"), "multipart/form-data", "Set the enctype of a form (encoding in IE6/7 #6743)" );
  566. });
  567. test( "prop('tabindex')", function() {
  568. expect( 11 );
  569. // inputs without tabIndex attribute
  570. equal( jQuery("#inputWithoutTabIndex").prop("tabindex"), 0, "input without tabindex" );
  571. equal( jQuery("#buttonWithoutTabIndex").prop("tabindex"), 0, "button without tabindex" );
  572. equal( jQuery("#textareaWithoutTabIndex").prop("tabindex"), 0, "textarea without tabindex" );
  573. // elements not natively tabbable
  574. equal( jQuery("#listWithTabIndex").prop("tabindex"), 5, "not natively tabbable, with tabindex set to 0" );
  575. equal( jQuery("#divWithNoTabIndex").prop("tabindex"), -1, "not natively tabbable, no tabindex set" );
  576. // anchor with href
  577. equal( jQuery("#linkWithNoTabIndex").prop("tabindex"), 0, "anchor with href, no tabindex set" );
  578. equal( jQuery("#linkWithTabIndex").prop("tabindex"), 2, "anchor with href, tabindex set to 2" );
  579. equal( jQuery("#linkWithNegativeTabIndex").prop("tabindex"), -1, "anchor with href, tabindex set to -1" );
  580. // anchor without href
  581. equal( jQuery("#linkWithNoHrefWithNoTabIndex").prop("tabindex"), -1, "anchor without href, no tabindex set" );
  582. equal( jQuery("#linkWithNoHrefWithTabIndex").prop("tabindex"), 1, "anchor without href, tabindex set to 2" );
  583. equal( jQuery("#linkWithNoHrefWithNegativeTabIndex").prop("tabindex"), -1, "anchor without href, no tabindex set" );
  584. });
  585. test( "prop('tabindex', value)", 10, function() {
  586. var clone,
  587. element = jQuery("#divWithNoTabIndex");
  588. equal( element.prop("tabindex"), -1, "start with no tabindex" );
  589. // set a positive string
  590. element.prop( "tabindex", "1" );
  591. equal( element.prop("tabindex"), 1, "set tabindex to 1 (string)" );
  592. // set a zero string
  593. element.prop( "tabindex", "0" );
  594. equal( element.prop("tabindex"), 0, "set tabindex to 0 (string)" );
  595. // set a negative string
  596. element.prop( "tabindex", "-1" );
  597. equal( element.prop("tabindex"), -1, "set tabindex to -1 (string)" );
  598. // set a positive number
  599. element.prop( "tabindex", 1 );
  600. equal( element.prop("tabindex"), 1, "set tabindex to 1 (number)" );
  601. // set a zero number
  602. element.prop( "tabindex", 0 );
  603. equal( element.prop("tabindex"), 0, "set tabindex to 0 (number)" );
  604. // set a negative number
  605. element.prop( "tabindex", -1 );
  606. equal( element.prop("tabindex"), -1, "set tabindex to -1 (number)" );
  607. element = jQuery("#linkWithTabIndex");
  608. equal( element.prop("tabindex"), 2, "start with tabindex 2" );
  609. element.prop( "tabindex", -1 );
  610. equal( element.prop("tabindex"), -1, "set negative tabindex" );
  611. clone = element.clone();
  612. clone.prop( "tabindex", 1 );
  613. equal( clone[ 0 ].getAttribute("tabindex"), "1", "set tabindex on cloned element" );
  614. });
  615. test( "removeProp(String)", function() {
  616. expect( 6 );
  617. var attributeNode = document.createAttribute("irrelevant"),
  618. commentNode = document.createComment("some comment"),
  619. textNode = document.createTextNode("some text"),
  620. obj = {};
  621. strictEqual(
  622. jQuery( "#firstp" ).prop( "nonexisting", "foo" ).removeProp( "nonexisting" )[ 0 ]["nonexisting"],
  623. undefined,
  624. "removeprop works correctly on DOM element nodes"
  625. );
  626. jQuery.each( [ document, obj ], function( i, ele ) {
  627. var $ele = jQuery( ele );
  628. $ele.prop( "nonexisting", "foo" ).removeProp("nonexisting");
  629. strictEqual( ele["nonexisting"], undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." );
  630. });
  631. jQuery.each( [ commentNode, textNode, attributeNode ], function( i, ele ) {
  632. var $ele = jQuery( ele );
  633. $ele.prop( "nonexisting", "foo" ).removeProp("nonexisting");
  634. strictEqual( ele["nonexisting"], undefined, "removeProp works correctly on non DOM element nodes (bug #7500)." );
  635. });
  636. });
  637. test( "val()", function() {
  638. expect( 21 + ( jQuery.fn.serialize ? 6 : 0 ) );
  639. var checks, $button;
  640. document.getElementById("text1").value = "bla";
  641. equal( jQuery("#text1").val(), "bla", "Check for modified value of input element" );
  642. QUnit.reset();
  643. equal( jQuery("#text1").val(), "Test", "Check for value of input element" );
  644. // ticket #1714 this caused a JS error in IE
  645. equal( jQuery("#first").val(), "", "Check a paragraph element to see if it has a value" );
  646. ok( jQuery([]).val() === undefined, "Check an empty jQuery object will return undefined from val" );
  647. equal( jQuery("#select2").val(), "3", "Call val() on a single='single' select" );
  648. deepEqual( jQuery("#select3").val(), [ "1", "2" ], "Call val() on a multiple='multiple' select" );
  649. equal( jQuery("#option3c").val(), "2", "Call val() on a option element with value" );
  650. equal( jQuery("#option3a").val(), "", "Call val() on a option element with empty value" );
  651. equal( jQuery("#option3e").val(), "no value", "Call val() on a option element with no value attribute" );
  652. equal( jQuery("#option3a").val(), "", "Call val() on a option element with no value attribute" );
  653. jQuery("#select3").val("");
  654. deepEqual( jQuery("#select3").val(), [""], "Call val() on a multiple='multiple' select" );
  655. deepEqual( jQuery("#select4").val(), [], "Call val() on multiple='multiple' select with all disabled options" );
  656. jQuery("#select4 optgroup").add("#select4 > [disabled]").attr( "disabled", false );
  657. deepEqual( jQuery("#select4").val(), [ "2", "3" ], "Call val() on multiple='multiple' select with some disabled options" );
  658. jQuery("#select4").attr( "disabled", true );
  659. deepEqual( jQuery("#select4").val(), [ "2", "3" ], "Call val() on disabled multiple='multiple' select" );
  660. equal( jQuery("#select5").val(), "3", "Check value on ambiguous select." );
  661. jQuery("#select5").val( 1 );
  662. equal( jQuery("#select5").val(), "1", "Check value on ambiguous select." );
  663. jQuery("#select5").val( 3 );
  664. equal( jQuery("#select5").val(), "3", "Check value on ambiguous select." );
  665. strictEqual(
  666. jQuery("<select name='select12584' id='select12584'><option value='1' disabled='disabled'>1</option></select>").val(),
  667. null,
  668. "Select-one with only option disabled (#12584)"
  669. );
  670. if ( jQuery.fn.serialize ) {
  671. 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");
  672. deepEqual( checks.serialize(), "", "Get unchecked values." );
  673. equal( checks.eq( 3 ).val(), "on", "Make sure a value of 'on' is provided if none is specified." );
  674. checks.val([ "2" ]);
  675. deepEqual( checks.serialize(), "test=2", "Get a single checked value." );
  676. checks.val([ "1", "" ]);
  677. deepEqual( checks.serialize(), "test=1&test=", "Get multiple checked values." );
  678. checks.val([ "", "2" ]);
  679. deepEqual( checks.serialize(), "test=2&test=", "Get multiple checked values." );
  680. checks.val([ "1", "on" ]);
  681. deepEqual( checks.serialize(), "test=1&test=on", "Get multiple checked values." );
  682. checks.remove();
  683. }
  684. $button = jQuery("<button value='foobar'>text</button>").insertAfter("#button");
  685. equal( $button.val(), "foobar", "Value retrieval on a button does not return innerHTML" );
  686. equal( $button.val("baz").html(), "text", "Setting the value does not change innerHTML" );
  687. equal( jQuery("<option/>").val("test").attr("value"), "test", "Setting value sets the value attribute" );
  688. });
  689. test("val() with non-matching values on dropdown list", function() {
  690. expect( 3 );
  691. jQuery("#select5").val( "" );
  692. equal( jQuery("#select5").val(), null, "Non-matching set on select-one" );
  693. var select6 = jQuery("<select multiple id=\"select6\"><option value=\"1\">A</option><option value=\"2\">B</option></select>").appendTo("#form");
  694. jQuery(select6).val( "nothing" );
  695. equal( jQuery(select6).val(), null, "Non-matching set (single value) on select-multiple" );
  696. jQuery(select6).val( ["nothing1", "nothing2"] );
  697. equal( jQuery(select6).val(), null, "Non-matching set (array of values) on select-multiple" );
  698. select6.remove();
  699. });
  700. if ( "value" in document.createElement("meter") &&
  701. "value" in document.createElement("progress") ) {
  702. test( "val() respects numbers without exception (Bug #9319)", function() {
  703. expect( 4 );
  704. var $meter = jQuery("<meter min='0' max='10' value='5.6'></meter>"),
  705. $progress = jQuery("<progress max='10' value='1.5'></progress>");
  706. try {
  707. equal( typeof $meter.val(), "number", "meter, returns a number and does not throw exception" );
  708. equal( $meter.val(), $meter[ 0 ].value, "meter, api matches host and does not throw exception" );
  709. equal( typeof $progress.val(), "number", "progress, returns a number and does not throw exception" );
  710. equal( $progress.val(), $progress[ 0 ].value, "progress, api matches host and does not throw exception" );
  711. } catch( e ) {}
  712. $meter.remove();
  713. $progress.remove();
  714. });
  715. }
  716. var testVal = function( valueObj ) {
  717. expect( 8 );
  718. QUnit.reset();
  719. jQuery("#text1").val( valueObj("test") );
  720. equal( document.getElementById("text1").value, "test", "Check for modified (via val(String)) value of input element" );
  721. jQuery("#text1").val( valueObj( undefined ) );
  722. equal( document.getElementById("text1").value, "", "Check for modified (via val(undefined)) value of input element" );
  723. jQuery("#text1").val( valueObj( 67 ) );
  724. equal( document.getElementById("text1").value, "67", "Check for modified (via val(Number)) value of input element" );
  725. jQuery("#text1").val( valueObj( null ) );
  726. equal( document.getElementById("text1").value, "", "Check for modified (via val(null)) value of input element" );
  727. var j,
  728. $select1 = jQuery("#select1");
  729. $select1.val( valueObj("3") );
  730. equal( $select1.val(), "3", "Check for modified (via val(String)) value of select element" );
  731. $select1.val( valueObj( 2 ) );
  732. equal( $select1.val(), "2", "Check for modified (via val(Number)) value of select element" );
  733. $select1.append("<option value='4'>four</option>");
  734. $select1.val( valueObj( 4 ) );
  735. equal( $select1.val(), "4", "Should be possible to set the val() to a newly created option" );
  736. // using contents will get comments regular, text, and comment nodes
  737. j = jQuery("#nonnodes").contents();
  738. j.val( valueObj( "asdf" ) );
  739. equal( j.val(), "asdf", "Check node,textnode,comment with val()" );
  740. j.removeAttr("value");
  741. };
  742. test( "val(String/Number)", function() {
  743. testVal( bareObj );
  744. });
  745. test( "val(Function)", function() {
  746. testVal( functionReturningObj );
  747. });
  748. test( "val(Array of Numbers) (Bug #7123)", function() {
  749. expect( 4 );
  750. 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' />");
  751. var elements = jQuery("input[name=arrayTest]").val([ 1, 2 ]);
  752. ok( elements[ 0 ].checked, "First element was checked" );
  753. ok( elements[ 1 ].checked, "Second element was checked" );
  754. ok( !elements[ 2 ].checked, "Third element was unchecked" );
  755. ok( !elements[ 3 ].checked, "Fourth element remained unchecked" );
  756. elements.remove();
  757. });
  758. test( "val(Function) with incoming value", function() {
  759. expect( 10 );
  760. QUnit.reset();
  761. var oldVal = jQuery("#text1").val();
  762. jQuery("#text1").val(function( i, val ) {
  763. equal( val, oldVal, "Make sure the incoming value is correct." );
  764. return "test";
  765. });
  766. equal( document.getElementById("text1").value, "test", "Check for modified (via val(String)) value of input element" );
  767. oldVal = jQuery("#text1").val();
  768. jQuery("#text1").val(function( i, val ) {
  769. equal( val, oldVal, "Make sure the incoming value is correct." );
  770. return 67;
  771. });
  772. equal( document.getElementById("text1").value, "67", "Check for modified (via val(Number)) value of input element" );
  773. oldVal = jQuery("#select1").val();
  774. jQuery("#select1").val(function( i, val ) {
  775. equal( val, oldVal, "Make sure the incoming value is correct." );
  776. return "3";
  777. });
  778. equal( jQuery("#select1").val(), "3", "Check for modified (via val(String)) value of select element" );
  779. oldVal = jQuery("#select1").val();
  780. jQuery("#select1").val(function( i, val ) {
  781. equal( val, oldVal, "Make sure the incoming value is correct." );
  782. return 2;
  783. });
  784. equal( jQuery("#select1").val(), "2", "Check for modified (via val(Number)) value of select element" );
  785. jQuery("#select1").append("<option value='4'>four</option>");
  786. oldVal = jQuery("#select1").val();
  787. jQuery("#select1").val(function( i, val ) {
  788. equal( val, oldVal, "Make sure the incoming value is correct." );
  789. return 4;
  790. });
  791. equal( jQuery("#select1").val(), "4", "Should be possible to set the val() to a newly created option" );
  792. });
  793. // testing if a form.reset() breaks a subsequent call to a select element's .val() (in IE only)
  794. test( "val(select) after form.reset() (Bug #2551)", function() {
  795. expect( 3 );
  796. jQuery("<form id='kk' name='kk'><select id='kkk'><option value='cf'>cf</option><option value='gf'>gf</option></select></form>").appendTo("#qunit-fixture");
  797. jQuery("#kkk").val("gf");
  798. document["kk"].reset();
  799. equal( jQuery("#kkk")[ 0 ].value, "cf", "Check value of select after form reset." );
  800. equal( jQuery("#kkk").val(), "cf", "Check value of select after form reset." );
  801. // re-verify the multi-select is not broken (after form.reset) by our fix for single-select
  802. deepEqual( jQuery("#select3").val(), ["1", "2"], "Call val() on a multiple='multiple' select" );
  803. jQuery("#kk").remove();
  804. });
  805. var testAddClass = function( valueObj ) {
  806. expect( 9 );
  807. var pass, j, i,
  808. div = jQuery("#qunit-fixture div");
  809. div.addClass( valueObj("test") );
  810. pass = true;
  811. for ( i = 0; i < div.length; i++ ) {
  812. if ( !~div.get( i ).className.indexOf("test") ) {
  813. pass = false;
  814. }
  815. }
  816. ok( pass, "Add Class" );
  817. // using contents will get regular, text, and comment nodes
  818. j = jQuery("#nonnodes").contents();
  819. j.addClass( valueObj("asdf") );
  820. ok( j.hasClass("asdf"), "Check node,textnode,comment for addClass" );
  821. div = jQuery("<div/>");
  822. div.addClass( valueObj("test") );
  823. equal( div.attr("class"), "test", "Make sure there's no extra whitespace." );
  824. div.attr( "class", " foo" );
  825. div.addClass( valueObj("test") );
  826. equal( div.attr("class"), "foo test", "Make sure there's no extra whitespace." );
  827. div.attr( "class", "foo" );
  828. div.addClass( valueObj("bar baz") );
  829. equal( div.attr("class"), "foo bar baz", "Make sure there isn't too much trimming." );
  830. div.removeClass();
  831. div.addClass( valueObj("foo") ).addClass( valueObj("foo") );
  832. equal( div.attr("class"), "foo", "Do not add the same class twice in separate calls." );
  833. div.addClass( valueObj("fo") );
  834. equal( div.attr("class"), "foo fo", "Adding a similar class does not get interrupted." );
  835. div.removeClass().addClass("wrap2");
  836. ok( div.addClass("wrap").hasClass("wrap"), "Can add similarly named classes");
  837. div.removeClass();
  838. div.addClass( valueObj("bar bar") );
  839. equal( div.attr("class"), "bar", "Do not add the same class twice in the same call." );
  840. };
  841. test( "addClass(String)", function() {
  842. testAddClass( bareObj );
  843. });
  844. test( "addClass(Function)", function() {
  845. testAddClass( functionReturningObj );
  846. });
  847. test( "addClass(Function) with incoming value", function() {
  848. expect( 52 );
  849. var pass, i,
  850. div = jQuery("#qunit-fixture div"),
  851. old = div.map(function() {
  852. return jQuery(this).attr("class") || "";
  853. });
  854. div.addClass(function( i, val ) {
  855. if ( this.id !== "_firebugConsole" ) {
  856. equal( val, old[ i ], "Make sure the incoming value is correct." );
  857. return "test";
  858. }
  859. });
  860. pass = true;
  861. for ( i = 0; i < div.length; i++ ) {
  862. if ( div.get(i).className.indexOf("test") === -1 ) {
  863. pass = false;
  864. }
  865. }
  866. ok( pass, "Add Class" );
  867. });
  868. var testRemoveClass = function(valueObj) {
  869. expect( 8 );
  870. var $set = jQuery("#qunit-fixture div"),
  871. div = document.createElement("div");
  872. $set.addClass("test").removeClass( valueObj("test") );
  873. ok( !$set.is(".test"), "Remove Class" );
  874. $set.addClass("test").addClass("foo").addClass("bar");
  875. $set.removeClass( valueObj("test") ).removeClass( valueObj("bar") ).removeClass( valueObj("foo") );
  876. ok( !$set.is(".test,.bar,.foo"), "Remove multiple classes" );
  877. // Make sure that a null value doesn't cause problems
  878. $set.eq( 0 ).addClass("expected").removeClass( valueObj( null ) );
  879. ok( $set.eq( 0 ).is(".expected"), "Null value passed to removeClass" );
  880. $set.eq( 0 ).addClass("expected").removeClass( valueObj("") );
  881. ok( $set.eq( 0 ).is(".expected"), "Empty string passed to removeClass" );
  882. // using contents will get regular, text, and comment nodes
  883. $set = jQuery("#nonnodes").contents();
  884. $set.removeClass( valueObj("asdf") );
  885. ok( !$set.hasClass("asdf"), "Check node,textnode,comment for removeClass" );
  886. jQuery( div ).removeClass( valueObj("foo") );
  887. strictEqual( jQuery( div ).attr("class"), undefined, "removeClass doesn't create a class attribute" );
  888. div.className = " test foo ";
  889. jQuery( div ).removeClass( valueObj("foo") );
  890. equal( div.className, "test", "Make sure remaining className is trimmed." );
  891. div.className = " test ";
  892. jQuery( div ).removeClass( valueObj("test") );
  893. equal( div.className, "", "Make sure there is nothing left after everything is removed." );
  894. };
  895. test( "removeClass(String) - simple", function() {
  896. testRemoveClass( bareObj );
  897. });
  898. test( "removeClass(Function) - simple", function() {
  899. testRemoveClass( functionReturningObj );
  900. });
  901. test( "removeClass(Function) with incoming value", function() {
  902. expect( 52 );
  903. var $divs = jQuery("#qunit-fixture div").addClass("test"), old = $divs.map(function() {
  904. return jQuery( this ).attr("class");
  905. });
  906. $divs.removeClass(function( i, val ) {
  907. if ( this.id !== "_firebugConsole" ) {
  908. equal( val, old[ i ], "Make sure the incoming value is correct." );
  909. return "test";
  910. }
  911. });
  912. ok( !$divs.is(".test"), "Remove Class" );
  913. });
  914. test( "removeClass() removes duplicates", function() {
  915. expect( 1 );
  916. var $div = jQuery( jQuery.parseHTML("<div class='x x x'></div>") );
  917. $div.removeClass("x");
  918. ok( !$div.hasClass("x"), "Element with multiple same classes does not escape the wrath of removeClass()" );
  919. });
  920. test("removeClass(undefined) is a no-op", function() {
  921. expect( 1 );
  922. var $div = jQuery("<div class='base second'></div>");
  923. $div.removeClass( undefined );
  924. ok( $div.hasClass("base") && $div.hasClass("second"), "Element still has classes after removeClass(undefined)" );
  925. });
  926. var testToggleClass = function(valueObj) {
  927. expect( 17 );
  928. var e = jQuery("#firstp");
  929. ok( !e.is(".test"), "Assert class not present" );
  930. e.toggleClass( valueObj("test") );
  931. ok( e.is(".test"), "Assert class present" );
  932. e.toggleClass( valueObj("test") );
  933. ok( !e.is(".test"), "Assert class not present" );
  934. // class name with a boolean
  935. e.toggleClass( valueObj("test"), false );
  936. ok( !e.is(".test"), "Assert class not present" );
  937. e.toggleClass( valueObj("test"), true );
  938. ok( e.is(".test"), "Assert class present" );
  939. e.toggleClass( valueObj("test"), false );
  940. ok( !e.is(".test"), "Assert class not present" );
  941. // multiple class names
  942. e.addClass("testA testB");
  943. ok( e.is(".testA.testB"), "Assert 2 different classes present" );
  944. e.toggleClass( valueObj("testB testC") );
  945. ok( (e.is(".testA.testC") && !e.is(".testB")), "Assert 1 class added, 1 class removed, and 1 class kept" );
  946. e.toggleClass( valueObj("testA testC") );
  947. ok( (!e.is(".testA") && !e.is(".testB") && !e.is(".testC")), "Assert no class present" );
  948. // toggleClass storage
  949. e.toggleClass( true );
  950. ok( e[ 0 ].className === "", "Assert class is empty (data was empty)" );
  951. e.addClass("testD testE");
  952. ok( e.is(".testD.testE"), "Assert class present" );
  953. e.toggleClass();
  954. ok( !e.is(".testD.testE"), "Assert class not present" );
  955. ok( jQuery._data(e[ 0 ], "__className__") === "testD testE", "Assert data was stored" );
  956. e.toggleClass();
  957. ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
  958. e.toggleClass( false );
  959. ok( !e.is(".testD.testE"), "Assert class not present" );
  960. e.toggleClass( true );
  961. ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
  962. e.toggleClass();
  963. e.toggleClass( false );
  964. e.toggleClass();
  965. ok( e.is(".testD.testE"), "Assert class present (restored from data)" );
  966. // Cleanup
  967. e.removeClass("testD");
  968. QUnit.expectJqData( e[ 0 ], "__className__" );
  969. };
  970. test( "toggleClass(String|boolean|undefined[, boolean])", function() {
  971. testToggleClass( bareObj );
  972. });
  973. test( "toggleClass(Function[, boolean])", function() {
  974. testToggleClass( functionReturningObj );
  975. });
  976. test( "toggleClass(Function[, boolean]) with incoming value", function() {
  977. expect( 14 );
  978. var e = jQuery("#firstp"),
  979. old = e.attr("class") || "";
  980. ok( !e.is(".test"), "Assert class not present" );
  981. e.toggleClass(function( i, val ) {
  982. equal( old, val, "Make sure the incoming value is correct." );
  983. return "test";
  984. });
  985. ok( e.is(".test"), "Assert class present" );
  986. old = e.attr("class");
  987. e.toggleClass(function( i, val ) {
  988. equal( old, val, "Make sure the incoming value is correct." );
  989. return "test";
  990. });
  991. ok( !e.is(".test"), "Assert class not present" );
  992. old = e.attr("class") || "";
  993. // class name with a boolean
  994. e.toggleClass(function( i, val, state ) {
  995. equal( old, val, "Make sure the incoming value is correct." );
  996. equal( state, false, "Make sure that the state is passed in." );
  997. return "test";
  998. }, false );
  999. ok( !e.is(".test"), "Assert class not present" );
  1000. old = e.attr("class") || "";
  1001. e.toggleClass(function( i, val, state ) {
  1002. equal( old, val, "Make sure the incoming value is correct." );
  1003. equal( state, true, "Make sure that the state is passed in." );
  1004. return "test";
  1005. }, true );
  1006. ok( e.is(".test"), "Assert class present" );
  1007. old = e.attr("class");
  1008. e.toggleClass(function( i, val, state ) {
  1009. equal( old, val, "Make sure the incoming value is correct." );
  1010. equal( state, false, "Make sure that the state is passed in." );
  1011. return "test";
  1012. }, false );
  1013. ok( !e.is(".test"), "Assert class not present" );
  1014. });
  1015. test( "addClass, removeClass, hasClass", function() {
  1016. expect( 17 );
  1017. var jq = jQuery("<p>Hi</p>"), x = jq[ 0 ];
  1018. jq.addClass("hi");
  1019. equal( x.className, "hi", "Check single added class" );
  1020. jq.addClass("foo bar");
  1021. equal( x.className, "hi foo bar", "Check more added classes" );
  1022. jq.removeClass();
  1023. equal( x.className, "", "Remove all classes" );
  1024. jq.addClass("hi foo bar");
  1025. jq.removeClass("foo");
  1026. equal( x.className, "hi bar", "Check removal of one class" );
  1027. ok( jq.hasClass("hi"), "Check has1" );
  1028. ok( jq.hasClass("bar"), "Check has2" );
  1029. jq = jQuery("<p class='class1\nclass2\tcla.ss3\n\rclass4'></p>");
  1030. ok( jq.hasClass("class1"), "Check hasClass with line feed" );
  1031. ok( jq.is(".class1"), "Check is with line feed" );
  1032. ok( jq.hasClass("class2"), "Check hasClass with tab" );
  1033. ok( jq.is(".class2"), "Check is with tab" );
  1034. ok( jq.hasClass("cla.ss3"), "Check hasClass with dot" );
  1035. ok( jq.hasClass("class4"), "Check hasClass with carriage return" );
  1036. ok( jq.is(".class4"), "Check is with carriage return" );
  1037. jq.removeClass("class2");
  1038. ok( jq.hasClass("class2") === false, "Check the class has been properly removed" );
  1039. jq.removeClass("cla");
  1040. ok( jq.hasClass("cla.ss3"), "Check the dotted class has not been removed" );
  1041. jq.removeClass("cla.ss3");
  1042. ok( jq.hasClass("cla.ss3") === false, "Check the dotted class has been removed" );
  1043. jq.removeClass("class4");
  1044. ok( jq.hasClass("class4") === false, "Check the class has been properly removed" );
  1045. });
  1046. test( "contents().hasClass() returns correct values", function() {
  1047. expect( 2 );
  1048. var $div = jQuery("<div><span class='foo'></span><!-- comment -->text</div>"),
  1049. $contents = $div.contents();
  1050. ok( $contents.hasClass("foo"), "Found 'foo' in $contents" );
  1051. ok( !$contents.hasClass("undefined"), "Did not find 'undefined' in $contents (correctly)" );
  1052. });
  1053. test( "hasClass correctly interprets non-space separators (#13835)", function() {
  1054. expect( 4 );
  1055. var
  1056. map = {
  1057. tab: "&#9;",
  1058. "line-feed": "&#10;",
  1059. "form-feed": "&#12;",
  1060. "carriage-return": "&#13;"
  1061. },
  1062. classes = jQuery.map( map, function( separator, label ) {
  1063. return " " + separator + label + separator + " ";
  1064. }),
  1065. $div = jQuery( "<div class='" + classes + "'></div>" );
  1066. jQuery.each( map, function( label ) {
  1067. ok( $div.hasClass( label ), label.replace( "-", " " ) );
  1068. });
  1069. });
  1070. test( "coords returns correct values in IE6/IE7, see #10828", function() {
  1071. expect( 1 );
  1072. var area,
  1073. map = jQuery("<map />");
  1074. area = map.html("<area shape='rect' coords='0,0,0,0' href='#' alt='a' />").find("area");
  1075. equal( area.attr("coords"), "0,0,0,0", "did not retrieve coords correctly" );
  1076. });