attributes.js 52 KB

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