Нет описания

attributes.js 72KB

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